test_useractivation_has_been_activated.html (4308B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <title>User activation test: has been user gesture activated</title> 5 <script src="/tests/SimpleTest/SimpleTest.js"></script> 6 <script src="/tests/SimpleTest/EventUtils.js"></script> 7 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 8 </head> 9 <body> 10 <iframe></iframe> 11 <iframe></iframe> 12 <script> 13 14 function waitForEvent(aTarget, aEvent, aCallback) { 15 return new Promise((aResolve) => { 16 aTarget.addEventListener(aEvent, function listener(event) { 17 aCallback(event); 18 aResolve(); 19 }, { once: true }); 20 }); 21 } 22 23 let [iframe0, iframe1] = document.querySelectorAll("iframe"); 24 25 function doCheck(aDocument, aName, aHasBeenUserGestureActivated) { 26 is(SpecialPowers.wrap(aDocument).hasBeenUserGestureActivated, 27 aHasBeenUserGestureActivated, 28 `check has-been-user-activated on the ${aName}`); 29 if (aHasBeenUserGestureActivated) { 30 ok(SpecialPowers.wrap(aDocument).lastUserGestureTimeStamp > 0, 31 `check last-user-gesture-timestamp on the ${aName}`); 32 } else { 33 is(SpecialPowers.wrap(aDocument).lastUserGestureTimeStamp, 0, 34 `check last-user-gesture-timestamp on the ${aName}`); 35 } 36 } 37 38 add_task(async function checkInitialStatus() { 39 doCheck(document, "top-level document", false); 40 doCheck(frames[0].document, "first iframe", false); 41 doCheck(frames[1].document, "second iframe", false); 42 }); 43 44 add_task(async function triggerUserActivation() { 45 // Trigger user activation on the first iframe. 46 SpecialPowers.wrap(frames[0].document).notifyUserGestureActivation(); 47 // We should also propagate to all the ancestors. 48 doCheck(document, "top-level document", true); 49 doCheck(frames[0].document, "first iframe", true); 50 doCheck(frames[1].document, "second iframe", false); 51 }); 52 53 add_task(async function iframeNavigation() { 54 frames[0].frameElement.src = "file_empty.html"; 55 await waitForEvent(frames[0].frameElement, "load", () => {}); 56 // We should reset the flag on iframe that navigates away from current page, 57 // but the flag on its ancestor isn't changed. 58 doCheck(document, "top-level document", true); 59 doCheck(frames[0].document, "first iframe", false); 60 doCheck(frames[1].document, "second iframe", false); 61 }); 62 63 add_task(async function triggerUserActivationOnCrossOriginFrame() { 64 // Reset the activation flag. 65 SpecialPowers.wrap(document).clearUserGestureActivation(); 66 doCheck(document, "top-level document", false); 67 68 // load cross-origin test page on iframe. 69 frames[0].frameElement.src = "https://example.com/tests/dom/base/test/useractivation/file_iframe_user_activated.html"; 70 await waitForEvent(window, "message", (event) => { 71 if (event.data === "done") { 72 doCheck(document, "top-level document", true); 73 doCheck(frames[1].document, "second iframe", false); 74 } else { 75 ok(false, "receive unexpected message: " + event.data); 76 } 77 }); 78 }); 79 80 add_task(async function propagateToSameOriginConnectedSubframe() { 81 // Reset the activation flag. 82 SpecialPowers.wrap(document).clearUserGestureActivation(); 83 84 // load cross-origin test page on iframe. 85 iframe0.src = "https://example.com/tests/dom/base/test/useractivation/file_iframe_check_user_activation.html"; 86 await waitForEvent(window, "message", (event) => { 87 if (event.data !== "loaded") { 88 ok(false, "receive unexpected message: " + event.data); 89 } 90 }); 91 92 // Trigger user activation on top-level document. 93 SpecialPowers.wrap(document).notifyUserGestureActivation(); 94 doCheck(document, "top-level document", true); 95 doCheck(iframe1.contentDocument, "second iframe", true); 96 97 iframe0.contentWindow.postMessage("get", "*"); 98 await waitForEvent(window, "message", (event) => { 99 if (typeof event.data === "object") { 100 ok(!event.data.hasBeenActivated, "check has-been-user-activated on the first iframe"); 101 is(event.data.lastActivationTimestamp, 0, "check last-user-gesture-timestamp on the first iframe"); 102 } else { 103 ok(false, "receive unexpected message: " + event.data); 104 } 105 }); 106 }); 107 108 add_task(async function endTests() { 109 // Reset the activation flag in order not to interfere following test in the 110 // verify mode which would run the test using same document couple times. 111 SpecialPowers.wrap(document).clearUserGestureActivation(); 112 }); 113 114 </script> 115 </body>