addon-sdk/source/test/test-unit-test.js

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

mercurial