Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 <!DOCTYPE html> | |
| 2 <html> | |
| 3 <head> | |
| 4 <meta charset="utf-8"> | |
| 5 <title>ScrollState constructor behaves correctly</title> | |
|
Rick Byers
2015/04/10 14:51:44
update title
tdresser
2015/05/08 18:23:01
Done.
| |
| 6 <script src="../../../resources/testharness.js"></script> | |
| 7 <script src="../../../resources/testharnessreport.js"></script> | |
| 8 <style> | |
| 9 | |
| 10 * { | |
| 11 margin:0; | |
| 12 padding:0; | |
| 13 } | |
| 14 | |
| 15 *::-webkit-scrollbar { | |
| 16 width: 0 !important; | |
| 17 height: 0 !important; | |
| 18 } | |
| 19 | |
| 20 #a { | |
| 21 height:400px; | |
| 22 width:400px; | |
| 23 overflow:scroll; | |
| 24 } | |
| 25 | |
| 26 #b { | |
| 27 height:500px; | |
| 28 width:500px; | |
| 29 background-color:purple; | |
| 30 } | |
| 31 | |
| 32 #c { | |
| 33 height:300px; | |
| 34 width:300px; | |
| 35 overflow:scroll; | |
| 36 } | |
| 37 | |
| 38 #d { | |
| 39 height:400px; | |
| 40 width:400px; | |
| 41 background-color:green; | |
| 42 } | |
| 43 | |
| 44 body { | |
| 45 height:3000px; | |
| 46 } | |
| 47 | |
| 48 </style> | |
| 49 </head> | |
| 50 <body> | |
| 51 | |
| 52 <div id="a"> | |
| 53 <div id="b"> | |
| 54 <div id="c"> | |
| 55 <div id="d"> | |
| 56 </div> | |
| 57 </div> | |
| 58 </div> | |
| 59 </div> | |
| 60 | |
| 61 <script> | |
| 62 | |
| 63 if (!window.internals || !window.internals.runtimeFlags.scrollCustomizationEnabl ed || !window.eventSender) { | |
| 64 console.log("These tests only work with window.internals and " + | |
| 65 "window.eventSender exposed, and require scroll customization."); | |
|
Rick Byers
2015/04/10 14:51:44
Do you think we should be making these tests expli
tdresser
2015/05/08 18:23:01
Done.
| |
| 66 done(); | |
| 67 } | |
| 68 | |
| 69 var originalApplyScroll = Element.prototype.applyScroll; | |
| 70 var originalDistributeScroll = Element.prototype.distributeScroll; | |
| 71 var deltas = [-85, -75, -65, -55, -45]; | |
| 72 | |
| 73 var elements = [ | |
| 74 document.getElementById("d"), | |
| 75 document.getElementById("c"), | |
| 76 document.getElementById("b"), | |
| 77 document.getElementById("a"), | |
| 78 document.body, | |
| 79 document.documentElement]; | |
| 80 | |
| 81 document.body.id = "body"; | |
| 82 document.documentElement.id = "document"; | |
| 83 | |
| 84 function reset() { | |
| 85 for (var i = 0; i < elements.length; ++i) { | |
| 86 elements[i].scrollTop = 0; | |
| 87 elements[i].unappliedDeltaY = []; | |
| 88 elements[i].distributedDeltaY = []; | |
| 89 elements[i].numberOfScrollBegins = 0; | |
| 90 elements[i].numberOfScrollEnds = 0; | |
| 91 } | |
| 92 | |
| 93 Element.prototype.applyScroll = function(scrollState) { | |
|
Rick Byers
2015/04/10 14:51:44
Perhaps you should use the likely more common patt
tdresser
2015/05/08 18:23:02
Now that we're using attributes, setting the metho
tdresser
2015/06/08 15:37:36
I'm not sure how I would use the super keyword whe
| |
| 94 originalApplyScroll.call(this, scrollState); | |
| 95 if (!scrollState.isEnding && !scrollState.isBeginning) | |
| 96 this.unappliedDeltaY.push(scrollState.deltaY); | |
| 97 } | |
| 98 | |
| 99 Element.prototype.distributeScroll = function(scrollState) { | |
| 100 if (scrollState.isBeginning) | |
| 101 this.numberOfScrollBegins++; | |
| 102 else if (scrollState.isEnding) | |
| 103 this.numberOfScrollEnds++; | |
| 104 else | |
| 105 this.distributedDeltaY.push(scrollState.deltaY); | |
| 106 originalDistributeScroll.call(this, scrollState); | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 function applyDelta(d) { | |
| 111 eventSender.gestureScrollBegin(10, 10); | |
| 112 eventSender.gestureScrollUpdate(0, d); | |
| 113 eventSender.gestureScrollEnd(0, 0); | |
| 114 } | |
| 115 | |
| 116 test(function() { | |
| 117 reset(); | |
| 118 | |
| 119 // Scroll five times, with four scrollable elements. | |
| 120 var cScrollTop = [85, 100, 100, 100, 100]; | |
| 121 var aScrollTop = [0, 0, 65, 100, 100]; | |
| 122 var bodyScrollTop = [0, 0, 0, 0, 45]; | |
| 123 | |
| 124 for (var i = 0; i < deltas.length; ++i) { | |
| 125 applyDelta(deltas[i]); | |
| 126 assert_equals(a.scrollTop, aScrollTop[i], "For id 'a' on step " + i); | |
| 127 assert_equals(c.scrollTop, cScrollTop[i], "For id 'c' on step " + i); | |
| 128 assert_equals(document.body.scrollTop, bodyScrollTop[i], "For body on step " + i); | |
|
Rick Byers
2015/04/10 14:51:44
nit: use documentElement instead of body. body.sc
tdresser
2015/05/08 18:23:01
Done, though I had to enable ScrollTopLeftInterop
tdresser
2015/05/08 19:38:34
Correction, I just had to switch to using scrollin
| |
| 129 } | |
| 130 }, "Scroll offsets are modified correctly."); | |
| 131 | |
| 132 test(function() { | |
| 133 reset(); | |
| 134 | |
| 135 // Scroll five times, with six elements. | |
| 136 var unapplied = [ | |
| 137 // d, the innermost element, never applies any scroll. | |
| 138 [-85, -75, -65, -55, -45], | |
| 139 // c applies the first two scrolls, and then hits its scroll extents. | |
| 140 [0, 0, -65, -55, -45], | |
| 141 // b doesn't scroll, and so leaves the same deltas unapplied as c. | |
| 142 [0, 0, -65, -55, -45], | |
| 143 // a hits its scroll extent on the second last step. | |
| 144 [0, 0, 0, 0, -45], | |
| 145 // body doesn't scroll (it's the documentElement that scrolls, but the body' s | |
| 146 // scrollTop updates), and so leaves the same deltas unapplied as a. | |
| 147 [0, 0, 0, 0, -45], | |
| 148 // The documentElement performs the frame scroll. | |
| 149 [0, 0, 0, 0, 0]]; | |
| 150 | |
| 151 for (var i = 0; i < deltas.length; ++i) | |
| 152 applyDelta(deltas[i]); | |
| 153 | |
| 154 for (var i = 0; i < elements.length; ++i) { | |
| 155 var el = elements[i]; | |
| 156 // Every element sees the same deltas being distributed. | |
| 157 assert_array_equals(el.distributedDeltaY, deltas, "distributed delta for " + el.id); | |
| 158 assert_array_equals(el.unappliedDeltaY, unapplied[i], "unapplied delta for " + el.id); | |
| 159 } | |
| 160 | |
| 161 // Ensure that the document leaves scroll unapplied when appropriate. | |
| 162 var documentUnapplied = document.documentElement.unappliedDeltaY; | |
| 163 applyDelta(-4000); | |
| 164 assert_equals(documentUnapplied[documentUnapplied.length - 1], 0); | |
| 165 applyDelta(-4000); | |
| 166 assert_equals(documentUnapplied[documentUnapplied.length - 1], -4000); | |
| 167 }, "Correct amount of delta is consumed."); | |
| 168 | |
| 169 test(function() { | |
| 170 reset(); | |
| 171 | |
| 172 // Consume one pixel of delta per call to applyScroll. | |
| 173 var applyScroll = Element.prototype.applyScroll; | |
| 174 Element.prototype.applyScroll = function(scrollState) { | |
| 175 if (scrollState.deltaY !== 0) | |
| 176 scrollState.consumeDelta(0, -1); | |
| 177 applyScroll.call(this, scrollState); | |
| 178 } | |
| 179 | |
| 180 // Scroll five times, with four scrollable elements. | |
| 181 // The scroll distance is decreased more the higher up the scroll chain the el ement is. | |
| 182 var cScrollTop = [85 - 2, 100, 100, 100, 100]; | |
| 183 var aScrollTop = [0, 0, 65 - 4, 100, 100]; | |
| 184 var bodyScrollTop = [0, 0, 0, 0, 45 - 6]; | |
| 185 | |
| 186 for (var i = 0; i < deltas.length; ++i) { | |
| 187 applyDelta(deltas[i]); | |
| 188 assert_equals(c.scrollTop, cScrollTop[i], "For id 'c' on step " + i); | |
| 189 assert_equals(a.scrollTop, aScrollTop[i], "For id 'a' on step " + i); | |
| 190 assert_equals(document.body.scrollTop, bodyScrollTop[i], "For body on step " + i); | |
| 191 } | |
| 192 }, "Consuming deltas prevents scrolling."); | |
| 193 | |
| 194 test(function() { | |
| 195 reset(); | |
| 196 | |
| 197 for (var i = 0; i < deltas.length; ++i) | |
| 198 applyDelta(deltas[i]); | |
| 199 | |
| 200 for (var i = 0; i < elements.length; ++i) { | |
| 201 assert_equals(elements[i].numberOfScrollEnds, deltas.length); | |
| 202 assert_equals(elements[i].numberOfScrollBegins, deltas.length); | |
| 203 } | |
| 204 }, "Correct number of scroll end and begin events observed."); | |
| 205 | |
| 206 { | |
| 207 var flingTest = async_test("Touchscreen fling doesn't propagate."); | |
| 208 reset(); | |
| 209 | |
| 210 function assertScrollTops(cTop, aTop, bodyTop, step) { | |
| 211 assert_equals(c.scrollTop, cTop, "For id 'c' on step " + step); | |
| 212 assert_equals(a.scrollTop, aTop, "For id 'a' on step " + step); | |
| 213 assert_equals(document.body.scrollTop, bodyTop, "For body on step " + step); | |
| 214 }; | |
| 215 | |
| 216 var frame_actions = [ | |
| 217 function() { | |
| 218 eventSender.gestureFlingStart(10, 10, -1000000, -1000000, "touchscreen"); | |
| 219 }, | |
| 220 flingTest.step_func(function() { | |
| 221 assertScrollTops(0, 0, 0, 1); | |
| 222 }), | |
| 223 flingTest.step_func(function() { | |
| 224 assertScrollTops(100, 0, 0, 2); | |
| 225 }), | |
| 226 flingTest.step_func(function() { | |
| 227 assertScrollTops(100, 0, 0, 3); | |
| 228 }), | |
| 229 flingTest.step_func(function() { | |
| 230 assertScrollTops(100, 0, 0, 4); | |
| 231 flingTest.done(); | |
| 232 }) | |
| 233 ] | |
| 234 | |
| 235 function executeFrameActions(frame_actions) { | |
| 236 var frame = 0; | |
| 237 function raf() { | |
| 238 frame_actions[frame](); | |
| 239 frame++; | |
| 240 if (frame >= frame_actions.length) | |
| 241 return; | |
| 242 window.requestAnimationFrame(raf); | |
| 243 } | |
| 244 window.requestAnimationFrame(raf); | |
| 245 } | |
| 246 | |
| 247 executeFrameActions(frame_actions); | |
| 248 } | |
| 249 </script> | |
| 250 </body> | |
| 251 </html> | |
| OLD | NEW |