Browse Source

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.
martinfalisse 2 years ago
parent
commit
c719a542c5

+ 7 - 1
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<size_t>(Web::Platform::GenericFont::__Count));
 
     auto update_mapping = [&](Web::Platform::GenericFont generic_font, QFont::StyleHint qfont_style_hint, ReadonlySpan<DeprecatedString> fallbacks) {
+        if (m_is_layout_test_mode) {
+            m_generic_font_names[static_cast<size_t>(generic_font)] = "SerenitySans";
+            return;
+        }
+
         QFont qt_font;
         qt_font.setStyleHint(qfont_style_hint);
 

+ 2 - 1
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<DeprecatedString> m_generic_font_names;
     RefPtr<Gfx::Font> m_default_font;
     RefPtr<Gfx::Font> m_default_fixed_width_font;
+    bool m_is_layout_test_mode { false };
 };
 
 }

+ 3 - 1
Ladybird/WebContent/main.cpp

@@ -63,14 +63,16 @@ ErrorOr<int> 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));
 

+ 3 - 1
Userland/Libraries/LibWebView/ViewImplementation.cpp

@@ -126,7 +126,7 @@ void ViewImplementation::run_javascript(StringView js_source)
 
 #if !defined(AK_OS_SERENITY)
 
-ErrorOr<NonnullRefPtr<WebView::WebContentClient>> ViewImplementation::launch_web_content_process(ReadonlySpan<String> candidate_web_content_paths, EnableCallgrindProfiling enable_callgrind_profiling)
+ErrorOr<NonnullRefPtr<WebView::WebContentClient>> ViewImplementation::launch_web_content_process(ReadonlySpan<String> 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<NonnullRefPtr<WebView::WebContentClient>> 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())

+ 6 - 1
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<NonnullRefPtr<WebView::WebContentClient>> launch_web_content_process(ReadonlySpan<String> candidate_web_content_paths, EnableCallgrindProfiling = EnableCallgrindProfiling::No);
+    ErrorOr<NonnullRefPtr<WebView::WebContentClient>> launch_web_content_process(ReadonlySpan<String> candidate_web_content_paths, EnableCallgrindProfiling = EnableCallgrindProfiling::No, IsLayoutTestMode = IsLayoutTestMode::No);
 #endif
 
     struct SharedBitmap {

+ 6 - 3
Userland/Utilities/headless-browser.cpp

@@ -44,15 +44,16 @@
 
 class HeadlessWebContentView final : public WebView::ViewImplementation {
 public:
-    static ErrorOr<NonnullOwnPtr<HeadlessWebContentView>> create(Core::AnonymousBuffer theme, Gfx::IntSize const& window_size, StringView web_driver_ipc_path)
+    static ErrorOr<NonnullOwnPtr<HeadlessWebContentView>> 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<int> 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<int> 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<int> 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<Core::Timer> timer;
 
     if (dump_layout_tree) {