xpcom/tests/unit/test_nsIProcess.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/tests/unit/test_nsIProcess.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,185 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +// nsIProcess unit test
     1.8 +const TEST_ARGS = ["mozilla", "firefox", "thunderbird", "seamonkey", "foo",
     1.9 +                   "bar", "argument with spaces", "\"argument with quotes\""];
    1.10 +
    1.11 +const TEST_UNICODE_ARGS = ["M\u00F8z\u00EEll\u00E5",
    1.12 +                           "\u041C\u043E\u0437\u0438\u043B\u043B\u0430",
    1.13 +                           "\u09AE\u09CB\u099C\u09BF\u09B2\u09BE",
    1.14 +                           "\uD808\uDE2C\uD808\uDF63\uD808\uDDB7"];
    1.15 +
    1.16 +// test if a process can be started, polled for its running status
    1.17 +// and then killed
    1.18 +function test_kill()
    1.19 +{
    1.20 +  var file = get_test_program("TestBlockingProcess");
    1.21 + 
    1.22 +  var process = Components.classes["@mozilla.org/process/util;1"]
    1.23 +                          .createInstance(Components.interfaces.nsIProcess);
    1.24 +  process.init(file);
    1.25 +
    1.26 +  do_check_false(process.isRunning);
    1.27 +
    1.28 +  try {
    1.29 +    process.kill();
    1.30 +    do_throw("Attempting to kill a not-running process should throw");
    1.31 +  }
    1.32 +  catch (e) { }
    1.33 +
    1.34 +  process.run(false, [], 0);
    1.35 +
    1.36 +  do_check_true(process.isRunning);
    1.37 +
    1.38 +  process.kill();
    1.39 +
    1.40 +  do_check_false(process.isRunning);
    1.41 +
    1.42 +  try {
    1.43 +    process.kill();
    1.44 +    do_throw("Attempting to kill a not-running process should throw");
    1.45 +  }
    1.46 +  catch (e) { }
    1.47 +}
    1.48 +
    1.49 +// test if we can get an exit value from an application that is
    1.50 +// guaranteed to return an exit value of 42
    1.51 +function test_quick()
    1.52 +{
    1.53 +  var file = get_test_program("TestQuickReturn");
    1.54 +  
    1.55 +  var process = Components.classes["@mozilla.org/process/util;1"]
    1.56 +                          .createInstance(Components.interfaces.nsIProcess);
    1.57 +  process.init(file);
    1.58 +  
    1.59 +  // to get an exit value it must be a blocking process
    1.60 +  process.run(true, [], 0);
    1.61 +
    1.62 +  do_check_eq(process.exitValue, 42);
    1.63 +}
    1.64 +
    1.65 +function test_args(file, args, argsAreASCII)
    1.66 +{
    1.67 +  var process = Components.classes["@mozilla.org/process/util;1"]
    1.68 +                          .createInstance(Components.interfaces.nsIProcess);
    1.69 +  process.init(file);
    1.70 +
    1.71 +  if (argsAreASCII)  
    1.72 +    process.run(true, args, args.length);
    1.73 +  else  
    1.74 +    process.runw(true, args, args.length);
    1.75 +  
    1.76 +  do_check_eq(process.exitValue, 0);
    1.77 +}
    1.78 +
    1.79 +// test if an argument can be successfully passed to an application
    1.80 +// that will return 0 if "mozilla" is the only argument
    1.81 +function test_arguments()
    1.82 +{
    1.83 +  test_args(get_test_program("TestArguments"), TEST_ARGS, true);
    1.84 +}
    1.85 +
    1.86 +// test if Unicode arguments can be successfully passed to an application
    1.87 +function test_unicode_arguments()
    1.88 +{
    1.89 +  test_args(get_test_program("TestUnicodeArguments"), TEST_UNICODE_ARGS, false);
    1.90 +}
    1.91 +
    1.92 +function rename_and_test(asciiName, unicodeName, args, argsAreASCII)
    1.93 +{
    1.94 +  var asciiFile = get_test_program(asciiName);
    1.95 +  var asciiLeaf = asciiFile.leafName;
    1.96 +  var unicodeLeaf = asciiLeaf.replace(asciiName, unicodeName);
    1.97 +
    1.98 +  asciiFile.moveTo(null, unicodeLeaf);
    1.99 +
   1.100 +  var unicodeFile = get_test_program(unicodeName);
   1.101 +
   1.102 +  test_args(unicodeFile, args, argsAreASCII);
   1.103 +
   1.104 +  unicodeFile.moveTo(null, asciiLeaf);
   1.105 +}
   1.106 +
   1.107 +// test passing ASCII and Unicode arguments to an application with a Unicode name
   1.108 +function test_unicode_app()
   1.109 +{
   1.110 +  rename_and_test("TestArguments",
   1.111 +                  // "Unicode" in Tamil
   1.112 +                  "\u0BAF\u0BC1\u0BA9\u0BBF\u0B95\u0BCB\u0B9F\u0BCD",
   1.113 +                  TEST_ARGS, true);
   1.114 +
   1.115 +  rename_and_test("TestUnicodeArguments",
   1.116 +                  // "Unicode" in Thai
   1.117 +                  "\u0E22\u0E39\u0E19\u0E34\u0E42\u0E04\u0E14",
   1.118 +                  TEST_UNICODE_ARGS, false);
   1.119 +}
   1.120 +
   1.121 +// test if we get notified about a blocking process
   1.122 +function test_notify_blocking()
   1.123 +{
   1.124 +  var file = get_test_program("TestQuickReturn");
   1.125 +
   1.126 +  var process = Components.classes["@mozilla.org/process/util;1"]
   1.127 +                          .createInstance(Components.interfaces.nsIProcess);
   1.128 +  process.init(file);
   1.129 +
   1.130 +  process.runAsync([], 0, {
   1.131 +    observe: function(subject, topic, data) {
   1.132 +      process = subject.QueryInterface(Components.interfaces.nsIProcess);
   1.133 +      do_check_eq(topic, "process-finished");
   1.134 +      do_check_eq(process.exitValue, 42);
   1.135 +      test_notify_nonblocking();
   1.136 +    }
   1.137 +  });
   1.138 +}
   1.139 +
   1.140 +// test if we get notified about a non-blocking process
   1.141 +function test_notify_nonblocking()
   1.142 +{
   1.143 +  var file = get_test_program("TestArguments");
   1.144 +
   1.145 +  var process = Components.classes["@mozilla.org/process/util;1"]
   1.146 +                          .createInstance(Components.interfaces.nsIProcess);
   1.147 +  process.init(file);
   1.148 +
   1.149 +  process.runAsync(TEST_ARGS, TEST_ARGS.length, {
   1.150 +    observe: function(subject, topic, data) {
   1.151 +      process = subject.QueryInterface(Components.interfaces.nsIProcess);
   1.152 +      do_check_eq(topic, "process-finished");
   1.153 +      do_check_eq(process.exitValue, 0);
   1.154 +      test_notify_killed();
   1.155 +    }
   1.156 +  });
   1.157 +}
   1.158 +
   1.159 +// test if we get notified about a killed process
   1.160 +function test_notify_killed()
   1.161 +{
   1.162 +  var file = get_test_program("TestBlockingProcess");
   1.163 +
   1.164 +  var process = Components.classes["@mozilla.org/process/util;1"]
   1.165 +                          .createInstance(Components.interfaces.nsIProcess);
   1.166 +  process.init(file);
   1.167 +
   1.168 +  process.runAsync([], 0, {
   1.169 +    observe: function(subject, topic, data) {
   1.170 +      process = subject.QueryInterface(Components.interfaces.nsIProcess);
   1.171 +      do_check_eq(topic, "process-finished");
   1.172 +      do_test_finished();
   1.173 +    }
   1.174 +  });
   1.175 +
   1.176 +  process.kill();
   1.177 +}
   1.178 +
   1.179 +function run_test() {
   1.180 +  set_process_running_environment();
   1.181 +  test_kill();
   1.182 +  test_quick();
   1.183 +  test_arguments();
   1.184 +  test_unicode_arguments();
   1.185 +  test_unicode_app();
   1.186 +  do_test_pending();
   1.187 +  test_notify_blocking();
   1.188 +}

mercurial