2020-07-23 07:53:52 +01:00
|
|
|
/*
|
2021-04-28 22:46:44 +02:00
|
|
|
* Copyright (c) 2020-2021, the SerenityOS developers.
|
2020-07-23 07:53:52 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-23 07:53:52 +01:00
|
|
|
*/
|
|
|
|
|
|
2020-08-11 00:02:25 +02:00
|
|
|
#include <AK/URL.h>
|
2020-07-23 07:53:52 +01:00
|
|
|
#include <LibGUI/Application.h>
|
|
|
|
|
#include <LibGUI/BoxLayout.h>
|
|
|
|
|
#include <LibGUI/Widget.h>
|
|
|
|
|
#include <LibGUI/Window.h>
|
2021-05-18 18:46:54 +04:30
|
|
|
#include <LibTest/JavaScriptTestRunner.h>
|
|
|
|
|
#include <LibWeb/Bindings/MainThreadVM.h>
|
2020-07-28 19:18:23 +02:00
|
|
|
#include <LibWeb/HTML/Parser/HTMLDocumentParser.h>
|
2020-08-17 15:58:29 +02:00
|
|
|
#include <LibWeb/InProcessWebView.h>
|
2020-08-25 22:18:32 +04:30
|
|
|
#include <LibWeb/Loader/ResourceLoader.h>
|
2020-07-23 07:53:52 +01:00
|
|
|
|
2021-05-18 18:46:54 +04:30
|
|
|
using namespace Test::JS;
|
2020-07-23 07:53:52 +01:00
|
|
|
|
2021-05-18 18:46:54 +04:30
|
|
|
TEST_ROOT("Userland/Libraries/LibWeb/Tests");
|
2020-07-23 07:53:52 +01:00
|
|
|
|
2021-05-18 18:46:54 +04:30
|
|
|
RefPtr<Web::InProcessWebView> g_page_view;
|
|
|
|
|
RefPtr<GUI::Application> g_app;
|
|
|
|
|
Optional<URL> next_page_to_load;
|
|
|
|
|
Vector<Function<void(JS::Object&)>> after_initial_load_hooks;
|
|
|
|
|
Vector<Function<void(JS::Object&)>> before_initial_load_hooks;
|
2020-07-23 07:53:52 +01:00
|
|
|
|
2021-05-18 18:46:54 +04:30
|
|
|
TESTJS_MAIN_HOOK()
|
2020-07-24 21:24:11 +01:00
|
|
|
{
|
2021-05-18 18:46:54 +04:30
|
|
|
g_vm = Web::Bindings::main_thread_vm();
|
|
|
|
|
g_app = GUI::Application::construct(g_test_argc, g_test_argv);
|
|
|
|
|
auto window = GUI::Window::construct();
|
|
|
|
|
auto& main_widget = window->set_main_widget<GUI::Widget>();
|
|
|
|
|
main_widget.set_fill_with_background_color(true);
|
|
|
|
|
main_widget.set_layout<GUI::VerticalBoxLayout>();
|
|
|
|
|
auto& view = main_widget.add<Web::InProcessWebView>();
|
|
|
|
|
view.set_document(Web::DOM::Document::create());
|
|
|
|
|
g_page_view = view;
|
2020-07-24 21:24:11 +01:00
|
|
|
}
|
|
|
|
|
|
2021-05-18 18:46:54 +04:30
|
|
|
TESTJS_GLOBAL_FUNCTION(load_local_page, loadLocalPage)
|
2020-07-24 21:24:11 +01:00
|
|
|
{
|
2021-05-18 18:46:54 +04:30
|
|
|
auto name = vm.argument(0).to_string(global_object);
|
2020-09-27 18:36:49 +02:00
|
|
|
if (vm.exception())
|
2020-07-24 21:24:11 +01:00
|
|
|
return {};
|
|
|
|
|
|
2021-05-18 18:46:54 +04:30
|
|
|
// Clear the hooks
|
|
|
|
|
before_initial_load_hooks.clear();
|
|
|
|
|
after_initial_load_hooks.clear();
|
2020-07-24 21:24:11 +01:00
|
|
|
|
2021-05-18 18:46:54 +04:30
|
|
|
// Set the load URL
|
|
|
|
|
if (name.starts_with('/'))
|
|
|
|
|
next_page_to_load = URL::create_with_file_protocol(name);
|
|
|
|
|
else
|
|
|
|
|
next_page_to_load = URL::create_with_file_protocol(LexicalPath::join(g_test_root, "Pages", name).string());
|
2020-07-24 21:24:11 +01:00
|
|
|
return JS::js_undefined();
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-18 18:46:54 +04:30
|
|
|
TESTJS_GLOBAL_FUNCTION(after_initial_page_load, afterInitialPageLoad)
|
2020-08-22 20:31:37 +01:00
|
|
|
{
|
2021-05-18 18:46:54 +04:30
|
|
|
auto function = vm.argument(0);
|
|
|
|
|
if (!function.is_function()) {
|
|
|
|
|
dbgln("afterInitialPageLoad argument is not a function");
|
|
|
|
|
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotA, "Function");
|
|
|
|
|
return {};
|
2020-07-23 07:53:52 +01:00
|
|
|
}
|
|
|
|
|
|
2021-05-18 18:46:54 +04:30
|
|
|
after_initial_load_hooks.append([fn = JS::make_handle(&function.as_function()), &vm](auto& page_object) {
|
|
|
|
|
[[maybe_unused]] auto unused = vm.call(const_cast<JS::Function&>(*fn.cell()), JS::js_undefined(), &page_object);
|
2020-07-23 07:53:52 +01:00
|
|
|
});
|
2021-05-18 18:46:54 +04:30
|
|
|
return JS::js_undefined();
|
2020-07-23 07:53:52 +01:00
|
|
|
}
|
|
|
|
|
|
2021-05-18 18:46:54 +04:30
|
|
|
TESTJS_GLOBAL_FUNCTION(before_initial_page_load, beforeInitialPageLoad)
|
2020-07-23 07:53:52 +01:00
|
|
|
{
|
2021-05-18 18:46:54 +04:30
|
|
|
auto function = vm.argument(0);
|
|
|
|
|
if (!function.is_function()) {
|
|
|
|
|
dbgln("beforeInitialPageLoad argument is not a function");
|
|
|
|
|
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotA, "Function");
|
2020-07-23 07:53:52 +01:00
|
|
|
return {};
|
2021-05-18 18:46:54 +04:30
|
|
|
}
|
2020-07-23 07:53:52 +01:00
|
|
|
|
2021-05-18 18:46:54 +04:30
|
|
|
before_initial_load_hooks.append([fn = JS::make_handle(&function.as_function()), &vm](auto& page_object) {
|
|
|
|
|
[[maybe_unused]] auto unused = vm.call(const_cast<JS::Function&>(*fn.cell()), JS::js_undefined(), &page_object);
|
|
|
|
|
});
|
|
|
|
|
return JS::js_undefined();
|
2020-07-23 07:53:52 +01:00
|
|
|
}
|
|
|
|
|
|
2021-05-18 18:46:54 +04:30
|
|
|
TESTJS_GLOBAL_FUNCTION(wait_for_page_to_load, waitForPageToLoad)
|
2020-07-23 07:53:52 +01:00
|
|
|
{
|
2021-05-18 18:46:54 +04:30
|
|
|
// Create a new parser and immediately get its document to replace the old interpreter.
|
|
|
|
|
auto document = Web::DOM::Document::create();
|
2020-07-23 07:53:52 +01:00
|
|
|
|
2021-05-18 18:46:54 +04:30
|
|
|
// Run the "before" hooks
|
|
|
|
|
for (auto& entry : before_initial_load_hooks)
|
|
|
|
|
entry(document->interpreter().global_object());
|
2020-07-23 07:53:52 +01:00
|
|
|
|
2021-05-18 18:46:54 +04:30
|
|
|
// Set the load hook
|
2021-04-14 10:36:34 -04:00
|
|
|
Web::LoadRequest request;
|
2021-05-18 18:46:54 +04:30
|
|
|
request.set_url(next_page_to_load.value());
|
2021-04-14 10:36:34 -04:00
|
|
|
|
2021-05-18 18:46:54 +04:30
|
|
|
auto& loader = Web::ResourceLoader::the();
|
|
|
|
|
loader.load_sync(
|
2021-04-14 10:36:34 -04:00
|
|
|
request,
|
2021-04-03 15:11:36 +02:00
|
|
|
[&](auto data, auto&, auto) {
|
2020-12-13 16:59:22 +01:00
|
|
|
Web::HTML::HTMLDocumentParser parser(document, data, "utf-8");
|
2020-07-23 07:53:52 +01:00
|
|
|
// Now parse the HTML page.
|
2021-05-18 18:46:54 +04:30
|
|
|
parser.run(next_page_to_load.value());
|
|
|
|
|
g_page_view->set_document(&parser.document());
|
|
|
|
|
if (vm.exception()) {
|
|
|
|
|
// FIXME: Should we do something about this? the document itself threw unhandled exceptions...
|
|
|
|
|
vm.clear_exception();
|
2020-07-23 07:53:52 +01:00
|
|
|
}
|
|
|
|
|
|
2021-05-18 18:46:54 +04:30
|
|
|
// Run the "after" hooks
|
|
|
|
|
for (auto& entry : after_initial_load_hooks) {
|
|
|
|
|
entry(document->interpreter().global_object());
|
|
|
|
|
if (vm.exception())
|
|
|
|
|
break;
|
2020-07-23 07:53:52 +01:00
|
|
|
}
|
|
|
|
|
},
|
AK+Everywhere: Disallow constructing Functions from incompatible types
Previously, AK::Function would accept _any_ callable type, and try to
call it when called, first with the given set of arguments, then with
zero arguments, and if all of those failed, it would simply not call the
function and **return a value-constructed Out type**.
This lead to many, many, many hard to debug situations when someone
forgot a `const` in their lambda argument types, and many cases of
people taking zero arguments in their lambdas to ignore them.
This commit reworks the Function interface to not include any such
surprising behaviour, if your function instance is not callable with
the declared argument set of the Function, it can simply not be
assigned to that Function instance, end of story.
2021-06-05 23:04:31 +04:30
|
|
|
[&](auto&, auto) {
|
2021-05-18 18:46:54 +04:30
|
|
|
dbgln("Load of resource {} failed", next_page_to_load.value());
|
AK+Everywhere: Disallow constructing Functions from incompatible types
Previously, AK::Function would accept _any_ callable type, and try to
call it when called, first with the given set of arguments, then with
zero arguments, and if all of those failed, it would simply not call the
function and **return a value-constructed Out type**.
This lead to many, many, many hard to debug situations when someone
forgot a `const` in their lambda argument types, and many cases of
people taking zero arguments in their lambdas to ignore them.
This commit reworks the Function interface to not include any such
surprising behaviour, if your function instance is not callable with
the declared argument set of the Function, it can simply not be
assigned to that Function instance, end of story.
2021-06-05 23:04:31 +04:30
|
|
|
vm.throw_exception<JS::TypeError>(global_object, "Resource load failed");
|
2020-08-11 00:02:25 +02:00
|
|
|
});
|
2020-07-23 07:53:52 +01:00
|
|
|
|
2021-05-18 18:46:54 +04:30
|
|
|
return JS::js_undefined();
|
2020-07-23 07:53:52 +01:00
|
|
|
}
|