WebContent+headless-browser: Implement mismatch conditions for ref tests

Ref tests can now have a `<link rel="mismatch">` condition. The test
will pass if the expectation page doesn't match the test page.
This commit is contained in:
Tim Ledbetter 2024-12-04 11:38:09 +00:00 committed by Tim Ledbetter
parent d4d335ebda
commit 2455618995
Notes: github-actions[bot] 2024-12-04 19:51:02 +00:00
5 changed files with 53 additions and 3 deletions

View file

@ -411,13 +411,35 @@ void ConnectionFromClient::debug_request(u64 page_id, ByteString const& request,
if (request == "load-reference-page") {
if (auto* document = page->page().top_level_browsing_context().active_document()) {
auto maybe_link = document->query_selector("link[rel=match]"sv);
auto has_mismatch_selector = false;
auto maybe_link = [&]() -> Web::WebIDL::ExceptionOr<GC::Ptr<Web::DOM::Element>> {
auto maybe_link = document->query_selector("link[rel=match]"sv);
if (maybe_link.is_error() || maybe_link.value())
return maybe_link;
auto maybe_mismatch_link = document->query_selector("link[rel=mismatch]"sv);
if (maybe_mismatch_link.is_error() || maybe_mismatch_link.value()) {
has_mismatch_selector = maybe_mismatch_link.value();
return maybe_mismatch_link;
}
return nullptr;
}();
if (maybe_link.is_error() || !maybe_link.value()) {
// To make sure that we fail the ref-test if the link is missing, load the error page->
load_html(page_id, "<h1>Failed to find &lt;link rel=&quot;match&quot; /&gt; in ref test page!</h1> Make sure you added it.");
load_html(page_id, "<h1>Failed to find &lt;link rel=&quot;match&quot; /&gt; or &lt;link rel=&quot;mismatch&quot; /&gt; in ref test page!</h1> Make sure you added it.");
} else {
auto link = maybe_link.release_value();
auto url = document->parse_url(link->get_attribute_value(Web::HTML::AttributeNames::href));
if (url.query().has_value() && !url.query()->is_empty()) {
load_html(page_id, "<h1>Invalid ref test link - query string must be empty</h1>");
return;
}
if (has_mismatch_selector)
url.set_query("mismatch"_string);
load_url(page_id, url);
}
}

View file

@ -0,0 +1,6 @@
<!DOCTYPE html>
<style>
:root {
background-color: red;
}
</style>

View file

@ -0,0 +1,7 @@
<!DOCTYPE html>
<link rel="mismatch" href="../expected/mismatch-test-ref.html" />
<style>
:root {
background-color: green;
}
</style>

View file

@ -247,7 +247,10 @@ static void run_ref_test(HeadlessWebView& view, Test& test, URL::URL const& url,
});
auto handle_completed_test = [&test, url]() -> ErrorOr<TestResult> {
if (test.actual_screenshot->visually_equals(*test.expectation_screenshot))
VERIFY(test.ref_test_expectation_type.has_value());
auto should_match = test.ref_test_expectation_type == RefTestExpectationType::Match;
auto screenshot_matches = test.actual_screenshot->visually_equals(*test.expectation_screenshot);
if (should_match == screenshot_matches)
return TestResult::Pass;
if (Application::the().dump_failed_ref_tests) {
@ -291,6 +294,11 @@ static void run_ref_test(HeadlessWebView& view, Test& test, URL::URL const& url,
view.on_load_finish = [&view, &test, on_test_complete = move(on_test_complete)](auto const&) {
if (test.actual_screenshot) {
if (view.url().query().has_value() && view.url().query()->equals_ignoring_ascii_case("mismatch"sv)) {
test.ref_test_expectation_type = RefTestExpectationType::Mismatch;
} else {
test.ref_test_expectation_type = RefTestExpectationType::Match;
}
view.take_screenshot()->when_resolved([&test, on_test_complete = move(on_test_complete)](RefPtr<Gfx::Bitmap> screenshot) {
test.expectation_screenshot = move(screenshot);
on_test_complete();

View file

@ -37,6 +37,11 @@ enum class TestResult {
Crashed,
};
enum class RefTestExpectationType {
Match,
Mismatch,
};
static constexpr StringView test_result_to_string(TestResult result)
{
switch (result) {
@ -69,6 +74,8 @@ struct Test {
bool did_finish_test { false };
bool did_finish_loading { false };
Optional<RefTestExpectationType> ref_test_expectation_type {};
RefPtr<Gfx::Bitmap> actual_screenshot {};
RefPtr<Gfx::Bitmap> expectation_screenshot {};
};