Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / http / tests / media / media-source / webkitmediasource-add-and-remove-buffers.html
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="webkitmediasource-util.js"></script>
7         <script>
8             var segmentHelper = new MediaSourceTest.SegmentHelper(WebMSegmentInfo.testWebM);
9             var defaultSourceMimetype = segmentHelper.segmentInfo.type;
10
11             function expectExceptionOnAddBuffer(type, error)
12             {
13                 try {
14                     segmentHelper.sourceBuffer = mediaSource.addSourceBuffer(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 expectExceptionOnRemoveBuffer(buffer, error)
26             {
27                 try {
28                     mediaSource.removeSourceBuffer(buffer);
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(buf, error)
40             {
41                 try {
42                     segmentHelper.sourceBuffer.append(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 testAddBufferWhileClosed(videoTag)
54             {
55                 consoleWrite("Test adding an ID while closed.");
56                 expectExceptionOnAddBuffer(defaultSourceMimetype, DOMException.INVALID_STATE_ERR);
57             }
58
59             function testAddBufferFailureCases(runNextTestCase, videoTag)
60             {
61                 consoleWrite("Test empty type.");
62                 expectExceptionOnAddBuffer("", DOMException.INVALID_ACCESS_ERR);
63
64                 consoleWrite("Test an unsupported type.");
65                 expectExceptionOnAddBuffer("audio/x-unsupported-format", DOMException.NOT_SUPPORTED_ERR);
66
67                 consoleWrite("Test a supported type with an unsupported codec.");
68                 expectExceptionOnAddBuffer("video/webm; codecs=\"vp8, speex\"", DOMException.NOT_SUPPORTED_ERR);
69
70                 consoleWrite("Test reaching sourceID limit.");
71                 var reachedIdLimit = false;
72
73                 // The 20 here is an arbitrary upper limit to make sure the test terminates. This test
74                 // assumes that implementations won't support more than 20 sourceID's simultaneously.
75                 for (var i = 0; i < 20; ++i) {
76                     try {
77                         mediaSource.addSourceBuffer(defaultSourceMimetype);
78                     } catch(e) {
79                         if (e.code != DOMException.QUOTA_EXCEEDED_ERR) {
80                             failTest("Unexpected exception " + e);
81                             throw e;
82                         }
83                         reachedIdLimit = true;
84                         break;
85                     }
86                 }
87
88                 if (!reachedIdLimit) {
89                     failTest("Failed to reach SourceID limit.");
90                     return;
91                 }
92
93                 consoleWrite("Test that SourceBuffers can't be added while in the ended state.");
94                 mediaSource.endOfStream();
95                 expectExceptionOnAddBuffer(defaultSourceMimetype, DOMException.INVALID_STATE_ERR);
96
97                 runNextTestCase();
98             }
99
100             function testRemoveNullBuffer(runNextTestCase, videoTag)
101             {
102                 consoleWrite("Test null buffer case");
103                 expectExceptionOnRemoveBuffer(null, DOMException.INVALID_ACCESS_ERR);
104
105                 runNextTestCase();
106             }
107
108             function testRemoveAgain(runNextTestCase, videoTag)
109             {
110                 consoleWrite("Test removing a buffer that was already removed.");
111                 segmentHelper.sourceBuffer = mediaSource.addSourceBuffer(defaultSourceMimetype);
112                 mediaSource.removeSourceBuffer(segmentHelper.sourceBuffer);
113                 expectExceptionOnRemoveBuffer(segmentHelper.sourceBuffer, DOMException.INVALID_STATE_ERR);
114
115                 runNextTestCase();
116             }
117
118             function testRemoveBufferAfterEnded(runNextTestCase, videoTag)
119             {
120                 consoleWrite("Test that a buffer can be removed while in the ended state.");
121                 segmentHelper.sourceBuffer = mediaSource.addSourceBuffer(defaultSourceMimetype);
122                 mediaSource.endOfStream();
123                 mediaSource.removeSourceBuffer(segmentHelper.sourceBuffer);
124
125                 runNextTestCase();
126             }
127
128             function testAddBufferAfterRemoving(runNextTestCase, videoTag)
129             {
130                 consoleWrite("Test that a buffer can be added again after it is removed.");
131                 segmentHelper.sourceBuffer = mediaSource.addSourceBuffer(defaultSourceMimetype);
132                 mediaSource.removeSourceBuffer(segmentHelper.sourceBuffer);
133
134                 try {
135                     segmentHelper.sourceBuffer = mediaSource.addSourceBuffer(defaultSourceMimetype);
136                 } catch (e) {
137                     consoleWrite("Unexpected exception: " + e);
138                 }
139
140                 runNextTestCase();
141             }
142
143             function testAppendFailureCases(runNextTestCase, videoTag)
144             {
145                 var initSegment = segmentHelper.initSegment;
146                 var mediaSegment = segmentHelper.mediaSegments[0];
147
148                 segmentHelper.sourceBuffer = mediaSource.addSourceBuffer(defaultSourceMimetype);
149
150                 consoleWrite("Test a successful append.");
151                 segmentHelper.sourceBuffer.append(initSegment);
152
153                 consoleWrite("Test append with a null buffer.");
154                 expectExceptionOnAppend(null, DOMException.INVALID_ACCESS_ERR);
155
156                 consoleWrite("Test append after buffer has been removed.");
157                 mediaSource.removeSourceBuffer(segmentHelper.sourceBuffer);
158                 expectExceptionOnAppend(initSegment, DOMException.INVALID_STATE_ERR);
159
160                 runNextTestCase();
161             }
162
163             function onLoad()
164             {
165                 findMediaElement();
166
167                 mediaSource = new WebKitMediaSource();
168                 waitForEvent('webkitsourceopen', "", false, false, mediaSource);
169
170                 segmentHelper.init(video, function(success)
171                 {
172                     if (!success) {
173                         failTest("Failed to load segment data");
174                         return;
175                     }
176
177                     testAddBufferWhileClosed(video);
178
179                     var testCases = [
180                         testAddBufferFailureCases,
181                         testRemoveNullBuffer,
182                         testRemoveAgain,
183                         testRemoveBufferAfterEnded,
184                         testAddBufferAfterRemoving,
185                         testAppendFailureCases,
186                     ];
187
188                     MediaSourceTest.startSourceOpenTesting(video, testCases);
189                 });
190             }
191         </script>
192     </head>
193     <body onload="onLoad()">
194         <video> </video>
195         <p>Tests webkitSourceAddId() &amp; webkitSourceRemoveId() methods</p>
196     </body>
197 </html>