summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_scripting_contentScripts_css.js
blob: 21190d2d590edd66c8dc2e4ad0e18bb97dd7ac0a (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
"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,
    },
    allowInsecureRequests: true,
    temporarilyInstalled: true,
    ...otherProps,
  });
};

add_task(async function test_registerContentScripts_css() {
  let extension = makeExtension({
    async background() {
      // This script is injected in all frames after the styles so that we can
      // verify the registered styles.
      const checkAppliedStyleScript = {
        id: "check-applied-styles",
        allFrames: true,
        matches: ["http://*/*/*.html"],
        runAt: "document_idle",
        persistAcrossSessions: false,
        js: ["check-applied-styles.js"],
      };

      // Listen to the `load-test-case` message and unregister/register new
      // content scripts.
      browser.test.onMessage.addListener(async (msg, data) => {
        switch (msg) {
          case "load-test-case":
            const { title, params, skipCheckScriptRegistration } = data;
            const expectedScripts = [];

            await browser.scripting.unregisterContentScripts();

            if (!skipCheckScriptRegistration) {
              await browser.scripting.registerContentScripts([
                checkAppliedStyleScript,
              ]);

              expectedScripts.push(checkAppliedStyleScript);
            }

            expectedScripts.push(...params);

            const res = await browser.scripting.registerContentScripts(params);
            browser.test.assertEq(
              res,
              undefined,
              `${title} - expected no result`
            );
            const scripts =
              await browser.scripting.getRegisteredContentScripts();
            browser.test.assertEq(
              expectedScripts.length,
              scripts.length,
              `${title} - expected ${expectedScripts.length} registered scripts`
            );
            browser.test.assertEq(
              JSON.stringify(expectedScripts),
              JSON.stringify(scripts),
              `${title} - got expected registered scripts`
            );

            browser.test.sendMessage(`${msg}-done`);
            break;
          default:
            browser.test.fail(`received unexpected message: ${msg}`);
        }
      });

      browser.test.sendMessage("background-ready");
    },
    files: {
      "check-applied-styles.js": () => {
        browser.test.sendMessage(
          `background-color-${location.pathname.split("/").pop()}`,
          getComputedStyle(document.querySelector("#test")).backgroundColor
        );
      },
      "style-1.css": "#test { background-color: rgb(255, 0, 0); }",
      "style-2.css": "#test { background-color: rgb(0, 0, 255); }",
      "style-3.css": "html  { background-color: rgb(0, 255, 0); }",
      "script-document-start.js": async () => {
        const testElement = document.querySelector("html");

        browser.test.assertEq(
          "rgb(0, 255, 0)",
          getComputedStyle(testElement).backgroundColor,
          "got expected style in script-document-start.js"
        );

        testElement.style.backgroundColor = "rgb(4, 4, 4)";
      },
      "check-applied-styles-document-start.js": () => {
        browser.test.sendMessage(
          `background-color-${location.pathname.split("/").pop()}`,
          getComputedStyle(document.querySelector("html")).backgroundColor
        );
      },
      "script-document-end-and-idle.js": () => {
        const testElement = document.querySelector("#test");

        browser.test.assertEq(
          "rgb(255, 0, 0)",
          getComputedStyle(testElement).backgroundColor,
          "got expected style in script-document-end-and-idle.js"
        );

        testElement.style.backgroundColor = "rgb(5, 5, 5)";
      },
    },
  });

  const TEST_CASES = [
    {
      title: "a css file",
      params: [
        {
          id: "style-1",
          allFrames: false,
          matches: ["http://*/*/*.html"],
          // TODO: Bug 1759117 - runAt should not affect css injection
          runAt: "document_start",
          persistAcrossSessions: false,
          css: ["style-1.css"],
        },
      ],
      expected: ["rgb(255, 0, 0)", "rgba(0, 0, 0, 0)"],
    },
    {
      title: "css and allFrames: true",
      params: [
        {
          id: "style-1",
          allFrames: true,
          matches: ["http://*/*/*.html"],
          // TODO: Bug 1759117 - runAt should not affect css injection
          runAt: "document_start",
          persistAcrossSessions: false,
          css: ["style-1.css"],
        },
      ],
      expected: ["rgb(255, 0, 0)", "rgb(255, 0, 0)"],
    },
    {
      title: "css and allFrames: true but matches restricted to top frame",
      params: [
        {
          id: "style-1",
          allFrames: true,
          matches: ["http://*/*/file_with_iframe.html"],
          // TODO: Bug 1759117 - runAt should not affect css injection
          runAt: "document_start",
          persistAcrossSessions: false,
          css: ["style-1.css"],
        },
      ],
      expected: ["rgb(255, 0, 0)", "rgba(0, 0, 0, 0)"],
    },
    {
      title: "css and excludeMatches set",
      params: [
        {
          id: "style-1",
          allFrames: true,
          matches: ["http://*/*/*.html"],
          // TODO: Bug 1759117 - runAt should not affect css injection
          runAt: "document_start",
          persistAcrossSessions: false,
          css: ["style-1.css"],
          excludeMatches: ["http://*/*/file_with_iframe.html"],
        },
      ],
      expected: ["rgba(0, 0, 0, 0)", "rgb(255, 0, 0)"],
    },
    {
      title: "two css files",
      params: [
        {
          id: "style-1-and-2",
          allFrames: false,
          matches: ["http://*/*/*.html"],
          // TODO: Bug 1759117 - runAt should not affect css injection
          runAt: "document_start",
          persistAcrossSessions: false,
          css: ["style-1.css", "style-2.css"],
        },
      ],
      expected: ["rgb(0, 0, 255)", "rgba(0, 0, 0, 0)"],
    },
    {
      title: "two scripts with css",
      params: [
        {
          id: "style-1",
          allFrames: false,
          matches: ["http://*/*/*.html"],
          runAt: "document_end",
          persistAcrossSessions: false,
          css: ["style-1.css"],
        },
        {
          id: "style-2",
          allFrames: false,
          matches: ["http://*/*/*.html"],
          runAt: "document_start",
          persistAcrossSessions: false,
          css: ["style-2.css"],
        },
      ],
      // TODO: Bug 1759117 - The expected value should be `rgb(0, 0, 255)`
      // because runAt should not affect css injection and therefore the two
      // styles should be applied one after the other.
      expected: ["rgb(255, 0, 0)", "rgba(0, 0, 0, 0)"],
    },
    {
      title: "js and css with runAt: document_start",
      params: [
        {
          id: "js-and-style-start",
          allFrames: true,
          matches: ["http://*/*/*.html"],
          runAt: "document_start",
          persistAcrossSessions: false,
          css: ["style-3.css"],
          // Inject the check script last to be able to send a message back to
          // the test case. This works with `skipCheckScriptRegistration: true`
          // below.
          js: [
            "script-document-start.js",
            "check-applied-styles-document-start.js",
          ],
        },
      ],
      expected: ["rgb(4, 4, 4)", "rgb(4, 4, 4)"],
      skipCheckScriptRegistration: true,
    },
    {
      title: "js and css with runAt: document_end",
      params: [
        {
          id: "js-and-style-end",
          allFrames: true,
          matches: ["http://*/*/*.html"],
          runAt: "document_end",
          persistAcrossSessions: false,
          css: ["style-1.css"],
          // Inject the check script last to be able to send a message back to
          // the test case. This works with `skipCheckScriptRegistration: true`
          // below.
          js: ["script-document-end-and-idle.js", "check-applied-styles.js"],
        },
      ],
      expected: ["rgb(5, 5, 5)", "rgb(5, 5, 5)"],
      skipCheckScriptRegistration: true,
    },
    {
      title: "js and css with runAt: document_idle",
      params: [
        {
          id: "js-and-style-idle",
          allFrames: true,
          matches: ["http://*/*/*.html"],
          runAt: "document_idle",
          persistAcrossSessions: false,
          css: ["style-1.css"],
          // Inject the check script last to be able to send a message back to
          // the test case. This works with `skipCheckScriptRegistration: true`
          // below.
          js: ["script-document-end-and-idle.js", "check-applied-styles.js"],
        },
      ],
      expected: ["rgb(5, 5, 5)", "rgb(5, 5, 5)"],
      skipCheckScriptRegistration: true,
    },
  ];

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

  for (const {
    title,
    params,
    expected,
    skipCheckScriptRegistration,
  } of TEST_CASES) {
    extension.sendMessage("load-test-case", {
      title,
      params,
      skipCheckScriptRegistration,
    });
    await extension.awaitMessage("load-test-case-done");

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

    const backgroundColors = [
      await extension.awaitMessage("background-color-file_with_iframe.html"),
      await extension.awaitMessage("background-color-file_sample.html"),
    ];

    Assert.deepEqual(
      expected,
      backgroundColors,
      `${title} - got expected colors`
    );

    await contentPage.close();
  }

  await extension.unload();
});