OLD | NEW |
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <script src="../resources/js-test.js"></script> | 2 <script src="../resources/testharness.js"></script> |
3 <script src="../resources/intersection-observer-helper-functions.js"></script> | 3 <script src="../resources/testharnessreport.js"></script> |
4 <div style="width:100%; height:700px;"></div> | 4 |
| 5 <div id="leading-space" style="width:100%; height:700px;"></div> |
5 <div id="root" style="display: inline-block; overflow-y: scroll; height: 200px;
border: 3px solid black"> | 6 <div id="root" style="display: inline-block; overflow-y: scroll; height: 200px;
border: 3px solid black"> |
6 <div style="width:100px; height: 300px;"></div> | 7 <div style="width:100px; height: 300px;"></div> |
7 <div id="target" style="background-color: green; width:100px; height:100px"></
div> | 8 <div id="target" style="background-color: green; width:100px; height:100px"></
div> |
8 </div> | 9 </div> |
9 <div style="width:100%;height:700px;"></div> | 10 <div id="trailing-space" style="width:100%;height:700px;"></div> |
10 | 11 |
11 <script> | 12 <script> |
12 description("Simple intersection observer test with explicit root and target in
the same document."); | 13 function waitForNotification(f) { |
13 var target = document.getElementById("target"); | 14 requestAnimationFrame(() => { |
14 var root = document.getElementById("root"); | 15 setTimeout(() => { |
15 var entries = []; | 16 setTimeout(f); |
16 var observer = new IntersectionObserver( | 17 }); |
17 changes => { entries = entries.concat(changes) }, | 18 }); |
18 { root: document.getElementById("root") } | 19 } |
19 ); | |
20 | 20 |
21 onload = function() { | 21 onload = function() { |
| 22 var t = async_test("Simple intersection observer test with explicit root and t
arget in the same document."); |
| 23 |
| 24 test(function() { assert_equals(window.innerWidth, 800) }, "Window must be 800
pixels wide."); |
| 25 test(function() { assert_equals(window.innerHeight, 600) }, "Window must be 60
0 pixels high."); |
| 26 |
| 27 var target = document.getElementById("target"); |
| 28 var root = document.getElementById("root"); |
| 29 var entries = []; |
| 30 var observer = new IntersectionObserver(function(changes) { |
| 31 entries = entries.concat(changes) |
| 32 }, { root: document.getElementById("root") }); |
22 observer.observe(target); | 33 observer.observe(target); |
23 entries = entries.concat(observer.takeRecords()); | 34 entries = entries.concat(observer.takeRecords()); |
24 shouldBeEqualToNumber("entries.length", 0); | 35 test(function() { assert_equals(entries.length, 0) }, "No initial notification
s."); |
25 waitForNotification(step0); | 36 waitForNotification(step0); |
26 } | |
27 | 37 |
28 // Test that notifications are not generated when the target is overflow clipped
by the root. | 38 function checkEntry(i, expected) { |
29 function step0() { | 39 test(function() { assert_equals(entries.length, i+1) }, String(i+1) + " noti
fication(s)."); |
30 shouldBeEqualToNumber("entries.length", 0); | 40 if (expected && entries.length > i) { |
31 document.scrollingElement.scrollTop = 600; | 41 test(function() { assert_equals(entries[i].boundingClientRect.left, expect
ed[0]) }, |
32 waitForNotification(step1); | 42 "entries[" + i + "].boundingClientRect.left == " + expected[0]); |
33 } | 43 test(function() { assert_equals(entries[i].boundingClientRect.right, expec
ted[1]) }, |
34 | 44 "entries[" + i + "].boundingClientRect.right == " + expected[1]); |
35 function step1() { | 45 test(function() { assert_equals(entries[i].boundingClientRect.top, expecte
d[2]) }, |
36 shouldBeEqualToNumber("entries.length", 0); | 46 "entries[" + i + "].boundingClientRect.top == " + expected[2]); |
37 root.scrollTop = 150; | 47 test(function() { assert_equals(entries[i].boundingClientRect.bottom, expe
cted[3]) }, |
38 waitForNotification(step2); | 48 "entries[" + i + "].boundingClientRect.bottom == " + expected[3]); |
39 } | 49 test(function() { assert_equals(entries[i].intersectionRect.left, expected
[4]) }, |
40 | 50 "entries[" + i + "].intersectionRect.left == " + expected[4]); |
41 function step2() { | 51 test(function() { assert_equals(entries[i].intersectionRect.right, expecte
d[5]) }, |
42 shouldBeEqualToNumber("entries.length", 1); | 52 "entries[" + i + "].intersectionRect.right == " + expected[5]); |
43 if (entries.length > 0) { | 53 test(function() { assert_equals(entries[i].intersectionRect.top, expected[
6]) }, |
44 shouldBeEqualToNumber("entries[0].boundingClientRect.left", 11); | 54 "entries[" + i + "].intersectionRect.top == " + expected[6]); |
45 shouldBeEqualToNumber("entries[0].boundingClientRect.right", 111); | 55 test(function() { assert_equals(entries[i].intersectionRect.bottom, expect
ed[7]) }, |
46 shouldBeEqualToNumber("entries[0].boundingClientRect.top", 261); | 56 "entries[" + i + "].intersectionRect.bottom == " + expected[7]); |
47 shouldBeEqualToNumber("entries[0].boundingClientRect.bottom", 361); | 57 test(function() { assert_equals(entries[i].rootBounds.left, expected[8]) }
, |
48 shouldBeEqualToNumber("entries[0].intersectionRect.left", 11); | 58 "entries[" + i + "].rootBounds.left == " + expected[8]); |
49 shouldBeEqualToNumber("entries[0].intersectionRect.right", 111); | 59 test(function() { assert_equals(entries[i].rootBounds.right, expected[9])
}, |
50 shouldBeEqualToNumber("entries[0].intersectionRect.top", 261); | 60 "entries[" + i + "].rootBounds.right == " + expected[9]); |
51 shouldBeEqualToNumber("entries[0].intersectionRect.bottom", 311); | 61 test(function() { assert_equals(entries[i].rootBounds.top, expected[10]) }
, |
52 shouldBeEqualToNumber("entries[0].rootBounds.left", 11); | 62 "entries[" + i + "].rootBounds.top == " + expected[10]); |
53 shouldBeEqualToNumber("entries[0].rootBounds.right", 111); | 63 test(function() { assert_equals(entries[i].rootBounds.bottom, expected[11]
) }, |
54 shouldBeEqualToNumber("entries[0].rootBounds.top", 111); | 64 "entries[" + i + "].rootBounds.bottom == " + expected[11]); |
55 shouldBeEqualToNumber("entries[0].rootBounds.bottom", 311); | 65 test(function() { assert_true(entries[i].target === expected[12]) }, |
56 shouldEvaluateToSameObject("entries[0].target", target); | 66 "entries[" + i + "].target === " + expected[12]); |
57 } | 67 } |
58 document.scrollingElement.scrollTop = 0; | |
59 waitForNotification(step3); | |
60 } | |
61 | |
62 function step3() { | |
63 shouldBeEqualToNumber("entries.length", 1); | |
64 root.scrollTop = 0; | |
65 waitForNotification(step4); | |
66 } | |
67 | |
68 function step4() { | |
69 shouldBeEqualToNumber("entries.length", 2); | |
70 if (entries.length > 1) { | |
71 shouldBeEqualToNumber("entries[1].boundingClientRect.left", 11); | |
72 shouldBeEqualToNumber("entries[1].boundingClientRect.right", 111); | |
73 shouldBeEqualToNumber("entries[1].boundingClientRect.top", 1011); | |
74 shouldBeEqualToNumber("entries[1].boundingClientRect.bottom", 1111); | |
75 shouldBeEqualToNumber("entries[1].intersectionRect.left", 0); | |
76 shouldBeEqualToNumber("entries[1].intersectionRect.right", 0); | |
77 shouldBeEqualToNumber("entries[1].intersectionRect.top", 0); | |
78 shouldBeEqualToNumber("entries[1].intersectionRect.bottom", 0); | |
79 shouldBeEqualToNumber("entries[1].rootBounds.left", 11); | |
80 shouldBeEqualToNumber("entries[1].rootBounds.right", 111); | |
81 shouldBeEqualToNumber("entries[1].rootBounds.top", 711); | |
82 shouldBeEqualToNumber("entries[1].rootBounds.bottom", 911); | |
83 shouldEvaluateToSameObject("entries[1].target", target); | |
84 } | |
85 root.scrollTop = 150; | |
86 waitForNotification(step5); | |
87 } | |
88 | |
89 // This tests that notifications are generated even when the root element is off
screen. | |
90 function step5() { | |
91 shouldBeEqualToNumber("entries.length", 3); | |
92 if (entries.length > 2) { | |
93 shouldBeEqualToNumber("entries[2].boundingClientRect.left", 11); | |
94 shouldBeEqualToNumber("entries[2].boundingClientRect.right", 111); | |
95 shouldBeEqualToNumber("entries[2].boundingClientRect.top", 861); | |
96 shouldBeEqualToNumber("entries[2].boundingClientRect.bottom", 961); | |
97 shouldBeEqualToNumber("entries[2].intersectionRect.left", 11); | |
98 shouldBeEqualToNumber("entries[2].intersectionRect.right", 111); | |
99 shouldBeEqualToNumber("entries[2].intersectionRect.top", 861); | |
100 shouldBeEqualToNumber("entries[2].intersectionRect.bottom", 911); | |
101 shouldBeEqualToNumber("entries[2].rootBounds.left", 11); | |
102 shouldBeEqualToNumber("entries[2].rootBounds.right", 111); | |
103 shouldBeEqualToNumber("entries[2].rootBounds.top", 711); | |
104 shouldBeEqualToNumber("entries[2].rootBounds.bottom", 911); | |
105 shouldEvaluateToSameObject("entries[2].target", target); | |
106 } | 68 } |
107 | 69 |
108 finishJSTest(); | 70 function step0() { |
109 } | 71 test(function() { assert_equals(entries.length, 0) }, "No notifications afte
r first rAF."); |
| 72 document.scrollingElement.scrollTop = 600; |
| 73 waitForNotification(step1); |
| 74 } |
| 75 |
| 76 function step1() { |
| 77 test(function() { assert_equals(entries.length, 0) }, "No notifications afte
r scrolling window."); |
| 78 root.scrollTop = 150; |
| 79 waitForNotification(step2); |
| 80 } |
| 81 |
| 82 function step2() { |
| 83 checkEntry(0, [11, 111, 261, 361, 11, 111, 261, 311, 11, 111, 111, 311, targ
et]); |
| 84 document.scrollingElement.scrollTop = 0; |
| 85 waitForNotification(step3); |
| 86 } |
| 87 |
| 88 function step3() { |
| 89 checkEntry(0); |
| 90 root.scrollTop = 0; |
| 91 waitForNotification(step4); |
| 92 } |
| 93 |
| 94 function step4() { |
| 95 checkEntry(1, [11, 111, 1011, 1111, 0, 0, 0, 0, 11, 111, 711, 911, target]); |
| 96 root.scrollTop = 150; |
| 97 waitForNotification(step5); |
| 98 } |
| 99 |
| 100 // This tests that notifications are generated even when the root element is o
ff screen. |
| 101 function step5() { |
| 102 checkEntry(2, [11, 111, 861, 961, 11, 111, 861, 911, 11, 111, 711, 911, targ
et]); |
| 103 document.getElementById("leading-space").style.height = ""; |
| 104 document.getElementById("trailing-space").style.height = ""; |
| 105 t.done(); |
| 106 } |
| 107 }; |
110 </script> | 108 </script> |
OLD | NEW |