OLD | NEW |
(Empty) | |
| 1 'use strict'; |
| 2 |
| 3 /** |
| 4 ResizeTestHelper is a framework to test ResizeObserver |
| 5 notifications. Use it to make assertions about ResizeObserverEntries. |
| 6 This framework is needed because ResizeObservations are |
| 7 delivered asynchronously inside the event loop. |
| 8 |
| 9 It handles timeouts, and queueing of multiple steps in a test. |
| 10 |
| 11 Usage: |
| 12 |
| 13 Use createTest() to create tests. |
| 14 Make assertions inside entries, timeout callbacks. |
| 15 Every test should clean up after itself by calling helper.observer.disconnect(
); |
| 16 Chain tests together with nextTest(), or nextTestRaf() |
| 17 |
| 18 Example: |
| 19 |
| 20 var helper = new ResizeTestHelper(); |
| 21 |
| 22 function test0() { |
| 23 helper.createTest( |
| 24 "test0: test name here", |
| 25 setup => { // setup gets called when test starts |
| 26 helper.observer.observe(t3); |
| 27 }, |
| 28 entries => { // This is ResizeObserver callback |
| 29 // kick off next test by calling nextTestRaf |
| 30 helper.nextTestRaf(); |
| 31 }, |
| 32 timeout => { // timeout gets called on timeout |
| 33 // if timeout happens, and timeout is not defined, test will fail |
| 34 } |
| 35 ); |
| 36 */ |
| 37 |
| 38 function ResizeTestHelper() { |
| 39 this._pendingTests = []; |
| 40 this._observer = new ResizeObserver(this._handleNotification.bind(this)); |
| 41 } |
| 42 |
| 43 ResizeTestHelper.prototype = { |
| 44 |
| 45 TIMEOUT: 500, |
| 46 |
| 47 // @return ResizeObserver |
| 48 get observer() { |
| 49 return this._observer; |
| 50 }, |
| 51 |
| 52 _handleNotification: function(entries) { |
| 53 if (this._currentTest) { |
| 54 // console.log("notification"); |
| 55 let current = this._currentTest; |
| 56 delete this._currentTest; |
| 57 window.clearTimeout(current.timeoutId); |
| 58 current.test.step(_ => { |
| 59 // console.log("step"); |
| 60 let caughtEx = false; |
| 61 try { |
| 62 current.completion(entries); |
| 63 current.test.done(); |
| 64 } |
| 65 catch(ex) { |
| 66 caughtEx = ex; |
| 67 } |
| 68 if (caughtEx) |
| 69 throw caughtEx; |
| 70 }); |
| 71 } |
| 72 }, |
| 73 _handleTimeout: function() { |
| 74 if (this._currentTest) { |
| 75 let current = this._currentTest; |
| 76 delete this._currentTest; |
| 77 if (current.timeout) { // timeout is not an error |
| 78 current.timeout(); |
| 79 current.test.done(); |
| 80 } |
| 81 else { |
| 82 current.test.step(_ => { |
| 83 assert_unreached("Timed out waiting for notification. (" + this.TIMEOU
T + "ms)"); |
| 84 current.test.done(); |
| 85 }); |
| 86 } |
| 87 } |
| 88 }, |
| 89 |
| 90 // Start the next test |
| 91 nextTest: function() { |
| 92 if (this._currentTest) // only one test at a time |
| 93 return; |
| 94 if (this._pendingTests.length > 0) { |
| 95 this._currentTest = this._pendingTests.shift(); |
| 96 // console.log("executing ", this._currentTest.name); |
| 97 this._currentTest.setup(); |
| 98 this._currentTest.timeoutId = this._currentTest.test.step_timeout(this._ha
ndleTimeout.bind(this), this.TIMEOUT); |
| 99 } |
| 100 }, |
| 101 |
| 102 // Fires nextTest on rAF. Use it to trigger next test. |
| 103 nextTestRaf: function() { |
| 104 window.requestAnimationFrame( () => this.nextTest() ); |
| 105 }, |
| 106 |
| 107 // Adds new test to _pendingTests. |
| 108 createTest: function(name, setup, completion, timeoutCb) { |
| 109 // console.log('setup ', name); |
| 110 this._pendingTests.push( { |
| 111 name: name, |
| 112 test: async_test(name), |
| 113 setup: setup, |
| 114 completion: completion, |
| 115 timeout: timeoutCb }); |
| 116 } |
| 117 |
| 118 } |
OLD | NEW |