<!DOCTYPE html>
<html>
<head>
  <title>time origin value manual test</title>
  <link rel="help" href="https://w3c.github.io/hr-time/#time-origin-1">
  <link rel="prefetch" href="./resources/unload-a.html">
  <link rel="prefetch" href="./resources/unload-b.html">
  <link rel="prefetch" href="./resources/unload-c.html">
</head>
<body>
  <script src="/resources/testharness.js"></script>
  <script src="/resources/testharnessreport.js"></script>
  <script>
    setup({ explicit_timeout: true });

    const ACCEPTABLE_VARIANCE = 400; // ms

    const isRoughlyEqual = (a, b) => Math.abs(a - b) < ACCEPTABLE_VARIANCE;

    const timings = { a: {}, b: {}, c: {} };
    const t = async_test("hr-time time origin");

    window.mark = msg => {
      timings[msg.docName][msg.lifecycleEventName] = {
        performanceNow: msg.performanceNow,
        dateNow: msg.dateNow
      };

      if (msg.docName === "c" && msg.lifecycleEventName === "unload") {
        setTimeout(makeAssertions, 0);
      }
    };

    function makeAssertions () {
      t.step(() => {
        const loadTimeBetweenAandB = timings.b.load.dateNow - timings.a.unload.dateNow;
        const loadTimeBetweenBandC = timings.c.load.dateNow - timings.b.unload.dateNow;

        assert_true(
          isRoughlyEqual(loadTimeBetweenAandB, timings.b.load.performanceNow),
          "Document in reused window's time origin should be time of close of pop-up box."
        );
        assert_true(
          isRoughlyEqual(loadTimeBetweenBandC, timings.c.load.performanceNow),
          "Document in reused window's time origin should be time of close of pop-up box."
        );
        assert_true(
          !isRoughlyEqual(timings.a.unload.performanceNow, 0),
          "Time origin during unload event should match that of rest of document."
        );
        assert_true(
          !isRoughlyEqual(timings.b.unload.performanceNow, 0),
          "Time origin during unload event should match that of rest of document."
        );
        assert_true(
          !isRoughlyEqual(timings.c.unload.performanceNow, 0),
          "Time origin during unload event should match that of rest of document."
        );
      });
      t.done();
    }
  </script>

  <h2>Description</h2>
  <p>This test validates the behavior of <code>performance.now()</code> with respect to its time origin.</p>
  <div id="log">
   <h2>Manual Test Steps</h2>
   <ol>
    <li><a href="resources/unload-a.html" target="_blank">Click here</a>
   </ol>
  </div>
</body>
<html>
