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_ = []; | |
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 // Define a wrapper function that will: | |
36 // 1. To indicate that this expected callback did fire, remove this | |
37 // timeoutID from the list of IDs we are maintaining. | |
38 // 2. Execute the callback that has been passed-in. | |
39 var callbackWrapper = function(callback, timeoutIDs, index) { | |
40 timeoutIDs.splice(index, 1); | |
wolenetz
2013/08/01 18:32:24
I don't think this will always work as intended. E
anandc
2013/08/01 22:10:10
This was really useful. Thanks. Done.
| |
41 callback(); | |
42 } | |
43 | |
44 // Execute the wrapper after the specified delay. | |
45 var timeoutIDs = this.timeoutIDs_; | |
wolenetz
2013/08/01 18:32:24
nit: why not just use this.timeoutIDs_ everywhere,
anandc
2013/08/01 22:10:10
Done in most places.
There is a problem with the
| |
46 var index = timeoutIDs.length; | |
47 var timeoutID = setTimeout(function() {callbackWrapper(callback, timeout IDs, index);}, delay); | |
48 // Add to list of timeoutIDs, which we'll use eventually to check that | |
49 // all expected delayed callbacks did get executed. | |
50 timeoutIDs[index] = timeoutID; | |
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 var timeoutIDs = test.eventExpectations_.timeoutIDs_; | |
215 for (var i = 0; i < timeoutIDs.length; ++i) { | |
216 clearTimeout(timeoutIDs[i]); | |
217 } | |
218 | |
219 if (test.status == test.PASS) { | |
188 assert_false(test.eventExpectations_.expectingEvents(), "No pend ing event expectations."); | 220 assert_false(test.eventExpectations_.expectingEvents(), "No pend ing event expectations."); |
221 assert_equals(timeoutIDs.length, 0); | |
222 } | |
189 oldTestDone(); | 223 oldTestDone(); |
190 }; | 224 }; |
191 }; | 225 }; |
192 | 226 |
193 window['MediaSourceUtil'] = MediaSourceUtil; | 227 window['MediaSourceUtil'] = MediaSourceUtil; |
194 window['media_test'] = function(testFunction, description, options) | 228 window['media_test'] = function(testFunction, description, options) |
195 { | 229 { |
196 options = options || {}; | 230 options = options || {}; |
197 return async_test(function(test) | 231 return async_test(function(test) |
198 { | 232 { |
(...skipping 21 matching lines...) Expand all Loading... | |
220 }; | 254 }; |
221 | 255 |
222 openMediaSource_(test, mediaTag, function(mediaSource) | 256 openMediaSource_(test, mediaTag, function(mediaSource) |
223 { | 257 { |
224 testFunction(test, mediaTag, mediaSource); | 258 testFunction(test, mediaTag, mediaSource); |
225 }); | 259 }); |
226 }, description, options); | 260 }, description, options); |
227 | 261 |
228 }; | 262 }; |
229 })(window); | 263 })(window); |
OLD | NEW |