5 <title>Tests scripting.insertCSS()
</title>
6 <script type=
"text/javascript" src=
"/tests/SimpleTest/SimpleTest.js"></script>
7 <script type=
"text/javascript" src=
"/tests/SimpleTest/ExtensionTestUtils.js"></script>
8 <script type=
"text/javascript" src=
"head.js"></script>
9 <link rel=
"stylesheet" href=
"/tests/SimpleTest/test.css"/>
13 <script type=
"text/javascript">
16 const MOCHITEST_HOST_PERMISSIONS
= [
18 "*://mochi.xorigin-test/",
19 "*://test1.example.com/",
22 const makeExtension
= ({ manifest
: manifestProps
, ...otherProps
}) => {
23 return ExtensionTestUtils
.loadExtension({
26 permissions
: ["scripting"],
28 ...MOCHITEST_HOST_PERMISSIONS
,
29 // Used in `file_contains_iframe.html`
30 "https://example.org/",
32 granted_host_permissions
: true,
35 useAddonManager
: "temporary",
40 add_task(async
function setup() {
41 await SpecialPowers
.pushPrefEnv({
42 set: [["extensions.manifestV3.enabled", true]],
46 add_task(async
function test_insertCSS_and_removeCSS_params_validation() {
47 let extension
= makeExtension({
49 const tabs
= await browser
.tabs
.query({ active
: true });
53 title
: "no files and no css",
55 expectedError
: "Exactly one of files and css must be specified.",
58 title
: "both files and css are passed",
60 files
: ["styles.css"],
61 css
: "* { background: rgb(1, 1, 1) }",
63 expectedError
: "Exactly one of files and css must be specified.",
66 title
: "both allFrames and frameIds are passed",
73 files
: ["styles.css"],
75 expectedError
: "Cannot specify both 'allFrames' and 'frameIds'.",
78 title
: "empty css string with a file",
81 files
: ["styles.css"],
83 expectedError
: "Exactly one of files and css must be specified.",
87 for (const { title
, cssParams
, expectedError
} of TEST_CASES
) {
88 await browser
.test
.assertRejects(
89 browser
.scripting
.insertCSS({
90 target
: { tabId
: tabs
[0].id
},
94 `${title} - expected error for insertCSS()`
97 await browser
.test
.assertRejects(
98 browser
.scripting
.removeCSS({
99 target
: { tabId
: tabs
[0].id
},
103 `${title} - expected error for removeCSS()`
107 browser
.test
.notifyPass("checks-done");
111 await extension
.startup();
112 await extension
.awaitFinish("checks-done");
113 await extension
.unload();
116 add_task(async
function test_insertCSS_with_invalid_tabId() {
117 let extension
= makeExtension({
119 // This tab ID should not exist.
120 const tabId
= 123456789;
122 await browser
.test
.assertRejects(
123 browser
.scripting
.insertCSS({
125 css
: "* { background: rgb(1, 1, 1) }",
127 `Invalid tab ID: ${tabId}`
130 browser
.test
.notifyPass("insert-css");
134 await extension
.startup();
135 await extension
.awaitFinish("insert-css");
136 await extension
.unload();
139 add_task(async
function test_insertCSS_with_wrong_host_permissions() {
140 let extension
= makeExtension({
142 host_permissions
: [],
145 const tabs
= await browser
.tabs
.query({ active
: true });
147 browser
.test
.assertEq(1, tabs
.length
, "expected 1 tab");
149 browser
.test
.assertRejects(
150 browser
.scripting
.insertCSS({
151 target
: { tabId
: tabs
[0].id
},
152 css
: "* { background: rgb(1, 1, 1) }",
154 /Missing host permission for the tab/,
155 "expected host permission error"
158 browser
.test
.notifyPass("insert-css");
162 await extension
.startup();
163 await extension
.awaitFinish("insert-css");
164 await extension
.unload();
167 add_task(async
function test_insertCSS_and_removeCSS() {
168 let extension
= makeExtension({
170 permissions
: ["scripting", "webNavigation"],
173 const tabs
= await browser
.tabs
.query({ active
: true });
174 browser
.test
.assertEq(1, tabs
.length
, "expected 1 tab");
176 const tabId
= tabs
[0].id
;
178 const frames
= await browser
.webNavigation
.getAllFrames({ tabId
});
179 // 1. Top-level frame that loads `file_contains_iframe.html`
180 // 2. Frame that loads `file_contains_img.html`
181 browser
.test
.assertEq(2, frames
.length
, "expected 2 frames");
182 const frameIds
= frames
.map(frame
=> frame
.frameId
);
184 const cssColor1
= "rgb(1, 1, 1)";
185 const cssColor2
= "rgb(2, 2, 2)";
186 const cssColorInFile1
= "rgb(3, 3, 3)";
187 const defaultColor
= "rgba(0, 0, 0, 0)";
191 title
: "with css prop",
196 css
: `#div-1 { background: ${cssColor1} }`,
199 expectedResults
: [cssColor1
, defaultColor
],
202 title
: "with a file",
207 files
: ["file1.css"],
210 expectedResults
: [cssColorInFile1
, defaultColor
],
213 title
: "css prop in a single frame",
217 target
: { tabId
, frameIds
: [frameIds
[0]] },
218 css
: `#div-3 { background: ${cssColor2} }`,
221 expectedResults
: [cssColor2
, defaultColor
],
224 title
: "css prop in multiple frames",
228 target
: { tabId
, frameIds
},
229 css
: `#div-4 { background: ${cssColor1} }`,
232 expectedResults
: [cssColor1
, cssColor1
],
235 title
: "allFrames is true",
239 target
: { tabId
, allFrames
: true },
240 css
: `#div-5 { background: ${defaultColor} }`,
243 expectedResults
: [defaultColor
, defaultColor
],
246 title
: "origin: 'AUTHOR'",
251 css
: `#div-6 { background: ${cssColor1} }`,
256 css
: `#div-6 { background: ${cssColor2} }`,
260 expectedResults
: [cssColor2
, defaultColor
],
263 title
: "origin: 'USER'",
268 css
: `#div-7 { background: ${cssColor1} !important }`,
273 css
: `#div-7 { background: ${cssColor2} !important }`,
277 // User has higher importance.
278 expectedResults
: [cssColor1
, defaultColor
],
281 title
: "empty css string",
289 expectedResults
: [defaultColor
, defaultColor
],
292 title
: "allFrames is false",
296 target
: { tabId
, allFrames
: false },
297 css
: `#div-9 { background: ${cssColor1} }`,
300 expectedResults
: [cssColor1
, defaultColor
],
304 const getBackgroundColor
= elementId
=> {
305 return window
.getComputedStyle(document
.getElementById(elementId
))
315 // Create a unique element for the current test case.
316 await browser
.scripting
.executeScript({
317 target
: { tabId
, allFrames
: true },
319 const element
= document
.createElement("div");
320 element
.setAttribute("id", elementId
);
321 document
.body
.appendChild(element
);
326 for (const params
of cssParams
) {
327 const result
= await browser
.scripting
.insertCSS(params
);
328 // `insertCSS()` should not resolve to a value.
329 browser
.test
.assertEq(undefined, result
, "got expected empty result");
332 let results
= await browser
.scripting
.executeScript({
333 target
: { tabId
, allFrames
: true },
334 func
: getBackgroundColor
,
337 results
.sort((a
, b
) => a
.frameId
- b
.frameId
);
339 browser
.test
.assertEq(
340 expectedResults
.length
,
342 `${title} - got the expected number of results`
344 results
.forEach((result
, index
) => {
345 browser
.test
.assertEq(
346 expectedResults
[index
],
348 `${title} - got expected result (index=${index}): ${title}`
352 results
= await Promise
.all(
353 cssParams
.map(params
=> browser
.scripting
.removeCSS(params
))
355 // `removeCSS()` should not resolve to a value.
356 results
.forEach(result
=> {
357 browser
.test
.assertEq(undefined, result
, "got expected empty result");
360 results
= await browser
.scripting
.executeScript({
361 target
: { tabId
, allFrames
: true },
362 func
: getBackgroundColor
,
366 browser
.test
.assertTrue(
367 results
.every(({ result
}) => result
=== defaultColor
),
368 "got expected default color in all frames"
372 browser
.test
.notifyPass("insert-and-remove-css");
375 "file1.css": "#div-2 { background: rgb(3, 3, 3) }",
379 let tab
= await AppTestDelegate
.openNewForegroundTab(
381 "https://test1.example.com/tests/toolkit/components/extensions/test/mochitest/file_contains_iframe.html",
385 await extension
.startup();
386 await extension
.awaitFinish("insert-and-remove-css");
387 await extension
.unload();
389 await AppTestDelegate
.removeTab(window
, tab
);