diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
commit | 43a97878ce14b72f0981164f87f2e35e14151312 (patch) | |
tree | 620249daf56c0258faa40cbdcf9cfba06de2a846 /testing/web-platform/tests/paint-timing/resources/utils.js | |
parent | Initial commit. (diff) | |
download | firefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip |
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/web-platform/tests/paint-timing/resources/utils.js')
-rw-r--r-- | testing/web-platform/tests/paint-timing/resources/utils.js | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/testing/web-platform/tests/paint-timing/resources/utils.js b/testing/web-platform/tests/paint-timing/resources/utils.js new file mode 100644 index 0000000000..5766971dd0 --- /dev/null +++ b/testing/web-platform/tests/paint-timing/resources/utils.js @@ -0,0 +1,52 @@ +function waitForAnimationFrames(count) { + return new Promise(resolve => { + if (count-- <= 0) { + resolve(); + } else { + requestAnimationFrame(() => { + waitForAnimationFrames(count).then(resolve); + }); + } + }); +} + +// Asserts that there is currently no FCP reported. Pass t to add some wait, in case CSS is loaded +// and FCP is incorrectly fired afterwards. +async function assertNoFirstContentfulPaint(t) { + await waitForAnimationFrames(3); + assert_equals(performance.getEntriesByName('first-contentful-paint').length, 0, 'First contentful paint marked too early. '); +} + +// Function that is resolved once FCP is reported, using PerformanceObserver. It rejects after a long +// wait time so that failing tests don't timeout. +async function assertFirstContentfulPaint(t) { + return new Promise(resolve => { + function checkFCP() { + if (performance.getEntriesByName('first-contentful-paint').length === 1) { + resolve(); + } else { + t.step_timeout(checkFCP, 0); + } + } + t.step(checkFCP); + }); +} + +async function test_fcp(label, before_assert_fcp_func) { + setup({"hide_test_state": true}); + const style = document.createElement('style'); + document.head.appendChild(style); + await promise_test(async t => { + assert_implements(window.PerformancePaintTiming, "Paint Timing isn't supported."); + const main = document.getElementById('main'); + await new Promise(r => window.addEventListener('load', r)); + await assertNoFirstContentfulPaint(t); + main.className = 'preFCP'; + await assertNoFirstContentfulPaint(t); + if (before_assert_fcp_func) { + await before_assert_fcp_func(); + } + main.className = 'contentful'; + await assertFirstContentfulPaint(t); + }, label); +} |