michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: // nsIProcess unit test michael@0: const TEST_ARGS = ["mozilla", "firefox", "thunderbird", "seamonkey", "foo", michael@0: "bar", "argument with spaces", "\"argument with quotes\""]; michael@0: michael@0: const TEST_UNICODE_ARGS = ["M\u00F8z\u00EEll\u00E5", michael@0: "\u041C\u043E\u0437\u0438\u043B\u043B\u0430", michael@0: "\u09AE\u09CB\u099C\u09BF\u09B2\u09BE", michael@0: "\uD808\uDE2C\uD808\uDF63\uD808\uDDB7"]; michael@0: michael@0: // test if a process can be started, polled for its running status michael@0: // and then killed michael@0: function test_kill() michael@0: { michael@0: var file = get_test_program("TestBlockingProcess"); michael@0: michael@0: var process = Components.classes["@mozilla.org/process/util;1"] michael@0: .createInstance(Components.interfaces.nsIProcess); michael@0: process.init(file); michael@0: michael@0: do_check_false(process.isRunning); michael@0: michael@0: try { michael@0: process.kill(); michael@0: do_throw("Attempting to kill a not-running process should throw"); michael@0: } michael@0: catch (e) { } michael@0: michael@0: process.run(false, [], 0); michael@0: michael@0: do_check_true(process.isRunning); michael@0: michael@0: process.kill(); michael@0: michael@0: do_check_false(process.isRunning); michael@0: michael@0: try { michael@0: process.kill(); michael@0: do_throw("Attempting to kill a not-running process should throw"); michael@0: } michael@0: catch (e) { } michael@0: } michael@0: michael@0: // test if we can get an exit value from an application that is michael@0: // guaranteed to return an exit value of 42 michael@0: function test_quick() michael@0: { michael@0: var file = get_test_program("TestQuickReturn"); michael@0: michael@0: var process = Components.classes["@mozilla.org/process/util;1"] michael@0: .createInstance(Components.interfaces.nsIProcess); michael@0: process.init(file); michael@0: michael@0: // to get an exit value it must be a blocking process michael@0: process.run(true, [], 0); michael@0: michael@0: do_check_eq(process.exitValue, 42); michael@0: } michael@0: michael@0: function test_args(file, args, argsAreASCII) michael@0: { michael@0: var process = Components.classes["@mozilla.org/process/util;1"] michael@0: .createInstance(Components.interfaces.nsIProcess); michael@0: process.init(file); michael@0: michael@0: if (argsAreASCII) michael@0: process.run(true, args, args.length); michael@0: else michael@0: process.runw(true, args, args.length); michael@0: michael@0: do_check_eq(process.exitValue, 0); michael@0: } michael@0: michael@0: // test if an argument can be successfully passed to an application michael@0: // that will return 0 if "mozilla" is the only argument michael@0: function test_arguments() michael@0: { michael@0: test_args(get_test_program("TestArguments"), TEST_ARGS, true); michael@0: } michael@0: michael@0: // test if Unicode arguments can be successfully passed to an application michael@0: function test_unicode_arguments() michael@0: { michael@0: test_args(get_test_program("TestUnicodeArguments"), TEST_UNICODE_ARGS, false); michael@0: } michael@0: michael@0: function rename_and_test(asciiName, unicodeName, args, argsAreASCII) michael@0: { michael@0: var asciiFile = get_test_program(asciiName); michael@0: var asciiLeaf = asciiFile.leafName; michael@0: var unicodeLeaf = asciiLeaf.replace(asciiName, unicodeName); michael@0: michael@0: asciiFile.moveTo(null, unicodeLeaf); michael@0: michael@0: var unicodeFile = get_test_program(unicodeName); michael@0: michael@0: test_args(unicodeFile, args, argsAreASCII); michael@0: michael@0: unicodeFile.moveTo(null, asciiLeaf); michael@0: } michael@0: michael@0: // test passing ASCII and Unicode arguments to an application with a Unicode name michael@0: function test_unicode_app() michael@0: { michael@0: rename_and_test("TestArguments", michael@0: // "Unicode" in Tamil michael@0: "\u0BAF\u0BC1\u0BA9\u0BBF\u0B95\u0BCB\u0B9F\u0BCD", michael@0: TEST_ARGS, true); michael@0: michael@0: rename_and_test("TestUnicodeArguments", michael@0: // "Unicode" in Thai michael@0: "\u0E22\u0E39\u0E19\u0E34\u0E42\u0E04\u0E14", michael@0: TEST_UNICODE_ARGS, false); michael@0: } michael@0: michael@0: // test if we get notified about a blocking process michael@0: function test_notify_blocking() michael@0: { michael@0: var file = get_test_program("TestQuickReturn"); michael@0: michael@0: var process = Components.classes["@mozilla.org/process/util;1"] michael@0: .createInstance(Components.interfaces.nsIProcess); michael@0: process.init(file); michael@0: michael@0: process.runAsync([], 0, { michael@0: observe: function(subject, topic, data) { michael@0: process = subject.QueryInterface(Components.interfaces.nsIProcess); michael@0: do_check_eq(topic, "process-finished"); michael@0: do_check_eq(process.exitValue, 42); michael@0: test_notify_nonblocking(); michael@0: } michael@0: }); michael@0: } michael@0: michael@0: // test if we get notified about a non-blocking process michael@0: function test_notify_nonblocking() michael@0: { michael@0: var file = get_test_program("TestArguments"); michael@0: michael@0: var process = Components.classes["@mozilla.org/process/util;1"] michael@0: .createInstance(Components.interfaces.nsIProcess); michael@0: process.init(file); michael@0: michael@0: process.runAsync(TEST_ARGS, TEST_ARGS.length, { michael@0: observe: function(subject, topic, data) { michael@0: process = subject.QueryInterface(Components.interfaces.nsIProcess); michael@0: do_check_eq(topic, "process-finished"); michael@0: do_check_eq(process.exitValue, 0); michael@0: test_notify_killed(); michael@0: } michael@0: }); michael@0: } michael@0: michael@0: // test if we get notified about a killed process michael@0: function test_notify_killed() michael@0: { michael@0: var file = get_test_program("TestBlockingProcess"); michael@0: michael@0: var process = Components.classes["@mozilla.org/process/util;1"] michael@0: .createInstance(Components.interfaces.nsIProcess); michael@0: process.init(file); michael@0: michael@0: process.runAsync([], 0, { michael@0: observe: function(subject, topic, data) { michael@0: process = subject.QueryInterface(Components.interfaces.nsIProcess); michael@0: do_check_eq(topic, "process-finished"); michael@0: do_test_finished(); michael@0: } michael@0: }); michael@0: michael@0: process.kill(); michael@0: } michael@0: michael@0: function run_test() { michael@0: set_process_running_environment(); michael@0: test_kill(); michael@0: test_quick(); michael@0: test_arguments(); michael@0: test_unicode_arguments(); michael@0: test_unicode_app(); michael@0: do_test_pending(); michael@0: test_notify_blocking(); michael@0: }