Skip to content

Truly independent web browser

LibWeb+test-web: Wait for loading images before a ref test’s capture

sideshowbarker888c93605a59
diff --git a/Libraries/LibWeb/Internals/Internals.cpp b/Libraries/LibWeb/Internals/Internals.cpp
index 89a4eebf9f0..03bf443a15e 100644
--- a/Libraries/LibWeb/Internals/Internals.cpp
+++ b/Libraries/LibWeb/Internals/Internals.cpp
@@ -481,6 +481,16 @@ bool Internals::initial_paint_had_pending_fonts() const
return window().associated_document().font_computer().initial_paint_had_pending_fonts();
}
+bool Internals::has_pending_image_loads() const
+{
+ // A shared resource request stays in its fetching state until its image has decoded.
+ for (auto const& it : window().associated_document().shared_resource_requests()) {
+ if (it.value && it.value->is_fetching())
+ return true;
+ }
+ return false;
+}
+
void Internals::set_font_display_time(CSS::FontFace& font_face, u32 milliseconds)
{
font_face.set_font_display_time_for_testing(milliseconds);
diff --git a/Libraries/LibWeb/Internals/Internals.h b/Libraries/LibWeb/Internals/Internals.h
index 51e01e0922d..7006b4d97d6 100644
--- a/Libraries/LibWeb/Internals/Internals.h
+++ b/Libraries/LibWeb/Internals/Internals.h
@@ -69,6 +69,7 @@ public:
bool has_completed_initial_paint() const;
bool initial_paint_had_pending_fonts() const;
+ bool has_pending_image_loads() const;
void set_font_display_time(CSS::FontFace&, u32 milliseconds);
WebIDL::ExceptionOr<Utf16String> set_time_zone(Utf16String const& time_zone);
diff --git a/Libraries/LibWeb/Internals/Internals.idl b/Libraries/LibWeb/Internals/Internals.idl
index 8372e84037b..f9d321db23c 100644
--- a/Libraries/LibWeb/Internals/Internals.idl
+++ b/Libraries/LibWeb/Internals/Internals.idl
@@ -24,6 +24,8 @@ interface Internals {
readonly attribute boolean hasCompletedInitialPaint;
readonly attribute boolean initialPaintHadPendingFonts;
+ // Whether any image this document fetched is still loading or decoding.
+ readonly attribute boolean hasPendingImageLoads;
// Freezes the font display clock at the given elapsed time, without starting a font load.
undefined setFontDisplayTime(FontFace fontFace, unsigned long milliseconds);
DOMString setTimeZone(DOMString timeZone);
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index 46b321f9dfc..a60910c409f 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -1321,10 +1321,6 @@ Text/input/SiteIsolation/iframe/mixed-content-of-an-isolated-iframes-navigation-
Text/input/wpt-import/webmessaging/broadcastchannel/opaque-origin.html
[Skipped]
-
-; Flakes in CI
-Ref/input/wpt-import/css/css-content/element-replacement-dynamic.html
-
; Need absolutely positioned boxes fragmented across columns
Ref/input/wpt-import/css/css-position/position-absolute-in-inline-010.html
Ref/input/wpt-import/css/css-position/position-absolute-in-inline-011.html
diff --git a/Tests/LibWeb/test-web/main.cpp b/Tests/LibWeb/test-web/main.cpp
index db19998c843..64f5e28777c 100644
--- a/Tests/LibWeb/test-web/main.cpp
+++ b/Tests/LibWeb/test-web/main.cpp
@@ -269,8 +269,17 @@ static void try_write_harness_status(StringView reason)
warnln("Failed to write test-web harness status: {}", result.error());
}
-static String generate_wait_for_test_string(StringView wait_class, StringView on_finish_script = ""sv)
+enum class WaitForImages {
+ No,
+ Yes,
+};
+
+static String generate_wait_for_test_string(StringView wait_class, WaitForImages wait_for_images = WaitForImages::No, StringView on_finish_script = ""sv)
{
+ // A reference test's screenshot has to show the images the page uses. But a CSS image only starts loading once
+ // layout reaches the element that uses it, which can be after the test is otherwise done. So a reference test
+ // forces a layout, then waits until no image is loading. It polls with timers rather than animation frames, since
+ // a test may hold rendering back, and the capture itself updates the layout.
return MUST(String::formatted(R"(
function hasTestWaitClass() {{
return document.documentElement.classList.contains('{}');
@@ -280,13 +289,27 @@ function afterFontsAndPaint(callback) {{
document.fonts.ready.then(() => requestAnimationFrame(() => requestAnimationFrame(callback)));
}}
+const waitForImages = {};
+
+function afterImagesLoad(callback) {{
+ if (!waitForImages) {{
+ callback();
+ return;
+ }}
+ document.documentElement.getBoundingClientRect();
+ if (internals.hasPendingImageLoads)
+ setTimeout(() => afterImagesLoad(callback), 10);
+ else
+ callback();
+}}
+
if (!hasTestWaitClass()) {{
- afterFontsAndPaint(() => internals.signalTestIsDone("PASS"));
+ afterFontsAndPaint(() => afterImagesLoad(() => internals.signalTestIsDone("PASS")));
}} else {{
const observer = new MutationObserver(() => {{
if (!hasTestWaitClass()) {{
observer.disconnect();
- internals.signalTestIsDone("PASS");
+ afterImagesLoad(() => internals.signalTestIsDone("PASS"));
}}
}});
@@ -297,13 +320,13 @@ if (!hasTestWaitClass()) {{
{}
}}
)"sv,
- wait_class, on_finish_script));
+ wait_class, wait_for_images == WaitForImages::Yes ? "true"sv : "false"sv, on_finish_script));
}
static auto wait_for_test_completion = generate_wait_for_test_string("test-wait"sv);
// https://web-platform-tests.org/writing-tests/reftests.html#waiting-for-content-to-render
-static auto wait_for_reftest_completion = generate_wait_for_test_string("reftest-wait"sv, R"(
+static auto wait_for_reftest_completion = generate_wait_for_test_string("reftest-wait"sv, WaitForImages::Yes, R"(
afterFontsAndPaint(() => document.documentElement.dispatchEvent(new Event("TestRendered", { bubbles: true })));
)"sv);