test-test-utils.js 1.3 KB

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