xpcom/tests/unit/test_nsIProcess.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial