summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/xpcshell/test_webextension.js
blob: cd4b3761171d094055733b9c46ad560580ab800c (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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

const ID = "webextension1@tests.mozilla.org";

const profileDir = gProfD.clone();
profileDir.append("extensions");

const ADDONS = {
  webextension_1: {
    "manifest.json": {
      name: "Web Extension Name",
      version: "1.0",
      manifest_version: 2,
      browser_specific_settings: {
        gecko: {
          id: "webextension1@tests.mozilla.org",
        },
      },
      icons: {
        48: "icon48.png",
        64: "icon64.png",
      },
    },
    "chrome.manifest": "content webex ./\n",
  },
  webextension_3: {
    "manifest.json": {
      name: "Web Extensiøn __MSG_name__",
      description: "Descriptïon __MSG_desc__ of add-on",
      version: "1.0",
      manifest_version: 2,
      default_locale: "en",
      browser_specific_settings: {
        gecko: {
          id: "webextension3@tests.mozilla.org",
        },
      },
    },
    "_locales/en/messages.json": {
      name: {
        message: "foo ☹",
        description: "foo",
      },
      desc: {
        message: "bar ☹",
        description: "bar",
      },
    },
    "_locales/fr/messages.json": {
      name: {
        message: "le foo ☺",
        description: "foo",
      },
      desc: {
        message: "le bar ☺",
        description: "bar",
      },
    },
  },
};

let chromeReg = Cc["@mozilla.org/chrome/chrome-registry;1"].getService(
  Ci.nsIChromeRegistry
);

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

const {
  ExtensionParent: { GlobalManager },
} = ChromeUtils.importESModule(
  "resource://gre/modules/ExtensionParent.sys.mjs"
);

add_task(async function test_1() {
  await promiseStartupManager();

  equal(GlobalManager.extensionMap.size, 0);

  let { addon } = await AddonTestUtils.promiseInstallXPI(ADDONS.webextension_1);

  equal(GlobalManager.extensionMap.size, 1);
  ok(GlobalManager.extensionMap.has(ID));

  Assert.throws(
    () =>
      chromeReg.convertChromeURL(
        Services.io.newURI("chrome://webex/content/webex.xul")
      ),
    error => error.result == Cr.NS_ERROR_FILE_NOT_FOUND,
    "Chrome manifest should not have been registered"
  );

  let uri = do_get_addon_root_uri(profileDir, ID);

  checkAddon(ID, addon, {
    version: "1.0",
    name: "Web Extension Name",
    isCompatible: true,
    appDisabled: false,
    isActive: true,
    isSystem: false,
    type: "extension",
    isWebExtension: true,
    signedState: AddonManager.SIGNEDSTATE_PRIVILEGED,
    iconURL: `${uri}icon48.png`,
  });

  // Should persist through a restart
  await promiseShutdownManager();

  equal(GlobalManager.extensionMap.size, 0);

  await promiseStartupManager();

  equal(GlobalManager.extensionMap.size, 1);
  ok(GlobalManager.extensionMap.has(ID));

  addon = await promiseAddonByID(ID);

  uri = do_get_addon_root_uri(profileDir, ID);

  checkAddon(ID, addon, {
    version: "1.0",
    name: "Web Extension Name",
    isCompatible: true,
    appDisabled: false,
    isActive: true,
    isSystem: false,
    type: "extension",
    signedState: AddonManager.SIGNEDSTATE_PRIVILEGED,
    iconURL: `${uri}icon48.png`,
  });

  await addon.disable();

  equal(GlobalManager.extensionMap.size, 0);

  await addon.enable();

  equal(GlobalManager.extensionMap.size, 1);
  ok(GlobalManager.extensionMap.has(ID));

  await addon.uninstall();

  equal(GlobalManager.extensionMap.size, 0);
  Assert.ok(!GlobalManager.extensionMap.has(ID));

  await promiseShutdownManager();
});

// Writing the manifest direct to the profile should work
add_task(async function test_2() {
  await promiseWriteWebManifestForExtension(
    {
      name: "Web Extension Name",
      version: "1.0",
      manifest_version: 2,
      browser_specific_settings: {
        gecko: {
          id: ID,
        },
      },
    },
    profileDir
  );

  await promiseStartupManager();

  let addon = await promiseAddonByID(ID);
  checkAddon(ID, addon, {
    version: "1.0",
    name: "Web Extension Name",
    isCompatible: true,
    appDisabled: false,
    isActive: true,
    isSystem: false,
    type: "extension",
    signedState: AddonManager.SIGNEDSTATE_PRIVILEGED,
  });

  await addon.uninstall();

  await promiseRestartManager();
});

add_task(async function test_manifest_localization() {
  const extensionId = "webextension3@tests.mozilla.org";

  let { addon } = await AddonTestUtils.promiseInstallXPI(ADDONS.webextension_3);

  await addon.disable();

  checkAddon(ID, addon, {
    name: "Web Extensiøn foo ☹",
    description: "Descriptïon bar ☹ of add-on",
  });

  await restartWithLocales(["fr-FR"]);

  addon = await promiseAddonByID(extensionId);
  checkAddon(ID, addon, {
    name: "Web Extensiøn le foo ☺",
    description: "Descriptïon le bar ☺ of add-on",
  });

  await restartWithLocales(["de"]);

  addon = await promiseAddonByID(extensionId);
  checkAddon(ID, addon, {
    name: "Web Extensiøn foo ☹",
    description: "Descriptïon bar ☹ of add-on",
  });

  await addon.uninstall();
});

// Missing version should cause a failure
add_task(async function test_3() {
  await promiseWriteWebManifestForExtension(
    {
      name: "Web Extension Name",
      manifest_version: 2,
      browser_specific_settings: {
        gecko: {
          id: ID,
        },
      },
    },
    profileDir
  );

  await promiseRestartManager();

  let addon = await promiseAddonByID(ID);
  Assert.equal(addon, null);

  let file = getFileForAddon(profileDir, ID);
  Assert.ok(!file.exists());

  await promiseRestartManager();
});

// Incorrect manifest version should cause a failure
add_task(async function test_4() {
  await promiseWriteWebManifestForExtension(
    {
      name: "Web Extension Name",
      version: "1.0",
      manifest_version: 1,
      browser_specific_settings: {
        gecko: {
          id: ID,
        },
      },
    },
    profileDir
  );

  await promiseRestartManager();

  let addon = await promiseAddonByID(ID);
  Assert.equal(addon, null);

  let file = getFileForAddon(profileDir, ID);
  Assert.ok(!file.exists());

  await promiseRestartManager();
});

// Test that the "options_ui" manifest section is processed correctly.
add_task(async function test_options_ui() {
  let OPTIONS_RE =
    /^moz-extension:\/\/[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}\/options\.html$/;

  const extensionId = "webextension@tests.mozilla.org";
  let addon = await promiseInstallWebExtension({
    manifest: {
      browser_specific_settings: { gecko: { id: extensionId } },
      options_ui: {
        page: "options.html",
      },
    },
  });

  checkAddon(extensionId, addon, {
    optionsType: AddonManager.OPTIONS_TYPE_INLINE_BROWSER,
  });

  ok(
    OPTIONS_RE.test(addon.optionsURL),
    "Addon should have a moz-extension: options URL for /options.html"
  );

  await addon.uninstall();

  const ID2 = "webextension2@tests.mozilla.org";
  addon = await promiseInstallWebExtension({
    manifest: {
      browser_specific_settings: { gecko: { id: ID2 } },
      options_ui: {
        page: "options.html",
        open_in_tab: true,
      },
    },
  });

  checkAddon(ID2, addon, {
    optionsType: AddonManager.OPTIONS_TYPE_TAB,
  });

  ok(
    OPTIONS_RE.test(addon.optionsURL),
    "Addon should have a moz-extension: options URL for /options.html"
  );

  await addon.uninstall();
});

// Test that experiments permissions add the appropriate dependencies.
add_task(async function test_experiments_dependencies() {
  let addon = await promiseInstallWebExtension({
    manifest: {
      browser_specific_settings: { gecko: { id: "meh@experiment" } },
      permissions: ["experiments.meh"],
    },
  });

  checkAddon(addon.id, addon, {
    dependencies: ["meh@experiments.addons.mozilla.org"],
    // Add-on should be app disabled due to missing dependencies
    appDisabled: true,
  });

  await addon.uninstall();
});

add_task(async function developerShouldOverride() {
  let addon = await promiseInstallWebExtension({
    manifest: {
      default_locale: "en",
      developer: {
        name: "__MSG_name__",
        url: "__MSG_url__",
      },
      author: "Will be overridden by developer",
      homepage_url: "https://will.be.overridden",
    },
    files: {
      "_locales/en/messages.json": `{
        "name": {
          "message": "en name"
        },
        "url": {
          "message": "https://example.net/en"
        }
      }`,
    },
  });

  checkAddon(ID, addon, {
    creator: "en name",
    homepageURL: "https://example.net/en",
  });

  await addon.uninstall();
});

add_task(async function test_invalid_developer_does_not_override() {
  for (const { type, manifestProps, files } of [
    {
      type: "dictionary",
      manifestProps: {
        dictionaries: {
          "en-US": "en-US.dic",
        },
      },
      files: {
        "en-US.dic": "",
        "en-US.aff": "",
      },
    },
    {
      type: "theme",
      manifestProps: {
        theme: {
          colors: {
            frame: "#FFF",
            tab_background_text: "#000",
          },
        },
      },
    },
    {
      type: "locale",
      manifestProps: {
        langpack_id: "und",
        languages: {
          und: {
            chrome_resources: {
              global: "chrome/und/locale/und/global",
            },
            version: "20190326174300",
          },
        },
      },
    },
  ]) {
    const id = `${type}@mozilla.com`;
    const creator = "Some author";
    const homepageURL = "https://example.net";

    info(`== loading add-on with id=${id} ==`);

    for (let developer of [{}, null, { name: null, url: null }]) {
      let addon = await promiseInstallWebExtension({
        manifest: {
          author: creator,
          homepage_url: homepageURL,
          developer,
          browser_specific_settings: { gecko: { id } },
          ...manifestProps,
        },
        files,
      });

      checkAddon(id, addon, { type, creator, homepageURL });

      await addon.uninstall();
    }
  }
});

add_task(async function authorNotString() {
  ExtensionTestUtils.failOnSchemaWarnings(false);
  for (let author of [{}, [], 42]) {
    let addon = await promiseInstallWebExtension({
      manifest: {
        author,
        manifest_version: 2,
        name: "Web Extension Name",
        version: "1.0",
      },
    });

    checkAddon(ID, addon, {
      creator: null,
    });

    await addon.uninstall();
  }
  ExtensionTestUtils.failOnSchemaWarnings(true);
});

add_task(async function testThemeExtension() {
  let addon = await promiseInstallWebExtension({
    manifest: {
      author: "Some author",
      manifest_version: 2,
      name: "Web Extension Name",
      version: "1.0",
      theme: { images: { theme_frame: "example.png" } },
    },
  });

  checkAddon(ID, addon, {
    creator: "Some author",
    version: "1.0",
    name: "Web Extension Name",
    isCompatible: true,
    appDisabled: false,
    isActive: false,
    userDisabled: true,
    isSystem: false,
    type: "theme",
    isWebExtension: true,
    signedState: AddonManager.SIGNEDSTATE_PRIVILEGED,
  });

  await addon.uninstall();

  // Also test one without a proper 'theme' section.
  ExtensionTestUtils.failOnSchemaWarnings(false);
  addon = await promiseInstallWebExtension({
    manifest: {
      author: "Some author",
      manifest_version: 2,
      name: "Web Extension Name",
      version: "1.0",
      theme: null,
    },
  });
  ExtensionTestUtils.failOnSchemaWarnings(true);

  checkAddon(ID, addon, {
    type: "extension",
    isWebExtension: true,
  });

  await addon.uninstall();
});

// Test that we can update from a webextension to a webextension-theme
add_task(async function test_theme_upgrade() {
  // First install a regular webextension
  let addon = await promiseInstallWebExtension({
    manifest: {
      version: "1.0",
      name: "Test WebExtension 1 (temporary)",
      browser_specific_settings: {
        gecko: {
          id: ID,
        },
      },
    },
  });

  checkAddon(ID, addon, {
    version: "1.0",
    name: "Test WebExtension 1 (temporary)",
    isCompatible: true,
    appDisabled: false,
    isActive: true,
    type: "extension",
    signedState: AddonManager.SIGNEDSTATE_PRIVILEGED,
  });

  // Create a webextension theme with the same ID
  addon = await promiseInstallWebExtension({
    manifest: {
      version: "2.0",
      name: "Test WebExtension 1 (temporary)",
      browser_specific_settings: {
        gecko: {
          id: ID,
        },
      },
      theme: { images: { theme_frame: "example.png" } },
    },
  });

  checkAddon(ID, addon, {
    version: "2.0",
    name: "Test WebExtension 1 (temporary)",
    isCompatible: true,
    appDisabled: false,
    isActive: true,
    // This is what we're really interested in:
    type: "theme",
    isWebExtension: true,
  });

  await addon.uninstall();

  addon = await promiseAddonByID(ID);
  Assert.equal(addon, null);
});

add_task(async function test_developer_properties() {
  const name = "developer-name";
  const url = "https://example.org";

  for (const { type, manifestProps, files } of [
    {
      type: "dictionary",
      manifestProps: {
        dictionaries: {
          "en-US": "en-US.dic",
        },
      },
      files: {
        "en-US.dic": "",
        "en-US.aff": "",
      },
    },
    {
      type: "statictheme",
      manifestProps: {
        theme: {
          colors: {
            frame: "#FFF",
            tab_background_text: "#000",
          },
        },
      },
    },
    {
      type: "langpack",
      manifestProps: {
        langpack_id: "und",
        languages: {
          und: {
            chrome_resources: {
              global: "chrome/und/locale/und/global",
            },
            version: "20190326174300",
          },
        },
      },
    },
  ]) {
    const id = `${type}@mozilla.com`;

    info(`== loading add-on with id=${id} ==`);

    let addon = await promiseInstallWebExtension({
      manifest: {
        developer: {
          name,
          url,
        },
        author: "Will be overridden by developer",
        homepage_url: "https://will.be.overridden",
        browser_specific_settings: { gecko: { id } },
        ...manifestProps,
      },
      files,
    });

    checkAddon(id, addon, { creator: name, homepageURL: url });

    await addon.uninstall();
  }
});

add_task(async function test_invalid_homepage_and_developer_urls() {
  const INVALID_URLS = [
    "chrome://browser/content/",
    "data:text/json,...",
    "javascript:;",
    "/",
    "not-an-url",
  ];
  const EXPECTED_ERROR_RE =
    /Access denied for URL|may not load or link to|is not a valid URL/;

  for (let url of INVALID_URLS) {
    // First, we verify `homepage_url`, which has a `url` "format" defined
    // since it exists.
    let normalized = await ExtensionTestUtils.normalizeManifest({
      homepage_url: url,
    });
    ok(
      EXPECTED_ERROR_RE.test(normalized.error),
      `got expected error for ${url}`
    );

    // The `developer.url` now has a "format" but it was a late addition so we
    // are only raising a warning instead of an error.
    ExtensionTestUtils.failOnSchemaWarnings(false);
    normalized = await ExtensionTestUtils.normalizeManifest({
      developer: { url },
    });
    ok(!normalized.error, "expected no error");
    ok(
      // Despites this prop being named `errors`, we are checking the warnings
      // here.
      EXPECTED_ERROR_RE.test(normalized.errors[0]),
      `got expected warning for ${url}`
    );
    ExtensionTestUtils.failOnSchemaWarnings(true);
  }
});

add_task(async function test_valid_homepage_and_developer_urls() {
  let normalized = await ExtensionTestUtils.normalizeManifest({
    developer: { url: "https://example.com" },
  });
  ok(!normalized.error, "expected no error");

  normalized = await ExtensionTestUtils.normalizeManifest({
    homepage_url: "https://example.com",
  });
  ok(!normalized.error, "expected no error");
});