|
1 const Cc = Components.classes; |
|
2 const Ci = Components.interfaces; |
|
3 |
|
4 // 5 seconds. |
|
5 const kExpectedDelay1 = 5; |
|
6 // 1 second. |
|
7 const kExpectedDelay2 = 1; |
|
8 |
|
9 var gStartTime1; |
|
10 var gStartTime2; |
|
11 var timer; |
|
12 |
|
13 var observer1 = { |
|
14 observe: function observeTC1(subject, topic, data) { |
|
15 if (topic == "timer-callback") { |
|
16 // Stop timer, so it doesn't repeat (if test runs slowly). |
|
17 timer.cancel(); |
|
18 |
|
19 // Actual delay may not be exact, so convert to seconds and round. |
|
20 do_check_eq(Math.round((Date.now() - gStartTime1) / 1000), |
|
21 kExpectedDelay1); |
|
22 |
|
23 timer = null; |
|
24 |
|
25 do_print("1st timer triggered (before being cancelled). Should not have happened!"); |
|
26 do_check_true(false); |
|
27 } |
|
28 } |
|
29 }; |
|
30 |
|
31 var observer2 = { |
|
32 observe: function observeTC2(subject, topic, data) { |
|
33 if (topic == "timer-callback") { |
|
34 // Stop timer, so it doesn't repeat (if test runs slowly). |
|
35 timer.cancel(); |
|
36 |
|
37 // Actual delay may not be exact, so convert to seconds and round. |
|
38 do_check_eq(Math.round((Date.now() - gStartTime2) / 1000), |
|
39 kExpectedDelay2); |
|
40 |
|
41 timer = null; |
|
42 |
|
43 do_test_finished(); |
|
44 } |
|
45 } |
|
46 }; |
|
47 |
|
48 function run_test() { |
|
49 do_test_pending(); |
|
50 |
|
51 timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); |
|
52 |
|
53 // Initialize the timer (with some delay), then cancel it. |
|
54 gStartTime1 = Date.now(); |
|
55 timer.init(observer1, kExpectedDelay1 * 1000, timer.TYPE_REPEATING_PRECISE); |
|
56 timer.cancel(); |
|
57 |
|
58 // Re-initialize the timer (with a different delay). |
|
59 gStartTime2 = Date.now(); |
|
60 timer.init(observer2, kExpectedDelay2 * 1000, timer.TYPE_REPEATING_PRECISE); |
|
61 } |