chromium/third_party/blink/web_tests/virtual/threaded/fast/idle-callback/idle_periods.html

<!DOCTYPE html>
<title>window.requestIdleCallback callback behavior during idle periods.</title>
<link rel="author" title="Ross McIlroy" href="mailto:[email protected]" />
<link rel="help" href="http://www.w3.org/TR/requestidlecallback/"/>
<script src="../../../../resources/testharness.js"></script>
<script src="../../../../resources/testharnessreport.js"></script>
<link rel="stylesheet" href="../../../..//resources/testharness.css" />
<script>

async_test(function() {
  // Check that two separate calls to requestIdleCallbacks can run in the same
  // idle period.
  var callback_1_deadline;
  var callback_1_has_run = false;
  var callback_1 = function(deadline) {
    callback_1_has_run = true;
    var remaining = deadline.timeRemaining();
    if (remaining >= 5) {
      // Should be enough time to run the next callback in the current idle
      // period.
      callback_1_deadline = performance.now() + remaining;
    }
  };
  var callback_2 = this.step_func(function(deadline) {
    assert_true(callback_1_has_run, "Should run callbacks in order of posting if they don't have a timeout.");
    if (callback_1_deadline != undefined) {
      assert_true(performance.now() < callback_1_deadline, "Should be able to run both callbacks in the same idle period.");
      this.done();
    } else {
      // Might not have had enough idle time, so try again.
      callback_1_has_run = false;
      setTimeout(function() {
        requestIdleCallback(callback_1);
        requestIdleCallback(callback_2);
      }, 0);
    }
  });
  requestAnimationFrame(() => {
    requestIdleCallback(callback_1);
    requestIdleCallback(callback_2);
  });
}, 'requestIdleCallback can run multiple different requestIdleCallback callbacks in the same idle period.');

async_test(function() {
  // Check that if an idle callback calls requestIdleCallback the new callback
  // doesn't run in the current idle period.
  var idle_callbacks_remaining = 10;
  var self = this;
  var last_cb = null;
  requestIdleCallback(this.step_func(function rIC() {
    assert_not_equals(last_cb, "rIC", "A requestIdleCallback called during an idle period should not be run until the next idle period.");
    last_cb = "rIC";

    // Schedule a new requestIdleCallback.
    if (--idle_callbacks_remaining > 0) {
      requestIdleCallback(self.step_func(rIC));
    } else {
      self.done();
    }
  }));

  // Spin an empty rAF loop to cause an idle period each frame.
  requestAnimationFrame(function rAFLoop() {
    last_cb = "rAF";
    requestAnimationFrame(rAFLoop);
  });
}, 'Check that if an idle callback calls requestIdleCallback the new callback doesn\'t run in the current idle period.');
</script>
<h1>Description</h1>
<p>This test validates that window.requestIdleCallback deals with callbacks during idle periods correctly.</p>
<div id="log"></div>