2022-04-03 16:15:36 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Dex♪ <dexes.ttp@gmail.com>
|
2024-10-10 15:05:10 +00:00
|
|
|
* Copyright (c) 2023-2024, Tim Flynn <trflynn89@ladybird.org>
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2023, Andreas Kling <andreas@ladybird.org>
|
2024-10-01 10:49:14 +00:00
|
|
|
* Copyright (c) 2023-2024, Sam Atkins <sam@ladybird.org>
|
2022-04-03 16:15:36 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
#include <AK/ByteString.h>
|
2022-07-18 18:23:19 +00:00
|
|
|
#include <AK/LexicalPath.h>
|
2023-04-24 11:16:37 +00:00
|
|
|
#include <AK/Platform.h>
|
2023-03-13 11:50:26 +00:00
|
|
|
#include <AK/String.h>
|
2022-04-03 16:15:36 +00:00
|
|
|
#include <LibCore/EventLoop.h>
|
2023-03-13 11:21:49 +00:00
|
|
|
#include <LibCore/File.h>
|
2023-12-22 14:06:03 +00:00
|
|
|
#include <LibCore/Promise.h>
|
2023-10-03 21:42:10 +00:00
|
|
|
#include <LibCore/ResourceImplementationFile.h>
|
2022-04-03 16:15:36 +00:00
|
|
|
#include <LibCore/Timer.h>
|
2023-03-21 15:35:30 +00:00
|
|
|
#include <LibFileSystem/FileSystem.h>
|
2022-04-03 16:15:36 +00:00
|
|
|
#include <LibGfx/Bitmap.h>
|
2023-03-21 18:58:06 +00:00
|
|
|
#include <LibGfx/ImageFormats/PNGWriter.h>
|
2023-03-12 16:38:15 +00:00
|
|
|
#include <LibGfx/SystemTheme.h>
|
2024-03-18 03:22:27 +00:00
|
|
|
#include <LibURL/URL.h>
|
2024-11-10 15:26:07 +00:00
|
|
|
#include <LibWebView/Utilities.h>
|
2024-11-09 17:50:33 +00:00
|
|
|
#include <UI/Headless/Application.h>
|
|
|
|
#include <UI/Headless/HeadlessWebView.h>
|
|
|
|
#include <UI/Headless/Test.h>
|
2022-11-22 01:03:45 +00:00
|
|
|
|
2024-10-10 15:56:27 +00:00
|
|
|
static ErrorOr<NonnullRefPtr<Core::Timer>> load_page_for_screenshot_and_exit(Core::EventLoop& event_loop, Ladybird::HeadlessWebView& view, URL::URL const& url, int screenshot_timeout)
|
2022-11-22 01:03:45 +00:00
|
|
|
{
|
2023-03-12 16:38:15 +00:00
|
|
|
// FIXME: Allow passing the output path as an argument.
|
|
|
|
static constexpr auto output_file_path = "output.png"sv;
|
2022-11-22 01:03:45 +00:00
|
|
|
|
2023-03-21 15:35:30 +00:00
|
|
|
if (FileSystem::exists(output_file_path))
|
|
|
|
TRY(FileSystem::remove(output_file_path, FileSystem::RecursionMode::Disallowed));
|
2022-11-22 01:03:45 +00:00
|
|
|
|
2023-03-12 16:38:15 +00:00
|
|
|
outln("Taking screenshot after {} seconds", screenshot_timeout);
|
2022-11-22 01:03:45 +00:00
|
|
|
|
2024-04-16 18:34:01 +00:00
|
|
|
auto timer = Core::Timer::create_single_shot(
|
2023-03-12 16:38:15 +00:00
|
|
|
screenshot_timeout * 1000,
|
|
|
|
[&]() {
|
2024-10-04 15:08:14 +00:00
|
|
|
auto promise = view.take_screenshot();
|
|
|
|
|
|
|
|
if (auto screenshot = MUST(promise->await())) {
|
2023-03-12 16:38:15 +00:00
|
|
|
outln("Saving screenshot to {}", output_file_path);
|
2022-11-22 01:03:45 +00:00
|
|
|
|
2023-03-12 16:38:15 +00:00
|
|
|
auto output_file = MUST(Core::File::open(output_file_path, Core::File::OpenMode::Write));
|
|
|
|
auto image_buffer = MUST(Gfx::PNGWriter::encode(*screenshot));
|
2023-03-01 16:24:50 +00:00
|
|
|
MUST(output_file->write_until_depleted(image_buffer.bytes()));
|
2023-03-12 16:38:15 +00:00
|
|
|
} else {
|
|
|
|
warnln("No screenshot available");
|
|
|
|
}
|
2022-11-22 01:03:45 +00:00
|
|
|
|
2023-03-12 16:38:15 +00:00
|
|
|
event_loop.quit(0);
|
2024-04-16 18:34:01 +00:00
|
|
|
});
|
2022-11-22 01:03:45 +00:00
|
|
|
|
2024-03-27 17:32:27 +00:00
|
|
|
view.load(url);
|
2022-11-22 01:03:45 +00:00
|
|
|
timer->start();
|
2023-03-12 16:38:15 +00:00
|
|
|
return timer;
|
2022-11-22 01:03:45 +00:00
|
|
|
}
|
|
|
|
|
2024-07-30 18:01:05 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|
|
|
{
|
2024-11-10 15:26:07 +00:00
|
|
|
WebView::platform_init();
|
2024-07-30 18:01:05 +00:00
|
|
|
|
2024-10-10 15:56:27 +00:00
|
|
|
auto app = Ladybird::Application::create(arguments, "about:newtab"sv);
|
2024-10-04 14:20:22 +00:00
|
|
|
TRY(app->launch_services());
|
2024-07-30 18:01:05 +00:00
|
|
|
|
|
|
|
Core::ResourceImplementation::install(make<Core::ResourceImplementationFile>(MUST(String::from_byte_string(app->resources_folder))));
|
|
|
|
|
|
|
|
auto theme_path = LexicalPath::join(app->resources_folder, "themes"sv, "Default.ini"sv);
|
2023-03-12 16:38:15 +00:00
|
|
|
auto theme = TRY(Gfx::load_system_theme(theme_path.string()));
|
2022-04-03 16:15:36 +00:00
|
|
|
|
2023-03-12 16:38:15 +00:00
|
|
|
// FIXME: Allow passing the window size as an argument.
|
2024-11-12 15:36:53 +00:00
|
|
|
static constexpr Web::DevicePixelSize window_size { 800, 600 };
|
2022-04-03 16:15:36 +00:00
|
|
|
|
2024-07-30 18:01:05 +00:00
|
|
|
if (!app->test_root_path.is_empty()) {
|
2024-10-04 14:20:22 +00:00
|
|
|
app->test_root_path = LexicalPath::absolute_path(TRY(FileSystem::current_working_directory()), app->test_root_path);
|
2024-11-05 23:44:19 +00:00
|
|
|
TRY(app->launch_test_fixtures());
|
2024-10-10 15:56:27 +00:00
|
|
|
TRY(Ladybird::run_tests(theme, window_size));
|
|
|
|
|
|
|
|
return 0;
|
2023-05-27 17:46:17 +00:00
|
|
|
}
|
|
|
|
|
2024-10-21 22:54:25 +00:00
|
|
|
auto& view = app->create_web_view(move(theme), window_size);
|
2024-10-01 10:33:21 +00:00
|
|
|
|
2024-07-30 18:01:05 +00:00
|
|
|
VERIFY(!WebView::Application::chrome_options().urls.is_empty());
|
|
|
|
auto const& url = WebView::Application::chrome_options().urls.first();
|
|
|
|
if (!url.is_valid()) {
|
|
|
|
warnln("Invalid URL: \"{}\"", url);
|
2023-10-30 17:27:53 +00:00
|
|
|
return Error::from_string_literal("Invalid URL");
|
|
|
|
}
|
|
|
|
|
2024-10-04 15:16:29 +00:00
|
|
|
if (app->dump_layout_tree || app->dump_text) {
|
2024-10-10 15:56:27 +00:00
|
|
|
Ladybird::Test test { app->dump_layout_tree ? Ladybird::TestMode::Layout : Ladybird::TestMode::Text };
|
2024-10-26 16:22:18 +00:00
|
|
|
Ladybird::run_dump_test(view, test, url, app->per_test_timeout_in_seconds * 1000);
|
2023-12-24 11:28:49 +00:00
|
|
|
|
2024-10-04 15:16:29 +00:00
|
|
|
auto completion = MUST(view.test_promise().await());
|
2024-10-10 15:56:27 +00:00
|
|
|
return completion.result == Ladybird::TestResult::Pass ? 0 : 1;
|
2023-12-24 11:28:49 +00:00
|
|
|
}
|
|
|
|
|
2024-10-21 22:52:12 +00:00
|
|
|
RefPtr<Core::Timer> timer;
|
|
|
|
if (!WebView::Application::chrome_options().webdriver_content_ipc_path.has_value())
|
|
|
|
timer = TRY(load_page_for_screenshot_and_exit(Core::EventLoop::current(), view, url, app->screenshot_timeout));
|
2023-12-24 11:28:49 +00:00
|
|
|
|
2024-10-21 22:52:12 +00:00
|
|
|
return app->execute();
|
2022-04-03 16:15:36 +00:00
|
|
|
}
|