File: test_ext_scripting_removeCSS.html

package info (click to toggle)
firefox 148.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,719,656 kB
  • sloc: cpp: 7,618,171; javascript: 6,701,506; ansic: 3,781,787; python: 1,418,364; xml: 638,647; asm: 438,962; java: 186,285; sh: 62,885; makefile: 19,010; objc: 13,092; perl: 12,763; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (129 lines) | stat: -rw-r--r-- 3,511 bytes parent folder | download | duplicates (2)
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
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Tests scripting.removeCSS()</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
  <script type="text/javascript" src="head.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
</head>
<body>

<script type="text/javascript">
"use strict";

const MOCHITEST_HOST_PERMISSIONS = [
  "*://mochi.test/",
  "*://mochi.xorigin-test/",
  "*://test1.example.com/",
];

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

add_task(async function test_removeCSS_with_invalid_tabId() {
  let extension = makeExtension({
    async background() {
      // This tab ID should not exist.
      const tabId = 123456789;

      await browser.test.assertRejects(
        browser.scripting.removeCSS({
          target: { tabId },
          css: "* { background: rgb(42, 42, 42) }",
        }),
        `Invalid tab ID: ${tabId}`
      );

      browser.test.notifyPass("remove-css");
    },
  });

  await extension.startup();
  await extension.awaitFinish("remove-css");
  await extension.unload();
});

add_task(async function test_removeCSS_without_insertCSS_called_before() {
  let extension = makeExtension({
    async background() {
      const tabs = await browser.tabs.query({ active: true });
      browser.test.assertEq(1, tabs.length, "expected 1 tab");

      browser.scripting
        .removeCSS({
          target: { tabId: tabs[0].id },
          css: "* { background: rgb(42, 42, 42) }",
        })
        .then(() => {
          browser.test.notifyPass("remove-css");
        })
        .catch(() => {
          browser.test.notifyFail("remove-css");
        });
    },
  });

  await extension.startup();
  await extension.awaitFinish("remove-css");
  await extension.unload();
});

add_task(async function test_removeCSS_with_origin_mismatch() {
  let extension = makeExtension({
    async background() {
      const tabs = await browser.tabs.query({ active: true });
      browser.test.assertEq(1, tabs.length, "expected 1 tab");

      const cssColor = "rgb(42, 42, 42)";
      const cssParams = {
        target: { tabId: tabs[0].id },
        css: `* { background: ${cssColor} !important }`,
      };

      await browser.scripting.insertCSS({ ...cssParams, origin: "AUTHOR" });

      let results = await browser.scripting.executeScript({
        target: { tabId: tabs[0].id },
        func: () => {
          return window.getComputedStyle(document.body).backgroundColor;
        },
      });

      browser.test.assertEq(cssColor, results[0].result, "got expected color");

      // Here, we pass a different origin, which should result in no CSS
      // removal.
      await browser.scripting.removeCSS({ ...cssParams, origin: "USER" });

      browser.test.assertEq(
        cssColor,
        results[0].result,
        "got expected color after removeCSS"
      );

      browser.test.notifyPass("remove-css");
    },
  });

  await extension.startup();
  await extension.awaitFinish("remove-css");
  await extension.unload();
});

</script>

</body>
</html>