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

"use strict";

/**
 * Verifies the value of a cue's .line property.
 * @param {Element} browser The <xul:browser> hosting the <video>
 * @param {String} videoID The ID of the video being checked
 * @param {Integer} trackIndex The index of the track to be loaded
 * @param {Integer} cueIndex The index of the cue to be tested on
 * @param {Integer|String} expectedValue The expected line value of the cue
 */
async function verifyLineForCue(
  browser,
  videoID,
  trackIndex,
  cueIndex,
  expectedValue
) {
  await SpecialPowers.spawn(
    browser,
    [{ videoID, trackIndex, cueIndex, expectedValue }],
    async args => {
      info("Checking .line property values");
      const video = content.document.getElementById(args.videoID);
      const activeCues = video.textTracks[args.trackIndex].activeCues;
      const vttCueLine = activeCues[args.cueIndex].line;
      is(vttCueLine, args.expectedValue, "Cue line should have expected value");
    }
  );
}

/**
 * This test ensures that text tracks appear in expected order if
 * VTTCue.line property is auto.
 */
add_task(async function test_text_tracks_new_window_line_auto() {
  info("Running test: new window - line auto");
  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 => {
      let trackIndex = 2;
      await prepareVideosAndWebVTTTracks(browser, videoID, trackIndex);

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

      await verifyLineForCue(browser, videoID, trackIndex, 0, "auto");
      await verifyLineForCue(browser, videoID, trackIndex, 1, "auto");

      await SpecialPowers.spawn(pipBrowser, [], async () => {
        info("Checking text track content in pip window");
        let textTracks = content.document.getElementById("texttracks");
        ok(textTracks, "TextTracks container should exist in the pip window");
        await ContentTaskUtils.waitForCondition(() => {
          return textTracks.textContent;
        }, `Text track is still not visible on the pip window. Got ${textTracks.textContent}`);

        let cueDivs = textTracks.children;

        is(cueDivs.length, 2, "There should be 2 active cues");
        // cue1 in this case refers to the first cue to be defined in the vtt file.
        // cue2 is therefore the next cue to be defined right after in the vtt file.
        ok(
          cueDivs[0].textContent.includes("cue 2"),
          `cue 2 should be above. Got: ${cueDivs[0].textContent}`
        );
        ok(
          cueDivs[1].textContent.includes("cue 1"),
          `cue 1 should be below. Got: ${cueDivs[1].textContent}`
        );
      });
    }
  );
});

/**
 * This test ensures that text tracks appear in expected order if
 * VTTCue.line property is an integer.
 */
add_task(async function test_text_tracks_new_window_line_integer() {
  info("Running test: new window - line integer");
  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 => {
      let trackIndex = 3;
      await prepareVideosAndWebVTTTracks(browser, videoID, trackIndex);

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

      await verifyLineForCue(browser, videoID, trackIndex, 0, 2);
      await verifyLineForCue(browser, videoID, trackIndex, 1, 3);
      await verifyLineForCue(browser, videoID, trackIndex, 2, 1);

      await SpecialPowers.spawn(pipBrowser, [], async () => {
        info("Checking text track content in pip window");
        let textTracks = content.document.getElementById("texttracks");
        ok(textTracks, "TextTracks container should exist in the pip window");
        await ContentTaskUtils.waitForCondition(() => {
          return textTracks.textContent;
        }, `Text track is still not visible on the pip window. Got ${textTracks.textContent}`);

        let cueDivs = textTracks.children;

        is(cueDivs.length, 3, "There should be 3 active cues");

        // cue1 in this case refers to the first cue to be defined in the vtt file.
        // cue2 is therefore the next cue to be defined right after in the vtt file.
        ok(
          cueDivs[0].textContent.includes("cue 3"),
          `cue 3 should be above. Got: ${cueDivs[0].textContent}`
        );
        ok(
          cueDivs[1].textContent.includes("cue 1"),
          `cue 1 should be next. Got: ${cueDivs[1].textContent}`
        );
        ok(
          cueDivs[2].textContent.includes("cue 2"),
          `cue 2 should be below. Got: ${cueDivs[2].textContent}`
        );
      });
    }
  );
});

/**
 * This test ensures that text tracks appear in expected order if
 * VTTCue.line property is a percentage value.
 */
add_task(async function test_text_tracks_new_window_line_percent() {
  info("Running test: new window - line percent");
  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 => {
      let trackIndex = 4;
      await prepareVideosAndWebVTTTracks(browser, videoID, trackIndex);

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

      await verifyLineForCue(browser, videoID, trackIndex, 0, 90);
      await verifyLineForCue(browser, videoID, trackIndex, 1, 10);
      await verifyLineForCue(browser, videoID, trackIndex, 2, 50);

      await SpecialPowers.spawn(pipBrowser, [], async () => {
        info("Checking text track content in pip window");
        let textTracks = content.document.getElementById("texttracks");
        ok(textTracks, "TextTracks container should exist in the pip window");

        await ContentTaskUtils.waitForCondition(() => {
          return textTracks.textContent;
        }, `Text track is still not visible on the pip window. Got ${textTracks.textContent}`);

        let cueDivs = textTracks.children;
        is(cueDivs.length, 3, "There should be 3 active cues");

        // cue1 in this case refers to the first cue to be defined in the vtt file.
        // cue2 is therefore the next cue to be defined right after in the vtt file.
        ok(
          cueDivs[0].textContent.includes("cue 2"),
          `cue 2 should be above. Got: ${cueDivs[0].textContent}`
        );
        ok(
          cueDivs[1].textContent.includes("cue 3"),
          `cue 3 should be next. Got: ${cueDivs[1].textContent}`
        );
        ok(
          cueDivs[2].textContent.includes("cue 1"),
          `cue 1 should be below. Got: ${cueDivs[2].textContent}`
        );
      });
    }
  );
});