1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";
add_task(function* test_alarm_without_permissions() {
function backgroundScript() {
browser.test.assertTrue(!browser.alarms,
"alarm API is not available when the alarm permission is not required");
browser.test.notifyPass("alarms_permission");
}
let extension = ExtensionTestUtils.loadExtension({
background: `(${backgroundScript})()`,
manifest: {
permissions: [],
},
});
yield extension.startup();
yield extension.awaitFinish("alarms_permission");
yield extension.unload();
});
add_task(function* test_alarm_fires() {
function backgroundScript() {
let ALARM_NAME = "test_ext_alarms";
let timer;
browser.alarms.onAlarm.addListener(alarm => {
browser.test.assertEq(ALARM_NAME, alarm.name, "alarm has the correct name");
clearTimeout(timer);
browser.test.notifyPass("alarm-fires");
});
browser.alarms.create(ALARM_NAME, {delayInMinutes: 0.02});
timer = setTimeout(async () => {
browser.test.fail("alarm fired within expected time");
let wasCleared = await browser.alarms.clear(ALARM_NAME);
browser.test.assertTrue(wasCleared, "alarm was cleared");
browser.test.notifyFail("alarm-fires");
}, 10000);
}
let extension = ExtensionTestUtils.loadExtension({
background: `(${backgroundScript})()`,
manifest: {
permissions: ["alarms"],
},
});
yield extension.startup();
yield extension.awaitFinish("alarm-fires");
yield extension.unload();
});
add_task(function* test_alarm_fires_with_when() {
function backgroundScript() {
let ALARM_NAME = "test_ext_alarms";
let timer;
browser.alarms.onAlarm.addListener(alarm => {
browser.test.assertEq(ALARM_NAME, alarm.name, "alarm has the expected name");
clearTimeout(timer);
browser.test.notifyPass("alarm-when");
});
browser.alarms.create(ALARM_NAME, {when: Date.now() + 1000});
timer = setTimeout(async () => {
browser.test.fail("alarm fired within expected time");
let wasCleared = await browser.alarms.clear(ALARM_NAME);
browser.test.assertTrue(wasCleared, "alarm was cleared");
browser.test.notifyFail("alarm-when");
}, 10000);
}
let extension = ExtensionTestUtils.loadExtension({
background: `(${backgroundScript})()`,
manifest: {
permissions: ["alarms"],
},
});
yield extension.startup();
yield extension.awaitFinish("alarm-when");
yield extension.unload();
});
add_task(function* test_alarm_clear_non_matching_name() {
async function backgroundScript() {
let ALARM_NAME = "test_ext_alarms";
browser.alarms.create(ALARM_NAME, {when: Date.now() + 2000});
let wasCleared = await browser.alarms.clear(ALARM_NAME + "1");
browser.test.assertFalse(wasCleared, "alarm was not cleared");
let alarms = await browser.alarms.getAll();
browser.test.assertEq(1, alarms.length, "alarm was not removed");
browser.test.notifyPass("alarm-clear");
}
let extension = ExtensionTestUtils.loadExtension({
background: `(${backgroundScript})()`,
manifest: {
permissions: ["alarms"],
},
});
yield extension.startup();
yield extension.awaitFinish("alarm-clear");
yield extension.unload();
});
add_task(function* test_alarm_get_and_clear_single_argument() {
async function backgroundScript() {
browser.alarms.create({when: Date.now() + 2000});
let alarm = await browser.alarms.get();
browser.test.assertEq("", alarm.name, "expected alarm returned");
let wasCleared = await browser.alarms.clear();
browser.test.assertTrue(wasCleared, "alarm was cleared");
let alarms = await browser.alarms.getAll();
browser.test.assertEq(0, alarms.length, "alarm was removed");
browser.test.notifyPass("alarm-single-arg");
}
let extension = ExtensionTestUtils.loadExtension({
background: `(${backgroundScript})()`,
manifest: {
permissions: ["alarms"],
},
});
yield extension.startup();
yield extension.awaitFinish("alarm-single-arg");
yield extension.unload();
});
add_task(function* test_get_get_all_clear_all_alarms() {
async function backgroundScript() {
const ALARM_NAME = "test_alarm";
let suffixes = [0, 1, 2];
for (let suffix of suffixes) {
browser.alarms.create(ALARM_NAME + suffix, {when: Date.now() + (suffix + 1) * 10000});
}
let alarms = await browser.alarms.getAll();
browser.test.assertEq(suffixes.length, alarms.length, "expected number of alarms were found");
alarms.forEach((alarm, index) => {
browser.test.assertEq(ALARM_NAME + index, alarm.name, "alarm has the expected name");
});
for (let suffix of suffixes) {
let alarm = await browser.alarms.get(ALARM_NAME + suffix);
browser.test.assertEq(ALARM_NAME + suffix, alarm.name, "alarm has the expected name");
browser.test.sendMessage(`get-${suffix}`);
}
let wasCleared = await browser.alarms.clear(ALARM_NAME + suffixes[0]);
browser.test.assertTrue(wasCleared, "alarm was cleared");
alarms = await browser.alarms.getAll();
browser.test.assertEq(2, alarms.length, "alarm was removed");
let alarm = await browser.alarms.get(ALARM_NAME + suffixes[0]);
browser.test.assertEq(undefined, alarm, "non-existent alarm is undefined");
browser.test.sendMessage(`get-invalid`);
wasCleared = await browser.alarms.clearAll();
browser.test.assertTrue(wasCleared, "alarms were cleared");
alarms = await browser.alarms.getAll();
browser.test.assertEq(0, alarms.length, "no alarms exist");
browser.test.sendMessage("clearAll");
browser.test.sendMessage("clear");
browser.test.sendMessage("getAll");
}
let extension = ExtensionTestUtils.loadExtension({
background: `(${backgroundScript})()`,
manifest: {
permissions: ["alarms"],
},
});
yield Promise.all([
extension.startup(),
extension.awaitMessage("getAll"),
extension.awaitMessage("get-0"),
extension.awaitMessage("get-1"),
extension.awaitMessage("get-2"),
extension.awaitMessage("clear"),
extension.awaitMessage("get-invalid"),
extension.awaitMessage("clearAll"),
]);
yield extension.unload();
});
|