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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/test-timer.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,177 @@
     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 +var timer = require("sdk/timers");
     1.9 +const { Loader } = require("sdk/test/loader");
    1.10 +
    1.11 +exports.testSetTimeout = function(assert, end) {
    1.12 +  timer.setTimeout(function() {
    1.13 +    assert.pass("testSetTimeout passed");
    1.14 +    end();
    1.15 +  }, 1);
    1.16 +};
    1.17 +
    1.18 +exports.testParamedSetTimeout = function(assert, end) {
    1.19 +  let params = [1, 'foo', { bar: 'test' }, null, undefined];
    1.20 +  timer.setTimeout.apply(null, [function() {
    1.21 +    assert.equal(arguments.length, params.length);
    1.22 +    for (let i = 0, ii = params.length; i < ii; i++)
    1.23 +      assert.equal(params[i], arguments[i]);
    1.24 +    end();
    1.25 +  }, 1].concat(params));
    1.26 +};
    1.27 +
    1.28 +exports.testClearTimeout = function(assert, end) {
    1.29 +  var myFunc = function myFunc() {
    1.30 +    assert.fail("myFunc() should not be called in testClearTimeout");
    1.31 +  };
    1.32 +  var id = timer.setTimeout(myFunc, 1);
    1.33 +  timer.setTimeout(function() {
    1.34 +    assert.pass("testClearTimeout passed");
    1.35 +    end();
    1.36 +  }, 2);
    1.37 +  timer.clearTimeout(id);
    1.38 +};
    1.39 +
    1.40 +exports.testParamedClearTimeout = function(assert, end) {
    1.41 +  let params = [1, 'foo', { bar: 'test' }, null, undefined];
    1.42 +  var myFunc = function myFunc() {
    1.43 +    assert.fail("myFunc() should not be called in testClearTimeout");
    1.44 +  };
    1.45 +  var id = timer.setTimeout(myFunc, 1);
    1.46 +  timer.setTimeout.apply(null, [function() {
    1.47 +    assert.equal(arguments.length, params.length);
    1.48 +    for (let i = 0, ii = params.length; i < ii; i++)
    1.49 +      assert.equal(params[i], arguments[i]);
    1.50 +    end();
    1.51 +  }, 1].concat(params));
    1.52 +  timer.clearTimeout(id);
    1.53 +};
    1.54 +
    1.55 +exports.testSetInterval = function (assert, end) {
    1.56 +  var count = 0;
    1.57 +  var id = timer.setInterval(function () {
    1.58 +    count++;
    1.59 +    if (count >= 5) {
    1.60 +      timer.clearInterval(id);
    1.61 +      assert.pass("testSetInterval passed");
    1.62 +      end();
    1.63 +    }
    1.64 +  }, 1);
    1.65 +};
    1.66 +
    1.67 +exports.testParamedSetInerval = function(assert, end) {
    1.68 +  let params = [1, 'foo', { bar: 'test' }, null, undefined];
    1.69 +  let count = 0;
    1.70 +  let id = timer.setInterval.apply(null, [function() {
    1.71 +    count ++;
    1.72 +    if (count < 5) {
    1.73 +      assert.equal(arguments.length, params.length);
    1.74 +      for (let i = 0, ii = params.length; i < ii; i++)
    1.75 +        assert.equal(params[i], arguments[i]);
    1.76 +    } else {
    1.77 +      timer.clearInterval(id);
    1.78 +      end();
    1.79 +    }
    1.80 +  }, 1].concat(params));
    1.81 +};
    1.82 +
    1.83 +exports.testClearInterval = function (assert, end) {
    1.84 +  timer.clearInterval(timer.setInterval(function () {
    1.85 +    assert.fail("setInterval callback should not be called");
    1.86 +  }, 1));
    1.87 +  var id = timer.setInterval(function () {
    1.88 +    timer.clearInterval(id);
    1.89 +    assert.pass("testClearInterval passed");
    1.90 +    end();
    1.91 +  }, 2);
    1.92 +};
    1.93 +
    1.94 +exports.testParamedClearInterval = function(assert, end) {
    1.95 +  timer.clearInterval(timer.setInterval(function () {
    1.96 +    assert.fail("setInterval callback should not be called");
    1.97 +  }, 1, timer, {}, null));
    1.98 +
    1.99 +  let id = timer.setInterval(function() {
   1.100 +    timer.clearInterval(id);
   1.101 +    assert.equal(3, arguments.length);
   1.102 +    end();
   1.103 +  }, 2, undefined, 'test', {});
   1.104 +};
   1.105 +
   1.106 +
   1.107 +exports.testImmediate = function(assert, end) {
   1.108 +  let actual = [];
   1.109 +  let ticks = 0;
   1.110 +  timer.setImmediate(function(...params) {
   1.111 +    actual.push(params);
   1.112 +    assert.equal(ticks, 1, "is a next tick");
   1.113 +    assert.deepEqual(actual, [["start", "immediates"]]);
   1.114 +  }, "start", "immediates");
   1.115 +
   1.116 +  timer.setImmediate(function(...params) {
   1.117 +    actual.push(params);
   1.118 +    assert.deepEqual(actual, [["start", "immediates"],
   1.119 +                                  ["added"]]);
   1.120 +    assert.equal(ticks, 1, "is a next tick");
   1.121 +    timer.setImmediate(function(...params) {
   1.122 +      actual.push(params);
   1.123 +      assert.equal(ticks, 2, "is second tick");
   1.124 +      assert.deepEqual(actual, [["start", "immediates"],
   1.125 +                                    ["added"],
   1.126 +                                    [],
   1.127 +                                    ["last", "immediate", "handler"],
   1.128 +                                    ["side-effect"]]);
   1.129 +      end();
   1.130 +    }, "side-effect");
   1.131 +  }, "added");
   1.132 +
   1.133 +  timer.setImmediate(function(...params) {
   1.134 +    actual.push(params);
   1.135 +    assert.equal(ticks, 1, "is a next tick");
   1.136 +    assert.deepEqual(actual, [["start", "immediates"],
   1.137 +                              ["added"],
   1.138 +                              []]);
   1.139 +    timer.clearImmediate(removeID);
   1.140 +  });
   1.141 +
   1.142 +  function removed() {
   1.143 +    assert.fail("should be removed");
   1.144 +  }
   1.145 +  let removeID = timer.setImmediate(removed);
   1.146 +
   1.147 +  timer.setImmediate(function(...params) {
   1.148 +    actual.push(params);
   1.149 +    assert.equal(ticks, 1, "is a next tick");
   1.150 +    assert.deepEqual(actual, [["start", "immediates"],
   1.151 +                              ["added"],
   1.152 +                              [],
   1.153 +                              ["last", "immediate", "handler"]]);
   1.154 +    ticks = ticks + 1;
   1.155 +  }, "last", "immediate", "handler");
   1.156 +
   1.157 +
   1.158 +  ticks = ticks + 1;
   1.159 +};
   1.160 +
   1.161 +exports.testUnload = function(assert, end) {
   1.162 +  var loader = Loader(module);
   1.163 +  var sbtimer = loader.require("sdk/timers");
   1.164 +
   1.165 +  var myFunc = function myFunc() {
   1.166 +    assert.fail("myFunc() should not be called in testUnload");
   1.167 +  };
   1.168 +
   1.169 +  sbtimer.setTimeout(myFunc, 1);
   1.170 +  sbtimer.setTimeout(myFunc, 1, 'foo', 4, {}, undefined);
   1.171 +  sbtimer.setInterval(myFunc, 1);
   1.172 +  sbtimer.setInterval(myFunc, 1, {}, null, 'bar', undefined, 87);
   1.173 +  loader.unload();
   1.174 +  timer.setTimeout(function() {
   1.175 +    assert.pass("timer testUnload passed");
   1.176 +    end();
   1.177 +  }, 2);
   1.178 +};
   1.179 +
   1.180 +require("test").run(exports);
   1.181 \ No newline at end of file

mercurial