From 9b8cef815bc76062145cc02d4e1bc9db0501df9d Mon Sep 17 00:00:00 2001 From: Santiago Gimeno Date: Mon, 26 Nov 2012 16:34:54 +0100 Subject: [PATCH] A couple of fixes in xtest extension - Return directly in case of error retrieving the extension. - QueryVersion should be GetVersion and fix its format. - Add a test for GetVersion. - Modify test-runner.js so it uses async library to add dynamically the test files to be used. --- lib/x11/ext/xtest.js | 12 +++++---- package.json | 5 ++-- test-runner.js | 63 +++++++++++++++++++++++++++++++++----------- test/xtest.js | 40 ++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+), 23 deletions(-) create mode 100644 test/xtest.js diff --git a/lib/x11/ext/xtest.js b/lib/x11/ext/xtest.js index 82b0c90..689c0b6 100644 --- a/lib/x11/ext/xtest.js +++ b/lib/x11/ext/xtest.js @@ -8,16 +8,18 @@ exports.requireExt = function(display, callback) X.QueryExtension('XTEST', function(err, ext) { if (!ext.present) - callback(new Error('extension not available')); + return callback(new Error('extension not available')); - ext.QueryVersion = function(clientMaj, clientMin, callback) + ext.GetVersion = function(clientMaj, clientMin, callback) { X.seq_num++; - X.pack_stream.pack('CCSLL', [ext.majorOpcode, 0, 3, clientMaj, clientMin]); + X.pack_stream.pack('CCSCxS', [ext.majorOpcode, 0, 2, clientMaj, clientMin]); X.replies[X.seq_num] = [ function(buf, opt) { - var res = buf.unpack('LL'); - return res; + var res = buf.unpack('S'); + // Major version is in byte 1 of Reply Header + // Minor version is in the body of the reply + return [ opt, res[0] ]; }, callback ]; diff --git a/package.json b/package.json index fec97b6..5c2b9aa 100644 --- a/package.json +++ b/package.json @@ -12,12 +12,13 @@ , "bugs" : { "url" : "http://github.com/sidorares/node-x11/issues" } , "licenses" : [ { "type" : "MIT" } ] , "repository" : { "type" : "git", "url" : "http://github.com/sidorares/node-x11.git" } - + , "main" : "./lib/x11" , "engines" : { "node" : "*" } , "devDependencies": { "mocha": "*", - "should": "*" + "should": "*", + "async": "*" } , "scripts": { "test": "node test-runner.js", diff --git a/test-runner.js b/test-runner.js index 4f6a2f4..93b303e 100644 --- a/test-runner.js +++ b/test-runner.js @@ -3,9 +3,10 @@ var Mocha = require('mocha'); var fs = require('fs'); var path = require('path'); var util = require('util'); +var async = require('async'); var mocha = new Mocha({ - timeout : 80000 + timeout : 80000 }); // To be able to perform the tests we need the server: @@ -40,19 +41,49 @@ var run_dpms_test = function(cb) { }); }; -// Add all files from test root directory -fs.readdirSync('./test').forEach(function(file) { - if (file === 'dpms.js') { - run_dpms_test(function(run) { - if (run) { - mocha.addFile(path.join('./test', file)); - } +var run_xtest_test = function(cb) { + var client = x11.createClient(function(dpy) { + var display = dpy; + var X = display.client; + X.require('dpms', function(ext) { + if (!util.isError(ext)) cb(true); + else cb(false); + }); + }); - mocha.run(function(){ - process.exit(); - }); - }); - } else { - mocha.addFile(path.join('./test', file)); - } -}); \ No newline at end of file + client.on('error', function() { + cb(false); + }); +}; + +// Add all files from test root directory +async.forEach( + fs.readdirSync('./test'), + function(file, cb) { + if (file === 'dpms.js') { + run_dpms_test(function(run) { + if (run) { + mocha.addFile(path.join('./test', file)); + } + + cb(); + }); + } else if (file === 'xtest.js') { + run_xtest_test(function(run) { + if (run) { + mocha.addFile(path.join('./test', file)); + } + + cb(); + }); + } else { + mocha.addFile(path.join('./test', file)); + cb(); + } + }, + function(err) { + mocha.run(function() { + process.exit(); + }); + } +); diff --git a/test/xtest.js b/test/xtest.js new file mode 100644 index 0000000..3d018af --- /dev/null +++ b/test/xtest.js @@ -0,0 +1,40 @@ +var x11 = require('../lib/x11'); +var should = require('should'); +var assert = require('assert'); +var util = require('util'); + +describe('XTEST extension', function() { + var display; + var X; + var xtest; + before(function(done) { + var client = x11.createClient(function(dpy) { + display = dpy; + X = display.client; + X.require('xtest', function(ext) { + if (util.isError(ext)) { + done(ext); + } else { + xtest = ext; + done(); + } + }); + }); + + client.on('error', done); + }); + + describe('GetVersion', function() { + it('should return version 2.2', function(done) { + xtest.GetVersion(2, 2, function(err, version) { + version.should.eql([2, 2]); + done(); + }); + }); + }); + + after(function(done) { + X.terminate(); + X.on('end', done); + }); +});