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

"use strict";

async function assertBackgroundScriptTypes(
  extensionTestWrapper,
  expectedScriptTypesMap
) {
  const { baseURI } = extensionTestWrapper.extension;
  let expectedMapWithResolvedURLs = Object.keys(expectedScriptTypesMap).reduce(
    (result, scriptPath) => {
      result[baseURI.resolve(scriptPath)] = expectedScriptTypesMap[scriptPath];
      return result;
    },
    {}
  );
  const page = await ExtensionTestUtils.loadContentPage(
    baseURI.resolve("_generated_background_page.html")
  );
  const scriptTypesMap = await page.spawn([], () => {
    const scripts = Array.from(
      this.content.document.querySelectorAll("script")
    );
    return scripts.reduce((result, script) => {
      result[script.getAttribute("src")] = script.getAttribute("type");
      return result;
    }, {});
  });
  await page.close();
  Assert.deepEqual(
    scriptTypesMap,
    expectedMapWithResolvedURLs,
    "Got the expected script type from the generated background page"
  );
}

async function testBackgroundScriptClassic({ manifestTypeClassicSet }) {
  const extension = ExtensionTestUtils.loadExtension({
    manifest: {
      background: {
        scripts: ["anotherScript.js", "main.js"],
        type: manifestTypeClassicSet ? "classic" : undefined,
      },
    },
    files: {
      "main.js": ``,
      "anotherScript.js": ``,
    },
  });

  await extension.startup();
  await assertBackgroundScriptTypes(extension, {
    "main.js": "text/javascript",
    "anotherScript.js": "text/javascript",
  });
  await extension.unload();
}

add_task(async function test_background_scripts_type_default() {
  await testBackgroundScriptClassic({ manifestTypeClassicSet: false });
});

add_task(async function test_background_scripts_type_classic() {
  await testBackgroundScriptClassic({ manifestTypeClassicSet: true });
});

add_task(async function test_background_scripts_type_module() {
  const extension = ExtensionTestUtils.loadExtension({
    manifest: {
      background: {
        scripts: ["anotherModule.js", "mainModule.js"],
        type: "module",
      },
    },
    files: {
      "mainModule.js": `
        import { initBackground } from "/importedModule.js";
        browser.test.log("mainModule.js - ESM module executing");
        initBackground();
      `,
      "importedModule.js": `
        export function initBackground() {
          browser.test.onMessage.addListener((msg) => {
            browser.test.log("importedModule.js - test message received");
            browser.test.sendMessage("esm-module-reply", msg);
          });
          browser.test.log("importedModule.js - initBackground executed");
        }
        browser.test.log("importedModule.js - ESM module loaded");
      `,
      "anotherModule.js": `
        browser.test.log("anotherModule.js - ESM module loaded");
      `,
    },
  });

  await extension.startup();
  await extension.sendMessage("test-event-value");
  equal(
    await extension.awaitMessage("esm-module-reply"),
    "test-event-value",
    "Got the expected event from the ESM module loaded from the background script"
  );
  await assertBackgroundScriptTypes(extension, {
    "mainModule.js": "module",
    "anotherModule.js": "module",
  });
  await extension.unload();
});

add_task(async function test_background_scripts_type_invalid() {
  const extension = ExtensionTestUtils.loadExtension({
    manifest: {
      background: {
        scripts: ["anotherScript.js", "main.js"],
        type: "invalid",
      },
    },
    files: {
      "main.js": ``,
      "anotherScript.js": ``,
    },
  });

  ExtensionTestUtils.failOnSchemaWarnings(false);
  await Assert.rejects(
    extension.startup(),
    /Error processing background: .* \.type must be one of/,
    "Expected install to fail"
  );
  ExtensionTestUtils.failOnSchemaWarnings(true);
});