2020-07-23 03:53:52 -03:00
/*
2021-04-28 17:46:44 -03:00
* Copyright ( c ) 2020 - 2021 , the SerenityOS developers .
2020-07-23 03:53:52 -03:00
*
2021-04-22 05:24:48 -03:00
* SPDX - License - Identifier : BSD - 2 - Clause
2020-07-23 03:53:52 -03:00
*/
2020-08-10 19:02:25 -03:00
# include <AK/URL.h>
2020-07-23 03:53:52 -03:00
# include <LibGUI/Application.h>
# include <LibGUI/BoxLayout.h>
# include <LibGUI/Widget.h>
# include <LibGUI/Window.h>
2021-05-18 11:16:54 -03:00
# include <LibTest/JavaScriptTestRunner.h>
# include <LibWeb/Bindings/MainThreadVM.h>
2021-10-14 12:12:53 -03:00
# include <LibWeb/Bindings/WindowObject.h>
2021-09-25 18:15:48 -03:00
# include <LibWeb/HTML/Parser/HTMLParser.h>
2021-10-14 12:12:53 -03:00
# include <LibWeb/HTML/Scripting/WindowEnvironmentSettingsObject.h>
2020-08-17 10:58:29 -03:00
# include <LibWeb/InProcessWebView.h>
2020-08-25 14:48:32 -03:00
# include <LibWeb/Loader/ResourceLoader.h>
2020-07-23 03:53:52 -03:00
2021-05-18 11:16:54 -03:00
using namespace Test : : JS ;
2020-07-23 03:53:52 -03:00
2021-05-18 11:16:54 -03:00
TEST_ROOT ( " Userland/Libraries/LibWeb/Tests " ) ;
2020-07-23 03:53:52 -03:00
2021-05-18 11:16:54 -03:00
RefPtr < Web : : InProcessWebView > g_page_view ;
RefPtr < GUI : : Application > g_app ;
Optional < URL > next_page_to_load ;
2022-02-07 11:12:41 -03:00
Vector < Function < JS : : ThrowCompletionOr < void > ( JS : : Object & ) > > after_initial_load_hooks ;
Vector < Function < JS : : ThrowCompletionOr < void > ( JS : : Object & ) > > before_initial_load_hooks ;
2021-10-14 12:12:53 -03:00
RefPtr < Web : : DOM : : Document > g_current_interpreter_document ;
2020-07-23 03:53:52 -03:00
2021-05-18 11:16:54 -03:00
TESTJS_MAIN_HOOK ( )
2020-07-24 17:24:11 -03:00
{
2021-05-18 11:16:54 -03:00
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 17:24:11 -03:00
}
2021-10-14 12:12:53 -03:00
struct TestWebGlobalObject : public Web : : Bindings : : WindowObject {
JS_OBJECT ( TestWebGlobalObject , Web : : Bindings : : WindowObject ) ;
public :
2022-03-07 19:08:26 -03:00
TestWebGlobalObject ( Web : : HTML : : Window & window )
2021-10-14 12:12:53 -03:00
: Web : : Bindings : : WindowObject ( window )
{
}
virtual ~ TestWebGlobalObject ( ) override = default ;
virtual void initialize_global_object ( ) override ;
JS_DECLARE_NATIVE_FUNCTION ( load_local_page ) ;
JS_DECLARE_NATIVE_FUNCTION ( after_initial_page_load ) ;
JS_DECLARE_NATIVE_FUNCTION ( before_initial_page_load ) ;
JS_DECLARE_NATIVE_FUNCTION ( wait_for_page_to_load ) ;
} ;
void TestWebGlobalObject : : initialize_global_object ( )
{
Base : : initialize_global_object ( ) ;
define_native_function ( " loadLocalPage " , load_local_page , 1 , JS : : default_attributes ) ;
define_native_function ( " afterInitialPageLoad " , after_initial_page_load , 1 , JS : : default_attributes ) ;
define_native_function ( " beforeInitialPageLoad " , before_initial_page_load , 1 , JS : : default_attributes ) ;
define_native_function ( " waitForPageToLoad " , wait_for_page_to_load , 0 , JS : : default_attributes ) ;
}
TESTJS_CREATE_INTERPRETER_HOOK ( )
{
// FIXME: This is a hack as the document we create needs to stay alive the entire time and we don't have insight into JavaScriptTestRUnner from here to work out the lifetime from here.
g_current_interpreter_document = Web : : DOM : : Document : : create ( ) ;
// FIXME: Use WindowProxy as the globalThis value.
auto interpreter = JS : : Interpreter : : create < TestWebGlobalObject > ( * g_vm , g_current_interpreter_document - > window ( ) ) ;
// FIXME: Work out the creation URL.
AK : : URL creation_url ;
Web : : HTML : : WindowEnvironmentSettingsObject : : setup ( creation_url , g_vm - > running_execution_context ( ) ) ;
return interpreter ;
}
JS_DEFINE_NATIVE_FUNCTION ( TestWebGlobalObject : : load_local_page )
2020-07-24 17:24:11 -03:00
{
2021-10-19 19:58:04 -03:00
auto name = TRY ( vm . argument ( 0 ) . to_string ( global_object ) ) ;
2020-07-24 17:24:11 -03:00
2021-05-18 11:16:54 -03:00
// Clear the hooks
before_initial_load_hooks . clear ( ) ;
after_initial_load_hooks . clear ( ) ;
2020-07-24 17:24:11 -03:00
2021-05-18 11:16:54 -03:00
// 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 17:24:11 -03:00
return JS : : js_undefined ( ) ;
}
2021-10-14 12:12:53 -03:00
JS_DEFINE_NATIVE_FUNCTION ( TestWebGlobalObject : : after_initial_page_load )
2020-08-22 16:31:37 -03:00
{
2021-05-18 11:16:54 -03:00
auto function = vm . argument ( 0 ) ;
if ( ! function . is_function ( ) ) {
dbgln ( " afterInitialPageLoad argument is not a function " ) ;
2021-10-19 19:58:04 -03:00
return vm . throw_completion < JS : : TypeError > ( global_object , JS : : ErrorType : : NotAnObjectOfType , " Function " ) ;
2020-07-23 03:53:52 -03:00
}
2022-02-07 11:12:41 -03:00
after_initial_load_hooks . append ( [ fn = JS : : make_handle ( & function . as_function ( ) ) , & global_object ] ( auto & page_object ) - > JS : : ThrowCompletionOr < void > {
TRY ( JS : : call ( global_object , const_cast < JS : : FunctionObject & > ( * fn . cell ( ) ) , JS : : js_undefined ( ) , & page_object ) ) ;
return { } ;
2020-07-23 03:53:52 -03:00
} ) ;
2021-05-18 11:16:54 -03:00
return JS : : js_undefined ( ) ;
2020-07-23 03:53:52 -03:00
}
2021-10-14 12:12:53 -03:00
JS_DEFINE_NATIVE_FUNCTION ( TestWebGlobalObject : : before_initial_page_load )
2020-07-23 03:53:52 -03:00
{
2021-05-18 11:16:54 -03:00
auto function = vm . argument ( 0 ) ;
if ( ! function . is_function ( ) ) {
dbgln ( " beforeInitialPageLoad argument is not a function " ) ;
2021-10-19 19:58:04 -03:00
return vm . throw_completion < JS : : TypeError > ( global_object , JS : : ErrorType : : NotAnObjectOfType , " Function " ) ;
2021-05-18 11:16:54 -03:00
}
2020-07-23 03:53:52 -03:00
2022-02-07 11:12:41 -03:00
before_initial_load_hooks . append ( [ fn = JS : : make_handle ( & function . as_function ( ) ) , & global_object ] ( auto & page_object ) - > JS : : ThrowCompletionOr < void > {
TRY ( JS : : call ( global_object , const_cast < JS : : FunctionObject & > ( * fn . cell ( ) ) , JS : : js_undefined ( ) , & page_object ) ) ;
return { } ;
2021-05-18 11:16:54 -03:00
} ) ;
return JS : : js_undefined ( ) ;
2020-07-23 03:53:52 -03:00
}
2021-10-14 12:12:53 -03:00
JS_DEFINE_NATIVE_FUNCTION ( TestWebGlobalObject : : wait_for_page_to_load )
2020-07-23 03:53:52 -03:00
{
2021-05-18 11:16:54 -03:00
// Create a new parser and immediately get its document to replace the old interpreter.
auto document = Web : : DOM : : Document : : create ( ) ;
2020-07-23 03:53:52 -03:00
2021-05-18 11:16:54 -03:00
// Run the "before" hooks
for ( auto & entry : before_initial_load_hooks )
2022-02-07 11:12:41 -03:00
TRY ( entry ( document - > interpreter ( ) . global_object ( ) ) ) ;
2020-07-23 03:53:52 -03:00
2021-05-18 11:16:54 -03:00
// Set the load hook
2021-04-14 11:36:34 -03:00
Web : : LoadRequest request ;
2021-05-18 11:16:54 -03:00
request . set_url ( next_page_to_load . value ( ) ) ;
2021-04-14 11:36:34 -03:00
2022-02-07 11:12:41 -03:00
JS : : ThrowCompletionOr < void > result = { } ;
2021-05-18 11:16:54 -03:00
auto & loader = Web : : ResourceLoader : : the ( ) ;
loader . load_sync (
2021-04-14 11:36:34 -03:00
request ,
2021-04-03 10:11:36 -03:00
[ & ] ( auto data , auto & , auto ) {
2022-02-21 17:54:21 -03:00
auto parser = Web : : HTML : : HTMLParser : : create ( document , data , " utf-8 " ) ;
2020-07-23 03:53:52 -03:00
// Now parse the HTML page.
2022-02-21 17:54:21 -03:00
parser - > run ( next_page_to_load . value ( ) ) ;
g_page_view - > set_document ( & parser - > document ( ) ) ;
2022-02-07 11:12:41 -03:00
// Note: Unhandled exceptions are just dropped here.
2020-07-23 03:53:52 -03:00
2021-05-18 11:16:54 -03:00
// Run the "after" hooks
for ( auto & entry : after_initial_load_hooks ) {
2022-02-07 11:12:41 -03:00
auto ran_or_error = entry ( document - > interpreter ( ) . global_object ( ) ) ;
if ( ran_or_error . is_error ( ) ) {
result = ran_or_error . release_error ( ) ;
2021-05-18 11:16:54 -03:00
break ;
2022-02-07 11:12:41 -03:00
}
2020-07-23 03:53:52 -03: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 15:34:31 -03:00
[ & ] ( auto & , auto ) {
2021-05-18 11:16:54 -03:00
dbgln ( " Load of resource {} failed " , next_page_to_load . value ( ) ) ;
2022-02-07 11:12:41 -03:00
result = vm . template throw_completion < JS : : TypeError > ( global_object , " Resource load failed " ) ;
2020-08-10 19:02:25 -03:00
} ) ;
2020-07-23 03:53:52 -03:00
2022-02-07 11:12:41 -03:00
if ( result . is_error ( ) )
return result . release_error ( ) ;
2021-05-18 11:16:54 -03:00
return JS : : js_undefined ( ) ;
2020-07-23 03:53:52 -03:00
}