test_pointerlock_focus.html (3259B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Bug 1646493 - test_pointerlock_focus.html</title> 6 <script src="/tests/SimpleTest/SimpleTest.js"></script> 7 <script src="/tests/SimpleTest/EventUtils.js"></script> 8 <link rel="stylesheet" href="/tests/SimpleTest/test.css"> 9 <style> 10 #target { 11 width: 50px; 12 height: 50px; 13 background-color: green; 14 } 15 16 iframe { 17 width: 250px; 18 height: 50px; 19 } 20 </style> 21 </head> 22 <body style="width: 100vw; height: 100vh; margin: 0;"> 23 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1646493">Mozilla Bug 1646493</a><br> 24 <div id="target"></div> 25 <input id="input"><br> 26 <iframe id="iframe" src="https://example.com/tests/dom/tests/mochitest/pointerlock/iframe_differentDOM.html"></iframe> 27 <script> 28 29 function waitForEventOnce(aTarget, aEvent) { 30 return new Promise(aResolve => { 31 aTarget.addEventListener(aEvent, aResolve, { once: true }); 32 }); 33 } 34 35 function unexpectedEvent(aEvent) { 36 ok(false, `Unexpected ${aEvent.type} event`); 37 } 38 39 async function requestPointerLock(aElement) { 40 let doc = aElement.ownerDocument; 41 doc.addEventListener("pointerlockerror", unexpectedEvent); 42 43 SpecialPowers.wrap(doc).notifyUserGestureActivation(); 44 aElement.requestPointerLock(); 45 await waitForEventOnce(doc, "pointerlockchange"); 46 47 is(doc.pointerLockElement, aElement, "target pointer locked"); 48 doc.removeEventListener("pointerlockerror", unexpectedEvent); 49 } 50 51 async function exitPointerLock(aDocument) { 52 if (aDocument.pointerLockElement) { 53 aDocument.exitPointerLock(); 54 await waitForEventOnce(aDocument, "pointerlockchange"); 55 } 56 is(aDocument.pointerLockElement, null, "pointer unlocked"); 57 } 58 59 let target = document.getElementById("target"); 60 let input = document.getElementById("input"); 61 62 add_task(async function init() { 63 await SimpleTest.promiseFocus(); 64 }); 65 66 add_task(async function focusMovesIntoXoriginIframe() { 67 let iframe = document.getElementById("iframe"); 68 69 input.focus(); 70 is(document.activeElement, input, "focus input"); 71 72 // Request pointer lock on parent window 73 await requestPointerLock(target); 74 75 // Move focus into child window with different origin 76 let win = iframe.contentWindow; 77 document.addEventListener("pointerlockchange", unexpectedEvent); 78 synthesizeKey("KEY_Tab"); 79 await SpecialPowers.spawn(win, [], async () => { 80 if (!content.document.hasFocus()) { 81 await new Promise((resolve) => { 82 content.document.addEventListener('focus', resolve, { once: true }); 83 }); 84 } 85 }); 86 is(document.pointerLockElement, target, "target pointer locked"); 87 document.removeEventListener("pointerlockchange", unexpectedEvent); 88 89 // Exit pointer lock 90 await exitPointerLock(document); 91 }); 92 93 add_task(async function focusMovesToAnotherTab() { 94 input.focus(); 95 is(document.activeElement, input, "focus input"); 96 97 // Request pointer lock on parent window 98 await requestPointerLock(target); 99 100 // Move focus to another tab 101 let promise = waitForEventOnce(document, "pointerlockchange"); 102 let win = window.open('iframe_differentDOM.html'); 103 await promise; 104 is(document.pointerLockElement, null, "pointer unlocked"); 105 106 win.close(); 107 await exitPointerLock(document); 108 }); 109 </script> 110 </body> 111 </html>