Unreviewed gardening; update LayoutTests/media-source to the most recent version...
[WebKit-https.git] / LayoutTests / imported / w3c / web-platform-tests / media-source / mediasource-remove.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         <meta  charset="utf-8">
6         <title>SourceBuffer.remove() test cases.</title>
7         <script src="/resources/testharness.js"></script>
8         <script src="/resources/testharnessreport.js"></script>
9         <script src="mediasource-util.js"></script>
10     </head>
11     <body>
12         <div id="log"></div>
13         <script>
14           mediasource_test(function(test, mediaElement, mediaSource)
15           {
16               var sourceBuffer = mediaSource.addSourceBuffer(MediaSourceUtil.AUDIO_VIDEO_TYPE);
17
18               mediaSource.duration = 10;
19
20               assert_throws(new TypeError(), function()
21               {
22                   sourceBuffer.remove(-1, 2);
23               }, "remove");
24
25               test.done();
26           }, "Test remove with an negative start.");
27
28           mediasource_test(function(test, mediaElement, mediaSource)
29           {
30               var sourceBuffer = mediaSource.addSourceBuffer(MediaSourceUtil.AUDIO_VIDEO_TYPE);
31
32               mediaSource.duration = 10;
33
34               [ undefined, NaN, Infinity, -Infinity ].forEach(function(item)
35               {
36                   assert_throws(new TypeError(), function()
37                   {
38                       sourceBuffer.remove(item, 2);
39                   }, "remove");
40               });
41
42               test.done();
43           }, "Test remove with non-finite start.");
44
45           mediasource_test(function(test, mediaElement, mediaSource)
46           {
47               var sourceBuffer = mediaSource.addSourceBuffer(MediaSourceUtil.AUDIO_VIDEO_TYPE);
48
49               mediaSource.duration = 10;
50
51               assert_throws(new TypeError(), function()
52               {
53                   sourceBuffer.remove(11, 12);
54               }, "remove");
55
56               test.done();
57           }, "Test remove with a start beyond the duration.");
58
59           mediasource_test(function(test, mediaElement, mediaSource)
60           {
61               var sourceBuffer = mediaSource.addSourceBuffer(MediaSourceUtil.AUDIO_VIDEO_TYPE);
62
63               mediaSource.duration = 10;
64
65               assert_throws(new TypeError(), function()
66               {
67                   sourceBuffer.remove(2, 1);
68               }, "remove");
69
70               test.done();
71           }, "Test remove with a start larger than the end.");
72
73           mediasource_test(function(test, mediaElement, mediaSource)
74           {
75               var sourceBuffer = mediaSource.addSourceBuffer(MediaSourceUtil.AUDIO_VIDEO_TYPE);
76
77               mediaSource.duration = 10;
78
79               assert_throws(new TypeError(), function()
80               {
81                   sourceBuffer.remove(0, Number.NEGATIVE_INFINITY);
82               }, "remove");
83
84               test.done();
85           }, "Test remove with a NEGATIVE_INFINITY end.");
86
87           mediasource_test(function(test, mediaElement, mediaSource)
88           {
89               var sourceBuffer = mediaSource.addSourceBuffer(MediaSourceUtil.AUDIO_VIDEO_TYPE);
90
91               mediaSource.duration = 10;
92
93               assert_throws(new TypeError(), function()
94               {
95                   sourceBuffer.remove(0, Number.NaN);
96               }, "remove");
97
98               test.done();
99           }, "Test remove with a NaN end.");
100
101           mediasource_test(function(test, mediaElement, mediaSource)
102           {
103               var sourceBuffer = mediaSource.addSourceBuffer(MediaSourceUtil.AUDIO_VIDEO_TYPE);
104
105               mediaSource.duration = 10;
106
107               mediaSource.removeSourceBuffer(sourceBuffer);
108
109               assert_throws("InvalidStateError", function()
110               {
111                   sourceBuffer.remove(1, 2);
112               }, "remove");
113
114               test.done();
115           }, "Test remove after SourceBuffer removed from mediaSource.");
116
117           mediasource_test(function(test, mediaElement, mediaSource)
118           {
119               var sourceBuffer = mediaSource.addSourceBuffer(MediaSourceUtil.AUDIO_VIDEO_TYPE);
120
121               assert_false(sourceBuffer.updating, "updating is false");
122               assert_equals(mediaSource.duration, NaN, "duration isn't set");
123
124               assert_throws(new TypeError(), function()
125               {
126                   sourceBuffer.remove(0, 0);
127               }, "remove");
128
129               test.done();
130           }, "Test remove with a NaN duration.");
131
132           mediasource_test(function(test, mediaElement, mediaSource)
133           {
134               var sourceBuffer = mediaSource.addSourceBuffer(MediaSourceUtil.AUDIO_VIDEO_TYPE);
135
136               mediaSource.duration = 10;
137
138               test.expectEvent(sourceBuffer, "updatestart");
139               test.expectEvent(sourceBuffer, "update");
140               test.expectEvent(sourceBuffer, "updateend");
141               sourceBuffer.remove(1, 2);
142
143               assert_true(sourceBuffer.updating, "updating");
144
145               assert_throws("InvalidStateError", function()
146               {
147                   sourceBuffer.remove(3, 4);
148               }, "remove");
149
150               test.waitForExpectedEvents(function()
151               {
152                   test.done();
153               });
154           }, "Test remove while update pending.");
155
156           mediasource_test(function(test, mediaElement, mediaSource)
157           {
158               var sourceBuffer = mediaSource.addSourceBuffer(MediaSourceUtil.AUDIO_VIDEO_TYPE);
159
160               mediaSource.duration = 10;
161
162               test.expectEvent(sourceBuffer, "updatestart");
163               test.expectEvent(sourceBuffer, "update");
164               test.expectEvent(sourceBuffer, "updateend");
165               sourceBuffer.remove(1, 2);
166
167               assert_true(sourceBuffer.updating, "updating");
168
169               assert_throws("InvalidStateError", function()
170               {
171                   sourceBuffer.abort();
172               }, "abort");
173
174               assert_true(sourceBuffer.updating, "updating");
175
176               test.waitForExpectedEvents(function()
177               {
178                   test.done();
179               });
180           }, "Test aborting a remove operation.");
181
182           mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
183           {
184               sourceBuffer.appendBuffer(mediaData);
185
186               test.expectEvent(sourceBuffer, "updatestart");
187               test.expectEvent(sourceBuffer, "update");
188               test.expectEvent(sourceBuffer, "updateend");
189
190               test.waitForExpectedEvents(function()
191               {
192                   assert_less_than(mediaSource.duration, 10)
193
194                   mediaSource.duration = 10;
195
196                   sourceBuffer.remove(mediaSource.duration, mediaSource.duration + 2);
197
198                   assert_true(sourceBuffer.updating, "updating");
199                   test.expectEvent(sourceBuffer, "updatestart");
200                   test.expectEvent(sourceBuffer, "update");
201                   test.expectEvent(sourceBuffer, "updateend");
202               });
203
204               test.waitForExpectedEvents(function()
205               {
206                   test.done();
207               });
208
209           }, "Test remove with a start at the duration.");
210
211           mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
212           {
213               test.expectEvent(sourceBuffer, "updatestart");
214               test.expectEvent(sourceBuffer, "update");
215               test.expectEvent(sourceBuffer, "updateend");
216               sourceBuffer.appendBuffer(mediaData);
217
218               test.waitForExpectedEvents(function()
219               {
220                   mediaSource.endOfStream();
221
222                   assert_equals(mediaSource.readyState, "ended");
223
224                   test.expectEvent(sourceBuffer, "updatestart");
225                   test.expectEvent(sourceBuffer, "update");
226                   test.expectEvent(sourceBuffer, "updateend");
227                   test.expectEvent(mediaSource, "sourceopen");
228                   sourceBuffer.remove(1, 2);
229
230                   assert_true(sourceBuffer.updating, "updating");
231                   assert_equals(mediaSource.readyState, "open");
232               });
233
234               test.waitForExpectedEvents(function()
235               {
236                   assert_false(sourceBuffer.updating, "updating");
237                   test.done();
238               });
239           }, "Test remove transitioning readyState from 'ended' to 'open'.");
240
241           function removeAppendedDataTests(callback, description)
242           {
243               mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
244               {
245                   test.expectEvent(sourceBuffer, "updatestart");
246                   test.expectEvent(sourceBuffer, "update");
247                   test.expectEvent(sourceBuffer, "updateend");
248                   sourceBuffer.appendBuffer(mediaData);
249
250                   test.waitForExpectedEvents(function()
251                   {
252                       mediaSource.endOfStream();
253                       assert_false(sourceBuffer.updating, "updating");
254
255                       var start = Math.max(segmentInfo.media[0].timev, segmentInfo.media[0].timea).toFixed(3);
256                       var duration = mediaElement.duration.toFixed(3);
257                       var subType = MediaSourceUtil.getSubType(segmentInfo.type);
258
259                       assertBufferedEquals(sourceBuffer, "{ [" + start + ", " + duration + ") }", "Initial buffered range.");
260                       callback(test, mediaSource, sourceBuffer, duration, subType, segmentInfo);
261                   });
262               }, description);
263           };
264           function removeAndCheckBufferedRanges(test, mediaSource, sourceBuffer, start, end, expected)
265           {
266               test.expectEvent(sourceBuffer, "updatestart");
267               test.expectEvent(sourceBuffer, "update");
268               test.expectEvent(sourceBuffer, "updateend");
269               sourceBuffer.remove(start, end);
270
271               test.waitForExpectedEvents(function()
272               {
273                   mediaSource.endOfStream();
274                   assert_false(sourceBuffer.updating, "updating");
275
276                   assertBufferedEquals(sourceBuffer, expected, "Buffered ranges after remove().");
277                   test.done();
278               });
279           }
280
281           removeAppendedDataTests(function(test, mediaSource, sourceBuffer, duration, subType, segmentInfo)
282           {
283               removeAndCheckBufferedRanges(test, mediaSource, sourceBuffer, 0, Number.POSITIVE_INFINITY, "{ }");
284           }, "Test removing all appended data.");
285
286           removeAppendedDataTests(function(test, mediaSource, sourceBuffer, duration, subType, segmentInfo)
287           {
288               var expectations = {
289                 webm: ("{ [3.187, " + duration + ") }"),
290                 mp4: ("{ [3.187, " + duration + ") }"),
291               };
292
293               // Note: Range doesn't start exactly at the end of the remove range because there isn't
294               // a keyframe there. The resulting range starts at the first keyframe >= the end time.
295               removeAndCheckBufferedRanges(test, mediaSource, sourceBuffer, 0, 3, expectations[subType]);
296           }, "Test removing beginning of appended data.");
297
298           removeAppendedDataTests(function(test, mediaSource, sourceBuffer, duration, subType, segmentInfo)
299           {
300               var start = Math.max(segmentInfo.media[0].timev, segmentInfo.media[0].timea).toFixed(3);
301               var expectations = {
302                 webm: ("{ [" + start + ", 1.012) [3.187, " + duration + ") }"),
303                 mp4: ("{ [" + start + ", 0.996) [3.187, " + duration + ") }"),
304               };
305
306               // Note: The first resulting range ends slightly after start because the removal algorithm only removes
307               // frames with a timestamp >= the start time. If a frame starts before and ends after the remove() start
308               // timestamp, then it stays in the buffer.
309               removeAndCheckBufferedRanges(test, mediaSource, sourceBuffer, 1, 3, expectations[subType]);
310           }, "Test removing the middle of appended data.");
311
312           removeAppendedDataTests(function(test, mediaSource, sourceBuffer, duration, subType, segmentInfo)
313           {
314               var start = Math.max(segmentInfo.media[0].timev, segmentInfo.media[0].timea).toFixed(3);
315               var expectations = {
316                 webm: "{ [" + start + ", 1.029) }",
317                 mp4: "{ [" + start + ", 1.022) }",
318               };
319
320               removeAndCheckBufferedRanges(test, mediaSource, sourceBuffer, 1, Number.POSITIVE_INFINITY, expectations[subType]);
321           }, "Test removing the end of appended data.");
322         </script>
323     </body>
324 </html>