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