summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/permissions-policy/experimental-features/vertical-scroll-wheel-block-manual.tentative.html
blob: 2627fd4154312b98bbe754a5ac0c544c762dd84e (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
<!doctype html>
<title>vertical-scroll test for 'mousewheel'</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/permissions-policy/experimental-features/resources/common.js"></script>
<script src="/permissions-policy/experimental-features/resources/vertical-scroll.js"></script>
<style>
html, body {
  height: 100%;
  width: 100%;
}

iframe {
  width: 90%;
  height: 90%;
  margin: 0;
  padding: 0;
}

.spacer {
  width: 100%;
  height: 100%;
  margin: 100%;
}

</style>
<iframe></iframe>
<br/>
<p>Spacers below to make page scrollable</p>
<br/>
<div class="spacer"></div>
<div class="spacer"></div>
<p> EOF </p>
<script>
  "use strict";

  let url = url_base + "vertical-scroll-wheel-block.html";
  function iframeElement() {
    return document.querySelector("iframe");
  }

  // Used as the polling interval when waiting for a specific condition.
  let verify_scroll_offset_delay = 5;
  let no_scroll_timout = 50;

  function waitUntilSatisfied(testInstance, predicate) {
    return new Promise((r) => {
      function testPredicate() {
        if (predicate()) {
          r();
        } else {
          testInstance.step_timeout(testPredicate, verify_scroll_offset_delay);
        }
      }
      testPredicate();
    });
  }

  function resetScroll(testInstance) {
    window.scrollTo({top: 0, left: 0, behavior: "instant"});
    return waitUntilSatisfied(testInstance, () => {
      return window.scrollX === 0 && window.scrollY === 0;
    });
  }

  function waitForMinimumScrollOffset(testInstance, minX, minY) {
    return waitUntilSatisfied(testInstance, () => {
      return window.scrollX >= minX && window.scrollY >= minY;
    });
  }

  function waitFor(testInstance, delay) {
    let checked_once = false;
    return waitUntilSatisfied(testInstance, () => {
      if (checked_once)
        return true;
      checked_once = true;
      return false;
    });
  }

  // Wait for the helper scripts to load.
  promise_test(async() => {
    if (window.input_api_ready)
      return Promise.resolve();
    await new Promise((r) => {
      window.resolve_on_input_api_ready = r;
    });
  }, "Make sure input injection API is ready.");

  // Sanity-check: Test API for scrolling along y-axis works as expected.
  promise_test(async(testInstance) => {
    await resetScroll(testInstance);
    await inject_wheel_scroll("down");
    await waitForMinimumScrollOffset(testInstance, 0, 1);
    assert_greater_than(window.scrollY, 0, "Expected vertical scroll.");
  }, "Sanity-check: the page scrolls vertically in response to vertical wheel.");

  // Sanity-check: Test API for scrolling along x-axis works as expected.
  promise_test(async(testInstance) => {
    await resetScroll(testInstance);
    await inject_wheel_scroll("right");
    await waitForMinimumScrollOffset(testInstance, 1, 0);
    assert_greater_than(window.scrollX, 0, "Expected horizontal scroll.");
  }, "Sanity-check: the page scrolls horizontally in response to horizontal wheel.");

  // Test that when 'vertical-scroll' is enabled, vertical scroll can be
  // blocked by canceling 'wheel' event.
  promise_test(async(testInstance) => {
    setFeatureState(iframeElement(), "vertical-scroll", "*");
    await loadUrlInIframe(iframeElement(), url);

    await resetScroll(testInstance);
    await inject_wheel_scroll("down")
    await waitFor(testInstance, no_scroll_timout);
    assert_equals(window.scrollY, 0, "Did not expected vertical scroll.");
  }, "When 'vertical-scroll' is enabled canceling vertical 'wheel' " +
     "blocks vertical scrolling.");

  // Test that when 'vertical-scroll' is disabled, vertical scroll cannot
  // be blocked by canceling 'wheel' event.
  promise_test(async(testInstance) => {
    setFeatureState(iframeElement(), "vertical-scroll", "'none'");
    await loadUrlInIframe(iframeElement(), url);

    await resetScroll(testInstance);
    await inject_wheel_scroll("down");
    await waitForMinimumScrollOffset(testInstance, 0, 1);
    assert_greater_than(window.scrollY, 0, "Expected vertical scroll.");
  }, "When 'vertical-scroll' is disabled canceling vertical 'wheel' " +
     "does not block vertical scrolling.");

  // Test that when 'vertical-scroll' is disabled, horizontal scroll can be
  // blocked by canceling 'wheel' event.
  promise_test(async(testInstance) => {
    setFeatureState(iframeElement(), "vertical-scroll", "'none'");
    await loadUrlInIframe(iframeElement(), url);

    await resetScroll(testInstance);
    await inject_wheel_scroll("right");
    await waitFor(testInstance, no_scroll_timout);
    assert_equals(window.scrollX, 0, "Did not expect horizontal scroll.");
  }, "When 'vertical-scroll' is disabled canceling horizontal 'wheel' " +
     "blocks horizontal scrolling.");
</script>