test-web.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2020-2021, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/URL.h>
  7. #include <LibGUI/Application.h>
  8. #include <LibGUI/BoxLayout.h>
  9. #include <LibGUI/Widget.h>
  10. #include <LibGUI/Window.h>
  11. #include <LibTest/JavaScriptTestRunner.h>
  12. #include <LibWeb/Bindings/MainThreadVM.h>
  13. #include <LibWeb/HTML/Parser/HTMLParser.h>
  14. #include <LibWeb/InProcessWebView.h>
  15. #include <LibWeb/Loader/ResourceLoader.h>
  16. using namespace Test::JS;
  17. TEST_ROOT("Userland/Libraries/LibWeb/Tests");
  18. RefPtr<Web::InProcessWebView> g_page_view;
  19. RefPtr<GUI::Application> g_app;
  20. Optional<URL> next_page_to_load;
  21. Vector<Function<void(JS::Object&)>> after_initial_load_hooks;
  22. Vector<Function<void(JS::Object&)>> before_initial_load_hooks;
  23. TESTJS_MAIN_HOOK()
  24. {
  25. g_vm = Web::Bindings::main_thread_vm();
  26. g_app = GUI::Application::construct(g_test_argc, g_test_argv);
  27. auto window = GUI::Window::construct();
  28. auto& main_widget = window->set_main_widget<GUI::Widget>();
  29. main_widget.set_fill_with_background_color(true);
  30. main_widget.set_layout<GUI::VerticalBoxLayout>();
  31. auto& view = main_widget.add<Web::InProcessWebView>();
  32. view.set_document(Web::DOM::Document::create());
  33. g_page_view = view;
  34. }
  35. TESTJS_GLOBAL_FUNCTION(load_local_page, loadLocalPage)
  36. {
  37. auto name = TRY(vm.argument(0).to_string(global_object));
  38. // Clear the hooks
  39. before_initial_load_hooks.clear();
  40. after_initial_load_hooks.clear();
  41. // Set the load URL
  42. if (name.starts_with('/'))
  43. next_page_to_load = URL::create_with_file_protocol(name);
  44. else
  45. next_page_to_load = URL::create_with_file_protocol(LexicalPath::join(g_test_root, "Pages", name).string());
  46. return JS::js_undefined();
  47. }
  48. TESTJS_GLOBAL_FUNCTION(after_initial_page_load, afterInitialPageLoad)
  49. {
  50. auto function = vm.argument(0);
  51. if (!function.is_function()) {
  52. dbgln("afterInitialPageLoad argument is not a function");
  53. return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "Function");
  54. }
  55. after_initial_load_hooks.append([fn = JS::make_handle(&function.as_function()), &vm](auto& page_object) {
  56. [[maybe_unused]] auto unused = vm.call(const_cast<JS::FunctionObject&>(*fn.cell()), JS::js_undefined(), &page_object);
  57. });
  58. return JS::js_undefined();
  59. }
  60. TESTJS_GLOBAL_FUNCTION(before_initial_page_load, beforeInitialPageLoad)
  61. {
  62. auto function = vm.argument(0);
  63. if (!function.is_function()) {
  64. dbgln("beforeInitialPageLoad argument is not a function");
  65. return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "Function");
  66. }
  67. before_initial_load_hooks.append([fn = JS::make_handle(&function.as_function()), &vm](auto& page_object) {
  68. [[maybe_unused]] auto unused = vm.call(const_cast<JS::FunctionObject&>(*fn.cell()), JS::js_undefined(), &page_object);
  69. });
  70. return JS::js_undefined();
  71. }
  72. TESTJS_GLOBAL_FUNCTION(wait_for_page_to_load, waitForPageToLoad)
  73. {
  74. // Create a new parser and immediately get its document to replace the old interpreter.
  75. auto document = Web::DOM::Document::create();
  76. // Run the "before" hooks
  77. for (auto& entry : before_initial_load_hooks)
  78. entry(document->interpreter().global_object());
  79. // Set the load hook
  80. Web::LoadRequest request;
  81. request.set_url(next_page_to_load.value());
  82. auto& loader = Web::ResourceLoader::the();
  83. loader.load_sync(
  84. request,
  85. [&](auto data, auto&, auto) {
  86. Web::HTML::HTMLParser parser(document, data, "utf-8");
  87. // Now parse the HTML page.
  88. parser.run(next_page_to_load.value());
  89. g_page_view->set_document(&parser.document());
  90. if (vm.exception()) {
  91. // FIXME: Should we do something about this? the document itself threw unhandled exceptions...
  92. vm.clear_exception();
  93. }
  94. // Run the "after" hooks
  95. for (auto& entry : after_initial_load_hooks) {
  96. entry(document->interpreter().global_object());
  97. if (vm.exception())
  98. break;
  99. }
  100. },
  101. [&](auto&, auto) {
  102. dbgln("Load of resource {} failed", next_page_to_load.value());
  103. vm.throw_exception<JS::TypeError>(global_object, "Resource load failed");
  104. });
  105. return JS::js_undefined();
  106. }