headless-browser.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. /*
  2. * Copyright (c) 2022, Dex♪ <dexes.ttp@gmail.com>
  3. * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  4. * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
  5. * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include <AK/Badge.h>
  10. #include <AK/ByteBuffer.h>
  11. #include <AK/ByteString.h>
  12. #include <AK/Function.h>
  13. #include <AK/JsonObject.h>
  14. #include <AK/JsonParser.h>
  15. #include <AK/LexicalPath.h>
  16. #include <AK/NonnullOwnPtr.h>
  17. #include <AK/Platform.h>
  18. #include <AK/String.h>
  19. #include <AK/Vector.h>
  20. #include <Ladybird/Types.h>
  21. #include <LibCore/ArgsParser.h>
  22. #include <LibCore/ConfigFile.h>
  23. #include <LibCore/DirIterator.h>
  24. #include <LibCore/Directory.h>
  25. #include <LibCore/EventLoop.h>
  26. #include <LibCore/File.h>
  27. #include <LibCore/Promise.h>
  28. #include <LibCore/ResourceImplementationFile.h>
  29. #include <LibCore/Timer.h>
  30. #include <LibDiff/Format.h>
  31. #include <LibDiff/Generator.h>
  32. #include <LibFileSystem/FileSystem.h>
  33. #include <LibGfx/Bitmap.h>
  34. #include <LibGfx/Font/FontDatabase.h>
  35. #include <LibGfx/ImageFormats/PNGWriter.h>
  36. #include <LibGfx/Point.h>
  37. #include <LibGfx/Rect.h>
  38. #include <LibGfx/ShareableBitmap.h>
  39. #include <LibGfx/Size.h>
  40. #include <LibGfx/StandardCursor.h>
  41. #include <LibGfx/SystemTheme.h>
  42. #include <LibIPC/File.h>
  43. #include <LibProtocol/RequestClient.h>
  44. #include <LibURL/URL.h>
  45. #include <LibWeb/Cookie/Cookie.h>
  46. #include <LibWeb/Cookie/ParsedCookie.h>
  47. #include <LibWeb/HTML/ActivateTab.h>
  48. #include <LibWeb/HTML/SelectedFile.h>
  49. #include <LibWeb/Worker/WebWorkerClient.h>
  50. #include <LibWebView/CookieJar.h>
  51. #include <LibWebView/Database.h>
  52. #include <LibWebView/URL.h>
  53. #include <LibWebView/ViewImplementation.h>
  54. #include <LibWebView/WebContentClient.h>
  55. #if !defined(AK_OS_SERENITY)
  56. # include <Ladybird/HelperProcess.h>
  57. # include <Ladybird/Utilities.h>
  58. #endif
  59. constexpr int DEFAULT_TIMEOUT_MS = 30000; // 30sec
  60. static StringView s_current_test_path;
  61. class HeadlessWebContentView final : public WebView::ViewImplementation {
  62. public:
  63. static ErrorOr<NonnullOwnPtr<HeadlessWebContentView>> create(Core::AnonymousBuffer theme, Gfx::IntSize const& window_size, String const& command_line, StringView web_driver_ipc_path, Ladybird::IsLayoutTestMode is_layout_test_mode = Ladybird::IsLayoutTestMode::No, Vector<ByteString> const& certificates = {}, StringView resources_folder = {})
  64. {
  65. RefPtr<Protocol::RequestClient> request_client;
  66. #if defined(AK_OS_SERENITY)
  67. auto database = TRY(WebView::Database::create());
  68. (void)resources_folder;
  69. (void)certificates;
  70. #else
  71. auto sql_server_paths = TRY(get_paths_for_helper_process("SQLServer"sv));
  72. auto sql_client = TRY(launch_sql_server_process(sql_server_paths));
  73. auto database = TRY(WebView::Database::create(move(sql_client)));
  74. auto request_server_paths = TRY(get_paths_for_helper_process("RequestServer"sv));
  75. request_client = TRY(launch_request_server_process(request_server_paths, resources_folder, certificates));
  76. #endif
  77. auto cookie_jar = TRY(WebView::CookieJar::create(*database));
  78. auto view = TRY(adopt_nonnull_own_or_enomem(new (nothrow) HeadlessWebContentView(move(database), move(cookie_jar), request_client)));
  79. #if defined(AK_OS_SERENITY)
  80. view->m_client_state.client = TRY(WebView::WebContentClient::try_create(*view));
  81. (void)command_line;
  82. (void)is_layout_test_mode;
  83. #else
  84. Ladybird::WebContentOptions web_content_options {
  85. .command_line = command_line,
  86. .executable_path = MUST(String::from_byte_string(MUST(Core::System::current_executable_path()))),
  87. .is_layout_test_mode = is_layout_test_mode,
  88. };
  89. auto request_server_socket = TRY(connect_new_request_server_client(*request_client));
  90. auto candidate_web_content_paths = TRY(get_paths_for_helper_process("WebContent"sv));
  91. view->m_client_state.client = TRY(launch_web_content_process(*view, candidate_web_content_paths, web_content_options, move(request_server_socket)));
  92. #endif
  93. view->client().async_update_system_theme(0, move(theme));
  94. view->client().async_update_system_fonts(0, Gfx::FontDatabase::default_font_query(), Gfx::FontDatabase::fixed_width_font_query(), Gfx::FontDatabase::window_title_font_query());
  95. view->m_viewport_rect = { { 0, 0 }, window_size };
  96. view->client().async_set_viewport_rect(0, view->m_viewport_rect.to_type<Web::DevicePixels>());
  97. view->client().async_set_window_size(0, window_size.to_type<Web::DevicePixels>());
  98. if (!web_driver_ipc_path.is_empty())
  99. view->client().async_connect_to_webdriver(0, web_driver_ipc_path);
  100. view->m_client_state.client->on_web_content_process_crash = [] {
  101. warnln("\033[31;1mWebContent Crashed!!\033[0m");
  102. if (!s_current_test_path.is_empty()) {
  103. warnln(" Last started test: {}", s_current_test_path);
  104. }
  105. VERIFY_NOT_REACHED();
  106. };
  107. return view;
  108. }
  109. RefPtr<Gfx::Bitmap> take_screenshot()
  110. {
  111. VERIFY(!m_pending_screenshot);
  112. m_pending_screenshot = Core::Promise<RefPtr<Gfx::Bitmap>>::construct();
  113. client().async_take_document_screenshot(0);
  114. auto screenshot = MUST(m_pending_screenshot->await());
  115. m_pending_screenshot = nullptr;
  116. return screenshot;
  117. }
  118. virtual void did_receive_screenshot(Badge<WebView::WebContentClient>, Gfx::ShareableBitmap const& screenshot) override
  119. {
  120. VERIFY(m_pending_screenshot);
  121. m_pending_screenshot->resolve(screenshot.bitmap());
  122. }
  123. ErrorOr<String> dump_layout_tree()
  124. {
  125. return String::from_byte_string(client().dump_layout_tree(0));
  126. }
  127. ErrorOr<String> dump_paint_tree()
  128. {
  129. return String::from_byte_string(client().dump_paint_tree(0));
  130. }
  131. ErrorOr<String> dump_text()
  132. {
  133. return String::from_byte_string(client().dump_text(0));
  134. }
  135. void clear_content_filters()
  136. {
  137. client().async_set_content_filters(0, {});
  138. }
  139. private:
  140. HeadlessWebContentView(NonnullRefPtr<WebView::Database> database, WebView::CookieJar cookie_jar, RefPtr<Protocol::RequestClient> request_client = nullptr)
  141. : m_database(move(database))
  142. , m_cookie_jar(move(cookie_jar))
  143. , m_request_client(move(request_client))
  144. {
  145. on_scroll_to_point = [this](auto position) {
  146. m_viewport_rect.set_location(position);
  147. client().async_set_viewport_rect(0, m_viewport_rect.to_type<Web::DevicePixels>());
  148. };
  149. on_scroll_by_delta = [this](auto x_delta, auto y_delta) {
  150. auto position = m_viewport_rect.location();
  151. position.set_x(position.x() + x_delta);
  152. position.set_y(position.y() + y_delta);
  153. if (on_scroll_to_point)
  154. on_scroll_to_point(position);
  155. };
  156. on_get_cookie = [this](auto const& url, auto source) {
  157. return m_cookie_jar.get_cookie(url, source);
  158. };
  159. on_set_cookie = [this](auto const& url, auto const& cookie, auto source) {
  160. m_cookie_jar.set_cookie(url, cookie, source);
  161. };
  162. on_request_worker_agent = [this]() {
  163. #if defined(AK_OS_SERENITY)
  164. auto worker_client = MUST(Web::HTML::WebWorkerClient::try_create());
  165. (void)this;
  166. #else
  167. auto worker_client = MUST(launch_web_worker_process(MUST(get_paths_for_helper_process("WebWorker"sv)), *m_request_client));
  168. #endif
  169. return worker_client->dup_socket();
  170. };
  171. }
  172. void update_zoom() override { }
  173. void initialize_client(CreateNewClient) override { }
  174. virtual Web::DevicePixelRect viewport_rect() const override { return m_viewport_rect.to_type<Web::DevicePixels>(); }
  175. virtual Gfx::IntPoint to_content_position(Gfx::IntPoint widget_position) const override { return widget_position; }
  176. virtual Gfx::IntPoint to_widget_position(Gfx::IntPoint content_position) const override { return content_position; }
  177. private:
  178. Gfx::IntRect m_viewport_rect;
  179. RefPtr<Core::Promise<RefPtr<Gfx::Bitmap>>> m_pending_screenshot;
  180. NonnullRefPtr<WebView::Database> m_database;
  181. WebView::CookieJar m_cookie_jar;
  182. RefPtr<Protocol::RequestClient> m_request_client;
  183. };
  184. static ErrorOr<NonnullRefPtr<Core::Timer>> load_page_for_screenshot_and_exit(Core::EventLoop& event_loop, HeadlessWebContentView& view, URL::URL url, int screenshot_timeout)
  185. {
  186. // FIXME: Allow passing the output path as an argument.
  187. static constexpr auto output_file_path = "output.png"sv;
  188. if (FileSystem::exists(output_file_path))
  189. TRY(FileSystem::remove(output_file_path, FileSystem::RecursionMode::Disallowed));
  190. outln("Taking screenshot after {} seconds", screenshot_timeout);
  191. auto timer = Core::Timer::create_single_shot(
  192. screenshot_timeout * 1000,
  193. [&]() {
  194. if (auto screenshot = view.take_screenshot()) {
  195. outln("Saving screenshot to {}", output_file_path);
  196. auto output_file = MUST(Core::File::open(output_file_path, Core::File::OpenMode::Write));
  197. auto image_buffer = MUST(Gfx::PNGWriter::encode(*screenshot));
  198. MUST(output_file->write_until_depleted(image_buffer.bytes()));
  199. } else {
  200. warnln("No screenshot available");
  201. }
  202. event_loop.quit(0);
  203. });
  204. view.load(url);
  205. timer->start();
  206. return timer;
  207. }
  208. enum class TestMode {
  209. Layout,
  210. Text,
  211. Ref,
  212. };
  213. enum class TestResult {
  214. Pass,
  215. Fail,
  216. Skipped,
  217. Timeout,
  218. };
  219. static StringView test_result_to_string(TestResult result)
  220. {
  221. switch (result) {
  222. case TestResult::Pass:
  223. return "Pass"sv;
  224. case TestResult::Fail:
  225. return "Fail"sv;
  226. case TestResult::Skipped:
  227. return "Skipped"sv;
  228. case TestResult::Timeout:
  229. return "Timeout"sv;
  230. }
  231. VERIFY_NOT_REACHED();
  232. }
  233. static ErrorOr<TestResult> run_dump_test(HeadlessWebContentView& view, StringView input_path, StringView expectation_path, TestMode mode, int timeout_in_milliseconds = DEFAULT_TIMEOUT_MS)
  234. {
  235. Core::EventLoop loop;
  236. bool did_timeout = false;
  237. auto timeout_timer = Core::Timer::create_single_shot(timeout_in_milliseconds, [&] {
  238. did_timeout = true;
  239. loop.quit(0);
  240. });
  241. auto url = URL::create_with_file_scheme(TRY(FileSystem::real_path(input_path)));
  242. String result;
  243. auto did_finish_test = false;
  244. auto did_finish_loading = false;
  245. if (mode == TestMode::Layout) {
  246. view.on_load_finish = [&](auto const& loaded_url) {
  247. // This callback will be called for 'about:blank' first, then for the URL we actually want to dump
  248. VERIFY(url.equals(loaded_url, URL::ExcludeFragment::Yes) || loaded_url.equals(URL::URL("about:blank")));
  249. if (url.equals(loaded_url, URL::ExcludeFragment::Yes)) {
  250. // NOTE: We take a screenshot here to force the lazy layout of SVG-as-image documents to happen.
  251. // It also causes a lot more code to run, which is good for finding bugs. :^)
  252. (void)view.take_screenshot();
  253. StringBuilder builder;
  254. builder.append(view.dump_layout_tree().release_value_but_fixme_should_propagate_errors());
  255. builder.append("\n"sv);
  256. builder.append(view.dump_paint_tree().release_value_but_fixme_should_propagate_errors());
  257. result = builder.to_string().release_value_but_fixme_should_propagate_errors();
  258. loop.quit(0);
  259. }
  260. };
  261. view.on_text_test_finish = {};
  262. } else if (mode == TestMode::Text) {
  263. view.on_load_finish = [&](auto const& loaded_url) {
  264. // NOTE: We don't want subframe loads to trigger the test finish.
  265. if (!url.equals(loaded_url, URL::ExcludeFragment::Yes))
  266. return;
  267. did_finish_loading = true;
  268. if (did_finish_test)
  269. loop.quit(0);
  270. };
  271. view.on_text_test_finish = [&]() {
  272. result = view.dump_text().release_value_but_fixme_should_propagate_errors();
  273. did_finish_test = true;
  274. if (did_finish_loading)
  275. loop.quit(0);
  276. };
  277. }
  278. view.load(url);
  279. timeout_timer->start();
  280. loop.exec();
  281. if (did_timeout)
  282. return TestResult::Timeout;
  283. if (expectation_path.is_empty()) {
  284. out("{}", result);
  285. return TestResult::Skipped;
  286. }
  287. auto expectation_file_or_error = Core::File::open(expectation_path, Core::File::OpenMode::Read);
  288. if (expectation_file_or_error.is_error()) {
  289. warnln("Failed opening '{}': {}", expectation_path, expectation_file_or_error.error());
  290. return expectation_file_or_error.release_error();
  291. }
  292. auto expectation_file = expectation_file_or_error.release_value();
  293. auto expectation = TRY(String::from_utf8(StringView(TRY(expectation_file->read_until_eof()).bytes())));
  294. auto actual = result;
  295. auto actual_trimmed = TRY(actual.trim("\n"sv, TrimMode::Right));
  296. auto expectation_trimmed = TRY(expectation.trim("\n"sv, TrimMode::Right));
  297. if (actual_trimmed == expectation_trimmed)
  298. return TestResult::Pass;
  299. auto const color_output = isatty(STDOUT_FILENO) ? Diff::ColorOutput::Yes : Diff::ColorOutput::No;
  300. if (color_output == Diff::ColorOutput::Yes)
  301. outln("\n\033[33;1mTest failed\033[0m: {}", input_path);
  302. else
  303. outln("\nTest failed: {}", input_path);
  304. auto hunks = TRY(Diff::from_text(expectation, actual, 3));
  305. auto out = TRY(Core::File::standard_output());
  306. TRY(Diff::write_unified_header(expectation_path, expectation_path, *out));
  307. for (auto const& hunk : hunks)
  308. TRY(Diff::write_unified(hunk, *out, color_output));
  309. return TestResult::Fail;
  310. }
  311. static ErrorOr<TestResult> run_ref_test(HeadlessWebContentView& view, StringView input_path, bool dump_failed_ref_tests, int timeout_in_milliseconds = DEFAULT_TIMEOUT_MS)
  312. {
  313. Core::EventLoop loop;
  314. bool did_timeout = false;
  315. auto timeout_timer = Core::Timer::create_single_shot(timeout_in_milliseconds, [&] {
  316. did_timeout = true;
  317. loop.quit(0);
  318. });
  319. RefPtr<Gfx::Bitmap> actual_screenshot, expectation_screenshot;
  320. view.on_load_finish = [&](auto const&) {
  321. if (actual_screenshot) {
  322. expectation_screenshot = view.take_screenshot();
  323. loop.quit(0);
  324. } else {
  325. actual_screenshot = view.take_screenshot();
  326. view.debug_request("load-reference-page");
  327. }
  328. };
  329. view.on_text_test_finish = [&] {
  330. dbgln("Unexpected text test finished during ref test for {}", input_path);
  331. };
  332. view.load(URL::create_with_file_scheme(TRY(FileSystem::real_path(input_path))));
  333. timeout_timer->start();
  334. loop.exec();
  335. if (did_timeout)
  336. return TestResult::Timeout;
  337. VERIFY(actual_screenshot);
  338. VERIFY(expectation_screenshot);
  339. if (actual_screenshot->visually_equals(*expectation_screenshot))
  340. return TestResult::Pass;
  341. if (dump_failed_ref_tests) {
  342. warnln("\033[33;1mRef test {} failed; dumping screenshots\033[0m", input_path);
  343. auto title = LexicalPath::title(input_path);
  344. auto dump_screenshot = [&](Gfx::Bitmap& bitmap, StringView path) -> ErrorOr<void> {
  345. auto screenshot_file = TRY(Core::File::open(path, Core::File::OpenMode::Write));
  346. auto encoded_data = TRY(Gfx::PNGWriter::encode(bitmap));
  347. TRY(screenshot_file->write_until_depleted(encoded_data));
  348. warnln("\033[33;1mDumped {}\033[0m", TRY(FileSystem::real_path(path)));
  349. return {};
  350. };
  351. auto mkdir_result = Core::System::mkdir("test-dumps"sv, 0755);
  352. if (mkdir_result.is_error() && mkdir_result.error().code() != EEXIST)
  353. return mkdir_result.release_error();
  354. TRY(dump_screenshot(*actual_screenshot, TRY(String::formatted("test-dumps/{}.png", title))));
  355. TRY(dump_screenshot(*expectation_screenshot, TRY(String::formatted("test-dumps/{}-ref.png", title))));
  356. }
  357. return TestResult::Fail;
  358. }
  359. static ErrorOr<TestResult> run_test(HeadlessWebContentView& view, StringView input_path, StringView expectation_path, TestMode mode, bool dump_failed_ref_tests)
  360. {
  361. // Clear the current document.
  362. // FIXME: Implement a debug-request to do this more thoroughly.
  363. auto promise = Core::Promise<Empty>::construct();
  364. view.on_load_finish = [&](auto) {
  365. promise->resolve({});
  366. };
  367. view.on_text_test_finish = {};
  368. view.on_request_file_picker = [&](auto const& accepted_file_types, auto allow_multiple_files) {
  369. // Create some dummy files for tests.
  370. Vector<Web::HTML::SelectedFile> selected_files;
  371. bool add_txt_files = accepted_file_types.filters.is_empty();
  372. bool add_cpp_files = false;
  373. for (auto const& filter : accepted_file_types.filters) {
  374. filter.visit(
  375. [](Web::HTML::FileFilter::FileType) {},
  376. [&](Web::HTML::FileFilter::MimeType const& mime_type) {
  377. if (mime_type.value == "text/plain"sv)
  378. add_txt_files = true;
  379. },
  380. [&](Web::HTML::FileFilter::Extension const& extension) {
  381. if (extension.value == "cpp"sv)
  382. add_cpp_files = true;
  383. });
  384. }
  385. if (add_txt_files) {
  386. selected_files.empend("file1"sv, MUST(ByteBuffer::copy("Contents for file1"sv.bytes())));
  387. if (allow_multiple_files == Web::HTML::AllowMultipleFiles::Yes) {
  388. selected_files.empend("file2"sv, MUST(ByteBuffer::copy("Contents for file2"sv.bytes())));
  389. selected_files.empend("file3"sv, MUST(ByteBuffer::copy("Contents for file3"sv.bytes())));
  390. selected_files.empend("file4"sv, MUST(ByteBuffer::copy("Contents for file4"sv.bytes())));
  391. }
  392. }
  393. if (add_cpp_files) {
  394. selected_files.empend("file1.cpp"sv, MUST(ByteBuffer::copy("int main() {{ return 1; }}"sv.bytes())));
  395. if (allow_multiple_files == Web::HTML::AllowMultipleFiles::Yes) {
  396. selected_files.empend("file2.cpp"sv, MUST(ByteBuffer::copy("int main() {{ return 2; }}"sv.bytes())));
  397. }
  398. }
  399. view.file_picker_closed(move(selected_files));
  400. };
  401. view.load(URL::URL("about:blank"sv));
  402. MUST(promise->await());
  403. s_current_test_path = input_path;
  404. switch (mode) {
  405. case TestMode::Text:
  406. case TestMode::Layout:
  407. return run_dump_test(view, input_path, expectation_path, mode);
  408. case TestMode::Ref:
  409. return run_ref_test(view, input_path, dump_failed_ref_tests);
  410. default:
  411. VERIFY_NOT_REACHED();
  412. }
  413. }
  414. struct Test {
  415. String input_path;
  416. String expectation_path;
  417. TestMode mode;
  418. Optional<TestResult> result;
  419. };
  420. static Vector<ByteString> s_skipped_tests;
  421. static ErrorOr<void> load_test_config(StringView test_root_path)
  422. {
  423. auto config_path = LexicalPath::join(test_root_path, "TestConfig.ini"sv);
  424. auto config_or_error = Core::ConfigFile::open(config_path.string());
  425. if (config_or_error.is_error()) {
  426. if (config_or_error.error().code() == ENOENT)
  427. return {};
  428. dbgln("Unable to open test config {}", config_path);
  429. return config_or_error.release_error();
  430. }
  431. auto config = config_or_error.release_value();
  432. for (auto const& group : config->groups()) {
  433. if (group == "Skipped"sv) {
  434. for (auto& key : config->keys(group))
  435. s_skipped_tests.append(LexicalPath::join(test_root_path, key).string());
  436. } else {
  437. warnln("Unknown group '{}' in config {}", group, config_path);
  438. }
  439. }
  440. return {};
  441. }
  442. static ErrorOr<void> collect_dump_tests(Vector<Test>& tests, StringView path, StringView trail, TestMode mode)
  443. {
  444. Core::DirIterator it(TRY(String::formatted("{}/input/{}", path, trail)).to_byte_string(), Core::DirIterator::Flags::SkipDots);
  445. while (it.has_next()) {
  446. auto name = it.next_path();
  447. auto input_path = TRY(FileSystem::real_path(TRY(String::formatted("{}/input/{}/{}", path, trail, name))));
  448. if (FileSystem::is_directory(input_path)) {
  449. TRY(collect_dump_tests(tests, path, TRY(String::formatted("{}/{}", trail, name)), mode));
  450. continue;
  451. }
  452. if (!name.ends_with(".html"sv) && !name.ends_with(".svg"sv))
  453. continue;
  454. auto basename = LexicalPath::title(name);
  455. auto expectation_path = TRY(String::formatted("{}/expected/{}/{}.txt", path, trail, basename));
  456. // FIXME: Test paths should be ByteString
  457. tests.append({ TRY(String::from_byte_string(input_path)), move(expectation_path), mode, {} });
  458. }
  459. return {};
  460. }
  461. static ErrorOr<void> collect_ref_tests(Vector<Test>& tests, StringView path)
  462. {
  463. TRY(Core::Directory::for_each_entry(path, Core::DirIterator::SkipDots, [&](Core::DirectoryEntry const& entry, Core::Directory const&) -> ErrorOr<IterationDecision> {
  464. if (entry.type == Core::DirectoryEntry::Type::Directory)
  465. return IterationDecision::Continue;
  466. auto input_path = TRY(FileSystem::real_path(TRY(String::formatted("{}/{}", path, entry.name))));
  467. // FIXME: Test paths should be ByteString
  468. tests.append({ TRY(String::from_byte_string(input_path)), {}, TestMode::Ref, {} });
  469. return IterationDecision::Continue;
  470. }));
  471. return {};
  472. }
  473. static ErrorOr<int> run_tests(HeadlessWebContentView& view, StringView test_root_path, StringView test_glob, bool dump_failed_ref_tests, bool dump_gc_graph)
  474. {
  475. view.clear_content_filters();
  476. TRY(load_test_config(test_root_path));
  477. Vector<Test> tests;
  478. TRY(collect_dump_tests(tests, TRY(String::formatted("{}/Layout", test_root_path)), "."sv, TestMode::Layout));
  479. TRY(collect_dump_tests(tests, TRY(String::formatted("{}/Text", test_root_path)), "."sv, TestMode::Text));
  480. TRY(collect_ref_tests(tests, TRY(String::formatted("{}/Ref", test_root_path))));
  481. tests.remove_all_matching([&](auto const& test) {
  482. return !test.input_path.bytes_as_string_view().matches(test_glob, CaseSensitivity::CaseSensitive);
  483. });
  484. size_t pass_count = 0;
  485. size_t fail_count = 0;
  486. size_t timeout_count = 0;
  487. size_t skipped_count = 0;
  488. bool is_tty = isatty(STDOUT_FILENO);
  489. outln("Running {} tests...", tests.size());
  490. for (size_t i = 0; i < tests.size(); ++i) {
  491. auto& test = tests[i];
  492. if (is_tty) {
  493. // Keep clearing and reusing the same line if stdout is a TTY.
  494. out("\33[2K\r");
  495. }
  496. out("{}/{}: {}", i + 1, tests.size(), LexicalPath::relative_path(test.input_path, test_root_path));
  497. if (is_tty)
  498. fflush(stdout);
  499. else
  500. outln("");
  501. if (s_skipped_tests.contains_slow(test.input_path.bytes_as_string_view())) {
  502. test.result = TestResult::Skipped;
  503. ++skipped_count;
  504. continue;
  505. }
  506. test.result = TRY(run_test(view, test.input_path, test.expectation_path, test.mode, dump_failed_ref_tests));
  507. switch (*test.result) {
  508. case TestResult::Pass:
  509. ++pass_count;
  510. break;
  511. case TestResult::Fail:
  512. ++fail_count;
  513. break;
  514. case TestResult::Timeout:
  515. ++timeout_count;
  516. break;
  517. case TestResult::Skipped:
  518. VERIFY_NOT_REACHED();
  519. break;
  520. }
  521. }
  522. if (is_tty)
  523. outln("\33[2K\rDone!");
  524. outln("==================================================");
  525. outln("Pass: {}, Fail: {}, Skipped: {}, Timeout: {}", pass_count, fail_count, skipped_count, timeout_count);
  526. outln("==================================================");
  527. for (auto& test : tests) {
  528. if (*test.result == TestResult::Pass)
  529. continue;
  530. outln("{}: {}", test_result_to_string(*test.result), test.input_path);
  531. }
  532. if (dump_gc_graph) {
  533. auto path = view.dump_gc_graph();
  534. if (path.is_error()) {
  535. warnln("Failed to dump GC graph: {}", path.error());
  536. } else {
  537. outln("GC graph dumped to {}", path.value());
  538. }
  539. }
  540. if (timeout_count == 0 && fail_count == 0)
  541. return 0;
  542. return 1;
  543. }
  544. ErrorOr<int> serenity_main(Main::Arguments arguments)
  545. {
  546. Core::EventLoop event_loop;
  547. int screenshot_timeout = 1;
  548. StringView raw_url;
  549. auto resources_folder = "/res"sv;
  550. StringView web_driver_ipc_path;
  551. bool dump_failed_ref_tests = false;
  552. bool dump_layout_tree = false;
  553. bool dump_text = false;
  554. bool dump_gc_graph = false;
  555. bool is_layout_test_mode = false;
  556. StringView test_root_path;
  557. ByteString test_glob;
  558. Vector<ByteString> certificates;
  559. Core::ArgsParser args_parser;
  560. args_parser.set_general_help("This utility runs the Browser in headless mode.");
  561. args_parser.add_option(screenshot_timeout, "Take a screenshot after [n] seconds (default: 1)", "screenshot", 's', "n");
  562. args_parser.add_option(dump_layout_tree, "Dump layout tree and exit", "dump-layout-tree", 'd');
  563. args_parser.add_option(dump_text, "Dump text and exit", "dump-text", 'T');
  564. args_parser.add_option(test_root_path, "Run tests in path", "run-tests", 'R', "test-root-path");
  565. args_parser.add_option(test_glob, "Only run tests matching the given glob", "filter", 'f', "glob");
  566. args_parser.add_option(dump_failed_ref_tests, "Dump screenshots of failing ref tests", "dump-failed-ref-tests", 'D');
  567. args_parser.add_option(dump_gc_graph, "Dump GC graph", "dump-gc-graph", 'G');
  568. args_parser.add_option(resources_folder, "Path of the base resources folder (defaults to /res)", "resources", 'r', "resources-root-path");
  569. args_parser.add_option(web_driver_ipc_path, "Path to the WebDriver IPC socket", "webdriver-ipc-path", 0, "path");
  570. args_parser.add_option(is_layout_test_mode, "Enable layout test mode", "layout-test-mode");
  571. args_parser.add_option(certificates, "Path to a certificate file", "certificate", 'C', "certificate");
  572. args_parser.add_positional_argument(raw_url, "URL to open", "url", Core::ArgsParser::Required::No);
  573. args_parser.parse(arguments);
  574. Gfx::FontDatabase::set_default_font_query("Katica 10 400 0");
  575. Gfx::FontDatabase::set_window_title_font_query("Katica 10 700 0");
  576. Gfx::FontDatabase::set_fixed_width_font_query("Csilla 10 400 0");
  577. Core::ResourceImplementation::install(make<Core::ResourceImplementationFile>(MUST(String::from_utf8(resources_folder))));
  578. auto theme_path = LexicalPath::join(resources_folder, "themes"sv, "Default.ini"sv);
  579. auto theme = TRY(Gfx::load_system_theme(theme_path.string()));
  580. // FIXME: Allow passing the window size as an argument.
  581. static constexpr Gfx::IntSize window_size { 800, 600 };
  582. if (!test_root_path.is_empty()) {
  583. // --run-tests implies --layout-test-mode.
  584. is_layout_test_mode = true;
  585. }
  586. StringBuilder command_line_builder;
  587. command_line_builder.join(' ', arguments.strings);
  588. auto view = TRY(HeadlessWebContentView::create(move(theme), window_size, MUST(command_line_builder.to_string()), web_driver_ipc_path, is_layout_test_mode ? Ladybird::IsLayoutTestMode::Yes : Ladybird::IsLayoutTestMode::No, certificates, resources_folder));
  589. if (!test_root_path.is_empty()) {
  590. test_glob = ByteString::formatted("*{}*", test_glob);
  591. return run_tests(*view, test_root_path, test_glob, dump_failed_ref_tests, dump_gc_graph);
  592. }
  593. auto url = WebView::sanitize_url(raw_url);
  594. if (!url.has_value()) {
  595. warnln("Invalid URL: \"{}\"", raw_url);
  596. return Error::from_string_literal("Invalid URL");
  597. }
  598. if (dump_layout_tree) {
  599. TRY(run_dump_test(*view, raw_url, ""sv, TestMode::Layout));
  600. return 0;
  601. }
  602. if (dump_text) {
  603. TRY(run_dump_test(*view, raw_url, ""sv, TestMode::Text));
  604. return 0;
  605. }
  606. if (web_driver_ipc_path.is_empty()) {
  607. auto timer = TRY(load_page_for_screenshot_and_exit(event_loop, *view, url.value(), screenshot_timeout));
  608. return event_loop.exec();
  609. }
  610. return 0;
  611. }