Implement the updated port/area-based Scroll Snap Module Level 1 Spec
[WebKit-https.git] / LayoutTests / tiled-drawing / scrolling / scroll-snap / scroll-snap-mandatory-borders.html
1 <!DOCTYPE html>
2 <html>
3     <head>
4         <style>
5             .horizontalGallery {
6                 width: 300px;
7                 height: 300px;
8                 overflow-y: hidden;
9                 overflow-x: auto;
10                 border-top: 20px solid black;
11                 border-bottom: 10px solid black;
12                 border-left: 15px solid black;
13                 border-right: 9px solid black;
14                 margin-bottom: 2px;
15                 -webkit-overflow-scrolling: touch;
16                 scroll-snap-type: x mandatory;
17             }
18             .horizontalGalleryDrawer {
19                 width: 1800px;
20                 height: 300px;
21             }
22             .verticalGallery {
23                 width: 300px;
24                 height: 300px;
25                 display: inline-block;
26                 overflow-x: hidden;
27                 overflow-y: auto;
28                 border-top: 20px solid black;
29                 border-bottom: 10px solid black;
30                 border-left: 15px solid black;
31                 border-right: 9px solid black;
32                 margin-top: 2px;
33                 -webkit-overflow-scrolling: touch;
34                 scroll-snap-type: y mandatory;
35             }
36             .verticalGalleryDrawer {
37                 width: 300px;
38                 height: 1800px;
39             }
40             .colorBox {
41                 height: 300px;
42                 width: 300px;
43                 float: left;
44                 scroll-snap-align: start;
45             }
46             #itemH0, #itemV0 { background-color: red; }
47             #itemH1, #itemV1 { background-color: green; }
48             #itemH2, #itemV2 { background-color: blue; }
49             #itemH3, #itemV3 { background-color: aqua; }
50             #itemH4, #itemV4 { background-color: yellow; }
51             #itemH5, #itemV5 { background-color: fuchsia; }
52         </style>
53         <script src="../../../resources/js-test.js"></script>
54         <script>
55         window.jsTestIsAsync = true;
56
57         var divScrollPositionBeforeGlide;
58         var divScrollPositionBeforeSnap;
59
60         function locationInWindowCoordinates(element)
61         {
62             var position = {};
63             position.x = element.offsetLeft;
64             position.y = element.offsetTop;
65
66             while (element.offsetParent) {
67                 position.x = position.x + element.offsetParent.offsetLeft;
68                 position.y = position.y + element.offsetParent.offsetTop;
69                 if (element == document.getElementsByTagName("body")[0])
70                     break;
71
72                 element = element.offsetParent;
73             }
74
75             return position;
76         }
77
78         function checkForScrollSnap(targetLabel)
79         {
80             var divTarget = document.getElementById(targetLabel);
81
82             var actualPosition = divTarget.scrollTop;
83             if (targetLabel == 'horizontalTarget')
84                 actualPosition = divTarget.scrollLeft;
85
86             // The div should have snapped back to the previous position
87             if (actualPosition != divScrollPositionBeforeSnap)
88                 testFailed("div did not snap back to proper location for " + targetLabel +". Expected " + divScrollPositionBeforeSnap + ", but got " + actualPosition);
89             else
90                 testPassed("div honored snap points.");
91
92             if (targetLabel == 'horizontalTarget')
93                 setTimeout(function() { scrollGlideTest('verticalTarget') }, 0);
94             else   
95                 finishJSTest();
96         }
97
98         function scrollSnapTest(targetLabel)
99         {
100             debug("Testing scroll-snap snap for " + targetLabel + ":");
101             var divTarget = document.getElementById(targetLabel);
102  
103             var dx = 0;
104             var dy = 0;
105             if (targetLabel == 'horizontalTarget') {
106                 divScrollPositionBeforeSnap = divTarget.scrollLeft;
107                 dx = -1;
108             } else {
109                 divScrollPositionBeforeSnap = divTarget.scrollTop;
110                 dy = -1;
111             }
112
113             var windowPosition = locationInWindowCoordinates(divTarget);
114
115             var startPosX = windowPosition.x + 0.5 * divTarget.clientWidth;
116             var startPosY = windowPosition.y + 0.5 * divTarget.clientHeight;
117             eventSender.mouseMoveTo(startPosX, startPosY); // Make sure we are just outside the iFrame
118             eventSender.mouseScrollByWithWheelAndMomentumPhases(dx, dy, 'began', 'none');
119             eventSender.mouseScrollByWithWheelAndMomentumPhases(dx, dy, 'changed', 'none');
120             eventSender.mouseScrollByWithWheelAndMomentumPhases(dx, dy, 'changed', 'none');
121             eventSender.mouseScrollByWithWheelAndMomentumPhases(0, 0, 'ended', 'none');
122             eventSender.callAfterScrollingCompletes(function() { return checkForScrollSnap(targetLabel); });
123         }
124
125         function checkForScrollGlide(targetLabel)
126         {
127             var divTarget = document.getElementById(targetLabel);
128
129             var actualPosition = divTarget.scrollTop;
130             var expectedPosition = divTarget.clientHeight;
131             if (targetLabel == 'horizontalTarget') {
132                 actualPosition = divTarget.scrollLeft;
133                 expectedPosition = divTarget.clientWidth;
134             }
135
136             // The div should have scrolled (glided) to the next snap point.
137             if (actualPosition == expectedPosition)
138                 testPassed("div scrolled to next window.");
139             else
140                 testFailed("div did not honor snap points. Expected " + expectedPosition + ", but got " + actualPosition);
141
142             setTimeout(function() { scrollSnapTest(targetLabel) }, 0);
143         }
144
145         function scrollGlideTest(targetLabel)
146         {
147             debug("Testing scroll-snap glide for " + targetLabel + ":");
148             var divTarget = document.getElementById(targetLabel);
149  
150             var dx = 0;
151             var dy = 0;
152             if (targetLabel == 'horizontalTarget') {
153                 divScrollPositionBeforeGlide = divTarget.scrollLeft;
154                 dx = -1;
155             } else {
156                 divScrollPositionBeforeGlide = divTarget.scrollTop;
157                 dy = -1;
158             }
159
160             var windowPosition = locationInWindowCoordinates(divTarget);
161
162             var startPosX = windowPosition.x + divTarget.clientWidth - 10;
163             var startPosY = windowPosition.y + 50;
164             eventSender.mouseMoveTo(startPosX, startPosY);
165             eventSender.mouseScrollByWithWheelAndMomentumPhases(dx, dy, 'began', 'none');
166             eventSender.mouseScrollByWithWheelAndMomentumPhases(dx, dy, 'changed', 'none');
167             eventSender.mouseScrollByWithWheelAndMomentumPhases(dx, dy, 'changed', 'none');
168             eventSender.mouseScrollByWithWheelAndMomentumPhases(dx, dy, 'changed', 'none');
169             eventSender.mouseScrollByWithWheelAndMomentumPhases(0, 0, 'ended', 'none');
170             eventSender.mouseScrollByWithWheelAndMomentumPhases(dx, dy, 'none', 'begin');
171             eventSender.mouseScrollByWithWheelAndMomentumPhases(dx, dy, 'none', 'continue');
172             eventSender.mouseScrollByWithWheelAndMomentumPhases(0, 0, 'none', 'end');
173             eventSender.callAfterScrollingCompletes(function() { return checkForScrollGlide(targetLabel); });
174         }
175
176         function onLoad()
177         {
178             if (window.eventSender) {
179                 eventSender.monitorWheelEvents();
180                 setTimeout(function() { scrollGlideTest('horizontalTarget') }, 0);
181             } else {
182                 var messageLocationH = document.getElementById('itemH0');
183                 var message = document.createElement('div');
184                 message.innerHTML = "<p>This test is better run under DumpRenderTree.<br/>To manually test it, place the mouse pointer<br/>"
185                     + "inside the red region at the top of the page,<br/>and then use the mouse wheel or a two-finger<br/>swipe to make a"
186                     + "small swipe gesture with<br/>some momentum.<br/><br/>"
187                     + "The region should scroll to show a green region.<br/><br/>"
188                     + "Next, perform a small scroll gesture that does<br/>not involve momentum. You should begin to<br/>see one of the colors "
189                     + "to the side of the current<br/>green box. When you release the wheel, the<br/>region should scroll back to a single color.";
190                 messageLocationH.appendChild(message);
191
192                 var messageLocationV = document.getElementById('itemV0');
193                 var message = document.createElement('div');
194                 message.innerHTML = "<p>You should also be able to repeat these tests steps for this vertical region.<br/>"
195                 messageLocationV.appendChild(message);
196             }
197         }
198         </script>
199     </head>
200     <body onload="onLoad();">
201         <div style="position: relative; width: 300px">
202             <div>Tests that the scroll-snap feature works properly in overflow regions.</div>
203             <div class="horizontalGallery" id="horizontalTarget">
204                 <div class="horizontalGalleryDrawer">
205                     <div id="itemH0" class="colorBox"></div>
206                     <div id="itemH1" class="colorBox"></div>
207                     <div id="itemH2" class="colorBox"></div>
208                     <div id="itemH3" class="colorBox"></div>
209                     <div id="itemH4" class="colorBox"></div>
210                     <div id="itemH5" class="colorBox"></div>
211                 </div>
212             </div>
213             <div class="verticalGallery" id="verticalTarget">
214                 <div class="verticalGalleryDrawer">
215                     <div id="itemV0" class="colorBox"></div>
216                     <div id="itemV1" class="colorBox"></div>
217                     <div id="itemV2" class="colorBox"></div>
218                     <div id="itemV3" class="colorBox"></div>
219                     <div id="itemV4" class="colorBox"></div>
220                     <div id="itemV5" class="colorBox"></div>
221                 </div>
222             </div>
223             <div id="console"></div>
224         </div>
225     </body>
226 </html>