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
|
<!doctype html>
<meta charset="utf-8">
<title>Async Clipboard write should cancel the prior pending request</title>
<link rel="help" href="https://github.com/w3c/clipboard-apis/issues/161">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="resources/user-activation.js"></script>
<iframe width="50" height="50" id="same" src="resources/page.html"></iframe><br>
<iframe width="50" height="50" id="cross" src="https://{{hosts[alt][]}}:{{ports[https][1]}}/clipboard-apis/resources/page.html"></iframe><br>
<input value="Test">
<script>
"use strict";
// Permissions are required in order to invoke navigator.clipboard functions in
// an automated test.
async function getPermissions() {
await tryGrantReadPermission();
await tryGrantWritePermission()
await waitForUserActivation();
}
function waitForMessage(msg) {
return new Promise((resolve) => {
window.addEventListener("message", function handler(e) {
if (e.data == msg) {
window.removeEventListener("message", handler);
resolve();
}
});
});
}
function generateRandomString() {
return "random number: " + Math.random();
}
function testCancelPendingWrite(funcToMakeNewRequest, msg) {
promise_test(async t => {
await getPermissions();
// Create a pending write request which should be rejected after a new
// request is made.
let resolvePendingPromise;
let promise = navigator.clipboard.write([
new ClipboardItem({
"text/plain": new Promise((resolve) => { resolvePendingPromise = resolve; }),
}),
]).catch(e => { return e; });
// Make a new request that should cancel the prior pending request.
let str = generateRandomString();
await funcToMakeNewRequest(str);
// Pending request should be rejected with NotAllowedError.
let error = await promise;
assert_not_equals(error, undefined);
assert_equals(error.name, "NotAllowedError");
// Resolve pending promise.
resolvePendingPromise(generateRandomString());
// Check clipboard data.
assert_equals(await navigator.clipboard.readText(), str);
}, msg);
}
testCancelPendingWrite(async (str) => {
// A new write request should cancel the prior pending request.
await navigator.clipboard.write([
new ClipboardItem({
"text/plain": str,
}),
]).catch(() => {
assert_true(false, "should not fail");
});
}, "clipboard.write() should cancel the prior pending one (same document)");
testCancelPendingWrite(async (str) => {
// A new write should cancel the prior pending request.
const iframe = document.getElementById("same");
iframe.contentWindow.postMessage(["write", str], "*");
await waitForMessage("done");
}, "clipboard.write() should cancel the prior pending one (same-origin iframe)");
testCancelPendingWrite(async (str) => {
// A new write should cancel the prior pending request.
const iframe = document.getElementById("cross");
iframe.contentWindow.postMessage(["write", str], "*");
await waitForMessage("done");
}, "clipboard.write() should cancel the prior pending one (cross-origin iframe)");
testCancelPendingWrite(async (str) => {
const input = document.querySelector("input");
input.value = str;
input.focus();
input.select();
// A new copy action should cancel the prior pending request.
const modifier_key = navigator.platform.includes("Mac") ? "\uE03D" : "\uE009";
await new test_driver.Actions().keyDown(modifier_key).keyDown("c").send();
}, "copy action should cancel the prior pending clipboard.write() request");
</script>
|