Ladybird: Add ability to add test fixtures for LibWeb tests

This will be used to add test fixtures that use external
resources or require a specific setup.
This commit is contained in:
Andrew Kaster 2024-11-05 16:44:19 -07:00 committed by Andrew Kaster
parent 97d47a38c9
commit ba78b294a0
Notes: github-actions[bot] 2024-11-09 20:31:06 +00:00
6 changed files with 98 additions and 0 deletions

View file

@ -5,6 +5,7 @@
*/
#include <Ladybird/Headless/Application.h>
#include <Ladybird/Headless/Fixture.h>
#include <Ladybird/Headless/HeadlessWebView.h>
#include <Ladybird/HelperProcess.h>
#include <Ladybird/Utilities.h>
@ -20,6 +21,12 @@ Application::Application(Badge<WebView::Application>, Main::Arguments&)
{
}
Application::~Application()
{
for (auto& fixture : Fixture::all())
fixture->teardown();
}
void Application::create_platform_arguments(Core::ArgsParser& args_parser)
{
args_parser.add_option(screenshot_timeout, "Take a screenshot after [n] seconds (default: 1)", "screenshot", 's', "n");
@ -72,6 +79,19 @@ ErrorOr<void> Application::launch_services()
return {};
}
ErrorOr<void> Application::launch_test_fixtures()
{
Fixture::initialize_fixtures();
// FIXME: Add option to only run specific fixtures from command line by name
// And an option to not run any fixtures at all
for (auto& fixture : Fixture::all()) {
if (auto result = fixture->setup(); result.is_error())
return result;
}
return {};
}
HeadlessWebView& Application::create_web_view(Core::AnonymousBuffer theme, Gfx::IntSize window_size)
{
auto web_view = HeadlessWebView::create(move(theme), window_size);

View file

@ -24,6 +24,8 @@ class Application : public WebView::Application {
WEB_VIEW_APPLICATION(Application)
public:
~Application();
static Application& the()
{
return static_cast<Application&>(WebView::Application::the());
@ -33,6 +35,7 @@ public:
virtual void create_platform_options(WebView::ChromeOptions&, WebView::WebContentOptions&) override;
ErrorOr<void> launch_services();
ErrorOr<void> launch_test_fixtures();
static Requests::RequestClient& request_client() { return *the().m_request_client; }
static ImageDecoderClient::Client& image_decoder_client() { return *the().m_image_decoder_client; }

View file

@ -1,6 +1,7 @@
set(SOURCES
${LADYBIRD_SOURCES}
Application.cpp
Fixture.cpp
HeadlessWebView.cpp
Test.cpp
main.cpp

View file

@ -0,0 +1,33 @@
/*
* Copyright (c) 2024, Andrew Kaster <andrew@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Ladybird/Headless/Fixture.h>
namespace Ladybird {
// Key function for Fixture
Fixture::~Fixture() = default;
Optional<Fixture&> Fixture::lookup(StringView name)
{
for (auto& fixture : all()) {
if (fixture->name() == name)
return *fixture;
}
return {};
}
Vector<NonnullOwnPtr<Fixture>>& Fixture::all()
{
static Vector<NonnullOwnPtr<Fixture>> fixtures;
return fixtures;
}
void Fixture::initialize_fixtures()
{
}
}

View file

@ -0,0 +1,40 @@
/*
* Copyright (c) 2024, Andrew Kaster <andrew@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Error.h>
#include <AK/NonnullOwnPtr.h>
#include <AK/Optional.h>
#include <AK/StringView.h>
#include <AK/Vector.h>
namespace Ladybird {
class Fixture {
public:
virtual ~Fixture();
virtual ErrorOr<void> setup() = 0;
void teardown()
{
if (is_running())
teardown_impl();
}
virtual StringView name() const = 0;
virtual bool is_running() const { return false; }
static void initialize_fixtures();
static Optional<Fixture&> lookup(StringView name);
static Vector<NonnullOwnPtr<Fixture>>& all();
protected:
virtual void teardown_impl() = 0;
};
}

View file

@ -76,6 +76,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
if (!app->test_root_path.is_empty()) {
app->test_root_path = LexicalPath::absolute_path(TRY(FileSystem::current_working_directory()), app->test_root_path);
TRY(app->launch_test_fixtures());
TRY(Ladybird::run_tests(theme, window_size));
return 0;