Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 // nsIProcess unit test
5 const TEST_ARGS = ["mozilla", "firefox", "thunderbird", "seamonkey", "foo",
6 "bar", "argument with spaces", "\"argument with quotes\""];
8 const TEST_UNICODE_ARGS = ["M\u00F8z\u00EEll\u00E5",
9 "\u041C\u043E\u0437\u0438\u043B\u043B\u0430",
10 "\u09AE\u09CB\u099C\u09BF\u09B2\u09BE",
11 "\uD808\uDE2C\uD808\uDF63\uD808\uDDB7"];
13 // test if a process can be started, polled for its running status
14 // and then killed
15 function test_kill()
16 {
17 var file = get_test_program("TestBlockingProcess");
19 var process = Components.classes["@mozilla.org/process/util;1"]
20 .createInstance(Components.interfaces.nsIProcess);
21 process.init(file);
23 do_check_false(process.isRunning);
25 try {
26 process.kill();
27 do_throw("Attempting to kill a not-running process should throw");
28 }
29 catch (e) { }
31 process.run(false, [], 0);
33 do_check_true(process.isRunning);
35 process.kill();
37 do_check_false(process.isRunning);
39 try {
40 process.kill();
41 do_throw("Attempting to kill a not-running process should throw");
42 }
43 catch (e) { }
44 }
46 // test if we can get an exit value from an application that is
47 // guaranteed to return an exit value of 42
48 function test_quick()
49 {
50 var file = get_test_program("TestQuickReturn");
52 var process = Components.classes["@mozilla.org/process/util;1"]
53 .createInstance(Components.interfaces.nsIProcess);
54 process.init(file);
56 // to get an exit value it must be a blocking process
57 process.run(true, [], 0);
59 do_check_eq(process.exitValue, 42);
60 }
62 function test_args(file, args, argsAreASCII)
63 {
64 var process = Components.classes["@mozilla.org/process/util;1"]
65 .createInstance(Components.interfaces.nsIProcess);
66 process.init(file);
68 if (argsAreASCII)
69 process.run(true, args, args.length);
70 else
71 process.runw(true, args, args.length);
73 do_check_eq(process.exitValue, 0);
74 }
76 // test if an argument can be successfully passed to an application
77 // that will return 0 if "mozilla" is the only argument
78 function test_arguments()
79 {
80 test_args(get_test_program("TestArguments"), TEST_ARGS, true);
81 }
83 // test if Unicode arguments can be successfully passed to an application
84 function test_unicode_arguments()
85 {
86 test_args(get_test_program("TestUnicodeArguments"), TEST_UNICODE_ARGS, false);
87 }
89 function rename_and_test(asciiName, unicodeName, args, argsAreASCII)
90 {
91 var asciiFile = get_test_program(asciiName);
92 var asciiLeaf = asciiFile.leafName;
93 var unicodeLeaf = asciiLeaf.replace(asciiName, unicodeName);
95 asciiFile.moveTo(null, unicodeLeaf);
97 var unicodeFile = get_test_program(unicodeName);
99 test_args(unicodeFile, args, argsAreASCII);
101 unicodeFile.moveTo(null, asciiLeaf);
102 }
104 // test passing ASCII and Unicode arguments to an application with a Unicode name
105 function test_unicode_app()
106 {
107 rename_and_test("TestArguments",
108 // "Unicode" in Tamil
109 "\u0BAF\u0BC1\u0BA9\u0BBF\u0B95\u0BCB\u0B9F\u0BCD",
110 TEST_ARGS, true);
112 rename_and_test("TestUnicodeArguments",
113 // "Unicode" in Thai
114 "\u0E22\u0E39\u0E19\u0E34\u0E42\u0E04\u0E14",
115 TEST_UNICODE_ARGS, false);
116 }
118 // test if we get notified about a blocking process
119 function test_notify_blocking()
120 {
121 var file = get_test_program("TestQuickReturn");
123 var process = Components.classes["@mozilla.org/process/util;1"]
124 .createInstance(Components.interfaces.nsIProcess);
125 process.init(file);
127 process.runAsync([], 0, {
128 observe: function(subject, topic, data) {
129 process = subject.QueryInterface(Components.interfaces.nsIProcess);
130 do_check_eq(topic, "process-finished");
131 do_check_eq(process.exitValue, 42);
132 test_notify_nonblocking();
133 }
134 });
135 }
137 // test if we get notified about a non-blocking process
138 function test_notify_nonblocking()
139 {
140 var file = get_test_program("TestArguments");
142 var process = Components.classes["@mozilla.org/process/util;1"]
143 .createInstance(Components.interfaces.nsIProcess);
144 process.init(file);
146 process.runAsync(TEST_ARGS, TEST_ARGS.length, {
147 observe: function(subject, topic, data) {
148 process = subject.QueryInterface(Components.interfaces.nsIProcess);
149 do_check_eq(topic, "process-finished");
150 do_check_eq(process.exitValue, 0);
151 test_notify_killed();
152 }
153 });
154 }
156 // test if we get notified about a killed process
157 function test_notify_killed()
158 {
159 var file = get_test_program("TestBlockingProcess");
161 var process = Components.classes["@mozilla.org/process/util;1"]
162 .createInstance(Components.interfaces.nsIProcess);
163 process.init(file);
165 process.runAsync([], 0, {
166 observe: function(subject, topic, data) {
167 process = subject.QueryInterface(Components.interfaces.nsIProcess);
168 do_check_eq(topic, "process-finished");
169 do_test_finished();
170 }
171 });
173 process.kill();
174 }
176 function run_test() {
177 set_process_running_environment();
178 test_kill();
179 test_quick();
180 test_arguments();
181 test_unicode_arguments();
182 test_unicode_app();
183 do_test_pending();
184 test_notify_blocking();
185 }