<!DOCTYPE HTML>
<meta charset=utf-8>
<title>Long Animation Frame Timing: basic</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/utils.js"></script>

<body>
<h1>Long Animation Frame: blocking duration</h1>
<div id="log"></div>
<script>

function loaf_blocking_duration_test(run, label) {
  const OVERHEAD_EPSILON = 5;
  const BLOCKING_THRESHOLD = 50;
  promise_test(async t => {
    let found = false;
    for (let i = 0; i < 10 && !found; ++i) {
      const longtask_promise = new Promise(resolve => new PerformanceObserver(
      (entries, observer) => {
        resolve(entries.getEntries());
        observer.disconnect();
      }).observe({entryTypes: ["longtask"]}));
      const [longtask_entries, loaf_entry] = await Promise.all(
        [longtask_promise, expect_long_frame(run, t)]);
      const overlapping = longtask_entries.filter(longtask =>
        (longtask.startTime >= loaf_entry.startTime &&
          longtask.startTime < (loaf_entry.startTime + loaf_entry.duration) &&
          (!loaf_entry.renderStart ||
          (longtask.startTime < loaf_entry.renderStart - OVERHEAD_EPSILON))));

      const longest_index = overlapping.reduce(
        (max, cur, i) => cur > overlapping[max] ? i : max, 0);
      let expected_blocking_duration = 0;
      overlapping.forEach(({duration}, i) => {
        if (i === longest_index && loaf_entry.renderStart)
          duration += loaf_entry.startTime + loaf_entry.duration -
          loaf_entry.renderStart;
        expected_blocking_duration += Math.max(0, duration - BLOCKING_THRESHOLD);
      });

      if (!overlapping.length && loaf_entry.renderStart) {
        expected_blocking_duration =
          Math.max(0,
            loaf_entry.startTime + loaf_entry.duration - loaf_entry.renderStart -
            BLOCKING_THRESHOLD);
      }

      if (Math.abs(loaf_entry.blockingDuration - expected_blocking_duration) <
          OVERHEAD_EPSILON) {
        found = true;
      }
    }
    assert_true(found);
  }, `LoAF blockingDuration should be equivalent to long tasks: ${label}`);
}

loaf_blocking_duration_test(t => t.step_timeout(busy_wait), "Non-rendering");
loaf_blocking_duration_test(t => t.step_timeout(() => {
  busy_wait();
  requestAnimationFrame(busy_wait);
}), "Rendering");
</script>
</body>
