OLD | NEW |
(Empty) | |
| 1 (function(window) { |
| 2 EventExpectationsManager = function(test) { |
| 3 this.test_ = test; |
| 4 this.eventTargetList_ = []; |
| 5 this.waitCallbacks_ = []; |
| 6 }; |
| 7 |
| 8 EventExpectationsManager.prototype.expectEvent = function(object, eventName,
description) |
| 9 { |
| 10 var eventInfo = { 'target': object, 'type': eventName, 'description': de
scription}; |
| 11 var expectations = this.getExpectations_(object); |
| 12 expectations.push(eventInfo); |
| 13 |
| 14 var t = this; |
| 15 var waitHandler = this.test_.step_func(function() { t.handleWaitCallback
_(); }); |
| 16 var eventHandler = this.test_.step_func(function(event) |
| 17 { |
| 18 object.removeEventListener(eventName, eventHandler); |
| 19 var expected = expectations[0]; |
| 20 assert_equals(event.target, expected.target, "Event target match."); |
| 21 assert_equals(event.type, expected.type, "Event types match."); |
| 22 assert_equals(eventInfo.description, expected.description, "Descript
ions match for '" + event.type + "'."); |
| 23 |
| 24 expectations.shift(1); |
| 25 if (t.waitCallbacks_.length > 0) |
| 26 setTimeout(waitHandler, 0); |
| 27 }); |
| 28 object.addEventListener(eventName, eventHandler); |
| 29 }; |
| 30 |
| 31 EventExpectationsManager.prototype.waitForExpectedEvents = function(callback
) |
| 32 { |
| 33 this.waitCallbacks_.push(callback); |
| 34 setTimeout(this.handleWaitCallback_.bind(this), 0); |
| 35 }; |
| 36 |
| 37 EventExpectationsManager.prototype.expectingEvents = function() |
| 38 { |
| 39 for (var i = 0; i < this.eventTargetList_.length; ++i) { |
| 40 if (this.eventTargetList_[i].expectations.length > 0) { |
| 41 return true; |
| 42 } |
| 43 } |
| 44 return false; |
| 45 } |
| 46 |
| 47 EventExpectationsManager.prototype.handleWaitCallback_ = function() |
| 48 { |
| 49 if (this.waitCallbacks_.length == 0 || this.expectingEvents()) |
| 50 return; |
| 51 var callback = this.waitCallbacks_.shift(1); |
| 52 callback(); |
| 53 }; |
| 54 |
| 55 EventExpectationsManager.prototype.getExpectations_ = function(target) |
| 56 { |
| 57 for (var i = 0; i < this.eventTargetList_.length; ++i) { |
| 58 var info = this.eventTargetList_[i]; |
| 59 if (info.target == target) { |
| 60 return info.expectations; |
| 61 } |
| 62 } |
| 63 var expectations = []; |
| 64 this.eventTargetList_.push({ 'target': target, 'expectations': expectati
ons }); |
| 65 return expectations; |
| 66 }; |
| 67 |
| 68 function loadData_(test, url, callback, isBinary) |
| 69 { |
| 70 var request = new XMLHttpRequest(); |
| 71 request.open("GET", url, true); |
| 72 if (isBinary) { |
| 73 request.responseType = 'arraybuffer'; |
| 74 } |
| 75 request.onload = test.step_func(function(event) |
| 76 { |
| 77 if (request.status != 200) { |
| 78 assert_unreached("Unexpected status code : " + request.status); |
| 79 return; |
| 80 } |
| 81 var response = request.response; |
| 82 if (isBinary) { |
| 83 response = new Uint8Array(response); |
| 84 } |
| 85 callback(new Uint8Array(response)); |
| 86 }); |
| 87 request.onerror = test.step_func(function(event) |
| 88 { |
| 89 assert_unreached("Unexpected error"); |
| 90 }); |
| 91 request.send(); |
| 92 } |
| 93 |
| 94 function openMediaSource_(test, mediaTag, callback) |
| 95 { |
| 96 var mediaSource = new MediaSource(); |
| 97 var mediaSourceURL = URL.createObjectURL(mediaSource); |
| 98 |
| 99 var eventHandler = test.step_func(onSourceOpen); |
| 100 function onSourceOpen(event) |
| 101 { |
| 102 mediaSource.removeEventListener('sourceopen', eventHandler); |
| 103 URL.revokeObjectURL(mediaSourceURL); |
| 104 callback(mediaSource); |
| 105 } |
| 106 |
| 107 mediaSource.addEventListener('sourceopen', eventHandler); |
| 108 mediaTag.src = mediaSourceURL; |
| 109 } |
| 110 |
| 111 var MediaSourceUtil = {}; |
| 112 |
| 113 MediaSourceUtil.loadTextData = function(test, url, callback) |
| 114 { |
| 115 loadData_(test, url, callback, false); |
| 116 }; |
| 117 |
| 118 MediaSourceUtil.loadBinaryData = function(test, url, callback) |
| 119 { |
| 120 loadData_(test, url, callback, true); |
| 121 }; |
| 122 |
| 123 function getFirstSupportedType(typeList) |
| 124 { |
| 125 for (var i = 0; i < typeList.length; ++i) { |
| 126 if (MediaSource.isTypeSupported(typeList[i])) |
| 127 return typeList[i]; |
| 128 } |
| 129 return ""; |
| 130 } |
| 131 |
| 132 var audioOnlyTypes = ['audio/webm;codecs="vorbis"', 'audio/mp4;codecs="mp4a.
40.2"']; |
| 133 var videoOnlyTypes = ['video/webm;codecs="vp8"', 'video/mp4;codecs="avc1.4D4
001"']; |
| 134 var audioVideoTypes = ['video/webm;codecs="vp8,vorbis"', 'video/mp4;codecs="
mp4a.40.2"']; |
| 135 MediaSourceUtil.AUDIO_ONLY_TYPE = getFirstSupportedType(audioOnlyTypes); |
| 136 MediaSourceUtil.VIDEO_ONLY_TYPE = getFirstSupportedType(videoOnlyTypes); |
| 137 MediaSourceUtil.AUDIO_VIDEO_TYPE = getFirstSupportedType(audioVideoTypes); |
| 138 |
| 139 function addExtraTestMethods(test) |
| 140 { |
| 141 test.failOnEvent = function(object, eventName) |
| 142 { |
| 143 object.addEventListener(eventName, test.step_func(function(event) |
| 144 { |
| 145 assert_unreached("Unexpected event '" + eventName + "'"); |
| 146 })); |
| 147 }; |
| 148 |
| 149 test.endOnEvent = function(object, eventName) |
| 150 { |
| 151 object.addEventListener(eventName, test.step_func(function(event) {
test.done(); })); |
| 152 }; |
| 153 |
| 154 test.eventExpectations_ = new EventExpectationsManager(test); |
| 155 test.expectEvent = function(object, eventName, description) |
| 156 { |
| 157 test.eventExpectations_.expectEvent(object, eventName, description); |
| 158 }; |
| 159 |
| 160 test.waitForExpectedEvents = function(callback) |
| 161 { |
| 162 test.eventExpectations_.waitForExpectedEvents(callback); |
| 163 }; |
| 164 |
| 165 var oldTestDone = test.done.bind(test); |
| 166 test.done = function() |
| 167 { |
| 168 if (test.status == test.PASS) |
| 169 assert_false(test.eventExpectations_.expectingEvents(), "No pend
ing event expectations."); |
| 170 oldTestDone(); |
| 171 }; |
| 172 }; |
| 173 |
| 174 window['MediaSourceUtil'] = MediaSourceUtil; |
| 175 window['mediasource_test'] = function(testFunction, description) |
| 176 { |
| 177 return async_test(function(test) { |
| 178 var mediaTag = document.createElement("video"); |
| 179 document.body.appendChild(mediaTag); |
| 180 |
| 181 addExtraTestMethods(test); |
| 182 |
| 183 // Overload done() so that element added to the document can be remo
ved. |
| 184 test.removeMediaElement_ = true; |
| 185 var oldTestDone = test.done.bind(test); |
| 186 test.done = function() |
| 187 { |
| 188 if (test.removeMediaElement_) { |
| 189 document.body.removeChild(mediaTag); |
| 190 test.removeMediaElement_ = false; |
| 191 } |
| 192 oldTestDone(); |
| 193 }; |
| 194 |
| 195 openMediaSource_(test, mediaTag, function(mediaSource) |
| 196 { |
| 197 testFunction(test, mediaTag, mediaSource); |
| 198 }); |
| 199 }, description); |
| 200 |
| 201 }; |
| 202 })(window); |
OLD | NEW |