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

"use strict";

/**
 * Tests that if a <video> element is being displayed in a
 * Picture-in-Picture window, that the window closes if that
 * original <video> is ever removed from the DOM.
 */
add_task(async () => {
  for (let videoID of ["with-controls", "no-controls"]) {
    info(`Testing ${videoID} case.`);

    await BrowserTestUtils.withNewTab(
      {
        url: TEST_PAGE,
        gBrowser,
      },
      async browser => {
        let pipWin = await triggerPictureInPicture(browser, videoID);
        Assert.ok(pipWin, "Got PiP window.");

        // First, let's make sure that removing the _other_ video doesn't cause
        // the special event to fire, nor the PiP window to close.
        await SpecialPowers.spawn(browser, [videoID], async videoID => {
          let doc = content.document;
          let otherVideo = doc.querySelector(`video:not([id="${videoID}"])`);
          let eventFired = false;

          let listener = e => {
            eventFired = true;
          };

          docShell.chromeEventHandler.addEventListener(
            "MozStopPictureInPicture",
            listener,
            {
              capture: true,
            }
          );
          otherVideo.remove();
          Assert.ok(
            !eventFired,
            "Should not have seen MozStopPictureInPicture for other video"
          );
          docShell.chromeEventHandler.removeEventListener(
            "MozStopPictureInPicture",
            listener,
            {
              capture: true,
            }
          );
        });

        Assert.ok(!pipWin.closed, "PiP window should still be open.");

        await SpecialPowers.spawn(browser, [videoID], async videoID => {
          let doc = content.document;
          let video = doc.querySelector(`#${videoID}`);

          let promise = ContentTaskUtils.waitForEvent(
            docShell.chromeEventHandler,
            "MozStopPictureInPicture",
            { capture: true }
          );
          video.remove();
          await promise;
        });

        try {
          await BrowserTestUtils.waitForCondition(
            () => pipWin.closed,
            "Player window closed."
          );
        } finally {
          if (!pipWin.closed) {
            await BrowserTestUtils.closeWindow(pipWin);
          }
        }
      }
    );
  }
});