aboutsummaryrefslogtreecommitdiff
path: root/tools/addon-sdk-1.5/packages/api-utils/tests/test-timer.js
blob: bf6917c3a9ad1d06aff3d8569e85419721216d6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

var timer = require("timer");
const { Loader } = require("./helpers");

exports.testSetTimeout = function(test) {
  timer.setTimeout(function() {
    test.pass("testSetTimeout passed");
    test.done();
  }, 1);
  test.waitUntilDone();
};

exports.testParamedSetTimeout = function(test) {
  let params = [1, 'foo', { bar: 'test' }, null, undefined];
  timer.setTimeout.apply(null, [function() {
    test.assertEqual(arguments.length, params.length);
    for (let i = 0, ii = params.length; i < ii; i++)
      test.assertEqual(params[i], arguments[i]);
    test.done();
  }, 1].concat(params));
  test.waitUntilDone();
};

exports.testClearTimeout = function(test) {
  var myFunc = function myFunc() {
    test.fail("myFunc() should not be called in testClearTimeout");
  };
  var id = timer.setTimeout(myFunc, 1);
  timer.setTimeout(function() {
    test.pass("testClearTimeout passed");
    test.done();
  }, 2);
  timer.clearTimeout(id);
  test.waitUntilDone();
};

exports.testParamedClearTimeout = function(test) {
  let params = [1, 'foo', { bar: 'test' }, null, undefined];
  var myFunc = function myFunc() {
    test.fail("myFunc() should not be called in testClearTimeout");
  };
  var id = timer.setTimeout(myFunc, 1);
  timer.setTimeout.apply(null, [function() {
    test.assertEqual(arguments.length, params.length);
    for (let i = 0, ii = params.length; i < ii; i++)
      test.assertEqual(params[i], arguments[i]);
    test.done();
  }, 1].concat(params));
  timer.clearTimeout(id);
  test.waitUntilDone();
};

exports.testSetInterval = function (test) {
  var count = 0;
  var id = timer.setInterval(function () {
    count++;
    if (count >= 5) {
      timer.clearInterval(id);
      test.pass("testSetInterval passed");
      test.done();
    }
  }, 1);
  test.waitUntilDone();
};

exports.testParamedSetInerval = function(test) {
  let params = [1, 'foo', { bar: 'test' }, null, undefined];
  let count = 0;
  let id = timer.setInterval.apply(null, [function() {
    count ++;
    if (count < 5) {
      test.assertEqual(arguments.length, params.length);
      for (let i = 0, ii = params.length; i < ii; i++)
        test.assertEqual(params[i], arguments[i]);
    } else {
      timer.clearInterval(id);
      test.done();
    }
  }, 1].concat(params));
  test.waitUntilDone();
};

exports.testClearInterval = function (test) {
  timer.clearInterval(timer.setInterval(function () {
    test.fail("setInterval callback should not be called");
  }, 1));
  var id = timer.setInterval(function () {
    timer.clearInterval(id);
    test.pass("testClearInterval passed");
    test.done();
  }, 2);
  test.waitUntilDone();
};

exports.testParamedClearInterval = function(test) {
  timer.clearInterval(timer.setInterval(function () {
    test.fail("setInterval callback should not be called");
  }, 1, timer, {}, null));

  let id = timer.setInterval(function() {
    timer.clearInterval(id);
    test.assertEqual(3, arguments.length);
    test.done();
  }, 2, undefined, 'test', {});
  test.waitUntilDone();
};


exports.testUnload = function(test) {
  var loader = Loader(module);
  var sbtimer = loader.require("timer");

  var myFunc = function myFunc() {
    test.fail("myFunc() should not be called in testUnload");
  };

  sbtimer.setTimeout(myFunc, 1);
  sbtimer.setTimeout(myFunc, 1, 'foo', 4, {}, undefined);
  sbtimer.setInterval(myFunc, 1);
  sbtimer.setInterval(myFunc, 1, {}, null, 'bar', undefined, 87);
  loader.unload();
  timer.setTimeout(function() {
    test.pass("timer testUnload passed");
    test.done();
  }, 2);
  test.waitUntilDone();
};