summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/css-scroll-snap/snap-after-relayout/multiple-aligned-targets/prefer-common-to-both-axes.html
blob: 09b81e7c0dad69746012a6738b2f21689b699806 (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
<!DOCTYPE html>
<html>
  <head>
    <link rel="help" href="https://drafts.csswg.org/css-scroll-snap"/>
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
    <script src="/dom/events/scrolling/scroll_support.js"></script>
    <script src="/resources/testdriver.js"></script>
    <script src="/resources/testdriver-actions.js"></script>
    <script src="/resources/testdriver-vendor.js"></script>
    <script src="resources/common.js" ></script>
  </head>
  <body>
    <style>
      .scroller {
        overflow: scroll;
        width: 350px;
        height: 350px;
        border: solid 1px black;
        scroll-snap-type: both mandatory;
        position: relative;
        resize: both;
      }
      .large-space {
        height: 300vh;
        width: 300vw;
        position: absolute;
      }
      .snap {
        scroll-snap-align: start;
      }
      .box {
        width: 100px;
        height: 100px;
        background-color: green;
        display: inline-block;
        position: relative;
      }
      .grid {
        position: absolute;
        width: 350px;
        height: 350px;
      }
      .snap:focus {
        background-color: blue;
      }
    </style>
    <div class="scroller" id="scroller">
      <div class="large-space"><div>
      <div class="grid" id="grid">
        <div id="box1" tabindex="1" class="snap box">Box 1</div>
        <div id="box2" tabindex="1" class="snap box">Box 2</div>
        <div id="box3" tabindex="1" class="snap box">Box 3</div>
        <div id="box4" tabindex="1" class="snap box">Box 4</div>
        <div id="box5" tabindex="1" class="snap box">Box 5</div>
        <div id="box6" tabindex="1" class="snap box">Box 6</div>
        <div id="box7" tabindex="1" class="snap box">Box 7</div>
        <div id="box8" tabindex="1" class="snap box">Box 8</div>
        <div id="box9" tabindex="1" class="snap box">Box 9</div>
      </div>
    </div>
    <script>
    window.onload = () => {
      // This test sets up a 3x3 grid within scroller:
      // -------------------------
      // | Box 1 | Box 2 | Box 3 |
      // ------------------------
      // | Box 4 | Box 5 | Box 6 |
      // -------------------------
      // | Box 7 | Box 8 | Box 9 |
      // -------------------------
      const scroller = document.getElementById("scroller");
      const boxes = document.querySelectorAll(".snap.box");
      function box(n) {
        return boxes[n - 1];
      }

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

        await runScrollSnapSelectionVerificationTest(t, scroller,
                              /*aligned_elements_x=*/[],
                              /*aligned_elements_y=*/[box(1), box(2), box(3)],
                              /*axis=*/ "y",
                              /*expected_target_x=*/null,
                              /*expected_target_y=*/box(1));

        await runScrollSnapSelectionVerificationTest(t, scroller,
                              /*aligned_elements_x=*/[],
                              /*aligned_elements_y=*/[box(4), box(5), box(6)],
                              /*axis=*/ "y",
                              /*expected_target_x=*/null,
                              /*expected_target_y=*/box(4));

        await runScrollSnapSelectionVerificationTest(t, scroller,
                              /*aligned_elements_x=*/[],
                              /*aligned_elements_y=*/[box(7), box(8), box(9)],
                              /*axis=*/"y",
                              /*expected_target_x=*/null,
                              /*expected_target_y=*/box(7));

        await runScrollSnapSelectionVerificationTest(t, scroller,
                              /*aligned_elements_x=*/[box(1), box(4), box(7)],
                              /*aligned_elements_y=*/[],
                              /*axis=*/"x",
                              /*expected_target_x=*/box(1));

        await runScrollSnapSelectionVerificationTest(t, scroller,
                              /*aligned_elements_x=*/[box(2), box(5), box(8)],
                              /*aligned_elements_y=*/[],
                              /*axis=*/"x",
                              /*expected_target_x=*/box(2));

        await runScrollSnapSelectionVerificationTest(t, scroller,
                              /*aligned_elements_x=*/[box(3), box(6), box(9)],
                              /*aligned_elements_y=*/[],
                              /*axis=*/"x",
                              /*expected_target_x=*/box(3));

        await runScrollSnapSelectionVerificationTest(t, scroller,
                              /*aligned_elements_x=*/[box(2), box(5), box(8)],
                              /*aligned_elements_y=*/[box(4), box(5), box(6)],
                              /*axis=*/"both",
                              /*expected_target_x=*/box(5),
                              /*expected_target_y=*/box(5));
      }, "scroller prefers target aligned in both axes.");

      promise_test(async (t) => {
        const box7 = box(7), box8 = box(8), box9 = box(9);
        const initial_box8_top = box8.offsetTop;
        t.add_cleanup(() => {
          box8.style.top = `${initial_box8_top}px`;
        });

        // Move box 8 below box7 and box9.
        box8.style.top = `${2 * box8.offsetTop}px`;

        // Snap to box8.
        scroller.scrollTop = box8.offsetTop;

        // Test that if box7 and box9 are also shifted to align with box7,
        // box8 is still treated as the selected snap target despite box7 being
        // aligned on both axes.
        runLayoutSnapSeletionVerificationTest(t, scroller, [box7, box9],
                                              box8, "y");
      }, "scroller follows selected snap target after layout shift, " +
      "regardless of common snap area.");
    }
  </script>
  </body>
</html>