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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/test-test-utils.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,46 @@
     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 +
     1.8 +'use strict'
     1.9 +
    1.10 +const { setTimeout } = require('sdk/timers');
    1.11 +const { waitUntil } = require('sdk/test/utils');
    1.12 +
    1.13 +exports.testWaitUntil = function (assert, done) {
    1.14 +  let bool = false;
    1.15 +  let finished = false;
    1.16 +  waitUntil(() => {
    1.17 +    if (finished)
    1.18 +      assert.fail('interval should be cleared after predicate is truthy');
    1.19 +    return bool;
    1.20 +  }).then(function () {
    1.21 +    assert.ok(bool,
    1.22 +      'waitUntil shouldn\'t call until predicate is truthy');
    1.23 +    finished = true;
    1.24 +    done();
    1.25 +  });
    1.26 +  setTimeout(() => { bool = true; }, 20);
    1.27 +};
    1.28 +
    1.29 +exports.testWaitUntilInterval = function (assert, done) {
    1.30 +  let bool = false;
    1.31 +  let finished = false;
    1.32 +  let counter = 0;
    1.33 +  waitUntil(() => {
    1.34 +    if (finished)
    1.35 +      assert.fail('interval should be cleared after predicate is truthy');
    1.36 +    counter++;
    1.37 +    return bool;
    1.38 +  }, 50).then(function () {
    1.39 +    assert.ok(bool,
    1.40 +      'waitUntil shouldn\'t call until predicate is truthy');
    1.41 +    assert.equal(counter, 1,
    1.42 +      'predicate should only be called once with a higher interval');
    1.43 +    finished = true;
    1.44 +    done();
    1.45 +  });
    1.46 +  setTimeout(() => { bool = true; }, 10);
    1.47 +};
    1.48 +
    1.49 +require('sdk/test').run(exports);

mercurial