summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/xpcshell/test_installtrigger_deprecation.js
blob: dfaeaa44f2b567e488ff87a2f65db8e6b9eb2b73 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "42", "42");

const testserver = createHttpServer({ hosts: ["example.com"] });

function createTestPage(body) {
  return `<!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
      </head>
      <body>
        ${body}
      </body>
    </html>
  `;
}

testserver.registerPathHandler(
  "/installtrigger_ua_detection.html",
  (request, response) => {
    response.write(
      createTestPage(`
    <button/>
    <script>
      document.querySelector("button").onclick = () => {
        typeof InstallTrigger;
      };
    </script>
  `)
    );
  }
);

testserver.registerPathHandler(
  "/installtrigger_install.html",
  (request, response) => {
    response.write(
      createTestPage(`
    <button/>
    <script>
      const install = InstallTrigger.install.bind(InstallTrigger);
      document.querySelector("button").onclick = () => {
        install({ fakeextensionurl: "http://example.com/fakeextensionurl.xpi" });
      };
    </script>
  `)
    );
  }
);

async function testDeprecationWarning(testPageURL, expectedDeprecationWarning) {
  const page = await ExtensionTestUtils.loadContentPage(testPageURL);

  const { message, messageInnerWindowID, pageInnerWindowID } = await page.spawn(
    [expectedDeprecationWarning],
    expectedWarning => {
      return new Promise(resolve => {
        const consoleListener = consoleMsg => {
          if (
            consoleMsg instanceof Ci.nsIScriptError &&
            consoleMsg.message?.includes(expectedWarning)
          ) {
            Services.console.unregisterListener(consoleListener);
            resolve({
              message: consoleMsg.message,
              messageInnerWindowID: consoleMsg.innerWindowID,
              pageInnerWindowID: this.content.windowGlobalChild.innerWindowId,
            });
          }
        };

        Services.console.registerListener(consoleListener);
        this.content.document.querySelector("button").click();
      });
    }
  );

  equal(
    typeof messageInnerWindowID,
    "number",
    `Warning message should be associated to an innerWindowID`
  );
  equal(
    messageInnerWindowID,
    pageInnerWindowID,
    `Deprecation warning "${message}" has been logged and associated to the expected window`
  );

  await page.close();

  return message;
}

add_task(
  {
    pref_set: [
      ["extensions.InstallTrigger.enabled", true],
      ["extensions.InstallTriggerImpl.enabled", true],
    ],
  },
  function testDeprecationWarningsOnUADetection() {
    return testDeprecationWarning(
      "http://example.com/installtrigger_ua_detection.html",
      "InstallTrigger is deprecated and will be removed in the future."
    );
  }
);

add_task(
  {
    pref_set: [
      ["extensions.InstallTrigger.enabled", true],
      ["extensions.InstallTriggerImpl.enabled", true],
    ],
  },
  async function testDeprecationWarningsOnInstallTriggerInstall() {
    const message = await testDeprecationWarning(
      "http://example.com/installtrigger_install.html",
      "InstallTrigger.install() is deprecated and will be removed in the future."
    );

    const moreInfoURL =
      "https://extensionworkshop.com/documentation/publish/self-distribution/";

    ok(
      message.includes(moreInfoURL),
      "Deprecation warning should include an url to self-distribution documentation"
    );
  }
);

async function testInstallTriggerDeprecationPrefs(expectedResults) {
  const page = await ExtensionTestUtils.loadContentPage("http://example.com");
  const promiseResults = page.spawn([], () => {
    return {
      uaDetectionResult: this.content.eval(
        "typeof InstallTrigger !== 'undefined'"
      ),
      typeofInstallMethod: this.content.eval("typeof InstallTrigger?.install"),
    };
  });
  if (expectedResults.error) {
    await Assert.rejects(
      promiseResults,
      expectedResults.error,
      "Got the expected error"
    );
  } else {
    Assert.deepEqual(
      await promiseResults,
      expectedResults,
      "Got the expected results"
    );
  }
  await page.close();
}

add_task(
  {
    pref_set: [
      ["extensions.InstallTrigger.enabled", true],
      ["extensions.InstallTriggerImpl.enabled", false],
    ],
  },
  function testInstallTriggerImplDisabled() {
    return testInstallTriggerDeprecationPrefs({
      uaDetectionResult: true,
      typeofInstallMethod: "undefined",
    });
  }
);

add_task(
  {
    pref_set: [["extensions.InstallTrigger.enabled", false]],
  },
  function testInstallTriggerDisabled() {
    return testInstallTriggerDeprecationPrefs({
      error: /ReferenceError: InstallTrigger is not defined/,
    });
  }
);

add_task(
  {
    pref_set: [
      ["extensions.remoteSettings.disabled", false],
      ["extensions.InstallTrigger.enabled", true],
      ["extensions.InstallTriggerImpl.enabled", true],
    ],
  },
  async function testInstallTriggerDeprecatedFromRemoteSettings() {
    await AddonTestUtils.promiseStartupManager();

    // InstallTrigger is expected to be initially enabled.
    await testInstallTriggerDeprecationPrefs({
      uaDetectionResult: true,
      typeofInstallMethod: "function",
    });

    info("Test remote settings update to hide InstallTrigger methods");

    // InstallTrigger global is expected to still be enabled, the install method
    // to have been hidden.
    const unexpectedPrefsBranchName = "extensions.unexpectedPrefs";
    await setAndEmitFakeRemoteSettingsData([
      {
        id: "AddonManagerSettings",
        installTriggerDeprecation: {
          "extensions.InstallTriggerImpl.enabled": false,
          // Unexpected preferences names would be just ignored.
          [`${unexpectedPrefsBranchName}.fromProcessedEntry`]: true,
        },
        otherFakeFutureSetting: {
          [`${unexpectedPrefsBranchName}.fromFakeFutureSetting`]: true,
        },
        // This entry is expected to always be processed when running this
        // xpcshell test, the appInfo platformVersion is always set to 42
        // by the call to AddonTestUtils's createAppInfo.
        filter_expression: "env.appinfo.platformVersion >= 42",
      },
      {
        // Entries entirely unexpected should be ignored even if they may be
        // including a property named as the ones that AMRemoteSettings (e.g.
        // it may be a new type of entry introduced for a new Firefox version,
        // which a previous version of Firefox shouldn't try to process avoid
        // undefined behaviors).
        id: "AddonManagerSettings-fxFutureVersion",
        // This entry is expected to always be filtered out by RemoteSettings,
        // while running this xpcshell test the platformInfo version is always set
        // to 42 by the call to AddonTestUtils's createAppInfo.
        filter_expression: "env.appinfo.platformVersion >= 200",
        installTriggerDeprecation: {
          // If processed, it would fail the assertion that follows
          // because it does change the same pref that the previous entry did
          // set to false.
          "extensions.InstallTriggerImpl.enabled": true,
        },
      },
    ]);
    await testInstallTriggerDeprecationPrefs({
      uaDetectionResult: true,
      typeofInstallMethod: "undefined",
    });

    const unexpectedPrefBranch = Services.prefs.getBranch(
      unexpectedPrefsBranchName
    );
    equal(
      unexpectedPrefBranch.getPrefType("fromFakeFutureSetting"),
      unexpectedPrefBranch.PREF_INVALID,
      "Preferences included in an unexpected entry property should not be set"
    );
    equal(
      unexpectedPrefBranch.getPrefType("fromProcessedEntry"),
      unexpectedPrefBranch.PREF_INVALID,
      undefined,
      "Unexpected pref included in the installTriggerDeprecation entry should not be set"
    );

    info("Test remote settings update to hide InstallTrigger global");
    // InstallTrigger global is expected to still be enabled, the install method
    // to have been hidden.
    await setAndEmitFakeRemoteSettingsData([
      {
        id: "AddonManagerSettings",
        installTriggerDeprecation: {
          "extensions.InstallTrigger.enabled": false,
        },
      },
    ]);
    await testInstallTriggerDeprecationPrefs({
      error: /ReferenceError: InstallTrigger is not defined/,
    });

    info("Test remote settings update to re-enable InstallTrigger global");
    // InstallTrigger global is expected to still be enabled, the install method
    // to have been hidden.
    await setAndEmitFakeRemoteSettingsData([
      {
        id: "AddonManagerSettings",
        installTriggerDeprecation: {
          "extensions.InstallTrigger.enabled": true,
          "extensions.InstallTriggerImpl.enabled": false,
        },
      },
    ]);
    await testInstallTriggerDeprecationPrefs({
      uaDetectionResult: true,
      typeofInstallMethod: "undefined",
    });

    info("Test remote settings update to re-enable InstallTrigger methods");
    // InstallTrigger global and method are both expected to be re-enabled.
    await setAndEmitFakeRemoteSettingsData([
      {
        id: "AddonManagerSettings",
        installTriggerDeprecation: {
          "extensions.InstallTrigger.enabled": true,
          "extensions.InstallTriggerImpl.enabled": true,
        },
      },
    ]);
    await testInstallTriggerDeprecationPrefs({
      uaDetectionResult: true,
      typeofInstallMethod: "function",
    });

    info("Test remote settings ignored when AMRemoteSettings is disabled");
    // RemoteSettings are expected to be ignored.
    Services.prefs.setBoolPref("extensions.remoteSettings.disabled", true);
    await setAndEmitFakeRemoteSettingsData(
      [
        {
          id: "AddonManagerSettings",
          installTriggerDeprecation: {
            "extensions.InstallTrigger.enabled": false,
            "extensions.InstallTriggerImpl.enabled": false,
          },
        },
      ],
      false /* expectClientInitialized */
    );
    await testInstallTriggerDeprecationPrefs({
      uaDetectionResult: true,
      typeofInstallMethod: "function",
    });

    info(
      "Test previously synchronized are processed on AOM started when AMRemoteSettings are enabled"
    );
    // RemoteSettings previously stored on disk are expected to disable InstallTrigger global and methods.
    await AddonTestUtils.promiseShutdownManager();
    Services.prefs.setBoolPref("extensions.remoteSettings.disabled", false);
    await AddonTestUtils.promiseStartupManager();
    await testInstallTriggerDeprecationPrefs({
      error: /ReferenceError: InstallTrigger is not defined/,
    });

    await AddonTestUtils.promiseShutdownManager();
  }
);