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

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:e75ffde0f46c
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 'use strict'
6
7 const { setTimeout } = require('sdk/timers');
8 const { waitUntil } = require('sdk/test/utils');
9
10 exports.testWaitUntil = function (assert, done) {
11 let bool = false;
12 let finished = false;
13 waitUntil(() => {
14 if (finished)
15 assert.fail('interval should be cleared after predicate is truthy');
16 return bool;
17 }).then(function () {
18 assert.ok(bool,
19 'waitUntil shouldn\'t call until predicate is truthy');
20 finished = true;
21 done();
22 });
23 setTimeout(() => { bool = true; }, 20);
24 };
25
26 exports.testWaitUntilInterval = function (assert, done) {
27 let bool = false;
28 let finished = false;
29 let counter = 0;
30 waitUntil(() => {
31 if (finished)
32 assert.fail('interval should be cleared after predicate is truthy');
33 counter++;
34 return bool;
35 }, 50).then(function () {
36 assert.ok(bool,
37 'waitUntil shouldn\'t call until predicate is truthy');
38 assert.equal(counter, 1,
39 'predicate should only be called once with a higher interval');
40 finished = true;
41 done();
42 });
43 setTimeout(() => { bool = true; }, 10);
44 };
45
46 require('sdk/test').run(exports);

mercurial