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