test_pointerlock_xorigin_iframe_movementXY.html (3358B)
1 <!DOCTYPE HTML> 2 <html> 3 <!--https://bugzilla.mozilla.org/show_bug.cgi?id=1866173--> 4 <head> 5 <title>Bug 1866173</title> 6 <script src="/tests/SimpleTest/EventUtils.js"></script> 7 <script src="/tests/SimpleTest/SimpleTest.js"></script> 8 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 9 <style> 10 iframe { 11 width: 10px; 12 height: 10px; 13 border: 1px solid blue; 14 display: block; 15 } 16 </style> 17 </head> 18 <body> 19 <a target="_blank"href="https://bugzilla.mozilla.org/show_bug.cgi?id=1866173">Mozilla Bug 1866173</a><br> 20 <iframe src="https://example.com/tests/dom/tests/mochitest/pointerlock/iframe_differentDOM.html"></iframe> 21 22 <pre id="test"> 23 <script type="text/javascript"> 24 25 function waitForMessage(aType) { 26 return new Promise((resolve) => { 27 window.addEventListener("message", function handler(e) { 28 info(`received ${JSON.stringify(e.data)} message`); 29 if (e.data && e.data.type == aType) { 30 window.removeEventListener("message", handler); 31 resolve(e.data); 32 } 33 }); 34 }); 35 } 36 37 async function waitAndTestMouseMoveMessage(aMoveMementX, aMoveMomentY) { 38 let message = await waitForMessage("mousemove"); 39 ok(true, `received mouemove event with movementX=${message.movementX} and movementY=${message.movementY}`); 40 return message; 41 } 42 43 const iframe = document.querySelector("iframe"); 44 45 add_setup(async () => { 46 await SpecialPowers.pushPrefEnv({ set: [["test.events.async.enabled", true]] }); 47 // Setup iframe for testing. 48 await SpecialPowers.spawn(iframe.contentWindow, [], async () => { 49 content.document.body.style = "margin: 0;"; 50 content.document.body.innerHTML = ` 51 <div id="container" style="height: 10px; width: 10px; background-color: #555;"></div> 52 `; 53 content.document.addEventListener("click", () => { 54 content.document.body.requestPointerLock(); 55 }); 56 content.document.addEventListener("pointerlockchange", (e) => { 57 content.parent.postMessage({ 58 type: e.type, 59 }, "*"); 60 }); 61 content.document.addEventListener("mousemove", (e) => { 62 content.parent.postMessage({ 63 type: e.type, 64 screenX: e.screenX, 65 screenY: e.screenY, 66 movementX: e.movementX, 67 movementY: e.movementY, 68 }, "*"); 69 }); 70 }); 71 }); 72 73 /** 74 * Test for Bug 1866173 75 */ 76 add_task(async function test_small_xorigin_iframe() { 77 let iframeCenterWidth = Math.round(iframe.getBoundingClientRect().width / 2); 78 let iframeCenterHeight = Math.round(iframe.getBoundingClientRect().height / 2); 79 // Click iframe to request pointer lock. 80 let promise = Promise.all([ 81 waitForMessage("pointerlockchange"), 82 // We force the mouse to move to the center position when entering pointer 83 // lock, which results in an additional mousemove event, see bug 1254044. 84 waitAndTestMouseMoveMessage(0, 0), 85 ]); 86 synthesizeMouse(iframe, iframeCenterWidth, iframeCenterHeight, {}); 87 await promise; 88 89 const movement = 2; 90 promise = waitAndTestMouseMoveMessage(movement, movement); 91 synthesizeMouse(iframe, iframeCenterWidth + movement, iframeCenterHeight + movement, { type: "mousemove" }); 92 await promise; 93 94 // Exit pointer lock 95 promise = waitForMessage("pointerlockchange"); 96 await SpecialPowers.spawn(iframe.contentWindow, [], async () => { 97 content.document.exitPointerLock(); 98 }); 99 await promise; 100 }); 101 102 </script> 103 </pre> 104 </body> 105 </html>