2 <title>window.requestIdleCallback callback behavior during idle periods.
</title>
3 <link rel=
"author" title=
"Ross McIlroy" href=
"mailto:rmcilroy@chromium.org" />
4 <link rel=
"help" href=
"http://www.w3.org/TR/requestidlecallback/"/>
5 <script src=
"../../../../resources/testharness.js"></script>
6 <script src=
"../../../../resources/testharnessreport.js"></script>
7 <link rel=
"stylesheet" href=
"../../../..//resources/testharness.css" />
10 async_test(function() {
11 // Check that two separate calls to requestIdleCallbacks can run in the same
13 var callback_1_deadline
;
14 var callback_1_has_run
= false;
15 var callback_1 = function(deadline
) {
16 callback_1_has_run
= true;
17 var remaining
= deadline
.timeRemaining();
19 // Should be enough time to run the next callback in the current idle
21 callback_1_deadline
= performance
.now() + remaining
;
24 var callback_2
= this.step_func(function(deadline
) {
25 assert_true(callback_1_has_run
, "Should run callbacks in order of posting if they don't have a timeout.");
26 if (callback_1_deadline
!= undefined) {
27 assert_true(performance
.now() < callback_1_deadline
, "Should be able to run both callbacks in the same idle period.");
30 // Might not have had enough idle time, so try again.
31 callback_1_has_run
= false;
32 setTimeout(function() {
33 requestIdleCallback(callback_1
);
34 requestIdleCallback(callback_2
);
38 requestIdleCallback(callback_1
);
39 requestIdleCallback(callback_2
);
40 }, 'requestIdleCallback can run multiple different requestIdleCallback callbacks in the same idle period.');
42 async_test(function() {
43 // Check that if an idle callback calls requestIdleCallback the new callback
44 // doesn't run in the current idle period.
45 var previous_deadline
;
46 var idle_callbacks_remaining
= 10;
48 requestIdleCallback(this.step_func(function rIC(deadline
) {
49 var remaining
= deadline
.timeRemaining();
50 var now
= performance
.now();
51 if (previous_deadline
!= undefined) {
52 assert_true(now
>= previous_deadline
, "A requestIdleCallback called during an idle period should not be run until the next idle period.");
55 // Schedule a new requestIdleCallback.
56 if (--idle_callbacks_remaining
> 0) {
57 previous_deadline
= now
+ remaining
;
58 requestIdleCallback(rIC
);
64 // Spin an empty rAF loop to cause an idle period each frame.
65 requestAnimationFrame(this.step_func(function rAFLoop() {
66 requestAnimationFrame(rAFLoop
);
68 }, 'Check that if an idle callback calls requestIdleCallback the new callback doesn\'t run in the current idle period.');
71 <p>This test validates that window.requestIdleCallback deals with callbacks during idle periods correctly.
</p>