summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_background_script_and_service_worker.js
blob: fc59b1810d1cc58583a1076b83821527ccd842b5 (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
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

async function testExtensionWithBackground({
  with_scripts = false,
  with_service_worker = false,
  with_page = false,
  expected_background_type,
  expected_manifest_warnings = [],
}) {
  let background = {};
  if (with_scripts) {
    background.scripts = ["scripts.js"];
  }
  if (with_service_worker) {
    background.service_worker = "sw.js";
  }
  if (with_page) {
    background.page = "page.html";
  }
  let extension = ExtensionTestUtils.loadExtension({
    manifest: { background },
    files: {
      "scripts.js": () => {
        browser.test.sendMessage("from_bg", "scripts");
      },
      "sw.js": () => {
        browser.test.sendMessage("from_bg", "service_worker");
      },
      "page.html": `<!DOCTYPE html><script src="page.js"></script>`,
      "page.js": () => {
        browser.test.sendMessage("from_bg", "page");
      },
    },
  });
  ExtensionTestUtils.failOnSchemaWarnings(false);
  await extension.startup();
  ExtensionTestUtils.failOnSchemaWarnings(true);
  Assert.deepEqual(
    extension.extension.warnings,
    expected_manifest_warnings,
    "Expected manifest warnings"
  );
  info("Waiting for background to start");
  Assert.equal(
    await extension.awaitMessage("from_bg"),
    expected_background_type,
    "Expected background type"
  );
  await extension.unload();
}

add_task(async function test_page_and_scripts() {
  await testExtensionWithBackground({
    with_page: true,
    with_scripts: true,
    // Should be expected_background_type: "scripts", not "page".
    // https://github.com/w3c/webextensions/issues/282#issuecomment-1443332913
    // ... but changing that may potentially affect backcompat of existing
    // Firefox add-ons.
    expected_background_type: "page",
    expected_manifest_warnings: [
      "Reading manifest: Warning processing background.scripts: An unexpected property was found in the WebExtension manifest.",
    ],
  });
});

add_task(
  { skip_if: () => WebExtensionPolicy.backgroundServiceWorkerEnabled },
  async function test_scripts_and_service_worker_when_sw_disabled() {
    await testExtensionWithBackground({
      with_scripts: true,
      with_service_worker: true,
      expected_background_type: "scripts",
      expected_manifest_warnings: [
        "Reading manifest: Warning processing background.service_worker: An unexpected property was found in the WebExtension manifest.",
      ],
    });
  }
);