|
1 |
| -var path = require('path'); |
2 |
| -var app_test = require('./nw_test_app'); |
| 1 | +var func = require('./' + global.tests_dir +'/start_app/script.js'); |
| 2 | +var execPath = func.getExecPath(); |
| 3 | +var fs = require('fs-extra'); |
| 4 | +var os = require('os'); |
| 5 | +var spawn = require('child_process').spawn; |
3 | 6 | var exec = require('child_process').exec;
|
4 |
| -var pid1, pid2, pid3, pid4; |
5 |
| -var pid1_exist, pid2_exist, pid3_exist, pid4_exist; |
6 |
| -var cmd; |
7 |
| -var child1, child2, child3, child4; |
8 |
| - |
9 |
| -if (process.platform == 'linux') |
10 |
| - cmd = 'ps -a | grep '; |
11 |
| -else if (process.platform == 'win32') |
12 |
| - cmd = 'tasklist | findstr '; |
13 |
| -else if (process.platform == 'darwin') |
14 |
| - return; |
15 |
| - |
16 |
| -describe('single-instance', function() { |
| 7 | + |
| 8 | +var app = new Array(); |
| 9 | +var child1, child2; |
| 10 | +var mac_app_path = path.join('tmp-nw', 'node-webkit.app'); |
| 11 | +function make_execuable_file(folder_path, done) { |
| 12 | + func.copyExecFiles(function() { |
| 13 | + func.copySourceFiles(folder_path); |
| 14 | + func.zipSourceFiles(function() { |
| 15 | + func.makeExecuableFile(); |
| 16 | + if (os.platform() == 'darwin') { |
| 17 | + var app_path = 'tmp-nw/node-webkit.app/Contents/Resources/app.nw'; |
| 18 | + fs.mkdir(app_path, function(err) { |
| 19 | + if(err && err.code !== 'EEXIST') throw err |
| 20 | + fs.copy('tmp-nw/index.html', path.join(app_path, 'index.html')); |
| 21 | + fs.copy('tmp-nw/package.html', path.join(app_path, 'package.html')); |
| 22 | + }); |
| 23 | + } |
| 24 | + done(); |
| 25 | + }); |
| 26 | + }); |
| 27 | +} |
| 28 | + |
| 29 | +function check_have(i, cmd, options, msg, last, done) { |
| 30 | + var result = false; |
| 31 | + app[i] = spawn(cmd, options); |
| 32 | + app[i].on('exit', function() { |
| 33 | + result = true; |
| 34 | + }); |
| 35 | + |
| 36 | + if (last == 2) { |
| 37 | + setTimeout(function() { |
| 38 | + if (result) { |
| 39 | + done(); |
| 40 | + } else { |
| 41 | + done(msg); |
| 42 | + } |
| 43 | + app[i].kill(); |
| 44 | + app[i - 1].kill(); |
| 45 | + }, 3000); |
| 46 | + } else { |
| 47 | + setTimeout(function() { |
| 48 | + if (result) { |
| 49 | + done(msg); |
| 50 | + } else { |
| 51 | + done(); |
| 52 | + } |
| 53 | + if (last == 1) { |
| 54 | + app[i].kill(); |
| 55 | + app[i - 1].kill(); |
| 56 | + } |
| 57 | + }, 3000); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +describe('single-instance', function() { |
17 | 62 | this.timeout(0);
|
18 | 63 |
|
19 | 64 | describe('single-instance false', function() {
|
20 |
| - var app_mul_path = path.join(global.tests_dir, 'single_instance', process.platform, 'app_mul'); |
21 |
| - before(function() { |
22 |
| - child3 = exec(app_mul_path); |
23 |
| - pid3 = child3.pid; |
24 |
| - console.log(pid3); |
25 |
| - |
26 |
| - // normally we won't launch app one after another immediately |
27 |
| - setTimeout(function(){ |
28 |
| - child4 = exec(app_mul_path); |
29 |
| - pid4 = child4.pid; |
30 |
| - console.log(pid4); |
31 |
| - },1000); |
32 |
| - }); |
33 |
| - |
34 |
| - it('should have a instance', function(done) { |
35 |
| - child = exec(cmd + pid3, function (error, stdout, stderr) { |
36 |
| - if (stdout == "") |
37 |
| - pid3_exist = false; |
38 |
| - else |
39 |
| - pid3_exist = true; |
40 |
| - |
41 |
| - if (pid3_exist, true) |
42 |
| - done(); |
43 |
| - else |
44 |
| - done("not have a instance"); |
45 |
| - }); |
46 |
| - }); |
47 |
| - |
48 |
| - it('should have a second instance', function(done) { |
49 |
| - setTimeout(function() { |
50 |
| - child = exec(cmd + pid4, function (error, stdout, stderr) { |
51 |
| - if (stdout == "") |
52 |
| - pid4_exist = false; |
53 |
| - else |
54 |
| - pid4_exist = true; |
55 |
| - |
56 |
| - if (pid4_exist, true) |
57 |
| - done(); |
58 |
| - else |
59 |
| - done('have a second instance'); |
60 |
| - }); |
61 |
| - }, 5500); |
62 |
| - }); |
| 65 | + before(function(done) { |
| 66 | + make_execuable_file('single_instance/mul', done); |
| 67 | + }); |
| 68 | + |
| 69 | + after(function() { |
| 70 | + fs.remove('tmp-nw', function (er) { |
| 71 | + if (er) throw er; |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should have a instance', function(done) { |
| 76 | + check_have(0, execPath, "", 'not have a instance', 0, done); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should have a second instance', function(done) { |
| 80 | + check_have(1, execPath, "", 'not have a second instance', 1, done); |
| 81 | + }); |
63 | 82 | });
|
64 | 83 |
|
| 84 | + |
65 | 85 | describe('single-instance default', function() {
|
66 |
| - var app_path = path.join(global.tests_dir, 'single_instance', process.platform, 'app'); |
67 |
| - before(function(done) { |
68 | 86 |
|
| 87 | + before(function(done) { |
69 | 88 | setTimeout(function() {
|
70 |
| - child1 = exec(app_path); |
71 |
| - pid1 = child1.pid; |
72 |
| - console.log(pid1); |
73 |
| - |
74 |
| - // normally we won't launch app one after another immediately |
75 |
| - // Otherwise,sometimes it won't work here |
76 |
| - setTimeout(function(){ |
77 |
| - child2 = exec(app_path); |
78 |
| - pid2 = child2.pid; |
79 |
| - console.log(pid2); |
80 |
| - done(); |
81 |
| - }, 1000); |
82 |
| - }, 2000) |
83 |
| - }); |
84 |
| - |
85 |
| - it('should have a instance', function(done) { |
86 |
| - child = exec(cmd + pid1, function (error, stdout, stderr) { |
87 |
| - if (stdout == "") |
88 |
| - pid1_exist = false; |
89 |
| - else |
90 |
| - pid1_exist = true; |
91 |
| - |
92 |
| - if (pid1_exist) |
93 |
| - done(); |
94 |
| - else |
95 |
| - done('not have a instance'); |
96 |
| - }); |
97 |
| - }); |
98 |
| - |
99 |
| - it('should not have a second instance', function(done) { |
100 |
| - setTimeout(function() { |
101 |
| - child = exec(cmd + pid2, function (error, stdout, stderr) { |
102 |
| - if (stdout == "") |
103 |
| - pid2_exist = false; |
104 |
| - else |
105 |
| - pid2_exist = true; |
106 |
| - |
107 |
| - if (!pid2_exist) |
108 |
| - done(); |
109 |
| - else |
110 |
| - done("have a second instance"); |
111 |
| - }); |
112 |
| - }, 5500); |
113 |
| - }); |
| 89 | + make_execuable_file('single_instance/single', done); |
| 90 | + }, 3000); |
| 91 | + }); |
| 92 | + |
| 93 | + after(function() { |
| 94 | + fs.remove('tmp-nw', function (er) { |
| 95 | + if (er) throw er; |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should have a instance', function(done) { |
| 100 | + check_have(2, execPath, "", 'not have a instance', 0, done); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should not have a second instance', function(done) { |
| 104 | + check_have(3, execPath, "", 'have a second instance', 2, done); |
| 105 | + }); |
114 | 106 | });
|
115 |
| - |
116 |
| -}); |
| 107 | + |
| 108 | + if (os.platform() == 'darwin') { |
| 109 | + describe('single-instance false(open app)', function(){ |
| 110 | + before(function(done) { |
| 111 | + setTimeout(function() { |
| 112 | + make_execuable_file('single_instance/open_mul', done); |
| 113 | + }, 3000); |
| 114 | + }); |
| 115 | + |
| 116 | + after(function() { |
| 117 | + fs.remove('tmp-nw', function (er) { |
| 118 | + if (er) throw er; |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should have a instance (open app)', function(done) { |
| 123 | + child1 = exec('open ' + mac_app_path); |
| 124 | + setTimeout(function () { |
| 125 | + var content = fs.readFileSync('tmp-nw/msg'); |
| 126 | + if (content + "" != "") |
| 127 | + done(); |
| 128 | + else |
| 129 | + done("not have a instance"); |
| 130 | + }, 6000); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should have a second instance (open app)', function(done) { |
| 134 | + child2 = exec('open ' + mac_app_path); |
| 135 | + var content = fs.readFileSync('tmp-nw/msg'); |
| 136 | + if (content + "" == "11") |
| 137 | + done(); |
| 138 | + else |
| 139 | + done("not have a instance"); |
| 140 | + }); |
| 141 | + |
| 142 | + }); |
| 143 | + |
| 144 | + describe('single-instance default(open app)', function(){ |
| 145 | + before(function(done) { |
| 146 | + setTimeout(function() { |
| 147 | + make_execuable_file('single_instance/open_single', done); |
| 148 | + }, 3000); |
| 149 | + }); |
| 150 | + |
| 151 | + after(function() { |
| 152 | + fs.remove('tmp-nw', function (er) { |
| 153 | + if (er) throw er; |
| 154 | + }); |
| 155 | + }); |
| 156 | + |
| 157 | + it('should have a instance (open app)', function(done) { |
| 158 | + child1 = exec('open ' + mac_app_path); |
| 159 | + setTimeout(function () { |
| 160 | + var content = fs.readFileSync('tmp-nw/msg_s'); |
| 161 | + if (content + "" != "") |
| 162 | + done(); |
| 163 | + else |
| 164 | + done("not have a instance"); |
| 165 | + }, 6000); |
| 166 | + }); |
| 167 | + |
| 168 | + it('should not have a second instance (open app)', function(done) { |
| 169 | + child2 = exec('open ' + mac_app_path); |
| 170 | + var content = fs.readFileSync('tmp-nw/msg_s'); |
| 171 | + if (content + "" == "11") |
| 172 | + done("have a second instance"); |
| 173 | + else |
| 174 | + done(); |
| 175 | + |
| 176 | + }); |
| 177 | + }); |
| 178 | + } |
| 179 | +}); |
0 commit comments