Backed out changeset 9d8b4c0b99ed (bug 1945683) for causing btime failures. CLOSED...
[gecko.git] / dom / tests / browser / browser_pointerlock_warning.js
blobf16778cf1fc4a8767907352d2b83145f684b8f9f
1 /**
2 * Any copyright is dedicated to the Public Domain.
3 * http://creativecommons.org/publicdomain/zero/1.0/
4 */
6 "use strict";
8 const BODY_URL =
9 "<body onpointerdown='this.requestPointerLock()' style='width: 100px; height: 100px;'></body>";
11 const TEST_URL = "data:text/html," + BODY_URL;
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>';
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.`
25 });
28 async function waitForWarningState(aWarningElement, aExpectedState) {
29 await BrowserTestUtils.waitForAttribute(aExpectedState, aWarningElement, "");
30 checkWarningState(
31 aWarningElement,
32 aExpectedState,
33 `Wait for ${aExpectedState} state`
37 // Make sure the pointerlock warning is shown and exited with the escape key
38 add_task(async function show_pointerlock_warning_escape() {
39 let urls = [TEST_URL, FRAME_TEST_URL];
40 for (let url of urls) {
41 info("Pointerlock warning test for " + url);
43 let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, url);
45 let warning = document.getElementById("pointerlock-warning");
46 let warningShownPromise = waitForWarningState(warning, "onscreen");
48 let expectedWarningText;
50 let bc = tab.linkedBrowser.browsingContext;
51 if (bc.children.length) {
52 // use the subframe if it exists
53 bc = bc.children[0];
54 expectedWarningText = "example.org";
55 } else {
56 expectedWarningText = "This document";
58 expectedWarningText +=
59 " has control of your pointer. Press Esc to take back control.";
61 await BrowserTestUtils.synthesizeMouse("body", 4, 4, {}, bc);
63 await warningShownPromise;
65 ok(true, "Pointerlock warning shown");
67 let warningHiddenPromise = waitForWarningState(warning, "hidden");
69 await BrowserTestUtils.waitForCondition(
70 () => warning.innerText == expectedWarningText,
71 "Warning text"
74 EventUtils.synthesizeKey("KEY_Escape");
75 await warningHiddenPromise;
77 ok(true, "Pointerlock warning hidden");
79 // Pointerlock should be released after escape is pressed.
80 await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
81 Assert.equal(content.document.pointerLockElement, null);
82 });
84 await BrowserTestUtils.removeTab(tab);
86 });
89 // XXX Bug 1580961 - this part of the test is disabled.
91 // Make sure the pointerlock warning is shown, but this time escape is not pressed until after the
92 // notification is closed via the timeout.
93 add_task(async function show_pointerlock_warning_timeout() {
94 let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_URL);
96 let warning = document.getElementById("pointerlock-warning");
97 let warningShownPromise = BrowserTestUtils.waitForAttribute(
98 "onscreen",
99 warning,
100 "true"
102 let warningHiddenPromise = BrowserTestUtils.waitForAttribute(
103 "hidden",
104 warning,
105 "true"
107 await BrowserTestUtils.synthesizeMouse("body", 4, 4, {}, tab.linkedBrowser);
109 await warningShownPromise;
110 ok(true, "Pointerlock warning shown");
111 await warningHiddenPromise;
113 // The warning closes after a few seconds, but this does not exit pointerlock mode.
114 await SpecialPowers.spawn(tab.linkedBrowser, [], async function() {
115 Assert.equal(content.document.pointerLockElement, content.document.body);
118 EventUtils.synthesizeKey("KEY_Escape");
120 ok(true, "Pointerlock warning hidden");
122 // Pointerlock should now be released.
123 await SpecialPowers.spawn(tab.linkedBrowser, [], async function() {
124 Assert.equal(content.document.pointerLockElement, null);
127 await BrowserTestUtils.removeTab(tab);