summaryrefslogtreecommitdiffstats
path: root/toolkit/components/pictureinpicture/tests/browser_changePiPSrcInFullscreen.js
blob: e971f17296c54136eb2a82c65cb50c433e5ad74f (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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const { sinon } = ChromeUtils.importESModule(
  "resource://testing-common/Sinon.sys.mjs"
);

const NEW_VIDEO_ASPECT_RATIO = 1.334;

async function switchVideoSource(browser, src) {
  await ContentTask.spawn(browser, { src }, async ({ src }) => {
    let doc = content.document;
    let video = doc.getElementById("no-controls");
    video.src = src;
  });
}

/**
 *
 * @param {Object} actual The actual size and position of the window
 * @param {Object} expected The expected size and position of the window
 * @param {String} message A message to print before asserting the size and position
 */
function assertEvent(actual, expected, message) {
  info(message);
  isfuzzy(
    actual.width,
    expected.width,
    ACCEPTABLE_DIFFERENCE,
    `The actual width: ${actual.width}. The expected width: ${expected.width}`
  );
  isfuzzy(
    actual.height,
    expected.height,
    ACCEPTABLE_DIFFERENCE,
    `The actual height: ${actual.height}. The expected height: ${expected.height}`
  );
  isfuzzy(
    actual.left,
    expected.left,
    ACCEPTABLE_DIFFERENCE,
    `The actual left: ${actual.left}. The expected left: ${expected.left}`
  );
  isfuzzy(
    actual.top,
    expected.top,
    ACCEPTABLE_DIFFERENCE,
    `The actual top: ${actual.top}. The expected top: ${expected.top}`
  );
}

/**
 * This test is our control test. This tests that when the PiP window exits
 * fullscreen it will return to the size it was before being fullscreened.
 */
add_task(async function testNoSrcChangeFullscreen() {
  // After opening the PiP window, it is resized to 640x360. There is a change
  // the PiP window will open with that size. To prevent that we override the
  // last saved position so we open at (0, 0) and 300x300.
  overrideSavedPosition(0, 0, 300, 300);
  await BrowserTestUtils.withNewTab(
    {
      url: TEST_PAGE,
      gBrowser,
    },
    async browser => {
      await ensureVideosReady(browser);
      let pipWin = await triggerPictureInPicture(browser, "no-controls");
      let controls = pipWin.document.getElementById("controls");
      const screen = pipWin.screen;

      let resizeEventArray = [];
      pipWin.addEventListener("resize", event => {
        let win = event.target;
        let obj = {
          width: win.innerWidth,
          height: win.innerHeight,
          left: win.screenLeft,
          top: win.screenTop,
        };
        resizeEventArray.push(obj);
      });

      // Move the PiP window to an unsaved location
      let left = 100;
      let top = 100;
      pipWin.moveTo(left, top);

      await BrowserTestUtils.waitForCondition(
        () => pipWin.screenLeft === 100 && pipWin.screenTop === 100,
        "Waiting for PiP to move to 100, 100"
      );

      let width = 640;
      let height = 360;

      let resizePromise = BrowserTestUtils.waitForEvent(pipWin, "resize");
      pipWin.resizeTo(width, height);
      await resizePromise;

      Assert.equal(
        resizeEventArray.length,
        1,
        "resizeEventArray should have 1 event"
      );

      let actualEvent = resizeEventArray.splice(0, 1)[0];
      let expectedEvent = { width, height, left, top };
      assertEvent(
        actualEvent,
        expectedEvent,
        "The PiP window has been correctly positioned before fullscreen"
      );

      await promiseFullscreenEntered(pipWin, async () => {
        EventUtils.sendMouseEvent(
          {
            type: "dblclick",
          },
          controls,
          pipWin
        );
      });

      Assert.equal(
        pipWin.document.fullscreenElement,
        pipWin.document.body,
        "Double-click caused us to enter fullscreen."
      );

      await BrowserTestUtils.waitForCondition(
        () => resizeEventArray.length === 1,
        "Waiting for resizeEventArray to have 1 event"
      );

      actualEvent = resizeEventArray.splice(0, 1)[0];
      expectedEvent = {
        width: screen.width,
        height: screen.height,
        left: screen.left,
        top: screen.top,
      };
      assertEvent(
        actualEvent,
        expectedEvent,
        "The PiP window has been correctly fullscreened before switching source"
      );

      await promiseFullscreenExited(pipWin, async () => {
        EventUtils.sendMouseEvent(
          {
            type: "dblclick",
          },
          controls,
          pipWin
        );
      });

      Assert.ok(
        !pipWin.document.fullscreenElement,
        "Double-click caused us to exit fullscreen."
      );

      await BrowserTestUtils.waitForCondition(
        () => resizeEventArray.length >= 1,
        "Waiting for resizeEventArray to have 1 event, got " +
          resizeEventArray.length
      );

      actualEvent = resizeEventArray.splice(0, 1)[0];
      expectedEvent = { width, height, left, top };
      assertEvent(
        actualEvent,
        expectedEvent,
        "The PiP window has been correctly positioned after exiting fullscreen"
      );

      await ensureMessageAndClosePiP(browser, "no-controls", pipWin, false);

      clearSavedPosition();
    }
  );
});

/**
 * This function tests changing the src of a Picture-in-Picture player while
 * the player is fullscreened and then ensuring the that video stays
 * fullscreened after the src change and that the player will resize to the new
 * video size.
 */
add_task(async function testChangingSameSizeVideoSrcFullscreen() {
  // After opening the PiP window, it is resized to 640x360. There is a change
  // the PiP window will open with that size. To prevent that we override the
  // last saved position so we open at (0, 0) and 300x300.
  overrideSavedPosition(0, 0, 300, 300);
  await BrowserTestUtils.withNewTab(
    {
      url: TEST_PAGE,
      gBrowser,
    },
    async browser => {
      await ensureVideosReady(browser);
      let pipWin = await triggerPictureInPicture(browser, "no-controls");
      let controls = pipWin.document.getElementById("controls");
      const screen = pipWin.screen;
      let sandbox = sinon.createSandbox();
      let resizeToVideoSpy = sandbox.spy(pipWin, "resizeToVideo");

      let resizeEventArray = [];
      pipWin.addEventListener("resize", event => {
        let win = event.target;
        let obj = {
          width: win.innerWidth,
          height: win.innerHeight,
          left: win.screenLeft,
          top: win.screenTop,
        };
        resizeEventArray.push(obj);
      });

      // Move the PiP window to an unsaved location
      let left = 100;
      let top = 100;
      pipWin.moveTo(left, top);

      await BrowserTestUtils.waitForCondition(
        () => pipWin.screenLeft === 100 && pipWin.screenTop === 100,
        "Waiting for PiP to move to 100, 100"
      );

      let width = 640;
      let height = 360;

      let resizePromise = BrowserTestUtils.waitForEvent(pipWin, "resize");
      pipWin.resizeTo(width, height);
      await resizePromise;

      Assert.equal(
        resizeEventArray.length,
        1,
        "resizeEventArray should have 1 event"
      );

      let actualEvent = resizeEventArray.splice(0, 1)[0];
      let expectedEvent = { width, height, left, top };
      assertEvent(
        actualEvent,
        expectedEvent,
        "The PiP window has been correctly positioned before fullscreen"
      );

      await promiseFullscreenEntered(pipWin, async () => {
        EventUtils.sendMouseEvent(
          {
            type: "dblclick",
          },
          controls,
          pipWin
        );
      });

      Assert.equal(
        pipWin.document.fullscreenElement,
        pipWin.document.body,
        "Double-click caused us to enter fullscreen."
      );

      await BrowserTestUtils.waitForCondition(
        () => resizeEventArray.length === 1,
        "Waiting for resizeEventArray to have 1 event"
      );

      actualEvent = resizeEventArray.splice(0, 1)[0];
      expectedEvent = {
        width: screen.width,
        height: screen.height,
        left: screen.left,
        top: screen.top,
      };
      assertEvent(
        actualEvent,
        expectedEvent,
        "The PiP window has been correctly fullscreened before switching source"
      );

      await switchVideoSource(browser, "test-video.mp4");

      await BrowserTestUtils.waitForCondition(
        () => resizeToVideoSpy.calledOnce,
        "Waiting for deferredResize to be updated"
      );

      await promiseFullscreenExited(pipWin, async () => {
        EventUtils.sendMouseEvent(
          {
            type: "dblclick",
          },
          controls,
          pipWin
        );
      });

      Assert.ok(
        !pipWin.document.fullscreenElement,
        "Double-click caused us to exit fullscreen."
      );

      await BrowserTestUtils.waitForCondition(
        () => resizeEventArray.length >= 1,
        "Waiting for resizeEventArray to have 1 event, got " +
          resizeEventArray.length
      );

      actualEvent = resizeEventArray.splice(0, 1)[0];
      expectedEvent = { width, height, left, top };
      assertEvent(
        actualEvent,
        expectedEvent,
        "The PiP window has been correctly positioned after exiting fullscreen"
      );

      sandbox.restore();
      await ensureMessageAndClosePiP(browser, "no-controls", pipWin, false);

      clearSavedPosition();
    }
  );
});

/**
 * This is similar to the previous test but in this test we switch to a video
 * with a different aspect ratio to confirm that the PiP window will take the
 * new aspect ratio after exiting fullscreen. We also exit fullscreen with the
 * escape key instead of double clicking in this test.
 */
add_task(async function testChangingDifferentSizeVideoSrcFullscreen() {
  // After opening the PiP window, it is resized to 640x360. There is a change
  // the PiP window will open with that size. To prevent that we override the
  // last saved position so we open at (0, 0) and 300x300.
  overrideSavedPosition(0, 0, 300, 300);
  await BrowserTestUtils.withNewTab(
    {
      url: TEST_PAGE,
      gBrowser,
    },
    async browser => {
      await ensureVideosReady(browser);
      let pipWin = await triggerPictureInPicture(browser, "no-controls");
      let controls = pipWin.document.getElementById("controls");
      const screen = pipWin.screen;
      let sandbox = sinon.createSandbox();
      let resizeToVideoSpy = sandbox.spy(pipWin, "resizeToVideo");

      let resizeEventArray = [];
      pipWin.addEventListener("resize", event => {
        let win = event.target;
        let obj = {
          width: win.innerWidth,
          height: win.innerHeight,
          left: win.screenLeft,
          top: win.screenTop,
        };
        resizeEventArray.push(obj);
      });

      // Move the PiP window to an unsaved location
      let left = 100;
      let top = 100;
      pipWin.moveTo(left, top);

      await BrowserTestUtils.waitForCondition(
        () => pipWin.screenLeft === 100 && pipWin.screenTop === 100,
        "Waiting for PiP to move to 100, 100"
      );

      let width = 640;
      let height = 360;

      let resizePromise = BrowserTestUtils.waitForEvent(pipWin, "resize");
      pipWin.resizeTo(width, height);
      await resizePromise;

      Assert.equal(
        resizeEventArray.length,
        1,
        "resizeEventArray should have 1 event"
      );

      let actualEvent = resizeEventArray.splice(0, 1)[0];
      let expectedEvent = { width, height, left, top };
      assertEvent(
        actualEvent,
        expectedEvent,
        "The PiP window has been correctly positioned before fullscreen"
      );

      await promiseFullscreenEntered(pipWin, async () => {
        EventUtils.sendMouseEvent(
          {
            type: "dblclick",
          },
          controls,
          pipWin
        );
      });

      Assert.equal(
        pipWin.document.fullscreenElement,
        pipWin.document.body,
        "Double-click caused us to enter fullscreen."
      );

      await BrowserTestUtils.waitForCondition(
        () => resizeEventArray.length === 1,
        "Waiting for resizeEventArray to have 1 event"
      );

      actualEvent = resizeEventArray.splice(0, 1)[0];
      expectedEvent = {
        width: screen.width,
        height: screen.height,
        left: screen.left,
        top: screen.top,
      };
      assertEvent(
        actualEvent,
        expectedEvent,
        "The PiP window has been correctly fullscreened before switching source"
      );

      let previousWidth = pipWin.getDeferredResize().width;

      await switchVideoSource(browser, "test-video-long.mp4");

      // Confirm that we are updating the `deferredResize` and not actually resizing
      await BrowserTestUtils.waitForCondition(
        () => resizeToVideoSpy.calledOnce,
        "Waiting for deferredResize to be updated"
      );

      // Confirm that we updated the deferredResize to the new width
      await BrowserTestUtils.waitForCondition(
        () => previousWidth !== pipWin.getDeferredResize().width,
        "Waiting for deferredResize to update"
      );

      await promiseFullscreenExited(pipWin, async () => {
        EventUtils.synthesizeKey("KEY_Escape", {}, pipWin);
      });

      Assert.ok(
        !pipWin.document.fullscreenElement,
        "Escape key caused us to exit fullscreen."
      );

      await BrowserTestUtils.waitForCondition(
        () => resizeEventArray.length >= 1,
        "Waiting for resizeEventArray to have 1 event, got " +
          resizeEventArray.length
      );

      actualEvent = resizeEventArray.splice(0, 1)[0];
      expectedEvent = {
        width: height * NEW_VIDEO_ASPECT_RATIO,
        height,
        left,
        top,
      };

      // When two resize events happen very close together we optimize by
      // "coalescing" the two resizes into a single resize event. Sometimes
      // the events aren't "coalesced" together (I don't know why) so I check
      // if the most recent event is what we are looking for and if it is not
      // then I'll wait for the resize event we are looking for.
      if (
        Math.abs(
          actualEvent.width - expectedEvent.width <= ACCEPTABLE_DIFFERENCE
        )
      ) {
        // The exit fullscreen resize events were "coalesced".
        assertEvent(
          actualEvent,
          expectedEvent,
          "The PiP window has been correctly positioned after exiting fullscreen"
        );
      } else {
        // For some reason the exit fullscreen resize events weren't "coalesced"
        // so we have to wait for the next resize event.
        await BrowserTestUtils.waitForCondition(
          () => resizeEventArray.length === 1,
          "Waiting for resizeEventArray to have 1 event"
        );

        actualEvent = resizeEventArray.splice(0, 1)[0];

        assertEvent(
          actualEvent,
          expectedEvent,
          "The PiP window has been correctly positioned after exiting fullscreen"
        );
      }

      sandbox.restore();
      await ensureMessageAndClosePiP(browser, "no-controls", pipWin, false);

      clearSavedPosition();
    }
  );
});