blob: bfb9b729aa6ab607c0427050d5b543d51a493c1f [file] [log] [blame]
Tobias Schneider9a6600e2017-06-28 17:46:451<!DOCTYPE html>
Hiroyuki Ikezoe9ddcc732019-05-18 23:45:292<meta name="viewport" content="width=device-width,initial-scale=1">
Tobias Schneider9a6600e2017-06-28 17:46:453<script src="/resources/testharness.js"></script>
4<script src="/resources/testharnessreport.js"></script>
5<script src="./resources/intersection-observer-test-utils.js"></script>
6
7<style>
8pre, #log {
9 position: absolute;
10 top: 0;
11 left: 200px;
12}
13.spacer {
14 height: calc(100vh + 100px);
15}
16#root {
17 display: inline-block;
18 overflow-y: scroll;
19 height: 200px;
20 border: 3px solid black;
21}
22#target {
23 width: 100px;
24 height: 100px;
25 background-color: green;
26}
27</style>
28
29<div class="spacer"></div>
30<div id="root">
31 <div style="height: 300px;"></div>
32 <div id="target"></div>
33</div>
34<div class="spacer"></div>
35
36<script>
37var vw = document.documentElement.clientWidth;
38var vh = document.documentElement.clientHeight;
39
40var entries = [];
41var root, target;
42
43runTestCycle(function() {
44 target = document.getElementById("target");
45 assert_true(!!target, "target exists");
46 root = document.getElementById("root");
47 assert_true(!!root, "root exists");
48 var observer = new IntersectionObserver(function(changes) {
49 entries = entries.concat(changes)
50 }, { root: root });
51 observer.observe(target);
52 entries = entries.concat(observer.takeRecords());
53 assert_equals(entries.length, 0, "No initial notifications.");
54 runTestCycle(step0, "First rAF");
55}, "IntersectionObserver in a single document with explicit root.");
56
57function step0() {
58 document.scrollingElement.scrollTop = vh;
59 runTestCycle(step1, "document.scrollingElement.scrollTop = window.innerHeight.");
60 checkLastEntry(entries, 0, [ 11, 111, vh + 411, vh + 511, 0, 0, 0, 0, 11, 111, vh + 111, vh + 311, false]);
61}
62
63function step1() {
64 root.scrollTop = 150;
65 runTestCycle(step2, "root.scrollTop = 150 with root scrolled into view.");
66 assert_equals(entries.length, 1, "No notifications after scrolling frame.");
67}
68
69function step2() {
70 document.scrollingElement.scrollTop = 0;
71 runTestCycle(step3, "document.scrollingElement.scrollTop = 0.");
72 checkLastEntry(entries, 1, [11, 111, 261, 361, 11, 111, 261, 311, 11, 111, 111, 311, true]);
73}
74
75function step3() {
76 root.scrollTop = 0;
77 runTestCycle(step4, "root.scrollTop = 0");
78 checkLastEntry(entries, 1);
79}
80
81function step4() {
82 root.scrollTop = 150;
83 runTestCycle(step5, "root.scrollTop = 150 with root scrolled out of view.");
84 checkLastEntry(entries, 2, [11, 111, vh + 411, vh + 511, 0, 0, 0, 0, 11, 111, vh + 111, vh + 311, false]);
85}
86
87// This tests that notifications are generated even when the root element is off screen.
88function step5() {
89 checkLastEntry(entries, 3, [11, 111, vh + 261, vh + 361, 11, 111, vh + 261, vh + 311, 11, 111, vh + 111, vh + 311, true]);
90}
91</script>