basic.html (2734B)
1 <!DOCTYPE html> 2 <title>window.requestIdleCallback exists</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 // The window.requestIdleCallback function is used to request callbacks during browser-defined idle time. 8 test(function() { 9 assert_equals(typeof window.requestIdleCallback, "function"); 10 }, "window.requestIdleCallback is defined"); 11 12 // The window.cancelIdleCallback function is used to cancel callbacks scheduled via requestIdleCallback. 13 test(function() { 14 assert_equals(typeof window.cancelIdleCallback, "function"); 15 }, "window.cancelIdleCallback is defined"); 16 17 // The requestIdleCallback method MUST return a long 18 test(function() { 19 assert_equals(typeof window.requestIdleCallback(function() {}), "number"); 20 }, "window.requestIdleCallback() returns a number"); 21 22 // The cancelIdleCallback method MUST return undefined 23 test(function() { 24 assert_equals(typeof window.cancelIdleCallback(1), "undefined"); 25 }, "window.cancelIdleCallback() returns undefined"); 26 27 async_test(function() { 28 // Check whether requestIdleCallback schedules a callback which gets executed 29 // and the deadline argument is passed correctly. 30 requestIdleCallback(this.step_func_done(function(deadline) { 31 assert_equals(arguments.length, 1, "Only one argument should be passed to callback."); 32 assert_class_string(deadline, "IdleDeadline"); 33 assert_equals(typeof deadline.timeRemaining, "function", "IdleDeadline.timeRemaining MUST be a function which returns the time remaining in milliseconds"); 34 assert_equals(typeof deadline.timeRemaining(), "number", "IdleDeadline.timeRemaining MUST return a double of the time remaining in milliseconds"); 35 assert_true(deadline.timeRemaining() <= 50, "IdleDeadline.timeRemaining() MUST be less than or equal to 50ms in the future."); 36 assert_equals(typeof deadline.didTimeout, "boolean", "IdleDeadline.didTimeout MUST be a boolean"); 37 assert_false(deadline.didTimeout, "IdleDeadline.didTimeout MUST be false if requestIdleCallback wasn't scheduled due to a timeout"); 38 })); 39 }, 'requestIdleCallback schedules callbacks'); 40 41 async_test(function() { 42 // Check whether requestIdleCallback schedules a callback which gets executed 43 // and the deadline argument is passed correctly. 44 var handle = requestIdleCallback(this.step_func(function(deadline) { 45 assert_unreached("callback should not be called if canceled with cancelIdleCallback"); 46 })); 47 cancelIdleCallback(handle); 48 step_timeout(this.step_func_done(), 200); 49 }, 'cancelIdleCallback cancels callbacks'); 50 51 </script> 52 <h1>Basic requestIdleCallback Tests</h1> 53 <div id="log"></div>