test_popup_blocker_async_callback.html (2666B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>Test for triggering popup by mouse events</title> 6 <script src="/tests/SimpleTest/SimpleTest.js"></script> 7 <script src="/tests/SimpleTest/EventUtils.js"></script> 8 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 9 </head> 10 <body> 11 <div id="target" style="width: 50px; height: 50px; background: green"></div> 12 <script> 13 14 SimpleTest.requestFlakyTimeout("Need to test setTimeout"); 15 16 function startTest(aTestAsyncFun, aAllowPopup = true) { 17 return new Promise(aResolve => { 18 let target = document.getElementById("target"); 19 target.addEventListener("click", () => { 20 aTestAsyncFun(() => { 21 let w = window.open("about:blank"); 22 is(!!w, aAllowPopup, `Should ${aAllowPopup ? "allow" : "block"} popup`); 23 if (w) { 24 w.close(); 25 } 26 aResolve(); 27 }); 28 }, {once: true}); 29 synthesizeMouseAtCenter(target, {type: "mousedown"}); 30 synthesizeMouseAtCenter(target, {type: "mouseup"}); 31 }); 32 } 33 34 add_setup(async function() { 35 await SpecialPowers.pushPrefEnv({"set": [ 36 ["dom.disable_open_during_load", true], 37 ["dom.serviceWorkers.enabled", true], 38 ["dom.serviceWorkers.testing.enabled", true], 39 ]}); 40 }); 41 42 [ 43 // setTimeout 44 function testSetTimout(aCallback) { 45 setTimeout(aCallback, 500); 46 }, 47 // fetch 48 function testFetch(aCallback) { 49 fetch("../dummy.html").then(aCallback); 50 }, 51 // requestStorageAccess 52 function testRequestStorageAccess(aCallback) { 53 SpecialPowers.pushPermissions([ 54 { 55 type: "storageAccessAPI", 56 allow: true, 57 context: window.document, 58 }, 59 ]).then( 60 () => document.requestStorageAccess().then(aCallback) 61 ); 62 }, 63 // serviceWorker.getRegistration 64 function testGetServiceWorkerRegistration(aCallback) { 65 navigator.serviceWorker.getRegistration("/app").then(aCallback); 66 }, 67 ].forEach(testAsyncFun => { 68 add_task(async function() { 69 info(`start ${testAsyncFun.name}`); 70 SpecialPowers.wrap(document).clearUserGestureActivation(); 71 await startTest(testAsyncFun); 72 await new Promise(aResolve => SimpleTest.executeSoon(aResolve)); 73 }); 74 }); 75 76 // Test popup should be blocked if user transient is timeout 77 add_task(async function timeout() { 78 info(`start user transient timeout`); 79 await SpecialPowers.pushPrefEnv({"set": [ 80 ["dom.user_activation.transient.timeout", 1000], 81 ]}); 82 SpecialPowers.wrap(document).clearUserGestureActivation(); 83 await startTest((aCallback) => { 84 setTimeout(aCallback, 2000); 85 }, false); 86 await new Promise(aResolve => SimpleTest.executeSoon(aResolve)); 87 }); 88 89 </script> 90 </body> 91 </html>