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