Add overflow scroll-snap tests
[WebKit-https.git] / LayoutTests / platform / mac-wk2 / tiled-drawing / scrolling / scroll-snap / scroll-snap-iframe.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: 2px;
11             }
12             .verticalGallery {
13                 width: 300px;
14                 height: 300px;
15                 display: inline-block;
16                 overflow-x: hidden;
17                 overflow-y: auto;
18                 margin: 2px;
19             }
20         </style>
21         <script src="../../../../../resources/js-test-pre.js"></script>
22         <script>
23         var iframeScrollPositionBeforeGlide;
24         var divScrollPositionBeforeSnap;
25
26         function locationInWindowCoordinates(element)
27         {
28             var position = {};
29             position.x = element.offsetLeft;
30             position.y = element.offsetTop;
31
32             while (element.offsetParent) {
33                 position.x = position.x + element.offsetParent.offsetLeft;
34                 position.y = position.y + element.offsetParent.offsetTop;
35                 if (element == document.getElementsByTagName("body")[0])
36                     break;
37
38                 element = element.offsetParent;
39             }
40
41             return position;
42         }
43
44         function finishTest()
45         {
46             finishJSTest();
47             testRunner.notifyDone();            
48         }
49
50         function checkForScrollSnap(targetLabel)
51         {
52             var iframeTarget = document.getElementById(targetLabel);
53
54             var actualPosition = iframeTarget.scrollTop;
55             if (targetLabel == 'horizontalTarget')
56                 actualPosition = iframeTarget.scrollLeft;
57
58             // The div should have snapped back to the previous position
59             if (actualPosition != divScrollPositionBeforeSnap)
60                 testFailed("div did not snap back to proper location for " + targetLabel +". Expected " + divScrollPositionBeforeSnap + ", but got " + actualPosition);
61             else
62                 testPassed("div honored snap points.");
63
64             if (targetLabel == 'horizontalTarget')
65                 setTimeout(scrollGlideTest('verticalTarget'), 0);
66             else   
67                 finishTest();
68         }
69
70         function scrollSnapTest(targetLabel)
71         {
72             debug("Testing scroll-snap snap for " + targetLabel + ":");
73             var iframeTarget = document.getElementById(targetLabel);
74  
75             var dx = 0;
76             var dy = 0;
77             if (targetLabel == 'horizontalTarget') {
78                 divScrollPositionBeforeSnap = iframeTarget.scrollLeft;
79                 dx = -1;
80             } else {
81                 divScrollPositionBeforeSnap = iframeTarget.scrollTop;
82                 dy = -1;
83             }
84
85             var startPosX = iframeTarget.offsetLeft + 20;
86             var startPosY = iframeTarget.offsetTop + 20;
87             eventSender.mouseMoveTo(startPosX, startPosY); // Make sure we are just outside the iFrame
88             eventSender.mouseScrollByWithWheelAndMomentumPhases(dx, dy, 'began', 'none', false);
89             eventSender.mouseScrollByWithWheelAndMomentumPhases(dx, dy, 'changed', 'none', false);
90             eventSender.mouseScrollByWithWheelAndMomentumPhases(dx, dy, 'changed', 'none', false);
91             eventSender.mouseScrollByWithWheelAndMomentumPhases(0, 0, 'ended', 'none', false);
92             eventSender.callAfterScrollingCompletes(function() { return checkForScrollSnap(targetLabel); });
93         }
94
95         function checkForScrollGlide(targetLabel)
96         {
97             var iframeTarget = document.getElementById(targetLabel);
98             var iframeBody = window.frames[targetLabel].document.body;
99
100             var actualPosition = iframeBody.scrollTop;
101             var expectedPosition = iframeTarget.clientHeight;
102             if (targetLabel == 'horizontalTarget') {
103                 actualPosition = iframeBody.scrollLeft;
104                 expectedPosition = iframeTarget.clientWidth;
105             }
106
107             // The div should have scrolled (glided) to the next snap point.
108             if (actualPosition == expectedPosition)
109                 testPassed("div scrolled to next window.");
110             else
111                 testFailed("div did not honor snap points. Expected " + expectedPosition + ", but got " + actualPosition);
112
113             setTimeout(scrollSnapTest(targetLabel), 0);
114         }
115
116         function scrollGlideTest(targetLabel)
117         {
118             debug("Testing scroll-snap glide for " + targetLabel + ":");
119             var iframeTarget = document.getElementById(targetLabel);
120             var iframeBody = window.frames[targetLabel].document.body;
121  
122             var dx = 0;
123             var dy = 0;
124             if (targetLabel == 'horizontalTarget') {
125                 iframeScrollPositionBeforeGlide = iframeBody.scrollLeft;
126                 dx = -1;
127             } else {
128                 iframeScrollPositionBeforeGlide = iframeBody.scrollTop;
129                 dy = -1;
130             }
131
132             var windowPosition = locationInWindowCoordinates(iframeTarget);
133
134             var startPosX = windowPosition.x + iframeTarget.clientWidth - 10;
135             var startPosY = windowPosition.y + 50;
136             eventSender.mouseMoveTo(startPosX, startPosY);
137             eventSender.mouseScrollByWithWheelAndMomentumPhases(dx, dy, 'began', 'none', false);
138             eventSender.mouseScrollByWithWheelAndMomentumPhases(dx, dy, 'changed', 'none', false);
139             eventSender.mouseScrollByWithWheelAndMomentumPhases(dx, dy, 'changed', 'none', false);
140             eventSender.mouseScrollByWithWheelAndMomentumPhases(dx, dy, 'changed', 'none', false);
141             eventSender.mouseScrollByWithWheelAndMomentumPhases(0, 0, 'ended', 'none', false);
142             eventSender.mouseScrollByWithWheelAndMomentumPhases(dx, dy, 'none', 'begin', false);
143             eventSender.mouseScrollByWithWheelAndMomentumPhases(dx, dy, 'none', 'continue', false);
144             eventSender.mouseScrollByWithWheelAndMomentumPhases(dx, dy, 'none', 'continue', false);
145             eventSender.mouseScrollByWithWheelAndMomentumPhases(dx, dy, 'none', 'continue', false);
146             eventSender.mouseScrollByWithWheelAndMomentumPhases(dx, dy, 'none', 'continue', false);
147             eventSender.mouseScrollByWithWheelAndMomentumPhases(0, 0, 'none', 'end', false);
148             eventSender.callAfterScrollingCompletes(function() { return checkForScrollGlide(targetLabel); });
149         }
150
151         function onLoad()
152         {
153             if (window.eventSender) {
154                 window.jsTestIsAsync = true;
155                 testRunner.dumpAsText();
156                 testRunner.waitUntilDone();
157
158                 eventSender.monitorWheelEvents();
159                 setTimeout(scrollGlideTest('horizontalTarget'), 0);
160             }
161         }
162         </script>
163     </head>
164     <body onload="onLoad();">
165         <div style="position: relative; width: 300px;">
166             <div>Tests that the scroll-snap feature works properly in iframes.</div>
167             <iframe id="horizontalTarget" class="horizontalGallery" src="resources/horizontal-mainframe.html" style="height: 300px; width: 300px"></iframe>
168             <iframe id="verticalTarget" class="verticalGallery" src="resources/vertical-mainframe.html"  style="height: 300px; width: 300px"></iframe>
169             <div id="console"></div>
170         </div>
171         <script src="../../../../../resources/js-test-post.js"></script>
172     </body>
173 </html>