test-web.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. /*
  2. * Copyright (c) 2020, The SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Function.h>
  27. #include <AK/JsonObject.h>
  28. #include <AK/JsonValue.h>
  29. #include <AK/QuickSort.h>
  30. #include <AK/URL.h>
  31. #include <LibCore/ArgsParser.h>
  32. #include <LibCore/DirIterator.h>
  33. #include <LibCore/File.h>
  34. #include <LibGUI/Application.h>
  35. #include <LibGUI/BoxLayout.h>
  36. #include <LibGUI/Widget.h>
  37. #include <LibGUI/Window.h>
  38. #include <LibJS/Interpreter.h>
  39. #include <LibJS/Lexer.h>
  40. #include <LibJS/Parser.h>
  41. #include <LibJS/Runtime/Array.h>
  42. #include <LibJS/Runtime/JSONObject.h>
  43. #include <LibWeb/HTML/Parser/HTMLDocumentParser.h>
  44. #include <LibWeb/InProcessWebView.h>
  45. #include <LibWeb/Loader/ResourceLoader.h>
  46. #include <signal.h>
  47. #include <sys/time.h>
  48. #define TOP_LEVEL_TEST_NAME "__$$TOP_LEVEL$$__"
  49. enum class TestResult {
  50. Pass,
  51. Fail,
  52. Skip,
  53. };
  54. struct JSTest {
  55. String name;
  56. TestResult result;
  57. String details;
  58. };
  59. struct JSSuite {
  60. String name;
  61. // A failed test takes precedence over a skipped test, which both have
  62. // precedence over a passed test
  63. TestResult most_severe_test_result { TestResult::Pass };
  64. Vector<JSTest> tests {};
  65. };
  66. struct ParserError {
  67. JS::Parser::Error error;
  68. String hint;
  69. };
  70. struct JSFileResult {
  71. String name;
  72. Optional<ParserError> error {};
  73. double time_taken { 0 };
  74. // A failed test takes precedence over a skipped test, which both have
  75. // precedence over a passed test
  76. TestResult most_severe_test_result { TestResult::Pass };
  77. Vector<JSSuite> suites {};
  78. Vector<String> logged_messages {};
  79. };
  80. struct JSTestRunnerCounts {
  81. int tests_failed { 0 };
  82. int tests_passed { 0 };
  83. int tests_skipped { 0 };
  84. int suites_failed { 0 };
  85. int suites_passed { 0 };
  86. int files_total { 0 };
  87. };
  88. Function<void(const URL&)> g_on_page_change;
  89. class TestRunnerObject final : public JS::Object {
  90. JS_OBJECT(TestRunnerObject, JS::Object);
  91. public:
  92. explicit TestRunnerObject(JS::GlobalObject&);
  93. virtual void initialize(JS::GlobalObject&) override;
  94. virtual ~TestRunnerObject() override;
  95. private:
  96. JS_DECLARE_NATIVE_FUNCTION(change_page);
  97. };
  98. TestRunnerObject::TestRunnerObject(JS::GlobalObject& global_object)
  99. : Object(*global_object.object_prototype())
  100. {
  101. }
  102. void TestRunnerObject::initialize(JS::GlobalObject& global_object)
  103. {
  104. Object::initialize(global_object);
  105. define_native_function("changePage", change_page, 1);
  106. }
  107. TestRunnerObject::~TestRunnerObject()
  108. {
  109. }
  110. JS_DEFINE_NATIVE_FUNCTION(TestRunnerObject::change_page)
  111. {
  112. auto url = vm.argument(0).to_string(global_object);
  113. if (vm.exception())
  114. return {};
  115. if (g_on_page_change)
  116. g_on_page_change(url);
  117. return JS::js_undefined();
  118. }
  119. class TestRunner {
  120. public:
  121. TestRunner(String web_test_root, String js_test_root, Web::InProcessWebView& page_view, bool print_times)
  122. : m_web_test_root(move(web_test_root))
  123. , m_js_test_root(move(js_test_root))
  124. , m_print_times(print_times)
  125. , m_page_view(page_view)
  126. {
  127. }
  128. void run();
  129. private:
  130. JSFileResult run_file_test(const String& test_path);
  131. void print_file_result(const JSFileResult& file_result) const;
  132. void print_test_results() const;
  133. String m_web_test_root;
  134. String m_js_test_root;
  135. bool m_print_times;
  136. double m_total_elapsed_time_in_ms { 0 };
  137. JSTestRunnerCounts m_counts;
  138. RefPtr<Web::InProcessWebView> m_page_view;
  139. RefPtr<JS::Program> m_js_test_common;
  140. RefPtr<JS::Program> m_web_test_common;
  141. };
  142. static void cleanup_and_exit()
  143. {
  144. // Clear the taskbar progress.
  145. #ifdef __serenity__
  146. fprintf(stderr, "\033]9;-1;\033\\");
  147. #endif
  148. exit(1);
  149. }
  150. #if 0
  151. static void handle_sigabrt(int)
  152. {
  153. dbg() << "test-web: SIGABRT received, cleaning up.";
  154. cleanup_and_exit();
  155. }
  156. #endif
  157. static double get_time_in_ms()
  158. {
  159. struct timeval tv1;
  160. auto return_code = gettimeofday(&tv1, nullptr);
  161. ASSERT(return_code >= 0);
  162. return static_cast<double>(tv1.tv_sec) * 1000.0 + static_cast<double>(tv1.tv_usec) / 1000.0;
  163. }
  164. template<typename Callback>
  165. void iterate_directory_recursively(const String& directory_path, Callback callback)
  166. {
  167. Core::DirIterator directory_iterator(directory_path, Core::DirIterator::Flags::SkipDots);
  168. while (directory_iterator.has_next()) {
  169. auto file_path = String::format("%s/%s", directory_path.characters(), directory_iterator.next_path().characters());
  170. if (Core::File::is_directory(file_path)) {
  171. iterate_directory_recursively(file_path, callback);
  172. } else {
  173. callback(move(file_path));
  174. }
  175. }
  176. }
  177. static Vector<String> get_test_paths(const String& test_root)
  178. {
  179. Vector<String> paths;
  180. iterate_directory_recursively(test_root, [&](const String& file_path) {
  181. if (!file_path.ends_with("test-common.js") && !file_path.ends_with(".html") && !file_path.ends_with(".ts"))
  182. paths.append(file_path);
  183. });
  184. quick_sort(paths);
  185. return paths;
  186. }
  187. void TestRunner::run()
  188. {
  189. size_t progress_counter = 0;
  190. auto test_paths = get_test_paths(m_web_test_root);
  191. g_on_page_change = [this](auto& page_to_load) {
  192. if (!page_to_load.is_valid()) {
  193. printf("Invalid page URL (%s) on page change", page_to_load.to_string().characters());
  194. cleanup_and_exit();
  195. }
  196. ASSERT(m_page_view->document());
  197. // We want to keep the same document since the interpreter is tied to the document,
  198. // and we don't want to lose the test state. So, we just clear the document and
  199. // give a new parser the existing document to work on.
  200. m_page_view->document()->remove_all_children();
  201. Web::ResourceLoader::the().load_sync(
  202. page_to_load,
  203. [&](auto data, auto&) {
  204. Web::HTML::HTMLDocumentParser parser(data, "utf-8", *m_page_view->document());
  205. parser.run(page_to_load);
  206. },
  207. [page_to_load](auto error) {
  208. printf("Failed to load test page: %s (%s)", page_to_load.to_string().characters(), error.characters());
  209. cleanup_and_exit();
  210. });
  211. };
  212. for (auto& path : test_paths) {
  213. ++progress_counter;
  214. print_file_result(run_file_test(path));
  215. #ifdef __serenity__
  216. fprintf(stderr, "\033]9;%zu;%zu;\033\\", progress_counter, test_paths.size());
  217. #endif
  218. }
  219. #ifdef __serenity__
  220. fprintf(stderr, "\033]9;-1;\033\\");
  221. #endif
  222. print_test_results();
  223. }
  224. static Result<NonnullRefPtr<JS::Program>, ParserError> parse_file(const String& file_path)
  225. {
  226. auto file = Core::File::construct(file_path);
  227. auto result = file->open(Core::IODevice::ReadOnly);
  228. if (!result) {
  229. printf("Failed to open the following file: \"%s\"\n", file_path.characters());
  230. cleanup_and_exit();
  231. }
  232. auto contents = file->read_all();
  233. String test_file_string(reinterpret_cast<const char*>(contents.data()), contents.size());
  234. file->close();
  235. auto parser = JS::Parser(JS::Lexer(test_file_string));
  236. auto program = parser.parse_program();
  237. if (parser.has_errors()) {
  238. auto error = parser.errors()[0];
  239. return Result<NonnullRefPtr<JS::Program>, ParserError>(ParserError { error, error.source_location_hint(test_file_string) });
  240. }
  241. return Result<NonnullRefPtr<JS::Program>, ParserError>(program);
  242. }
  243. static Optional<JsonValue> get_test_results(JS::Interpreter& interpreter)
  244. {
  245. auto result = interpreter.vm().get_variable("__TestResults__", interpreter.global_object());
  246. auto json_string = JS::JSONObject::stringify_impl(interpreter.global_object(), result, JS::js_undefined(), JS::js_undefined());
  247. auto json = JsonValue::from_string(json_string);
  248. if (!json.has_value())
  249. return {};
  250. return json.value();
  251. }
  252. JSFileResult TestRunner::run_file_test(const String& test_path)
  253. {
  254. double start_time = get_time_in_ms();
  255. ASSERT(m_page_view->document());
  256. auto& old_interpreter = m_page_view->document()->interpreter();
  257. // FIXME: This is a hack while we're refactoring Interpreter/VM stuff.
  258. JS::VM::InterpreterExecutionScope scope(old_interpreter);
  259. if (!m_js_test_common) {
  260. auto result = parse_file(String::format("%s/test-common.js", m_js_test_root.characters()));
  261. if (result.is_error()) {
  262. printf("Unable to parse %s/test-common.js\n", m_js_test_root.characters());
  263. printf("%s\n", result.error().error.to_string().characters());
  264. printf("%s\n", result.error().hint.characters());
  265. cleanup_and_exit();
  266. }
  267. m_js_test_common = result.value();
  268. }
  269. if (!m_web_test_common) {
  270. auto result = parse_file(String::format("%s/test-common.js", m_web_test_root.characters()));
  271. if (result.is_error()) {
  272. printf("Unable to parse %s/test-common.js\n", m_web_test_root.characters());
  273. printf("%s\n", result.error().error.to_string().characters());
  274. printf("%s\n", result.error().hint.characters());
  275. cleanup_and_exit();
  276. }
  277. m_web_test_common = result.value();
  278. }
  279. auto file_program = parse_file(test_path);
  280. if (file_program.is_error())
  281. return { test_path, file_program.error() };
  282. // Setup the test on the current page to get "__PageToLoad__".
  283. old_interpreter.run(old_interpreter.global_object(), *m_web_test_common);
  284. old_interpreter.run(old_interpreter.global_object(), *file_program.value());
  285. auto page_to_load = URL(old_interpreter.vm().get_variable("__PageToLoad__", old_interpreter.global_object()).as_string().string());
  286. if (!page_to_load.is_valid()) {
  287. printf("Invalid page URL for %s", test_path.characters());
  288. cleanup_and_exit();
  289. }
  290. JSFileResult file_result;
  291. Web::ResourceLoader::the().load_sync(
  292. page_to_load,
  293. [&](auto data, auto&) {
  294. // Create a new parser and immediately get its document to replace the old interpreter.
  295. Web::HTML::HTMLDocumentParser parser(data, "utf-8");
  296. auto& new_interpreter = parser.document().interpreter();
  297. // Setup the test environment and call "__BeforeInitialPageLoad__"
  298. new_interpreter.global_object().define_property(
  299. "libweb_tester",
  300. new_interpreter.heap().allocate<TestRunnerObject>(new_interpreter.global_object(), new_interpreter.global_object()),
  301. JS::Attribute::Enumerable | JS::Attribute::Configurable);
  302. new_interpreter.run(new_interpreter.global_object(), *m_js_test_common);
  303. new_interpreter.run(new_interpreter.global_object(), *m_web_test_common);
  304. new_interpreter.run(new_interpreter.global_object(), *file_program.value());
  305. auto& before_initial_page_load = new_interpreter.vm().get_variable("__BeforeInitialPageLoad__", new_interpreter.global_object()).as_function();
  306. (void)new_interpreter.call(before_initial_page_load, JS::js_undefined());
  307. if (new_interpreter.exception())
  308. new_interpreter.vm().clear_exception();
  309. // Now parse the HTML page.
  310. parser.run(page_to_load);
  311. m_page_view->set_document(&parser.document());
  312. // Finally run the test by calling "__AfterInitialPageLoad__"
  313. auto& after_initial_page_load = new_interpreter.vm().get_variable("__AfterInitialPageLoad__", new_interpreter.global_object()).as_function();
  314. (void)new_interpreter.call(after_initial_page_load, JS::js_undefined());
  315. if (new_interpreter.exception())
  316. new_interpreter.vm().clear_exception();
  317. auto test_json = get_test_results(new_interpreter);
  318. if (!test_json.has_value()) {
  319. printf("Received malformed JSON from test \"%s\"\n", test_path.characters());
  320. cleanup_and_exit();
  321. }
  322. file_result = { test_path.substring(m_web_test_root.length() + 1, test_path.length() - m_web_test_root.length() - 1) };
  323. // Collect logged messages
  324. auto& arr = new_interpreter.vm().get_variable("__UserOutput__", new_interpreter.global_object()).as_array();
  325. for (auto& entry : arr.indexed_properties()) {
  326. auto message = entry.value_and_attributes(&new_interpreter.global_object()).value;
  327. file_result.logged_messages.append(message.to_string_without_side_effects());
  328. }
  329. test_json.value().as_object().for_each_member([&](const String& suite_name, const JsonValue& suite_value) {
  330. JSSuite suite { suite_name };
  331. ASSERT(suite_value.is_object());
  332. suite_value.as_object().for_each_member([&](const String& test_name, const JsonValue& test_value) {
  333. JSTest test { test_name, TestResult::Fail, "" };
  334. ASSERT(test_value.is_object());
  335. ASSERT(test_value.as_object().has("result"));
  336. auto result = test_value.as_object().get("result");
  337. ASSERT(result.is_string());
  338. auto result_string = result.as_string();
  339. if (result_string == "pass") {
  340. test.result = TestResult::Pass;
  341. m_counts.tests_passed++;
  342. } else if (result_string == "fail") {
  343. test.result = TestResult::Fail;
  344. m_counts.tests_failed++;
  345. suite.most_severe_test_result = TestResult::Fail;
  346. ASSERT(test_value.as_object().has("details"));
  347. auto details = test_value.as_object().get("details");
  348. ASSERT(result.is_string());
  349. test.details = details.as_string();
  350. } else {
  351. test.result = TestResult::Skip;
  352. if (suite.most_severe_test_result == TestResult::Pass)
  353. suite.most_severe_test_result = TestResult::Skip;
  354. m_counts.tests_skipped++;
  355. }
  356. suite.tests.append(test);
  357. });
  358. if (suite.most_severe_test_result == TestResult::Fail) {
  359. m_counts.suites_failed++;
  360. file_result.most_severe_test_result = TestResult::Fail;
  361. } else {
  362. if (suite.most_severe_test_result == TestResult::Skip && file_result.most_severe_test_result == TestResult::Pass)
  363. file_result.most_severe_test_result = TestResult::Skip;
  364. m_counts.suites_passed++;
  365. }
  366. file_result.suites.append(suite);
  367. });
  368. m_counts.files_total++;
  369. file_result.time_taken = get_time_in_ms() - start_time;
  370. m_total_elapsed_time_in_ms += file_result.time_taken;
  371. },
  372. [page_to_load](auto error) {
  373. printf("Failed to load test page: %s (%s)", page_to_load.to_string().characters(), error.characters());
  374. cleanup_and_exit();
  375. });
  376. return file_result;
  377. }
  378. enum Modifier {
  379. BG_RED,
  380. BG_GREEN,
  381. FG_RED,
  382. FG_GREEN,
  383. FG_ORANGE,
  384. FG_GRAY,
  385. FG_BLACK,
  386. FG_BOLD,
  387. ITALIC,
  388. CLEAR,
  389. };
  390. static void print_modifiers(Vector<Modifier> modifiers)
  391. {
  392. for (auto& modifier : modifiers) {
  393. auto code = [&]() -> String {
  394. switch (modifier) {
  395. case BG_RED:
  396. return "\033[48;2;255;0;102m";
  397. case BG_GREEN:
  398. return "\033[48;2;102;255;0m";
  399. case FG_RED:
  400. return "\033[38;2;255;0;102m";
  401. case FG_GREEN:
  402. return "\033[38;2;102;255;0m";
  403. case FG_ORANGE:
  404. return "\033[38;2;255;102;0m";
  405. case FG_GRAY:
  406. return "\033[38;2;135;139;148m";
  407. case FG_BLACK:
  408. return "\033[30m";
  409. case FG_BOLD:
  410. return "\033[1m";
  411. case ITALIC:
  412. return "\033[3m";
  413. case CLEAR:
  414. return "\033[0m";
  415. }
  416. ASSERT_NOT_REACHED();
  417. };
  418. printf("%s", code().characters());
  419. }
  420. }
  421. void TestRunner::print_file_result(const JSFileResult& file_result) const
  422. {
  423. if (file_result.most_severe_test_result == TestResult::Fail || file_result.error.has_value()) {
  424. print_modifiers({ BG_RED, FG_BLACK, FG_BOLD });
  425. printf(" FAIL ");
  426. print_modifiers({ CLEAR });
  427. } else {
  428. if (m_print_times || file_result.most_severe_test_result != TestResult::Pass) {
  429. print_modifiers({ BG_GREEN, FG_BLACK, FG_BOLD });
  430. printf(" PASS ");
  431. print_modifiers({ CLEAR });
  432. } else {
  433. return;
  434. }
  435. }
  436. printf(" %s", file_result.name.characters());
  437. if (m_print_times) {
  438. print_modifiers({ CLEAR, ITALIC, FG_GRAY });
  439. if (file_result.time_taken < 1000) {
  440. printf(" (%dms)\n", static_cast<int>(file_result.time_taken));
  441. } else {
  442. printf(" (%.3fs)\n", file_result.time_taken / 1000.0);
  443. }
  444. print_modifiers({ CLEAR });
  445. } else {
  446. printf("\n");
  447. }
  448. if (!file_result.logged_messages.is_empty()) {
  449. print_modifiers({ FG_GRAY, FG_BOLD });
  450. #ifdef __serenity__
  451. printf(" ℹ Console output:\n");
  452. #else
  453. // This emoji has a second invisible byte after it. The one above does not
  454. printf(" ℹ️ Console output:\n");
  455. #endif
  456. print_modifiers({ CLEAR, FG_GRAY });
  457. for (auto& message : file_result.logged_messages)
  458. printf(" %s\n", message.characters());
  459. }
  460. if (file_result.error.has_value()) {
  461. auto test_error = file_result.error.value();
  462. print_modifiers({ FG_RED });
  463. #ifdef __serenity__
  464. printf(" ❌ The file failed to parse\n\n");
  465. #else
  466. // No invisible byte here, but the spacing still needs to be altered on the host
  467. printf(" ❌ The file failed to parse\n\n");
  468. #endif
  469. print_modifiers({ FG_GRAY });
  470. for (auto& message : test_error.hint.split('\n', true)) {
  471. printf(" %s\n", message.characters());
  472. }
  473. print_modifiers({ FG_RED });
  474. printf(" %s\n\n", test_error.error.to_string().characters());
  475. return;
  476. }
  477. if (file_result.most_severe_test_result != TestResult::Pass) {
  478. for (auto& suite : file_result.suites) {
  479. if (suite.most_severe_test_result == TestResult::Pass)
  480. continue;
  481. bool failed = suite.most_severe_test_result == TestResult::Fail;
  482. print_modifiers({ FG_GRAY, FG_BOLD });
  483. if (failed) {
  484. #ifdef __serenity__
  485. printf(" ❌ Suite: ");
  486. #else
  487. // No invisible byte here, but the spacing still needs to be altered on the host
  488. printf(" ❌ Suite: ");
  489. #endif
  490. } else {
  491. #ifdef __serenity__
  492. printf(" ⚠ Suite: ");
  493. #else
  494. // This emoji has a second invisible byte after it. The one above does not
  495. printf(" ⚠️ Suite: ");
  496. #endif
  497. }
  498. print_modifiers({ CLEAR, FG_GRAY });
  499. if (suite.name == TOP_LEVEL_TEST_NAME) {
  500. printf("<top-level>\n");
  501. } else {
  502. printf("%s\n", suite.name.characters());
  503. }
  504. print_modifiers({ CLEAR });
  505. for (auto& test : suite.tests) {
  506. if (test.result == TestResult::Pass)
  507. continue;
  508. print_modifiers({ FG_GRAY, FG_BOLD });
  509. printf(" Test: ");
  510. if (test.result == TestResult::Fail) {
  511. print_modifiers({ CLEAR, FG_RED });
  512. printf("%s (failed):\n", test.name.characters());
  513. printf(" %s\n", test.details.characters());
  514. } else {
  515. print_modifiers({ CLEAR, FG_ORANGE });
  516. printf("%s (skipped)\n", test.name.characters());
  517. }
  518. print_modifiers({ CLEAR });
  519. }
  520. }
  521. }
  522. }
  523. void TestRunner::print_test_results() const
  524. {
  525. printf("\nTest Suites: ");
  526. if (m_counts.suites_failed) {
  527. print_modifiers({ FG_RED });
  528. printf("%d failed, ", m_counts.suites_failed);
  529. print_modifiers({ CLEAR });
  530. }
  531. if (m_counts.suites_passed) {
  532. print_modifiers({ FG_GREEN });
  533. printf("%d passed, ", m_counts.suites_passed);
  534. print_modifiers({ CLEAR });
  535. }
  536. printf("%d total\n", m_counts.suites_failed + m_counts.suites_passed);
  537. printf("Tests: ");
  538. if (m_counts.tests_failed) {
  539. print_modifiers({ FG_RED });
  540. printf("%d failed, ", m_counts.tests_failed);
  541. print_modifiers({ CLEAR });
  542. }
  543. if (m_counts.tests_skipped) {
  544. print_modifiers({ FG_ORANGE });
  545. printf("%d skipped, ", m_counts.tests_skipped);
  546. print_modifiers({ CLEAR });
  547. }
  548. if (m_counts.tests_passed) {
  549. print_modifiers({ FG_GREEN });
  550. printf("%d passed, ", m_counts.tests_passed);
  551. print_modifiers({ CLEAR });
  552. }
  553. printf("%d total\n", m_counts.tests_failed + m_counts.tests_passed);
  554. printf("Files: %d total\n", m_counts.files_total);
  555. printf("Time: ");
  556. if (m_total_elapsed_time_in_ms < 1000.0) {
  557. printf("%dms\n\n", static_cast<int>(m_total_elapsed_time_in_ms));
  558. } else {
  559. printf("%-.3fs\n\n", m_total_elapsed_time_in_ms / 1000.0);
  560. }
  561. }
  562. int main(int argc, char** argv)
  563. {
  564. bool print_times = false;
  565. bool show_window = false;
  566. #if 0
  567. struct sigaction act;
  568. memset(&act, 0, sizeof(act));
  569. act.sa_flags = SA_NOCLDWAIT;
  570. act.sa_handler = handle_sigabrt;
  571. int rc = sigaction(SIGABRT, &act, nullptr);
  572. if (rc < 0) {
  573. perror("sigaction");
  574. return 1;
  575. }
  576. #endif
  577. Core::ArgsParser args_parser;
  578. args_parser.add_option(print_times, "Show duration of each test", "show-time", 't');
  579. args_parser.add_option(show_window, "Show window while running tests", "window", 'w');
  580. args_parser.parse(argc, argv);
  581. auto app = GUI::Application::construct(argc, argv);
  582. auto window = GUI::Window::construct();
  583. auto& main_widget = window->set_main_widget<GUI::Widget>();
  584. main_widget.set_fill_with_background_color(true);
  585. main_widget.set_layout<GUI::VerticalBoxLayout>();
  586. auto& view = main_widget.add<Web::InProcessWebView>();
  587. view.set_document(adopt(*new Web::DOM::Document));
  588. if (show_window) {
  589. window->set_title("LibWeb Test Window");
  590. window->resize(640, 480);
  591. window->show();
  592. }
  593. #ifdef __serenity__
  594. TestRunner("/home/anon/web-tests", "/home/anon/js-tests", view, print_times).run();
  595. #else
  596. char* serenity_root = getenv("SERENITY_ROOT");
  597. if (!serenity_root) {
  598. printf("test-web requires the SERENITY_ROOT environment variable to be set");
  599. return 1;
  600. }
  601. TestRunner(String::format("%s/Libraries/LibWeb/Tests", serenity_root), String::format("%s/Libraries/LibJS/Tests", serenity_root), view, print_times).run();
  602. #endif
  603. return 0;
  604. }