test-timer.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. var timer = require("sdk/timers");
  5. const { Loader } = require("sdk/test/loader");
  6. exports.testSetTimeout = function(assert, end) {
  7. timer.setTimeout(function() {
  8. assert.pass("testSetTimeout passed");
  9. end();
  10. }, 1);
  11. };
  12. exports.testParamedSetTimeout = function(assert, end) {
  13. let params = [1, 'foo', { bar: 'test' }, null, undefined];
  14. timer.setTimeout.apply(null, [function() {
  15. assert.equal(arguments.length, params.length);
  16. for (let i = 0, ii = params.length; i < ii; i++)
  17. assert.equal(params[i], arguments[i]);
  18. end();
  19. }, 1].concat(params));
  20. };
  21. exports.testClearTimeout = function(assert, end) {
  22. var myFunc = function myFunc() {
  23. assert.fail("myFunc() should not be called in testClearTimeout");
  24. };
  25. var id = timer.setTimeout(myFunc, 1);
  26. timer.setTimeout(function() {
  27. assert.pass("testClearTimeout passed");
  28. end();
  29. }, 2);
  30. timer.clearTimeout(id);
  31. };
  32. exports.testParamedClearTimeout = function(assert, end) {
  33. let params = [1, 'foo', { bar: 'test' }, null, undefined];
  34. var myFunc = function myFunc() {
  35. assert.fail("myFunc() should not be called in testClearTimeout");
  36. };
  37. var id = timer.setTimeout(myFunc, 1);
  38. timer.setTimeout.apply(null, [function() {
  39. assert.equal(arguments.length, params.length);
  40. for (let i = 0, ii = params.length; i < ii; i++)
  41. assert.equal(params[i], arguments[i]);
  42. end();
  43. }, 1].concat(params));
  44. timer.clearTimeout(id);
  45. };
  46. exports.testSetInterval = function (assert, end) {
  47. var count = 0;
  48. var id = timer.setInterval(function () {
  49. count++;
  50. if (count >= 5) {
  51. timer.clearInterval(id);
  52. assert.pass("testSetInterval passed");
  53. end();
  54. }
  55. }, 1);
  56. };
  57. exports.testParamedSetInerval = function(assert, end) {
  58. let params = [1, 'foo', { bar: 'test' }, null, undefined];
  59. let count = 0;
  60. let id = timer.setInterval.apply(null, [function() {
  61. count ++;
  62. if (count < 5) {
  63. assert.equal(arguments.length, params.length);
  64. for (let i = 0, ii = params.length; i < ii; i++)
  65. assert.equal(params[i], arguments[i]);
  66. } else {
  67. timer.clearInterval(id);
  68. end();
  69. }
  70. }, 1].concat(params));
  71. };
  72. exports.testClearInterval = function (assert, end) {
  73. timer.clearInterval(timer.setInterval(function () {
  74. assert.fail("setInterval callback should not be called");
  75. }, 1));
  76. var id = timer.setInterval(function () {
  77. timer.clearInterval(id);
  78. assert.pass("testClearInterval passed");
  79. end();
  80. }, 2);
  81. };
  82. exports.testParamedClearInterval = function(assert, end) {
  83. timer.clearInterval(timer.setInterval(function () {
  84. assert.fail("setInterval callback should not be called");
  85. }, 1, timer, {}, null));
  86. let id = timer.setInterval(function() {
  87. timer.clearInterval(id);
  88. assert.equal(3, arguments.length);
  89. end();
  90. }, 2, undefined, 'test', {});
  91. };
  92. exports.testImmediate = function(assert, end) {
  93. let actual = [];
  94. let ticks = 0;
  95. timer.setImmediate(function(...params) {
  96. actual.push(params);
  97. assert.equal(ticks, 1, "is a next tick");
  98. assert.deepEqual(actual, [["start", "immediates"]]);
  99. }, "start", "immediates");
  100. timer.setImmediate(function(...params) {
  101. actual.push(params);
  102. assert.deepEqual(actual, [["start", "immediates"],
  103. ["added"]]);
  104. assert.equal(ticks, 1, "is a next tick");
  105. timer.setImmediate(function(...params) {
  106. actual.push(params);
  107. assert.equal(ticks, 2, "is second tick");
  108. assert.deepEqual(actual, [["start", "immediates"],
  109. ["added"],
  110. [],
  111. ["last", "immediate", "handler"],
  112. ["side-effect"]]);
  113. end();
  114. }, "side-effect");
  115. }, "added");
  116. timer.setImmediate(function(...params) {
  117. actual.push(params);
  118. assert.equal(ticks, 1, "is a next tick");
  119. assert.deepEqual(actual, [["start", "immediates"],
  120. ["added"],
  121. []]);
  122. timer.clearImmediate(removeID);
  123. });
  124. function removed() {
  125. assert.fail("should be removed");
  126. }
  127. let removeID = timer.setImmediate(removed);
  128. timer.setImmediate(function(...params) {
  129. actual.push(params);
  130. assert.equal(ticks, 1, "is a next tick");
  131. assert.deepEqual(actual, [["start", "immediates"],
  132. ["added"],
  133. [],
  134. ["last", "immediate", "handler"]]);
  135. ticks = ticks + 1;
  136. }, "last", "immediate", "handler");
  137. ticks = ticks + 1;
  138. };
  139. exports.testUnload = function(assert, end) {
  140. var loader = Loader(module);
  141. var sbtimer = loader.require("sdk/timers");
  142. var myFunc = function myFunc() {
  143. assert.fail("myFunc() should not be called in testUnload");
  144. };
  145. sbtimer.setTimeout(myFunc, 1);
  146. sbtimer.setTimeout(myFunc, 1, 'foo', 4, {}, undefined);
  147. sbtimer.setInterval(myFunc, 1);
  148. sbtimer.setInterval(myFunc, 1, {}, null, 'bar', undefined, 87);
  149. loader.unload();
  150. timer.setTimeout(function() {
  151. assert.pass("timer testUnload passed");
  152. end();
  153. }, 2);
  154. };
  155. require("test").run(exports);