OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <html> |
| 3 <head> |
| 4 <meta charset="utf-8"> |
| 5 <title>Scroll customization methods are called appropriately.</title> |
| 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 test(function() { |
| 63 assert_true('ScrollState' in window, "'ScrollState' in window"); |
| 64 }, "These tests only work with scroll customization enabled."); |
| 65 |
| 66 var originalApplyScrolls = []; |
| 67 var originalDistributeScrolls = []; |
| 68 var deltas = [-85, -75, -65, -55, -45]; |
| 69 |
| 70 var elements = [ |
| 71 document.getElementById("d"), |
| 72 document.getElementById("c"), |
| 73 document.getElementById("b"), |
| 74 document.getElementById("a"), |
| 75 document.scrollingElement]; |
| 76 |
| 77 var scrollableElements = [elements[1], elements[3], elements[4]]; |
| 78 |
| 79 document.scrollingElement.id = "scrollingElement"; |
| 80 |
| 81 for (var i = 0; i < elements.length; ++i) { |
| 82 originalApplyScrolls[i] = elements[i].applyScroll; |
| 83 originalDistributeScrolls[i] = elements[i].distributeScroll; |
| 84 } |
| 85 |
| 86 function reset() { |
| 87 for (var i = 0; i < elements.length; ++i) { |
| 88 var j = i; |
| 89 elements[i].scrollTop = 0; |
| 90 elements[i].unappliedDeltaY = []; |
| 91 elements[i].distributedDeltaY = []; |
| 92 elements[i].numberOfScrollBegins = 0; |
| 93 elements[i].numberOfScrollEnds = 0; |
| 94 |
| 95 elements[i].setApplyScroll((function(originalApplyScroll, scrollState) { |
| 96 originalApplyScroll.call(this, scrollState); |
| 97 if (!scrollState.isEnding && !scrollState.isBeginning) |
| 98 this.unappliedDeltaY.push(scrollState.deltaY); |
| 99 }).bind(elements[i], originalApplyScrolls[i])); |
| 100 |
| 101 elements[i].setDistributeScroll((function(originalDistributeScroll, scrollSt
ate) { |
| 102 if (scrollState.isBeginning) |
| 103 this.numberOfScrollBegins++; |
| 104 else if (scrollState.isEnding) |
| 105 this.numberOfScrollEnds++; |
| 106 else |
| 107 this.distributedDeltaY.push(scrollState.deltaY); |
| 108 originalDistributeScroll.call(this, scrollState); |
| 109 }).bind(elements[i], originalDistributeScrolls[i])); |
| 110 } |
| 111 } |
| 112 |
| 113 function applyDelta(d) { |
| 114 eventSender.gestureScrollBegin(10, 10); |
| 115 eventSender.gestureScrollUpdate(0, d); |
| 116 eventSender.gestureScrollEnd(0, 0); |
| 117 } |
| 118 |
| 119 if ('ScrollState' in window) { |
| 120 test(function() { |
| 121 reset(); |
| 122 |
| 123 // Scroll five times, with three scrollable elements. |
| 124 var cScrollTop = [85, 100, 100, 100, 100]; |
| 125 var aScrollTop = [0, 0, 65, 100, 100]; |
| 126 var scrollingElementScrollTop = [0, 0, 0, 0, 45]; |
| 127 |
| 128 for (var i = 0; i < deltas.length; ++i) { |
| 129 applyDelta(deltas[i]); |
| 130 assert_equals(a.scrollTop, aScrollTop[i], "For id 'a' on step " + i); |
| 131 assert_equals(c.scrollTop, cScrollTop[i], "For id 'c' on step " + i); |
| 132 assert_equals(document.scrollingElement.scrollTop, scrollingElementScrollT
op[i], "For scrollingElement on step " + i); |
| 133 } |
| 134 }, "Scroll offsets are modified correctly."); |
| 135 |
| 136 test(function() { |
| 137 reset(); |
| 138 |
| 139 // Scroll five times, with five elements. |
| 140 var unapplied = [ |
| 141 // d, the innermost element, never applies any scroll. |
| 142 [-85, -75, -65, -55, -45], |
| 143 // c applies the first two scrolls, and then hits its scroll extents. |
| 144 [0, 0, -65, -55, -45], |
| 145 // b doesn't scroll, and so leaves the same deltas unapplied as c. |
| 146 [0, 0, -65, -55, -45], |
| 147 // a hits its scroll extent on the second last step. |
| 148 [0, 0, 0, 0, -45], |
| 149 // The scrollingElement performs the frame scroll. |
| 150 [0, 0, 0, 0, 0]]; |
| 151 |
| 152 for (var i = 0; i < deltas.length; ++i) |
| 153 applyDelta(deltas[i]); |
| 154 |
| 155 for (var i = 0; i < elements.length; ++i) { |
| 156 var el = elements[i]; |
| 157 // Every element sees the same deltas being distributed. |
| 158 assert_array_equals(el.distributedDeltaY, deltas, "distributed delta for "
+ el.id); |
| 159 assert_array_equals(el.unappliedDeltaY, unapplied[i], "unapplied delta for
" + el.id); |
| 160 } |
| 161 |
| 162 // Ensure that the document leaves scroll unapplied when appropriate. |
| 163 var documentUnapplied = document.scrollingElement.unappliedDeltaY; |
| 164 applyDelta(-4000); |
| 165 assert_equals(documentUnapplied[documentUnapplied.length - 1], 0); |
| 166 applyDelta(-4000); |
| 167 assert_equals(documentUnapplied[documentUnapplied.length - 1], -4000); |
| 168 }, "Correct amount of delta is consumed."); |
| 169 |
| 170 test(function() { |
| 171 reset(); |
| 172 // Consume one pixel of delta per call to applyScroll. |
| 173 for (var i = 0; i < elements.length; ++i) { |
| 174 if (scrollableElements.indexOf(elements[i]) == -1) |
| 175 continue; |
| 176 elements[i].setApplyScroll((function(originalApplyScroll, scrollState) { |
| 177 if (scrollState.deltaY !== 0) |
| 178 scrollState.consumeDelta(0, -1); |
| 179 originalApplyScroll.call(this, scrollState); |
| 180 }).bind(elements[i], originalApplyScrolls[i])); |
| 181 } |
| 182 // Scroll five times, with three scrollable elements. |
| 183 // The scroll distance is decreased more the higher up the scroll chain the
element is. |
| 184 var cScrollTop = [85 - 1, 100, 100, 100, 100]; |
| 185 var aScrollTop = [0, 0, 65 - 2, 100, 100]; |
| 186 var scrollingElementScrollTop = [0, 0, 0, 0, 45 - 3]; |
| 187 |
| 188 for (var i = 0; i < deltas.length; ++i) { |
| 189 applyDelta(deltas[i]); |
| 190 assert_equals(c.scrollTop, cScrollTop[i], "For id 'c' on step " + i); |
| 191 assert_equals(a.scrollTop, aScrollTop[i], "For id 'a' on step " + i); |
| 192 assert_equals(document.scrollingElement.scrollTop, scrollingElementScrollT
op[i], "For scrollingElement on step " + i); |
| 193 } |
| 194 }, "Consuming deltas prevents scrolling."); |
| 195 |
| 196 test(function() { |
| 197 reset(); |
| 198 |
| 199 for (var i = 0; i < deltas.length; ++i) |
| 200 applyDelta(deltas[i]); |
| 201 for (var i = 0; i < elements.length; ++i) { |
| 202 assert_equals(elements[i].numberOfScrollBegins, deltas.length, "Incorrect
number of begin events for " + elements[i].id); |
| 203 assert_equals(elements[i].numberOfScrollEnds, deltas.length, "Incorrect nu
mber of end events for " + elements[i].id); |
| 204 } |
| 205 }, "Correct number of scroll end and begin events observed."); |
| 206 |
| 207 test(function() { |
| 208 // toPerform is an array of length 4, specifying whether to |
| 209 // perform method |original| on |element| for scroll begin, first |
| 210 // update, second update and scroll end. |
| 211 function performSomeOf(element, original, toPerform) { |
| 212 var updateCount = 0; |
| 213 return function(scrollState) { |
| 214 if (scrollState.isBeginning && scrollState.isEnding) |
| 215 throw "performSomeOf requires that the scroll not both begin and end"; |
| 216 |
| 217 var isUpdate = !scrollState.isBeginning && !scrollState.isEnding; |
| 218 |
| 219 if ((scrollState.isBeginning && toPerform[0]) || |
| 220 (scrollState.isEnding && toPerform[3]) || |
| 221 (isUpdate && updateCount === 0 && toPerform[1]) || |
| 222 (isUpdate && updateCount > 0 && toPerform[2])) { |
| 223 original.call(element, scrollState); |
| 224 } |
| 225 |
| 226 if (isUpdate) |
| 227 updateCount++; |
| 228 }; |
| 229 } |
| 230 |
| 231 // Generates an array of all arrays of booleans of length n. |
| 232 // e.g. genPermutations(2) => |
| 233 // [[true, true], [true, false], [false, true], [false, false]]. |
| 234 function genPermutations(n) { |
| 235 if (n === 0) |
| 236 return [[]]; |
| 237 var perms = genPermutations(n - 1); |
| 238 return perms.map(function(x) { |
| 239 return x.concat([true]); |
| 240 }).concat(perms.map(function(x) { |
| 241 return x.concat([false]); |
| 242 })); |
| 243 } |
| 244 |
| 245 function applyDeltaWithTwoUpdates(d) { |
| 246 eventSender.gestureScrollBegin(10, 10); |
| 247 eventSender.gestureScrollUpdate(0, d/2); |
| 248 eventSender.gestureScrollUpdate(0, d/2); |
| 249 eventSender.gestureScrollEnd(0, 0); |
| 250 } |
| 251 |
| 252 var scroller = document.scrollingElement; |
| 253 |
| 254 for (applyScrollPermutation of genPermutations(4)) { |
| 255 reset(); |
| 256 scroller.setApplyScroll(performSomeOf(scroller, scroller.applyScroll, appl
yScrollPermutation)); |
| 257 |
| 258 for (distributeScrollPermutation of genPermutations(4)) { |
| 259 scroller.setDistributeScroll(performSomeOf(scroller, scroller.distribute
Scroll, distributeScrollPermutation)); |
| 260 applyDeltaWithTwoUpdates(-50); |
| 261 } |
| 262 } |
| 263 }, "Ensure that any subset of the native scroll methods can be called, without
crashing"); |
| 264 |
| 265 { |
| 266 // NOTE - this async test needs to be run last, as it shares state with the |
| 267 // other tests. If other tests are run after it, they'll modify the state |
| 268 // while this test is still running. |
| 269 var flingTest = async_test("Touchscreen fling doesn't propagate."); |
| 270 reset(); |
| 271 |
| 272 function assertScrollTops(cTop, aTop, scrollingElementTop, step) { |
| 273 assert_equals(c.scrollTop, cTop, "For id 'c' on step " + step); |
| 274 assert_equals(a.scrollTop, aTop, "For id 'a' on step " + step); |
| 275 assert_equals(document.scrollingElement.scrollTop, scrollingElementTop, "F
or scrollingElement on step " + step); |
| 276 }; |
| 277 |
| 278 var frame_actions = [ |
| 279 function() { |
| 280 eventSender.gestureFlingStart(10, 10, -1000000, -1000000, "touchscreen")
; |
| 281 }, |
| 282 flingTest.step_func(function() { |
| 283 assertScrollTops(0, 0, 0, 1); |
| 284 }), |
| 285 flingTest.step_func(function() { |
| 286 assertScrollTops(100, 0, 0, 2); |
| 287 }), |
| 288 flingTest.step_func(function() { |
| 289 assertScrollTops(100, 0, 0, 3); |
| 290 }), |
| 291 flingTest.step_func(function() { |
| 292 assertScrollTops(100, 0, 0, 4); |
| 293 flingTest.done(); |
| 294 }) |
| 295 ] |
| 296 |
| 297 function executeFrameActions(frame_actions) { |
| 298 var frame = 0; |
| 299 function raf() { |
| 300 frame_actions[frame](); |
| 301 frame++; |
| 302 if (frame >= frame_actions.length) |
| 303 return; |
| 304 window.requestAnimationFrame(raf); |
| 305 } |
| 306 window.requestAnimationFrame(raf); |
| 307 } |
| 308 |
| 309 executeFrameActions(frame_actions); |
| 310 } |
| 311 } |
| 312 |
| 313 </script> |
| 314 </body> |
| 315 </html> |
OLD | NEW |