From c719a542c5a51016124040b7c092cba946b538fd Mon Sep 17 00:00:00 2001 From: martinfalisse Date: Sat, 6 May 2023 12:46:14 +0200 Subject: [PATCH] LibWeb: Add `--layout-test-mode` flag to HeadlessBrowser The `layout-test-mode` flag changes the font to be SerenitySans as this is the font used for layout tests for cross-platform compatibility of tests. --- Ladybird/FontPluginQt.cpp | 8 +++++++- Ladybird/FontPluginQt.h | 3 ++- Ladybird/WebContent/main.cpp | 4 +++- Userland/Libraries/LibWebView/ViewImplementation.cpp | 4 +++- Userland/Libraries/LibWebView/ViewImplementation.h | 7 ++++++- Userland/Utilities/headless-browser.cpp | 9 ++++++--- 6 files changed, 27 insertions(+), 8 deletions(-) diff --git a/Ladybird/FontPluginQt.cpp b/Ladybird/FontPluginQt.cpp index 11d21ba581b..93944178e7d 100644 --- a/Ladybird/FontPluginQt.cpp +++ b/Ladybird/FontPluginQt.cpp @@ -18,7 +18,8 @@ extern DeprecatedString s_serenity_resource_root; namespace Ladybird { -FontPluginQt::FontPluginQt() +FontPluginQt::FontPluginQt(bool is_layout_test_mode) + : m_is_layout_test_mode(is_layout_test_mode) { // Load the default SerenityOS fonts... Gfx::FontDatabase::set_default_fonts_lookup_path(DeprecatedString::formatted("{}/res/fonts", s_serenity_resource_root)); @@ -69,6 +70,11 @@ void FontPluginQt::update_generic_fonts() m_generic_font_names.resize(static_cast(Web::Platform::GenericFont::__Count)); auto update_mapping = [&](Web::Platform::GenericFont generic_font, QFont::StyleHint qfont_style_hint, ReadonlySpan fallbacks) { + if (m_is_layout_test_mode) { + m_generic_font_names[static_cast(generic_font)] = "SerenitySans"; + return; + } + QFont qt_font; qt_font.setStyleHint(qfont_style_hint); diff --git a/Ladybird/FontPluginQt.h b/Ladybird/FontPluginQt.h index 35a577aed30..984628855f6 100644 --- a/Ladybird/FontPluginQt.h +++ b/Ladybird/FontPluginQt.h @@ -14,7 +14,7 @@ namespace Ladybird { class FontPluginQt final : public Web::Platform::FontPlugin { public: - FontPluginQt(); + FontPluginQt(bool is_layout_test_mode); virtual ~FontPluginQt(); virtual Gfx::Font& default_font() override; @@ -27,6 +27,7 @@ private: Vector m_generic_font_names; RefPtr m_default_font; RefPtr m_default_fixed_width_font; + bool m_is_layout_test_mode { false }; }; } diff --git a/Ladybird/WebContent/main.cpp b/Ladybird/WebContent/main.cpp index f0a044cc209..6ab5eec969f 100644 --- a/Ladybird/WebContent/main.cpp +++ b/Ladybird/WebContent/main.cpp @@ -63,14 +63,16 @@ ErrorOr serenity_main(Main::Arguments arguments) Web::FrameLoader::set_default_favicon_path(DeprecatedString::formatted("{}/res/icons/16x16/app-browser.png", s_serenity_resource_root)); int webcontent_fd_passing_socket { -1 }; + bool is_layout_test_mode = false; Core::ArgsParser args_parser; args_parser.add_option(webcontent_fd_passing_socket, "File descriptor of the passing socket for the WebContent connection", "webcontent-fd-passing-socket", 'c', "webcontent_fd_passing_socket"); + args_parser.add_option(is_layout_test_mode, "Is layout test mode", "layout-test-mode", 0); args_parser.parse(arguments); VERIFY(webcontent_fd_passing_socket >= 0); - Web::Platform::FontPlugin::install(*new Ladybird::FontPluginQt); + Web::Platform::FontPlugin::install(*new Ladybird::FontPluginQt(is_layout_test_mode)); Web::FrameLoader::set_error_page_url(DeprecatedString::formatted("file://{}/res/html/error.html", s_serenity_resource_root)); diff --git a/Userland/Libraries/LibWebView/ViewImplementation.cpp b/Userland/Libraries/LibWebView/ViewImplementation.cpp index 7ad00295f4d..7c61b584f51 100644 --- a/Userland/Libraries/LibWebView/ViewImplementation.cpp +++ b/Userland/Libraries/LibWebView/ViewImplementation.cpp @@ -126,7 +126,7 @@ void ViewImplementation::run_javascript(StringView js_source) #if !defined(AK_OS_SERENITY) -ErrorOr> ViewImplementation::launch_web_content_process(ReadonlySpan candidate_web_content_paths, EnableCallgrindProfiling enable_callgrind_profiling) +ErrorOr> ViewImplementation::launch_web_content_process(ReadonlySpan candidate_web_content_paths, EnableCallgrindProfiling enable_callgrind_profiling, IsLayoutTestMode is_layout_test_mode) { int socket_fds[2] {}; TRY(Core::System::socketpair(AF_LOCAL, SOCK_STREAM, 0, socket_fds)); @@ -162,6 +162,8 @@ ErrorOr> ViewImplementation::launch_web }; if (enable_callgrind_profiling == EnableCallgrindProfiling::No) arguments.remove(0, callgrind_prefix_length); + if (is_layout_test_mode == IsLayoutTestMode::Yes) + arguments.append("--layout-test-mode"sv); result = Core::System::exec(arguments[0], arguments.span(), Core::System::SearchInPath::Yes); if (!result.is_error()) diff --git a/Userland/Libraries/LibWebView/ViewImplementation.h b/Userland/Libraries/LibWebView/ViewImplementation.h index a7342a3cb83..872bf77eb19 100644 --- a/Userland/Libraries/LibWebView/ViewImplementation.h +++ b/Userland/Libraries/LibWebView/ViewImplementation.h @@ -24,6 +24,11 @@ enum class EnableCallgrindProfiling { Yes }; +enum class IsLayoutTestMode { + No, + Yes +}; + class ViewImplementation { public: virtual ~ViewImplementation() { } @@ -131,7 +136,7 @@ protected: virtual void create_client(EnableCallgrindProfiling = EnableCallgrindProfiling::No) {}; #if !defined(AK_OS_SERENITY) - ErrorOr> launch_web_content_process(ReadonlySpan candidate_web_content_paths, EnableCallgrindProfiling = EnableCallgrindProfiling::No); + ErrorOr> launch_web_content_process(ReadonlySpan candidate_web_content_paths, EnableCallgrindProfiling = EnableCallgrindProfiling::No, IsLayoutTestMode = IsLayoutTestMode::No); #endif struct SharedBitmap { diff --git a/Userland/Utilities/headless-browser.cpp b/Userland/Utilities/headless-browser.cpp index c63fdfdb8b1..e83f5de5f2d 100644 --- a/Userland/Utilities/headless-browser.cpp +++ b/Userland/Utilities/headless-browser.cpp @@ -44,15 +44,16 @@ class HeadlessWebContentView final : public WebView::ViewImplementation { public: - static ErrorOr> create(Core::AnonymousBuffer theme, Gfx::IntSize const& window_size, StringView web_driver_ipc_path) + static ErrorOr> create(Core::AnonymousBuffer theme, Gfx::IntSize const& window_size, StringView web_driver_ipc_path, WebView::IsLayoutTestMode is_layout_test_mode = WebView::IsLayoutTestMode::No) { auto view = TRY(adopt_nonnull_own_or_enomem(new (nothrow) HeadlessWebContentView())); #if defined(AK_OS_SERENITY) view->m_client_state.client = TRY(WebView::WebContentClient::try_create(*view)); + (void)is_layout_test_mode; #else auto candidate_web_content_paths = TRY(get_paths_for_helper_process("WebContent"sv)); - view->m_client_state.client = TRY(view->launch_web_content_process(candidate_web_content_paths)); + view->m_client_state.client = TRY(view->launch_web_content_process(candidate_web_content_paths, WebView::EnableCallgrindProfiling::No, is_layout_test_mode)); #endif view->client().async_update_system_theme(move(theme)); @@ -209,6 +210,7 @@ ErrorOr serenity_main(Main::Arguments arguments) auto resources_folder = "/res"sv; StringView web_driver_ipc_path; bool dump_layout_tree = false; + bool is_layout_test_mode = false; Core::ArgsParser args_parser; args_parser.set_general_help("This utility runs the Browser in headless mode."); @@ -216,6 +218,7 @@ ErrorOr serenity_main(Main::Arguments arguments) args_parser.add_option(dump_layout_tree, "Dump layout tree and exit", "dump-layout-tree", 'd'); args_parser.add_option(resources_folder, "Path of the base resources folder (defaults to /res)", "resources", 'r', "resources-root-path"); args_parser.add_option(web_driver_ipc_path, "Path to the WebDriver IPC socket", "webdriver-ipc-path", 0, "path"); + args_parser.add_option(is_layout_test_mode, "Enable layout test mode", "layout-test-mode", 0); args_parser.add_positional_argument(url, "URL to open", "url", Core::ArgsParser::Required::Yes); args_parser.parse(arguments); @@ -232,7 +235,7 @@ ErrorOr serenity_main(Main::Arguments arguments) // FIXME: Allow passing the window size as an argument. static constexpr Gfx::IntSize window_size { 800, 600 }; - auto view = TRY(HeadlessWebContentView::create(move(theme), window_size, web_driver_ipc_path)); + auto view = TRY(HeadlessWebContentView::create(move(theme), window_size, web_driver_ipc_path, is_layout_test_mode ? WebView::IsLayoutTestMode::Yes : WebView::IsLayoutTestMode::No)); RefPtr timer; if (dump_layout_tree) {