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

const server = createHttpServer();
server.registerDirectory("/data/", do_get_file("data"));

const BASE_URL = `http://localhost:${server.identity.primaryPort}/data`;

// 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: ["http://localhost/*"],
      granted_host_permissions: true,
      ...manifestProps,
    },
    temporarilyInstalled: true,
    ...otherProps,
  });
};

add_task(async function test_scripting_updateContentScripts() {
  let extension = makeExtension({
    async background() {
      const script = {
        id: "a-script",
        js: ["script-1.js"],
        matches: ["http://*/*/*.html"],
        persistAcrossSessions: false,
      };

      await browser.scripting.registerContentScripts([script]);
      await browser.scripting.updateContentScripts([
        {
          id: script.id,
          js: ["script-2.js"],
        },
      ]);
      let scripts = await browser.scripting.getRegisteredContentScripts();
      browser.test.assertEq(1, scripts.length, "expected 1 registered script");

      browser.test.sendMessage("background-ready");
    },
    files: {
      "script-1.js": () => {
        browser.test.fail("script-1 should not be executed");
      },
      "script-2.js": () => {
        browser.test.sendMessage(
          `script-2 executed in ${location.pathname.split("/").pop()}`
        );
      },
    },
  });

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

  let contentPage = await ExtensionTestUtils.loadContentPage(
    `${BASE_URL}/file_sample.html`
  );

  await extension.awaitMessage("script-2 executed in file_sample.html");

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

add_task(
  async function test_scripting_updateContentScripts_non_default_values() {
    let extension = makeExtension({
      async background() {
        const script = {
          id: "a-script",
          allFrames: true,
          matches: ["http://*/*/*.html"],
          runAt: "document_start",
          persistAcrossSessions: false,
          css: ["style.js"],
          excludeMatches: ["http://*/*/foobar.html"],
          js: ["script.js"],
        };

        await browser.scripting.registerContentScripts([script]);

        // This should not modify the previously registered script.
        await browser.scripting.updateContentScripts([{ id: script.id }]);

        let scripts = await browser.scripting.getRegisteredContentScripts();
        browser.test.assertEq(
          JSON.stringify([script]),
          JSON.stringify(scripts),
          "expected unmodified registered script"
        );

        browser.test.sendMessage("background-done");
      },
      files: {
        "script.js": "",
        "style.css": "",
      },
    });

    await extension.startup();
    await extension.awaitMessage("background-done");
    await extension.unload();
  }
);