async-navigator-clipboard-write-multiple.tentative.https.sub.html (3775B)
1 <!doctype html> 2 <meta charset="utf-8"> 3 <title>Async Clipboard write should cancel the prior pending request</title> 4 <link rel="help" href="https://github.com/w3c/clipboard-apis/issues/161"> 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 <script src="/resources/testdriver.js"></script> 8 <script src="/resources/testdriver-actions.js"></script> 9 <script src="/resources/testdriver-vendor.js"></script> 10 <script src="resources/user-activation.js"></script> 11 12 <iframe width="50" height="50" id="same" src="resources/page.html"></iframe><br> 13 <iframe width="50" height="50" id="cross" src="https://{{hosts[alt][]}}:{{ports[https][1]}}/clipboard-apis/resources/page.html"></iframe><br> 14 <input value="Test"> 15 <script> 16 "use strict"; 17 18 // Permissions are required in order to invoke navigator.clipboard functions in 19 // an automated test. 20 async function getPermissions() { 21 await tryGrantReadPermission(); 22 await tryGrantWritePermission() 23 await waitForUserActivation(); 24 } 25 26 function waitForMessage(msg) { 27 return new Promise((resolve) => { 28 window.addEventListener("message", function handler(e) { 29 if (e.data == msg) { 30 window.removeEventListener("message", handler); 31 resolve(); 32 } 33 }); 34 }); 35 } 36 37 function generateRandomString() { 38 return "random number: " + Math.random(); 39 } 40 41 function testCancelPendingWrite(funcToMakeNewRequest, msg) { 42 promise_test(async t => { 43 await getPermissions(); 44 45 // Create a pending write request which should be rejected after a new 46 // request is made. 47 let resolvePendingPromise; 48 let promise = navigator.clipboard.write([ 49 new ClipboardItem({ 50 "text/plain": new Promise((resolve) => { resolvePendingPromise = resolve; }), 51 }), 52 ]).catch(e => { return e; }); 53 54 // Make a new request that should cancel the prior pending request. 55 let str = generateRandomString(); 56 await funcToMakeNewRequest(str); 57 58 // Pending request should be rejected with NotAllowedError. 59 let error = await promise; 60 assert_not_equals(error, undefined); 61 assert_equals(error.name, "NotAllowedError"); 62 63 // Resolve pending promise. 64 resolvePendingPromise(generateRandomString()); 65 // Check clipboard data. 66 assert_equals(await navigator.clipboard.readText(), str); 67 }, msg); 68 } 69 70 testCancelPendingWrite(async (str) => { 71 // A new write request should cancel the prior pending request. 72 await navigator.clipboard.write([ 73 new ClipboardItem({ 74 "text/plain": str, 75 }), 76 ]).catch(() => { 77 assert_true(false, "should not fail"); 78 }); 79 }, "clipboard.write() should cancel the prior pending one (same document)"); 80 81 testCancelPendingWrite(async (str) => { 82 // A new write should cancel the prior pending request. 83 const iframe = document.getElementById("same"); 84 iframe.contentWindow.postMessage(["write", str], "*"); 85 await waitForMessage("done"); 86 }, "clipboard.write() should cancel the prior pending one (same-origin iframe)"); 87 88 testCancelPendingWrite(async (str) => { 89 // A new write should cancel the prior pending request. 90 const iframe = document.getElementById("cross"); 91 iframe.contentWindow.postMessage(["write", str], "*"); 92 await waitForMessage("done"); 93 }, "clipboard.write() should cancel the prior pending one (cross-origin iframe)"); 94 95 testCancelPendingWrite(async (str) => { 96 const input = document.querySelector("input"); 97 input.value = str; 98 input.focus(); 99 input.select(); 100 101 // A new copy action should cancel the prior pending request. 102 const modifier_key = navigator.platform.includes("Mac") ? "\uE03D" : "\uE009"; 103 await new test_driver.Actions().keyDown(modifier_key).keyDown("c").send(); 104 }, "copy action should cancel the prior pending clipboard.write() request"); 105 106 </script>