Add overflow scroll-snap tests
[WebKit-https.git] / LayoutTests / platform / mac-wk2 / 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-pre.js"></script>
45         <script>
46         var divScrollPositionBeforeGlide;
47         var divScrollPositionBeforeSnap;
48
49         function locationInWindowCoordinates(element)
50         {
51             var position = {};
52             position.x = element.offsetLeft;
53             position.y = element.offsetTop;
54
55             while (element.offsetParent) {
56                 position.x = position.x + element.offsetParent.offsetLeft;
57                 position.y = position.y + element.offsetParent.offsetTop;
58                 if (element == document.getElementsByTagName("body")[0])
59                     break;
60
61                 element = element.offsetParent;
62             }
63
64             return position;
65         }
66
67         function finishTest()
68         {
69             finishJSTest();
70             testRunner.notifyDone();            
71         }
72
73         function checkForScrollSnap(targetLabel)
74         {
75             var divTarget = document.getElementById(targetLabel);
76
77             var actualPosition = divTarget.scrollTop;
78             if (targetLabel == 'horizontalTarget')
79                 actualPosition = divTarget.scrollLeft;
80
81             // The div should have snapped back to the previous position
82             if (actualPosition != divScrollPositionBeforeSnap)
83                 testFailed("div did not snap back to proper location for " + targetLabel +". Expected " + divScrollPositionBeforeSnap + ", but got " + actualPosition);
84             else
85                 testPassed("div honored snap points.");
86
87             if (targetLabel == 'horizontalTarget')
88                 setTimeout(scrollGlideTest('verticalTarget'), 0);
89             else   
90                 finishTest();
91         }
92
93         function scrollSnapTest(targetLabel)
94         {
95             debug("Testing scroll-snap snap for " + targetLabel + ":");
96             var divTarget = document.getElementById(targetLabel);
97  
98             var dx = 0;
99             var dy = 0;
100             if (targetLabel == 'horizontalTarget') {
101                 divScrollPositionBeforeSnap = divTarget.scrollLeft;
102                 dx = -1;
103             } else {
104                 divScrollPositionBeforeSnap = divTarget.scrollTop;
105                 dy = -1;
106             }
107
108             var startPosX = divTarget.offsetLeft + 20;
109             var startPosY = divTarget.offsetTop + 20;
110             eventSender.mouseMoveTo(startPosX, startPosY); // Make sure we are just outside the iFrame
111             eventSender.mouseScrollByWithWheelAndMomentumPhases(dx, dy, 'began', 'none', false);
112             eventSender.mouseScrollByWithWheelAndMomentumPhases(dx, dy, 'changed', 'none', false);
113             eventSender.mouseScrollByWithWheelAndMomentumPhases(dx, dy, 'changed', 'none', false);
114             eventSender.mouseScrollByWithWheelAndMomentumPhases(0, 0, 'ended', 'none', false);
115             eventSender.callAfterScrollingCompletes(function() { return checkForScrollSnap(targetLabel); });
116         }
117
118         function checkForScrollGlide(targetLabel)
119         {
120             var divTarget = document.getElementById(targetLabel);
121
122             var actualPosition = divTarget.scrollTop;
123             var expectedPosition = divTarget.clientHeight;
124             if (targetLabel == 'horizontalTarget') {
125                 actualPosition = divTarget.scrollLeft;
126                 expectedPosition = divTarget.clientWidth;
127             }
128
129             // The div should have scrolled (glided) to the next snap point.
130             if (actualPosition == expectedPosition)
131                 testPassed("div scrolled to next window.");
132             else
133                 testFailed("div did not honor snap points. Expected " + expectedPosition + ", but got " + actualPosition);
134
135             setTimeout(scrollSnapTest(targetLabel), 0);
136         }
137
138         function scrollGlideTest(targetLabel)
139         {
140             debug("Testing scroll-snap glide for " + targetLabel + ":");
141             var divTarget = document.getElementById(targetLabel);
142  
143             var dx = 0;
144             var dy = 0;
145             if (targetLabel == 'horizontalTarget') {
146                 divScrollPositionBeforeGlide = divTarget.scrollLeft;
147                 dx = -1;
148             } else {
149                 divScrollPositionBeforeGlide = divTarget.scrollTop;
150                 dy = -1;
151             }
152
153             var windowPosition = locationInWindowCoordinates(divTarget);
154
155             var startPosX = windowPosition.x + divTarget.clientWidth - 10;
156             var startPosY = windowPosition.y + 50;
157             eventSender.mouseMoveTo(startPosX, startPosY);
158             eventSender.mouseScrollByWithWheelAndMomentumPhases(dx, dy, 'began', 'none', false);
159             eventSender.mouseScrollByWithWheelAndMomentumPhases(dx, dy, 'changed', 'none', false);
160             eventSender.mouseScrollByWithWheelAndMomentumPhases(dx, dy, 'changed', 'none', false);
161             eventSender.mouseScrollByWithWheelAndMomentumPhases(dx, dy, 'changed', 'none', false);
162             eventSender.mouseScrollByWithWheelAndMomentumPhases(0, 0, 'ended', 'none', false);
163             eventSender.mouseScrollByWithWheelAndMomentumPhases(dx, dy, 'none', 'begin', false);
164             eventSender.mouseScrollByWithWheelAndMomentumPhases(dx, dy, 'none', 'continue', false);
165             eventSender.mouseScrollByWithWheelAndMomentumPhases(dx, dy, 'none', 'continue', false);
166             eventSender.mouseScrollByWithWheelAndMomentumPhases(dx, dy, 'none', 'continue', false);
167             eventSender.mouseScrollByWithWheelAndMomentumPhases(dx, dy, 'none', 'continue', false);
168             eventSender.mouseScrollByWithWheelAndMomentumPhases(0, 0, 'none', 'end', false);
169             eventSender.callAfterScrollingCompletes(function() { return checkForScrollGlide(targetLabel); });
170         }
171
172         function onLoad()
173         {
174             if (window.eventSender) {
175                 window.jsTestIsAsync = true;
176                 testRunner.dumpAsText();
177                 testRunner.waitUntilDone();
178
179                 eventSender.monitorWheelEvents();
180                 setTimeout(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         <script src="../../../../../resources/js-test-post.js"></script>
226     </body>
227 </html>