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 Features: |
| 10 - can queue multiple notification steps in a test |
| 11 - handles timeouts |
| 12 - returns Promise that is fullfilled when test completes. |
| 13 Use to chain tests (since parallel async ResizeObserver tests |
| 14 would conflict if reusing same DOM elements). |
| 15 |
| 16 Usage: |
| 17 |
| 18 create ResizeTestHelper for every test. |
| 19 Make assertions inside notify, timeout callbacks. |
| 20 Start tests with helper.start() |
| 21 Chain tests with Promises. |
| 22 Counts animation frames, see startCountingRaf |
| 23 */ |
| 24 |
| 25 /* |
| 26 @param name: test name |
| 27 @param steps: |
| 28 { |
| 29 setup: function(ResizeObserver) { |
| 30 // called at the beginning of the test step |
| 31 // your observe/resize code goes here |
| 32 }, |
| 33 notify: function(entries, observer) { |
| 34 // ResizeObserver callback. |
| 35 // Make assertions here. |
| 36 // Return true if next step should start on the next event loop. |
| 37 }, |
| 38 timeout: function() { |
| 39 // Define this if your test expects to time out. |
| 40 // If undefined, timeout is assert_unreached. |
| 41 } |
| 42 } |
| 43 */ |
| 44 function ResizeTestHelper(name, steps) |
| 45 { |
| 46 this._name = name; |
| 47 this._steps = steps || []; |
| 48 this._stepIdx = -1; |
| 49 this._harnessTest = null; |
| 50 this._observer = new ResizeObserver(this._handleNotification.bind(this)); |
| 51 this._timeoutBind = this._handleTimeout.bind(this); |
| 52 this._nextStepBind = this._nextStep.bind(this); |
| 53 } |
| 54 |
| 55 ResizeTestHelper.TIMEOUT = 100; |
| 56 |
| 57 ResizeTestHelper.prototype = { |
| 58 get _currentStep() { |
| 59 return this._steps[this._stepIdx]; |
| 60 }, |
| 61 |
| 62 _nextStep: function() { |
| 63 if (++this._stepIdx == this._steps.length) |
| 64 return this._done(); |
| 65 this._timeoutId = this._harnessTest.step_timeout( |
| 66 this._timeoutBind, ResizeTestHelper.TIMEOUT); |
| 67 try { |
| 68 this._steps[this._stepIdx].setup(this._observer); |
| 69 } |
| 70 catch(err) { |
| 71 console.error(err); |
| 72 this._harnessTest.step(() => { |
| 73 assert_unreached("Caught a throw, possible syntax error"); |
| 74 }); |
| 75 } |
| 76 }, |
| 77 |
| 78 _handleNotification: function(entries) { |
| 79 if (this._timeoutId) { |
| 80 window.clearTimeout(this._timeoutId); |
| 81 delete this._timeoutId; |
| 82 } |
| 83 this._harnessTest.step(() => { |
| 84 let rafDelay = this._currentStep.notify(entries, this._observer); |
| 85 if (rafDelay) |
| 86 window.requestAnimationFrame(this._nextStepBind); |
| 87 else |
| 88 this._nextStep(); |
| 89 }); |
| 90 }, |
| 91 |
| 92 _handleTimeout: function() { |
| 93 delete this._timeoutId; |
| 94 this._harnessTest.step(() => { |
| 95 if (this._currentStep.timeout) { |
| 96 this._currentStep.timeout(); |
| 97 } |
| 98 else { |
| 99 assert_unreached("Timed out waiting for notification. (" + ResizeTestHel
per.TIMEOUT + "ms)"); |
| 100 } |
| 101 this._nextStep(); |
| 102 }); |
| 103 }, |
| 104 |
| 105 _done: function() { |
| 106 this._observer.disconnect(); |
| 107 delete this._observer; |
| 108 this._harnessTest.done(); |
| 109 if (this._rafCountRequest) { |
| 110 window.cancelAnimationFrame(this._rafCountRequest); |
| 111 delete this._rafCountRequest; |
| 112 } |
| 113 window.requestAnimationFrame(() => { this._resolvePromise(); }); |
| 114 }, |
| 115 |
| 116 start: function() { |
| 117 this._harnessTest = async_test(this._name); |
| 118 this._harnessTest.step(() => { |
| 119 assert_equals(this._stepIdx, -1, "start can only be called once"); |
| 120 this._nextStep(); |
| 121 }); |
| 122 return new Promise( (resolve, reject) => { |
| 123 this._resolvePromise = resolve; |
| 124 this._rejectPromise = reject; |
| 125 }); |
| 126 }, |
| 127 |
| 128 get rafCount() { |
| 129 if (!this._rafCountRequest) |
| 130 throw "rAF count is not active"; |
| 131 return this._rafCount; |
| 132 }, |
| 133 |
| 134 _incrementRaf: function() { |
| 135 if (this._rafCountRequest) { |
| 136 this._rafCount++; |
| 137 this._rafCountRequest = window.requestAnimationFrame(this._incrementRafBin
d); |
| 138 } |
| 139 }, |
| 140 |
| 141 startCountingRaf: function() { |
| 142 if (this._rafCountRequest) |
| 143 window.cancelAnimationFrame(this._rafCountRequest); |
| 144 if (!this._incrementRafBind) |
| 145 this._incrementRafBind = this._incrementRaf.bind(this); |
| 146 this._rafCount = 0; |
| 147 this._rafCountRequest = window.requestAnimationFrame(this._incrementRafBind)
; |
| 148 } |
| 149 } |
OLD | NEW |