test-unit-test.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. const timer = require("sdk/timers");
  5. const { Loader } = require("sdk/test/loader");
  6. var setupCalled = false, teardownCalled = false;
  7. exports.setup = function() {
  8. setupCalled = true;
  9. };
  10. exports.teardown = function() {
  11. teardownCalled = true;
  12. setupCalled = false;
  13. };
  14. // Important note - unit tests are run in alphabetical order. The following
  15. // unit tests for setup/teardown are order dependent, sometimes the result of
  16. // one test is checked in the next test (testing for teardown does this). When
  17. // tests are cohesively a single unit, they are named <test_name> - partN where
  18. // N is their order in the sequence. Secondly, because these tests should be
  19. // run before all others, they start with an A.
  20. exports.testASetupTeardownSyncTestPart1 = function(test) {
  21. test.assertEqual(true, setupCalled, 'setup function was called before this');
  22. test.assertEqual(false, teardownCalled, 'teardown function was not called before this');
  23. };
  24. exports.testASetupTeardownSyncTestPart2 = function(test) {
  25. test.assertEqual(true, setupCalled, 'setup was re-called before this');
  26. test.assertEqual(true, teardownCalled, 'teardown was called after first function');
  27. };
  28. exports.testATeardownAsyncTestPart1 = function(test) {
  29. teardownCalled = false;
  30. timer.setTimeout(function() {
  31. test.assertEqual(false, teardownCalled, "teardown not called until done");
  32. test.done();
  33. }, 200);
  34. test.waitUntilDone();
  35. };
  36. exports.testATeardownAsyncTestPart2 = function(test) {
  37. test.assertEqual(true, teardownCalled, "teardown called after done");
  38. };
  39. exports.testWaitUntilInstant = function(test) {
  40. test.waitUntilDone();
  41. test.waitUntil(function () true, "waitUntil with instant true pass")
  42. .then(function () test.done());
  43. }
  44. exports.testWaitUntil = function(test) {
  45. test.waitUntilDone();
  46. let succeed = false;
  47. test.waitUntil(function () succeed, "waitUntil pass")
  48. .then(function () test.done());
  49. timer.setTimeout(function () {
  50. succeed = true;
  51. }, 200);
  52. }
  53. exports.testWaitUntilEqual = function(test) {
  54. test.waitUntilDone();
  55. let succeed = false;
  56. test.waitUntilEqual("foo", function () succeed ? "foo" : "bar",
  57. "waitUntilEqual pass")
  58. .then(function () test.done());
  59. timer.setTimeout(function () {
  60. succeed = true;
  61. }, 200);
  62. }
  63. exports.testWaitUntilNotEqual = function(test) {
  64. test.waitUntilDone();
  65. let succeed = false;
  66. test.waitUntilNotEqual("foo", function () succeed ? "bar" : "foo",
  67. "waitUntilNotEqual pass")
  68. .then(function () test.done());
  69. timer.setTimeout(function () {
  70. succeed = true;
  71. }, 200);
  72. }
  73. exports.testWaitUntilMatches = function(test) {
  74. test.waitUntilDone();
  75. let succeed = false;
  76. test.waitUntilMatches(function () succeed ? "foo" : "bar",
  77. /foo/, "waitUntilEqual pass")
  78. .then(function () test.done());
  79. timer.setTimeout(function () {
  80. succeed = true;
  81. }, 200);
  82. }
  83. exports.testWaitUntilErrorInCallback = function(test) {
  84. test.waitUntilDone();
  85. test.expectFail(function() {
  86. test.waitUntil(function () {throw "oops"}, "waitUntil pass")
  87. .then(function () test.done());
  88. });
  89. }
  90. exports.testWaitUntilTimeoutInCallback = function(test) {
  91. test.waitUntilDone();
  92. let expected = [];
  93. let message = 0;
  94. if (require("@test/options").parseable) {
  95. expected.push(["print", "TEST-START | wait4ever\n"]);
  96. expected.push(["error", "fail:", "Timed out"]);
  97. expected.push(["error", "test assertion never became true:\n", "assertion failed, value is false\n"]);
  98. expected.push(["print", "TEST-END | wait4ever\n"]);
  99. }
  100. else {
  101. expected.push(["info", "executing 'wait4ever'"]);
  102. expected.push(["error", "fail:", "Timed out"]);
  103. expected.push(["error", "test assertion never became true:\n", "assertion failed, value is false\n"]);
  104. }
  105. function checkExpected(name, args) {
  106. if (expected.length == 0 || expected[0][0] != name) {
  107. test.fail("Saw an unexpected console." + name + "() call " + args);
  108. return;
  109. }
  110. message++;
  111. let expectedArgs = expected.shift().slice(1);
  112. for (let i = 0; i < expectedArgs.length; i++)
  113. test.assertEqual(args[i], expectedArgs[i], "Should have seen the right message in argument " + i + " of message " + message);
  114. if (expected.length == 0)
  115. test.done();
  116. }
  117. let runner = new (require("sdk/deprecated/unit-test").TestRunner)({
  118. console: {
  119. error: function() {
  120. checkExpected("error", Array.slice(arguments));
  121. },
  122. info: function () {
  123. checkExpected("info", Array.slice(arguments));
  124. },
  125. trace: function () {},
  126. exception: function () {},
  127. print: function () {
  128. checkExpected("print", Array.slice(arguments));
  129. }
  130. }
  131. });
  132. runner.start({
  133. test: {
  134. name: "wait4ever",
  135. testFunction: function(test) {
  136. test.waitUntilDone(100);
  137. test.waitUntil(function() false);
  138. }
  139. },
  140. onDone: function() {}
  141. });
  142. };
  143. exports.testExpectFail = function(test) {
  144. test.expectFail(function() {
  145. test.fail('expectFail masking .fail');
  146. });
  147. test.expectFail(function() {
  148. test.assert(false, 'expectFail masking .assert');
  149. });
  150. test.assert(true, 'assert should pass with no expectFail');
  151. /*
  152. test.expectFail(function() {
  153. test.expectFail(function() {
  154. test.fail('this should blow up');
  155. });
  156. });
  157. */
  158. };
  159. exports.testAssertFunction = function(test) {
  160. test.assertFunction(function() {}, 'assertFunction with function');
  161. test.expectFail(function() {
  162. test.assertFunction(null, 'assertFunction with non-function');
  163. });
  164. };
  165. exports.testAssertUndefined = function(test) {
  166. test.assertUndefined(undefined, 'assertUndefined with undefined');
  167. test.expectFail(function() {
  168. test.assertUndefined(null, 'assertUndefined with null');
  169. });
  170. test.expectFail(function() {
  171. test.assertUndefined(false, 'assertUndefined with false');
  172. });
  173. test.expectFail(function() {
  174. test.assertUndefined(0, 'assertUndefined with 0');
  175. });
  176. };
  177. exports.testAssertNotUndefined = function(test) {
  178. test.expectFail(function() {
  179. test.assertNotUndefined(undefined, 'assertNotUndefined with undefined');
  180. });
  181. test.assertNotUndefined(null, 'assertNotUndefined with null');
  182. test.assertNotUndefined(false, 'assertNotUndefined with false');
  183. test.assertNotUndefined(0, 'assertNotUndefined with 0');
  184. };
  185. exports.testAssertNull = function(test) {
  186. test.assertNull(null, 'assertNull with null');
  187. test.expectFail(function() {
  188. test.assertNull(undefined, 'assertNull with undefined');
  189. });
  190. test.expectFail(function() {
  191. test.assertNull(false, 'assertNull with false');
  192. });
  193. test.expectFail(function() {
  194. test.assertNull(0, 'assertNull with 0');
  195. });
  196. };
  197. exports.testAssertNotNull = function(test) {
  198. test.assertNotNull(undefined, 'assertNotNull with undefined');
  199. test.assertNotNull(false, 'assertNotNull with false');
  200. test.assertNotNull(0, 'assertNotNull with 0');
  201. test.expectFail(function() {
  202. test.assertNotNull(null, 'testAssertNotNull with null');
  203. });
  204. };
  205. exports.testAssertObject = function(test) {
  206. test.assertObject({}, 'assertObject with {}' );
  207. test.assertObject(new Object(), 'assertObject with new Object');
  208. test.expectFail(function() {
  209. test.assertObject('fail', 'assertObject with string');
  210. });
  211. };
  212. exports.testAssertString = function(test) {
  213. test.assertString('', 'assertString with ""');
  214. test.assertString(new String(), 'assertString with new String');
  215. };
  216. exports.testAssertArray = function(test) {
  217. test.assertArray([], 'assertArray with []');
  218. test.assertArray(new Array(), 'assertArray with new Array');
  219. };
  220. exports.testNumber = function(test) {
  221. test.assertNumber(1, 'assertNumber with 1');
  222. test.assertNumber(new Number('2'), 'assertNumber with new Number("2")' );
  223. };