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