Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 (function(window) { | 1 (function(window) { |
| 2 EventExpectationsManager = function(test) | 2 EventExpectationsManager = function(test) |
| 3 { | 3 { |
| 4 this.test_ = test; | 4 this.test_ = test; |
| 5 this.eventTargetList_ = []; | 5 this.eventTargetList_ = []; |
| 6 this.waitCallbacks_ = []; | 6 this.waitCallbacks_ = []; |
| 7 this.timeoutIDs_ = new Array(); | |
|
wolenetz
2013/08/02 20:04:25
new Array() is the same as []. At least according
anandc
2013/08/02 20:40:38
Thanks for trying out the different options. Going
| |
| 7 }; | 8 }; |
| 8 | 9 |
| 9 EventExpectationsManager.prototype.expectEvent = function(object, eventName, description) | 10 EventExpectationsManager.prototype.expectEvent = function(object, eventName, description) |
| 10 { | 11 { |
| 11 var eventInfo = { 'target': object, 'type': eventName, 'description': de scription}; | 12 var eventInfo = { 'target': object, 'type': eventName, 'description': de scription}; |
| 12 var expectations = this.getExpectations_(object); | 13 var expectations = this.getExpectations_(object); |
| 13 expectations.push(eventInfo); | 14 expectations.push(eventInfo); |
| 14 | 15 |
| 15 var t = this; | 16 var t = this; |
| 16 var waitHandler = this.test_.step_func(function() { t.handleWaitCallback _(); }); | 17 var waitHandler = this.test_.step_func(function() { t.handleWaitCallback _(); }); |
| 17 var eventHandler = this.test_.step_func(function(event) | 18 var eventHandler = this.test_.step_func(function(event) |
| 18 { | 19 { |
| 19 object.removeEventListener(eventName, eventHandler); | 20 object.removeEventListener(eventName, eventHandler); |
| 20 var expected = expectations[0]; | 21 var expected = expectations[0]; |
| 21 assert_equals(event.target, expected.target, "Event target match."); | 22 assert_equals(event.target, expected.target, "Event target match."); |
| 22 assert_equals(event.type, expected.type, "Event types match."); | 23 assert_equals(event.type, expected.type, "Event types match."); |
| 23 assert_equals(eventInfo.description, expected.description, "Descript ions match for '" + event.type + "'."); | 24 assert_equals(eventInfo.description, expected.description, "Descript ions match for '" + event.type + "'."); |
| 24 | 25 |
| 25 expectations.shift(1); | 26 expectations.shift(1); |
| 26 if (t.waitCallbacks_.length > 0) | 27 if (t.waitCallbacks_.length > 0) |
| 27 setTimeout(waitHandler, 0); | 28 setTimeout(waitHandler, 0); |
| 28 }); | 29 }); |
| 29 object.addEventListener(eventName, eventHandler); | 30 object.addEventListener(eventName, eventHandler); |
| 30 }; | 31 }; |
| 31 | 32 |
| 33 EventExpectationsManager.prototype.expectDelayedCallback = function(callback , delay) | |
| 34 { | |
| 35 var timeoutIDHolder = 0; | |
| 36 // Define a wrapper function that will: | |
| 37 // 1. To indicate that this expected callback did fire, remove this | |
| 38 // timeoutID from the list of IDs we are maintaining. | |
| 39 // 2. Execute the callback that has been passed-in. | |
| 40 var callbackWrapper = function(callback, timeoutIDs) { | |
| 41 delete timeoutIDs[timeoutIDHolder]; | |
| 42 callback(); | |
| 43 } | |
| 44 | |
| 45 // Execute the wrapper after the specified delay. | |
| 46 var timeoutIDs = this.timeoutIDs_; | |
| 47 timeoutIDHolder = setTimeout(function() {callbackWrapper(callback, timeo utIDs);}, delay); | |
| 48 // Add to list of timeoutIDs, which we'll use eventually to check that | |
| 49 // all expected delayed callbacks did get executed. | |
| 50 this.timeoutIDs_[timeoutIDHolder] = timeoutIDHolder; | |
| 51 }; | |
| 52 | |
| 32 EventExpectationsManager.prototype.waitForExpectedEvents = function(callback ) | 53 EventExpectationsManager.prototype.waitForExpectedEvents = function(callback ) |
| 33 { | 54 { |
| 34 this.waitCallbacks_.push(callback); | 55 this.waitCallbacks_.push(callback); |
| 35 setTimeout(this.handleWaitCallback_.bind(this), 0); | 56 setTimeout(this.handleWaitCallback_.bind(this), 0); |
| 36 }; | 57 }; |
| 37 | 58 |
| 38 EventExpectationsManager.prototype.expectingEvents = function() | 59 EventExpectationsManager.prototype.expectingEvents = function() |
| 39 { | 60 { |
| 40 for (var i = 0; i < this.eventTargetList_.length; ++i) { | 61 for (var i = 0; i < this.eventTargetList_.length; ++i) { |
| 41 if (this.eventTargetList_[i].expectations.length > 0) { | 62 if (this.eventTargetList_[i].expectations.length > 0) { |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 169 { | 190 { |
| 170 object.addEventListener(eventName, test.step_func(function(event) { test.done(); })); | 191 object.addEventListener(eventName, test.step_func(function(event) { test.done(); })); |
| 171 }; | 192 }; |
| 172 | 193 |
| 173 test.eventExpectations_ = new EventExpectationsManager(test); | 194 test.eventExpectations_ = new EventExpectationsManager(test); |
| 174 test.expectEvent = function(object, eventName, description) | 195 test.expectEvent = function(object, eventName, description) |
| 175 { | 196 { |
| 176 test.eventExpectations_.expectEvent(object, eventName, description); | 197 test.eventExpectations_.expectEvent(object, eventName, description); |
| 177 }; | 198 }; |
| 178 | 199 |
| 200 test.expectDelayedCallback = function(callback, delay) | |
| 201 { | |
| 202 test.eventExpectations_.expectDelayedCallback(callback, delay); | |
| 203 } | |
| 204 | |
| 179 test.waitForExpectedEvents = function(callback) | 205 test.waitForExpectedEvents = function(callback) |
| 180 { | 206 { |
| 181 test.eventExpectations_.waitForExpectedEvents(callback); | 207 test.eventExpectations_.waitForExpectedEvents(callback); |
| 182 }; | 208 }; |
| 183 | 209 |
| 184 var oldTestDone = test.done.bind(test); | 210 var oldTestDone = test.done.bind(test); |
| 185 test.done = function() | 211 test.done = function() |
| 186 { | 212 { |
| 187 if (test.status == test.PASS) | 213 // loop through and clear timeoutIDs |
| 214 for (var i in test.eventExpectations_.timeoutIDs_[i]) { | |
|
wolenetz
2013/08/02 20:04:25
I believe this [i] breaks the enumeration code.
Ex
anandc
2013/08/02 20:40:38
Stupid bug. Thanks for catching. Fixed.
| |
| 215 clearTimeout(test.eventExpectations_.timeoutIDs_[i]); | |
| 216 } | |
| 217 | |
| 218 if (test.status == test.PASS) { | |
| 188 assert_false(test.eventExpectations_.expectingEvents(), "No pend ing event expectations."); | 219 assert_false(test.eventExpectations_.expectingEvents(), "No pend ing event expectations."); |
| 220 assert_equals(Object.keys(test.eventExpectations_.timeoutIDs_).l ength, 0); | |
| 221 } | |
| 189 oldTestDone(); | 222 oldTestDone(); |
| 190 }; | 223 }; |
| 191 }; | 224 }; |
| 192 | 225 |
| 193 window['MediaSourceUtil'] = MediaSourceUtil; | 226 window['MediaSourceUtil'] = MediaSourceUtil; |
| 194 window['media_test'] = function(testFunction, description, options) | 227 window['media_test'] = function(testFunction, description, options) |
| 195 { | 228 { |
| 196 options = options || {}; | 229 options = options || {}; |
| 197 return async_test(function(test) | 230 return async_test(function(test) |
| 198 { | 231 { |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 220 }; | 253 }; |
| 221 | 254 |
| 222 openMediaSource_(test, mediaTag, function(mediaSource) | 255 openMediaSource_(test, mediaTag, function(mediaSource) |
| 223 { | 256 { |
| 224 testFunction(test, mediaTag, mediaSource); | 257 testFunction(test, mediaTag, mediaSource); |
| 225 }); | 258 }); |
| 226 }, description, options); | 259 }, description, options); |
| 227 | 260 |
| 228 }; | 261 }; |
| 229 })(window); | 262 })(window); |
| OLD | NEW |