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

"use strict";

/**
 * This test ensures that text tracks disappear from the pip window
 * when the pref is disabled.
 */
add_task(async function test_text_tracks_existing_window_pref_disabled() {
  info("Running test: existing window - pref disabled");
  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.");
      let pipBrowser = pipWin.document.getElementById("browser");

      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}`);
      });

      info("Turning off pref");
      await SpecialPowers.popPrefEnv();
      await SpecialPowers.pushPrefEnv({
        set: [
          [
            "media.videocontrols.picture-in-picture.display-text-tracks.enabled",
            false,
          ],
        ],
      });

      // Verify that cue is no longer on the pip window
      info("Checking that cue is no longer on pip window");
      await SpecialPowers.spawn(pipBrowser, [], async () => {
        let textTracks = content.document.getElementById("texttracks");
        await ContentTaskUtils.waitForCondition(() => {
          return !textTracks.textContent;
        }, `Text track is still visible on the pip window. Got ${textTracks.textContent}`);
        info("Successfully removed text tracks from pip window");
      });
    }
  );
});

/**
 * This test ensures that text tracks shown on the source video
 * window appear on an existing pip window when the pref is enabled.
 */
add_task(async function test_text_tracks_existing_window_pref_enabled() {
  info("Running test: existing window - pref enabled");
  await SpecialPowers.pushPrefEnv({
    set: [
      [
        "media.videocontrols.picture-in-picture.display-text-tracks.enabled",
        false,
      ],
    ],
  });
  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.");
      let pipBrowser = pipWin.document.getElementById("browser");

      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");
        ok(
          !textTracks.textContent,
          "Text tracks should not be visible on the pip window"
        );
      });

      info("Turning on pref");
      await SpecialPowers.popPrefEnv();
      await SpecialPowers.pushPrefEnv({
        set: [
          [
            "media.videocontrols.picture-in-picture.display-text-tracks.enabled",
            true,
          ],
        ],
      });

      info("Checking that cue is on pip window");
      await SpecialPowers.spawn(pipBrowser, [], async () => {
        let textTracks = content.document.getElementById("texttracks");
        await ContentTaskUtils.waitForCondition(() => {
          return textTracks.textContent;
        }, `Text track is still not visible on the pip window. Got ${textTracks.textContent}`);
        info("Successfully displayed text tracks on pip window");
      });
    }
  );
});

/**
 * This test ensures that text tracks update to the correct track
 * when a new track is selected.
 */
add_task(async function test_text_tracks_existing_window_new_track() {
  info("Running test: existing window - new track");
  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.");
      let pipBrowser = pipWin.document.getElementById("browser");

      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}`);
        ok(
          textTracks.textContent.includes("track 1"),
          "Track 1 should be loaded"
        );
      });

      // Change track in the content window
      await SpecialPowers.spawn(browser, [videoID], async videoID => {
        let video = content.document.getElementById(videoID);
        let tracks = video.textTracks;

        info("Changing to a new track");
        let track1 = tracks[0];
        track1.mode = "disabled";
        let track2 = tracks[1];
        track2.mode = "showing";
      });

      // Ensure new track is loaded
      await SpecialPowers.spawn(pipBrowser, [], async () => {
        info("Checking new text track content in pip window");
        let textTracks = content.document.getElementById("texttracks");

        await ContentTaskUtils.waitForCondition(() => {
          return textTracks.textContent;
        }, `Text track is still not visible on the pip window. Got ${textTracks.textContent}`);
        ok(
          textTracks.textContent.includes("track 2"),
          "Track 2 should be loaded"
        );
      });
    }
  );
});

/**
 * This test ensures that text tracks are correctly updated.
 */
add_task(async function test_text_tracks_existing_window_cues() {
  info("Running test: existing window - cues");
  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.");
      let pipBrowser = pipWin.document.getElementById("browser");

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

        // Verify that first cue appears
        info("Checking first cue on pip window");
        await ContentTaskUtils.waitForCondition(() => {
          return textTracks.textContent;
        }, `Text track is still not visible on the pip window. Got ${textTracks.textContent}`);
        ok(
          textTracks.textContent.includes("cue 1"),
          `Expected text should be displayed on the pip window. Got ${textTracks.textContent}.`
        );
      });

      // Play video to move to the next cue
      await waitForNextCue(browser, videoID);

      // Test remaining cues
      await SpecialPowers.spawn(pipBrowser, [], async () => {
        let textTracks = content.document.getElementById("texttracks");

        // Verify that empty cue makes text disappear
        info("Checking empty cue in pip window");
        await ContentTaskUtils.waitForCondition(() => {
          info(`Current text content is: ${textTracks.textContent}`);
          return !textTracks.textContent;
        }, `Text track is still visible on the pip window. Got ${textTracks.textContent}`);
      });

      await waitForNextCue(browser, videoID);

      // Wait and verify second cue
      await SpecialPowers.spawn(pipBrowser, [], async () => {
        let textTracks = content.document.getElementById("texttracks");
        info("Checking second cue in pip window");
        await ContentTaskUtils.waitForCondition(() => {
          // Cue may not appear right away after cuechange event.
          // Wait until it appears before verifying text content.
          info(`Current text content is: ${textTracks.textContent}`);
          return (
            textTracks.textContent && textTracks.textContent.includes("cue 2")
          );
        }, `Text track is still not visible on the pip window. Got ${textTracks.textContent}`);
      });
    }
  );
});

/**
 * This test ensures that text tracks disappear if no track is selected.
 */
add_task(async function test_text_tracks_existing_window_no_track() {
  info("Running test: existing window - no track");
  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.");
      let pipBrowser = pipWin.document.getElementById("browser");

      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}`);
      });

      // Remove track in the content window
      await SpecialPowers.spawn(browser, [videoID], async videoID => {
        let video = content.document.getElementById(videoID);
        let tracks = video.textTracks;

        info("Removing tracks");
        let track1 = tracks[0];
        track1.mode = "disabled";
        let track2 = tracks[1];
        track2.mode = "disabled";
      });

      await SpecialPowers.spawn(pipBrowser, [], async () => {
        info("Checking that text track disappears from pip window");
        let textTracks = content.document.getElementById("texttracks");

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

/**
 * This test ensures that text tracks appear correctly if there are multiple active cues.
 */
add_task(async function test_text_tracks_existing_window_multi_cue() {
  info("Running test: existing window - multi cue");
  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, 2);
      let pipWin = await triggerPictureInPicture(browser, videoID);
      ok(pipWin, "Got Picture-in-Picture window.");
      let pipBrowser = pipWin.document.getElementById("browser");

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

        // Verify multiple active cues
        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}`);
        is(textTracks.children.length, 2, "Text tracks should load 2 cues");
      });
    }
  );
});

/**
 * This test ensures that the showHiddenTextTracks override correctly shows
 * text tracks with a mode of "hidden".
 */
const prepareHiddenTrackTest = () =>
  new Promise((resolve, reject) => {
    BrowserTestUtils.withNewTab(
      {
        url: TEST_PAGE_WITH_WEBVTT,
        gBrowser,
      },

      async browser => {
        const videoID = "with-controls";
        await prepareVideosAndWebVTTTracks(browser, videoID, 0, "hidden");
        await SpecialPowers.spawn(browser, [{ videoID }], async args => {
          let video = content.document.getElementById(args.videoID);
          const tracks = video.textTracks;
          ok(tracks[0].mode === "hidden", "Track 1 mode is 'hidden'");
        });

        let pipWin = await triggerPictureInPicture(browser, videoID);
        ok(pipWin, "Got Picture-in-Picture window.");
        let pipBrowser = pipWin.document.getElementById("browser");
        if (!pipBrowser) {
          reject();
        }
        resolve(pipBrowser);
      }
    );
  });

add_task(async function test_hidden_text_tracks_override() {
  info("Running test - showHiddenTextTracks");

  info("hidden mode with override");
  Services.ppmm.sharedData.set(SHARED_DATA_KEY, {
    "*://example.com/*": { showHiddenTextTracks: true },
  });
  Services.ppmm.sharedData.flush();

  await prepareHiddenTrackTest().then(async pipBrowser => {
    await SpecialPowers.spawn(pipBrowser, [], async () => {
      info("Checking text track content in pip window");
      let textTracks = content.document.getElementById("texttracks");

      // Verify text track is showing in PiP window.
      ok(textTracks, "TextTracks container should exist in the pip window");
      ok(textTracks.textContent.includes("track 1"), "Track 1 should be shown");
    });
  });

  info("hidden mode without override");
  Services.ppmm.sharedData.set(SHARED_DATA_KEY, {});
  Services.ppmm.sharedData.flush();

  await prepareHiddenTrackTest().then(async pipBrowser => {
    await SpecialPowers.spawn(pipBrowser, [], async () => {
      info("Checking text track content in pip window");
      let textTracks = content.document.getElementById("texttracks");

      // Verify text track is [not] showing in PiP window.
      ok(
        !textTracks || !textTracks.textContent.length,
        "Text track should NOT appear in PiP window."
      );
    });
  });
});