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 ResizeTestHelper.TIMEOUT = 500; | |
43 | |
44 ResizeTestHelper.prototype = { | |
45 | |
46 // @return ResizeObserver | |
47 get observer() { | |
48 return this._observer; | |
49 }, | |
50 | |
51 _handleNotification: function(entries) { | |
52 if (this._currentTest) { | |
53 // console.log("notification"); | |
54 let current = this._currentTest; | |
55 delete this._currentTest; | |
56 window.clearTimeout(current.timeoutId); | |
57 current.test.step(_ => { | |
58 // console.log("step"); | |
59 let caughtEx = false; | |
60 try { | |
61 current.completion(entries); | |
62 current.test.done(); | |
63 } | |
64 catch(ex) { | |
65 caughtEx = ex; | |
66 } | |
67 if (caughtEx) | |
68 throw caughtEx; | |
69 }); | |
70 } | |
71 }, | |
72 _handleTimeout: function() { | |
73 if (this._currentTest) { | |
74 let current = this._currentTest; | |
75 delete this._currentTest; | |
76 if (current.timeout) { // timeout is not an error | |
77 current.timeout(); | |
78 current.test.done(); | |
79 } | |
80 else { | |
81 current.test.step(_ => { | |
82 assert_unreached("Timed out waiting for notification. (" + ResizeTestH
elper.TIMEOUT + "ms)"); | |
83 current.test.done(); | |
84 }); | |
85 } | |
86 } | |
87 }, | |
88 | |
89 // Start the next test | |
90 nextTest: function() { | |
91 if (this._currentTest) // only one test at a time | |
92 return; | |
93 if (this._pendingTests.length > 0) { | |
94 this._currentTest = this._pendingTests.shift(); | |
95 // console.log("executing ", this._currentTest.name); | |
96 this._currentTest.setup(); | |
97 this._currentTest.timeoutId = this._currentTest.test.step_timeout(this._ha
ndleTimeout.bind(this), ResizeTestHelper.TIMEOUT); | |
98 } | |
99 }, | |
100 | |
101 // Fires nextTest on rAF. Use it to trigger next test. | |
102 nextTestRaf: function() { | |
103 window.requestAnimationFrame( () => this.nextTest() ); | |
104 }, | |
105 | |
106 // Adds new test to _pendingTests. | |
107 createTest: function(name, setup, completion, timeoutCb) { | |
108 // console.log('setup ', name); | |
109 this._pendingTests.push( { | |
110 name: name, | |
111 test: async_test(name), | |
112 setup: setup, | |
113 completion: completion, | |
114 timeout: timeoutCb }); | |
115 } | |
116 | |
117 } | |
OLD | NEW |