1 <!DOCTYPE html>
2 <html>
3 <head>
4 <script src="/media-resources/video-test.js"></script>
5 <script src="/media/resources/media-source/webm/segment-info.js"></script>
6 <script src="media-source.js"></script>
7 <script>
8 var segmentHelper = new MediaSourceTest.SegmentHelper(WebMSegmentInfo.testWebM);
9 var defaultSourceMimetype = segmentHelper.segmentInfo.type;
10
11 function expectExceptionOnAddId(videoTag, id, type, error)
12 {
13 try {
14 videoTag.webkitSourceAddId(id, type);
15 failTest("Expected an exception");
16 } catch (e) {
17 if (!(e.code == error)) {
18 failTest("Unexpected exception " + e);
19 throw e;
20 }
21 consoleWrite("Got expected exception " + e);
22 }
23 }
24
25 function expectExceptionOnRemoveId(videoTag, id, error)
26 {
27 try {
28 videoTag.webkitSourceRemoveId(id);
29 failTest("Expected an exception");
30 } catch (e) {
31 if (!(e.code == error)) {
32 failTest("Unexpected exception " + e);
33 throw e;
34 }
35 consoleWrite("Got expected exception " + e);
36 }
37 }
38
39 function expectExceptionOnAppend(videoTag, id, buf, error)
40 {
41 try {
42 videoTag.webkitSourceAppend(id, buf);
43 failTest("Expected an exception");
44 } catch (e) {
45 if (!(e.code == error)) {
46 failTest("Unexpected exception " + e);
47 throw e;
48 }
49 consoleWrite("Got expected exception " + e);
50 }
51 }
52
53 function testAddIdWhileClosed(videoTag)
54 {
55 consoleWrite("Test adding an ID while closed.");
56 expectExceptionOnAddId(videoTag, "123", defaultSourceMimetype, DOMException.INVALID_STATE_ERR);
57 }
58
59 function testAddIdFailureCases(runNextTestCase, videoTag)
60 {
61 consoleWrite("Test empty ID case");
62 expectExceptionOnAddId(videoTag, "", defaultSourceMimetype, DOMException.INVALID_ACCESS_ERR);
63
64 videoTag.webkitSourceAddId("123", defaultSourceMimetype);
65
66 consoleWrite("Test adding the same ID again.");
67 expectExceptionOnAddId(videoTag, "123", defaultSourceMimetype, DOMException.INVALID_STATE_ERR);
68 videoTag.webkitSourceRemoveId("123");
69
70 consoleWrite("Test empty type.");
71 expectExceptionOnAddId(videoTag, "234", "", DOMException.INVALID_ACCESS_ERR);
72
73 consoleWrite("Test an unsupported type.");
74 expectExceptionOnAddId(videoTag, "234", "audio/x-unsupported-format", DOMException.NOT_SUPPORTED_ERR);
75
76 consoleWrite("Test a supported type with an unsupported codec.");
77 expectExceptionOnAddId(videoTag, "234", "video/webm; codecs=\"vp8, speex\"", DOMException.NOT_SUPPORTED_ERR);
78
79 consoleWrite("Test reaching sourceID limit.");
80 var reachedIdLimit = false;
81
82 // The 20 here is an arbitrary upper limit to make sure the test terminates. This test
83 // assumes that implementations won't support more than 20 sourceID's simultaneously.
84 for (var i = 0; i < 20; ++i) {
85 var sourceID = "sourceID-" + i;
86 try {
87 videoTag.webkitSourceAddId(sourceID, defaultSourceMimetype);
88 } catch(e) {
89 if (e.code != DOMException.QUOTA_EXCEEDED_ERR) {
90 failTest("Unexpected exception " + e);
91 throw e;
92 }
93 reachedIdLimit = true;
94 break;
95 }
96 }
97
98 if (!reachedIdLimit) {
99 failTest("Failed to reach SourceID limit.");
100 return;
101 }
102
103 consoleWrite("Test that SourceIDs can't be added while in the ended state.");
104 videoTag.webkitSourceEndOfStream(videoTag.EOS_NO_ERROR);
105 expectExceptionOnAddId(videoTag, "123", defaultSourceMimetype, DOMException.INVALID_STATE_ERR);
106
107 runNextTestCase();
108 }
109
110 function testRemoveEmptyId(runNextTestCase, videoTag)
111 {
112 consoleWrite("Test empty ID case");
113 expectExceptionOnRemoveId(videoTag, "", DOMException.INVALID_ACCESS_ERR);
114
115 runNextTestCase();
116 }
117
118 function testRemoveNonexistentId(runNextTestCase, videoTag)
119 {
120 consoleWrite("Test removing an ID that was never added.");
121 expectExceptionOnRemoveId(videoTag, "345", DOMException.SYNTAX_ERR);
122
123 runNextTestCase();
124 }
125
126 function testRemoveAgain(runNextTestCase, videoTag)
127 {
128 consoleWrite("Test removing an ID that was already removed.");
129 videoTag.webkitSourceAddId("123", defaultSourceMimetype);
130 videoTag.webkitSourceRemoveId("123");
131 expectExceptionOnRemoveId(videoTag, "123", DOMException.SYNTAX_ERR);
132
133 runNextTestCase();
134 }
135
136 function testRemoveIdAfterEnded(runNextTestCase, videoTag)
137 {
138 consoleWrite("Test that an ID can be removed while in the ended state.");
139 videoTag.webkitSourceAddId("123", defaultSourceMimetype);
140 videoTag.webkitSourceEndOfStream(videoTag.EOS_NO_ERROR);
141 videoTag.webkitSourceRemoveId("123");
142
143 runNextTestCase();
144 }
145
146 function testAddIdAfterRemoving(runNextTestCase, videoTag)
147 {
148 consoleWrite("Test that an ID can be added again after it is removed.");
149 videoTag.webkitSourceAddId("123", defaultSourceMimetype);
150 videoTag.webkitSourceRemoveId("123");
151
152 try {
153 videoTag.webkitSourceAddId("123", defaultSourceMimetype);
154 } catch (e) {
155 consoleWrite("Unexpected exception: " + e);
156 }
157
158 runNextTestCase();
159 }
160
161 function testAppendFailureCases(runNextTestCase, videoTag)
162 {
163 var initSegment = segmentHelper.initSegment;
164 var mediaSegment = segmentHelper.mediaSegments[0];
165
166 videoTag.webkitSourceAddId("123", defaultSourceMimetype);
167
168 consoleWrite("Test append with empty ID.");
169 expectExceptionOnAppend(videoTag, "", initSegment, DOMException.INVALID_ACCESS_ERR);
170
171 consoleWrite("Test append with an invalid ID");
172 expectExceptionOnAppend(videoTag, "234", initSegment, DOMException.SYNTAX_ERR);
173
174 consoleWrite("Test append with a null buffer.");
175 expectExceptionOnAppend(videoTag, "123", null, DOMException.INVALID_ACCESS_ERR);
176
177 consoleWrite("Test a successful append.");
178 videoTag.webkitSourceAppend("123", initSegment);
179
180 videoTag.webkitSourceRemoveId("123");
181 expectExceptionOnAppend(videoTag, "123", mediaSegment, DOMException.SYNTAX_ERR);
182
183 runNextTestCase();
184 }
185
186 function onLoad()
187 {
188 findMediaElement();
189
190 waitForEvent('loadstart');
191 waitForEvent('webkitsourceopen');
192
193 segmentHelper.init(video, function(success)
194 {
195 if (!success) {
196 failTest("Failed to load segment data");
197 return;
198 }
199
200 testAddIdWhileClosed(video);
201
202 var testCases = [
203 testAddIdFailureCases,
204 testRemoveEmptyId,
205 testRemoveNonexistentId,
206 testRemoveAgain,
207 testRemoveIdAfterEnded,
208 testAddIdAfterRemoving,
209 testAppendFailureCases,
210 ];
211
212 MediaSourceTest.startSourceOpenTesting(video, testCases);
213 });
214 }
215 </script>
216 </head>
217 <body onload="onLoad()">
218 <video> </video>
219 <p>Tests webkitSourceAddId() & webkitSourceRemoveId() methods</p>
220 </body>
221 </html>