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