Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8e69cd1

Browse files
committed
Merge pull request nwjs#311 from owenc4a4/test_case
[test]reload app, cross window call, automatic app test
2 parents 06a399f + e979cb5 commit 8e69cd1

10 files changed

Lines changed: 609 additions & 14 deletions

File tree

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<html>
2+
<head>
3+
<script type="text/javascript" src="../../zepto.min.js"></script>
4+
<script type="text/javascript">
5+
$(document).ready(function(){
6+
$("body").append('<p>Main</p>');
7+
});
8+
var data;
9+
function showMess(){
10+
$("body").append('<p>Main</p>');
11+
}
12+
</script>
13+
</head>
14+
<body>
15+
<script>
16+
var gui = require('nw.gui');
17+
var program = require('../../node_modules/commander');
18+
var net = require('net');
19+
//command line arguments.
20+
program
21+
.option('--auto', 'run app automatic')
22+
.option('-p, --port <port>', "set port used by socket")
23+
.parse([ 'node-webkit', 'nw-test' ].concat(gui.App.argv));
24+
25+
var prot = 13013;
26+
if (program.port) port = program.port;
27+
28+
29+
var windows = [];
30+
var winattr = {x: 50, y: 50, width: 300, height: 300,
31+
frame: false, toolbar: false};
32+
33+
function next(win, arr) {
34+
winattr.x += 400;
35+
if (winattr.x > 1600) { winattr.x = 50; }
36+
arr.push(win);
37+
}
38+
39+
function newWindow(){
40+
var win = gui.Window.open('test.html', winattr);
41+
next(win, windows);
42+
return win;
43+
}
44+
45+
function newBrowserWindow() {
46+
var win = window.open('test.html', null, 'screenX='+winattr.x+
47+
',screenY='+winattr.y+',width=400,height=300');
48+
win = gui.Window.get(win);
49+
next(win, windows);
50+
return win;
51+
}
52+
53+
function fourTimes(f){
54+
for (var i = 0; i < 4; i++) f();
55+
}
56+
57+
function testWindows(){
58+
for (var i = 0; i < windows.length; i++){
59+
if (windows[i].test) windows[i].test();
60+
}
61+
}
62+
63+
64+
gui.Window.get().on('close', function() {
65+
for (var i = 0; i < windows.length; i++) windows[i].close(true);
66+
this.close(true);
67+
});
68+
69+
70+
if (program.auto){
71+
var client = net.connect({port: port});
72+
client.setEncoding('utf8');
73+
74+
client.on('data', function(data) {
75+
if (data == 'newWindow') {
76+
var win = newWindow();
77+
78+
win.on('loaded', function() {
79+
if (win.test) {
80+
win.test();
81+
82+
if (win.isTestSuccess()) {
83+
client.write('ok');
84+
} else {
85+
client.write('call does not success');
86+
}
87+
88+
} else {
89+
client.write('call does not exist');
90+
}// if (win.test)
91+
win.close();
92+
});
93+
94+
}//if (data == 'newWindow')
95+
96+
if (data == 'newBrowserWindow') {
97+
var win;
98+
try {
99+
win = newBrowserWindow();
100+
}
101+
catch (e){
102+
client.write('there is error in creating window');
103+
return;
104+
}
105+
win.on('loaded', function(){
106+
if (win.test) {
107+
win.test();
108+
109+
if (win.isTestSuccess()) {
110+
client.write('ok');
111+
} else {
112+
client.write('call does not success');
113+
}
114+
115+
} else {
116+
client.write('call does not exist');
117+
}// if (win.test)
118+
win.close();
119+
});
120+
121+
122+
}//if (data == 'newBrowserWindow')
123+
124+
});
125+
}
126+
127+
128+
</script>
129+
<button onclick="fourTimes(newWindow)">New gui windows</button>
130+
<button onclick="fourTimes(newBrowserWindow)">New browser windows</button><br>
131+
<button onclick="testWindows()">Test windows</button>
132+
<button onclick="gui.App.quit()">quit</button>
133+
</body>
134+
</html>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
var assert = require('assert');
2+
var spawn = require('child_process').spawn;
3+
var path = require('path');
4+
var net = require('net');
5+
var server = global.server
6+
7+
describe('AppTest', function(){
8+
describe('call across window', function(){
9+
10+
var app;
11+
var exec_argv;
12+
var socket;
13+
before(function(done){
14+
//this.timeout(0);
15+
exec_argv = [path.join('app_tests', 'calls_across_window'),
16+
'--port',
17+
global.port];
18+
19+
if (global.auto) exec_argv.push('--auto');
20+
21+
server.on('connection', function(s){
22+
socket = s;
23+
s.setEncoding('utf8');
24+
s.on('end', function(){
25+
console.log('client dissconnect');
26+
});
27+
done();
28+
});
29+
app = spawn(process.execPath, exec_argv);
30+
31+
})
32+
33+
after(function(done){
34+
this.timeout(0);
35+
/*
36+
server.removeAllListeners('connection');
37+
app.on('exit', function(c){
38+
done();
39+
});
40+
*/
41+
app.kill();
42+
done();
43+
})
44+
45+
afterEach(function(){
46+
//console.log('before Each');
47+
socket.removeAllListeners('data');
48+
})
49+
50+
it ('nw window function call', function(done) {
51+
52+
socket.on('data', function(data) {
53+
if (data == 'ok') {
54+
done();
55+
} else {
56+
done(data);
57+
}
58+
});
59+
socket.write('newWindow');
60+
61+
})
62+
63+
it ('brower window function call', function(done) {
64+
//this.timeout(0);
65+
socket.on('data', function(data) {
66+
if (data == 'ok') {
67+
done();
68+
} else {
69+
done(data);
70+
}
71+
});
72+
socket.write('newBrowserWindow');
73+
74+
})
75+
76+
})
77+
})
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "nw-test",
3+
"main": "index.html"
4+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<html>
2+
<head>
3+
<script type="text/javascript" src="../../zepto.min.js"></script>
4+
<script type="text/javascript">
5+
var gui = require('nw.gui');
6+
var win = gui.Window.get();
7+
8+
function test2(){
9+
}
10+
11+
gui.Window.get().test = function(){
12+
document.getElementById('msg').innerHTML = 'Test called';
13+
}
14+
15+
gui.Window.get().isTestSuccess = function(){
16+
if (document.getElementById('msg').innerHTML == 'Test called') {
17+
return true;
18+
} else {
19+
return false;
20+
}
21+
}
22+
23+
</script>
24+
</head>
25+
<body>
26+
<button onclick="win.test()"> click</button> <br>
27+
<script type="text/javascript">
28+
document.write('Window id='+win.id+' routing_id='+win.routing_id);
29+
</script>
30+
<p id="msg" style='color: blue'></p>
31+
</body>
32+
</html>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "nw-test",
3+
"main": "http://html5test.com"
4+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<html>
2+
<head>
3+
<script>
4+
var gui = require('nw.gui');
5+
function quit(){
6+
gui.App.quit();
7+
}
8+
function closeWindow(){
9+
gui.Window.get().close();
10+
}
11+
</script>
12+
</head>
13+
14+
<body>
15+
16+
<script>
17+
18+
if (global.mydata == undefined){
19+
global.mydata = 0;
20+
}else{
21+
global.mydata = global.mydata + 1;
22+
}
23+
24+
25+
var program = require('../../node_modules/commander');
26+
var net = require('net');
27+
//command line arguments.
28+
program
29+
.option('--auto', 'run app automatic')
30+
.option('-p, --port <port>', "set port used by socket")
31+
.option('-t, --type <n>', "type")
32+
.parse([ 'node-webkit', 'nw-test' ].concat(gui.App.argv));
33+
34+
/*
35+
0: close window after reload.
36+
1: quit after reload.
37+
2: close window after reloadDev.
38+
3: quit window after reloadDev.
39+
*/
40+
var type = -1;
41+
var prot = 13013;
42+
if (program.type && program.type >= 0 && program.type < 4) type = program.type;
43+
if (program.auto) console.log('auto');
44+
if (program.port) port = program.port;
45+
46+
setTimeout(function(){
47+
gui.Window.get().show();
48+
document.body.insertAdjacentHTML('beforeEnd',
49+
'<p>' + global.mydata + 'times. window will hide in 2s.</p>');
50+
}, 1000);
51+
52+
setTimeout(function(){
53+
gui.Window.get().hide();
54+
}, 2000);
55+
56+
setTimeout(function(){
57+
document.body.insertAdjacentHTML('beforeEnd',
58+
'<p> show after reload app' +
59+
', window can be closed and should not crash. </p>');
60+
gui.Window.get().show();
61+
62+
if (program.auto){
63+
if (type == 0 || type == 1 && global.mydata == 0){
64+
location.reload();
65+
}
66+
67+
if (type == 0 && global.mydata > 0){
68+
gui.Window.get().close();
69+
}
70+
71+
if (type == 1 && global.mydata > 0){
72+
gui.App.quit();
73+
}
74+
75+
if (type == 3 || type == 2){
76+
var client = net.connect({port: port});
77+
client.setEncoding('utf8');
78+
client.write('open');
79+
client.on('data', function(data){
80+
if (data == 'quit'){
81+
if (type == 3) gui.App.quit();
82+
if (type == 2) gui.Window.get().close();
83+
}
84+
if (data == 'reload'){
85+
gui.Window.get().reload(3);
86+
}
87+
});
88+
89+
}
90+
91+
}
92+
93+
94+
}, 3000);
95+
96+
</script>
97+
<button onclick="quit()">quit</button>
98+
<br>
99+
<button onclick="closeWindow()">close window</button>
100+
<br>
101+
<button onclick="location.reload()">reload</button>
102+
<br>
103+
<button onclick="gui.Window.get().reload(3)">reloadDev</button>
104+
105+
</body>
106+
</html>

0 commit comments

Comments
 (0)