PageRenderTime 24ms CodeModel.GetById 13ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

/services/sync/tests/unit/test_utils_namedTimer.js

http://github.com/zpao/v8monkey
JavaScript | 69 lines | 52 code | 11 blank | 6 comment | 0 complexity | a39b62ade30b0634e2bb1d3b95901f24 MD5 | raw file
 1/* Any copyright is dedicated to the Public Domain.
 2   http://creativecommons.org/publicdomain/zero/1.0/ */
 3
 4Cu.import("resource://services-sync/util.js");
 5
 6function run_test() {
 7  run_next_test();
 8}
 9
10add_test(function test_required_args() {
11  try {
12    Utils.namedTimer(function callback() {
13      do_throw("Shouldn't fire.");
14    }, 0);
15    do_throw("Should have thrown!");
16  } catch(ex) {
17    run_next_test();
18  }
19});
20
21add_test(function test_simple() {
22  _("Test basic properties of Utils.namedTimer.");
23
24  const delay = 200;
25  let that = {};
26  let t0 = Date.now();
27  Utils.namedTimer(function callback(timer) {
28    do_check_eq(this, that);
29    do_check_eq(this._zetimer, null);
30    do_check_true(timer instanceof Ci.nsITimer);
31    // Difference should be ~delay, but hard to predict on all platforms,
32    // particularly Windows XP.
33    do_check_true(Date.now() > t0);
34    run_next_test();
35  }, delay, that, "_zetimer");
36});
37
38add_test(function test_delay() {
39  _("Test delaying a timer that hasn't fired yet.");
40  
41  const delay = 100;
42  let that = {};
43  let t0 = Date.now();
44  function callback(timer) {
45    // Difference should be ~2*delay, but hard to predict on all platforms,
46    // particularly Windows XP.
47    do_check_true((Date.now() - t0) > delay);
48    run_next_test();
49  }
50  Utils.namedTimer(callback, delay, that, "_zetimer");
51  Utils.namedTimer(callback, 2 * delay, that, "_zetimer");
52  run_next_test();
53});
54
55add_test(function test_clear() {
56  _("Test clearing a timer that hasn't fired yet.");
57
58  const delay = 0;
59  let that = {};
60  Utils.namedTimer(function callback(timer) {
61    do_throw("Shouldn't fire!");
62  }, delay, that, "_zetimer");
63
64  that._zetimer.clear();
65  do_check_eq(that._zetimer, null);
66  Utils.nextTick(run_next_test);
67
68  run_next_test();
69});