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
|
const { AddonTestUtils } = ChromeUtils.importESModule(
"resource://testing-common/AddonTestUtils.sys.mjs"
);
const { ExtensionTestUtils } = ChromeUtils.importESModule(
"resource://testing-common/ExtensionXPCShellUtils.sys.mjs"
);
const { BranchedAddonStudyAction } = ChromeUtils.importESModule(
"resource://normandy/actions/BranchedAddonStudyAction.sys.mjs"
);
const { BaseAction } = ChromeUtils.importESModule(
"resource://normandy/actions/BaseAction.sys.mjs"
);
const { TelemetryEvents } = ChromeUtils.importESModule(
"resource://normandy/lib/TelemetryEvents.sys.mjs"
);
const { AddonManager } = ChromeUtils.importESModule(
"resource://gre/modules/AddonManager.sys.mjs"
);
const { AddonStudies } = ChromeUtils.importESModule(
"resource://normandy/lib/AddonStudies.sys.mjs"
);
/* import-globals-from utils.js */
load("utils.js");
NormandyTestUtils.init({ add_task });
const { decorate_task } = NormandyTestUtils;
const global = this;
add_task(async () => {
ExtensionTestUtils.init(global);
AddonTestUtils.init(global);
AddonTestUtils.createAppInfo(
"xpcshell@tests.mozilla.org",
"XPCShell",
"1",
"1.9.2"
);
AddonTestUtils.overrideCertDB();
await AddonTestUtils.promiseStartupManager();
TelemetryEvents.init();
});
decorate_task(
withMockApiServer(),
AddonStudies.withStudies([]),
async function test_addon_unenroll({ server: apiServer }) {
const ID = "study@tests.mozilla.org";
// Create a test extension that uses webextension experiments to install
// an unenroll listener.
let xpi = AddonTestUtils.createTempWebExtensionFile({
manifest: {
version: "1.0",
browser_specific_settings: {
gecko: { id: ID },
},
experiment_apis: {
study: {
schema: "schema.json",
parent: {
scopes: ["addon_parent"],
script: "api.js",
paths: [["study"]],
},
},
},
},
files: {
"schema.json": JSON.stringify([
{
namespace: "study",
events: [
{
name: "onStudyEnded",
type: "function",
},
],
},
]),
"api.js": () => {
// The code below is serialized into a file embedded in an extension.
// But by including it here as code, eslint can analyze it. However,
// this code runs in a different environment with different globals,
// the following two lines avoid false eslint warnings:
/* globals browser, ExtensionAPI */
/* eslint-disable-next-line no-shadow */
const { AddonStudies } = ChromeUtils.importESModule(
"resource://normandy/lib/AddonStudies.sys.mjs"
);
const { ExtensionCommon } = ChromeUtils.importESModule(
"resource://gre/modules/ExtensionCommon.sys.mjs"
);
this.study = class extends ExtensionAPI {
getAPI(context) {
return {
study: {
onStudyEnded: new ExtensionCommon.EventManager({
context,
name: "study.onStudyEnded",
register: fire => {
AddonStudies.addUnenrollListener(
this.extension.id,
reason => fire.sync(reason)
);
return () => {};
},
}).api(),
},
};
}
};
},
},
background() {
browser.study.onStudyEnded.addListener(reason => {
browser.test.sendMessage("got-event", reason);
return new Promise(resolve => {
browser.test.onMessage.addListener(resolve);
});
});
},
});
const server = AddonTestUtils.createHttpServer({ hosts: ["example.com"] });
server.registerFile("/study.xpi", xpi);
const API_ID = 999;
apiServer.registerPathHandler(
`/api/v1/extension/${API_ID}/`,
(request, response) => {
response.setStatusLine(request.httpVersion, 200, "OK");
response.write(
JSON.stringify({
id: API_ID,
name: "Addon Unenroll Fixture",
xpi: "http://example.com/study.xpi",
extension_id: ID,
version: "1.0",
hash: CryptoUtils.getFileHash(xpi, "sha256"),
hash_algorithm: "sha256",
})
);
}
);
// Begin by telling Normandy to install the test extension above
// that uses a webextension experiment to register a blocking callback
// to be invoked when the study ends.
let extension = ExtensionTestUtils.expectExtension(ID);
const RECIPE_ID = 1;
const UNENROLL_REASON = "test-ending";
let action = new BranchedAddonStudyAction();
await action.processRecipe(
{
id: RECIPE_ID,
type: "addon-study",
arguments: {
slug: "addon-unenroll-test",
userFacingDescription: "A recipe to test add-on unenrollment",
userFacingName: "Add-on Unenroll Test",
isEnrollmentPaused: false,
branches: [
{
ratio: 1,
slug: "only",
extensionApiId: API_ID,
},
],
},
},
BaseAction.suitability.FILTER_MATCH
);
await extension.awaitStartup();
let addon = await AddonManager.getAddonByID(ID);
ok(addon, "Extension is installed");
// Tell Normandy to end the study, the extension event should be fired.
let unenrollPromise = action.unenroll(RECIPE_ID, UNENROLL_REASON);
let receivedReason = await extension.awaitMessage("got-event");
info("Got onStudyEnded event in extension");
equal(receivedReason, UNENROLL_REASON, "Unenroll reason should be passed");
// The extension has not yet finished its unenrollment tasks, so it
// should not yet be uninstalled.
addon = await AddonManager.getAddonByID(ID);
ok(addon, "Extension has not yet been uninstalled");
// Once the extension does resolve the promise returned from the
// event listener, the uninstall can proceed.
extension.sendMessage("resolve");
await unenrollPromise;
addon = await AddonManager.getAddonByID(ID);
equal(
addon,
null,
"After resolving studyEnded promise, extension is uninstalled"
);
}
);
/* Test that a broken unenroll listener doesn't stop the add-on from being removed */
decorate_task(
withMockApiServer(),
AddonStudies.withStudies([]),
async function test_addon_unenroll({ server: apiServer }) {
const ID = "study@tests.mozilla.org";
// Create a dummy webextension
// an unenroll listener that throws an error.
let xpi = AddonTestUtils.createTempWebExtensionFile({
manifest: {
version: "1.0",
browser_specific_settings: {
gecko: { id: ID },
},
},
});
const server = AddonTestUtils.createHttpServer({ hosts: ["example.com"] });
server.registerFile("/study.xpi", xpi);
const API_ID = 999;
apiServer.registerPathHandler(
`/api/v1/extension/${API_ID}/`,
(request, response) => {
response.setStatusLine(request.httpVersion, 200, "OK");
response.write(
JSON.stringify({
id: API_ID,
name: "Addon Fixture",
xpi: "http://example.com/study.xpi",
extension_id: ID,
version: "1.0",
hash: CryptoUtils.getFileHash(xpi, "sha256"),
hash_algorithm: "sha256",
})
);
}
);
// Begin by telling Normandy to install the test extension above that uses a
// webextension experiment to register a callback when the study ends that
// throws an error.
let extension = ExtensionTestUtils.expectExtension(ID);
const RECIPE_ID = 1;
const UNENROLL_REASON = "test-ending";
let action = new BranchedAddonStudyAction();
await action.processRecipe(
{
id: RECIPE_ID,
type: "addon-study",
arguments: {
slug: "addon-unenroll-test",
userFacingDescription: "A recipe to test add-on unenrollment",
userFacingName: "Add-on Unenroll Test",
isEnrollmentPaused: false,
branches: [
{
ratio: 1,
slug: "only",
extensionApiId: API_ID,
},
],
},
},
BaseAction.suitability.FILTER_MATCH
);
await extension.startupPromise;
let addon = await AddonManager.getAddonByID(ID);
ok(addon, "Extension is installed");
let listenerDeferred = Promise.withResolvers();
AddonStudies.addUnenrollListener(ID, () => {
listenerDeferred.resolve();
throw new Error("This listener is busted");
});
// Tell Normandy to end the study, the extension event should be fired.
await action.unenroll(RECIPE_ID, UNENROLL_REASON);
await listenerDeferred;
addon = await AddonManager.getAddonByID(ID);
equal(
addon,
null,
"Extension is uninstalled even though it threw an exception in the callback"
);
}
);
|