| rmcilroy | 9d39ad8 | 2016-11-16 16:32:22 | [diff] [blame] | 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> |
| qiuzhong | 9f2b713 | 2018-12-10 09:53:50 | [diff] [blame] | 7 | // The window.requestIdleCallback function is used to request callbacks during browser-defined idle time. |
| rmcilroy | 9d39ad8 | 2016-11-16 16:32:22 | [diff] [blame] | 8 | test(function() { |
| 9 | assert_equals(typeof window.requestIdleCallback, "function"); |
| qiuzhong | 9f2b713 | 2018-12-10 09:53:50 | [diff] [blame] | 10 | }, "window.requestIdleCallback is defined"); |
| rmcilroy | 9d39ad8 | 2016-11-16 16:32:22 | [diff] [blame] | 11 | |
| qiuzhong | 9f2b713 | 2018-12-10 09:53:50 | [diff] [blame] | 12 | // The window.cancelIdleCallback function is used to cancel callbacks scheduled via requestIdleCallback. |
| rmcilroy | 9d39ad8 | 2016-11-16 16:32:22 | [diff] [blame] | 13 | test(function() { |
| 14 | assert_equals(typeof window.cancelIdleCallback, "function"); |
| qiuzhong | 9f2b713 | 2018-12-10 09:53:50 | [diff] [blame] | 15 | }, "window.cancelIdleCallback is defined"); |
| rmcilroy | 9d39ad8 | 2016-11-16 16:32:22 | [diff] [blame] | 16 | |
| qiuzhong | 9f2b713 | 2018-12-10 09:53:50 | [diff] [blame] | 17 | // The requestIdleCallback method MUST return a long |
| rmcilroy | 9d39ad8 | 2016-11-16 16:32:22 | [diff] [blame] | 18 | test(function() { |
| 19 | assert_equals(typeof window.requestIdleCallback(function() {}), "number"); |
| qiuzhong | 9f2b713 | 2018-12-10 09:53:50 | [diff] [blame] | 20 | }, "window.requestIdleCallback() returns a number"); |
| rmcilroy | 9d39ad8 | 2016-11-16 16:32:22 | [diff] [blame] | 21 | |
| qiuzhong | 9f2b713 | 2018-12-10 09:53:50 | [diff] [blame] | 22 | // The cancelIdleCallback method MUST return void |
| rmcilroy | 9d39ad8 | 2016-11-16 16:32:22 | [diff] [blame] | 23 | test(function() { |
| 24 | assert_equals(typeof window.cancelIdleCallback(1), "undefined"); |
| qiuzhong | 9f2b713 | 2018-12-10 09:53:50 | [diff] [blame] | 25 | }, "window.cancelIdleCallback() returns undefined"); |
| rmcilroy | 9d39ad8 | 2016-11-16 16:32:22 | [diff] [blame] | 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); |
| Philip Jägenstedt | d977904 | 2017-10-04 12:31:24 | [diff] [blame] | 48 | step_timeout(this.step_func_done(), 200); |
| rmcilroy | 9d39ad8 | 2016-11-16 16:32:22 | [diff] [blame] | 49 | }, 'cancelIdleCallback cancels callbacks'); |
| 50 | |
| 51 | </script> |
| 52 | <h1>Basic requestIdleCallback Tests</h1> |
| 53 | <div id="log"></div> |