Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / http / tests / media / media-source / mediasource-util.js
1 (function(window) {
2     // Set the testharness.js timeout to 120 seconds so that it is higher than
3     // the LayoutTest timeout. This prevents testharness.js from prematurely
4     // terminating tests and allows the LayoutTest runner to control when to
5     // timeout the test.
6     // FIXME: Change this to use explicit_timeout instead once /resources/testharness.js
7     //        is updated to a more recent version.
8     setup({ timeout: 120000 });
9
10     var SEGMENT_INFO_LIST = [
11         {
12             url: '/media/resources/media-source/webm/test.webm',
13             type: 'video/webm; codecs="vp8, vorbis"',
14             // FIXME: Get the init segment duration fixed to match duration after append.
15             //        See http://crbug.com/354284.
16             durationInInitSegment: 6.042,
17             duration: 6.051,
18             // Supports jagged-ended stream end timestamps with some less than duration:
19             bufferedRangeEndBeforeEndOfStream: 6.040,
20             init: { offset: 0, size: 4357 },
21             media: [
22                 {  offset: 4357, size: 11830, timecode: 0 },
23                 {  offset: 16187, size: 12588, timecode: 0.385 },
24                 {  offset: 28775, size: 14588, timecode: 0.779 },
25                 {  offset: 43363, size: 13023, timecode: 1.174 },
26                 {  offset: 56386, size: 13127, timecode: 1.592 },
27                 {  offset: 69513, size: 14456, timecode: 1.987 },
28                 {  offset: 83969, size: 13458, timecode: 2.381 },
29                 {  offset: 97427, size: 14566, timecode: 2.776 },
30                 {  offset: 111993, size: 13201, timecode: 3.171 },
31                 {  offset: 125194, size: 14061, timecode: 3.566 },
32                 {  offset: 139255, size: 15353, timecode: 3.96 },
33                 {  offset: 154608, size: 13618, timecode: 4.378 },
34                 {  offset: 168226, size: 15094, timecode: 4.773 },
35                 {  offset: 183320, size: 13069, timecode: 5.168 },
36                 {  offset: 196389, size: 13788, timecode: 5.563 },
37                 {  offset: 210177, size: 9009, timecode: 5.957 },
38             ],
39         },
40         {
41             url: '/media/resources/media-source/mp4/test.mp4',
42             type: 'video/mp4; codecs="mp4a.40.2, avc1.4D401E"',
43             durationInInitSegment: 6.0368,
44             duration: 6.0424,
45             bufferedRangeEndBeforeEndOfStream: 6.0368,
46             init: { offset: 0, size: 1178 },
47             media: [
48                 {  offset: 1246, size: 23828, timecode: 0 },
49                 {  offset: 25142, size: 25394, timecode: 0.797 },
50                 {  offset: 50604, size: 24761, timecode: 1.594 },
51                 {  offset: 75433, size: 25138, timecode: 2.390 },
52                 {  offset: 100639, size: 22935, timecode: 3.187 },
53                 {  offset: 123642, size: 24995, timecode: 3.984},
54                 {  offset: 148637, size: 24968, timecode: 4.781 },
55                 {  offset: 173689, size: 19068, timecode: 5.578 },
56                 {  offset: 192757, size: 200, timecode: 5.619 },
57             ],
58         }
59     ];
60     EventExpectationsManager = function(test)
61     {
62         this.test_ = test;
63         this.eventTargetList_ = [];
64         this.waitCallbacks_ = [];
65     };
66
67     EventExpectationsManager.prototype.expectEvent = function(object, eventName, description)
68     {
69         var eventInfo = { 'target': object, 'type': eventName, 'description': description};
70         var expectations = this.getExpectations_(object);
71         expectations.push(eventInfo);
72
73         var t = this;
74         var waitHandler = this.test_.step_func(this.handleWaitCallback_.bind(this));
75         var eventHandler = this.test_.step_func(function(event)
76         {
77             object.removeEventListener(eventName, eventHandler);
78             var expected = expectations[0];
79             assert_equals(event.target, expected.target, "Event target match.");
80             assert_equals(event.type, expected.type, "Event types match.");
81             assert_equals(eventInfo.description, expected.description, "Descriptions match for '" +  event.type + "'.");
82
83             expectations.shift(1);
84             if (t.waitCallbacks_.length > 0)
85                 setTimeout(waitHandler, 0);
86         });
87         object.addEventListener(eventName, eventHandler);
88     };
89
90     EventExpectationsManager.prototype.waitForExpectedEvents = function(callback)
91     {
92         this.waitCallbacks_.push(callback);
93         setTimeout(this.test_.step_func(this.handleWaitCallback_.bind(this)), 0);
94     };
95
96     EventExpectationsManager.prototype.expectingEvents = function()
97     {
98         for (var i = 0; i < this.eventTargetList_.length; ++i) {
99             if (this.eventTargetList_[i].expectations.length > 0) {
100                 return true;
101             }
102         }
103         return false;
104     }
105
106     EventExpectationsManager.prototype.handleWaitCallback_ = function()
107     {
108         if (this.waitCallbacks_.length == 0 || this.expectingEvents())
109             return;
110         var callback = this.waitCallbacks_.shift(1);
111         callback();
112     };
113
114     EventExpectationsManager.prototype.getExpectations_ = function(target)
115     {
116         for (var i = 0; i < this.eventTargetList_.length; ++i) {
117             var info = this.eventTargetList_[i];
118             if (info.target == target) {
119                 return info.expectations;
120             }
121         }
122         var expectations = [];
123         this.eventTargetList_.push({ 'target': target, 'expectations': expectations });
124         return expectations;
125     };
126
127     function loadData_(test, url, callback, isBinary)
128     {
129         var request = new XMLHttpRequest();
130         request.open("GET", url, true);
131         if (isBinary) {
132             request.responseType = 'arraybuffer';
133         }
134         request.onload = test.step_func(function(event)
135         {
136             if (request.status != 200) {
137                 assert_unreached("Unexpected status code : " + request.status);
138                 return;
139             }
140             var response = request.response;
141             if (isBinary) {
142                 response = new Uint8Array(response);
143             }
144             callback(response);
145         });
146         request.onerror = test.step_func(function(event)
147         {
148             assert_unreached("Unexpected error");
149         });
150         request.send();
151     }
152
153     function openMediaSource_(test, mediaTag, callback)
154     {
155         var mediaSource = new MediaSource();
156         var mediaSourceURL = URL.createObjectURL(mediaSource);
157
158         var eventHandler = test.step_func(onSourceOpen);
159         function onSourceOpen(event)
160         {
161             mediaSource.removeEventListener('sourceopen', eventHandler);
162             URL.revokeObjectURL(mediaSourceURL);
163             callback(mediaSource);
164         }
165
166         mediaSource.addEventListener('sourceopen', eventHandler);
167         mediaTag.src = mediaSourceURL;
168     }
169
170     var MediaSourceUtil = {};
171
172     MediaSourceUtil.loadTextData = function(test, url, callback)
173     {
174         loadData_(test, url, callback, false);
175     };
176
177     MediaSourceUtil.loadBinaryData = function(test, url, callback)
178     {
179         loadData_(test, url, callback, true);
180     };
181
182     MediaSourceUtil.fetchManifestAndData = function(test, manifestFilename, callback)
183     {
184         var baseURL = '/media/resources/media-source/';
185         var manifestURL = baseURL + manifestFilename;
186         MediaSourceUtil.loadTextData(test, manifestURL, function(manifestText)
187         {
188             var manifest = JSON.parse(manifestText);
189
190             assert_true(MediaSource.isTypeSupported(manifest.type), manifest.type + " is supported.");
191
192             var mediaURL = baseURL + manifest.url;
193             MediaSourceUtil.loadBinaryData(test, mediaURL, function(mediaData)
194             {
195                 callback(manifest.type, mediaData);
196             });
197         });
198     };
199
200     MediaSourceUtil.extractSegmentData = function(mediaData, info)
201     {
202         var start = info.offset;
203         var end = start + info.size;
204         return mediaData.subarray(start, end);
205     }
206
207     MediaSourceUtil.getMediaDataForPlaybackTime = function(mediaData, segmentInfo, playbackTimeToAdd)
208     {
209         assert_less_than_equal(playbackTimeToAdd, segmentInfo.duration);
210         var mediaInfo = segmentInfo.media;
211         var start = mediaInfo[0].offset;
212         var numBytes = 0;
213         var segmentIndex = 0;
214         while (segmentIndex < mediaInfo.length && mediaInfo[segmentIndex].timecode <= playbackTimeToAdd)
215         {
216           numBytes += mediaInfo[segmentIndex].size;
217           ++segmentIndex;
218         }
219         return mediaData.subarray(start, numBytes + start);
220     }
221
222     function getFirstSupportedType(typeList)
223     {
224         for (var i = 0; i < typeList.length; ++i) {
225             if (MediaSource.isTypeSupported(typeList[i]))
226                 return typeList[i];
227         }
228         return "";
229     }
230
231     function getSegmentInfo()
232     {
233         for (var i = 0; i < SEGMENT_INFO_LIST.length; ++i) {
234             var segmentInfo = SEGMENT_INFO_LIST[i];
235             if (MediaSource.isTypeSupported(segmentInfo.type)) {
236                 return segmentInfo;
237             }
238         }
239         return null;
240     }
241
242     var audioOnlyTypes = ['audio/webm;codecs="vorbis"', 'audio/mp4;codecs="mp4a.40.2"'];
243     var videoOnlyTypes = ['video/webm;codecs="vp8"', 'video/mp4;codecs="avc1.4D4001"'];
244     var audioVideoTypes = ['video/webm;codecs="vp8,vorbis"', 'video/mp4;codecs="mp4a.40.2"'];
245     MediaSourceUtil.AUDIO_ONLY_TYPE = getFirstSupportedType(audioOnlyTypes);
246     MediaSourceUtil.VIDEO_ONLY_TYPE = getFirstSupportedType(videoOnlyTypes);
247     MediaSourceUtil.AUDIO_VIDEO_TYPE = getFirstSupportedType(audioVideoTypes);
248     MediaSourceUtil.SEGMENT_INFO = getSegmentInfo();
249
250     MediaSourceUtil.getSubType = function(mimetype) {
251         var slashIndex = mimetype.indexOf("/");
252         var semicolonIndex = mimetype.indexOf(";");
253         if (slashIndex <= 0) {
254             assert_unreached("Invalid mimetype '" + mimetype + "'");
255             return;
256         }
257
258         var start = slashIndex + 1;
259         if (semicolonIndex >= 0) {
260             if (semicolonIndex <= start) {
261                 assert_unreached("Invalid mimetype '" + mimetype + "'");
262                 return;
263             }
264
265             return mimetype.substr(start, semicolonIndex - start)
266         }
267
268         return mimetype.substr(start);
269     };
270
271     // TODO: Add wrapper object to MediaSourceUtil that binds loaded mediaData to its
272     // associated segmentInfo.
273
274     function addExtraTestMethods(test)
275     {
276         test.failOnEvent = function(object, eventName)
277         {
278             object.addEventListener(eventName, test.step_func(function(event)
279             {
280                 assert_unreached("Unexpected event '" + eventName + "'");
281             }));
282         };
283
284         test.endOnEvent = function(object, eventName)
285         {
286             object.addEventListener(eventName, test.step_func(function(event) { test.done(); }));
287         };
288
289         test.eventExpectations_ = new EventExpectationsManager(test);
290         test.expectEvent = function(object, eventName, description)
291         {
292             test.eventExpectations_.expectEvent(object, eventName, description);
293         };
294
295         test.waitForExpectedEvents = function(callback)
296         {
297             test.eventExpectations_.waitForExpectedEvents(callback);
298         };
299
300         test.waitForCurrentTimeChange = function(mediaElement, callback)
301         {
302             var initialTime = mediaElement.currentTime;
303
304             var onTimeUpdate = test.step_func(function()
305             {
306                 if (mediaElement.currentTime != initialTime) {
307                     mediaElement.removeEventListener('timeupdate', onTimeUpdate);
308                     callback();
309                 }
310             });
311
312             mediaElement.addEventListener('timeupdate', onTimeUpdate);
313         }
314
315         var oldTestDone = test.done.bind(test);
316         test.done = function()
317         {
318             if (test.status == test.PASS) {
319                 assert_false(test.eventExpectations_.expectingEvents(), "No pending event expectations.");
320             }
321             oldTestDone();
322         };
323     };
324
325     window['MediaSourceUtil'] = MediaSourceUtil;
326     window['media_test'] = function(testFunction, description, options)
327     {
328         options = options || {};
329         return async_test(function(test)
330         {
331             addExtraTestMethods(test);
332             testFunction(test);
333         }, description, options);
334     };
335     window['mediasource_test'] = function(testFunction, description, options)
336     {
337         return media_test(function(test)
338         {
339             var mediaTag = document.createElement("video");
340             document.body.appendChild(mediaTag);
341
342             // Overload done() so that element added to the document can be removed.
343             test.removeMediaElement_ = true;
344             var oldTestDone = test.done.bind(test);
345             test.done = function()
346             {
347                 if (test.removeMediaElement_) {
348                     document.body.removeChild(mediaTag);
349                     test.removeMediaElement_ = false;
350                 }
351                 oldTestDone();
352             };
353
354             openMediaSource_(test, mediaTag, function(mediaSource)
355             {
356                 testFunction(test, mediaTag, mediaSource);
357             });
358         }, description, options);
359     };
360
361     window['mediasource_testafterdataloaded'] = function(testFunction, description, options)
362     {
363         mediasource_test(function(test, mediaElement, mediaSource)
364         {
365             var segmentInfo = MediaSourceUtil.SEGMENT_INFO;
366
367             if (!segmentInfo) {
368                 assert_unreached("No segment info compatible with this MediaSource implementation.");
369                 return;
370             }
371
372             test.failOnEvent(mediaElement, 'error');
373
374             var sourceBuffer = mediaSource.addSourceBuffer(segmentInfo.type);
375             MediaSourceUtil.loadBinaryData(test, segmentInfo.url, function(mediaData)
376             {
377                 testFunction(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData);
378             });
379         }, description, options);
380     }
381
382     function timeRangesToString(ranges)
383     {
384         var s = "{";
385         for (var i = 0; i < ranges.length; ++i) {
386             s += " [" + ranges.start(i).toFixed(3) + ", " + ranges.end(i).toFixed(3) + ")";
387         }
388         return s + " }";
389     }
390
391     window['assertBufferedEquals'] = function(obj, expected, description)
392     {
393         var actual = timeRangesToString(obj.buffered);
394         assert_equals(actual, expected, description);
395     };
396
397 })(window);