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

const { AddonTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/AddonTestUtils.sys.mjs"
);

AddonTestUtils.initMochitest(this);

add_task(async function testExecuteScript() {
  let { MessageChannel } = ChromeUtils.importESModule(
    "resource://testing-common/MessageChannel.sys.mjs"
  );

  // When the first extension is started, ProxyMessenger.init adds MessageChannel
  // listeners for Services.mm and Services.ppmm, and they are never unsubscribed.
  // We have to exclude them after the extension has been unloaded to get an accurate
  // test.
  function getMessageManagersSize(messageManagers) {
    return Array.from(messageManagers).filter(([mm]) => {
      return ![Services.mm, Services.ppmm].includes(mm);
    }).length;
  }

  let messageManagersSize = getMessageManagersSize(
    MessageChannel.messageManagers
  );
  let responseManagersSize = MessageChannel.responseManagers.size;

  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "http://mochi.test:8888/",
    true
  );

  async function background() {
    let tasks = [
      {
        background: "rgba(0, 0, 0, 0)",
        foreground: "rgb(0, 113, 4)",
        promise: () => {
          return browser.tabs.insertCSS({
            file: "file2.css",
          });
        },
      },
      {
        background: "rgb(42, 42, 42)",
        foreground: "rgb(0, 113, 4)",
        promise: () => {
          return browser.tabs.insertCSS({
            code: "* { background: rgb(42, 42, 42) }",
          });
        },
      },
      {
        background: "rgb(43, 43, 43)",
        foreground: "rgb(0, 113, 4)",
        promise: () => {
          return browser.tabs
            .insertCSS({
              code: "* { background: rgb(100, 100, 100) !important }",
              cssOrigin: "author",
            })
            .then(r =>
              browser.tabs.insertCSS({
                code: "* { background: rgb(43, 43, 43) !important }",
                cssOrigin: "author",
              })
            );
        },
      },
      {
        background: "rgb(100, 100, 100)",
        foreground: "rgb(0, 113, 4)",
        promise: () => {
          // User has higher importance
          return browser.tabs
            .insertCSS({
              code: "* { background: rgb(100, 100, 100) !important }",
              cssOrigin: "user",
            })
            .then(r =>
              browser.tabs.insertCSS({
                code: "* { background: rgb(44, 44, 44) !important }",
                cssOrigin: "author",
              })
            );
        },
      },
    ];

    function checkCSS() {
      let computedStyle = window.getComputedStyle(document.body);
      return [computedStyle.backgroundColor, computedStyle.color];
    }

    try {
      for (let { promise, background, foreground } of tasks) {
        let result = await promise();

        browser.test.assertEq(undefined, result, "Expected callback result");

        [result] = await browser.tabs.executeScript({
          code: `(${checkCSS})()`,
        });

        browser.test.assertEq(
          background,
          result[0],
          "Expected background color"
        );
        browser.test.assertEq(
          foreground,
          result[1],
          "Expected foreground color"
        );
      }

      browser.test.notifyPass("insertCSS");
    } catch (e) {
      browser.test.fail(`Error: ${e} :: ${e.stack}`);
      browser.test.notifyFail("insertCSS");
    }
  }

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["http://mochi.test/"],
    },

    background,

    files: {
      "file2.css": "* { color: rgb(0, 113, 4) }",
    },
  });

  await extension.startup();

  await extension.awaitFinish("insertCSS");

  await extension.unload();

  BrowserTestUtils.removeTab(tab);

  // Make sure that we're not holding on to references to closed message
  // managers.
  is(
    getMessageManagersSize(MessageChannel.messageManagers),
    messageManagersSize,
    "Message manager count"
  );
  is(
    MessageChannel.responseManagers.size,
    responseManagersSize,
    "Response manager count"
  );
  is(MessageChannel.pendingResponses.size, 0, "Pending response count");
});

add_task(async function testInsertCSS_cleanup() {
  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "http://mochi.test:8888/",
    true
  );

  async function background() {
    await browser.tabs.insertCSS({ code: "* { background: rgb(42, 42, 42) }" });
    await browser.tabs.insertCSS({ file: "customize_fg_color.css" });

    browser.test.notifyPass("insertCSS");
  }

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["http://mochi.test/"],
    },
    background,
    files: {
      "customize_fg_color.css": `* { color: rgb(255, 0, 0) }`,
    },
  });

  await extension.startup();

  await extension.awaitFinish("insertCSS");

  const getTabContentComputedStyle = async () => {
    let computedStyle = content.getComputedStyle(content.document.body);
    return [computedStyle.backgroundColor, computedStyle.color];
  };

  const appliedStyles = await SpecialPowers.spawn(
    tab.linkedBrowser,
    [],
    getTabContentComputedStyle
  );

  is(
    appliedStyles[0],
    "rgb(42, 42, 42)",
    "The injected CSS code has been applied as expected"
  );
  is(
    appliedStyles[1],
    "rgb(255, 0, 0)",
    "The injected CSS file has been applied as expected"
  );

  await extension.unload();

  const unloadedStyles = await SpecialPowers.spawn(
    tab.linkedBrowser,
    [],
    getTabContentComputedStyle
  );

  is(
    unloadedStyles[0],
    "rgba(0, 0, 0, 0)",
    "The injected CSS code has been removed as expected"
  );
  is(
    unloadedStyles[1],
    "rgb(0, 0, 0)",
    "The injected CSS file has been removed as expected"
  );

  BrowserTestUtils.removeTab(tab);
});

// Verify that no removeSheet/removeSheetUsingURIString errors are logged while
// cleaning up css injected using a manifest content script or tabs.insertCSS.
add_task(async function test_csscode_cleanup_on_closed_windows() {
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["http://example.com/*"],
      content_scripts: [
        {
          matches: ["http://example.com/*"],
          css: ["content.css"],
          run_at: "document_start",
        },
      ],
    },

    files: {
      "content.css": "body { min-width: 15px; }",
    },

    async background() {
      browser.runtime.onConnect.addListener(port => {
        port.onDisconnect.addListener(() => {
          browser.test.sendMessage("port-disconnected");
        });
        browser.test.sendMessage("port-connected");
      });

      await browser.tabs.create({
        url: "http://example.com/",
        active: true,
      });

      await browser.tabs.insertCSS({
        code: "body { max-width: 50px; }",
      });

      // Create a port, as a way to detect when the content script has been
      // destroyed and any removeSheet error already collected (if it has been
      // raised during the content scripts cleanup).
      await browser.tabs.executeScript({
        code: `(${function () {
          const { maxWidth, minWidth } = window.getComputedStyle(document.body);
          browser.test.sendMessage("body-styles", { maxWidth, minWidth });
          browser.runtime.connect();
        }})();`,
      });
    },
  });

  await extension.startup();

  let { messages } = await AddonTestUtils.promiseConsoleOutput(async () => {
    info("Waiting for content scripts to be injected");

    const { maxWidth, minWidth } = await extension.awaitMessage("body-styles");
    is(maxWidth, "50px", "tabs.insertCSS applied");
    is(minWidth, "15px", "manifest.content_scripts CSS applied");

    await extension.awaitMessage("port-connected");
    const tab = gBrowser.selectedTab;

    info("Close tab and wait for content script port to be disconnected");
    BrowserTestUtils.removeTab(tab);
    await extension.awaitMessage("port-disconnected");
  });

  // Look for nsIDOMWindowUtils.removeSheet and
  // nsIDOMWindowUtils.removeSheetUsingURIString errors.
  AddonTestUtils.checkMessages(
    messages,
    {
      forbidden: [{ errorMessage: /nsIDOMWindowUtils.removeSheet/ }],
    },
    "Expect no remoteSheet errors"
  );

  await extension.unload();
});