summaryrefslogtreecommitdiffstats
path: root/dom/media/webvtt/test/mochitest/test_trackelementevent.html
blob: f78033a89d9c776ccf0e10544137ee0796140b63 (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test for Bug 882677 - Implement the 'sourcing out of band text tracks' algorithm</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="manifest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<video id="v" src="seek.webm" preload="auto">
<script type="text/javascript">
/**
 * This test is used to ensure that we can load resource from vtt files correctly
 * and will dispatch `error` event for invalid vtt files.
 */
var video = document.getElementById("v");

async function runTest() {
  let tracks = createTextTrackElements();
  appendTracksToVideo(tracks);
  await waitUntilsTrackLoadedOrGetError(tracks);
  SimpleTest.finish()
}

SimpleTest.waitForExplicitFinish();
onload = runTest;

/**
 * The following are test helper functions.
 */
function createTextTrackElements() {
  // Only first track has valid vtt resource, other tracks should get the error
  // event because of invalid vtt resources.
  let trackOne = document.createElement("track");
  trackOne.src = "basic.vtt";
  trackOne.kind = "subtitles";
  trackOne.expectedLoaded = true;

  let trackTwo = document.createElement("track");
  trackTwo.src = "bad-signature.vtt";
  trackTwo.kind = "captions";
  trackTwo.expectedLoaded = false;

  let trackThree = document.createElement("track");
  trackThree.src = "bad.vtt";
  trackThree.kind = "chapters";
  trackThree.expectedLoaded = false;

  return [trackOne, trackTwo, trackThree];
}

function appendTracksToVideo(tracks) {
  for (let track of tracks) {
    video.appendChild(track);
  }
}

async function waitUntilsTrackLoadedOrGetError(tracks) {
  let promises = [];
  for (let track of tracks) {
    // explictly enable those track in order to start loading.
    track.track.mode = "hidden";
    if (track.expectedLoaded) {
      info(`adding 'load' event to wait list.`);
      promises.push(once(track, "load"));
    } else {
      info(`adding 'error' event to wait list.`);
      promises.push(once(track, "error"));
    }
  }
  info(`wait until tracks finish loading or get error.`);
  await Promise.all(promises);
  ok(true, "all tracks finish loading or get error.");
}
</script>
</body>
</html>