summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/media-source/mediasource-sourcebufferlist.html
blob: 760e6df46cbdacc7cd89c7a847332d7bb23c8469 (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
<!DOCTYPE html>
<!-- Copyright © 2016 Chromium authors and World Wide Web Consortium, (Massachusetts Institute of Technology, ERCIM, Keio University, Beihang). -->
<html>
    <head>
        <title>SourceBufferList test cases.</title>
        <script src="/resources/testharness.js"></script>
        <script src="/resources/testharnessreport.js"></script>
        <script src="mediasource-util.js"></script>
    </head>
    <body>
        <div id="log"></div>
        <script>
          function verifySourceBufferLists(mediaSource, expected)
          {
              assert_equals(mediaSource.sourceBuffers.length, expected.length, "sourceBuffers length");
              assert_equals(mediaSource.activeSourceBuffers.length, 0, "activeSourceBuffers length");
              for (var i = 0; i < expected.length; ++i) {
                assert_equals(mediaSource.sourceBuffers[i], expected[i], "Verifying mediaSource.sourceBuffers[" + i + "]");
              }
              assert_equals(mediaSource.sourceBuffers[expected.length], undefined,
                "If index is greater than or equal to the length attribute then return undefined.");
          }

          mediasource_test(function(test, mediaElement, mediaSource)
          {
              var sourceBufferA = mediaSource.addSourceBuffer(MediaSourceUtil.VIDEO_ONLY_TYPE);
              verifySourceBufferLists(mediaSource, [sourceBufferA]);

              var sourceBufferB = mediaSource.addSourceBuffer(MediaSourceUtil.AUDIO_ONLY_TYPE);
              verifySourceBufferLists(mediaSource, [sourceBufferA, sourceBufferB]);
              test.done();
          }, "Test SourceBufferList getter method");

          mediasource_test(function(test, mediaElement, mediaSource)
          {
              test.expectEvent(mediaSource.sourceBuffers, "addsourcebuffer", "sourceBuffers");
              var sourceBufferA = mediaSource.addSourceBuffer(MediaSourceUtil.VIDEO_ONLY_TYPE);
              var sourceBufferB = null;

              test.waitForExpectedEvents(function()
              {
                  test.expectEvent(mediaSource.sourceBuffers, "addsourcebuffer", "sourceBuffers");
                  sourceBufferB = mediaSource.addSourceBuffer(MediaSourceUtil.AUDIO_ONLY_TYPE);

                  verifySourceBufferLists(mediaSource, [sourceBufferA, sourceBufferB]);
              });

              test.waitForExpectedEvents(function()
              {
                  test.expectEvent(mediaSource.sourceBuffers, "removesourcebuffer", "sourceBuffers");
                  mediaSource.removeSourceBuffer(sourceBufferA);

                  verifySourceBufferLists(mediaSource, [sourceBufferB]);

                  test.expectEvent(mediaSource.sourceBuffers, "addsourcebuffer", "sourceBuffers");
                  sourceBufferA = mediaSource.addSourceBuffer(MediaSourceUtil.VIDEO_ONLY_TYPE);

                  verifySourceBufferLists(mediaSource, [sourceBufferB, sourceBufferA]);
              });

              test.waitForExpectedEvents(function()
              {
                  test.done();
              });
          }, "Test SourceBufferList event dispatching.");

          mediasource_test(function(test, mediaElement, mediaSource)
          {
              test.expectEvent(mediaSource.sourceBuffers, "addsourcebuffer", "sourceBuffers");
              test.expectEvent(mediaSource.sourceBuffers, "addsourcebuffer", "sourceBuffers");
              var sourceBufferA = mediaSource.addSourceBuffer(MediaSourceUtil.VIDEO_ONLY_TYPE);
              var sourceBufferB = mediaSource.addSourceBuffer(MediaSourceUtil.AUDIO_ONLY_TYPE);

              verifySourceBufferLists(mediaSource, [sourceBufferA, sourceBufferB]);

              test.waitForExpectedEvents(function()
              {
                  verifySourceBufferLists(mediaSource, [sourceBufferA, sourceBufferB]);

                  // Force the media element to close the MediaSource object.
                  test.expectEvent(mediaSource.sourceBuffers, "removesourcebuffer", "sourceBuffers");
                  test.expectEvent(mediaSource, "sourceclose", "mediaSource closing");
                  mediaElement.src = "";
              });

              test.waitForExpectedEvents(function()
              {
                  assert_equals(mediaSource.readyState, "closed", "mediaSource is closed.");

                  verifySourceBufferLists(mediaSource, []);
                  test.done();
              });
          }, "Test that only 1 removesourcebuffer event fires on each SourceBufferList when the MediaSource closes.");

        </script>
    </body>
</html>