summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/fullscreen/browser_fullscreen_warning.js
blob: b8bab5f90ccd8ca4289fd32db480dfabe677156f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

function checkWarningState(aWarningElement, aExpectedState, aMsg) {
  ["hidden", "ontop", "onscreen"].forEach(state => {
    is(
      aWarningElement.hasAttribute(state),
      state == aExpectedState,
      `${aMsg} - check ${state} attribute.`
    );
  });
}

async function waitForWarningState(aWarningElement, aExpectedState) {
  await BrowserTestUtils.waitForAttribute(aExpectedState, aWarningElement, "");
  checkWarningState(
    aWarningElement,
    aExpectedState,
    `Wait for ${aExpectedState} state`
  );
}

add_setup(async function init() {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["full-screen-api.enabled", true],
      ["full-screen-api.allow-trusted-requests-only", false],
    ],
  });
});

add_task(async function test_fullscreen_display_none() {
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: `data:text/html,
      <html>
        <head>
          <meta charset="utf-8"/>
          <title>Fullscreen Test</title>
        </head>
        <body id="body">
        <iframe
         src="https://example.org/browser/browser/base/content/test/fullscreen/fullscreen.html"
         hidden
         allowfullscreen></iframe>
        </body>
      </html>`,
    },
    async function (browser) {
      let warning = document.getElementById("fullscreen-warning");
      checkWarningState(
        warning,
        "hidden",
        "Should not show full screen warning initially"
      );

      let warningShownPromise = waitForWarningState(warning, "onscreen");
      // Enter fullscreen
      await SpecialPowers.spawn(browser, [], async () => {
        let frame = content.document.querySelector("iframe");
        frame.focus();
        await SpecialPowers.spawn(frame, [], () => {
          content.document.getElementById("request").click();
        });
      });
      await warningShownPromise;
      ok(true, "Fullscreen warning shown");
      // Exit fullscreen
      let exitFullscreenPromise = BrowserTestUtils.waitForEvent(
        document,
        "fullscreenchange",
        false,
        () => !document.fullscreenElement
      );
      document.getElementById("fullscreen-exit-button").click();
      await exitFullscreenPromise;

      checkWarningState(
        warning,
        "hidden",
        "Should hide fullscreen warning after exiting fullscreen"
      );
    }
  );
});

add_task(async function test_fullscreen_pointerlock_conflict() {
  await BrowserTestUtils.withNewTab("https://example.com", async browser => {
    let fsWarning = document.getElementById("fullscreen-warning");
    let plWarning = document.getElementById("pointerlock-warning");

    checkWarningState(
      fsWarning,
      "hidden",
      "Should not show full screen warning initially"
    );
    checkWarningState(
      plWarning,
      "hidden",
      "Should not show pointer lock warning initially"
    );

    let fsWarningShownPromise = waitForWarningState(fsWarning, "onscreen");
    info("Entering full screen and pointer lock.");
    await SpecialPowers.spawn(browser, [], async () => {
      await content.document.body.requestFullscreen();
      await content.document.body.requestPointerLock();
    });

    await fsWarningShownPromise;
    checkWarningState(
      plWarning,
      "hidden",
      "Should not show pointer lock warning"
    );

    info("Exiting pointerlock");
    await SpecialPowers.spawn(browser, [], async () => {
      await content.document.exitPointerLock();
    });

    checkWarningState(
      fsWarning,
      "onscreen",
      "Should still show full screen warning"
    );
    checkWarningState(
      plWarning,
      "hidden",
      "Should not show pointer lock warning"
    );

    // Cleanup
    info("Exiting fullscreen");
    await document.exitFullscreen();
  });
});

// https://bugzilla.mozilla.org/show_bug.cgi?id=1821884
add_task(async function test_reshow_fullscreen_notification() {
  await BrowserTestUtils.withNewTab("https://example.com", async browser => {
    let newWin = await BrowserTestUtils.openNewBrowserWindow();
    let fsWarning = document.getElementById("fullscreen-warning");

    info("Entering full screen and wait for the fullscreen warning to appear.");
    await SimpleTest.promiseFocus(window);
    await Promise.all([
      waitForWarningState(fsWarning, "onscreen"),
      BrowserTestUtils.waitForEvent(fsWarning, "transitionend"),
      SpecialPowers.spawn(browser, [], async () => {
        content.document.body.requestFullscreen();
      }),
    ]);

    info(
      "Switch focus away from the fullscreen window, the fullscreen warning should still hide automatically."
    );
    await Promise.all([
      waitForWarningState(fsWarning, "hidden"),
      SimpleTest.promiseFocus(newWin),
    ]);

    info(
      "Switch focus back to the fullscreen window, the fullscreen warning should show again."
    );
    await Promise.all([
      waitForWarningState(fsWarning, "onscreen"),
      SimpleTest.promiseFocus(window),
    ]);

    info("Wait for fullscreen warning timed out.");
    await waitForWarningState(fsWarning, "hidden");

    info("The fullscreen warning should not show again.");
    await SimpleTest.promiseFocus(newWin);
    await SimpleTest.promiseFocus(window);
    await new Promise(resolve => {
      requestAnimationFrame(() => {
        requestAnimationFrame(resolve);
      });
    });
    checkWarningState(
      fsWarning,
      "hidden",
      "The fullscreen warning should not show."
    );

    info("Close new browser window.");
    await BrowserTestUtils.closeWindow(newWin);

    info("Exit fullscreen.");
    await document.exitFullscreen();
  });
});

add_task(async function test_fullscreen_reappear() {
  await BrowserTestUtils.withNewTab("https://example.com", async browser => {
    let fsWarning = document.getElementById("fullscreen-warning");

    info("Entering full screen and wait for the fullscreen warning to appear.");
    await Promise.all([
      waitForWarningState(fsWarning, "onscreen"),
      SpecialPowers.spawn(browser, [], async () => {
        content.document.body.requestFullscreen();
      }),
    ]);

    info("Wait for fullscreen warning timed out.");
    await waitForWarningState(fsWarning, "hidden");

    info("Move mouse to the top of screen.");
    await Promise.all([
      waitForWarningState(fsWarning, "ontop"),
      EventUtils.synthesizeMouse(document.documentElement, 100, 0, {
        type: "mousemove",
      }),
    ]);

    info("Wait for fullscreen warning timed out again.");
    await waitForWarningState(fsWarning, "hidden");

    info("Exit fullscreen.");
    await document.exitFullscreen();
  });
});

// https://bugzilla.mozilla.org/show_bug.cgi?id=1847901
add_task(async function test_fullscreen_warning_disabled() {
  // Disable fullscreen warning
  await SpecialPowers.pushPrefEnv({
    set: [["full-screen-api.warning.timeout", 0]],
  });

  await BrowserTestUtils.withNewTab("https://example.com", async browser => {
    let newWin = await BrowserTestUtils.openNewBrowserWindow();
    let fsWarning = document.getElementById("fullscreen-warning");
    let mut = new MutationObserver(mutations => {
      ok(false, `${mutations[0].attributeName} attribute should not change`);
    });
    mut.observe(fsWarning, {
      attributeFilter: ["hidden", "onscreen", "ontop"],
    });

    info("Entering full screen.");
    await SimpleTest.promiseFocus(window);
    await SpecialPowers.spawn(browser, [], async () => {
      return content.document.body.requestFullscreen();
    });
    // Wait a bit to ensure no state change.
    await new Promise(resolve => {
      requestAnimationFrame(() => {
        requestAnimationFrame(resolve);
      });
    });

    info("The fullscreen warning should still not show after switching focus.");
    await SimpleTest.promiseFocus(newWin);
    await SimpleTest.promiseFocus(window);
    // Wait a bit to ensure no state change.
    await new Promise(resolve => {
      requestAnimationFrame(() => {
        requestAnimationFrame(resolve);
      });
    });

    mut.disconnect();

    info("Close new browser window.");
    await BrowserTestUtils.closeWindow(newWin);

    info("Exit fullscreen.");
    await document.exitFullscreen();
  });

  // Revert the setting to avoid affecting subsequent tests.
  await SpecialPowers.popPrefEnv();
});