| OLD | NEW |
| (Empty) | |
| 1 <!DOCTYPE html> |
| 2 <title>window.requestIdleCallback callback behavior during idle periods.</title> |
| 3 <link rel="author" title="Ross McIlroy" href="mailto:rmcilroy@chromium.org" /> |
| 4 <script src="/resources/testharness.js"></script> |
| 5 <script src="/resources/testharnessreport.js"></script> |
| 6 <script> |
| 7 |
| 8 async_test(function() { |
| 9 // Check that two separate calls to requestIdleCallbacks can run in the same |
| 10 // idle period. |
| 11 var callback_1_deadline; |
| 12 var callback_1_has_run = false; |
| 13 var callback_1 = function(deadline) { |
| 14 callback_1_has_run = true; |
| 15 var remaining = deadline.timeRemaining(); |
| 16 if (remaining >= 5) { |
| 17 // Should be enough time to run the next callback in the current idle |
| 18 // period. |
| 19 callback_1_deadline = performance.now() + remaining; |
| 20 } |
| 21 }; |
| 22 var callback_2 = this.step_func(function(deadline) { |
| 23 assert_true(callback_1_has_run, "Should run callbacks in order of posting if
they don't have a timeout."); |
| 24 if (callback_1_deadline != undefined) { |
| 25 assert_true(performance.now() < callback_1_deadline, "Should be able to ru
n both callbacks in the same idle period."); |
| 26 this.done(); |
| 27 } else { |
| 28 // Might not have had enough idle time, so try again. |
| 29 callback_1_has_run = false; |
| 30 setTimeout(function() { |
| 31 requestIdleCallback(callback_1); |
| 32 requestIdleCallback(callback_2); |
| 33 }, 0); |
| 34 } |
| 35 }); |
| 36 requestIdleCallback(callback_1); |
| 37 requestIdleCallback(callback_2); |
| 38 }, 'requestIdleCallback can run multiple different requestIdleCallback callbacks
in the same idle period.'); |
| 39 |
| 40 async_test(function() { |
| 41 // Check that if an idle callback calls requestIdleCallback, the new callback |
| 42 // doesn't get the same deadline (i.e., runs in a new idle period). |
| 43 var previous_deadline = undefined; |
| 44 var idle_callbacks_remaining = 10; |
| 45 var rIC = this.step_func(function(deadline) { |
| 46 var now = performance.now(); |
| 47 var remaining = deadline.timeRemaining(); |
| 48 var new_deadline = now + remaining; |
| 49 if (previous_deadline != undefined) { |
| 50 assert_true(new_deadline > previous_deadline, "A requestIdleCallback sched
uled during an idle period should be called back with a deadline greater than th
at in the current idle period."); |
| 51 } |
| 52 |
| 53 // Schedule a new requestIdleCallback. |
| 54 if (--idle_callbacks_remaining > 0) { |
| 55 previous_deadline = new_deadline; |
| 56 requestIdleCallback(rIC); |
| 57 } else { |
| 58 this.done(); |
| 59 } |
| 60 }); |
| 61 |
| 62 // Spin an empty rAF loop to cause an idle period each frame. |
| 63 var idle_task_posted = false; |
| 64 requestAnimationFrame(function rAFLoop() { |
| 65 if (!idle_task_posted) { |
| 66 requestIdleCallback(rIC); |
| 67 idle_task_posted = true; |
| 68 } |
| 69 requestAnimationFrame(rAFLoop); |
| 70 }); |
| 71 }, 'Check that if an idle callback calls requestIdleCallback the new callback do
esn\'t run in the current idle period.'); |
| 72 </script> |
| 73 <h1>Test of requestIdleCallback idle period behavior</h1> |
| 74 <p>This test validates that window.requestIdleCallback deals with callbacks duri
ng idle periods correctly.</p> |
| 75 <div id="log"></div> |
| OLD | NEW |