headless-browser: Move completion handlers for dump/ref tests to lambdas

No functional difference here, and this probably seems a bit silly, but
the purpose is to make the diff of a future patch much easier to read.
This commit is contained in:
Timothy Flynn 2024-10-04 11:29:37 -04:00 committed by Andreas Kling
commit 49a53a6194
Notes: github-actions[bot] 2024-10-06 17:25:25 +00:00

View file

@ -291,48 +291,7 @@ static ErrorOr<TestResult> run_dump_test(HeadlessWebContentView& view, URL::URL
auto did_finish_test = false; auto did_finish_test = false;
auto did_finish_loading = false; auto did_finish_loading = false;
if (mode == TestMode::Layout) { auto handle_completed_test = [&]() -> ErrorOr<TestResult> {
view.on_load_finish = [&](auto const& loaded_url) {
// This callback will be called for 'about:blank' first, then for the URL we actually want to dump
VERIFY(url.equals(loaded_url, URL::ExcludeFragment::Yes) || loaded_url.equals(URL::URL("about:blank")));
if (url.equals(loaded_url, URL::ExcludeFragment::Yes)) {
// NOTE: We take a screenshot here to force the lazy layout of SVG-as-image documents to happen.
// It also causes a lot more code to run, which is good for finding bugs. :^)
view.take_screenshot()->when_resolved([&](auto) {
auto promise = view.request_internal_page_info(WebView::PageInfoType::LayoutTree | WebView::PageInfoType::PaintTree);
result = MUST(promise->await());
loop.quit(0);
});
}
};
view.on_text_test_finish = {};
} else if (mode == TestMode::Text) {
view.on_load_finish = [&](auto const& loaded_url) {
// NOTE: We don't want subframe loads to trigger the test finish.
if (!url.equals(loaded_url, URL::ExcludeFragment::Yes))
return;
did_finish_loading = true;
if (did_finish_test)
loop.quit(0);
};
view.on_text_test_finish = [&](auto const& text) {
result = text;
did_finish_test = true;
if (did_finish_loading)
loop.quit(0);
};
}
view.load(url);
timeout_timer->start();
loop.exec();
if (did_timeout) if (did_timeout)
return TestResult::Timeout; return TestResult::Timeout;
@ -378,6 +337,51 @@ static ErrorOr<TestResult> run_dump_test(HeadlessWebContentView& view, URL::URL
TRY(Diff::write_unified(hunk, *out, color_output)); TRY(Diff::write_unified(hunk, *out, color_output));
return TestResult::Fail; return TestResult::Fail;
};
if (mode == TestMode::Layout) {
view.on_load_finish = [&](auto const& loaded_url) {
// This callback will be called for 'about:blank' first, then for the URL we actually want to dump
VERIFY(url.equals(loaded_url, URL::ExcludeFragment::Yes) || loaded_url.equals(URL::URL("about:blank")));
if (url.equals(loaded_url, URL::ExcludeFragment::Yes)) {
// NOTE: We take a screenshot here to force the lazy layout of SVG-as-image documents to happen.
// It also causes a lot more code to run, which is good for finding bugs. :^)
view.take_screenshot()->when_resolved([&](auto) {
auto promise = view.request_internal_page_info(WebView::PageInfoType::LayoutTree | WebView::PageInfoType::PaintTree);
result = MUST(promise->await());
loop.quit(0);
});
}
};
view.on_text_test_finish = {};
} else if (mode == TestMode::Text) {
view.on_load_finish = [&](auto const& loaded_url) {
// NOTE: We don't want subframe loads to trigger the test finish.
if (!url.equals(loaded_url, URL::ExcludeFragment::Yes))
return;
did_finish_loading = true;
if (did_finish_test)
loop.quit(0);
};
view.on_text_test_finish = [&](auto const& text) {
result = text;
did_finish_test = true;
if (did_finish_loading)
loop.quit(0);
};
}
view.load(url);
timeout_timer->start();
loop.exec();
return handle_completed_test();
} }
static ErrorOr<TestResult> run_ref_test(HeadlessWebContentView& view, URL::URL const& url, int timeout_in_milliseconds = DEFAULT_TIMEOUT_MS) static ErrorOr<TestResult> run_ref_test(HeadlessWebContentView& view, URL::URL const& url, int timeout_in_milliseconds = DEFAULT_TIMEOUT_MS)
@ -391,28 +395,8 @@ static ErrorOr<TestResult> run_ref_test(HeadlessWebContentView& view, URL::URL c
}); });
RefPtr<Gfx::Bitmap> actual_screenshot, expectation_screenshot; RefPtr<Gfx::Bitmap> actual_screenshot, expectation_screenshot;
view.on_load_finish = [&](auto const&) {
if (actual_screenshot) {
view.take_screenshot()->when_resolved([&](auto screenshot) {
expectation_screenshot = move(screenshot);
loop.quit(0);
});
} else {
view.take_screenshot()->when_resolved([&](auto screenshot) {
actual_screenshot = move(screenshot);
view.debug_request("load-reference-page");
});
}
};
view.on_text_test_finish = [&](auto const&) {
dbgln("Unexpected text test finished during ref test for {}", url);
};
view.load(url);
timeout_timer->start();
loop.exec();
auto handle_completed_test = [&]() -> ErrorOr<TestResult> {
if (did_timeout) if (did_timeout)
return TestResult::Timeout; return TestResult::Timeout;
@ -441,6 +425,31 @@ static ErrorOr<TestResult> run_ref_test(HeadlessWebContentView& view, URL::URL c
} }
return TestResult::Fail; return TestResult::Fail;
};
view.on_load_finish = [&](auto const&) {
if (actual_screenshot) {
view.take_screenshot()->when_resolved([&](auto screenshot) {
expectation_screenshot = move(screenshot);
loop.quit(0);
});
} else {
view.take_screenshot()->when_resolved([&](auto screenshot) {
actual_screenshot = move(screenshot);
view.debug_request("load-reference-page");
});
}
};
view.on_text_test_finish = [&](auto const&) {
dbgln("Unexpected text test finished during ref test for {}", url);
};
view.load(url);
timeout_timer->start();
loop.exec();
return handle_completed_test();
} }
static ErrorOr<TestResult> run_test(HeadlessWebContentView& view, StringView input_path, StringView expectation_path, TestMode mode) static ErrorOr<TestResult> run_test(HeadlessWebContentView& view, StringView input_path, StringView expectation_path, TestMode mode)