|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 Cu.import("resource://services-common/utils.js"); |
|
5 |
|
6 function run_test() { |
|
7 run_next_test(); |
|
8 } |
|
9 |
|
10 add_test(function test_required_args() { |
|
11 try { |
|
12 CommonUtils.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 |
|
21 add_test(function test_simple() { |
|
22 _("Test basic properties of CommonUtils.namedTimer."); |
|
23 |
|
24 const delay = 200; |
|
25 let that = {}; |
|
26 let t0 = Date.now(); |
|
27 CommonUtils.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 |
|
38 add_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 CommonUtils.namedTimer(callback, delay, that, "_zetimer"); |
|
51 CommonUtils.namedTimer(callback, 2 * delay, that, "_zetimer"); |
|
52 run_next_test(); |
|
53 }); |
|
54 |
|
55 add_test(function test_clear() { |
|
56 _("Test clearing a timer that hasn't fired yet."); |
|
57 |
|
58 const delay = 0; |
|
59 let that = {}; |
|
60 CommonUtils.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 CommonUtils.nextTick(run_next_test); |
|
67 |
|
68 run_next_test(); |
|
69 }); |