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 <div id="leading-space" style="width:100%; height:700px;"></div> |
5 <div id="target" style="background-color: green; width:100px; height:100px"></di
v> | 5 <div id="target" style="background-color: green; width:100px; height:100px"></di
v> |
6 <div style="width:100%; height:700px;"></div> | 6 <div id="trailing-space" style="width:100%; height:700px;"></div> |
7 | 7 |
8 <script> | 8 <script> |
9 description("Simple intersection observer test with no explicit root and one doc
ument."); | 9 function waitForNotification(f) { |
10 var target = document.getElementById("target"); | 10 requestAnimationFrame(() => { |
11 var entries = []; | 11 setTimeout(() => { |
12 var observer = new IntersectionObserver(changes => { entries = entries.concat(ch
anges) }); | 12 setTimeout(f); |
| 13 }); |
| 14 }); |
| 15 } |
13 | 16 |
14 onload = function() { | 17 onload = function() { |
| 18 var t = async_test("Simple intersection observer test with no explicit root an
d one document."); |
| 19 |
| 20 test(function() { assert_equals(window.innerWidth, 800) }, "Window must be 800
pixels wide."); |
| 21 test(function() { assert_equals(window.innerHeight, 600) }, "Window must be 60
0 pixels high."); |
| 22 |
| 23 var target = document.getElementById("target"); |
| 24 var entries = []; |
| 25 var observer = new IntersectionObserver(function(changes) { |
| 26 entries = entries.concat(changes) |
| 27 }); |
15 observer.observe(target); | 28 observer.observe(target); |
16 entries = entries.concat(observer.takeRecords()); | 29 entries = entries.concat(observer.takeRecords()); |
17 shouldBeEqualToNumber("entries.length", 0); | 30 test(function() { assert_equals(entries.length, 0) }, "No initial notification
s."); |
18 document.scrollingElement.scrollTop = 300; | 31 waitForNotification(step0); |
19 waitForNotification(step1); | 32 |
| 33 function checkEntry(i, expected) { |
| 34 test(function() { assert_equals(entries.length, i+1) }, String(i+1) + " noti
fication(s)."); |
| 35 if (expected && entries.length > i) { |
| 36 test(function() { assert_equals(entries[i].boundingClientRect.left, expect
ed[0]) }, |
| 37 "entries[" + i + "].boundingClientRect.left == " + expected[0]); |
| 38 test(function() { assert_equals(entries[i].boundingClientRect.right, expec
ted[1]) }, |
| 39 "entries[" + i + "].boundingClientRect.right == " + expected[1]); |
| 40 test(function() { assert_equals(entries[i].boundingClientRect.top, expecte
d[2]) }, |
| 41 "entries[" + i + "].boundingClientRect.top == " + expected[2]); |
| 42 test(function() { assert_equals(entries[i].boundingClientRect.bottom, expe
cted[3]) }, |
| 43 "entries[" + i + "].boundingClientRect.bottom == " + expected[3]); |
| 44 test(function() { assert_equals(entries[i].intersectionRect.left, expected
[4]) }, |
| 45 "entries[" + i + "].intersectionRect.left == " + expected[4]); |
| 46 test(function() { assert_equals(entries[i].intersectionRect.right, expecte
d[5]) }, |
| 47 "entries[" + i + "].intersectionRect.right == " + expected[5]); |
| 48 test(function() { assert_equals(entries[i].intersectionRect.top, expected[
6]) }, |
| 49 "entries[" + i + "].intersectionRect.top == " + expected[6]); |
| 50 test(function() { assert_equals(entries[i].intersectionRect.bottom, expect
ed[7]) }, |
| 51 "entries[" + i + "].intersectionRect.bottom == " + expected[7]); |
| 52 test(function() { assert_equals(entries[i].rootBounds.left, expected[8]) }
, |
| 53 "entries[" + i + "].rootBounds.left == " + expected[8]); |
| 54 test(function() { assert_equals(entries[i].rootBounds.right, expected[9])
}, |
| 55 "entries[" + i + "].rootBounds.right == " + expected[9]); |
| 56 test(function() { assert_equals(entries[i].rootBounds.top, expected[10]) }
, |
| 57 "entries[" + i + "].rootBounds.top == " + expected[10]); |
| 58 test(function() { assert_equals(entries[i].rootBounds.bottom, expected[11]
) }, |
| 59 "entries[" + i + "].rootBounds.bottom == " + expected[11]); |
| 60 test(function() { assert_true(entries[i].boundingClientRect === entries[i]
.boundingClientRect) }, |
| 61 » "entries[" + i + "].boundingClientRect is a stable object."); |
| 62 test(function() { assert_true(entries[i].intersectionRect === entries[i].i
ntersectionRect) }, |
| 63 » "entries[" + i + "].intersectionRect is a stable object."); |
| 64 test(function() { assert_true(entries[i].rootBounds === entries[i].rootBou
nds) }, |
| 65 » "entries[" + i + "].rootBounds is a stable object."); |
| 66 test(function() { assert_true(entries[i].target === expected[12]) }, |
| 67 "entries[" + i + "].target === " + expected[12]); |
| 68 } |
| 69 } |
| 70 |
| 71 function step0() { |
| 72 test(function() { assert_equals(entries.length, 0) }, "No notifications afte
r first rAF."); |
| 73 document.scrollingElement.scrollTop = 300; |
| 74 waitForNotification(step1); |
| 75 } |
| 76 |
| 77 function step1() { |
| 78 checkEntry(0, [8, 108, 408, 508, 8, 108, 408, 508, 0, 785, 0, 600, target]); |
| 79 document.scrollingElement.scrollTop = 100; |
| 80 waitForNotification(step2); |
| 81 } |
| 82 |
| 83 function step2() { |
| 84 checkEntry(1, [8, 108, 608, 708, 0, 0, 0, 0, 0, 785, 0, 600, target]); |
| 85 document.getElementById("leading-space").style.height = ""; |
| 86 document.getElementById("trailing-space").style.height = ""; |
| 87 document.scrollingElement.scrollTop = 0; |
| 88 t.done(); |
| 89 } |
20 }; | 90 }; |
21 | 91 |
22 function step1() { | |
23 shouldBeEqualToNumber("entries.length", 1); | |
24 if (entries.length > 0) { | |
25 shouldBeEqualToNumber("entries[0].boundingClientRect.left", 8); | |
26 shouldBeEqualToNumber("entries[0].boundingClientRect.right", 108); | |
27 shouldBeEqualToNumber("entries[0].boundingClientRect.top", 408); | |
28 shouldBeEqualToNumber("entries[0].boundingClientRect.bottom", 508); | |
29 shouldBeEqualToNumber("entries[0].intersectionRect.left", 8); | |
30 shouldBeEqualToNumber("entries[0].intersectionRect.right", 108); | |
31 shouldBeEqualToNumber("entries[0].intersectionRect.top", 408); | |
32 shouldBeEqualToNumber("entries[0].intersectionRect.bottom", 508); | |
33 shouldBeEqualToNumber("entries[0].rootBounds.left", 0); | |
34 shouldBeEqualToNumber("entries[0].rootBounds.right", 785); | |
35 shouldBeEqualToNumber("entries[0].rootBounds.top", 0); | |
36 shouldBeEqualToNumber("entries[0].rootBounds.bottom", 600); | |
37 shouldEvaluateToSameObject("entries[0].target", target); | |
38 | |
39 // ClientRect members of IntersectionObserverEntry should be stable. | |
40 shouldEvaluateToSameObject("entries[0].boundingClientRect", entries[0].bound
ingClientRect); | |
41 shouldEvaluateToSameObject("entries[0].intersectionRect", entries[0].interse
ctionRect); | |
42 shouldEvaluateToSameObject("entries[0].rootBounds", entries[0].rootBounds); | |
43 } | |
44 document.scrollingElement.scrollTop = 100; | |
45 waitForNotification(step2); | |
46 } | |
47 | |
48 function step2() { | |
49 shouldBeEqualToNumber("entries.length", 2); | |
50 if (entries.length > 1) { | |
51 shouldBeEqualToNumber("entries[1].boundingClientRect.left", 8); | |
52 shouldBeEqualToNumber("entries[1].boundingClientRect.right", 108); | |
53 shouldBeEqualToNumber("entries[1].boundingClientRect.top", 608); | |
54 shouldBeEqualToNumber("entries[1].boundingClientRect.bottom", 708); | |
55 shouldBeEqualToNumber("entries[1].intersectionRect.left", 0); | |
56 shouldBeEqualToNumber("entries[1].intersectionRect.right", 0); | |
57 shouldBeEqualToNumber("entries[1].intersectionRect.top", 0); | |
58 shouldBeEqualToNumber("entries[1].intersectionRect.bottom", 0); | |
59 shouldBeEqualToNumber("entries[1].rootBounds.left", 0); | |
60 shouldBeEqualToNumber("entries[1].rootBounds.right", 785); | |
61 shouldBeEqualToNumber("entries[1].rootBounds.top", 0); | |
62 shouldBeEqualToNumber("entries[1].rootBounds.bottom", 600); | |
63 shouldEvaluateToSameObject("entries[1].target", target); | |
64 } | |
65 finishJSTest(); | |
66 document.scrollingElement.scrollTop = 0; | |
67 } | |
68 | |
69 </script> | 92 </script> |
OLD | NEW |