Skip to content

Commit 3f0701b

Browse files
committed
Merge pull request nwjs#474 from owenc4a4/test
Test case for `new-instance`, `loaded event` and fix some problem in test case
2 parents f874e21 + 68fa02c commit 3f0701b

File tree

13 files changed

+251
-9
lines changed

13 files changed

+251
-9
lines changed

tests/app_tests/call_require_with_node-main_set/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
}
1515
catch (e) {
1616
result.ok = false;
17-
result.error = e;
17+
result.error = e.message;
1818
}
1919

2020
var client = require('../../nw_test_app').createClient({
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<html>
2+
<head>
3+
</head>
4+
<body>
5+
<script>
6+
var gui = require('nw.gui');
7+
gui.Window.get().showDevTools();
8+
9+
var times = 0;
10+
11+
function open_window(func) {
12+
var wnd = gui.Window.open("popup.html", {show: false})
13+
14+
wnd.on("loaded",function(){
15+
if (times < 8) {
16+
times += 1;
17+
func(func);
18+
console.log(times + 'wait for next.');
19+
} else {
20+
console.log(times + 'all done!');
21+
22+
var client = require('../../nw_test_app').createClient({
23+
argv: gui.App.argv,
24+
data: "ok"
25+
});
26+
}
27+
28+
});
29+
}
30+
31+
gui.Window.get().on('close', function() {
32+
gui.App.quit();
33+
})
34+
35+
36+
open_window(open_window);
37+
38+
39+
</script>
40+
41+
<button onclick="open_window(open_window)">click</button>
42+
43+
</body>
44+
</html>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
var path = require('path');
3+
var app_test = require('./nw_test_app');
4+
5+
describe('loaded event', function() {
6+
7+
it('loaded event can been fired',
8+
function(done) {
9+
this.timeout(0);
10+
var result = false;
11+
12+
var child = app_test.createChildProcess({
13+
execPath: process.execPath,
14+
appPath: path.join('app_tests', 'loaded_event'),
15+
end: function(data, app) {
16+
result = true;
17+
done();
18+
app.kill();
19+
}
20+
});
21+
22+
23+
setTimeout(function(){
24+
if (!result) {
25+
child.app.kill();
26+
child.removeConnection();
27+
done('loaded evenet does not been fired');
28+
}
29+
}, 3000);
30+
//child.app.stderr.on('data', function(d){ console.log ('app' + d);});
31+
32+
})
33+
34+
35+
})
36+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "nw-demo",
3+
"main": "index.html"
4+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<html>
2+
<head>
3+
</head>
4+
<body>
5+
<script>
6+
7+
8+
</script>
9+
10+
11+
</body>
12+
</html>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<html>
2+
<head>
3+
</head>
4+
<body>
5+
<span>The pid of main page is: </span>
6+
<span id="msg" style="color:Green"></span>
7+
<br/>
8+
<span>The pid of new-instance page is: </span>
9+
<span id="msg2" style="color:Green"></span>
10+
<script>
11+
var gui = require('nw.gui');
12+
document.getElementById('msg').innerHTML = process.pid;
13+
14+
var wnd = gui.Window.open("popup.html", {show: true, 'new-instance': true})
15+
16+
17+
var client = require('../../nw_test_app').createClient({
18+
argv: gui.App.argv,
19+
data: process.pid
20+
});
21+
22+
23+
gui.Window.get().on('close', function() {
24+
gui.App.quit();
25+
})
26+
27+
28+
29+
30+
</script>
31+
32+
33+
</body>
34+
</html>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
var path = require('path');
3+
var app_test = require('./nw_test_app');
4+
var server = global.server;
5+
var cb;
6+
7+
describe('new-instance', function() {
8+
9+
after(function() {
10+
server.removeListener('connection', cb);
11+
});
12+
13+
it('loaded event can been fired', function(done) {
14+
this.timeout(0);
15+
var result = false;
16+
var times = 0;
17+
var pid1, pid2;
18+
19+
var child = app_test.createChildProcess({
20+
execPath: process.execPath,
21+
appPath: path.join('app_tests', 'new-instance'),
22+
no_connect: true,
23+
24+
});
25+
26+
27+
server.on('connection', cb = function(socket){
28+
socket.setEncoding('utf8');
29+
30+
socket.on('data', function(data) {
31+
32+
if (times == 0) {
33+
pid1 = data;
34+
times += 1;
35+
} else {
36+
pid2 = data;
37+
38+
if (pid1 != pid2) {
39+
done();
40+
} else {
41+
done('they are in the same process');
42+
}
43+
44+
child.app.kill();
45+
} // if (times == 0)
46+
});
47+
}); // server.on('connection', cb = function(socket)
48+
49+
50+
})
51+
52+
53+
})
54+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "nw-demo",
3+
"main": "index.html"
4+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<html>
2+
<head>
3+
</head>
4+
<body>
5+
<span>The pid of page is: </span>
6+
<span id="msg" style="color:Green"></span>
7+
<script>
8+
9+
var gui = require('nw.gui');
10+
11+
document.getElementById('msg').innerHTML = process.pid;
12+
13+
var client = require('../../nw_test_app').createClient({
14+
argv: gui.App.argv,
15+
data: process.pid
16+
});
17+
</script>
18+
19+
20+
</body>
21+
</html>

tests/app_tests/plugin/mocha_test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ describe('Plugin', function() {
1919
setTimeout(function(){
2020
if (!result) {
2121
done('nw crash.');
22+
child.removeConnection();
2223
}
23-
}, 3000);
24+
}, 1000);
2425

2526

2627
})

tests/app_tests/snapshot/mocha_test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ describe('snapshot', function() {
4949

5050
setTimeout(function(){
5151
if (!result) {
52-
done('the native code does not been executed')
52+
done('the native code does not been executed');
53+
child.removeConnection();
54+
child.app.kill();
5355
}
5456
}, 3000);
5557
//child.app.stderr.on('data', function(d){ console.log ('app' + d);});

tests/menu/index.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,13 @@
176176
document.body.appendChild(element);
177177
}
178178
}));
179+
180+
sub1.append(new gui.MenuItem({
181+
type: 'checkbox',
182+
checked: true, //<------------------------------------------------!!!!!!
183+
label: 'Test CheckBox'
184+
}));
185+
179186
var sub2 = new gui.Menu();
180187
sub2.append(new gui.MenuItem({
181188
label: 'Test2',

tests/nw_test_app/app_test.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ var port = global.port;
99
var program = require('commander');
1010

1111

12+
/*
13+
* options:
14+
* argv: the argv of nw
15+
* data: (string|json) data to be sended to control
16+
*
17+
*
18+
*/
1219
exports.createClient = function(options) {
1320
var
1421
argv = options.argv;
@@ -30,7 +37,8 @@ exports.createClient = function(options) {
3037
if (program.auto) {
3138
var client = net.connect({port: client_prot});
3239
client.setEncoding('utf8');
33-
40+
41+
if (options.data != null) {
3442
if (options.delay) {
3543
setTimeout(function(){
3644
client.end(JSON.stringify(options.data));
@@ -40,6 +48,8 @@ exports.createClient = function(options) {
4048
client.end(JSON.stringify(options.data));
4149
}
4250

51+
}
52+
4353
}
4454
return {'auto': program.auto};
4555
}
@@ -52,20 +62,31 @@ exports.init = function(arg1, arg2) {
5262
function childProcess() {
5363
this.socket = null;
5464
this.app = null;
55-
65+
this.server_connection_cb = null;
5666
}
5767

58-
childProcess.prototype.a = function(cb) {
59-
this.socket.on('data', cb(data));
68+
childProcess.prototype.removeConnection = function() {
69+
try {
70+
server.removeListener('connection', this.server_connection_cb);
71+
} catch (e) {
72+
}
6073
}
6174

6275
/*
6376
* options:
6477
* execPath: (string)the path of nw.
6578
* appPath: (string)the path of app.
79+
*
6680
* end: (function)we should do the report here, after get child process's result.
6781
* data: JSON object
6882
* app: nodejs childProcess.spawn
83+
* no_connect: (bool) whether listen the connection event
84+
*
85+
* notices:
86+
* now we can only receive data one time.
87+
* if your client exists error then crash, and casue the data can not been
88+
* sended, please call `removeConnection()`. otherwise there would be problem.
89+
*
6990
*/
7091
exports.createChildProcess = function(options) {
7192

@@ -78,7 +99,8 @@ exports.createChildProcess = function(options) {
7899
app, cb,
79100
no_connect = options.no_connect || false,
80101
child = new childProcess();
81-
102+
103+
82104
if (!no_connect) {
83105

84106
server.on('connection', cb = function(socket){
@@ -91,7 +113,8 @@ exports.createChildProcess = function(options) {
91113
server.removeListener('connection', cb);
92114
});
93115

94-
}); //server.on()
116+
}); //server.on()
117+
child.server_connection_cb = cb;
95118
} //if (no_conect)
96119

97120
app = spawn(execPath, exec_argv);

0 commit comments

Comments
 (0)