blob: 429134ec4360f1bbbae4db608a93a6ba25930604 [file] [log] [blame]
rmcilroy9d39ad82016-11-16 16:32:221<!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>
qiuzhong9f2b7132018-12-10 09:53:507// The window.requestIdleCallback function is used to request callbacks during browser-defined idle time.
rmcilroy9d39ad82016-11-16 16:32:228test(function() {
9 assert_equals(typeof window.requestIdleCallback, "function");
qiuzhong9f2b7132018-12-10 09:53:5010}, "window.requestIdleCallback is defined");
rmcilroy9d39ad82016-11-16 16:32:2211
qiuzhong9f2b7132018-12-10 09:53:5012// The window.cancelIdleCallback function is used to cancel callbacks scheduled via requestIdleCallback.
rmcilroy9d39ad82016-11-16 16:32:2213test(function() {
14 assert_equals(typeof window.cancelIdleCallback, "function");
qiuzhong9f2b7132018-12-10 09:53:5015}, "window.cancelIdleCallback is defined");
rmcilroy9d39ad82016-11-16 16:32:2216
qiuzhong9f2b7132018-12-10 09:53:5017// The requestIdleCallback method MUST return a long
rmcilroy9d39ad82016-11-16 16:32:2218test(function() {
19 assert_equals(typeof window.requestIdleCallback(function() {}), "number");
qiuzhong9f2b7132018-12-10 09:53:5020}, "window.requestIdleCallback() returns a number");
rmcilroy9d39ad82016-11-16 16:32:2221
qiuzhong9f2b7132018-12-10 09:53:5022// The cancelIdleCallback method MUST return void
rmcilroy9d39ad82016-11-16 16:32:2223test(function() {
24 assert_equals(typeof window.cancelIdleCallback(1), "undefined");
qiuzhong9f2b7132018-12-10 09:53:5025}, "window.cancelIdleCallback() returns undefined");
rmcilroy9d39ad82016-11-16 16:32:2226
27async_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
41async_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);
Philip Jägenstedtd9779042017-10-04 12:31:2448 step_timeout(this.step_func_done(), 200);
rmcilroy9d39ad82016-11-16 16:32:2249}, 'cancelIdleCallback cancels callbacks');
50
51</script>
52<h1>Basic requestIdleCallback Tests</h1>
53<div id="log"></div>