Xianzhu Wang | eca1777 | 2019-01-08 23:15:15 | [diff] [blame] | 1 | // Run a callback after a frame update. |
[email protected] | a0f4d1dc | 2015-02-26 01:31:43 | [diff] [blame] | 2 | // |
Xianzhu Wang | eca1777 | 2019-01-08 23:15:15 | [diff] [blame] | 3 | // Note that this file has two copies: |
| 4 | // resources/run-after-layout-and-paint.js |
| 5 | // and |
| 6 | // http/tests/resources/run-after-layout-and-paint.js. |
| 7 | // They should be kept always the same. |
| 8 | // |
| 9 | // The function runAfterLayoutAndPaint() has two modes: |
| 10 | // - traditional mode, for existing tests, and tests needing customized |
| 11 | // notifyDone timing: |
[email protected] | a0f4d1dc | 2015-02-26 01:31:43 | [diff] [blame] | 12 | // if (window.testRunner) |
| 13 | // testRunner.waitUntilDone(); |
[email protected] | 334039da | 2015-05-08 03:38:47 | [diff] [blame] | 14 | // runAfterLayoutAndPaint(function() { |
[email protected] | a0f4d1dc | 2015-02-26 01:31:43 | [diff] [blame] | 15 | // ... // some code which modifies style/layout |
| 16 | // if (window.testRunner) |
| 17 | // testRunner.notifyDone(); |
[email protected] | a0f4d1dc | 2015-02-26 01:31:43 | [diff] [blame] | 18 | // // Or notifyDone any time later if needed. |
| 19 | // }); |
| 20 | // |
Xianzhu Wang | eca1777 | 2019-01-08 23:15:15 | [diff] [blame] | 21 | // - autoNotifyDone mode, for new tests which just need to change style/layout |
| 22 | // and finish: |
[email protected] | 334039da | 2015-05-08 03:38:47 | [diff] [blame] | 23 | // runAfterLayoutAndPaint(function() { |
[email protected] | a0f4d1dc | 2015-02-26 01:31:43 | [diff] [blame] | 24 | // ... // some code which modifies style/layout |
| 25 | // }, true); |
Xianzhu Wang | eca1777 | 2019-01-08 23:15:15 | [diff] [blame] | 26 | // |
| 27 | // Note that because we always update a frame before finishing a test, |
| 28 | // we don't need |
| 29 | // runAfterLayoutAndPaint(function() { testRunner.notifyDone(); }) |
| 30 | // to ensure the test finish after a frame update. |
| 31 | // |
wangxianzhu | ecaad1e | 2016-07-18 16:54:55 | [diff] [blame] | 32 | if (window.internals) |
wangxianzhu | 5d28b2b6 | 2016-09-07 00:41:30 | [diff] [blame] | 33 | internals.runtimeFlags.paintUnderInvalidationCheckingEnabled = true; |
wangxianzhu | 86f41e76 | 2016-06-30 23:56:57 | [diff] [blame] | 34 | |
[email protected] | 334039da | 2015-05-08 03:38:47 | [diff] [blame] | 35 | function runAfterLayoutAndPaint(callback, autoNotifyDone) { |
[email protected] | a0f4d1dc | 2015-02-26 01:31:43 | [diff] [blame] | 36 | if (!window.testRunner) { |
| 37 | // For manual test. Delay 500ms to allow us to see the visual change |
| 38 | // caused by the callback. |
| 39 | setTimeout(callback, 500); |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | if (autoNotifyDone) |
| 44 | testRunner.waitUntilDone(); |
| 45 | |
Xianzhu Wang | eca1777 | 2019-01-08 23:15:15 | [diff] [blame] | 46 | // We do requestAnimationFrame and setTimeout to ensure a frame has started |
| 47 | // and layout and paint have run. The requestAnimationFrame fires after the |
| 48 | // frame has started but before layout and paint. The setTimeout fires |
| 49 | // at the beginning of the next frame, meaning that the previous frame has |
| 50 | // completed layout and paint. |
| 51 | // See http://crrev.com/c/1395193/10/third_party/blink/web_tests/http/tests/resources/run-after-layout-and-paint.js |
| 52 | // for more discussions. |
| 53 | requestAnimationFrame(function() { |
| 54 | setTimeout(function() { |
| 55 | callback(); |
| 56 | if (autoNotifyDone) |
| 57 | testRunner.notifyDone(); |
Wanming Lin | 79ddcf8 | 2020-12-18 05:51:00 | [diff] [blame] | 58 | }, 1); |
[email protected] | 8866d95 | 2014-02-28 01:14:10 | [diff] [blame] | 59 | }); |
| 60 | } |
Alice Boxhall | 2e84249 | 2019-02-27 19:48:44 | [diff] [blame] | 61 | |
| 62 | function test_after_layout_and_paint(func, name, properties) { |
| 63 | var test = async_test(name, properties); |
| 64 | runAfterLayoutAndPaint(test.step_func(() => { |
| 65 | func.call(test, test); |
| 66 | test.done(); |
| 67 | }, false)); |
| 68 | } |
| 69 | |
| 70 | function async_test_after_layout_and_paint(func, name, properties) { |
| 71 | var test = async_test(name, properties); |
| 72 | runAfterLayoutAndPaint(test.step_func(() => { |
| 73 | func.call(test, test); |
| 74 | }, false)); |
| 75 | } |