Skip to content

Commit e09f5d5

Browse files
committed
add new test case "reload_application"
1 parent 08b12b4 commit e09f5d5

File tree

5 files changed

+313
-0
lines changed

5 files changed

+313
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>reload application test</title>
6+
</head>
7+
<body>
8+
<p>reload application test</p>
9+
10+
<script src="../res/mocha_util.js"></script>
11+
<script src="mocha_test.js"></script>
12+
13+
</body>
14+
15+
</html>
16+
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
var net = require('net');
24+
25+
/*
26+
0: close window after reload.
27+
1: quit after reload.
28+
2: close window after reloadDev.
29+
3: quit window after reloadDev.
30+
*/
31+
var type = gui.App.argv[0]||0;
32+
var prot = 13013;
33+
var auto = true;
34+
35+
setTimeout(function(){
36+
gui.Window.get().show();
37+
document.body.insertAdjacentHTML('beforeEnd',
38+
'<p>' + global.mydata + 'times. window will hide in 2s.</p>');
39+
}, 1000);
40+
41+
setTimeout(function(){
42+
gui.Window.get().hide();
43+
}, 2000);
44+
45+
setTimeout(function(){
46+
document.body.insertAdjacentHTML('beforeEnd',
47+
'<p> show after reload app' +
48+
', window can be closed and should not crash. </p>');
49+
gui.Window.get().show();
50+
51+
if (auto){
52+
if (type == 0 || type == 1 && global.mydata == 0){
53+
location.reload();
54+
}
55+
56+
if (type == 0 && global.mydata > 0){
57+
gui.Window.get().close();
58+
}
59+
60+
if (type == 1 && global.mydata > 0){
61+
gui.App.quit();
62+
}
63+
64+
if (type == 3 || type == 2){
65+
66+
var client = net.connect({port: prot});
67+
client.setEncoding('utf8');
68+
client.write('open');
69+
client.on('data', function(data){
70+
if (data == 'quit'){
71+
if (type == 3) setTimeout(gui.App.quit, 1000);
72+
if (type == 2) gui.Window.get().close();
73+
}
74+
if (data == 'reload'){
75+
gui.Window.get().reload(3);
76+
}
77+
});
78+
79+
}
80+
81+
}
82+
83+
84+
}, 3000);
85+
86+
</script>
87+
<button onclick="quit()">quit</button>
88+
<br>
89+
<button onclick="closeWindow()">close window</button>
90+
<br>
91+
<button onclick="location.reload()">reload</button>
92+
<br>
93+
<button onclick="gui.Window.get().reload(3)">reloadDev</button>
94+
95+
</body>
96+
</html>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "reload application",
3+
"main": "index.html",
4+
"window": {
5+
"show": false
6+
}
7+
}
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
var assert = require('assert');
2+
var spawn = require('child_process').spawn;
3+
var path = require('path');
4+
var net = require('net');
5+
var fs = require('fs-extra');
6+
var curDir = fs.realpathSync('.');
7+
8+
var cb;
9+
10+
describe('AppTest', function(){
11+
12+
describe('reload app (long-to-run)', function(){
13+
14+
15+
var server;
16+
17+
before(function(done) {
18+
server = createTCPServer(13013);
19+
done();
20+
});
21+
22+
after(function () {
23+
server.close();
24+
});
25+
26+
afterEach(function(){
27+
try {
28+
server.removeAllListeners('connection');
29+
} catch (e) {
30+
}
31+
32+
});
33+
34+
35+
36+
var spawnChild = function(type) {
37+
return spawn(process.execPath, [path.join(curDir, 'internal'), type]);
38+
};
39+
40+
41+
it('close window after reload', function(done){
42+
this.timeout(0);
43+
var result = false;
44+
45+
var app = spawnChild(0);
46+
app.on('exit', function (code){
47+
if (code != 0) return done('error');
48+
result = true;
49+
done();
50+
});
51+
52+
setTimeout(function(){
53+
if (!result) {
54+
app.kill();
55+
done("Timeout, Can not close window.");
56+
}
57+
}, 30000);
58+
59+
});
60+
61+
62+
63+
it('quit app after reload', function(done){
64+
this.timeout(0);
65+
var result = false;
66+
var app = spawnChild(1);
67+
app.on('exit', function (code){
68+
if (code) {
69+
done('error: the error code is: ' + code);
70+
} else {
71+
result = true;
72+
done();
73+
}
74+
});
75+
76+
setTimeout(function(){
77+
if (!result) {
78+
app.kill();
79+
done("Timeout, Can not quit App.");
80+
}
81+
}, 30000);
82+
83+
84+
});
85+
86+
87+
it('close window after reload dev', function(done){
88+
this.timeout(0);
89+
var times = 0;
90+
var result = false;
91+
92+
server.on('connection', cb = function(socket){
93+
94+
//console.log('client connect in 1');
95+
socket.setEncoding('utf8');
96+
socket.on('error', function(er) {
97+
//console.log(er);
98+
});
99+
socket.on('data', function(data){
100+
if (data == 'open'){
101+
if (times == 0){
102+
times += 1;
103+
socket.write('reload');
104+
} else if (times == 1){
105+
socket.write('quit');
106+
}
107+
108+
}// if(data == 'open')
109+
110+
});
111+
});
112+
113+
var app = spawnChild(2);
114+
115+
app.on('exit', function (code){
116+
if (code) {
117+
done('error: the error code is: ' + code);
118+
} else {
119+
result = true;
120+
done();
121+
}
122+
});
123+
124+
125+
setTimeout(function(){
126+
if (!result) {
127+
app.kill();
128+
done("Timeout, Can not close window.");
129+
}
130+
}, 30000);
131+
132+
133+
});
134+
135+
136+
it('quit app after reload dev', function(done){
137+
this.timeout(0);
138+
var times = 0;
139+
var result = false;
140+
141+
server.on('connection', cb = function(socket){
142+
socket.on('error', function(er) {
143+
//console.log(er);
144+
});
145+
//console.log('client connect');
146+
socket.setEncoding('utf8');
147+
socket.on('data', function(data){
148+
if (data == 'open'){
149+
150+
if (times == 0){
151+
times += 1;
152+
socket.write('reload');
153+
} else if (times == 1){
154+
socket.write('quit');
155+
}
156+
157+
}// if(data == 'open')
158+
159+
});
160+
});
161+
162+
var app = spawnChild(3);
163+
164+
app.on('exit', function (code){
165+
if (code != 0) return done('error');
166+
result = true;
167+
done();
168+
});
169+
170+
171+
setTimeout(function(){
172+
if (!result) {
173+
app.kill();
174+
done("Timeout, Can not quit App.");
175+
}
176+
}, 30000);
177+
178+
179+
});
180+
181+
182+
183+
184+
});
185+
186+
});
187+
188+
189+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name":"reload_application_wrapper",
3+
"main":"index.html"
4+
}
5+

0 commit comments

Comments
 (0)