blob: be1980040d298b4d6d0055976ed42c202dec9888 [file] [log] [blame]
Tobias Schneider9a6600e2017-06-28 17:46:451<!DOCTYPE html>
2<script src="/resources/testharness.js"></script>
3<script src="/resources/testharnessreport.js"></script>
4<script src="./resources/intersection-observer-test-utils.js"></script>
5
6<style>
7pre, #log {
8 position: absolute;
9 top: 0;
10 left: 200px;
11}
12.spacer {
13 height: calc(100vh + 100px);
14}
15
16</style>
17<div id="leading-space" class="spacer"></div>
18<div id="trailing-space" class="spacer"></div>
19
20<script>
Tobias Schneidere8d533c2017-08-31 15:55:2121// Pick this number to be comfortably greater than the length of two frames at 60Hz.
22var timeSkew = 40;
23
Tobias Schneider9a6600e2017-06-28 17:46:4524var topWindowEntries = [];
25var iframeWindowEntries = [];
26var targetIframe;
27var topWindowTimeBeforeNotification;
28var iframeWindowTimeBeforeNotification;
29
30async_test(function(t) {
Tobias Schneidere8d533c2017-08-31 15:55:2131 t.step_timeout(function() {
Tobias Schneider9a6600e2017-06-28 17:46:4532 targetIframe = document.createElement("iframe");
33 assert_true(!!targetIframe, "iframe exists");
34 targetIframe.src = "resources/timestamp-subframe.html";
35 var trailingSpace = document.getElementById("trailing-space");
36 assert_true(!!trailingSpace, "trailing-space exists");
37 trailingSpace.parentNode.insertBefore(targetIframe, trailingSpace);
38 targetIframe.onload = function() {
39 var target = targetIframe.contentDocument.getElementById("target");
40 var iframeScroller = targetIframe.contentDocument.scrollingElement;
41
42 // Observer created here, callback created in iframe context. Timestamps should be
43 // from this window.
44 var observer = new IntersectionObserver(
45 targetIframe.contentDocument.createObserverCallback(topWindowEntries), {});
46 assert_true(!!observer, "Observer exists");
47 observer.observe(target);
48
49 // Callback created here, observer created in iframe. Timestamps should be
50 // from iframe window.
51 observer = targetIframe.contentDocument.createObserver(function(newEntries) {
52 iframeWindowEntries = iframeWindowEntries.concat(newEntries);
53 });
54 observer.observe(target);
55 runTestCycle(step1, "First rAF after iframe is loaded.");
56 t.done();
57 };
Tobias Schneidere8d533c2017-08-31 15:55:2158 }, timeSkew);
Tobias Schneider9a6600e2017-06-28 17:46:4559}, "Check that timestamps correspond to the to execution context that created the observer.");
60
61function step1() {
62 document.scrollingElement.scrollTop = 200;
63 targetIframe.contentDocument.scrollingElement.scrollTop = 250;
64 topWindowTimeBeforeNotification = performance.now();
65 iframeWindowTimeBeforeNotification = targetIframe.contentWindow.performance.now();
66 runTestCycle(step2, "Generate notifications.");
67 assert_equals(topWindowEntries.length, 1, "One notification to top window observer.");
68 assert_equals(iframeWindowEntries.length, 1, "One notification to iframe observer.");
69}
70
71function step2() {
72 document.scrollingElement.scrollTop = 0;
73 var topWindowTimeAfterNotification = performance.now();
74 var iframeWindowTimeAfterNotification = targetIframe.contentWindow.performance.now();
75
Tobias Schneidere8d533c2017-08-31 15:55:2176 assert_approx_equals(
77 topWindowEntries[1].time - topWindowTimeBeforeNotification,
78 iframeWindowEntries[1].time - iframeWindowTimeBeforeNotification,
79 // Since all intersections are computed in a tight loop between 2 frames,
80 // an epsilon of 16ms (the length of one frame at 60Hz) turned out to be
81 // reliable, even at slow frame rates.
82 16,
83 "Notification times are relative to the expected time origins");
Tobias Schneider9a6600e2017-06-28 17:46:4584
85 assert_equals(topWindowEntries.length, 2, "Top window observer has two notifications.");
86 assert_between_inclusive(
87 topWindowEntries[1].time,
88 topWindowTimeBeforeNotification,
89 topWindowTimeAfterNotification,
90 "Notification to top window observer is within the expected range.");
91
92 assert_equals(iframeWindowEntries.length, 2, "Iframe observer has two notifications.");
93 assert_between_inclusive(
94 iframeWindowEntries[1].time,
95 iframeWindowTimeBeforeNotification,
96 iframeWindowTimeAfterNotification,
97 "Notification to iframe observer is within the expected range.");
98}
99</script>