dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / aria / tests_aria_mathml.lucb
#==============================================================================================
#
# tests_aria_mathml - region r13b's ARIA and MathML code against the reference build
#
# DESCRIPTION:
# Ladybird has no unit tests for ARIA or MathML. This runs the cases of
# tests_aria_mathml_cases (a local oracle's output: the reference Ladybird build on the
# same inputs) through the port and compares:
#
# rolename role_from_string
# roles every Role's name and categories (is_abstract_role, ...)
# classes the role object of every non-abstract Role (build_role_object, the
# generated classes): its flags, name source, table sizes, required
# context roles and owned elements in the tables' order, and implicit
# values
# role ARIAMixin::role_or_default, through role_from_role_attribute_value
# defaultrole ARIAMixin::default_role
# global has_global_aria_attribute
# idrefs parse_id_reference_list of an attribute
# states AriaData::build_data and state_or_property_to_string_value of every
# state and property
# rolejson build_role_object of an element's role and its serialize_as_json
# class the class the element factory makes for a MathML element
# hints is_presentational_hint and apply_presentational_hints of MathML
# elements
#
# Documents are web_test_create_document's (page/tests_page): the real Document
# constructor in a settings realm, of type HTML, parsed with scripting disabled.
#
#==============================================================================================
# mark: Helpers =================================================================================
## The first element of `document`, in tree order, whose local name is `tag`.
func test_aria_first(document: DomDocument*, tag: str) -> DomElement*:
let name = ak.fly_string_from_str(tag)
for node in dom_node_subtree(document.dom_node()):
if let element = as_if_dom_element(node):
let local_name = dom_element_local_name(element)
if ak.fly_string_eq_fly_string(&local_name, &name):
return element
trap("no element of that name")
## Appends a role's name, or `none`, and a newline.
func test_aria_put_role(builder: ak.StringBuilder*, role: AriaRole?):
if let present = role:
ak.string_builder_append_string_view(builder, aria_role_name(present))
else:
ak.string_builder_append(builder, "none")
ak.string_builder_append(builder, "\n")
## Appends `1` or `0`.
func test_aria_put_bool(builder: ak.StringBuilder*, value: bool):
ak.string_builder_append(builder, "1" if value else "0")
## Appends a number.
func test_aria_put_number(builder: ak.StringBuilder*, value: i64):
let text = ak.string_number_i64(value)
ak.string_builder_append_string_view(builder, ak.string_bytes_as_string_view(&text))
## Appends a String.
func test_aria_put_string(builder: ak.StringBuilder*, value: ak.String):
ak.string_builder_append_string_view(builder, ak.string_bytes_as_string_view(&value))
## The last Role and StateAndProperties (Role::window, StateAndProperties::AriaValueText).
let test_aria_last_role: i32 = (i32)AriaRole.window
let test_aria_last_state: i32 = (i32)AriaStateAndProperties.aria_value_text
## The `classes` line of one role object.
func test_aria_put_class(out: ak.StringBuilder*, role: AriaRole, object: AriaRoleType*):
ak.string_builder_append_string_view(out, aria_role_name(role))
ak.string_builder_append(out, " ")
test_aria_put_bool(out, aria_role_type_accessible_name_required(object))
test_aria_put_bool(out, aria_role_type_children_are_presentational(object))
ak.string_builder_append(out, " ")
test_aria_put_number(out, (i64)(i32)aria_role_type_name_from_source(object))
ak.string_builder_append(out, " ")
let supported_states = aria_role_type_supported_states(object)
let supported_properties = aria_role_type_supported_properties(object)
let required_states = aria_role_type_required_states(object)
let required_properties = aria_role_type_required_properties(object)
let prohibited_states = aria_role_type_prohibited_states(object)
let prohibited_properties = aria_role_type_prohibited_properties(object)
let sizes = [ak.hash_table_size(&supported_states), ak.hash_table_size(&supported_properties), ak.hash_table_size(&required_states),
ak.hash_table_size(&required_properties), ak.hash_table_size(&prohibited_states), ak.hash_table_size(&prohibited_properties)]
for (index, size) in sizes.indexed():
if index > 0:
ak.string_builder_append(out, ",")
test_aria_put_number(out, (i64)size)
ak.string_builder_append(out, " context")
let context_roles = aria_role_type_required_context_roles(object)
for context in ak.hash_table_begin_const(&context_roles):
ak.string_builder_append(out, " ")
ak.string_builder_append_string_view(out, aria_role_name(*context))
ak.string_builder_append(out, " owned")
let owned_elements = aria_role_type_required_owned_elements(object)
for owned in ak.hash_table_begin_const(&owned_elements):
ak.string_builder_append(out, " ")
ak.string_builder_append_string_view(out, aria_role_name(*owned))
ak.string_builder_append(out, " defaults")
for index in 0..=test_aria_last_state:
let state = (AriaStateAndProperties)index
let value = aria_role_type_default_value_for_property_or_state(object, state)
if value == AriaDefaultValueType.empty:
continue
ak.string_builder_append(out, " ")
ak.string_builder_append_string_view(out, aria_state_or_property_to_string(state))
ak.string_builder_append(out, "=")
match value:
.empty: trap("unreachable")
.f64_(number): test_aria_put_string(out, ak.string_number_f64(number))
.aria_orientation(orientation):
ak.string_builder_append(out, "orientation")
test_aria_put_number(out, (i64)(i32)orientation)
.aria_live(live):
ak.string_builder_append(out, "live")
test_aria_put_number(out, (i64)(i32)live)
.bool_(flag): ak.string_builder_append(out, "true" if flag else "false")
.aria_has_popup(has_popup):
ak.string_builder_append(out, "haspopup")
test_aria_put_number(out, (i64)(i32)has_popup)
ak.string_builder_append(out, "\n")
# mark: Running a case ===========================================================================
## An HTML document of `realm` parsed from `html` with scripting disabled.
func test_aria_parse(realm: Realm*, html: str) -> DomDocument*:
let document = web_test_create_document(realm, DocumentDomDocumentType.html)
let parser = html_parser_create(document, ak.sv(html), HtmlParserScriptingMode.disabled, ak.sv("UTF-8"))
html_parser_run_url(parser, dom_document_url(document))
return document
## The port's output for one case, in the oracle's format.
func test_aria_run(realm: Realm*, test_case: const TestAriaMathmlCase*) -> ak.String:
var out = ak.string_builder_new()
let mode = test_case.mode
if mode == "rolename":
test_aria_put_role(&out, aria_role_from_string(ak.sv(test_case.arguments[0])))
elif mode == "roles":
for index in 0..=test_aria_last_role:
let role = (AriaRole)index
ak.string_builder_append_string_view(&out, aria_role_name(role))
ak.string_builder_append(&out, " ")
test_aria_put_bool(&out, aria_is_abstract_role(role))
test_aria_put_bool(&out, aria_is_widget_role(role))
test_aria_put_bool(&out, aria_is_document_structure_role(role))
test_aria_put_bool(&out, aria_is_landmark_role(role))
test_aria_put_bool(&out, aria_is_live_region_role(role))
test_aria_put_bool(&out, aria_is_windows_role(role))
test_aria_put_bool(&out, aria_allows_name_from_content(role))
ak.string_builder_append(&out, "\n")
elif mode == "classes":
var data: AriaData
aria_data_construct(&data)
for index in 0..=test_aria_last_role:
let role = (AriaRole)index
if aria_is_abstract_role(role):
continue
let variants = 2 if role == AriaRole.separator else 1
for focusable in 0..<variants:
let object = aria_role_type_build_role_object(role, focusable == 1, data) else trap("build_role_object")
test_aria_put_class(&out, role, object)
elif mode == "role":
let document = test_aria_parse(realm, test_case.arguments[1])
let element = test_aria_first(document, test_case.arguments[0])
test_aria_put_role(&out, aria_mixin_role_or_default(&element.aria_mixin))
elif mode == "defaultrole":
let document = test_aria_parse(realm, test_case.arguments[1])
let element = test_aria_first(document, test_case.arguments[0])
test_aria_put_role(&out, aria_mixin_default_role(&element.aria_mixin))
elif mode == "global":
let document = test_aria_parse(realm, test_case.arguments[1])
let element = test_aria_first(document, test_case.arguments[0])
test_aria_put_bool(&out, aria_mixin_has_global_aria_attribute(&element.aria_mixin))
ak.string_builder_append(&out, "\n")
elif mode == "idrefs":
let document = test_aria_parse(realm, test_case.arguments[2])
let element = test_aria_first(document, test_case.arguments[0])
let attribute = dom_element_get_attribute(element, ak.fly_string_from_str(test_case.arguments[1]))
let list = aria_mixin_parse_id_reference_list(&element.aria_mixin, attribute)
for id in ak.vector_span_const(&list):
test_aria_put_string(&out, id)
ak.string_builder_append(&out, "\n")
ak.string_builder_append(&out, "end\n")
elif mode == "states":
let document = test_aria_parse(realm, test_case.arguments[1])
let element = test_aria_first(document, test_case.arguments[0])
let data = aria_data_build_data(&element.aria_mixin) else trap("build_data")
for index in 0..=test_aria_last_state:
let state = (AriaStateAndProperties)index
ak.string_builder_append_string_view(&out, aria_state_or_property_to_string(state))
ak.string_builder_append(&out, "=")
test_aria_put_string(&out, aria_state_or_property_to_string_value(state, *data, AriaDefaultValueType.empty) else trap("to_string_value"))
ak.string_builder_append(&out, "\n")
elif mode == "rolejson":
let document = test_aria_parse(realm, test_case.arguments[1])
let element = test_aria_first(document, test_case.arguments[0])
if let role = aria_mixin_role_or_default(&element.aria_mixin):
let data = aria_data_build_data(&element.aria_mixin) else trap("build_data")
let focusable = dom_event_target_is_focusable(element.dom_event_target())
let object = aria_role_type_build_role_object(role, focusable, *data) catch failure:
ak.string_builder_append(&out, "error ")
ak.string_builder_append(&out, failure.message)
ak.string_builder_append(&out, "\n")
return ak.string_builder_to_string_without_validation(&out)
var json = ak.string_builder_new()
var object_serializer = ak.json_object_serializer_try_create(&json) else trap("try_create")
aria_role_type_serialize_as_json(object, &object_serializer) else trap("serialize_as_json")
ak.json_object_serializer_finish(&object_serializer) else trap("finish")
ak.string_builder_append_string_view(&out, ak.string_builder_string_view(&json))
ak.string_builder_append(&out, "\n")
else:
ak.string_builder_append(&out, "none\n")
elif mode == "class":
let document = test_aria_parse(realm, test_case.arguments[1])
let element = test_aria_first(document, test_case.arguments[0])
ak.string_builder_append(&out, element.cell().vtable.class_info.name)
ak.string_builder_append(&out, "\n")
elif mode == "hints":
let document = test_aria_parse(realm, test_case.arguments[1])
let element = test_aria_first(document, test_case.arguments[0])
if let attributes = element.m_attributes:
for i in 0..<dom_named_node_map_length(attributes):
let attribute = dom_named_node_map_item(attributes, (u32)i) else trap("NamedNodeMap::item in range")
let name = dom_attr_name(attribute)
ak.string_builder_append(&out, "hint ")
html_parser_append_fly_string(&out, name)
ak.string_builder_append(&out, " 1\n" if dom_element_is_presentational_hint(element, name) else " 0\n")
var properties: ak.Vector[CssStyleProperty]
dom_element_apply_presentational_hints(element, &properties)
for property in &ak.vector_span_const(&properties):
html_parser_append_fly_string(&out, css_data.string_from_property_id(property.property_id))
ak.string_builder_append(&out, ": ")
html_parser_append_string(&out, css_style_value_to_string(property.value, css_syntax.SerializationMode.normal))
ak.string_builder_append(&out, "\n")
else:
trap("unknown mode")
return ak.string_builder_to_string_without_validation(&out)
## Runs every case of `mode`, printing each mismatch, and fails if any.
func test_aria_run_mode(mode: str):
web_initialize_strings()
let (vm, realm) = web_test_create_vm_and_settings_realm()
defer web_test_destroy_vm(vm)
var failures = 0
var count = 0
# The documents and their trees live in the VM's heap (the test's own allocator is a small
# fixed buffer).
let allocator: memory.Allocator = vm_heap(vm)
with allocator:
for test_case in &test_aria_mathml_cases:
if test_case.mode != mode:
continue
count += 1
let actual = test_aria_run(realm, test_case)
if ak.string_eq_string_view(&actual, ak.sv(test_case.expected)):
continue
failures += 1
let view = ak.string_bytes_as_string_view(&actual)
print(f"{mode} {test_case.arguments[0]} {test_case.arguments[1]} {test_case.arguments[2]}\nexpected:\n{test_case.expected}actual:\n{(str)ak.string_view_bytes(&view)}")
assert(count > 0, "no cases of the mode")
assert(failures == 0, "cases differ from the reference build")
# mark: Tests ====================================================================================
test "ARIA role_from_string matches the reference build":
test_aria_run_mode("rolename")
test "ARIA role names and the role categories of every role":
test_aria_run_mode("roles")
test "the generated role classes: build_role_object's object for every non-abstract role, with its tables and implicit values":
test_aria_run_mode("classes")
test "ARIAMixin::role_or_default: the role attribute's tokens, synonyms, required contexts, named landmarks and presentation conflicts":
test_aria_run_mode("role")
test "ARIAMixin::default_role of MathML and HTML elements":
test_aria_run_mode("defaultrole")
test "ARIAMixin::has_global_aria_attribute":
test_aria_run_mode("global")
test "ARIAMixin::parse_id_reference_list keeps the existing IDs in order":
test_aria_run_mode("idrefs")
test "AriaData parses every state and property, and state_or_property_to_string_value prints them":
test_aria_run_mode("states")
test "scenario: the role object of an element and its serialize_as_json, as the reference build":
test_aria_run_mode("rolejson")
test "the element factory makes MathMLElement, MathMLMiElement and MathMLMspaceElement":
test_aria_run_mode("class")
test "MathML presentational hints: dir, mathcolor, mathbackground, mathsize, displaystyle, scriptlevel, mathvariant, mspace sizes":
test_aria_run_mode("hints")