summaryrefslogtreecommitdiffstats
path: root/dom/events/test/clipboard/browser_navigator_clipboard_readText_multiple.js
blob: 1d0d2884b76bf18133a7ce90faa734440c9097ef (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
/* -*- Mode: JavaScript; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const kBaseUrlForContent = getRootDirectory(gTestPath).replace(
  "chrome://mochitests/content",
  "https://example.com"
);
const kContentFileName = "file_toplevel.html";
const kContentFileUrl = kBaseUrlForContent + kContentFileName;

async function waitForPasteContextMenu() {
  await waitForPasteMenuPopupEvent("shown");
  const pasteButton = document.getElementById(kPasteMenuItemId);
  info("Wait for paste button enabled");
  await BrowserTestUtils.waitForMutationCondition(
    pasteButton,
    { attributeFilter: ["disabled"] },
    () => !pasteButton.disabled,
    "Wait for paste button enabled"
  );
}

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["dom.events.asyncClipboard.readText", true],
      ["test.events.async.enabled", true],
      // Avoid paste button delay enabling making test too long.
      ["security.dialog_enable_delay", 0],
    ],
  });
});

add_task(async function test_multiple_readText_from_same_frame_allow() {
  // Randomized text to avoid overlapping with other tests.
  const clipboardText = await promiseWritingRandomTextToClipboard();

  await BrowserTestUtils.withNewTab(kContentFileUrl, async function (browser) {
    const pasteButtonIsShown = waitForPasteContextMenu();
    const readTextRequest1 = SpecialPowers.spawn(browser, [], async () => {
      content.document.notifyUserGestureActivation();
      return content.eval(`navigator.clipboard.readText();`);
    });
    await pasteButtonIsShown;

    info("readText() from same frame again before interact with paste button");
    const readTextRequest2 = SpecialPowers.spawn(browser, [], async () => {
      content.document.notifyUserGestureActivation();
      return content.eval(`navigator.clipboard.readText();`);
    });
    // Give some time for the second request to arrive parent process.
    await SpecialPowers.spawn(browser, [], async () => {
      return new Promise(resolve => {
        content.setTimeout(resolve, 0);
      });
    });

    info("Click paste button, both request should be resolved");
    await promiseClickPasteButton();
    is(
      await readTextRequest1,
      clipboardText,
      "First request should be resolved"
    );
    is(
      await readTextRequest2,
      clipboardText,
      "Second request should be resolved"
    );
  });
});

add_task(async function test_multiple_readText_from_same_frame_deny() {
  // Randomized text to avoid overlapping with other tests.
  const clipboardText = await promiseWritingRandomTextToClipboard();

  await BrowserTestUtils.withNewTab(kContentFileUrl, async function (browser) {
    const pasteButtonIsShown = waitForPasteContextMenu();
    const readTextRequest1 = SpecialPowers.spawn(browser, [], async () => {
      content.document.notifyUserGestureActivation();
      return content.eval(`navigator.clipboard.readText();`);
    });
    await pasteButtonIsShown;

    info("readText() from same frame again before interact with paste button");
    const readTextRequest2 = SpecialPowers.spawn(browser, [], async () => {
      content.document.notifyUserGestureActivation();
      return content.eval(`navigator.clipboard.readText();`);
    });
    // Give some time for the second request to arrive parent process.
    await SpecialPowers.spawn(browser, [], async () => {
      return new Promise(resolve => {
        content.setTimeout(resolve, 0);
      });
    });

    info("Dismiss paste button, both request should be rejected");
    await promiseDismissPasteButton();
    await Assert.rejects(
      readTextRequest1,
      /NotAllowedError/,
      "First request should be rejected"
    );
    await Assert.rejects(
      readTextRequest2,
      /NotAllowedError/,
      "Second request should be rejected"
    );
  });
});

add_task(async function test_multiple_readText_from_same_origin_frame() {
  // Randomized text to avoid overlapping with other tests.
  const clipboardText = await promiseWritingRandomTextToClipboard();

  await BrowserTestUtils.withNewTab(kContentFileUrl, async function (browser) {
    const pasteButtonIsShown = waitForPasteContextMenu();
    const readTextRequest1 = SpecialPowers.spawn(browser, [], async () => {
      content.document.notifyUserGestureActivation();
      return content.eval(`navigator.clipboard.readText();`);
    });
    await pasteButtonIsShown;

    info(
      "readText() from same origin child frame again before interacting with paste button"
    );
    const sameOriginFrame = browser.browsingContext.children[0];
    const readTextRequest2 = SpecialPowers.spawn(
      sameOriginFrame,
      [],
      async () => {
        content.document.notifyUserGestureActivation();
        return content.eval(`navigator.clipboard.readText();`);
      }
    );
    // Give some time for the second request to arrive parent process.
    await SpecialPowers.spawn(sameOriginFrame, [], async () => {
      return new Promise(resolve => {
        content.setTimeout(resolve, 0);
      });
    });

    info("Click paste button, both request should be resolved");
    await promiseClickPasteButton();
    is(
      await readTextRequest1,
      clipboardText,
      "First request should be resolved"
    );
    is(
      await readTextRequest2,
      clipboardText,
      "Second request should be resolved"
    );
  });
});

add_task(async function test_multiple_readText_from_cross_origin_frame() {
  // Randomized text to avoid overlapping with other tests.
  const clipboardText = await promiseWritingRandomTextToClipboard();

  await BrowserTestUtils.withNewTab(kContentFileUrl, async function (browser) {
    const pasteButtonIsShown = waitForPasteContextMenu();
    const readTextRequest1 = SpecialPowers.spawn(browser, [], async () => {
      content.document.notifyUserGestureActivation();
      return content.eval(`navigator.clipboard.readText();`);
    });
    await pasteButtonIsShown;

    info(
      "readText() from different origin child frame again before interacting with paste button"
    );
    const crossOriginFrame = browser.browsingContext.children[1];
    await Assert.rejects(
      SpecialPowers.spawn(crossOriginFrame, [], async () => {
        content.document.notifyUserGestureActivation();
        return content.eval(`navigator.clipboard.readText();`);
      }),
      /NotAllowedError/,
      "Second request should be rejected"
    );

    info("Click paste button, both request should be resolved");
    await promiseClickPasteButton();
    is(
      await readTextRequest1,
      clipboardText,
      "First request should be resolved"
    );
  });
});

add_task(async function test_multiple_readText_from_background_frame() {
  // Randomized text to avoid overlapping with other tests.
  const clipboardText = await promiseWritingRandomTextToClipboard();

  const backgroundTab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    kContentFileUrl
  );

  await BrowserTestUtils.withNewTab(kContentFileUrl, async function (browser) {
    const pasteButtonIsShown = waitForPasteContextMenu();
    const readTextRequest1 = SpecialPowers.spawn(browser, [], async () => {
      content.document.notifyUserGestureActivation();
      return content.eval(`navigator.clipboard.readText();`);
    });
    await pasteButtonIsShown;

    info(
      "readText() from background tab again before interact with paste button"
    );
    await Assert.rejects(
      SpecialPowers.spawn(backgroundTab.linkedBrowser, [], async () => {
        content.document.notifyUserGestureActivation();
        return content.eval(`navigator.clipboard.readText();`);
      }),
      /NotAllowedError/,
      "Second request should be rejected"
    );

    info("Click paste button, both request should be resolved");
    await promiseClickPasteButton();
    is(
      await readTextRequest1,
      clipboardText,
      "First request should be resolved"
    );
  });

  await BrowserTestUtils.removeTab(backgroundTab);
});

add_task(async function test_multiple_readText_from_background_window() {
  // Randomized text to avoid overlapping with other tests.
  const clipboardText = await promiseWritingRandomTextToClipboard();

  await BrowserTestUtils.withNewTab(kContentFileUrl, async function (browser) {
    const newWin = await BrowserTestUtils.openNewBrowserWindow();
    const backgroundTab = await BrowserTestUtils.openNewForegroundTab(
      newWin.gBrowser,
      kContentFileUrl
    );
    await SimpleTest.promiseFocus(browser);

    info("readText() from background window");
    await Assert.rejects(
      SpecialPowers.spawn(backgroundTab.linkedBrowser, [], async () => {
        content.document.notifyUserGestureActivation();
        return content.eval(`navigator.clipboard.readText();`);
      }),
      /NotAllowedError/,
      "Request from background window should be rejected"
    );

    const pasteButtonIsShown = waitForPasteContextMenu();
    const readTextRequest1 = SpecialPowers.spawn(browser, [], async () => {
      content.document.notifyUserGestureActivation();
      return content.eval(`navigator.clipboard.readText();`);
    });
    await pasteButtonIsShown;

    info(
      "readText() from background window again before interact with paste button"
    );
    await Assert.rejects(
      SpecialPowers.spawn(backgroundTab.linkedBrowser, [], async () => {
        content.document.notifyUserGestureActivation();
        return content.eval(`navigator.clipboard.readText();`);
      }),
      /NotAllowedError/,
      "Second request should be rejected"
    );

    info("Click paste button, both request should be resolved");
    await promiseClickPasteButton();
    is(
      await readTextRequest1,
      clipboardText,
      "First request should be resolved"
    );

    await BrowserTestUtils.closeWindow(newWin);
  });
});

add_task(async function test_multiple_readText_focuse_in_chrome_document() {
  // Randomized text to avoid overlapping with other tests.
  const clipboardText = await promiseWritingRandomTextToClipboard();

  const win = await BrowserTestUtils.openNewBrowserWindow();
  const tab = await BrowserTestUtils.openNewForegroundTab(
    win.gBrowser,
    kContentFileUrl
  );

  info("Move focus to url bar");
  win.gURLBar.focus();

  info("readText() from web content");
  await Assert.rejects(
    SpecialPowers.spawn(tab.linkedBrowser, [], async () => {
      content.document.notifyUserGestureActivation();
      return content.eval(`navigator.clipboard.readText();`);
    }),
    /NotAllowedError/,
    "Request should be rejected when focus is not in content"
  );

  await BrowserTestUtils.closeWindow(win);
});