Skip to content

Commit 1fb87ea

Browse files
committed
Append test cases
1 parent b5d2137 commit 1fb87ea

File tree

27 files changed

+842
-0
lines changed

27 files changed

+842
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
var result = result ||[];
2+
result.push('inject-js-end');
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script type="text/javascript">
9+
var result = result||[];
10+
result.push("script-start");
11+
12+
setTimeout(function(){
13+
var gui = require('nw.gui');
14+
var port = gui.App.argv[0]||13013;
15+
var client = require('net').createConnection(port);
16+
console.log(JSON.stringify(result));
17+
client.write(JSON.stringify(result));
18+
},1000);
19+
</script>
20+
</body>
21+
</html>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
var path = require('path');
2+
var spawn = require('child_process').spawn;
3+
var server = global.server;
4+
var server_port = global.port;
5+
var assert = require('assert');
6+
7+
8+
9+
describe('inject-js',function(){
10+
var result = null;
11+
var child = null;
12+
13+
var connection_handler_callback = undefined;
14+
var connection_handler = function(socket){
15+
socket.on('data',function(data){
16+
result = JSON.parse(data);
17+
if (typeof connection_handler_callback === 'function'){
18+
(connection_handler_callback)();
19+
}
20+
});
21+
};
22+
23+
before(function(done){
24+
this.timeout(0);
25+
var app_path = path.join(global.tests_dir,'inject-js');
26+
var exec_argv = [app_path,server_port];
27+
child = spawn(process.execPath,exec_argv);
28+
server.on('connection',connection_handler);
29+
connection_handler_callback = done;
30+
});
31+
32+
after(function(done){
33+
server.removeListener('connection',connection_handler);
34+
child.kill();
35+
done();
36+
});
37+
38+
it('result.length should equal 4',function(done){
39+
assert.notEqual(result,null);
40+
assert.equal(result.length,4);
41+
done();
42+
});
43+
it('inject-js-start should run first',function(done){
44+
assert.equal(result[0],'inject-js-start');
45+
done();
46+
});
47+
it('document script should run after inject-js-start',function(done){
48+
assert.equal(result[1],'script-start');
49+
done();
50+
});
51+
it('inject-js-end should before window.onload',function(done){
52+
assert.equal(result[2],'inject-js-end');
53+
done();
54+
});
55+
it('inject-js-start should run last',function(done){
56+
assert.equal(result[3],'onload');
57+
done();
58+
});
59+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name":"nw_1403514049",
3+
"main":"index.html",
4+
"inject-js-end":"./end.js",
5+
"inject-js-start":"./start.js"
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var result = result ||[];
2+
result.push('inject-js-start');
3+
4+
window.onload = function(){
5+
result.push('onload')
6+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<h1>Iframe</h1>
9+
<script type="text/javascript">
10+
global.results.push("iframe-script");
11+
</script>
12+
</body>
13+
</html>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf8">
5+
<title>test</title>
6+
</head>
7+
<body>
8+
<h1>it works!</h1>
9+
<script type="text/javascript">
10+
//start js code here
11+
var gui = require('nw.gui');
12+
var port = gui.App.argv[0]||13013;
13+
global.results = [];
14+
15+
var new_win = gui.Window.open('new_win.html');
16+
new_win.on('document-start',function(frame){
17+
global.results.push({
18+
"name":'top-window-document-start',
19+
"flag":frame === null
20+
});
21+
});
22+
new_win.on('document-end',function(frame){
23+
global.results.push({
24+
"name":'top-window-document-end',
25+
"flag":frame === null
26+
});
27+
});
28+
29+
setTimeout(function(){
30+
new_win.close();
31+
new_win = undefined;
32+
33+
var iframe = document.createElement('iframe');
34+
document.body.appendChild(iframe);
35+
var win = gui.Window.get();
36+
win.on('document-start',function(frame){
37+
global.results.push({
38+
"name":"iframe-document-start",
39+
"flag":frame === iframe
40+
});
41+
});
42+
win.on('document-end',function(frame){
43+
global.results.push({
44+
"name":"iframe-document-end",
45+
"flag":frame === iframe
46+
});
47+
});
48+
iframe.onload = function(){
49+
global.results.push("onload-from-iframe");
50+
setTimeout(function(){
51+
var client = require('net').createConnection(port);
52+
client.write(JSON.stringify(global.results));
53+
},1000);
54+
};
55+
iframe.src = "iframe.html";
56+
},1000);
57+
</script>
58+
</body>
59+
</html>
60+
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
var path = require('path');
2+
var spawn = require('child_process').spawn;
3+
var server = global.server;
4+
var server_port = global.port;
5+
var assert = require('assert');
6+
7+
describe('document-start/end',function(){
8+
9+
var results = undefined;
10+
var connection_handler_callback = undefined;
11+
var connection_handler = function(socket){
12+
socket.on('data',function(data){
13+
results = JSON.parse(data+"");
14+
if (typeof connection_handler_callback == 'function'){
15+
(connection_handler_callback)();
16+
}
17+
})
18+
};
19+
var child = null;
20+
before(function(done){
21+
this.timeout(0);
22+
server.on('connection',connection_handler);
23+
var app_path = path.join(global.tests_dir,'window-document-event');
24+
var exec_argv = [app_path,server_port];
25+
child = spawn(process.execPath,exec_argv);
26+
connection_handler_callback = done;
27+
//we call done when we receive data from child
28+
});
29+
after(function(done){
30+
server.removeListener('connection',connection_handler);
31+
child.kill();
32+
done();
33+
});
34+
35+
it('results should not equal undefined',function(done){
36+
assert.notEqual(results,undefined);
37+
done();
38+
})
39+
40+
it('new window document-start run first',function(done){
41+
assert.equal(results[0]['flag'],true)
42+
assert.equal(results[0]['name'],'top-window-document-start')
43+
done();
44+
});
45+
it('new window script run between document-start and document-end',function(done){
46+
assert.equal(results[1],'new-window-script');
47+
done();
48+
});
49+
it('new window document-end should run before onload event',function(done){
50+
assert.equal(results[2]['flag'],true)
51+
assert.equal(results[2]['name'],'top-window-document-end')
52+
done();
53+
});
54+
it('new window onload should run last',function(done){
55+
assert.equal(results[3],'onload-from-new-window');
56+
done();
57+
});
58+
59+
it('iframe document-start should run first',function(done){
60+
assert.equal(results[4]['flag'],true)
61+
assert.equal(results[4]['name'],'iframe-document-start')
62+
done();
63+
});
64+
it('iframe script run between document-start and document-end',function(done){
65+
assert.equal(results[5],'iframe-script');
66+
done();
67+
});
68+
it('iframe document-end should run later',function(done){
69+
assert.equal(results[6]['flag'],true)
70+
assert.equal(results[6]['name'],'iframe-document-end')
71+
done();
72+
});
73+
it('iframe onload should run last',function(done){
74+
assert.equal(results[7],'onload-from-iframe');
75+
done();
76+
});
77+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<h1>New Window</h1>
9+
10+
<script type="text/javascript">
11+
global.results.push("new-window-script");
12+
window.onload = function(){
13+
global.results.push("onload-from-new-window");
14+
};
15+
</script>
16+
</body>
17+
</html>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name":"nw_1403587578",
3+
"main":"index.html",
4+
"dependencies":{}
5+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf8">
5+
<title>test</title>
6+
</head>
7+
<body>
8+
<h1>iframe</h1>
9+
<script type="text/javascript">
10+
var iframe_window_flag = false;
11+
</script>
12+
</body>
13+
</html>
14+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf8">
5+
<title>test</title>
6+
</head>
7+
<body>
8+
<h1>it works!</h1>
9+
<iframe id="iframe" src="iframe.html"></iframe>
10+
<script type="text/javascript">
11+
var gui = require('nw.gui');
12+
var port = gui.App.argv[0]||13013;
13+
var client = require('net').connect(port);
14+
var local_window_flag = false;
15+
var win = gui.Window.get();
16+
win.eval(null,"local_window_flag = true;");
17+
var iframe = document.getElementById('iframe');
18+
iframe.onload = function(){
19+
win.eval(iframe,"iframe_window_flag = true;")
20+
setTimeout(function(){
21+
var iframe_window_flag = iframe.contentWindow.iframe_window_flag;
22+
if (local_window_flag && iframe_window_flag){
23+
client.write('success');
24+
} else {
25+
client.write('fail');
26+
}
27+
},1000)
28+
};
29+
</script>
30+
</body>
31+
</html>
32+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
var server = global.server;
2+
var server_port = global.port;
3+
var result = false;
4+
var assert = require('assert');
5+
var spawn = require('child_process').spawn;
6+
var path = require('path');
7+
8+
var connection_handler_callback = undefined;
9+
var connection_handler = function(socket){
10+
socket.on('data',function(data){
11+
if (data.toString() == "success"){
12+
result = true;
13+
}
14+
if (typeof connection_handler_callback === 'function'){
15+
(connection_handler_callback)();
16+
}
17+
})
18+
};
19+
20+
describe('Window.eval',function(){
21+
var child = null;
22+
23+
before(function(done){
24+
this.timeout(0);
25+
server.on('connection',connection_handler);
26+
var app_path = path.join(global.tests_dir,'window-eval');
27+
var exec_args = [app_path,server_port];
28+
child = spawn(process.execPath,exec_args);
29+
connection_handler_callback = done;
30+
});
31+
32+
after(function(done){
33+
server.removeListener('connection',connection_handler);
34+
child.kill();
35+
done();
36+
});
37+
38+
it("Window.eval should works",function(done){
39+
assert.equal(result,true);
40+
done();
41+
});
42+
})
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name":"nw_1403254112",
3+
"main":"index.html",
4+
"dependencies":{}
5+
}

0 commit comments

Comments
 (0)