File's structured serialization should serialize lastModified attribute
[WebKit-https.git] / LayoutTests / imported / w3c / web-platform-tests / media-source / mediasource-activesourcebuffers.html
1 <!DOCTYPE html>
2 <!-- Copyright © 2016 Chromium authors and World Wide Web Consortium, (Massachusetts Institute of Technology, ERCIM, Keio University, Beihang). -->
3 <html>
4     <head>
5         <title>Checks MediaSource.activeSourceBuffers and changes to selected/enabled track state</title>
6         <script src="/resources/testharness.js"></script>
7         <script src="/resources/testharnessreport.js"></script>
8         <script src="mediasource-util.js"></script>
9     </head>
10     <body>
11         <div id="log"></div>
12         <script>
13           // Audio / Video files supported by the user agent under test
14           var subType = MediaSourceUtil.getSubType(MediaSourceUtil.AUDIO_ONLY_TYPE);
15           var manifestFilenameAudio = subType + "/test-a-128k-44100Hz-1ch-manifest.json";
16           var manifestFilenameVideo = subType + "/test-v-128k-320x240-30fps-10kfr-manifest.json";
17           var manifestFilenameAV = subType + "/test-av-384k-44100Hz-1ch-320x240-30fps-10kfr-manifest.json";
18
19           mediasource_test(function(test, mediaElement, mediaSource)
20           {
21               mediaElement.addEventListener("error", test.unreached_func("Unexpected event 'error'"));
22               MediaSourceUtil.fetchManifestAndData(test, manifestFilenameAudio, function (typeAudio, dataAudio)
23               {
24                   var sourceBuffer = mediaSource.addSourceBuffer(typeAudio);
25                   assert_equals(mediaSource.sourceBuffers.length, 1,
26                     "sourceBuffers list contains one SourceBuffer");
27                   assert_equals(mediaSource.activeSourceBuffers.length, 0,
28                     "activeSourceBuffers is empty to start with");
29
30                   test.expectEvent(mediaSource.activeSourceBuffers, "addsourcebuffer");
31                   test.expectEvent(mediaElement, "loadedmetadata");
32                   sourceBuffer.appendBuffer(dataAudio);
33
34                   test.waitForExpectedEvents(function()
35                   {
36                       assert_equals(mediaSource.activeSourceBuffers.length, 1,
37                         "activeSourceBuffers updated when media element is loaded");
38                       assert_equals(mediaSource.activeSourceBuffers[0], sourceBuffer,
39                         "activeSourceBuffers contains sourceBuffer when media element is loaded");
40                       test.done();
41                   });
42               });
43           }, "SourceBuffer added to activeSourceBuffers list when its only audio track gets loaded (and thus becomes enabled).");
44
45
46           mediasource_test(function(test, mediaElement, mediaSource)
47           {
48               mediaElement.addEventListener("error", test.unreached_func("Unexpected event 'error'"));
49               MediaSourceUtil.fetchManifestAndData(test, manifestFilenameVideo, function (typeVideo, dataVideo)
50               {
51                   var sourceBuffer = mediaSource.addSourceBuffer(typeVideo);
52                   assert_equals(mediaSource.sourceBuffers.length, 1,
53                     "sourceBuffers list contains one SourceBuffer");
54                   assert_equals(mediaSource.activeSourceBuffers.length, 0,
55                     "activeSourceBuffers is empty to start with");
56
57                   test.expectEvent(mediaSource.activeSourceBuffers, "addsourcebuffer");
58                   test.expectEvent(mediaElement, "loadedmetadata");
59                   sourceBuffer.appendBuffer(dataVideo);
60
61                   test.waitForExpectedEvents(function()
62                   {
63                       assert_equals(mediaSource.activeSourceBuffers.length, 1,
64                         "activeSourceBuffers updated when media element is loaded");
65                       assert_equals(mediaSource.activeSourceBuffers[0], sourceBuffer,
66                         "activeSourceBuffers contains sourceBuffer when media element is loaded");
67                       test.done();
68                   });
69               });
70           }, "SourceBuffer added to activeSourceBuffers list when its only video track gets loaded (and thus becomes selected).");
71
72           function mediaSourceActiveSourceBufferOrderTest(addAudioFirst, appendAudioFirst)
73           {
74               mediasource_test(function(test, mediaElement, mediaSource)
75               {
76                   mediaElement.addEventListener("error", test.unreached_func("Unexpected event 'error'"));
77                   MediaSourceUtil.fetchManifestAndData(test, manifestFilenameAudio, function (typeAudio, dataAudio)
78                   {
79                       MediaSourceUtil.fetchManifestAndData(test, manifestFilenameVideo, function (typeVideo, dataVideo)
80                       {
81                           var sourceBufferAudio, sourceBufferVideo, expectedFirstSB, expectedSecondSB;
82                           if (addAudioFirst) {
83                               expectedFirstSB = sourceBufferAudio = mediaSource.addSourceBuffer(typeAudio);
84                               expectedSecondSB = sourceBufferVideo = mediaSource.addSourceBuffer(typeVideo);
85                           } else {
86                               expectedFirstSB = sourceBufferVideo = mediaSource.addSourceBuffer(typeVideo);
87                               expectedSecondSB = sourceBufferAudio = mediaSource.addSourceBuffer(typeAudio);
88                           }
89
90                           assert_equals(mediaSource.activeSourceBuffers.length, 0,
91                             "activeSourceBuffers is empty to start with");
92                           assert_equals(mediaSource.sourceBuffers.length, 2,
93                             "sourceBuffers list contains both SourceBuffers");
94                           assert_equals(mediaSource.sourceBuffers[0], expectedFirstSB,
95                             "first SourceBuffer matches expectation");
96                           assert_equals(mediaSource.sourceBuffers[1], expectedSecondSB,
97                             "second SourceBuffer matches expectation");
98                           test.expectEvent(mediaSource.activeSourceBuffers, "addsourcebuffer");
99                           test.expectEvent(mediaSource.activeSourceBuffers, "addsourcebuffer");
100                           test.expectEvent(mediaElement, "loadedmetadata");
101                           if (appendAudioFirst) {
102                               sourceBufferAudio.appendBuffer(dataAudio);
103                               sourceBufferVideo.appendBuffer(dataVideo);
104                           } else {
105                               sourceBufferVideo.appendBuffer(dataVideo);
106                               sourceBufferAudio.appendBuffer(dataAudio);
107                           }
108
109                           test.waitForExpectedEvents(function()
110                           {
111                               assert_equals(mediaSource.activeSourceBuffers.length, 2,
112                                 "activeSourceBuffers list updated when tracks are loaded");
113                               assert_equals(mediaSource.activeSourceBuffers[0], mediaSource.sourceBuffers[0],
114                                 "first active SourceBuffer matches first SourceBuffer");
115                               assert_equals(mediaSource.activeSourceBuffers[1], mediaSource.sourceBuffers[1],
116                                 "second active SourceBuffer matches second SourceBuffer");
117                               test.done();
118                           });
119                       });
120                   });
121               },
122               "Active SourceBuffers must appear in the same order as they appear in the sourceBuffers attribute: " +
123                 (addAudioFirst ? "audio is first sourceBuffer" : "video is first sourceBuffer") + ", " +
124                 (appendAudioFirst ? "audio media appended first" : "video media appended first"));
125           }
126
127           mediaSourceActiveSourceBufferOrderTest(true, true);
128           mediaSourceActiveSourceBufferOrderTest(true, false);
129           mediaSourceActiveSourceBufferOrderTest(false, true);
130           mediaSourceActiveSourceBufferOrderTest(false, false);
131
132           mediasource_test(function(test, mediaElement, mediaSource)
133           {
134               mediaElement.addEventListener("error", test.unreached_func("Unexpected event 'error'"));
135               MediaSourceUtil.fetchManifestAndData(test, manifestFilenameAudio, function (typeAudio, dataAudio)
136               {
137                   MediaSourceUtil.fetchManifestAndData(test, manifestFilenameVideo, function (typeVideo, dataVideo)
138                   {
139                       var sourceBufferAudio = mediaSource.addSourceBuffer(typeAudio);
140                       var sourceBufferVideo = mediaSource.addSourceBuffer(typeVideo);
141
142                       test.expectEvent(sourceBufferAudio.audioTracks, "addtrack");
143                       test.expectEvent(sourceBufferVideo.videoTracks, "addtrack");
144                       sourceBufferAudio.appendBuffer(dataAudio);
145                       sourceBufferVideo.appendBuffer(dataVideo);
146
147                       test.waitForExpectedEvents(function()
148                       {
149                           assert_equals(mediaSource.activeSourceBuffers.length, 2,
150                             "activeSourceBuffers list updated when tracks are loaded");
151                           assert_equals(sourceBufferAudio.audioTracks.length, 1,
152                             "audio track list contains loaded audio track");
153                           assert_equals(sourceBufferVideo.videoTracks.length, 1,
154                             "video track list contains loaded video track");
155
156                           test.expectEvent(mediaSource.activeSourceBuffers, "removesourcebuffer");
157                           sourceBufferAudio.audioTracks[0].enabled = false;
158                       });
159
160                       test.waitForExpectedEvents(function()
161                       {
162                           assert_equals(mediaSource.activeSourceBuffers.length, 1,
163                             "audio source buffer no longer in the activeSourceBuffers list");
164                           assert_equals(mediaSource.activeSourceBuffers[0], sourceBufferVideo,
165                             "activeSourceBuffers list only contains the video SourceBuffer");
166
167                           test.expectEvent(mediaSource.activeSourceBuffers, "addsourcebuffer");
168                           test.expectEvent(mediaSource.activeSourceBuffers, "removesourcebuffer");
169                           sourceBufferAudio.audioTracks[0].enabled = true;
170                           sourceBufferVideo.videoTracks[0].selected = false;
171                       });
172
173                       test.waitForExpectedEvents(function()
174                       {
175                           assert_equals(mediaSource.activeSourceBuffers.length, 1,
176                             "video source buffer no longer in the activeSourceBuffers list");
177                           assert_equals(mediaSource.activeSourceBuffers[0], sourceBufferAudio,
178                             "activeSourceBuffers list only contains the audio SourceBuffer");
179                           test.done();
180                       });
181                   });
182               });
183           }, "Active SourceBuffers list reflects changes to selected audio/video tracks associated with separate SourceBuffers.");
184
185
186           mediasource_test(function(test, mediaElement, mediaSource)
187           {
188               mediaElement.addEventListener("error", test.unreached_func("Unexpected event 'error'"));
189               MediaSourceUtil.fetchManifestAndData(test, manifestFilenameAV, function (typeAV, dataAV)
190               {
191                   var sourceBuffer = mediaSource.addSourceBuffer(typeAV);
192
193                   test.expectEvent(sourceBuffer.audioTracks, "addtrack");
194                   test.expectEvent(sourceBuffer.videoTracks, "addtrack");
195                   sourceBuffer.appendBuffer(dataAV);
196
197                   test.waitForExpectedEvents(function()
198                   {
199                       assert_equals(mediaSource.activeSourceBuffers.length, 1,
200                         "activeSourceBuffers list updated when tracks are loaded");
201                       assert_equals(sourceBuffer.audioTracks.length, 1,
202                         "audio track list contains loaded audio track");
203                       assert_equals(sourceBuffer.videoTracks.length, 1,
204                         "video track list contains loaded video track");
205
206                       mediaSource.activeSourceBuffers.addEventListener("removesourcebuffer", test.unreached_func(
207                         "Unexpected removal from activeSourceBuffers list"));
208                       mediaSource.activeSourceBuffers.addEventListener("addsourcebuffer", test.unreached_func(
209                         "Unexpected insertion in activeSourceBuffers list"));
210
211                       // Changes should only trigger events at the
212                       // AudioTrack/VideoTrack instance
213                       test.expectEvent(sourceBuffer.audioTracks, "change");
214                       sourceBuffer.audioTracks[0].enabled = false;
215                   });
216
217                   test.waitForExpectedEvents(function()
218                   {
219                       assert_equals(mediaSource.activeSourceBuffers.length, 1,
220                         "activeSourceBuffers list unchanged");
221
222                       test.expectEvent(sourceBuffer.videoTracks, "change");
223                       sourceBuffer.audioTracks[0].enabled = true;
224                       sourceBuffer.videoTracks[0].selected = false;
225                   });
226
227                   test.waitForExpectedEvents(function()
228                   {
229                       assert_equals(mediaSource.activeSourceBuffers.length, 1,
230                         "activeSourceBuffers list unchanged");
231                       test.done();
232                   });
233               });
234           }, "Active SourceBuffers list ignores changes to selected audio/video tracks " +
235             "that do not affect the activation of the SourceBuffer.");
236         </script>
237     </body>
238 </html>