summaryrefslogtreecommitdiffstats
path: root/toolkit/components/pictureinpicture/tests/browser_occluded_window.js
blob: 6ee66f2ade080b4a1fe9a19bc32977f48c77b3b4 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/**
 * Tests that the forceAppWindowActive flag is correctly set whenever a PiP window is opened
 * and closed across multiple tabs on the same browser window.
 */
add_task(async function forceActiveMultiPiPTabs() {
  let videoID = "with-controls";

  await BrowserTestUtils.withNewTab(
    {
      url: TEST_PAGE,
      gBrowser,
    },
    async browser => {
      await ensureVideosReady(browser);

      let bc = browser.ownerGlobal.browsingContext;
      info("is window active: " + bc.isActive);

      info("Opening new tab");
      let newTab = await BrowserTestUtils.openNewForegroundTab(
        gBrowser,
        TEST_PAGE
      );
      let newTabBrowser = newTab.linkedBrowser;
      await ensureVideosReady(newTabBrowser);

      ok(!bc.forceAppWindowActive, "Forced window active should be false");
      info("is window active: " + bc.isActive);

      info("Now opening PiP windows");
      let pipWin = await triggerPictureInPicture(browser, videoID);
      ok(pipWin, "Got Picture-in-Picture window.");

      ok(
        bc.forceAppWindowActive,
        "Forced window active should be true since PiP is open"
      );
      info("is window active: " + bc.isActive);

      let newTabPiPWin = await triggerPictureInPicture(newTabBrowser, videoID);
      ok(newTabPiPWin, "Got Picture-in-Picture window in new tab");
      ok(
        bc.forceAppWindowActive,
        "Force window active should still be true after opening a new PiP window in new tab"
      );
      info("is window active: " + bc.isActive);

      let pipClosedNewTab = BrowserTestUtils.domWindowClosed(newTabPiPWin);
      let pipUnloadedNewTab = BrowserTestUtils.waitForEvent(
        newTabPiPWin,
        "unload"
      );
      let closeButtonNewTab = newTabPiPWin.document.getElementById("close");
      info("Selecting close button");
      EventUtils.synthesizeMouseAtCenter(closeButtonNewTab, {}, newTabPiPWin);
      info("Waiting for PiP window to close");
      await pipUnloadedNewTab;
      await pipClosedNewTab;

      ok(
        bc.forceAppWindowActive,
        "Force window active should still be true after removing new tab's PiP window"
      );

      info("is window active: " + bc.isActive);

      let pipClosed = BrowserTestUtils.domWindowClosed(pipWin);
      let pipUnloaded = BrowserTestUtils.waitForEvent(pipWin, "unload");
      let closeButton = pipWin.document.getElementById("close");
      info("Selecting close button");
      EventUtils.synthesizeMouseAtCenter(closeButton, {}, pipWin);
      info("Waiting for PiP window to close");
      await pipUnloaded;
      await pipClosed;

      ok(
        !bc.forceAppWindowActive,
        "Force window active should now be false after removing the last PiP window"
      );

      info("is window active: " + bc.isActive);

      await BrowserTestUtils.removeTab(newTab);
    }
  );
});

/**
 * Tests that the forceAppWindowActive flag is correctly set when a tab with PiP enabled is
 * moved to another window.
 */
add_task(async function forceActiveMovePiPToWindow() {
  let videoID = "with-controls";

  await BrowserTestUtils.withNewTab(
    {
      url: TEST_PAGE,
      gBrowser,
    },
    async browser => {
      info("Opening first tab");

      await ensureVideosReady(browser);

      let tab = gBrowser.getTabForBrowser(browser);
      let bc = browser.ownerGlobal.browsingContext;

      info("is window active: " + bc.isActive);

      ok(!bc.forceAppWindowActive, "Forced window active should be false");
      info("is window active: " + bc.isActive);

      info("Now opening PiP windows");
      let pipWin = await triggerPictureInPicture(browser, videoID);
      ok(pipWin, "Got Picture-in-Picture window in first tab.");

      ok(
        bc.forceAppWindowActive,
        "Forced window active should be true since PiP is open"
      );
      info("is window active: " + bc.isActive);

      let swapDocShellsPromise = BrowserTestUtils.waitForEvent(
        browser,
        "SwapDocShells"
      );
      let tabClosePromise = BrowserTestUtils.waitForEvent(tab, "TabClose");
      let tabSwapPiPPromise = BrowserTestUtils.waitForEvent(
        tab,
        "TabSwapPictureInPicture"
      );
      info("Replacing tab with window");
      let newWindow = gBrowser.replaceTabWithWindow(tab);
      let newWinLoadedPromise = BrowserTestUtils.waitForEvent(
        newWindow,
        "load"
      );

      info("Waiting for new window to initialize after swap");
      await Promise.all([
        tabSwapPiPPromise,
        swapDocShellsPromise,
        tabClosePromise,
        newWinLoadedPromise,
      ]);

      let newWindowBC = newWindow.browsingContext;
      tab = newWindow.gBrowser.selectedTab;

      ok(
        !bc.forceAppWindowActive,
        "Force window active should no longer be true after moving the previous tab to a new window"
      );
      info("is window active: " + bc.isActive);
      ok(
        newWindowBC.forceAppWindowActive,
        "Force window active should be true for new window since PiP is open"
      );
      info("is secondary window active: " + newWindowBC.isActive);

      let pipClosed = BrowserTestUtils.domWindowClosed(pipWin);
      let pipUnloaded = BrowserTestUtils.waitForEvent(pipWin, "unload");
      let closeButton = pipWin.document.getElementById("close");
      info("Selecting close button");
      EventUtils.synthesizeMouseAtCenter(closeButton, {}, pipWin);
      info("Waiting for PiP window to close");
      await pipUnloaded;
      await pipClosed;

      ok(
        !newWindowBC.forceAppWindowActive,
        "Force window active should now be false for new window after removing the last PiP window"
      );
      info("is secondary window active: " + newWindowBC.isActive);

      await BrowserTestUtils.removeTab(tab);
    }
  );
});

/**
 * Tests that the forceAppWindowActive flag is correctly set when multiple PiP
 * windows are created for a single PiP window.
 */
add_task(async function forceActiveMultiPiPSamePage() {
  let videoID1 = "with-controls";
  let videoID2 = "no-controls";

  await BrowserTestUtils.withNewTab(
    {
      url: TEST_PAGE,
      gBrowser,
    },
    async browser => {
      await ensureVideosReady(browser);
      let bc = browser.ownerGlobal.browsingContext;

      ok(
        !bc.forceAppWindowActive,
        "Forced window active should be false at the start of the test"
      );
      info("is window active: " + bc.isActive);

      let pipWin1 = await triggerPictureInPicture(browser, videoID1);
      ok(pipWin1, "Got Picture-in-Picture window 1.");

      ok(
        bc.forceAppWindowActive,
        "Forced window active should be true since PiP is open"
      );
      info("is window active: " + bc.isActive);

      let pipWin2 = await triggerPictureInPicture(browser, videoID2);
      ok(pipWin2, "Got Picture-in-Picture window 2.");

      ok(
        bc.forceAppWindowActive,
        "Forced window active should be true after opening another PiP window on the same page"
      );
      info("is window active: " + bc.isActive);

      let pipClosed1 = BrowserTestUtils.domWindowClosed(pipWin1);
      let pipUnloaded1 = BrowserTestUtils.waitForEvent(pipWin1, "unload");
      let closeButton1 = pipWin1.document.getElementById("close");
      info("Selecting close button");
      EventUtils.synthesizeMouseAtCenter(closeButton1, {}, pipWin1);
      info("Waiting for PiP window to close");
      await pipUnloaded1;
      await pipClosed1;

      ok(
        bc.forceAppWindowActive,
        "Force window active should still be true after removing PiP window 1"
      );
      info("is window active: " + bc.isActive);

      let pipClosed2 = BrowserTestUtils.domWindowClosed(pipWin2);
      let pipUnloaded2 = BrowserTestUtils.waitForEvent(pipWin2, "unload");
      let closeButton2 = pipWin2.document.getElementById("close");
      info("Selecting close button");
      EventUtils.synthesizeMouseAtCenter(closeButton2, {}, pipWin2);
      info("Waiting for PiP window to close");
      await pipUnloaded2;
      await pipClosed2;

      ok(
        !bc.forceAppWindowActive,
        "Force window active should now be false after removing PiP window 2"
      );
      info("is window active: " + bc.isActive);
    }
  );
});