4 <script src=
"/w3c/resources/testharness.js"></script>
5 <script src=
"/w3c/resources/testharnessreport.js"></script>
6 <script src=
"mediasource-util.js"></script>
7 <link rel='stylesheet' href='/w3c/resources/testharness.css'
>
12 function twoMediaElementTest(testFunction
, description
)
14 media_test(function(test
)
16 var firstMediaTag
= document
.createElement('video');
17 var secondMediaTag
= document
.createElement('video');
18 document
.body
.appendChild(firstMediaTag
);
19 document
.body
.appendChild(secondMediaTag
);
21 // Overload done() so that elements added to the document can be
23 var removeMediaElements
= true;
24 var oldTestDone
= test
.done
.bind(test
);
25 test
.done = function()
27 if (removeMediaElements
) {
28 document
.body
.removeChild(secondMediaTag
);
29 document
.body
.removeChild(firstMediaTag
);
30 removeMediaElements
= false;
35 testFunction(test
, firstMediaTag
, secondMediaTag
);
39 twoMediaElementTest(function(test
, firstMediaTag
, secondMediaTag
)
41 // When attachment of mediaSource to two MediaElements is done
42 // without an intervening stable state, exactly one of the two
43 // MediaElements should successfully attach, and the other one
44 // should get error event due to mediaSource already in 'open'
46 var mediaSource
= new MediaSource();
47 var mediaSourceURL
= URL
.createObjectURL(mediaSource
);
48 var gotSourceOpen
= false;
50 var doneIfFinished
= test
.step_func(function()
52 if (gotSourceOpen
&& gotError
)
55 var errorHandler
= test
.step_func(function(e
)
57 firstMediaTag
.removeEventListener('error', errorHandler
);
58 secondMediaTag
.removeEventListener('error', errorHandler
);
60 var eventTarget
= e
.target
;
62 if (eventTarget
== firstMediaTag
) {
63 otherTarget
= secondMediaTag
;
65 assert_equals(eventTarget
, secondMediaTag
, 'Error target check');
66 otherTarget
= firstMediaTag
;
69 assert_true(eventTarget
.error
!= null, 'Error state on one tag');
70 assert_equals(eventTarget
.error
.code
, MediaError
.MEDIA_ERR_SRC_NOT_SUPPORTED
, 'Expected error code');
71 assert_equals(otherTarget
.error
, null, 'No error on other tag');
73 assert_equals(eventTarget
.networkState
, HTMLMediaElement
.NETWORK_NO_SOURCE
,
74 'Tag with error state networkState');
75 assert_equals(otherTarget
.networkState
, HTMLMediaElement
.NETWORK_LOADING
,
76 'Tag without error state networkState');
82 test
.expectEvent(mediaSource
, 'sourceopen', 'An attachment succeeded');
83 firstMediaTag
.addEventListener('error', errorHandler
);
84 secondMediaTag
.addEventListener('error', errorHandler
);
86 firstMediaTag
.src
= mediaSourceURL
;
87 secondMediaTag
.src
= mediaSourceURL
;
89 test
.waitForExpectedEvents(function()
91 assert_equals(mediaSource
.readyState
, 'open', 'Source is opened');
95 }, 'Test exactly one succeeds when two MediaElements attach to same MediaSource');
97 mediasource_test(function(test
, mediaElement
, mediaSource
) {
98 assert_equals(mediaSource
.readyState
, 'open', 'Source open');
99 // Set the tag's src attribute. This should close mediaSource,
100 // reattach it to the tag, and initiate source reopening.
101 test
.expectEvent(mediaSource
, 'sourceopen', 'Source attached again');
102 mediaElement
.src
= URL
.createObjectURL(mediaSource
);
103 assert_equals(mediaSource
.readyState
, 'closed', 'Source closed');
105 test
.waitForExpectedEvents(function()
107 assert_equals(mediaSource
.readyState
, 'open', 'Source reopened');
110 }, 'Test that MediaSource can reattach if closed first');