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

"use strict";

/**
 * Tests that the correct video is opened in the
 * Picture-in-Picture player when opened via keyboard shortcut.
 * The shortcut will open the first unpaused video
 * or the longest video on the page.
 */
add_task(async function test_video_selection() {
  await BrowserTestUtils.withNewTab(
    {
      url: TEST_ROOT + "test-video-selection.html",
      gBrowser,
    },
    async browser => {
      await ensureVideosReady(browser);

      let pipVideoID = await SpecialPowers.spawn(browser, [], () => {
        let videoList = content.document.querySelectorAll("video");
        let longestDuration = -1;
        let pipVideoID = null;

        for (let video of videoList) {
          if (!video.paused) {
            pipVideoID = video.id;
            break;
          }
          if (video.duration > longestDuration) {
            pipVideoID = video.id;
            longestDuration = video.duration;
          }
        }
        return pipVideoID;
      });

      let domWindowOpened = BrowserTestUtils.domWindowOpenedAndLoaded(null);
      let videoReady = SpecialPowers.spawn(
        browser,
        [pipVideoID],
        async videoID => {
          let video = content.document.getElementById(videoID);
          await ContentTaskUtils.waitForCondition(() => {
            return video.isCloningElementVisually;
          }, "Video is being cloned visually.");
        }
      );

      let eventObj = { accelKey: true, shiftKey: true };
      if (AppConstants.platform == "macosx") {
        eventObj.altKey = true;
      }
      EventUtils.synthesizeKey("]", eventObj, window);

      let pipWin = await domWindowOpened;
      await videoReady;

      ok(pipWin, "Got Picture-in-Picture window.");

      await ensureMessageAndClosePiP(browser, pipVideoID, pipWin, false);

      pipVideoID = await SpecialPowers.spawn(browser, [], () => {
        let videoList = content.document.querySelectorAll("video");
        videoList[1].play();
        videoList[2].play();
        let longestDuration = -1;
        let pipVideoID = null;

        for (let video of videoList) {
          if (!video.paused) {
            pipVideoID = video.id;
            break;
          }
          if (video.duration > longestDuration) {
            pipVideoID = video.id;
            longestDuration = video.duration;
          }
        }

        return pipVideoID;
      });

      // Next time we want to use a keyboard shortcut with the main window in focus again.
      await SimpleTest.promiseFocus(browser);

      domWindowOpened = BrowserTestUtils.domWindowOpenedAndLoaded(null);
      videoReady = SpecialPowers.spawn(browser, [pipVideoID], async videoID => {
        let video = content.document.getElementById(videoID);
        await ContentTaskUtils.waitForCondition(() => {
          return video.isCloningElementVisually;
        }, "Video is being cloned visually.");
      });

      EventUtils.synthesizeKey("]", eventObj, window);

      pipWin = await domWindowOpened;
      await videoReady;

      ok(pipWin, "Got Picture-in-Picture window.");

      await ensureMessageAndClosePiP(browser, pipVideoID, pipWin, false);
    }
  );
});