summaryrefslogtreecommitdiffstats
path: root/layout/base/tests/test_scroll_snapping_scrollbars.html
blob: 7caf62a596343a0ddff2b4d63f180c53b07940ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<!DOCTYPE HTML>
<html>
<head>
  <title>Test for scroll snapping</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>

<p id="display"></p>
<div id="sc" style="margin: 0px; padding: 0px; overflow: scroll; width:250px; height: 250px;">
  <div id="sd" style="margin: 0px; padding: 0px; width: 1250px; height: 1250px;"></div>
</div>

<pre id="test">
<script class="testbody" type="text/javascript">

var runtime = SpecialPowers.Cc["@mozilla.org/xre/app-info;1"]
                                .getService(SpecialPowers.Ci.nsIXULRuntime);
var isMac = navigator.platform.indexOf("Mac") != -1;
var isGtk = runtime.widgetToolkit.indexOf("gtk") != -1;
var isWin = navigator.platform.indexOf("Win") != -1;
var isSN = /mac os x 10\.6/.test(navigator.userAgent.toLowerCase());

// Half of the scrollbar control width, in CSS pixels
var scrollbarOffset = isWin ? 8 : 5;

// OSX 10.6 scroll bar thumbs are off-center due to the bundling of buttons on one end
// of the scroll bar frame.
var scrollbarCenter = isSN ? 100 : 125;

var testCases = [
  {
    "description"   : "Drag scrollbar left, expect scroll snapping.",
    "snapCoord"     : "500px 500px",
    "startScroll"   : { "x" : 500, "y" : 500 },
    "endScroll"     : { "x" : 500, "y" : 500 },
    "mousePosition" : { "x" : scrollbarCenter, "y" : 250 - scrollbarOffset },
    "mouseOffset"   : { "x" : -10, "y" : 0 },
    "duration"      : "0",
    "runMac"        : true,
    "runGtk"        : true,
    "runWin"        : true
  },
  {
    "description"   : "Drag scrollbar right, expect scroll snapping.",
    "snapCoord"     : "500px 500px",
    "startScroll"   : { "x" : 500, "y" : 500 },
    "endScroll"     : { "x" : 500, "y" : 500 },
    "mousePosition" : { "x" : scrollbarCenter, "y" : 250 - scrollbarOffset },
    "mouseOffset"   : { "x" : 10, "y" : 0 },
    "duration"      : "0",
    "runMac"        : true,
    "runGtk"        : true,
    "runWin"        : true
  },
  {
    "description"   : "Drag scrollbar up, expect scroll snapping.",
    "snapCoord"     : "500px 500px",
    "startScroll"   : { "x" : 500, "y" : 500 },
    "endScroll"     : { "x" : 500, "y" : 500 },
    "mousePosition" : { "x" : 250 - scrollbarOffset, "y" :  scrollbarCenter },
    "mouseOffset"   : { "x" : 0, "y" : -10 },
    "duration"      : "0",
    "runMac"        : true,
    "runGtk"        : true,
    "runWin"        : true
  },
  {
    "description"   : "Drag scrollbar down, expect scroll snapping.",
    "snapCoord"     : "500px 500px",
    "startScroll"   : { "x" : 500, "y" : 500 },
    "endScroll"     : { "x" : 500, "y" : 500 },
    "mousePosition" : { "x" : 250 - scrollbarOffset, "y" :  scrollbarCenter },
    "mouseOffset"   : { "x" : 0, "y" : 10 },
    "duration"      : "0",
    "runMac"        : true,
    "runGtk"        : true,
    "runWin"        : true
  },
  {
    "description"   : "Page scrollbar left, expect scroll snapping.",
    "snapCoord"     : "500px 500px, 1000px 500px",
    "startScroll"   : { "x" : 1000, "y" : 500 },
    "endScroll"     : { "x" : 500, "y" : 500 },
    "mousePosition" : { "x" : 50, "y" : 250 - scrollbarOffset },
    "mouseOffset"   : { "x" : 0, "y" : 0 },
    "duration"      : "0",
    "runMac"        : true,
    "runGtk"        : true,
    "runWin"        : true
  },
  {
    "description"   : "Page scrollbar right, expect scroll snapping.",
    "snapCoord"     : "500px 500px, 0px 500px",
    "startScroll"   : { "x" : 0, "y" : 500 },
    "endScroll"     : { "x" : 500, "y" : 500 },
    "mousePosition" : { "x" : 200, "y" : 250 - scrollbarOffset },
    "mouseOffset"   : { "x" : 0, "y" : 0 },
    "duration"      : "0",
    "runMac"        : true,
    "runGtk"        : true,
    "runWin"        : true
  },
  {
    "description"   : "Page scrollbar up, expect scroll snapping.",
    "snapCoord"     : "500px 500px, 500px 1000px",
    "startScroll"   : { "x" : 500, "y" : 1000 },
    "endScroll"     : { "x" : 500, "y" : 500 },
    "mousePosition" : { "x" : 250 - scrollbarOffset, "y" : 50 },
    "mouseOffset"   : { "x" : 0, "y" : 0 },
    "duration"      : "0",
    "runMac"        : true,
    "runGtk"        : true,
    "runWin"        : true
  },
  {
    "description"   : "Page scrollbar down, expect scroll snapping.",
    "snapCoord"     : "500px 500px, 500px 0px",
    "startScroll"   : { "x" : 500, "y" : 0 },
    "endScroll"     : { "x" : 500, "y" : 500 },
    "mousePosition" : { "x" : 250 - scrollbarOffset, "y" : 200 },
    "mouseOffset"   : { "x" : 0, "y" : 0 },
    "duration"      : "0",
    "runMac"        : true,
    "runGtk"        : true,
    "runWin"        : true
  },
  {
    "description"   : "Click scrollbar left button, expect scroll snapping.",
    "snapCoord"     : "50px 500px, 250px 500px, 500px 500px, 750px 500px, 950px 500px",
    "startScroll"   : { "x" : 500, "y" : 500 },
    "endScroll"     : { "x" : 250, "y" : 500 },
    "mousePosition" : { "x" : scrollbarOffset, "y" : 250 - scrollbarOffset },
    "mouseOffset"   : { "x" : 0, "y" : 0 },
    "duration"      : "0",
    "runMac"        : false, // OSX does not have have line-scroll buttons
    "runGtk"        : false, // Some GTK themes may not have scroll buttons
    "runWin"        : true
  },
  {
    "description"   : "Hold scrollbar left button until repeating, expect scroll snapping.",
    "snapCoord"     : "50px 500px, 500px 500px, 950px 500px",
    "startScroll"   : { "x" : 500, "y" : 500 },
    "endScroll"     : { "x" : 50, "y" : 500 },
    "mousePosition" : { "x" : scrollbarOffset, "y" : 250 - scrollbarOffset },
    "mouseOffset"   : { "x" : 0, "y" : 0 },
    "duration"      : "500",
    "runMac"        : false, // OSX does not have have line-scroll buttons
    "runGtk"        : false, // Some GTK themes may not have scroll buttons
    "runWin"        : true
  },
  {
    "description"   : "Click scrollbar right button, expect scroll snapping.",
    "snapCoord"     : "50px 500px, 250px 500px, 500px 500px, 750px 500px, 950px 500px",
    "startScroll"   : { "x" : 500, "y" : 500 },
    "endScroll"     : { "x" : 750, "y" : 500 },
    "mousePosition" : { "x" : 250 - scrollbarOffset * 3, "y" : 250 - scrollbarOffset },
    "mouseOffset"   : { "x" : 0, "y" : 0 },
    "duration"      : "0",
    "runMac"        : false, // OSX does not have have line-scroll buttons
    "runGtk"        : false, // Some GTK themes may not have scroll buttons
    "runWin"        : true
  },
  {
    "description"   : "Hold scrollbar right button until repeating, expect scroll snapping.",
    "snapCoord"     : "50px 500px, 500px 500px, 950px 500px",
    "startScroll"   : { "x" : 500, "y" : 500 },
    "endScroll"     : { "x" : 950, "y" : 500 },
    "mousePosition" : { "x" : 250 - scrollbarOffset * 3, "y" : 250 - scrollbarOffset },
    "mouseOffset"   : { "x" : 0, "y" : 0 },
    "duration"      : "500",
    "runMac"        : false, // OSX does not have have line-scroll buttons
    "runGtk"        : false, // Some GTK themes may not have scroll buttons
    "runWin"        : true
  },
  {
    "description"   : "Click scrollbar up button, expect scroll snapping.",
    "snapCoord"     : "500px 50px, 500px 250px, 500px 500px, 500px 750px, 500px 950px",
    "startScroll"   : { "x" : 500, "y" : 500 },
    "endScroll"     : { "x" : 500, "y" : 250 },
    "mousePosition" : { "x" : 250 - scrollbarOffset, "y" : scrollbarOffset },
    "mouseOffset"   : { "x" : 0, "y" : 0 },
    "duration"      : "0",
    "runMac"        : false, // OSX does not have have line-scroll buttons
    "runGtk"        : false, // Some GTK themes may not have scroll buttons
    "runWin"        : true
  },
  {
    "description"   : "Hold scrollbar up button until repeating, expect scroll snapping.",
    "snapCoord"     : "500px 50px, 500px 500px, 500px 950px",
    "startScroll"   : { "x" : 500, "y" : 500 },
    "endScroll"     : { "x" : 500, "y" : 50 },
    "mousePosition" : { "x" : 250 - scrollbarOffset, "y" : scrollbarOffset },
    "mouseOffset"   : { "x" : 0, "y" : 0 },
    "duration"      : "500",
    "runMac"        : false, // OSX does not have have line-scroll buttons
    "runGtk"        : false, // Some GTK themes may not have scroll buttons
    "runWin"        : true
  },
  {
    "description"   : "Click scrollbar down button, expect scroll snapping.",
    "snapCoord"     : "500px 50px, 500px 250px, 500px 500px, 500px 750px, 500px 950px",
    "startScroll"   : { "x" : 500, "y" : 500 },
    "endScroll"     : { "x" : 500, "y" : 750 },
    "mousePosition" : { "x" : 250 - scrollbarOffset, "y" : 250 - scrollbarOffset * 3},
    "mouseOffset"   : { "x" : 0, "y" : 0 },
    "duration"      : "0",
    "runMac"        : false, // OSX does not have have line-scroll buttons
    "runGtk"        : false, // Some GTK themes may not have scroll buttons
    "runWin"        : true
  },
  {
    "description"   : "Hold scrollbar down button until repeating, expect scroll snapping.",
    "snapCoord"     : "500px 50px, 500px 500px, 500px 950px",
    "startScroll"   : { "x" : 500, "y" : 500 },
    "endScroll"     : { "x" : 500, "y" : 950 },
    "mousePosition" : { "x" : 250 - scrollbarOffset, "y" : 250 - scrollbarOffset * 3},
    "mouseOffset"   : { "x" : 0, "y" : 0 },
    "duration"      : "500",
    "runMac"        : false, // OSX does not have have line-scroll buttons
    "runGtk"        : false, // Some GTK themes may not have scroll buttons
    "runWin"        : true
  },

];

var step = 0;
var sc; // Scroll Container
var sd; // Scrolled Div

var lastScrollTop;
var lastScrollLeft;
var stopFrameCount;
var winUtils = SpecialPowers.DOMWindowUtils;

function doTest() {
  var testCase = testCases[step];

  stopFrameCount = 0;
  lastScrollTop = sc.scrollTop;
  lastScrollLeft = sc.scrollLeft;

  sc.scrollTo(testCase.startScroll.x, testCase.startScroll.y);
  sc.style.scrollSnapType = "mandatory";
  sd.style.scrollSnapCoordinate = testCase.snapCoord;

  synthesizeMouse(sc,
                  testCase.mousePosition.x,
                  testCase.mousePosition.y,
                  { type: "mousedown" });

  synthesizeMouse(sc,
                  testCase.mousePosition.x + testCase.mouseOffset.x,
                  testCase.mousePosition.y + testCase.mouseOffset.y,
                  { type: "mousemove" });

  stopFrameCount = 0;
  waitForScrollStart();
}

function waitForScrollStart() {
  // Wait for up to 30 frames for scrolling to start
  var testCase = testCases[step];
  if (testCase.startScroll.y != sc.scrollTop
      || testCase.startScroll.x != sc.scrollLeft
      || ++stopFrameCount < 30) {
    window.requestAnimationFrame(doMouseUp);
  } else {
    window.requestAnimationFrame(waitForScrollStart);
  }
}

function doMouseUp() {
  var testCase = testCases[step];
  isnot("(" + sc.scrollLeft + "," + sc.scrollTop + ")",
        "(" + testCase.startScroll.x +"," + testCase.startScroll.y + ")",
        "Step " + step + ": Synthesized mouse events move scroll position. ("
          + testCase.description + ")");

  window.setTimeout(function() {
    synthesizeMouse(sc,
                    testCase.mousePosition.x + testCase.mouseOffset.x,
                    testCase.mousePosition.y + testCase.mouseOffset.y,
                    { type: "mouseup" });

    stopFrameCount = 0;
    window.requestAnimationFrame(waitForScrollStop);
  }, testCase.duration);
}

function waitForScrollStop() {
  if (stopFrameCount > 30) {
    // We have the same position for 30 consecutive frames -- we are stopped
    verifyTest();
  } else {
    // Still moving
    if (lastScrollTop == sc.scrollTop && lastScrollLeft == sc.scrollLeft) {
      stopFrameCount++;
    } else {
      stopFrameCount = 0;
      lastScrollTop = sc.scrollTop;
      lastScrollLeft = sc.scrollLeft;
    }
    window.requestAnimationFrame(waitForScrollStop);
  }
}

function verifyTest() {
  // Test ended, check if scroll position matches expected position
  var testCase = testCases[step];
  is("(" + sc.scrollLeft + "," + sc.scrollTop + ")",
     "(" + testCase.endScroll.x +"," + testCase.endScroll.y + ")",
      "Step " + step + ": " + testCase.description);

  // Find next test to run
  while (true) {
    if (++step == testCases.length) {
      SimpleTest.finish();
      break;
    } else {
      testCase = testCases[step];
      if ((testCase.runGtk && isGtk)
          || (testCase.runMac && isMac)
          || (testCase.runWin && isWin)) {
        doTest();
        break;
      }
    }
  }
}

SimpleTest.waitForExplicitFinish();
SimpleTest.requestFlakyTimeout("Delays added to allow synthesized mouse " +
                               "events to trigger scrollbar repeating scrolls.");
addLoadEvent(function() {
  sc = document.getElementById("sc");
  sd = document.getElementById("sd");
  SpecialPowers.pushPrefEnv({
    "set": [["layout.css.scroll-snap.enabled", true],
            ["layout.css.scroll-snap.proximity-threshold", 100]]},
    doTest);
});
</script>
</pre>
</body>

</html>