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

"use strict";

/**
 * Tests that the subtitles button hides after switching to a video that does not have subtitles
 */
add_task(async () => {
  await SpecialPowers.pushPrefEnv({
    set: [
      [
        "media.videocontrols.picture-in-picture.display-text-tracks.enabled",
        true,
      ],
    ],
  });

  let videoID = "with-controls";

  await BrowserTestUtils.withNewTab(
    {
      url: TEST_PAGE_WITH_WEBVTT,
      gBrowser,
    },
    async browser => {
      await prepareVideosAndWebVTTTracks(browser, videoID);

      let pipWin = await triggerPictureInPicture(browser, videoID);
      ok(pipWin, "Got Picture-in-Picture window.");

      // Need to make sure that the PiP window is at least the minimum height
      let multiplier = 1;
      while (true) {
        if (multiplier * pipWin.innerHeight > 325) {
          break;
        }
        multiplier += 0.5;
      }

      pipWin.moveTo(50, 50);
      pipWin.resizeTo(
        pipWin.innerWidth * multiplier,
        pipWin.innerHeight * multiplier
      );

      let subtitlesButton = pipWin.document.querySelector("#closed-caption");
      await TestUtils.waitForCondition(() => {
        return !subtitlesButton.disabled;
      }, "Waiting for subtitles button to be enabled");
      ok(!subtitlesButton.disabled, "The subtitles button is enabled");

      let emptied = SpecialPowers.spawn(browser, [{ videoID }], async args => {
        let video = content.document.getElementById(args.videoID);
        info("Waiting for emptied event to be called");
        await ContentTaskUtils.waitForEvent(video, "emptied");
      });

      await SpecialPowers.spawn(browser, [{ videoID }], async args => {
        let video = content.document.getElementById(args.videoID);
        video.setAttribute("src", video.src);
        let len = video.textTracks.length;
        for (let i = 0; i < len; i++) {
          video.removeChild(video.children[0]);
        }
        video.load();
      });

      await emptied;

      await TestUtils.waitForCondition(() => {
        return subtitlesButton.disabled;
      }, "Waiting for subtitles button to be disabled after it was enabled");
      ok(subtitlesButton.disabled, "The subtitles button is disabled");

      await BrowserTestUtils.closeWindow(pipWin);
    }
  );
});

/**
 * Tests the the subtitles button shows after switching from a video with no subtitles to a video with subtitles
 */
add_task(async () => {
  const videoID = "with-controls";
  const videoID2 = "with-controls-no-tracks";

  await BrowserTestUtils.withNewTab(
    {
      url: TEST_PAGE_WITH_WEBVTT,
      gBrowser,
    },
    async browser => {
      let pipWin = await triggerPictureInPicture(browser, videoID2);
      ok(pipWin, "Got Picture-in-Picture window.");

      // Need to make sure that the PiP window is at least the minimum height
      let multiplier = 1;
      while (true) {
        if (multiplier * pipWin.innerHeight > 325) {
          break;
        }
        multiplier += 0.5;
      }

      pipWin.moveTo(50, 50);
      pipWin.resizeTo(
        pipWin.innerWidth * multiplier,
        pipWin.innerHeight * multiplier
      );

      let subtitlesButton = pipWin.document.querySelector("#closed-caption");
      await TestUtils.waitForCondition(() => {
        return subtitlesButton.disabled;
      }, "Making sure the subtitles button is disabled initially");
      ok(subtitlesButton.disabled, "The subtitles button is disabled");

      await SpecialPowers.spawn(
        browser,
        [{ videoID, videoID2 }],
        async args => {
          let video2 = content.document.getElementById(args.videoID2);

          let track = video2.addTextTrack("captions", "English", "en");
          track.mode = "showing";
          track.addCue(
            new content.window.VTTCue(0, 12, "[Test] This is the first cue")
          );
          track.addCue(
            new content.window.VTTCue(18.7, 21.5, "This is the second cue")
          );

          video2.setAttribute("src", video2.src);
          video2.load();

          is(
            video2.textTracks.length,
            1,
            "Number of tracks loaded should be 1"
          );
          video2.play();
          video2.pause();
        }
      );

      subtitlesButton = pipWin.document.querySelector("#closed-caption");
      await TestUtils.waitForCondition(() => {
        return !subtitlesButton.disabled;
      }, "Waiting for the subtitles button to be enabled after switching to a video with subtitles.");
      ok(!subtitlesButton.disabled, "The subtitles button is enabled");

      await BrowserTestUtils.closeWindow(pipWin);
    }
  );
});