test_nsIProcess.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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\""];
  7. const TEST_UNICODE_ARGS = ["M\u00F8z\u00EEll\u00E5",
  8. "\u041C\u043E\u0437\u0438\u043B\u043B\u0430",
  9. "\u09AE\u09CB\u099C\u09BF\u09B2\u09BE",
  10. "\uD808\uDE2C\uD808\uDF63\uD808\uDDB7"];
  11. // test if a process can be started, polled for its running status
  12. // and then killed
  13. function test_kill()
  14. {
  15. var file = get_test_program("TestBlockingProcess");
  16. var process = Components.classes["@mozilla.org/process/util;1"]
  17. .createInstance(Components.interfaces.nsIProcess);
  18. process.init(file);
  19. do_check_false(process.isRunning);
  20. try {
  21. process.kill();
  22. do_throw("Attempting to kill a not-running process should throw");
  23. }
  24. catch (e) { }
  25. process.run(false, [], 0);
  26. do_check_true(process.isRunning);
  27. process.kill();
  28. do_check_false(process.isRunning);
  29. try {
  30. process.kill();
  31. do_throw("Attempting to kill a not-running process should throw");
  32. }
  33. catch (e) { }
  34. }
  35. // test if we can get an exit value from an application that is
  36. // guaranteed to return an exit value of 42
  37. function test_quick()
  38. {
  39. var file = get_test_program("TestQuickReturn");
  40. var process = Components.classes["@mozilla.org/process/util;1"]
  41. .createInstance(Components.interfaces.nsIProcess);
  42. process.init(file);
  43. // to get an exit value it must be a blocking process
  44. process.run(true, [], 0);
  45. do_check_eq(process.exitValue, 42);
  46. }
  47. function test_args(file, args, argsAreASCII)
  48. {
  49. var process = Components.classes["@mozilla.org/process/util;1"]
  50. .createInstance(Components.interfaces.nsIProcess);
  51. process.init(file);
  52. if (argsAreASCII)
  53. process.run(true, args, args.length);
  54. else
  55. process.runw(true, args, args.length);
  56. do_check_eq(process.exitValue, 0);
  57. }
  58. // test if an argument can be successfully passed to an application
  59. // that will return 0 if "mozilla" is the only argument
  60. function test_arguments()
  61. {
  62. test_args(get_test_program("TestArguments"), TEST_ARGS, true);
  63. }
  64. // test if Unicode arguments can be successfully passed to an application
  65. function test_unicode_arguments()
  66. {
  67. test_args(get_test_program("TestUnicodeArguments"), TEST_UNICODE_ARGS, false);
  68. }
  69. function rename_and_test(asciiName, unicodeName, args, argsAreASCII)
  70. {
  71. var asciiFile = get_test_program(asciiName);
  72. var asciiLeaf = asciiFile.leafName;
  73. var unicodeLeaf = asciiLeaf.replace(asciiName, unicodeName);
  74. asciiFile.moveTo(null, unicodeLeaf);
  75. var unicodeFile = get_test_program(unicodeName);
  76. test_args(unicodeFile, args, argsAreASCII);
  77. unicodeFile.moveTo(null, asciiLeaf);
  78. }
  79. // test passing ASCII and Unicode arguments to an application with a Unicode name
  80. function test_unicode_app()
  81. {
  82. rename_and_test("TestArguments",
  83. // "Unicode" in Tamil
  84. "\u0BAF\u0BC1\u0BA9\u0BBF\u0B95\u0BCB\u0B9F\u0BCD",
  85. TEST_ARGS, true);
  86. rename_and_test("TestUnicodeArguments",
  87. // "Unicode" in Thai
  88. "\u0E22\u0E39\u0E19\u0E34\u0E42\u0E04\u0E14",
  89. TEST_UNICODE_ARGS, false);
  90. }
  91. // test if we get notified about a blocking process
  92. function test_notify_blocking()
  93. {
  94. var file = get_test_program("TestQuickReturn");
  95. var process = Components.classes["@mozilla.org/process/util;1"]
  96. .createInstance(Components.interfaces.nsIProcess);
  97. process.init(file);
  98. process.runAsync([], 0, {
  99. observe: function(subject, topic, data) {
  100. process = subject.QueryInterface(Components.interfaces.nsIProcess);
  101. do_check_eq(topic, "process-finished");
  102. do_check_eq(process.exitValue, 42);
  103. test_notify_nonblocking();
  104. }
  105. });
  106. }
  107. // test if we get notified about a non-blocking process
  108. function test_notify_nonblocking()
  109. {
  110. var file = get_test_program("TestArguments");
  111. var process = Components.classes["@mozilla.org/process/util;1"]
  112. .createInstance(Components.interfaces.nsIProcess);
  113. process.init(file);
  114. process.runAsync(TEST_ARGS, TEST_ARGS.length, {
  115. observe: function(subject, topic, data) {
  116. process = subject.QueryInterface(Components.interfaces.nsIProcess);
  117. do_check_eq(topic, "process-finished");
  118. do_check_eq(process.exitValue, 0);
  119. test_notify_killed();
  120. }
  121. });
  122. }
  123. // test if we get notified about a killed process
  124. function test_notify_killed()
  125. {
  126. var file = get_test_program("TestBlockingProcess");
  127. var process = Components.classes["@mozilla.org/process/util;1"]
  128. .createInstance(Components.interfaces.nsIProcess);
  129. process.init(file);
  130. process.runAsync([], 0, {
  131. observe: function(subject, topic, data) {
  132. process = subject.QueryInterface(Components.interfaces.nsIProcess);
  133. do_check_eq(topic, "process-finished");
  134. do_test_finished();
  135. }
  136. });
  137. process.kill();
  138. }
  139. function run_test() {
  140. set_process_running_environment();
  141. test_kill();
  142. test_quick();
  143. test_arguments();
  144. test_unicode_arguments();
  145. test_unicode_app();
  146. do_test_pending();
  147. test_notify_blocking();
  148. }