summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/intersection-observer/isIntersecting-change-events.html
blob: 99bc65bd60afee82f2ddd6b5380437d94811e30d (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
<!DOCTYPE html>
<meta name="viewport" content="width=device-width,initial-scale=1">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="./resources/intersection-observer-test-utils.js"></script>

<style>
pre, #log {
  position: absolute;
  top: 0;
  left: 200px;
}
#root {
  position: absolute;
  top: 0;
  left: 0;
  width: 150px;
  height: 200px;
  overflow-y: scroll;
}
#target1, #target2, #target3, #target4 {
  width: 100px;
  height: 100px;
}
#target1 {
  background-color: green;
}
#target2 {
  background-color: red;
}
#target3 {
  background-color: blue;
}
#target4 {
  background-color: yellow;
}
</style>

<div id="root">
  <div id="target1"></div>
  <div id="target2"></div>
  <div id="target3"></div>
</div>

<script>
var entries = [];
var observer;

runTestCycle(function() {
  var root = document.getElementById('root');
  var target1 = document.getElementById('target1');
  var target2 = document.getElementById('target2');
  var target3 = document.getElementById('target3');
  assert_true(!!root, "root element exists.");
  assert_true(!!target1, "target1 element exists.");
  assert_true(!!target2, "target2 element exists.");
  assert_true(!!target3, "target3 element exists.");
  observer = new IntersectionObserver(function(changes) {
    entries = entries.concat(changes);
  }, { root: root });
  observer.observe(target1);
  observer.observe(target2);
  observer.observe(target3);
  entries = entries.concat(observer.takeRecords());
  assert_equals(entries.length, 0, "No initial notifications.");
  runTestCycle(step0, "Rects in initial notifications should report initial positions.");
}, "isIntersecting changes should trigger notifications.");

function step0() {
  assert_equals(entries.length, 3, "Has 3 initial notifications.");
  checkRect(entries[0].boundingClientRect, [0, 100, 0, 100], "Check 1st entry rect");
  assert_equals(entries[0].target.id, 'target1', "Check 1st entry target id.");
  checkIsIntersecting(entries, 0, true);
  checkRect(entries[1].boundingClientRect, [0, 100, 100, 200], "Check 2nd entry rect");
  assert_equals(entries[1].target.id, 'target2', "Check 2nd entry target id.");
  checkIsIntersecting(entries, 1, true);
  checkRect(entries[2].boundingClientRect, [0, 100, 200, 300], "Check 3rd entry rect");
  assert_equals(entries[2].target.id, 'target3', "Check 3rd entry target id.");
  checkIsIntersecting(entries, 2, true);
  runTestCycle(step1, "Set scrollTop=100 and check for no new notifications.");
  root.scrollTop = 100;
}

function step1() {
  assert_equals(entries.length, 3, "Has 3 total notifications because isIntersecting did not change.");
  runTestCycle(step2, "Add 4th target.");
  root.scrollTop = 0;
  var target4 = document.createElement('div');
  target4.setAttribute('id', 'target4');
  root.appendChild(target4);
  observer.observe(target4);
}

function step2() {
  assert_equals(entries.length, 4, "Has 4 total notifications because 4th element was added.");
  checkRect(entries[3].boundingClientRect, [0, 100, 300, 400], "Check 4th entry rect");
  assert_equals(entries[3].target.id, 'target4', "Check 4th entry target id.");
  checkIsIntersecting(entries, 3, false);
  assert_equals(entries[3].intersectionRatio, 0, 'target4 initially has intersectionRatio of 0.');
  runTestCycle(step3, "Set scrollTop=100 and check for one new notification.");
  root.scrollTop = 100;
}

function step3() {
  assert_equals(entries.length, 5, "Has 5 total notifications.");
  checkRect(entries[4].boundingClientRect, [0, 100, 200, 300], "Check 5th entry rect");
  assert_equals(entries[4].target.id, 'target4', "Check 5th entry target id.");
  checkIsIntersecting(entries, 4, true);
  assert_equals(entries[4].intersectionRatio, 0, 'target4 still has intersectionRatio of 0.');
  root.scrollTop = 0; // reset to make it easier to refresh and run the test
}

</script>