test_useractivation_transient.html (7041B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <title>User activation test: transient flag</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 SimpleTest.requestFlakyTimeout("Timeouts are needed to test transient user_activation"); 15 16 let timeout = SpecialPowers.getIntPref("dom.user_activation.transient.timeout") + 1000; 17 let [iframe0, iframe1] = document.querySelectorAll("iframe"); 18 19 function waitForEvent(aTarget, aEvent, aCallback) { 20 return new Promise((aResolve) => { 21 aTarget.addEventListener(aEvent, function listener(event) { 22 aCallback(event); 23 aResolve(); 24 }, { once: true }); 25 }); 26 } 27 28 add_task(async function checkInitialStatus() { 29 ok(!SpecialPowers.wrap(document).hasValidTransientUserGestureActivation, 30 "check has-valid-transient-user-activation on top-level document"); 31 ok(!SpecialPowers.wrap(frames[0].document).hasValidTransientUserGestureActivation, 32 "check has-valid-transient-user-activation on first iframe"); 33 ok(!SpecialPowers.wrap(frames[1].document).hasValidTransientUserGestureActivation, 34 "check has-valid-transient-user-activation on second iframe"); 35 }); 36 37 add_task(async function triggerUserActivation() { 38 // Trigger user activation on the first iframe. 39 SpecialPowers.wrap(frames[0].document).notifyUserGestureActivation(); 40 41 // We should also propagate to all the ancestors. 42 ok(SpecialPowers.wrap(document).hasValidTransientUserGestureActivation, 43 "check has-valid-transient-user-activation on the top-level document"); 44 ok(SpecialPowers.wrap(frames[0].document).hasValidTransientUserGestureActivation, 45 "check has-valid-transient-user-activation on the first iframe"); 46 ok(!SpecialPowers.wrap(frames[1].document).hasValidTransientUserGestureActivation, 47 "check has-valid-transient-user-activation on the second iframe"); 48 }); 49 50 add_task(async function iframeNavigation() { 51 frames[0].frameElement.src = "file_empty.html"; 52 await waitForEvent(frames[0].frameElement, "load", () => {}); 53 // We should reset the flag on iframe that navigates away from current page, 54 // but the flag on its ancestor isn't changed. 55 ok(SpecialPowers.wrap(document).hasValidTransientUserGestureActivation, 56 "check has-valid-transient-user-activation on the top-level document"); 57 ok(!SpecialPowers.wrap(frames[0].document).hasValidTransientUserGestureActivation, 58 "check has-valid-transient-user-activation on the first iframe"); 59 ok(!SpecialPowers.wrap(frames[1].document).hasValidTransientUserGestureActivation, 60 "check has-valid-transient-user-activation on the second iframe"); 61 }); 62 63 add_task(async function triggerUserActivationTimeout() { 64 // Trigger user activation on the first iframe. 65 SpecialPowers.wrap(frames[0].document).notifyUserGestureActivation(); 66 67 // hasValidTransientUserGestureActivation should return false after timeout. 68 await new Promise((aResolve) => { 69 setTimeout(() => { 70 ok(!SpecialPowers.wrap(document).hasValidTransientUserGestureActivation, 71 "check has-valid-transient-user-activation on the top-level document"); 72 ok(!SpecialPowers.wrap(frames[0].document).hasValidTransientUserGestureActivation, 73 "check has-valid-transient-user-activation on the first iframe"); 74 ok(!SpecialPowers.wrap(frames[1].document).hasValidTransientUserGestureActivation, 75 "check has-valid-transient-user-activation on the second iframe"); 76 aResolve(); 77 }, timeout); 78 }); 79 80 // Trigger user activation again. 81 SpecialPowers.wrap(frames[0].document).notifyUserGestureActivation(); 82 ok(SpecialPowers.wrap(document).hasValidTransientUserGestureActivation, 83 "check has-valid-transient-user-activation on the top-level document"); 84 ok(SpecialPowers.wrap(frames[0].document).hasValidTransientUserGestureActivation, 85 "check has-valid-transient-user-activation on the first iframe"); 86 ok(!SpecialPowers.wrap(frames[1].document).hasValidTransientUserGestureActivation, 87 "check has-valid-transient-user-activation on the second iframe"); 88 }); 89 90 add_task(async function triggerUserActivationOnCrossOriginFrame() { 91 // Reset the activation flag. 92 SpecialPowers.wrap(document).clearUserGestureActivation(); 93 94 // load cross-origin test page on iframe. 95 frames[0].frameElement.src = "https://example.com/tests/dom/base/test/useractivation/file_iframe_user_activated.html"; 96 await waitForEvent(window, "message", (event) => { 97 if (event.data === "done") { 98 ok(SpecialPowers.wrap(document).hasValidTransientUserGestureActivation, 99 "check has-valid-transient-user-activation on the top-level document"); 100 ok(!SpecialPowers.wrap(frames[1].document).hasValidTransientUserGestureActivation, 101 "check has-valid-transient-user-activation on the second iframe"); 102 } else { 103 ok(false, "receive unexpected message: " + event.data); 104 } 105 }); 106 107 // hasValidTransientUserGestureActivation should return false after timeout. 108 await new Promise((aResolve) => { 109 setTimeout(() => { 110 ok(!SpecialPowers.wrap(document).hasValidTransientUserGestureActivation, 111 "check has-valid-transient-user-activation on the top-level document"); 112 ok(!SpecialPowers.wrap(frames[1].document).hasValidTransientUserGestureActivation, 113 "check has-valid-transient-user-activation on the second iframe"); 114 aResolve(); 115 }, timeout); 116 }); 117 }); 118 119 add_task(async function propagateToSameOriginConnectedSubframe() { 120 // Reset the activation flag. 121 SpecialPowers.wrap(document).clearUserGestureActivation(); 122 123 // load cross-origin test page on iframe. 124 iframe0.src = "https://example.com/tests/dom/base/test/useractivation/file_iframe_check_user_activation.html"; 125 await waitForEvent(window, "message", (event) => { 126 if (event.data !== "loaded") { 127 ok(false, "receive unexpected message: " + event.data); 128 } 129 }); 130 131 // Trigger user activation on top-level document. 132 SpecialPowers.wrap(document).notifyUserGestureActivation(); 133 ok(SpecialPowers.wrap(document).hasValidTransientUserGestureActivation, 134 "check has-valid-transient-user-activation on the top-level document"); 135 ok(SpecialPowers.wrap(iframe1.contentDocument).hasValidTransientUserGestureActivation, 136 "check has-valid-transient-user-activation on the second iframe"); 137 138 iframe0.contentWindow.postMessage("get", "*"); 139 await waitForEvent(window, "message", (event) => { 140 if (typeof event.data === "object") { 141 ok(!event.data.isActivated, "check has-valid-transient-user-activation on the first iframe"); 142 } else { 143 ok(false, "receive unexpected message: " + event.data); 144 } 145 }); 146 }); 147 148 add_task(async function endTests() { 149 // Reset the activation flag in order not to interfere following test in the 150 // verify mode which would run the test using same document couple times. 151 SpecialPowers.wrap(document).clearUserGestureActivation(); 152 }); 153 154 </script> 155 </body>