summaryrefslogtreecommitdiffstats
path: root/dom/media/webvtt/test/mochitest/test_texttrack_mode_change_during_loading.html
blob: 974f452092387ddc9e781ec6d9192689b50418f8 (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
<!DOCTYPE HTML>
<html>
<head>
  <title>WebVTT : changing track's mode during loading</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="manifest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<script class="testbody" type="text/javascript">
/**
 * This test is to ensure that we won't get `error` event when we change track's
 * mode during loading. In this test, track element starts loading after setting
 * the src and we would start another load later just after the channel which is
 * used to fetch data starts. The second load is triggered by mode changes, and
 * it should stop the prevous load and won't generate any error.
 */
async function startTest() {
  const video = createVideo();
  const trackElement = createAndAppendtrackElemententToVideo(video);

  await changeTrackModeDuringLoading(trackElement);
  await waitUntilTrackLoaded(trackElement);

  removeNodeAndSource(video);
  SimpleTest.finish();
}

SimpleTest.waitForExplicitFinish();
SpecialPowers.pushPrefEnv({"set": [["media.webvtt.testing.events", true]]},
                            startTest);

/**
 * The following are test helper functions.
 */
function createVideo() {
  info(`create video`);
  let video = document.createElement("video");
  video.src = "gizmo.mp4";
  document.body.appendChild(video);
  return video;
}

function createAndAppendtrackElemententToVideo(video) {
  let trackElement = document.createElement("track");
  trackElement.default = true;
  video.append(trackElement);
  return trackElement;
}

async function changeTrackModeDuringLoading(trackElement) {
  info(`set src to start loading`);
  trackElement.src = "basic.vtt";

  info(`wait until starting loading resource.`);
  await once(trackElement, "mozStartedLoadingTextTrack");

  info(`changeing track's mode during loading should not cause loading failed.`);
  trackElement.onerror = () => {
    ok(false, `Should not get error event!`);
  }
  trackElement.track.mode = "hidden";
}

async function waitUntilTrackLoaded(trackElement) {
  if (trackElement.readyState != 2) {
    info(`wait until the track finishes loading`);
    await once(trackElement, "load");
  }
  is(trackElement.readyState, 2, "Track::ReadyState should be set to LOADED.");
  is(trackElement.track.cues.length, 6, "Cue list length should be 6.");
}
</script>
</body>
</html>