summaryrefslogtreecommitdiffstats
path: root/toolkit/components/normandy/test/browser/browser_Normandy.js
blob: 1480bd13a4c3e3df6a49722baac71f935db8d818 (plain)
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
"use strict";

const { TelemetryUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/TelemetryUtils.sys.mjs"
);
const { Normandy } = ChromeUtils.importESModule(
  "resource://normandy/Normandy.sys.mjs"
);
const { AddonRollouts } = ChromeUtils.importESModule(
  "resource://normandy/lib/AddonRollouts.sys.mjs"
);
const { PreferenceExperiments } = ChromeUtils.importESModule(
  "resource://normandy/lib/PreferenceExperiments.sys.mjs"
);
const { PreferenceRollouts } = ChromeUtils.importESModule(
  "resource://normandy/lib/PreferenceRollouts.sys.mjs"
);
const { RecipeRunner } = ChromeUtils.importESModule(
  "resource://normandy/lib/RecipeRunner.sys.mjs"
);
const {
  NormandyTestUtils: { factories },
} = ChromeUtils.importESModule(
  "resource://testing-common/NormandyTestUtils.sys.mjs"
);

const experimentPref1 = "test.initExperimentPrefs1";
const experimentPref2 = "test.initExperimentPrefs2";
const experimentPref3 = "test.initExperimentPrefs3";
const experimentPref4 = "test.initExperimentPrefs4";

function withStubInits() {
  return function (testFunction) {
    return decorate(
      withStub(AddonRollouts, "init"),
      withStub(AddonStudies, "init"),
      withStub(PreferenceRollouts, "init"),
      withStub(PreferenceExperiments, "init"),
      withStub(RecipeRunner, "init"),
      withStub(TelemetryEvents, "init"),
      testFunction
    );
  };
}

decorate_task(
  withPrefEnv({
    set: [
      [`app.normandy.startupExperimentPrefs.${experimentPref1}`, true],
      [`app.normandy.startupExperimentPrefs.${experimentPref2}`, 2],
      [`app.normandy.startupExperimentPrefs.${experimentPref3}`, "string"],
    ],
  }),
  async function testApplyStartupPrefs() {
    const defaultBranch = Services.prefs.getDefaultBranch("");
    for (const pref of [experimentPref1, experimentPref2, experimentPref3]) {
      is(
        defaultBranch.getPrefType(pref),
        defaultBranch.PREF_INVALID,
        `Pref ${pref} don't exist before being initialized.`
      );
    }

    let oldValues = Normandy.applyStartupPrefs(
      "app.normandy.startupExperimentPrefs."
    );

    Assert.deepEqual(
      oldValues,
      {
        [experimentPref1]: null,
        [experimentPref2]: null,
        [experimentPref3]: null,
      },
      "the correct set of old values should be reported"
    );

    ok(
      defaultBranch.getBoolPref(experimentPref1),
      `Pref ${experimentPref1} has a default value after being initialized.`
    );
    is(
      defaultBranch.getIntPref(experimentPref2),
      2,
      `Pref ${experimentPref2} has a default value after being initialized.`
    );
    is(
      defaultBranch.getCharPref(experimentPref3),
      "string",
      `Pref ${experimentPref3} has a default value after being initialized.`
    );

    for (const pref of [experimentPref1, experimentPref2, experimentPref3]) {
      ok(
        !defaultBranch.prefHasUserValue(pref),
        `Pref ${pref} doesn't have a user value after being initialized.`
      );
      Services.prefs.clearUserPref(pref);
      defaultBranch.deleteBranch(pref);
    }
  }
);

decorate_task(
  withPrefEnv({
    set: [
      ["app.normandy.startupExperimentPrefs.test.existingPref", "experiment"],
    ],
  }),
  async function testApplyStartupPrefsExisting() {
    const defaultBranch = Services.prefs.getDefaultBranch("");
    defaultBranch.setCharPref("test.existingPref", "default");
    Normandy.applyStartupPrefs("app.normandy.startupExperimentPrefs.");
    is(
      defaultBranch.getCharPref("test.existingPref"),
      "experiment",
      "applyStartupPrefs overwrites the default values of existing preferences."
    );
  }
);

decorate_task(
  withPrefEnv({
    set: [
      ["app.normandy.startupExperimentPrefs.test.mismatchPref", "experiment"],
    ],
  }),
  async function testApplyStartupPrefsMismatch() {
    const defaultBranch = Services.prefs.getDefaultBranch("");
    defaultBranch.setIntPref("test.mismatchPref", 2);
    Normandy.applyStartupPrefs("app.normandy.startupExperimentPrefs.");
    is(
      defaultBranch.getPrefType("test.mismatchPref"),
      Services.prefs.PREF_INT,
      "applyStartupPrefs skips prefs that don't match the existing default value's type."
    );
  }
);

decorate_task(
  withStub(Normandy, "finishInit"),
  async function testStartupDelayed({ finishInitStub }) {
    let originalDeferred = Normandy.uiAvailableNotificationObserved;
    let mockUiAvailableDeferred = PromiseUtils.defer();
    Normandy.uiAvailableNotificationObserved = mockUiAvailableDeferred;

    let initPromise = Normandy.init();
    await null;

    ok(
      !finishInitStub.called,
      "When initialized, do not call finishInit immediately."
    );

    Normandy.observe(null, "sessionstore-windows-restored");
    await initPromise;
    ok(
      finishInitStub.called,
      "Once the sessionstore-windows-restored event is observed, finishInit should be called."
    );

    Normandy.uiAvailableNotificationObserved = originalDeferred;
  }
);

// During startup, preferences that are changed for experiments should
// be record by calling PreferenceExperiments.recordOriginalValues.
decorate_task(
  withStub(PreferenceExperiments, "recordOriginalValues", {
    as: "experimentsRecordOriginalValuesStub",
  }),
  withStub(PreferenceRollouts, "recordOriginalValues", {
    as: "rolloutsRecordOriginalValueStub",
  }),
  async function testApplyStartupPrefs({
    experimentsRecordOriginalValuesStub,
    rolloutsRecordOriginalValueStub,
  }) {
    const defaultBranch = Services.prefs.getDefaultBranch("");

    defaultBranch.setBoolPref(experimentPref1, false);
    defaultBranch.setIntPref(experimentPref2, 1);
    defaultBranch.setCharPref(experimentPref3, "original string");
    // experimentPref4 is left unset

    Normandy.applyStartupPrefs("app.normandy.startupExperimentPrefs.");
    Normandy.studyPrefsChanged = { "test.study-pref": 1 };
    Normandy.rolloutPrefsChanged = { "test.rollout-pref": 1 };
    await Normandy.finishInit();

    Assert.deepEqual(
      experimentsRecordOriginalValuesStub.args,
      [[{ "test.study-pref": 1 }]],
      "finishInit should record original values of the study prefs"
    );
    Assert.deepEqual(
      rolloutsRecordOriginalValueStub.args,
      [[{ "test.rollout-pref": 1 }]],
      "finishInit should record original values of the study prefs"
    );

    // cleanup
    defaultBranch.deleteBranch(experimentPref1);
    defaultBranch.deleteBranch(experimentPref2);
    defaultBranch.deleteBranch(experimentPref3);
  }
);

// Test that startup prefs are handled correctly when there is a value on the user branch but not the default branch.
decorate_task(
  withPrefEnv({
    set: [
      ["app.normandy.startupExperimentPrefs.testing.does-not-exist", "foo"],
      ["testing.does-not-exist", "foo"],
    ],
  }),
  withStub(PreferenceExperiments, "recordOriginalValues"),
  async function testApplyStartupPrefsNoDefaultValue() {
    Normandy.applyStartupPrefs("app.normandy.startupExperimentPrefs");
    ok(
      true,
      "initExperimentPrefs should not throw for prefs that doesn't exist on the default branch"
    );
  }
);

decorate_task(withStubInits(), async function testStartup() {
  const initObserved = TestUtils.topicObserved("shield-init-complete");
  await Normandy.finishInit();
  ok(AddonStudies.init.called, "startup calls AddonStudies.init");
  ok(
    PreferenceExperiments.init.called,
    "startup calls PreferenceExperiments.init"
  );
  ok(RecipeRunner.init.called, "startup calls RecipeRunner.init");
  await initObserved;
});

decorate_task(withStubInits(), async function testStartupPrefInitFail() {
  PreferenceExperiments.init.rejects();

  await Normandy.finishInit();
  ok(AddonStudies.init.called, "startup calls AddonStudies.init");
  ok(AddonRollouts.init.called, "startup calls AddonRollouts.init");
  ok(
    PreferenceExperiments.init.called,
    "startup calls PreferenceExperiments.init"
  );
  ok(RecipeRunner.init.called, "startup calls RecipeRunner.init");
  ok(TelemetryEvents.init.called, "startup calls TelemetryEvents.init");
  ok(PreferenceRollouts.init.called, "startup calls PreferenceRollouts.init");
});

decorate_task(
  withStubInits(),
  async function testStartupAddonStudiesInitFail() {
    AddonStudies.init.rejects();

    await Normandy.finishInit();
    ok(AddonStudies.init.called, "startup calls AddonStudies.init");
    ok(AddonRollouts.init.called, "startup calls AddonRollouts.init");
    ok(
      PreferenceExperiments.init.called,
      "startup calls PreferenceExperiments.init"
    );
    ok(RecipeRunner.init.called, "startup calls RecipeRunner.init");
    ok(TelemetryEvents.init.called, "startup calls TelemetryEvents.init");
    ok(PreferenceRollouts.init.called, "startup calls PreferenceRollouts.init");
  }
);

decorate_task(
  withStubInits(),
  async function testStartupTelemetryEventsInitFail() {
    TelemetryEvents.init.throws();

    await Normandy.finishInit();
    ok(AddonStudies.init.called, "startup calls AddonStudies.init");
    ok(AddonRollouts.init.called, "startup calls AddonRollouts.init");
    ok(
      PreferenceExperiments.init.called,
      "startup calls PreferenceExperiments.init"
    );
    ok(RecipeRunner.init.called, "startup calls RecipeRunner.init");
    ok(TelemetryEvents.init.called, "startup calls TelemetryEvents.init");
    ok(PreferenceRollouts.init.called, "startup calls PreferenceRollouts.init");
  }
);

decorate_task(
  withStubInits(),
  async function testStartupPreferenceRolloutsInitFail() {
    PreferenceRollouts.init.throws();

    await Normandy.finishInit();
    ok(AddonStudies.init.called, "startup calls AddonStudies.init");
    ok(AddonRollouts.init.called, "startup calls AddonRollouts.init");
    ok(
      PreferenceExperiments.init.called,
      "startup calls PreferenceExperiments.init"
    );
    ok(RecipeRunner.init.called, "startup calls RecipeRunner.init");
    ok(TelemetryEvents.init.called, "startup calls TelemetryEvents.init");
    ok(PreferenceRollouts.init.called, "startup calls PreferenceRollouts.init");
  }
);

// Test that disabling telemetry removes all stored enrollment IDs
decorate_task(
  PreferenceExperiments.withMockExperiments([
    factories.preferenceStudyFactory({
      enrollmentId: "test-enrollment-id",
    }),
  ]),
  AddonStudies.withStudies([
    factories.addonStudyFactory({ slug: "test-study" }),
  ]),
  PreferenceRollouts.withTestMock(),
  AddonRollouts.withTestMock(),
  async function disablingTelemetryClearsEnrollmentIds({
    prefExperiments: [prefExperiment],
    addonStudies: [addonStudy],
  }) {
    const prefRollout = {
      slug: "test-rollout",
      state: PreferenceRollouts.STATE_ACTIVE,
      preferences: [],
      enrollmentId: "test-enrollment-id",
    };
    await PreferenceRollouts.add(prefRollout);
    const addonRollout = {
      slug: "test-rollout",
      state: AddonRollouts.STATE_ACTIVE,
      extension: {},
      enrollmentId: "test-enrollment-id",
    };
    await AddonRollouts.add(addonRollout);

    // pre-check
    ok(
      (await PreferenceExperiments.get(prefExperiment.slug)).enrollmentId,
      "pref experiment should have an enrollment id"
    );
    ok(
      (await AddonStudies.get(addonStudy.recipeId)).enrollmentId,
      "addon study should have an enrollment id"
    );
    ok(
      (await PreferenceRollouts.get(prefRollout.slug)).enrollmentId,
      "pref rollout should have an enrollment id"
    );
    ok(
      (await AddonRollouts.get(addonRollout.slug)).enrollmentId,
      "addon rollout should have an enrollment id"
    );

    // trigger telemetry being disabled
    await Normandy.observe(
      null,
      TelemetryUtils.TELEMETRY_UPLOAD_DISABLED_TOPIC,
      null
    );

    // no enrollment IDs anymore
    is(
      (await PreferenceExperiments.get(prefExperiment.slug)).enrollmentId,
      TelemetryEvents.NO_ENROLLMENT_ID_MARKER,
      "pref experiment should not have an enrollment id"
    );
    is(
      (await AddonStudies.get(addonStudy.recipeId)).enrollmentId,
      TelemetryEvents.NO_ENROLLMENT_ID_MARKER,
      "addon study should not have an enrollment id"
    );
    is(
      (await PreferenceRollouts.get(prefRollout.slug)).enrollmentId,
      TelemetryEvents.NO_ENROLLMENT_ID_MARKER,
      "pref rollout should not have an enrollment id"
    );
    is(
      (await AddonRollouts.get(addonRollout.slug)).enrollmentId,
      TelemetryEvents.NO_ENROLLMENT_ID_MARKER,
      "addon rollout should not have an enrollment id"
    );
  }
);