https://github.com/web-platform-tests/wpt
Raw File
Tip revision: 1ea17fc1edda1c4cd0eb3915dc98ae8185e50d80 authored by James Graham on 22 August 2017, 14:43:51 UTC
Consolidate logic for multi-global tests.
Tip revision: 1ea17fc
timestamp.html
<!DOCTYPE html>
<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;
}
.spacer {
  height: calc(100vh + 100px);
}

</style>
<div id="leading-space" class="spacer"></div>
<div id="trailing-space" class="spacer"></div>

<script>
var topWindowEntries = [];
var iframeWindowEntries = [];
var targetIframe;
var topWindowTimeOnTestStart;
var topWindowTimeBeforeCreatingIframe;
var topWindowTimeBeforeNotification;
var iframeWindowTimeBeforeNotification;
var timeSkew;

function waitFor(numFrames, callback) {
  if (numFrames <= 0) {
    callback();
    return;
  }
  window.requestAnimationFrame(waitFor.bind(null, numFrames - 1, callback));
}

async_test(function(t) {
  topWindowTimeOnTestStart = performance.now();
  waitFor(3, function () {
    topWindowTimeBeforeCreatingIframe = performance.now();
    timeSkew = topWindowTimeBeforeCreatingIframe - topWindowTimeOnTestStart;
    targetIframe = document.createElement("iframe");
    assert_true(!!targetIframe, "iframe exists");
    targetIframe.src = "resources/timestamp-subframe.html";
    var trailingSpace = document.getElementById("trailing-space");
    assert_true(!!trailingSpace, "trailing-space exists");
    trailingSpace.parentNode.insertBefore(targetIframe, trailingSpace);
    targetIframe.onload = function() {
      var target = targetIframe.contentDocument.getElementById("target");
      var iframeScroller = targetIframe.contentDocument.scrollingElement;

      // Observer created here, callback created in iframe context.  Timestamps should be
      // from this window.
      var observer = new IntersectionObserver(
          targetIframe.contentDocument.createObserverCallback(topWindowEntries), {});
      assert_true(!!observer, "Observer exists");
      observer.observe(target);

      // Callback created here, observer created in iframe.  Timestamps should be
      // from iframe window.
      observer = targetIframe.contentDocument.createObserver(function(newEntries) {
        iframeWindowEntries = iframeWindowEntries.concat(newEntries);
      });
      observer.observe(target);
      runTestCycle(step1, "First rAF after iframe is loaded.");
      t.done();
    };
  });
}, "Check that timestamps correspond to the to execution context that created the observer.");

function step1() {
  document.scrollingElement.scrollTop = 200;
  targetIframe.contentDocument.scrollingElement.scrollTop = 250;
  topWindowTimeBeforeNotification = performance.now();
  iframeWindowTimeBeforeNotification = targetIframe.contentWindow.performance.now();
  runTestCycle(step2, "Generate notifications.");
  assert_equals(topWindowEntries.length, 1, "One notification to top window observer.");
  assert_equals(iframeWindowEntries.length, 1, "One notification to iframe observer.");
}

function step2() {
  document.scrollingElement.scrollTop = 0;
  var topWindowTimeAfterNotification = performance.now();
  var iframeWindowTimeAfterNotification = targetIframe.contentWindow.performance.now();

  // Test results are only significant if there's a gap between
  // top window time and iframe window time.
  assert_greater_than(topWindowTimeBeforeNotification, iframeWindowTimeAfterNotification,
    "Time ranges for top and iframe windows are disjoint.");

  assert_equals(topWindowEntries.length, 2, "Top window observer has two notifications.");
  assert_between_inclusive(
      topWindowEntries[1].time,
      topWindowTimeBeforeNotification,
      topWindowTimeAfterNotification,
      "Notification to top window observer is within the expected range.");

  assert_equals(iframeWindowEntries.length, 2, "Iframe observer has two notifications.");
  assert_between_inclusive(
      iframeWindowEntries[1].time,
      iframeWindowTimeBeforeNotification,
      iframeWindowTimeAfterNotification,
      "Notification to iframe observer is within the expected range.");
}
</script>
back to top