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