summaryrefslogtreecommitdiffstats
path: root/dom/media/test/test_streams_tracks.html
blob: 845bc36ca35cc9c94832f94581b969c052f2227f (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test MediaStreamTrack interfaces</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
  <script type="text/javascript" src="manifest.js"></script>
</head>
<body>
<pre id="test">
<script class="testbody" type="text/javascript">
const manager = new MediaTestManager;

function testTracks(tracks, hasTrack, kind, src) {
  is(tracks.length, hasTrack ? 1 : 0, `Correct ${kind} track count for ${src}`);
  for (const track of tracks) {
    is(track.readyState, "live", `Track ${track.id} should still be live`);
    is(track.kind, kind, `Correct track kind for track ${track.id} of ${src}`);
    ok(/\{........-....-....-....-............\}/.test(track.id),
      `id ${track.id} for ${track.kind} track of ${src} has correct form`);
  }
}

async function startTest(test, token) {
  try {
    info(`Starting test of ${test.name}`);
    const element = document.createElement("video");

    element.token = token;
    manager.started(token);

    element.src = test.name;
    element.test = test;
    const stream = element.mozCaptureStreamUntilEnded();

    element.play();

    await new Promise(r => element.onloadedmetadata = r);

    testTracks(stream.getAudioTracks(), test.hasAudio, "audio", test.name);
    testTracks(stream.getVideoTracks(), test.hasVideo, "video", test.name);
    const tracks = stream.getTracks();

    await new Promise(r => element.onended = r);

    for (let i = 0; i < tracks.length; ++i) {
      await Promise.race(
        tracks.map(t => new Promise(r => t.onended = r))
      );
      await new Promise(r => stream.onremovetrack = r);
    }

    testTracks(stream.getAudioTracks(), false, "audio", test.name);
    testTracks(stream.getVideoTracks(), false, "video", test.name);
  } catch(e) {
    ok(false, `Caught error: ${e}`);
  } finally {
    manager.finished(token);
  }
}

manager.runTests(gTrackTests, startTest);
</script>
</pre>
</body>
</html>