test_popup_blocker_mouse_event.html (3158B)
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 function sendMouseEvent(element, eventName, button, listenEventName, handler) { 15 let needToCheckHandler = false; 16 let handlerIsCalled = false; 17 if (listenEventName && handler) { 18 needToCheckHandler = true; 19 element.addEventListener(listenEventName, (e) => { 20 handler(e); 21 handlerIsCalled = true; 22 }, {once: true}); 23 } 24 synthesizeMouseAtCenter(element, {type: eventName, button}); 25 if (needToCheckHandler) { 26 ok(handlerIsCalled, "Handler should be called"); 27 } 28 } 29 30 function checkAllowOpenPopup(e) { 31 let w = window.open("about:blank"); 32 ok(w, `Should allow popup in the ${e.type} listener with button=${e.button}`); 33 if (w) { 34 w.close(); 35 } 36 } 37 38 function checkBlockOpenPopup(e) { 39 let w = window.open("about:blank"); 40 ok(!w, `Should block popup in the ${e.type} listener with button=${e.button}`); 41 if (w) { 42 w.close(); 43 } 44 } 45 46 add_setup(async function() { 47 await SpecialPowers.pushPrefEnv({ 48 set: [ 49 // Enable the popup blocker 50 ["dom.disable_open_during_load", true], 51 ], 52 }); 53 54 await new Promise(resolve => SimpleTest.waitForFocus(resolve)); 55 }); 56 57 const LEFT_BUTTON = 0; 58 const MIDDLE_BUTTON = 1; 59 const RIGHT_BUTTON = 2; 60 let target = document.getElementById("target"); 61 62 async function testMouseDownUpMove() { 63 // Left button 64 sendMouseEvent(target, "mousedown", LEFT_BUTTON, "mousedown", checkAllowOpenPopup); 65 sendMouseEvent(target, "mousemove", LEFT_BUTTON, "mousemove", checkBlockOpenPopup); 66 sendMouseEvent(target, "mouseup", LEFT_BUTTON, "mouseup", checkBlockOpenPopup); 67 68 // Middle button 69 sendMouseEvent(target, "mousedown", MIDDLE_BUTTON, "mousedown", checkAllowOpenPopup); 70 sendMouseEvent(target, "mousemove", MIDDLE_BUTTON, "mousemove", checkBlockOpenPopup); 71 sendMouseEvent(target, "mouseup", MIDDLE_BUTTON, "mouseup", checkBlockOpenPopup); 72 73 // Right button 74 sendMouseEvent(target, "mousedown", RIGHT_BUTTON, "mousedown", checkAllowOpenPopup); 75 sendMouseEvent(target, "mousemove", RIGHT_BUTTON, "mousemove", checkBlockOpenPopup); 76 sendMouseEvent(target, "mouseup", RIGHT_BUTTON, "mouseup", checkBlockOpenPopup); 77 } 78 79 async function testMouseClick() { 80 // Left button 81 sendMouseEvent(target, "mousedown", LEFT_BUTTON); 82 sendMouseEvent(target, "mouseup", LEFT_BUTTON, "click", checkAllowOpenPopup); 83 } 84 85 async function testMouseAuxclick() { 86 // Middle button 87 sendMouseEvent(target, "mousedown", MIDDLE_BUTTON); 88 sendMouseEvent(target, "mouseup", MIDDLE_BUTTON, "auxclick", checkAllowOpenPopup); 89 90 // Right button 91 sendMouseEvent(target, "mousedown", RIGHT_BUTTON); 92 sendMouseEvent(target, "mouseup", RIGHT_BUTTON, "auxclick", checkAllowOpenPopup); 93 } 94 95 add_task(testMouseDownUpMove); 96 add_task(testMouseClick); 97 add_task(testMouseAuxclick); 98 99 </script> 100 </body> 101 </html>