run-after-layout-and-paint.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Run a callback after a frame update.
  2. //
  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:
  12. // if (window.testRunner)
  13. // testRunner.waitUntilDone();
  14. // runAfterLayoutAndPaint(function() {
  15. // ... // some code which modifies style/layout
  16. // if (window.testRunner)
  17. // testRunner.notifyDone();
  18. // // Or notifyDone any time later if needed.
  19. // });
  20. //
  21. // - autoNotifyDone mode, for new tests which just need to change style/layout
  22. // and finish:
  23. // runAfterLayoutAndPaint(function() {
  24. // ... // some code which modifies style/layout
  25. // }, true);
  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. //
  32. if (window.internals)
  33. internals.runtimeFlags.paintUnderInvalidationCheckingEnabled = true;
  34. function runAfterLayoutAndPaint(callback, autoNotifyDone) {
  35. if (!window.testRunner) {
  36. // For manual test. Delay 500ms to allow us to see the visual change
  37. // caused by the callback.
  38. setTimeout(callback, 500);
  39. return;
  40. }
  41. if (autoNotifyDone)
  42. testRunner.waitUntilDone();
  43. // We do requestAnimationFrame and setTimeout to ensure a frame has started
  44. // and layout and paint have run. The requestAnimationFrame fires after the
  45. // frame has started but before layout and paint. The setTimeout fires
  46. // at the beginning of the next frame, meaning that the previous frame has
  47. // completed layout and paint.
  48. // See http://crrev.com/c/1395193/10/third_party/blink/web_tests/http/tests/resources/run-after-layout-and-paint.js
  49. // for more discussions.
  50. requestAnimationFrame(function() {
  51. setTimeout(function() {
  52. callback();
  53. if (autoNotifyDone)
  54. testRunner.notifyDone();
  55. }, 1);
  56. });
  57. }
  58. function test_after_layout_and_paint(func, name, properties) {
  59. var test = async_test(name, properties);
  60. runAfterLayoutAndPaint(test.step_func(() => {
  61. func.call(test, test);
  62. test.done();
  63. }, false));
  64. }
  65. function async_test_after_layout_and_paint(func, name, properties) {
  66. var test = async_test(name, properties);
  67. runAfterLayoutAndPaint(test.step_func(() => {
  68. func.call(test, test);
  69. }, false));
  70. }