diff --git a/tests/app_tests/calls_across_window/index.html b/tests/app_tests/calls_across_window/index.html new file mode 100644 index 0000000000..328f50329f --- /dev/null +++ b/tests/app_tests/calls_across_window/index.html @@ -0,0 +1,134 @@ + + + + + + + + +
+ + + + diff --git a/tests/app_tests/calls_across_window/mocha_test.js b/tests/app_tests/calls_across_window/mocha_test.js new file mode 100644 index 0000000000..c567b74050 --- /dev/null +++ b/tests/app_tests/calls_across_window/mocha_test.js @@ -0,0 +1,77 @@ +var assert = require('assert'); +var spawn = require('child_process').spawn; +var path = require('path'); +var net = require('net'); +var server = global.server + +describe('AppTest', function(){ + describe('call across window', function(){ + + var app; + var exec_argv; + var socket; + before(function(done){ + //this.timeout(0); + exec_argv = [path.join('app_tests', 'calls_across_window'), + '--port', + global.port]; + + if (global.auto) exec_argv.push('--auto'); + + server.on('connection', function(s){ + socket = s; + s.setEncoding('utf8'); + s.on('end', function(){ + console.log('client dissconnect'); + }); + done(); + }); + app = spawn(process.execPath, exec_argv); + + }) + + after(function(done){ + this.timeout(0); + /* + server.removeAllListeners('connection'); + app.on('exit', function(c){ + done(); + }); + */ + app.kill(); + done(); + }) + + afterEach(function(){ + //console.log('before Each'); + socket.removeAllListeners('data'); + }) + + it ('nw window function call', function(done) { + + socket.on('data', function(data) { + if (data == 'ok') { + done(); + } else { + done(data); + } + }); + socket.write('newWindow'); + + }) + + it ('brower window function call', function(done) { + //this.timeout(0); + socket.on('data', function(data) { + if (data == 'ok') { + done(); + } else { + done(data); + } + }); + socket.write('newBrowserWindow'); + + }) + + }) +}) diff --git a/tests/app_tests/calls_across_window/package.json b/tests/app_tests/calls_across_window/package.json new file mode 100644 index 0000000000..59f6f7a921 --- /dev/null +++ b/tests/app_tests/calls_across_window/package.json @@ -0,0 +1,4 @@ +{ + "name": "nw-test", + "main": "index.html" +} diff --git a/tests/app_tests/calls_across_window/test.html b/tests/app_tests/calls_across_window/test.html new file mode 100644 index 0000000000..fd0faf2e63 --- /dev/null +++ b/tests/app_tests/calls_across_window/test.html @@ -0,0 +1,32 @@ + + + + + + +
+ +

+ + diff --git a/tests/app_tests/htmltest_should_score_452/package.json b/tests/app_tests/htmltest_should_score_452/package.json new file mode 100644 index 0000000000..6907c2045c --- /dev/null +++ b/tests/app_tests/htmltest_should_score_452/package.json @@ -0,0 +1,4 @@ +{ + "name": "nw-test", + "main": "http://html5test.com" +} diff --git a/tests/app_tests/reload_application/index.html b/tests/app_tests/reload_application/index.html new file mode 100644 index 0000000000..3f86648d4e --- /dev/null +++ b/tests/app_tests/reload_application/index.html @@ -0,0 +1,106 @@ + + + + + + + + + +
+ +
+ +
+ + + + diff --git a/tests/app_tests/reload_application/mocha_test.js b/tests/app_tests/reload_application/mocha_test.js new file mode 100644 index 0000000000..94f517b142 --- /dev/null +++ b/tests/app_tests/reload_application/mocha_test.js @@ -0,0 +1,182 @@ +var assert = require('assert'); +var spawn = require('child_process').spawn; +var path = require('path'); +var net = require('net'); +var server = global.server; + +describe('AppTest', function(){ + + describe('reload app', function(){ + + var exec_argv; + + beforeEach(function(){ + exec_argv = [path.join('app_tests', 'reload_application'), + '--port', + global.port]; + + if (global.auto) exec_argv.push('--auto'); + + }); + + afterEach(function(){ + server.removeAllListeners('connection'); + }); + + it('close window after reload', function(done){ + this.timeout(0); + exec_argv.push('--type'); + exec_argv.push(0); + server.on('connection', function(socket){ + socket.on('end', function(){ + //console.log('client disconnect'); + }); + + console.log('client connect'); + socket.setEncoding('utf8'); + socket.on('data', function(data){ + }); + }); + + var app = spawn(process.execPath, exec_argv); + app.on('exit', function (code){ + if (code != 0) return done('error'); + //console.log('end'); + done(); + }); + + if (global.auto){ + setTimeout(function(){ + + app.kill(); + done("Timeout, Can not close window."); + }, 10000); + } + + + + + }) + + + it('quit app after reload', function(done){ + this.timeout(0); + exec_argv.push('--type'); + exec_argv.push(1); + server.on('connection', function(socket){ + socket.on('end', function(){ + }); + + console.log('client connect'); + socket.setEncoding('utf8'); + socket.on('data', function(data){ + }); + }); + + var app = spawn(process.execPath, exec_argv); + + app.on('exit', function (code){ + if (code != 0) return done('error'); + //console.log('in 2'); + done(); + }); + + if (global.auto){ + setTimeout(function(){ + app.kill(); + done("Timeout, Can not quit App."); + }, 10000); + } + + }) + + it('close window after reload dev', function(done){ + this.timeout(0); + exec_argv.push('--type'); + exec_argv.push(2); + var times = 0; + server.on('connection', function(socket){ + socket.on('end', function(){ + //console.log('client diss'); + }); + + //console.log('client connect'); + socket.setEncoding('utf8'); + socket.on('data', function(data){ + if (data == 'open'){ + + if (times == 0){ + times += 1; + socket.write('reload'); + } else if (times == 1){ + socket.write('quit'); + } + + }// if(data == 'open') + + }); + }); + + var app = spawn(process.execPath, exec_argv); + + app.on('exit', function (code){ + if (code != 0) return done('error'); + done(); + }); + + if (global.auto && 1 == 2){ + setTimeout(function(){ + app.kill(); + done("Timeout, Can not close window."); + }, 10000); + } + + }) + + it('quit app after reload dev', function(done){ + this.timeout(0); + exec_argv.push('--type'); + exec_argv.push(3); + var times = 0; + server.on('connection', function(socket){ + socket.on('end', function(){ + //console.log('client diss'); + }); + + //console.log('client connect'); + socket.setEncoding('utf8'); + socket.on('data', function(data){ + if (data == 'open'){ + + if (times == 0){ + times += 1; + socket.write('reload'); + } else if (times == 1){ + socket.write('quit'); + } + + }// if(data == 'open') + + }); + }); + + var app = spawn(process.execPath, exec_argv); + + app.on('exit', function (code){ + if (code != 0) return done('error'); + done(); + }); + + if (global.auto){ + setTimeout(function(){ + app.kill(); + done("Timeout, Can not quit App."); + }, 10000); + } + + }) + + + }) +}) + diff --git a/tests/app_tests/reload_application/package.json b/tests/app_tests/reload_application/package.json new file mode 100644 index 0000000000..3323c9b893 --- /dev/null +++ b/tests/app_tests/reload_application/package.json @@ -0,0 +1,7 @@ +{ + "name": "reload application", + "main": "index.html", + "window": { + "show": false + } +} diff --git a/tests/index.html b/tests/index.html index 08bdcf1bef..9deffd54dd 100644 --- a/tests/index.html +++ b/tests/index.html @@ -32,13 +32,18 @@ var gui = require('nw.gui'); var path = require('path'); var program = require('commander'); - + + //socket server + var net = require('net'); + global.server = net.createServer(); + var server = global.server; // TODO // 1) Make user choose which test to run in command line. // 2) Add option to print results to file. // Hook process.exit to App.quit. process.exit = function() { + server.close(); gui.App.quit(); } @@ -54,6 +59,7 @@ .option('-s, --slow ', '"slow" test threshold in milliseconds [75]') .option('-b, --bail', "bail after first test failure") .option('-A, --async-only', "force all tests to take a callback (async)") + .option('-p, --port ', "set the port used by socket") .parse([ 'node-webkit', 'nw-test' ].concat(gui.App.argv)); // --silent @@ -80,16 +86,19 @@ process.stdout.write = function(data) { fs.writeFileSync(program.output, data, 'utf8'); } + + global.auto = true; + + global.port = 13013; + if (program.port) global.port = program.port; + // Call this to set 'describe', 'it' and else. mocha.setup('bdd'); // Read all test files. - fs.readdir('tests', function(err, dirs) { - if (err) - throw err; - - for (var i = 0; i < dirs.length; ++i) { + var dirs = fs.readdirSync('tests'); + for (var i = 0; i < dirs.length; ++i) { var test_file_path = path.join('tests', dirs[i], dirs[i] + '.js'); var content = fs.readFileSync(test_file_path); @@ -101,12 +110,43 @@ eval('(function() {' + content + '})();'); } - // Run! - if (program.silent) - mocha.run(gui.App.quit); - else - mocha.run(); + // Read app test files. + dirs = fs.readdirSync('app_tests'); + for (var i = 0; i < dirs.length;i++){ + var test_file_path = path.join('app_tests',dirs[i], 'mocha_test.js'); + + if (fs.existsSync(test_file_path)){ + var content = fs.readFileSync(test_file_path); + eval('(function() {' + content + '})();'); + } + } + + + server.on('listening', function(){ + + // Run! + if (program.silent) + mocha.run(gui.App.quit); + else + mocha.run(); + + }); + + server.on('error', function(e){ + if (e.code == 'EADDRINUSE'){ + console.log('Port in use, retrying new..'); + server.close(); + global.port += 1; + server.listen(global.port); + } + }); + + server.listen(global.port); + + + + diff --git a/tests/tests/website/website.js b/tests/tests/website/website.js index 9b529eaeed..5964d64f9a 100644 --- a/tests/tests/website/website.js +++ b/tests/tests/website/website.js @@ -8,9 +8,18 @@ describe('website', function() { this.timeout(0); var win = gui.Window.open('http://html5test.com', { show: false }); win.on('loaded', function() { - var score = win.window.document.getElementById('results').childNodes[0].childNodes[1].innerHTML;; - assert.equal(score >= 445, true); - done(); + var results = win.window.document.getElementById('results'); + if (results == null){ + done('Can not connect to the web'); + return; + } + var score = results.childNodes[0].childNodes[1].innerHTML;; + if (score >= 445) { + done(); + } else { + done('have a low score'); + } + }); gui.Window.get().on('close', function() { win.close(true);