summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/media-source/mediasource-append-buffer.html
blob: 750ccaf456f7f1348d2d0844798513b05e4b1136 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
<!DOCTYPE html>
<!-- Copyright © 2016 Chromium authors and World Wide Web Consortium, (Massachusetts Institute of Technology, ERCIM, Keio University, Beihang). -->
<html>
    <head>
        <title>SourceBuffer.appendBuffer() test cases</title>
        <meta name="timeout" content="long">
        <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>
          mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
          {
              mediaElement.addEventListener('error', test.unreached_func("Unexpected event 'error'"));

              test.expectEvent(sourceBuffer, "updatestart", "Append started.");
              test.expectEvent(sourceBuffer, "update", "Append success.");
              test.expectEvent(sourceBuffer, "updateend", "Append ended.");
              sourceBuffer.appendBuffer(mediaData);

              assert_true(sourceBuffer.updating, "updating attribute is true");

              test.waitForExpectedEvents(function()
              {
                 assert_false(sourceBuffer.updating, "updating attribute is false");
                 test.done();
              });
          }, "Test SourceBuffer.appendBuffer() event dispatching.");

          mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
          {
              mediaElement.addEventListener('error', test.unreached_func("Unexpected event 'error'"));

              test.expectEvent(sourceBuffer, "updatestart", "Append started.");
              test.expectEvent(sourceBuffer, "update", "Append success.");
              test.expectEvent(sourceBuffer, "updateend", "Append ended.");
              sourceBuffer.appendBuffer(mediaData);

              assert_true(sourceBuffer.updating, "updating attribute is true");

              assert_throws_dom("InvalidStateError",
                  function() { sourceBuffer.appendBuffer(mediaData); },
                  "appendBuffer() throws an exception there is a pending append.");

              assert_true(sourceBuffer.updating, "updating attribute is true");

              test.waitForExpectedEvents(function()
              {
                  assert_false(sourceBuffer.updating, "updating attribute is false");
                  test.done();
              });
          }, "Test SourceBuffer.appendBuffer() call during a pending appendBuffer().");

          mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
          {
              mediaElement.addEventListener('error', test.unreached_func("Unexpected event 'error'"));

              test.expectEvent(sourceBuffer, "updatestart", "Append started.");
              test.expectEvent(sourceBuffer, "abort", "Append aborted.");
              test.expectEvent(sourceBuffer, "updateend", "Append ended.");
              sourceBuffer.appendBuffer(mediaData);

              assert_true(sourceBuffer.updating, "updating attribute is true");

              sourceBuffer.abort();

              assert_false(sourceBuffer.updating, "updating attribute is false");

              test.waitForExpectedEvents(function()
              {
                  assert_false(sourceBuffer.updating, "updating attribute is false");
                  test.done();
              });
          }, "Test SourceBuffer.abort() call during a pending appendBuffer().");

          mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
          {
              mediaElement.addEventListener('error', test.unreached_func("Unexpected event 'error'"));

              test.expectEvent(sourceBuffer, "updatestart", "Append started.");
              test.expectEvent(sourceBuffer, "update", "Append success.");
              test.expectEvent(sourceBuffer, "updateend", "Append ended.");
              sourceBuffer.appendBuffer(mediaData);
              assert_true(sourceBuffer.updating, "updating attribute is true");

              test.waitForExpectedEvents(function()
              {
                  assert_false(sourceBuffer.updating, "updating attribute is false");

                  test.expectEvent(mediaSource, "sourceended", "MediaSource sourceended event");
                  mediaSource.endOfStream();
                  assert_equals(mediaSource.readyState, "ended", "MediaSource readyState is 'ended'");
              });

              test.waitForExpectedEvents(function()
              {
                  assert_equals(mediaSource.readyState, "ended", "MediaSource readyState is 'ended'");

                  test.expectEvent(mediaSource, "sourceopen", "MediaSource sourceopen event");
                  test.expectEvent(sourceBuffer, "updatestart", "Append started.");
                  test.expectEvent(sourceBuffer, "update", "Append success.");
                  test.expectEvent(sourceBuffer, "updateend", "Append ended.");
                  sourceBuffer.appendBuffer(mediaData);

                  assert_equals(mediaSource.readyState, "open", "MediaSource readyState is 'open'");
                  assert_true(sourceBuffer.updating, "updating attribute is true");
              });

              test.waitForExpectedEvents(function()
              {
                  assert_equals(mediaSource.readyState, "open", "MediaSource readyState is 'open'");
                  assert_false(sourceBuffer.updating, "updating attribute is false");
                  test.done();
              });
          }, "Test SourceBuffer.appendBuffer() triggering an 'ended' to 'open' transition.");

          mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
          {
              test.expectEvent(sourceBuffer, "updatestart", "Append started.");
              test.expectEvent(sourceBuffer, "update", "Append success.");
              test.expectEvent(sourceBuffer, "updateend", "Append ended.");
              sourceBuffer.appendBuffer(mediaData);
              assert_true(sourceBuffer.updating, "updating attribute is true");

              test.waitForExpectedEvents(function()
              {
                  assert_false(sourceBuffer.updating, "updating attribute is false");

                  test.expectEvent(mediaSource, "sourceended", "MediaSource sourceended event");
                  mediaSource.endOfStream();
                  assert_equals(mediaSource.readyState, "ended", "MediaSource readyState is 'ended'");
              });

              test.waitForExpectedEvents(function()
              {
                  assert_equals(mediaSource.readyState, "ended", "MediaSource readyState is 'ended'");

                  test.expectEvent(mediaSource, "sourceopen", "MediaSource sourceopen event");
                  test.expectEvent(sourceBuffer, "updatestart", "Append started.");
                  test.expectEvent(sourceBuffer, "update", "Append success.");
                  test.expectEvent(sourceBuffer, "updateend", "Append ended.");
                  sourceBuffer.appendBuffer(new Uint8Array(0));

                  assert_equals(mediaSource.readyState, "open", "MediaSource readyState is 'open'");
                  assert_true(sourceBuffer.updating, "updating attribute is true");
              });

              test.waitForExpectedEvents(function()
              {
                  assert_equals(mediaSource.readyState, "open", "MediaSource readyState is 'open'");
                  assert_false(sourceBuffer.updating, "updating attribute is false");
                  test.done();
              });
          }, "Test zero byte SourceBuffer.appendBuffer() call triggering an 'ended' to 'open' transition.");

          mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
          {
              mediaElement.addEventListener('error', test.unreached_func("Unexpected event 'error'"));

              test.expectEvent(sourceBuffer, "updatestart", "Append started.");
              test.expectEvent(sourceBuffer, "abort", "Append aborted.");
              test.expectEvent(sourceBuffer, "updateend", "Append ended.");
              sourceBuffer.appendBuffer(mediaData);

              assert_true(sourceBuffer.updating, "updating attribute is true");
              assert_equals(mediaSource.activeSourceBuffers.length, 0, "activeSourceBuffers.length");

              test.expectEvent(mediaSource.sourceBuffers, "removesourcebuffer", "sourceBuffers");
              mediaSource.removeSourceBuffer(sourceBuffer);

              assert_false(sourceBuffer.updating, "updating attribute is false");

              assert_throws_dom("InvalidStateError",
                  function() { sourceBuffer.appendBuffer(mediaData); },
                  "appendBuffer() throws an exception because it isn't attached to the mediaSource anymore.");

              test.waitForExpectedEvents(function()
              {
                  assert_false(sourceBuffer.updating, "updating attribute is false");
                  test.done();
              });
          }, "Test MediaSource.removeSourceBuffer() call during a pending appendBuffer().");

          mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
          {
              mediaElement.addEventListener('error', test.unreached_func("Unexpected event 'error'"));

              test.expectEvent(sourceBuffer, "updatestart", "Append started.");
              test.expectEvent(sourceBuffer, "updateend", "Append ended.");
              sourceBuffer.appendBuffer(mediaData);

              assert_true(sourceBuffer.updating, "updating attribute is true");

              assert_throws_dom("InvalidStateError",
                  function() { mediaSource.duration = 1.0; },
                  "set duration throws an exception when updating attribute is true.");

              test.waitForExpectedEvents(function()
              {
                  assert_false(sourceBuffer.updating, "updating attribute is false");
                  test.done();
              });
          }, "Test set MediaSource.duration during a pending appendBuffer() for one of its SourceBuffers.");

          mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
          {
              mediaElement.addEventListener("error", test.unreached_func("Unexpected event 'error'"));
              mediaSource.addEventListener("sourceended", test.unreached_func("Unexpected event 'sourceended'"));

              test.expectEvent(sourceBuffer, "updatestart", "Append started.");
              test.expectEvent(sourceBuffer, "updateend", "Append ended.");
              sourceBuffer.appendBuffer(mediaData);

              assert_true(sourceBuffer.updating, "updating attribute is true");

              assert_throws_dom("InvalidStateError",
                  function() { mediaSource.endOfStream(); },
                  "endOfStream() throws an exception when updating attribute is true.");

              assert_equals(mediaSource.readyState, "open");

              test.waitForExpectedEvents(function()
              {
                  assert_false(sourceBuffer.updating, "updating attribute is false");
                  assert_equals(mediaSource.readyState, "open");
                  test.done();
              });
          }, "Test MediaSource.endOfStream() during a pending appendBuffer() for one of its SourceBuffers.");

          mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
          {
              mediaElement.addEventListener('error', test.unreached_func("Unexpected event 'error'"));

              test.expectEvent(sourceBuffer, "updatestart", "Append started.");
              test.expectEvent(sourceBuffer, "updateend", "Append ended.");
              sourceBuffer.appendBuffer(mediaData);

              assert_true(sourceBuffer.updating, "updating attribute is true");

              assert_throws_dom("InvalidStateError",
                  function() { sourceBuffer.timestampOffset = 10.0; },
                  "set timestampOffset throws an exception when updating attribute is true.");

              test.waitForExpectedEvents(function()
              {
                  assert_false(sourceBuffer.updating, "updating attribute is false");
                  test.done();
              });
          }, "Test set SourceBuffer.timestampOffset during a pending appendBuffer().");

          mediasource_test(function(test, mediaElement, mediaSource)
          {
              var sourceBuffer = mediaSource.addSourceBuffer(MediaSourceUtil.VIDEO_ONLY_TYPE);

              test.expectEvent(sourceBuffer, "updatestart", "Append started.");
              test.expectEvent(sourceBuffer, "update", "Append success.");
              test.expectEvent(sourceBuffer, "updateend", "Append ended.");
              sourceBuffer.appendBuffer(new Uint8Array(0));

              assert_true(sourceBuffer.updating, "updating attribute is true");

              test.waitForExpectedEvents(function()
              {
                  assert_false(sourceBuffer.updating, "updating attribute is false");
                  test.done();
              });
          }, "Test appending an empty ArrayBufferView.");

          mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
          {
              test.expectEvent(sourceBuffer, "updatestart", "Append started.");
              test.expectEvent(sourceBuffer, "update", "Append success.");
              test.expectEvent(sourceBuffer, "updateend", "Append ended.");

              var arrayBufferView = new Uint8Array(mediaData);

              assert_equals(arrayBufferView.length, mediaData.length, "arrayBufferView.length before transfer.");

              // Send the buffer as in a message so it gets neutered.
              window.postMessage( "test", "*", [arrayBufferView.buffer]);

              assert_equals(arrayBufferView.length, 0, "arrayBufferView.length after transfer.");

              sourceBuffer.appendBuffer(arrayBufferView);

              assert_true(sourceBuffer.updating, "updating attribute is true");

              test.waitForExpectedEvents(function()
              {
                 assert_false(sourceBuffer.updating, "updating attribute is false");
                 test.done();
              });
          }, "Test appending a neutered ArrayBufferView.");

          mediasource_test(function(test, mediaElement, mediaSource)
          {
              var sourceBuffer = mediaSource.addSourceBuffer(MediaSourceUtil.VIDEO_ONLY_TYPE);

              test.expectEvent(sourceBuffer, "updatestart", "Append started.");
              test.expectEvent(sourceBuffer, "update", "Append success.");
              test.expectEvent(sourceBuffer, "updateend", "Append ended.");
              sourceBuffer.appendBuffer(new ArrayBuffer(0));

              assert_true(sourceBuffer.updating, "updating attribute is true");

              test.waitForExpectedEvents(function()
              {
                  assert_false(sourceBuffer.updating, "updating attribute is false");
                  test.done();
              });
          }, "Test appending an empty ArrayBuffer.");

          mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
          {
              test.expectEvent(sourceBuffer, "updatestart", "Append started.");
              test.expectEvent(sourceBuffer, "update", "Append success.");
              test.expectEvent(sourceBuffer, "updateend", "Append ended.");

              var arrayBuffer = mediaData.buffer.slice(0);

              assert_equals(arrayBuffer.byteLength, mediaData.buffer.byteLength, "arrayBuffer.byteLength before transfer.");

              // Send the buffer as in a message so it gets neutered.
              window.postMessage( "test", "*", [arrayBuffer]);

              assert_equals(arrayBuffer.byteLength, 0, "arrayBuffer.byteLength after transfer.");

              sourceBuffer.appendBuffer(arrayBuffer);

              assert_true(sourceBuffer.updating, "updating attribute is true");

              test.waitForExpectedEvents(function()
              {
                 assert_false(sourceBuffer.updating, "updating attribute is false");
                 test.done();
              });
          }, "Test appending a neutered ArrayBuffer.");

          mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
          {
              var initSegment = MediaSourceUtil.extractSegmentData(mediaData, segmentInfo.init);
              var halfIndex = (initSegment.length + 1) / 2;
              var partialInitSegment = initSegment.subarray(0, halfIndex);
              var remainingInitSegment = initSegment.subarray(halfIndex);
              var mediaSegment = MediaSourceUtil.extractSegmentData(mediaData, segmentInfo.media[0]);

              test.expectEvent(sourceBuffer, "updateend", "partialInitSegment append ended.");
              sourceBuffer.appendBuffer(partialInitSegment);

              test.waitForExpectedEvents(function()
              {
                  assert_equals(mediaElement.readyState, mediaElement.HAVE_NOTHING);
                  assert_equals(mediaSource.duration, Number.NaN);
                  test.expectEvent(sourceBuffer, "updateend", "remainingInitSegment append ended.");
                  test.expectEvent(mediaElement, "loadedmetadata", "loadedmetadata event received.");
                  sourceBuffer.appendBuffer(remainingInitSegment);
              });

              test.waitForExpectedEvents(function()
              {
                  assert_equals(mediaElement.readyState, mediaElement.HAVE_METADATA);
                  assert_equals(mediaSource.duration, segmentInfo.duration);
                  test.expectEvent(sourceBuffer, "updateend", "mediaSegment append ended.");
                  test.expectEvent(mediaElement, "loadeddata", "loadeddata fired.");
                  sourceBuffer.appendBuffer(mediaSegment);
              });

              test.waitForExpectedEvents(function()
              {
                  assert_greater_than_equal(mediaElement.readyState, mediaElement.HAVE_CURRENT_DATA);
                  assert_equals(sourceBuffer.updating, false);
                  assert_equals(mediaSource.readyState, "open");
                  test.done();
              });
          }, "Test appendBuffer with partial init segments.");

          mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
          {
              var initSegment = MediaSourceUtil.extractSegmentData(mediaData, segmentInfo.init);
              var mediaSegment = MediaSourceUtil.extractSegmentData(mediaData, segmentInfo.media[0]);
              var halfIndex = (mediaSegment.length + 1) / 2;
              var partialMediaSegment = mediaSegment.subarray(0, halfIndex);
              var remainingMediaSegment = mediaSegment.subarray(halfIndex);

              test.expectEvent(sourceBuffer, "updateend", "InitSegment append ended.");
              test.expectEvent(mediaElement, "loadedmetadata", "loadedmetadata done.");
              sourceBuffer.appendBuffer(initSegment);

              test.waitForExpectedEvents(function()
              {
                  assert_equals(mediaElement.readyState, mediaElement.HAVE_METADATA);
                  assert_equals(mediaSource.duration, segmentInfo.duration);
                  test.expectEvent(sourceBuffer, "updateend", "partial media segment append ended.");
                  sourceBuffer.appendBuffer(partialMediaSegment);
              });

              test.waitForExpectedEvents(function()
              {
                  test.expectEvent(sourceBuffer, "updateend", "mediaSegment append ended.");
                  test.expectEvent(mediaElement, "loadeddata", "loadeddata fired.");
                  sourceBuffer.appendBuffer(remainingMediaSegment);
              });

              test.waitForExpectedEvents(function()
              {
                  assert_greater_than_equal(mediaElement.readyState, mediaElement.HAVE_CURRENT_DATA);
                  assert_equals(mediaSource.readyState, "open");
                  assert_equals(sourceBuffer.updating, false);
                  test.done();
              });
          }, "Test appendBuffer with partial media segments.");

          mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
          {
              var initSegment = MediaSourceUtil.extractSegmentData(mediaData, segmentInfo.init);
              var mediaSegment = MediaSourceUtil.extractSegmentData(mediaData, segmentInfo.media[0]);

              assert_equals(mediaElement.readyState, mediaElement.HAVE_NOTHING);
              assert_equals(mediaSource.duration, Number.NaN);

              // readyState is changing as per the Initialization Segment Received algorithm.
              var loadedmetadataCalled = false;
              mediaElement.addEventListener("loadedmetadata", function metadata(e) {
                 loadedmetadataCalled = true;
                 e.target.removeEventListener(e.type, metadata);
              });
              sourceBuffer.addEventListener("updateend", test.step_func(function updateend(e) {
                 assert_true(loadedmetadataCalled);
                 assert_equals(mediaElement.readyState, mediaElement.HAVE_METADATA);
                 e.target.removeEventListener(e.type, updateend);
              }));
              test.expectEvent(sourceBuffer, "updateend", "remainingInitSegment append ended.");
              test.expectEvent(mediaElement, "loadedmetadata", "loadedmetadata event received.");
              sourceBuffer.appendBuffer(initSegment);

              test.waitForExpectedEvents(function()
              {
                  assert_equals(mediaElement.readyState, mediaElement.HAVE_METADATA);
                  assert_equals(mediaSource.duration, segmentInfo.duration);
                  // readyState is changing as per the Coded Frame Processing algorithm.
                  var loadeddataCalled = false;
                  mediaElement.addEventListener("loadeddata", function loadeddata(e) {
                      loadeddataCalled = true;
                      e.target.removeEventListener(e.type, loadeddata);
                  });
                  sourceBuffer.addEventListener("updateend", test.step_func(function updateend(e) {
                      assert_true(loadeddataCalled);
                      assert_greater_than_equal(mediaElement.readyState, mediaElement.HAVE_CURRENT_DATA);
                      e.target.removeEventListener(e.type, updateend);
                  }));
                  test.expectEvent(sourceBuffer, "updateend", "mediaSegment append ended.");
                  test.expectEvent(mediaElement, "loadeddata", "loadeddata fired.");
                  sourceBuffer.appendBuffer(mediaSegment);
              });

              test.waitForExpectedEvents(function()
              {
                  assert_greater_than_equal(mediaElement.readyState, mediaElement.HAVE_CURRENT_DATA);
                  assert_equals(sourceBuffer.updating, false);
                  assert_equals(mediaSource.readyState, "open");
                  test.done();
              });
          }, "Test appendBuffer events order.");

          mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
          {
              var initSegment = MediaSourceUtil.extractSegmentData(mediaData, segmentInfo.init);
              var partialInitSegment = initSegment.subarray(0, initSegment.length / 2);
              var mediaSegment = MediaSourceUtil.extractSegmentData(mediaData, segmentInfo.media[0]);

              test.expectEvent(sourceBuffer, "updateend", "partialInitSegment append ended.");
              sourceBuffer.appendBuffer(partialInitSegment);

              test.waitForExpectedEvents(function()
              {
                  // Call abort to reset the parser.
                  sourceBuffer.abort();

                  // Append the full intiialization segment.
                  test.expectEvent(sourceBuffer, "updateend", "initSegment append ended.");
                  sourceBuffer.appendBuffer(initSegment);
              });

              test.waitForExpectedEvents(function()
              {
                  test.expectEvent(sourceBuffer, "updateend", "mediaSegment append ended.");
                  test.expectEvent(mediaElement, "loadeddata", "loadeddata fired.");
                  sourceBuffer.appendBuffer(mediaSegment);
              });

              test.waitForExpectedEvents(function()
              {
                  test.done();
              });
          }, "Test abort in the middle of an initialization segment.");

          mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
          {
              test.expectEvent(mediaSource.sourceBuffers, "removesourcebuffer", "SourceBuffer removed.");
              mediaSource.removeSourceBuffer(sourceBuffer);
              test.waitForExpectedEvents(function()
              {
                  assert_throws_dom("InvalidStateError",
                      function() { sourceBuffer.abort(); },
                      "sourceBuffer.abort() throws an exception for InvalidStateError.");

                  test.done();
              });
          }, "Test abort after removing sourcebuffer.");

          mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
          {
              var initSegment = MediaSourceUtil.extractSegmentData(mediaData, segmentInfo.init);
              var mediaSegment = MediaSourceUtil.extractSegmentData(mediaData, segmentInfo.media[0]);

              test.expectEvent(sourceBuffer, "updateend", "initSegment append ended.");
              sourceBuffer.appendBuffer(initSegment);

              test.waitForExpectedEvents(function()
              {
                  assert_equals(mediaSource.readyState, "open", "readyState is open after init segment appended.");
                  test.expectEvent(sourceBuffer, "updateend", "mediaSegment append ended.");
                  sourceBuffer.appendBuffer(mediaSegment);
              });

              test.waitForExpectedEvents(function()
              {
                  assert_equals(sourceBuffer.buffered.length, 1, "sourceBuffer has a buffered range");
                  assert_equals(mediaSource.readyState, "open", "readyState is open after media segment appended.");
                  test.expectEvent(mediaSource, "sourceended", "source ended");
                  mediaSource.endOfStream();
              });

              test.waitForExpectedEvents(function()
              {
                  assert_equals(mediaSource.readyState, "ended", "readyState is ended.");
                  assert_throws_dom("InvalidStateError",
                      function() { sourceBuffer.abort(); },
                      "sourceBuffer.abort() throws an exception for InvalidStateError.");
                  test.done();
              });

          }, "Test abort after readyState is ended following init segment and media segment.");

          mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
          {
              test.expectEvent(sourceBuffer, "updatestart", "Append started.");
              test.expectEvent(sourceBuffer, "updateend", "Append ended.");
              sourceBuffer.appendWindowStart = 1;
              sourceBuffer.appendWindowEnd = 100;
              sourceBuffer.appendBuffer(mediaData);

              test.waitForExpectedEvents(function()
              {
                  assert_false(sourceBuffer.updating, "updating attribute is false");
                  sourceBuffer.abort();
                  assert_equals(sourceBuffer.appendWindowStart, 0, "appendWindowStart is reset to 0");
                  assert_equals(sourceBuffer.appendWindowEnd, Number.POSITIVE_INFINITY,
                      "appendWindowEnd is reset to +INFINITY");
                  test.done();
              });
          }, "Test abort after appendBuffer update ends.");

          mediasource_test(function(test, mediaElement, mediaSource)
          {
              var sourceBuffer = mediaSource.addSourceBuffer(MediaSourceUtil.VIDEO_ONLY_TYPE);

              test.expectEvent(sourceBuffer, "updatestart", "Append started.");
              test.expectEvent(sourceBuffer, "update", "Append success.");
              test.expectEvent(sourceBuffer, "updateend", "Append ended.");

              assert_throws_js( TypeError,
                  function() { sourceBuffer.appendBuffer(null); },
                  "appendBuffer(null) throws an exception.");
              test.done();
          }, "Test appending null.");

          mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
          {
              mediaSource.removeSourceBuffer(sourceBuffer);

              assert_throws_dom( "InvalidStateError",
                  function() { sourceBuffer.appendBuffer(mediaData); },
                  "appendBuffer() throws an exception when called after removeSourceBuffer().");
              test.done();
          }, "Test appending after removeSourceBuffer().");

          mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
          {
              // Media elements using MSE should not fire the stalled event. See discussion at
              // https://github.com/w3c/media-source/issues/88#issuecomment-374406928
              mediaElement.addEventListener("stalled", test.unreached_func("Unexpected 'stalled' event."));

              // Prime the media element with initial appends.
              var initSegment = MediaSourceUtil.extractSegmentData(mediaData, segmentInfo.init);
              var mediaSegment = MediaSourceUtil.extractSegmentData(mediaData, segmentInfo.media[0]);
              test.expectEvent(sourceBuffer, "updateend", "initSegment append ended.");
              sourceBuffer.appendBuffer(initSegment);
              test.waitForExpectedEvents(function()
              {
                  assert_equals(mediaSource.readyState, "open", "readyState is open after init segment appended.");
                  test.expectEvent(sourceBuffer, "updateend", "mediaSegment append ended.");
                  sourceBuffer.appendBuffer(mediaSegment);
              });

              // Verify state and wait for the 'stalled' event.
              test.waitForExpectedEvents(function()
              {
                  assert_equals(sourceBuffer.buffered.length, 1, "sourceBuffer has a buffered range");
                  assert_equals(mediaSource.readyState, "open", "readyState is open after media segment appended.");

                  // Set timeout to 4 seconds. This creates an opportunity for UAs to _improperly_ fire the stalled event.
                  // For media elements doing progressive download (not MSE), stalled is thrown after ~3 seconds of the
                  // download failing to progress.
                  test.step_timeout(function() { test.done(); }, 4000);
              });
          }, "Test slow appending does not trigger stalled events.");

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