Source code
Revision control
Copy as Markdown
Other Tools
Test Info: Errors
- This test failed 1 times in the preceding 30 days. quicksearch this test
- Manifest: dom/base/test/useractivation/mochitest.toml
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test for triggering popup by mouse events</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="/tests/SimpleTest/EventUtils.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<div id="target" style="width: 50px; height: 50px; background: green"></div>
<script>
var experimental = false;
function sendMouseEvent(element, eventName, button, listenEventName, handler) {
let needToCheckHandler = false;
let handlerIsCalled = false;
if (listenEventName && handler) {
needToCheckHandler = true;
element.addEventListener(listenEventName, (e) => {
handler(e);
handlerIsCalled = true;
}, {once: true});
}
synthesizeMouseAtCenter(element, {type: eventName, button});
if (needToCheckHandler) {
ok(handlerIsCalled, "Handler should be called");
}
}
function checkAllowOpenPopup(e) {
let w = window.open("about:blank");
ok(w, `Should allow popup in the ${e.type} listener with button=${e.button}`);
if (w) {
w.close();
}
}
function checkBlockOpenPopup(e) {
let w = window.open("about:blank");
ok(!w, `Should block popup in the ${e.type} listener with button=${e.button}`);
if (w) {
w.close();
}
}
add_setup(async function() {
await SpecialPowers.pushPrefEnv({
set: [
// Enable the popup blocker
["dom.disable_open_during_load", true],
],
});
await new Promise(resolve => SimpleTest.waitForFocus(resolve));
});
const LEFT_BUTTON = 0;
const MIDDLE_BUTTON = 1;
const RIGHT_BUTTON = 2;
let target = document.getElementById("target");
async function beginTest() {
if (!experimental) {
info("NOT EXPERIMENTAL");
await SpecialPowers.pushPrefEnv({
set: [
["dom.popup.experimental", false]
],
});
} else {
info("EXPERIMENTAL");
await SpecialPowers.pushPrefEnv({
set: [
["dom.popup.experimental", true],
],
});
}
}
async function endTest() {
await SpecialPowers.popPrefEnv();
}
async function testMouseDownUpMove() {
await beginTest();
// Left button
sendMouseEvent(target, "mousedown", LEFT_BUTTON, "mousedown", checkAllowOpenPopup);
sendMouseEvent(target, "mousemove", LEFT_BUTTON, "mousemove", checkBlockOpenPopup);
sendMouseEvent(target, "mouseup", LEFT_BUTTON, "mouseup", checkBlockOpenPopup);
// Middle button
sendMouseEvent(target, "mousedown", MIDDLE_BUTTON, "mousedown", checkAllowOpenPopup);
sendMouseEvent(target, "mousemove", MIDDLE_BUTTON, "mousemove", checkBlockOpenPopup);
sendMouseEvent(target, "mouseup", MIDDLE_BUTTON, "mouseup", checkBlockOpenPopup);
// Right button
sendMouseEvent(target, "mousedown", RIGHT_BUTTON, "mousedown", experimental ?
checkAllowOpenPopup :
checkBlockOpenPopup);
sendMouseEvent(target, "mousemove", RIGHT_BUTTON, "mousemove", checkBlockOpenPopup);
sendMouseEvent(target, "mouseup", RIGHT_BUTTON, "mouseup", checkBlockOpenPopup);
await endTest();
}
async function testMouseClick() {
await beginTest();
// Left button
sendMouseEvent(target, "mousedown", LEFT_BUTTON);
sendMouseEvent(target, "mouseup", LEFT_BUTTON, "click", checkAllowOpenPopup);
await endTest();
}
async function testMouseAuxclick() {
await beginTest();
// Middle button
sendMouseEvent(target, "mousedown", MIDDLE_BUTTON);
sendMouseEvent(target, "mouseup", MIDDLE_BUTTON, "auxclick", checkAllowOpenPopup);
// Right button
sendMouseEvent(target, "mousedown", RIGHT_BUTTON);
sendMouseEvent(target, "mouseup", RIGHT_BUTTON, "auxclick", checkAllowOpenPopup);
await endTest();
}
add_task(testMouseDownUpMove);
add_task(testMouseClick);
add_task(testMouseAuxclick);
add_task(() => experimental = true);
add_task(testMouseDownUpMove);
add_task(testMouseClick);
add_task(testMouseAuxclick);
</script>
</body>
</html>