summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/dom/events/scrolling/scrollend-event-for-user-scroll.html
blob: 5146c5f719a1e4728a2c9aa0bdd54516ac1fe5c1 (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
<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="scroll_support.js"></script>
<style>
    #targetDiv {
        width: 200px;
        height: 200px;
        overflow: scroll;
    }

    #innerDiv {
        width: 400px;
        height: 400px;
    }
</style>
</head>
<body style="margin:0" onload=runTest()>
    <div id="targetDiv">
        <div id="innerDiv">
        </div>
    </div>
</body>

<script>
var target_div = document.getElementById('targetDiv');

async function resetTargetScrollState(test) {
  if (target_div.scrollTop != 0 || target_div.scrollLeft != 0) {
    target_div.scrollTop = 0;
    target_div.scrollLeft = 0;
    return waitForScrollendEvent(test, target_div);
  }
}

async function verifyScrollStopped(test) {
  const unscaled_pause_time_in_ms = 100;
  const x = target_div.scrollLeft;
  const y = target_div.scrollTop;
  return new Promise(resolve => {
    test.step_timeout(() => {
      assert_equals(x, target_div.scrollLeft);
      assert_equals(y, target_div.scrollTop);
      resolve();
    }, unscaled_pause_time_in_ms);
  });
}

async function verifyNoScrollendOnDocument(test) {
  const callback =
      test.unreached_func("window got unexpected scrollend event.");
  window.addEventListener('scrollend', callback);
}

async function createScrollendPromise(test) {
  return waitForScrollendEvent(test, target_div).then(evt => {
      assert_false(evt.cancelable, 'Event is not cancelable');
      assert_false(evt.bubbles, 'Event targeting element does not bubble');
  });
}

function runTest() {
  promise_test(async (t) => {
    // Skip the test on a Mac as they do not support touch screens.
    const isMac = navigator.platform.toUpperCase().indexOf('MAC')>=0;
    if (isMac)
      return;

    await resetTargetScrollState(t);
    await waitForCompositorCommit();

    const targetScrollendPromise = createScrollendPromise(t);
    verifyNoScrollendOnDocument(t);

    // Perform a touch drag on target div and wait for target_div to get
    // a scrollend event.
    await new test_driver.Actions()
        .addPointer('TestPointer', 'touch')
        .pointerMove(0, 0, {origin: target_div}) // 0, 0 is center of element.
        .pointerDown()
        .addTick()
        .pointerMove(0, -40, {origin: target_div}) //  Drag up to move down.
        .addTick()
        .pause(200) //  Prevent inertial scroll.
        .pointerUp()
        .send();

    await targetScrollendPromise;

    assert_true(target_div.scrollTop > 0);
    await verifyScrollStopped(t);
  }, 'Tests that the target_div gets scrollend event when touch dragging.');

  promise_test(async (t) => {
    // Skip test on platforms that do not have a visible scrollbar (e.g.
    // overlay scrollbar).
    const scrollbar_width = target_div.offsetWidth - target_div.clientWidth;
    if (scrollbar_width == 0)
      return;

    await resetTargetScrollState(t);
    await waitForCompositorCommit();

    const targetScrollendPromise = createScrollendPromise(t);
    verifyNoScrollendOnDocument(t);

    const bounds = target_div.getBoundingClientRect();
    const x = bounds.right - scrollbar_width / 2;
    const y = bounds.bottom - 20;
    await new test_driver.Actions()
        .addPointer('TestPointer', 'mouse')
        .pointerMove(x, y, {origin: 'viewport'})
        .pointerDown()
        .addTick()
        .pointerUp()
        .send();

    await targetScrollendPromise;
    assert_true(target_div.scrollTop > 0);
    await verifyScrollStopped(t);
  }, 'Tests that the target_div gets scrollend event when clicking ' +
     'scrollbar.');

  // Same issue as previous test.
  promise_test(async (t) => {
    // Skip test on platforms that do not have a visible scrollbar (e.g.
    // overlay scrollbar).
    const scrollbar_width = target_div.offsetWidth - target_div.clientWidth;
    if (scrollbar_width == 0)
      return;

    resetTargetScrollState(t);
    const targetScrollendPromise = createScrollendPromise(t);
    verifyNoScrollendOnDocument(t);

    const bounds = target_div.getBoundingClientRect();
    const x = bounds.right - scrollbar_width / 2;
    const y = bounds.top + 30;
    const dy = 30;
    await new test_driver.Actions()
        .addPointer('TestPointer', 'mouse')
        .pointerMove(x, y, { origin: 'viewport' })
        .pointerDown()
        .pointerMove(x, y + dy, { origin: 'viewport' })
        .addTick()
        .pointerUp()
        .send();

    await targetScrollendPromise;
    assert_true(target_div.scrollTop > 0);
    await verifyScrollStopped(t);
  }, 'Tests that the target_div gets scrollend event when dragging the ' +
      'scrollbar thumb.');

  promise_test(async (t) => {
    resetTargetScrollState(t);
    const targetScrollendPromise = createScrollendPromise(t);
    verifyNoScrollendOnDocument(t);

    const x = 0;
    const y = 0;
    const dx = 0;
    const dy = 40;
    const duration_ms = 10;
    await new test_driver.Actions()
        .scroll(x, y, dx, dy, { origin: target_div }, duration_ms)
        .send();

    await targetScrollendPromise;
    assert_true(target_div.scrollTop > 0);
    await verifyScrollStopped(t);
  }, 'Tests that the target_div gets scrollend event when mouse wheel ' +
     'scrolling.');

  promise_test(async (t) => {
    await resetTargetScrollState(t);
    await waitForCompositorCommit();

    verifyNoScrollendOnDocument(t);
    const targetScrollendPromise = createScrollendPromise(t);

    target_div.focus();
    window.test_driver.send_keys(target_div, '\ue015');

    await targetScrollendPromise;
    assert_true(target_div.scrollTop > 0);
    await verifyScrollStopped(t);
  }, 'Tests that the target_div gets scrollend event when sending DOWN key ' +
     'to the target.');
}

</script>
</html>