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 id="beforeFrame" style="width:100%; height:700px;"></div> | 4 |
5 <div id="afterFrame" style="width:100%; height:700px;"></div> | 5 <div id="leading-space" style="width:100%; height:700px;"></div> |
| 6 <div id="trailing-space" style="width:100%; height:700px;"></div> |
6 | 7 |
7 <script> | 8 <script> |
8 description("Test that intersection observer time is relative to time in the cal
lback context."); | 9 function waitForNotification(f) { |
9 var topWindowEntries = []; | 10 requestAnimationFrame(() => { |
10 var iframeWindowEntries = []; | 11 setTimeout(() => { |
11 var topWindowObserver; | 12 setTimeout(f); |
12 var iframeWindowObserver; | 13 }); |
13 var targetIframe; | |
14 var iframeScroller; | |
15 var topWindowTime; | |
16 var iframeWindowTime; | |
17 var timestampTolerance = 24; | |
18 | |
19 function step0() { | |
20 // Test results are only significant if there's a sufficient gap between | |
21 // top window time and iframe window time. | |
22 topWindowTime = performance.now(); | |
23 iframeWindowTime = targetIframe.contentWindow.performance.now(); | |
24 shouldBeGreaterThan("topWindowTime - iframeWindowTime", "2.5 * timestampTolera
nce"); | |
25 | |
26 shouldBeEqualToNumber("topWindowEntries.length", 0); | |
27 shouldBeEqualToNumber("iframeWindowEntries.length", 0); | |
28 document.scrollingElement.scrollTop = 200; | |
29 iframeScroller.scrollTop = 250; | |
30 waitForNotification(step1); | |
31 } | |
32 | |
33 function step1() { | |
34 topWindowTime = performance.now(); | |
35 iframeWindowTime = targetIframe.contentWindow.performance.now(); | |
36 shouldBeEqualToNumber("topWindowEntries.length", 1); | |
37 if (topWindowEntries.length) | |
38 shouldBeCloseTo("topWindowEntries[0].time", "topWindowTime", timestampTolera
nce); | |
39 | |
40 shouldBeEqualToNumber("iframeWindowEntries.length", 1); | |
41 if (iframeWindowEntries.length) { | |
42 shouldBeCloseTo("iframeWindowEntries[0].time", "iframeWindowTime", timestamp
Tolerance); | |
43 } | |
44 waitForNotification(finishJSTest); | |
45 document.scrollingElement.scrollTop = 0; | |
46 } | |
47 | |
48 function runTest() { | |
49 var target = targetIframe.contentDocument.getElementById("target"); | |
50 iframeScroller = targetIframe.contentDocument.scrollingElement; | |
51 | |
52 // Observer created here, callback created in iframe context. Timestamps shou
ld be | |
53 // from this window. | |
54 topWindowObserver = new IntersectionObserver(targetIframe.contentDocument.crea
teObserverCallback(topWindowEntries), {}); | |
55 topWindowObserver.observe(target); | |
56 | |
57 // Callback created here, observer created in iframe. Timestamps should be | |
58 // from iframe window. | |
59 iframeWindowObserver = targetIframe.contentDocument.createObserver(function(ne
wEntries) { | |
60 for (var i = 0; i < newEntries.length; i++) | |
61 iframeWindowEntries.push(newEntries[i]); | |
62 }); | 14 }); |
63 iframeWindowObserver.observe(target); | |
64 | |
65 waitForNotification(step0); | |
66 } | 15 } |
67 | 16 |
68 onload = function() { | 17 onload = function() { |
| 18 var t = async_test("Test that intersection observer time is relative to time i
n the callback context."); |
| 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 // Pick this number to be comfortably greater than the length of two frames at
60Hz. |
| 24 var timeSkew = 40; |
| 25 |
| 26 var topWindowEntries = []; |
| 27 var iframeWindowEntries = []; |
| 28 var targetIframe; |
| 29 var topWindowTimeBeforeNotification; |
| 30 var iframeWindowTimeBeforeNotification; |
| 31 |
69 setTimeout(function() { | 32 setTimeout(function() { |
70 targetIframe = document.createElement("iframe"); | 33 targetIframe = document.createElement("iframe"); |
71 targetIframe.src = "../resources/intersection-observer-timestamp-subframe.ht
ml"; | 34 targetIframe.src = "resources/timestamp-subframe.html"; |
72 targetIframe.style = "height: 100px; overflow-y: scroll"; | 35 var trailingSpace = document.getElementById("trailing-space"); |
73 var afterFrame = document.getElementById("afterFrame"); | 36 trailingSpace.parentNode.insertBefore(targetIframe, trailingSpace); |
74 afterFrame.parentNode.insertBefore(targetIframe, afterFrame); | 37 targetIframe.onload = step0; |
75 targetIframe.onload = runTest; | 38 }, timeSkew); |
76 }, 2.5 * timestampTolerance); | 39 |
| 40 function step0() { |
| 41 var target = targetIframe.contentDocument.getElementById("target"); |
| 42 var iframeScroller = targetIframe.contentDocument.scrollingElement; |
| 43 |
| 44 // Observer created here, callback created in iframe context. Timestamps sh
ould be |
| 45 // from this window. |
| 46 var observer = new IntersectionObserver( |
| 47 targetIframe.contentDocument.createObserverCallback(topWindowEntries), {
}); |
| 48 observer.observe(target); |
| 49 |
| 50 // Callback created here, observer created in iframe. Timestamps should be |
| 51 // from iframe window. |
| 52 observer = targetIframe.contentDocument.createObserver(function(newEntries)
{ |
| 53 for (var i = 0; i < newEntries.length; i++) |
| 54 iframeWindowEntries.push(newEntries[i]); |
| 55 }); |
| 56 observer.observe(target); |
| 57 waitForNotification(step1); |
| 58 } |
| 59 |
| 60 function step1() { |
| 61 test(function() { |
| 62 assert_equals(topWindowEntries.length, 0); |
| 63 }, "No notifications to top window observer."); |
| 64 test(function() { |
| 65 assert_equals(iframeWindowEntries.length, 0); |
| 66 }, "No notifications to iframe observer."); |
| 67 document.scrollingElement.scrollTop = 200; |
| 68 targetIframe.contentDocument.scrollingElement.scrollTop = 250; |
| 69 topWindowTimeBeforeNotification = performance.now(); |
| 70 iframeWindowTimeBeforeNotification = targetIframe.contentWindow.performance.
now(); |
| 71 waitForNotification(step2); |
| 72 } |
| 73 |
| 74 function step2() { |
| 75 var topWindowTimeAfterNotification = performance.now(); |
| 76 var iframeWindowTimeAfterNotification = targetIframe.contentWindow.performan
ce.now(); |
| 77 |
| 78 // Test results are only significant if there's a gap between |
| 79 // top window time and iframe window time. |
| 80 test(function() { |
| 81 assert_greater_than(topWindowTimeBeforeNotification, iframeWindowTimeAfter
Notification); |
| 82 }, "Time ranges for top and iframe windows are disjoint."); |
| 83 |
| 84 test(function() { |
| 85 assert_equals(topWindowEntries.length, 1) |
| 86 }, "Top window observer has one notification."); |
| 87 if (topWindowEntries.length > 0) { |
| 88 test(function() { |
| 89 assert_between_inclusive( |
| 90 topWindowEntries[0].time, |
| 91 topWindowTimeBeforeNotification, |
| 92 topWindowTimeAfterNotification); |
| 93 }, "Notification to top window observer is within the expected range."); |
| 94 } |
| 95 |
| 96 test(function() { |
| 97 assert_equals(iframeWindowEntries.length, 1) |
| 98 }, "Iframe observer has one notification."); |
| 99 if (iframeWindowEntries.length > 0) { |
| 100 test(function() { |
| 101 assert_between_inclusive( |
| 102 iframeWindowEntries[0].time, |
| 103 iframeWindowTimeBeforeNotification, |
| 104 iframeWindowTimeAfterNotification); |
| 105 }, "Notification to iframe observer is within the expected range."); |
| 106 } |
| 107 |
| 108 document.getElementById("leading-space").style.height = ""; |
| 109 document.getElementById("trailing-space").style.height = ""; |
| 110 document.scrollingElement.scrollTop = 0; |
| 111 t.done(); |
| 112 } |
77 }; | 113 }; |
78 | |
79 </script> | 114 </script> |
OLD | NEW |