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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";
AddonTestUtils.init(this);
AddonTestUtils.overrideCertDB();
AddonTestUtils.createAppInfo(
"xpcshell@tests.mozilla.org",
"XPCShell",
"42",
"42"
);
add_task(async function setup() {
Services.prefs.setBoolPref(
"extensions.webextOptionalPermissionPrompts",
false
);
registerCleanupFunction(() => {
Services.prefs.clearUserPref("extensions.webextOptionalPermissionPrompts");
});
await ExtensionTestUtils.startAddonManager();
});
add_task(async function test_management_permission() {
async function background() {
const permObj = { permissions: ["management"] };
let hasPerm = await browser.permissions.contains(permObj);
browser.test.assertTrue(!hasPerm, "does not have management permission");
browser.test.assertTrue(
!!browser.management,
"management namespace exists"
);
// These require permission
let requires_permission = [
"getAll",
"get",
"install",
"setEnabled",
"onDisabled",
"onEnabled",
"onInstalled",
"onUninstalled",
];
async function testAvailable() {
// These are always available regardless of permission.
for (let fn of ["getSelf", "uninstallSelf"]) {
browser.test.assertTrue(
!!browser.management[fn],
`management.${fn} exists`
);
}
let hasPerm = await browser.permissions.contains(permObj);
for (let fn of requires_permission) {
browser.test.assertEq(
hasPerm,
!!browser.management[fn],
`management.${fn} does not exist`
);
}
}
await testAvailable();
browser.test.onMessage.addListener(async msg => {
browser.test.log("test with permission");
// get permission
await browser.permissions.request(permObj);
let hasPerm = await browser.permissions.contains(permObj);
browser.test.assertTrue(
hasPerm,
"management permission.request accepted"
);
await testAvailable();
browser.management.onInstalled.addListener(() => {
browser.test.fail("onInstalled listener invoked");
});
browser.test.log("test without permission");
// remove permission
await browser.permissions.remove(permObj);
hasPerm = await browser.permissions.contains(permObj);
browser.test.assertFalse(
hasPerm,
"management permission.request removed"
);
await testAvailable();
browser.test.sendMessage("done");
});
browser.test.sendMessage("started");
}
let extension = ExtensionTestUtils.loadExtension({
manifest: {
browser_specific_settings: {
gecko: {
id: "management@test",
},
},
optional_permissions: ["management"],
},
background,
useAddonManager: "temporary",
});
await extension.startup();
await extension.awaitMessage("started");
await withHandlingUserInput(extension, async () => {
extension.sendMessage("request");
});
await extension.awaitMessage("done");
// Verify the onInstalled listener does not get used.
// The listener will make the test fail if fired.
let ext2 = ExtensionTestUtils.loadExtension({
manifest: {
browser_specific_settings: {
gecko: {
id: "on-installed@test",
},
},
optional_permissions: ["management"],
},
useAddonManager: "temporary",
});
await ext2.startup();
await ext2.unload();
await extension.unload();
});
add_task(async function test_management_getAll() {
const id1 = "get_all_test1@tests.mozilla.com";
const id2 = "get_all_test2@tests.mozilla.com";
function getManifest(id) {
return {
browser_specific_settings: {
gecko: {
id,
},
},
name: id,
version: "1.0",
short_name: id,
permissions: ["management"],
};
}
async function background() {
browser.test.onMessage.addListener(async (msg, id) => {
let addon = await browser.management.get(id);
browser.test.sendMessage("addon", addon);
});
let addons = await browser.management.getAll();
browser.test.assertEq(
2,
addons.length,
"management.getAll returned correct number of add-ons."
);
browser.test.sendMessage("addons", addons);
}
let extension1 = ExtensionTestUtils.loadExtension({
manifest: getManifest(id1),
useAddonManager: "temporary",
});
let extension2 = ExtensionTestUtils.loadExtension({
manifest: getManifest(id2),
background,
useAddonManager: "temporary",
});
await extension1.startup();
await extension2.startup();
let addons = await extension2.awaitMessage("addons");
for (let id of [id1, id2]) {
let addon = addons.find(a => {
return a.id === id;
});
equal(
addon.name,
id,
`The extension with id ${id} was returned by getAll.`
);
equal(addon.shortName, id, "Additional extension metadata was correct");
}
extension2.sendMessage("getAddon", id1);
let addon = await extension2.awaitMessage("addon");
equal(addon.name, id1, `The extension with id ${id1} was returned by get.`);
equal(addon.shortName, id1, "Additional extension metadata was correct");
extension2.sendMessage("getAddon", id2);
addon = await extension2.awaitMessage("addon");
equal(addon.name, id2, `The extension with id ${id2} was returned by get.`);
equal(addon.shortName, id2, "Additional extension metadata was correct");
await extension2.unload();
await extension1.unload();
});
add_task(
{ pref_set: [["extensions.eventPages.enabled", true]] },
async function test_management_event_page() {
let extension = ExtensionTestUtils.loadExtension({
useAddonManager: "permanent",
manifest: {
permissions: ["management"],
background: { persistent: false },
},
background() {
browser.management.onInstalled.addListener(details => {
browser.test.sendMessage("onInstalled", details);
});
browser.management.onUninstalled.addListener(details => {
browser.test.sendMessage("onUninstalled", details);
});
browser.management.onEnabled.addListener(() => {
browser.test.sendMessage("onEnabled");
});
browser.management.onDisabled.addListener(() => {
browser.test.sendMessage("onDisabled");
});
},
});
await extension.startup();
let events = ["onInstalled", "onUninstalled", "onEnabled", "onDisabled"];
for (let event of events) {
assertPersistentListeners(extension, "management", event, {
primed: false,
});
}
await extension.terminateBackground();
for (let event of events) {
assertPersistentListeners(extension, "management", event, {
primed: true,
});
}
let testExt = ExtensionTestUtils.loadExtension({
useAddonManager: "permanent",
manifest: {
browser_specific_settings: { gecko: { id: "test-ext@mochitest" } },
},
background() {},
});
await testExt.startup();
let details = await extension.awaitMessage("onInstalled");
equal(testExt.id, details.id, "got onInstalled event");
await AddonTestUtils.promiseRestartManager();
await extension.awaitStartup();
await testExt.awaitStartup();
for (let event of events) {
assertPersistentListeners(extension, "management", event, {
primed: true,
});
}
// Test uninstalling an addon wakes up the watching extension.
let uninstalled = testExt.unload();
details = await extension.awaitMessage("onUninstalled");
equal(testExt.id, details.id, "got onUninstalled event");
await extension.unload();
await uninstalled;
}
);
// Sanity check that Addon listeners are removed on context close.
add_task(
{
// __AddonManagerInternal__ is exposed for debug builds only.
skip_if: () => !AppConstants.DEBUG,
},
async function test_management_unregister_listener() {
let extension = ExtensionTestUtils.loadExtension({
manifest: {
permissions: ["management"],
},
files: {
"extpage.html": `<!DOCTYPE html><script src="extpage.js"></script>`,
"extpage.js": function () {
browser.management.onInstalled.addListener(() => {});
},
},
});
await extension.startup();
const page = await ExtensionTestUtils.loadContentPage(
`moz-extension://${extension.uuid}/extpage.html`
);
const { AddonManager } = ChromeUtils.importESModule(
"resource://gre/modules/AddonManager.sys.mjs"
);
function assertManagementAPIAddonListener(expect) {
let found = false;
for (const addonListener of AddonManager.__AddonManagerInternal__
?.addonListeners || []) {
if (
Object.getPrototypeOf(addonListener).constructor.name ===
"ManagementAddonListener"
) {
found = true;
}
}
equal(
found,
expect,
`${
expect ? "Should" : "Should not"
} have found an AOM addonListener registered by the management API`
);
}
assertManagementAPIAddonListener(true);
await page.close();
assertManagementAPIAddonListener(false);
await extension.unload();
}
);
|