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

"use strict";

async function createTab() {
  return BrowserTestUtils.openNewForegroundTab({
    gBrowser,
    opening: TEST_PAGE,
    waitForLoad: true,
  });
}

function getTelemetryMaxPipCount(resetMax = false) {
  const scalarData = Services.telemetry.getSnapshotForScalars(
    "main",
    resetMax
  ).parent;
  return scalarData["pictureinpicture.most_concurrent_players"];
}

/**
 * Tests that multiple PiPs can be opened and closed in a single tab
 */
add_task(async () => {
  await BrowserTestUtils.withNewTab(
    {
      url: TEST_PAGE,
      gBrowser,
    },
    async browser => {
      let firstPip = await triggerPictureInPicture(browser, "with-controls");
      ok(firstPip, "Got first PiP window");

      let secondPip = await triggerPictureInPicture(browser, "no-controls");
      ok(secondPip, "Got second PiP window");

      await ensureMessageAndClosePiP(browser, "with-controls", firstPip, false);
      info("First PiP was still open and is now closed");

      await ensureMessageAndClosePiP(browser, "no-controls", secondPip, false);
      info("Second PiP was still open and is now closed");
    }
  );
});

/**
 * Tests that multiple PiPs can be opened and closed across different tabs
 */
add_task(async () => {
  let firstTab = await createTab();
  let secondTab = await createTab();

  gBrowser.selectedTab = firstTab;

  let firstPip = await triggerPictureInPicture(
    firstTab.linkedBrowser,
    "with-controls"
  );
  ok(firstPip, "Got first PiP window");

  gBrowser.selectedTab = secondTab;

  let secondPip = await triggerPictureInPicture(
    secondTab.linkedBrowser,
    "with-controls"
  );
  ok(secondPip, "Got second PiP window");

  await ensureMessageAndClosePiP(
    firstTab.linkedBrowser,
    "with-controls",
    firstPip,
    false
  );
  info("First Picture-in-Picture window was open and is now closed.");

  await ensureMessageAndClosePiP(
    secondTab.linkedBrowser,
    "with-controls",
    secondPip,
    false
  );
  info("Second Picture-in-Picture window was open and is now closed.");

  BrowserTestUtils.removeTab(firstTab);
  BrowserTestUtils.removeTab(secondTab);
});

/**
 * Tests that when a tab is closed; that only PiPs originating from this tab
 * are closed as well
 */
add_task(async () => {
  let firstTab = await createTab();
  let secondTab = await createTab();

  let firstPip = await triggerPictureInPicture(
    firstTab.linkedBrowser,
    "with-controls"
  );
  ok(firstPip, "Got first PiP window");

  let secondPip = await triggerPictureInPicture(
    secondTab.linkedBrowser,
    "with-controls"
  );
  ok(secondPip, "Got second PiP window");

  let firstClosed = BrowserTestUtils.domWindowClosed(firstPip);
  BrowserTestUtils.removeTab(firstTab);
  await firstClosed;
  info("First PiP closed after closing the first tab");

  await assertVideoIsBeingCloned(secondTab.linkedBrowser, "#with-controls");
  info("Second PiP is still open after first tab close");

  let secondClosed = BrowserTestUtils.domWindowClosed(secondPip);
  BrowserTestUtils.removeTab(secondTab);
  await secondClosed;
  info("Second PiP closed after closing the second tab");
});

/**
 * Check that correct number of pip players are recorded for Telemetry
 * tracking
 */
add_task(async () => {
  // run this to flush recorded values from previous tests
  getTelemetryMaxPipCount(true);

  let firstTab = await createTab();
  let secondTab = await createTab();

  gBrowser.selectedTab = firstTab;

  let firstPip = await triggerPictureInPicture(
    firstTab.linkedBrowser,
    "with-controls"
  );
  ok(firstPip, "Got first PiP window");

  Assert.equal(
    getTelemetryMaxPipCount(true),
    1,
    "There should only be 1 PiP window"
  );

  let secondPip = await triggerPictureInPicture(
    firstTab.linkedBrowser,
    "no-controls"
  );
  ok(secondPip, "Got second PiP window");

  Assert.equal(
    getTelemetryMaxPipCount(true),
    2,
    "There should be 2 PiP windows"
  );

  await ensureMessageAndClosePiP(
    firstTab.linkedBrowser,
    "no-controls",
    secondPip,
    false
  );
  info("Second PiP was open and is now closed");

  gBrowser.selectedTab = secondTab;

  let thirdPip = await triggerPictureInPicture(
    secondTab.linkedBrowser,
    "with-controls"
  );
  ok(thirdPip, "Got third PiP window");

  let fourthPip = await triggerPictureInPicture(
    secondTab.linkedBrowser,
    "no-controls"
  );
  ok(fourthPip, "Got fourth PiP window");

  Assert.equal(
    getTelemetryMaxPipCount(false),
    3,
    "There should now be 3 PiP windows"
  );

  gBrowser.selectedTab = firstTab;

  await ensureMessageAndClosePiP(
    firstTab.linkedBrowser,
    "with-controls",
    firstPip,
    false
  );
  info("First PiP was open, it is now closed.");

  gBrowser.selectedTab = secondTab;

  await ensureMessageAndClosePiP(
    secondTab.linkedBrowser,
    "with-controls",
    thirdPip,
    false
  );
  info("Third PiP was open, it is now closed.");

  await ensureMessageAndClosePiP(
    secondTab.linkedBrowser,
    "no-controls",
    fourthPip,
    false
  );
  info("Fourth PiP was open, it is now closed.");

  Assert.equal(
    getTelemetryMaxPipCount(false),
    3,
    "Max PiP count should still be 3"
  );

  BrowserTestUtils.removeTab(firstTab);
  BrowserTestUtils.removeTab(secondTab);
});