summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/css-contain/content-visibility/content-visibility-030.html
blob: 397f8de519f08e00764ff00b8dac331c4d9d401c (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
<!doctype HTML>
<html>
<meta charset="utf8">
<title>Content Visibility: intersection observer interactions</title>
<link rel="author" title="Vladimir Levin" href="mailto:vmpstr@chromium.org">
<link rel="help" href="https://drafts.csswg.org/css-contain/#content-visibility">
<meta name="assert" content="content-visibility hidden is not intersecting from IO perspective">

<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<style>
div {
  width: 100px;
  height: 100px;
}
#spacer {
  height: 3000px;
}
.hidden {
  content-visibility: hidden;
}
</style>

<div id="target1">
  <div id="target2"></div>
</div>
<div id="target3">
  <div id="target4"></div>
</div>
<div id="spacer"></div>
<div id="find_me"></div>

<script>
async_test((t) => {
  let target1, target2, target3, target4;
  let observer;
  let entries = [];

  // Set everything up.
  function enqueueStep1() {
    target1 = document.getElementById("target1");
    target2 = document.getElementById("target2");
    target3 = document.getElementById("target3");
    target4 = document.getElementById("target4");

    observer = new IntersectionObserver((new_entries) => {
      entries = entries.concat(new_entries);
    });
    observer.observe(target1);
    observer.observe(target2);
    observer.observe(target3);
    observer.observe(target4);

    entries = entries.concat(observer.takeRecords());
    t.step(() => { assert_equals(entries.length, 0, "No initial notifications") });
    requestAnimationFrame(() => {
      requestAnimationFrame(() => {
        runStep1();
      });
    });
  }

  // Verify that all elements are visible at the start, with intersection events.
  function runStep1() {
    const step = arguments.callee.name;
    t.step(() => {
      assert_equals(entries.length, 4, step);
      // Clear the observed visible targets.
      for (let i = 0; i < entries.length; ++i) {
        assert_true(entries[i].isIntersecting);
        assert_true(entries[i].target === target1 ||
                    entries[i].target === target2 ||
                    entries[i].target === target3 ||
                    entries[i].target === target4, step);
      }
    });

    entries = [];
    enqueueStep2();
  }

  // Lock target3.
  async function enqueueStep2() {
    target3.classList.add("hidden");
    requestAnimationFrame(() => {
      requestAnimationFrame(() => {
        runStep2();
      });
    });
  }

  // Verify that the hidden element received a not-intersecting event.
  function runStep2() {
    const step = arguments.callee.name;
    t.step(() => {
      assert_equals(entries.length, 1, step);
      assert_false(entries[0].isIntersecting, step);
      assert_equals(entries[0].target, target4, step);
    });

    entries = [];
    enqueueStep3();
  }

  // Scroll all elements off screen.
  function enqueueStep3() {
    document.getElementById("find_me").scrollIntoView();
    requestAnimationFrame(() => {
      requestAnimationFrame(() => {
        runStep3();
      });
    });
  }

  // Verify that all elements received not intersecting event, except
  // target4, which was already not intersecting due to being hidden.
  function runStep3() {
    const step = arguments.callee.name;
    t.step(() => {
      assert_equals(entries.length, 3, step);
      for (let i = 0; i < entries.length; ++i) {
        assert_false(entries[i].isIntersecting, step);
        assert_not_equals(entries[i].target, target4, step);
      }
    });

    entries = [];
    enqueueStep4();
  }

  // Scroll the elements back on screen.
  function enqueueStep4() {
    target1.scrollIntoView();
    requestAnimationFrame(() => {
      requestAnimationFrame(() => {
        runStep4();
      });
    });
  }

  // Verify that all elements received not intersecting event, except
  // target4, which remains not intersecting.
  function runStep4() {
    const step = arguments.callee.name;
    t.step(() => {
      assert_equals(entries.length, 3, step);
      for (let i = 0; i < entries.length; ++i) {
        assert_true(entries[i].isIntersecting);
        assert_not_equals(entries[i].target, target4, step);
      }
    });

    entries = [];
    enqueueStep5();
  }

  // Unlock target3.
  async function enqueueStep5() {
    target3.classList.remove("hidden");
    requestAnimationFrame(() => {
      requestAnimationFrame(() => {
        runStep5();
      });
    });
  }

  function runStep5() {
    const step = arguments.callee.name;
    t.step(() => {
      assert_equals(entries.length, 1, step);
      assert_true(entries[0].isIntersecting, step);
      assert_equals(entries[0].target, target4, step);
    });
    t.done();
  }


  window.onload = () => {
    requestAnimationFrame(enqueueStep1);
  };
}, "IntersectionObserver interactions");
</script>