summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_scripting_contentScripts_file.js
blob: 3c806439ce631d2a289a582a5c6b7c9b3d1a476a (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
"use strict";

const FILE_DUMMY_URL = Services.io.newFileURI(
  do_get_file("data/dummy_page.html")
).spec;

// ExtensionContent.jsm needs to know when it's running from xpcshell, to use
// the right timeout for content scripts executed at document_idle.
ExtensionTestUtils.mockAppInfo();

Services.prefs.setBoolPref("extensions.manifestV3.enabled", true);

const makeExtension = ({ manifest: manifestProps, ...otherProps }) => {
  return ExtensionTestUtils.loadExtension({
    manifest: {
      manifest_version: 3,
      permissions: ["scripting"],
      host_permissions: ["<all_urls>"],
      granted_host_permissions: true,
      ...manifestProps,
    },
    temporarilyInstalled: true,
    ...otherProps,
  });
};

add_task(async function test_registered_content_script_with_files() {
  let extension = makeExtension({
    async background() {
      const MATCHES = [
        { id: "script-1", matches: ["<all_urls>"] },
        { id: "script-2", matches: ["file:///*"] },
        { id: "script-3", matches: ["file://*/*dummy_page.html"] },
        { id: "fail-if-executed", matches: ["*://*/*"] },
      ];

      await browser.scripting.registerContentScripts(
        MATCHES.map(({ id, matches }) => ({
          id,
          js: [`${id}.js`],
          matches,
          persistAcrossSessions: false,
        }))
      );

      browser.test.sendMessage("background-ready");
    },
    files: {
      "script-1.js": () => {
        browser.test.sendMessage("script-1-ran");
      },
      "script-2.js": () => {
        browser.test.sendMessage("script-2-ran");
      },
      "script-3.js": () => {
        browser.test.sendMessage("script-3-ran");
      },
      "fail-if-executed.js": () => {
        browser.test.fail("this script should not be executed");
      },
    },
  });

  await extension.startup();
  await extension.awaitMessage("background-ready");

  let contentPage = await ExtensionTestUtils.loadContentPage(FILE_DUMMY_URL);

  await Promise.all([
    extension.awaitMessage("script-1-ran"),
    extension.awaitMessage("script-2-ran"),
    extension.awaitMessage("script-3-ran"),
  ]);

  await contentPage.close();
  await extension.unload();
});