dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / html / tests_history.lucb
#==============================================================================================
#
# tests_history - History as data, the Navigation entry list, NavigationHistoryEntry,
# BeforeUnloadEvent and the structured (de)serialization of history states
#
# DESCRIPTION:
# Ladybird tests these through Tests/LibWeb/Text (script), so these pin the C++ region p2r
# ports: a History starts at index 0, length 0 and state null, and Document sets its index
# and length; can_have_its_url_rewritten compares the URL components the spec lists per
# scheme; a Navigation keeps no entries while its document is the initial about:blank (or
# its origin is opaque), and otherwise makes one NavigationHistoryEntry per session
# history entry, its current entry the initial one's; a BeforeUnloadEvent is a trusted
# Event with an empty return value; null and undefined serialize to their one-byte records
# and deserialize back. The helpers are tests_html_elements_2's, tests_html_browsing's and
# bindings/tests_bindings'.
#
#==============================================================================================
# mark: History =================================================================================
test "History: created with index 0, length 0 and a null state; Document's setters and the state":
let (vm, realm, document) = test_elements_setup()
defer web_test_destroy_vm(vm)
let allocator: memory.Allocator = vm_heap(vm)
with allocator:
let history = html_history_create(realm)
assert(is_html_history(history) and is_bind_platform_object(history) and not is_dom_event_target(history))
assert(html_history_index(history) == 0 and html_history_length(history) == 0)
assert(js_value_is_null(html_history_unsafe_state(history)))
html_history_set_index(history, 2)
html_history_set_length(history, 3)
assert(html_history_index(history) == 2 and html_history_length(history) == 3)
html_history_set_state(history, js_undefined())
assert(js_value_is_undefined(html_history_unsafe_state(history)))
# Document::history() makes one on first use and keeps it.
let document_history = dom_document_history(document)
assert(dom_document_history(document) == document_history and document_history != history)
## can_have_its_url_rewritten for a document at `document_url` and `target_url`.
func test_history_can_rewrite(document: DomDocument*, document_url: str, target_url: str) -> bool:
dom_document_set_url(document, web_url.parser_basic_parse(ak.sv(document_url)) else trap("the document URL parses"))
return html_can_have_its_url_rewritten(document, web_url.parser_basic_parse(ak.sv(target_url)) else trap("the target URL parses"))
test "can_have_its_url_rewritten: scheme, credentials, host and port must match; then per scheme":
let (vm, _, document) = test_elements_setup()
defer web_test_destroy_vm(vm)
let allocator: memory.Allocator = vm_heap(vm)
with allocator:
# http(s): any path, query and fragment.
assert(test_history_can_rewrite(document, "https://a.test/x?q#f", "https://a.test/y/z?r#g"))
assert(not test_history_can_rewrite(document, "https://a.test/x", "http://a.test/x"))
assert(not test_history_can_rewrite(document, "https://a.test/x", "https://b.test/x"))
assert(not test_history_can_rewrite(document, "https://a.test/x", "https://a.test:8443/x"))
assert(not test_history_can_rewrite(document, "https://u@a.test/x", "https://a.test/x"))
assert(not test_history_can_rewrite(document, "https://u:p@a.test/x", "https://u:q@a.test/x"))
# file: the path must match; query and fragment may differ.
assert(test_history_can_rewrite(document, "file:///tmp/a.html", "file:///tmp/a.html?x#y"))
assert(not test_history_can_rewrite(document, "file:///tmp/a.html", "file:///tmp/b.html"))
# Other schemes: only the fragment may differ.
assert(test_history_can_rewrite(document, "about:blank", "about:blank#x"))
assert(not test_history_can_rewrite(document, "about:blank", "about:blank?x"))
assert(not test_history_can_rewrite(document, "about:blank", "about:srcdoc"))
assert(not test_history_can_rewrite(document, "data:text/html,a", "data:text/html,a?"))
# mark: Navigation ==============================================================================
test "Navigation: no entries for the initial about:blank; one NavigationHistoryEntry per entry otherwise":
let vm = web_test_create_vm()
defer web_test_destroy_vm(vm)
_ = web_test_install_agent(vm)
web_test_install_event_loop_plugin()
defer web_test_remove_event_loop_plugin()
let allocator: memory.Allocator = vm_heap(vm)
with allocator:
let (test_page, traversable) = test_browsing_page_with_traversable(vm)
_ = test_page
let document = html_navigable_active_document(traversable.html_navigable()) else trap("no document")
let window = dom_document_window(document) else trap("no window")
let navigation = html_window_navigation(window)
assert(html_window_navigation(window) == navigation and is_html_navigation(navigation) and is_dom_event_target(navigation))
assert(navigation.m_current_entry_index == -1 and html_navigation_ongoing_navigate_event(navigation) == none)
html_navigation_set_focus_changed_during_ongoing_navigation(navigation, true)
assert(html_navigation_focus_changed_during_ongoing_navigation(navigation))
html_navigation_set_focus_changed_during_ongoing_navigation(navigation, false)
let first = html_session_history_entry_create()
let second = html_session_history_entry_create()
var shes: ak.Vector[HtmlSessionHistoryEntry*]
ak.vector_append_t(&shes, first)
ak.vector_append_t(&shes, second)
# The initial about:blank has entries and events disabled; the AD-HOC flag is reset all the same.
assert(dom_document_is_initial_about_blank(document) and html_navigation_has_entries_and_events_disabled(navigation))
html_navigation_set_was_initial_about_blank_opened(navigation, true)
html_navigation_initialize_the_navigation_api_entries_for_a_new_document(navigation, shes, second)
assert(ak.vector_is_empty(&navigation.m_entry_list) and navigation.m_current_entry_index == -1)
assert(not navigation.m_was_initial_about_blank_opened)
assert(html_navigation_current_entry(navigation) == none)
# An opaque origin keeps them disabled.
dom_document_set_is_initial_about_blank(document, false)
dom_document_set_origin(document, web_url.origin_create_opaque())
assert(html_navigation_has_entries_and_events_disabled(navigation))
# A document with a tuple origin: one entry per session history entry, the current one the initial SHE's.
let url = web_url.parser_basic_parse(ak.sv("https://a.test/")) else trap("parses")
dom_document_set_origin(document, web_url.url_origin(&url))
assert(not html_navigation_has_entries_and_events_disabled(navigation))
html_navigation_initialize_the_navigation_api_entries_for_a_new_document(navigation, shes, second)
assert(ak.vector_size(&navigation.m_entry_list) == 2 and navigation.m_current_entry_index == 1)
let current = html_navigation_current_entry(navigation) else trap("a current entry")
assert(is_html_navigation_history_entry(current) and html_navigation_history_entry_session_history_entry(current) == second)
assert(html_navigation_get_the_navigation_api_entry_index(navigation, first) == 0)
assert(html_navigation_get_the_navigation_api_entry_index(navigation, html_session_history_entry_create()) == -1)
# The entries are kept by the navigation through a collection.
let weak = gc.make_weak(current)
test_html_forms_collect(vm)
assert(web_test_alive(&weak))
# mark: BeforeUnloadEvent =======================================================================
test "BeforeUnloadEvent::create: a trusted Event with an empty return value":
let (vm, realm, _) = test_elements_setup()
defer web_test_destroy_vm(vm)
let allocator: memory.Allocator = vm_heap(vm)
with allocator:
let event = html_before_unload_event_create(realm, html_event_names_beforeunload)
assert(is_dom_event(event) and is_html_before_unload_event(event) and not is_html_toggle_event(event))
assert(dom_event_is_trusted(event.dom_event()) and not dom_event_cancelable(event.dom_event()))
assert(test_fly_is(dom_event_type(event.dom_event()), "beforeunload"))
let empty = html_before_unload_event_return_value(event)
assert(ak.string_is_empty(&empty))
html_before_unload_event_set_return_value(event, ak.string_from_str("stay"))
assert(test_structure_is(html_before_unload_event_return_value(event), "stay"))
var init = DomEventInit()
init.cancelable = true
let cancelable = html_before_unload_event_create(realm, html_event_names_beforeunload, init)
assert(dom_event_cancelable(cancelable.dom_event()))
# mark: Structured serialization ================================================================
test "StructuredSerializeForStorage and StructuredDeserialize: undefined and null as one-byte records":
let (vm, realm, _) = test_elements_setup()
defer web_test_destroy_vm(vm)
let allocator: memory.Allocator = vm_heap(vm)
with allocator:
let null_record = html_structured_serialize_for_storage(vm, js_null()) else trap("serializes")
let undefined_record = html_structured_serialize_for_storage(vm, js_undefined()) else trap("serializes")
assert(ak.vector_size(&null_record) == 1 and *ak.vector_at_const(&null_record, 0) == 2)
assert(ak.vector_size(&undefined_record) == 1 and *ak.vector_at_const(&undefined_record, 0) == 1)
assert(js_value_is_null(html_structured_deserialize(vm, null_record, realm) else trap("deserializes")))
assert(js_value_is_undefined(html_structured_deserialize(vm, undefined_record, realm) else trap("deserializes")))
# A session history entry's states.
let she = html_session_history_entry_create()
assert(js_value_is_null(html_structured_deserialize(vm, html_session_history_entry_classic_history_api_state(she), realm) else trap("deserializes")))