summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/mochitest/test_ext_scripting_executeScript_injectImmediately.html
blob: 9d05925adca6d1f15e3834249228d084346eb14b (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
<!DOCTYPE HTML>
  <html>
<head>
  <meta charset="utf-8">
  <title>Tests scripting.executeScript() and injectImmediately</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,
        // Used in `file_contains_iframe.html`
        "https://example.org/",
      ],
      granted_host_permissions: true,
      ...manifestProps,
    },
    useAddonManager: "temporary",
    ...otherProps,
  });
};

add_task(async function setup() {
  await SpecialPowers.pushPrefEnv({
    set: [["extensions.manifestV3.enabled", true]],
  });
});

add_task(async function test_executeScript_injectImmediately() {
  let extension = makeExtension({
    async background() {
      const tabs = await browser.tabs.query({ active: true });
      const tabId = tabs[0].id;

      let onUpdatedPromise = (tabId, url, status) => {
        return new Promise(resolve => {
          browser.tabs.onUpdated.addListener(function listener(_, changed, tab) {
            if (tabId == tab.id && changed.status == status && tab.url == url) {
              browser.tabs.onUpdated.removeListener(listener);
              resolve();
            }
          });
        });
      };

      const url = [
        "https://test1.example.com/tests/toolkit/components/extensions/test/mochitest/",
        `file_slowed_document.sjs?with-iframe&r=${Math.random()}`,
      ].join("");
      const loadingPromise = onUpdatedPromise(tabId, url, "loading");
      const completePromise = onUpdatedPromise(tabId, url, "complete");

      await browser.tabs.update(tabId, { url });
      await loadingPromise;

      const func = () => {
        window.counter = (window.counter || 0) + 1;

        return window.counter;
      };

      let results = await Promise.all([
        // counter = 1
        browser.scripting.executeScript({
          target: { tabId },
          func,
          injectImmediately: true,
        }),
        // counter = 3
        browser.scripting.executeScript({
          target: { tabId },
          func,
          injectImmediately: false,
        }),
        // counter = 4
        browser.scripting.executeScript({
          target: { tabId },
          func,
          // `injectImmediately` is `false` by default
        }),
        // counter = 2
        browser.scripting.executeScript({
          target: { tabId },
          func,
          injectImmediately: true,
        }),
        // counter = 5
        browser.scripting.executeScript({
          target: { tabId },
          func,
          injectImmediately: false,
        }),
      ]);
      browser.test.assertEq(
        5,
        results.length,
        "got expected number of results"
      );
      browser.test.assertEq(
        "1 3 4 2 5",
        results.map(res => res[0].result).join(" "),
        `got expected results: ${JSON.stringify(results)}`
      );

      await completePromise;

      browser.test.notifyPass("execute-script");
    },
  });


  let tab = await AppTestDelegate.openNewForegroundTab(
    window,
    "https://test1.example.com/",
    true
  );

  await extension.startup();
  await extension.awaitFinish("execute-script");
  await extension.unload();

  await AppTestDelegate.removeTab(window, tab);
});

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

      const tabId = tabs[0].id;

      const func = () => {
        window.counter = (window.counter || 0) + 1;

        return window.counter;
      };

      let results = await Promise.all([
        browser.scripting.executeScript({
          target: { tabId },
          func,
          injectImmediately: true,
        }),
        browser.scripting.executeScript({
          target: { tabId },
          func,
          injectImmediately: false,
        }),
        browser.scripting.executeScript({
          target: { tabId },
          func,
          // `injectImmediately` is `false` by default
        }),
        browser.scripting.executeScript({
          target: { tabId },
          func,
          injectImmediately: true,
        }),
        browser.scripting.executeScript({
          target: { tabId },
          func,
          injectImmediately: false,
        }),
      ]);
      browser.test.assertEq(
        5,
        results.length,
        "got expected number of results"
      );
      browser.test.assertEq(
        "1 2 3 4 5",
        results.map(res => res[0].result).join(" "),
        `got expected results: ${JSON.stringify(results)}`
      );

      browser.test.notifyPass("execute-script");
    },
  });

  let tab = await AppTestDelegate.openNewForegroundTab(
    window,
    "https://test1.example.com/tests/toolkit/components/extensions/test/mochitest/file_contains_iframe.html",
    true
  );

  await extension.startup();
  await extension.awaitFinish("execute-script");
  await extension.unload();

  await AppTestDelegate.removeTab(window, tab);
});

</script>

</body>
</html>