| Tobias Schneider | 9a6600e | 2017-06-28 17:46:45 | [diff] [blame] | 1 | <!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> |
| 7 | pre, #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 Schneider | e8d533c | 2017-08-31 15:55:21 | [diff] [blame] | 21 | // Pick this number to be comfortably greater than the length of two frames at 60Hz. |
| 22 | var timeSkew = 40; |
| 23 | |
| Tobias Schneider | 9a6600e | 2017-06-28 17:46:45 | [diff] [blame] | 24 | var topWindowEntries = []; |
| 25 | var iframeWindowEntries = []; |
| 26 | var targetIframe; |
| 27 | var topWindowTimeBeforeNotification; |
| 28 | var iframeWindowTimeBeforeNotification; |
| 29 | |
| 30 | async_test(function(t) { |
| Tobias Schneider | e8d533c | 2017-08-31 15:55:21 | [diff] [blame] | 31 | t.step_timeout(function() { |
| Tobias Schneider | 9a6600e | 2017-06-28 17:46:45 | [diff] [blame] | 32 | 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 Schneider | e8d533c | 2017-08-31 15:55:21 | [diff] [blame] | 58 | }, timeSkew); |
| Tobias Schneider | 9a6600e | 2017-06-28 17:46:45 | [diff] [blame] | 59 | }, "Check that timestamps correspond to the to execution context that created the observer."); |
| 60 | |
| 61 | function 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 | |
| 71 | function step2() { |
| 72 | document.scrollingElement.scrollTop = 0; |
| 73 | var topWindowTimeAfterNotification = performance.now(); |
| 74 | var iframeWindowTimeAfterNotification = targetIframe.contentWindow.performance.now(); |
| 75 | |
| Tobias Schneider | e8d533c | 2017-08-31 15:55:21 | [diff] [blame] | 76 | 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 Schneider | 9a6600e | 2017-06-28 17:46:45 | [diff] [blame] | 84 | |
| 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> |