browser_pointerlock_warning.js (4031B)
1 /** 2 * Any copyright is dedicated to the Public Domain. 3 * http://creativecommons.org/publicdomain/zero/1.0/ 4 */ 5 6 "use strict"; 7 8 const BODY_URL = 9 "<body onpointerdown='this.requestPointerLock()' style='width: 100px; height: 100px;'></body>"; 10 11 const TEST_URL = "data:text/html," + BODY_URL; 12 13 const FRAME_TEST_URL = 14 'data:text/html,<body><iframe src="http://example.org/document-builder.sjs?html=' + 15 encodeURI(BODY_URL) + 16 '"></iframe></body>'; 17 18 function checkWarningState(aWarningElement, aExpectedState, aMsg) { 19 ["hidden", "ontop", "onscreen"].forEach(state => { 20 is( 21 aWarningElement.hasAttribute(state), 22 state == aExpectedState, 23 `${aMsg} - check ${state} attribute.` 24 ); 25 }); 26 } 27 28 async function waitForWarningState(aWarningElement, aExpectedState) { 29 await BrowserTestUtils.waitForAttribute(aExpectedState, aWarningElement, ""); 30 checkWarningState( 31 aWarningElement, 32 aExpectedState, 33 `Wait for ${aExpectedState} state` 34 ); 35 } 36 37 add_setup(async function () { 38 await SpecialPowers.pushPrefEnv({ 39 set: [["test.wait300msAfterTabSwitch", true]], 40 }); 41 }); 42 43 // Make sure the pointerlock warning is shown and exited with the escape key 44 add_task(async function show_pointerlock_warning_escape() { 45 let urls = [TEST_URL, FRAME_TEST_URL]; 46 for (let url of urls) { 47 info("Pointerlock warning test for " + url); 48 49 let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, url); 50 51 let warning = document.getElementById("pointerlock-warning"); 52 let warningShownPromise = waitForWarningState(warning, "onscreen"); 53 54 let expectedWarningText; 55 56 let bc = tab.linkedBrowser.browsingContext; 57 if (bc.children.length) { 58 // use the subframe if it exists 59 bc = bc.children[0]; 60 expectedWarningText = "example.org"; 61 } else { 62 expectedWarningText = "This document"; 63 } 64 expectedWarningText += 65 " has control of your pointer. Press Esc to take back control."; 66 67 await BrowserTestUtils.synthesizeMouse("body", 4, 4, {}, bc); 68 69 await warningShownPromise; 70 71 ok(true, "Pointerlock warning shown"); 72 73 let warningHiddenPromise = waitForWarningState(warning, "hidden"); 74 75 await BrowserTestUtils.waitForCondition( 76 () => warning.innerText == expectedWarningText, 77 "Warning text" 78 ); 79 80 EventUtils.synthesizeKey("KEY_Escape"); 81 await warningHiddenPromise; 82 83 ok(true, "Pointerlock warning hidden"); 84 85 // Pointerlock should be released after escape is pressed. 86 await SpecialPowers.spawn(tab.linkedBrowser, [], async function () { 87 Assert.equal(content.document.pointerLockElement, null); 88 }); 89 90 await BrowserTestUtils.removeTab(tab); 91 } 92 }); 93 94 /* 95 // XXX Bug 1580961 - this part of the test is disabled. 96 // 97 // Make sure the pointerlock warning is shown, but this time escape is not pressed until after the 98 // notification is closed via the timeout. 99 add_task(async function show_pointerlock_warning_timeout() { 100 let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_URL); 101 102 let warning = document.getElementById("pointerlock-warning"); 103 let warningShownPromise = BrowserTestUtils.waitForAttribute( 104 "onscreen", 105 warning, 106 "true" 107 ); 108 let warningHiddenPromise = BrowserTestUtils.waitForAttribute( 109 "hidden", 110 warning, 111 "true" 112 ); 113 await BrowserTestUtils.synthesizeMouse("body", 4, 4, {}, tab.linkedBrowser); 114 115 await warningShownPromise; 116 ok(true, "Pointerlock warning shown"); 117 await warningHiddenPromise; 118 119 // The warning closes after a few seconds, but this does not exit pointerlock mode. 120 await SpecialPowers.spawn(tab.linkedBrowser, [], async function() { 121 Assert.equal(content.document.pointerLockElement, content.document.body); 122 }); 123 124 EventUtils.synthesizeKey("KEY_Escape"); 125 126 ok(true, "Pointerlock warning hidden"); 127 128 // Pointerlock should now be released. 129 await SpecialPowers.spawn(tab.linkedBrowser, [], async function() { 130 Assert.equal(content.document.pointerLockElement, null); 131 }); 132 133 await BrowserTestUtils.removeTab(tab); 134 }); 135 */