| OLD | NEW |
| (Empty) | |
| 1 <!DOCTYPE html> |
| 2 <script src="../../../resources/testharness.js"></script> |
| 3 <script src="../../../resources/testharnessreport.js"></script> |
| 4 <style> |
| 5 |
| 6 body { margin: 80px 0; height: 5000px; } |
| 7 #a, #b { height: 200px; width: 200px; } |
| 8 #c { height: 400px; } |
| 9 #left, #c { display: inline-block; width: 200px; vertical-align: top; } |
| 10 #a.f { position: fixed; top: 30px; left: 150px; } |
| 11 #a { background-color: #fcc; } |
| 12 #b { background-color: #cfc; } |
| 13 #c { background-color: #ccf; } |
| 14 |
| 15 </style> |
| 16 <div id="left"> |
| 17 <div id="a"></div> |
| 18 <div id="b"></div> |
| 19 </div><div id="c"></div> |
| 20 <script> |
| 21 |
| 22 internals.runtimeFlags.scrollAnchoringEnabled = true; |
| 23 |
| 24 // The scroll handler marks #a as position: fixed when scroll offset > 300px. |
| 25 // This triggers layout during which we anchor to #b, which has moved up 200px. |
| 26 // After our anchoring adjustment, the scroll offset is < 300px again, so the |
| 27 // scroll handler removes position: fixed from #a, triggering a second layout, |
| 28 // during which #b "bounces" back to its original location. |
| 29 // |
| 30 // The desired behavior here is for ScrollAnchor to follow #b back the first |
| 31 // time it bounces, but not follow a second bounce. |
| 32 // |
| 33 // There are two failure modes we are concerned about: |
| 34 // |
| 35 // - If ScrollAnchor doesn't follow #b back to its original position during the |
| 36 // FIRST bounce, we've "trapped" the user above 300px. Every time they try to |
| 37 // scroll past this point we will push them back up. |
| 38 // |
| 39 // - If ScrollAnchor follows #b religiously, we get stuck in an infinite cycle |
| 40 // of continuous bouncing, which looks hilariously broken. |
| 41 |
| 42 var a = document.querySelector('#a'); |
| 43 var fixed = false; |
| 44 onscroll = function() { |
| 45 var y = scrollY; |
| 46 if (fixed == (y > 300)) |
| 47 return; |
| 48 if (y < 130) { |
| 49 // Clear the scroll anchor. |
| 50 scrollBy(0, 10); |
| 51 scrollBy(0, -10); |
| 52 } |
| 53 a.className = (fixed = !fixed) ? "f" : ""; |
| 54 }; |
| 55 |
| 56 var frame = () => new Promise((resolve) => { requestAnimationFrame(resolve); }); |
| 57 |
| 58 var waitFor = function(condition, failmsg, deadline) { |
| 59 if (!deadline) deadline = Date.now() + 1000; |
| 60 if (condition()) return Promise.resolve(); |
| 61 else if (Date.now() > deadline) return Promise.reject(failmsg); |
| 62 else return frame().then(() => waitFor(condition, failmsg, deadline)); |
| 63 }; |
| 64 |
| 65 var waitFrames = function(n, condition, failmsg) { |
| 66 var p = Promise.resolve(); |
| 67 var check = () => (!condition || condition() ? |
| 68 Promise.resolve() : Promise.reject(failmsg)); |
| 69 while (n--) |
| 70 p = p.then(check).then(frame); |
| 71 return p.then(check); |
| 72 }; |
| 73 |
| 74 var scrollSettlesAt = function(expectedY) { |
| 75 return waitFor(() => (scrollY == expectedY), |
| 76 "scroll did not reach " + expectedY) |
| 77 .then(() => waitFrames(3)) |
| 78 .then(() => waitFrames(3, () => (scrollY == expectedY), |
| 79 "scroll did not stay at " + expectedY)); |
| 80 }; |
| 81 |
| 82 promise_test(function() { |
| 83 return Promise.resolve() |
| 84 |
| 85 // The 320px offset tests for the first failure mode. |
| 86 .then(() => { eventSender.continuousMouseScrollBy(0, -320); }) |
| 87 .then(() => scrollSettlesAt(320)) |
| 88 |
| 89 .then(() => { scrollTo(0, 0); }) |
| 90 .then(() => frame()) |
| 91 |
| 92 // The 360px offset tests for the second failure mode. |
| 93 .then(() => { eventSender.continuousMouseScrollBy(0, -360); }) |
| 94 .then(() => scrollSettlesAt(360)); |
| 95 |
| 96 }, "Scroll anchoring with scroll event handlers"); |
| 97 |
| 98 </script> |
| OLD | NEW |