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