|
1 XPCOMUtils.defineLazyModuleGetter(this, "AlarmService", |
|
2 "resource://gre/modules/AlarmService.jsm"); |
|
3 |
|
4 /* |
|
5 * Tests for Bug 867868 and related Alarm API bugs. |
|
6 * |
|
7 * NOTE: These tests pass the alarm time to AlarmService as a number and not as |
|
8 * a date. See bug 810973 about Date truncating milliseconds. AlarmService does |
|
9 * not officially allow a integer, but nor does it disallow it. Of course this |
|
10 * test will break if AlarmService adds a type check, hence this note. |
|
11 * FIXME: when bug 810973 is fixed. |
|
12 */ |
|
13 |
|
14 function add_alarm_future(cb) { |
|
15 let alarmId = undefined; |
|
16 AlarmService.add({ |
|
17 date: new Date(Date.now() + 143), |
|
18 ignoreTimezone: true |
|
19 }, |
|
20 function onAlarmFired(aAlarm) { |
|
21 ok(alarmId === aAlarm.id, "Future alarm fired successfully."); |
|
22 cb(); |
|
23 }, |
|
24 function onSuccess(aAlarmId) { |
|
25 alarmId = aAlarmId; |
|
26 }, |
|
27 function onError(error) { |
|
28 ok(false, "Unexpected error adding future alarm " + error); |
|
29 cb(); |
|
30 }); |
|
31 } |
|
32 |
|
33 function add_alarm_present(cb) { |
|
34 let self = this; |
|
35 let alarmId = undefined; |
|
36 AlarmService.add({ |
|
37 date: new Date(), |
|
38 ignoreTimezone: true |
|
39 }, |
|
40 function onAlarmFired(aAlarm) { |
|
41 ok(alarmId === aAlarm.id, "Present alarm fired successfully."); |
|
42 cb(); |
|
43 }, |
|
44 function onSuccess(aAlarmId) { |
|
45 alarmId = aAlarmId; |
|
46 }, function onError(error) { |
|
47 ok(false, "Unexpected error adding alarm for current time " + error); |
|
48 cb(); |
|
49 }); |
|
50 } |
|
51 |
|
52 function add_alarm_past(cb) { |
|
53 let self = this; |
|
54 let alarmId = undefined; |
|
55 AlarmService.add({ |
|
56 date: new Date(Date.now() - 5), |
|
57 ignoreTimezone: true |
|
58 }, |
|
59 function onAlarmFired(aAlarm) { |
|
60 ok(alarmId === aAlarm.id, "Past alarm fired successfully."); |
|
61 cb(); |
|
62 }, |
|
63 function onSuccess(aAlarmId) { |
|
64 alarmId = aAlarmId; |
|
65 }, |
|
66 function onError(error) { |
|
67 ok(false, "Unexpected error adding alarm for time in the past " + error); |
|
68 cb(); |
|
69 }); |
|
70 } |
|
71 |
|
72 function trigger_all_alarms(cb) { |
|
73 let n = 10; |
|
74 let counter = 0; |
|
75 let date = new Date(Date.now() + 57); |
|
76 function onAlarmFired() { |
|
77 counter++; |
|
78 info("trigger_all_alarms count " + counter); |
|
79 if (counter == n) { |
|
80 ok(true, "All " + n + " alarms set to a particular time fired."); |
|
81 cb(); |
|
82 } |
|
83 } |
|
84 |
|
85 for (let i = 0; i < n; i++) { |
|
86 AlarmService.add( |
|
87 { |
|
88 date: date, |
|
89 ignoreTimezone: true |
|
90 }, |
|
91 onAlarmFired |
|
92 ); |
|
93 } |
|
94 } |
|
95 |
|
96 function multiple_handlers(cb) { |
|
97 let d = new Date(Date.now() + 100); |
|
98 let called = 0; |
|
99 |
|
100 function done() { |
|
101 if (called == 2) { |
|
102 ok(true, "Two alarms for the same time fired."); |
|
103 cb(); |
|
104 } |
|
105 } |
|
106 |
|
107 function handler1() { |
|
108 called++; |
|
109 done(); |
|
110 } |
|
111 |
|
112 function handler2() { |
|
113 called++; |
|
114 done(); |
|
115 } |
|
116 |
|
117 AlarmService.add( |
|
118 { |
|
119 date: d, |
|
120 ignoreTimezone: true |
|
121 }, |
|
122 handler1 |
|
123 ); |
|
124 AlarmService.add( |
|
125 { |
|
126 date: d, |
|
127 ignoreTimezone: true |
|
128 }, |
|
129 handler2 |
|
130 ); |
|
131 } |
|
132 |
|
133 function same_time_alarms(cb) { |
|
134 var fired = 0; |
|
135 var delay = new Date(Date.now() + 100); |
|
136 |
|
137 function check() { |
|
138 fired++; |
|
139 if (fired == 4) { |
|
140 ok(true, "All alarms set for the same time fired."); |
|
141 cb(); |
|
142 } |
|
143 } |
|
144 |
|
145 function addImmediateAlarm() { |
|
146 fired++; |
|
147 AlarmService.add({ |
|
148 date: delay, |
|
149 ignoreTimezone: true |
|
150 }, check); |
|
151 } |
|
152 |
|
153 AlarmService.add({ |
|
154 date: delay, |
|
155 ignoreTimezone: true |
|
156 }, addImmediateAlarm); |
|
157 |
|
158 AlarmService.add({ |
|
159 date: delay, |
|
160 ignoreTimezone: true |
|
161 }, addImmediateAlarm); |
|
162 } |
|
163 |
|
164 function test() { |
|
165 var tests = [ |
|
166 add_alarm_future, |
|
167 add_alarm_present, |
|
168 add_alarm_past, |
|
169 trigger_all_alarms, |
|
170 multiple_handlers, |
|
171 same_time_alarms |
|
172 ] |
|
173 |
|
174 var testIndex = -1; |
|
175 function nextTest() { |
|
176 testIndex++; |
|
177 if (testIndex >= tests.length) |
|
178 return; |
|
179 |
|
180 waitForExplicitFinish(); |
|
181 tests[testIndex](function() { |
|
182 finish(); |
|
183 nextTest(); |
|
184 }); |
|
185 } |
|
186 nextTest(); |
|
187 } |