Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / css / tests_computed_properties_2.lucb

#==============================================================================================
#
#   tests_computed_properties_2 - focused tests of the property store, ComputedValues and
#                                 StyleProperty
#
#   DESCRIPTION:
#       The parts of region r40 the reader oracle (tests_computed_properties) does not reach:
#       ComputedProperties' importance, inheritance and animation flags, `property()` with
#       animated and transitioned values, revert, equality, for_each_property, the pseudo-class
#       bitmap, `content` with quotes (the C++ behaviour worked through by hand), the mutable
#       ComputedValues setters (the border-radius flag), inherit_from, clone_inherited_values,
#       the small value structs, to_gfx_scaling_mode and StyleProperty::operator==.
#
#==============================================================================================

# mark: Helpers =================================================================================

## A ComputedProperties with every longhand at its initial value (as StyleComputer::create_document_style starts).
func tcp2_initial_style(vm: Vm*) -> CssComputedProperties*:
    let style = heap_allocate_css_computed_properties(vm_heap(vm))
    for i in 0..<css_data.number_of_longhand_properties:
        let property_id = (css_data.PropertyId)(i + (usize)css_data.first_longhand_property_id)
        css_computed_properties_set_property(style, property_id, css_property_initial_value(property_id))
    return style

## A px LengthStyleValue as a StyleValue.
func tcp2_px(value: f64) -> CssStyleValue*:
    return css_length_style_value_create(css_length_make_px_f64(value)).css_style_value()

## Whether a String holds `text`.
func tcp2_is(string: ak.String, text: str) -> bool:
    return ak.string_eq_string_view(&string, ak.sv(text))

## The lambda of a for_each_property test: counts the properties and remembers the last one.
struct Tcp2PropertyCounter: CssComputedPropertiesPropertyCallback:
    var count: usize
    var last: css_data.PropertyId?

    func call(property_id: css_data.PropertyId, value: CssStyleValue*):
        _ = value
        self.count += 1
        self.last = property_id

# mark: ComputedProperties ======================================================================

test "ComputedProperties: the important, inherited and animation flags":
    web_initialize_strings()
    let (vm, realm) = web_test_create_vm_and_settings_realm()
    _ = realm
    defer web_test_destroy_vm(vm)
    let allocator: memory.Allocator = vm_heap(vm)
    with allocator:
        let style = tcp2_initial_style(vm)
        let width = css_data.PropertyId.width
        assert(not css_computed_properties_is_property_important(style, width))
        css_computed_properties_set_property(style, width, tcp2_px(10.0), CssComputedPropertiesInherited.yes, css_syntax.Important.yes)
        assert(css_computed_properties_is_property_important(style, width) and css_computed_properties_is_property_inherited(style, width))
        # The neighbouring longhands' bits are untouched.
        assert(not css_computed_properties_is_property_important(style, css_data.PropertyId.white_space_collapse))
        assert(not css_computed_properties_is_property_important(style, css_data.PropertyId.widows))
        css_computed_properties_set_property_important(style, width, css_syntax.Important.no)
        css_computed_properties_set_property_inherited(style, width, CssComputedPropertiesInherited.no)
        assert(not css_computed_properties_is_property_important(style, width) and not css_computed_properties_is_property_inherited(style, width))
        # The first and last longhands (bit 0 of byte 0, the last byte's bit).
        css_computed_properties_set_property_important(style, css_data.first_longhand_property_id, css_syntax.Important.yes)
        css_computed_properties_set_property_inherited(style, css_data.last_longhand_property_id, CssComputedPropertiesInherited.yes)
        assert(css_computed_properties_is_property_important(style, css_data.first_longhand_property_id))
        assert(css_computed_properties_is_property_inherited(style, css_data.last_longhand_property_id))
        assert(style.m_property_important[0] == 1 and style.m_property_inherited[(313 // 8)] == (u8)1 << (u8)(313 % 8))

        # set_property_without_modifying_flags keeps the flags and clears the font caches for font properties.
        css_computed_properties_set_property(style, width, tcp2_px(5.0), CssComputedPropertiesInherited.yes, css_syntax.Important.yes)
        css_computed_properties_set_property_without_modifying_flags(style, width, tcp2_px(6.0))
        assert(css_computed_properties_is_property_important(style, width) and css_computed_properties_is_property_inherited(style, width))
        assert(tcp2_is(css_style_value_to_string(css_computed_properties_property(style, width), css_syntax.SerializationMode.normal), "6px"))

test "ComputedProperties: animated and transitioned values, revert, equality, for_each_property":
    web_initialize_strings()
    let (vm, realm) = web_test_create_vm_and_settings_realm()
    _ = realm
    defer web_test_destroy_vm(vm)
    let allocator: memory.Allocator = vm_heap(vm)
    with allocator:
        let style = tcp2_initial_style(vm)
        let opacity = css_data.PropertyId.opacity
        let base = css_number_style_value_create(0.5).css_style_value()
        let animated = css_number_style_value_create(0.25).css_style_value()
        css_computed_properties_set_property(style, opacity, base)
        css_computed_properties_set_animated_property(style, opacity, animated, CssAnimatedPropertyResultOfTransition.no)
        assert(css_computed_properties_property(style, opacity) == animated)
        assert(css_computed_properties_property(style, opacity, CssComputedPropertiesWithAnimationsApplied.no) == base)
        # Important properties override animated but not transitioned properties.
        css_computed_properties_set_property_important(style, opacity, css_syntax.Important.yes)
        assert(css_computed_properties_property(style, opacity) == base)
        css_computed_properties_set_animated_property(style, opacity, animated, CssAnimatedPropertyResultOfTransition.yes)
        assert(css_computed_properties_is_animated_property_result_of_transition(style, opacity))
        assert(css_computed_properties_property(style, opacity) == animated)

        # reset_non_inherited_animated_properties drops the animated values that are not inherited.
        let color = css_data.PropertyId.color
        css_computed_properties_set_animated_property(style, color, css_keyword_style_value_create(css_data.Keyword.currentcolor).css_style_value(), CssAnimatedPropertyResultOfTransition.no, CssComputedPropertiesInherited.yes)
        css_computed_properties_reset_non_inherited_animated_properties(style)
        var animated_values = css_computed_properties_animated_property_values(style)
        assert(ak.hash_map_size(&animated_values) == 1 and ak.hash_map_get_k(&animated_values, color) != none)
        css_computed_properties_remove_animated_property(style, color)
        animated_values = css_computed_properties_animated_property_values(style)
        assert(ak.hash_map_size(&animated_values) == 0)

        # revert_property copies the value and the flags of the style reverted to.
        let other = tcp2_initial_style(vm)
        assert(not css_computed_properties_eq(style, other))
        css_computed_properties_revert_property(style, opacity, other)
        assert(not css_computed_properties_is_property_important(style, opacity))
        assert(css_computed_properties_eq(style, other))
        # Equal values compare equal even as distinct objects; a different type does not.
        css_computed_properties_set_property(other, css_data.PropertyId.width, tcp2_px(3.0))
        css_computed_properties_set_property(style, css_data.PropertyId.width, tcp2_px(3.0))
        assert(css_computed_properties_eq(style, other))
        css_computed_properties_set_property(style, css_data.PropertyId.width, css_keyword_style_value_create(css_data.Keyword.auto).css_style_value())
        assert(not css_computed_properties_eq(style, other))

        var counter = Tcp2PropertyCounter()
        css_computed_properties_for_each_property(style, &counter)
        assert(counter.count == css_data.number_of_longhand_properties)
        assert(counter.last == css_data.last_longhand_property_id)

        # The pseudo-class matches attempted while computing the style.
        assert(not css_computed_properties_has_attempted_match_against_pseudo_class(style, css_data.PseudoClass.hover))
        var bitmap = CssPseudoClassBitmap()
        css_pseudo_class_bitmap_set(&bitmap, css_data.PseudoClass.hover, true)
        css_computed_properties_set_attempted_pseudo_class_matches(style, bitmap)
        assert(css_computed_properties_has_attempted_match_against_pseudo_class(style, css_data.PseudoClass.hover))
        assert(not css_computed_properties_has_attempted_match_against_pseudo_class(style, css_data.PseudoClass.focus))

        # display_before_box_type_transformation starts at InitialValues::display().
        let before = css_computed_properties_display_before_box_type_transformation(style)
        assert(tcp2_is(css_display_to_string(&before), "inline"))

## `content: open-quote "x" close-quote close-quote no-open-quote open-quote` with `quotes`, nesting from `level`.
func tcp2_content(vm: Vm*, quotes: CssStyleValue*?, level: u32) -> CssComputedPropertiesContentDataAndQuoteNestingLevel:
    let style = tcp2_initial_style(vm)
    if let q = quotes:
        css_computed_properties_set_property(style, css_data.PropertyId.quotes, q)
    var items: ak.Vector[CssStyleValue*]
    ak.vector_append_t(&items, css_keyword_style_value_create(css_data.Keyword.open_quote).css_style_value())
    ak.vector_append_t(&items, css_string_style_value_create(ak.fly_string_from_str("x")).css_style_value())
    ak.vector_append_t(&items, css_keyword_style_value_create(css_data.Keyword.close_quote).css_style_value())
    ak.vector_append_t(&items, css_keyword_style_value_create(css_data.Keyword.close_quote).css_style_value())
    ak.vector_append_t(&items, css_keyword_style_value_create(css_data.Keyword.no_open_quote).css_style_value())
    ak.vector_append_t(&items, css_keyword_style_value_create(css_data.Keyword.open_quote).css_style_value())
    let list = css_style_value_list_create(items, CssStyleValueListSeparator.space)
    css_computed_properties_set_property(style, css_data.PropertyId.content, css_content_style_value_create(list, none).css_style_value())
    # Without counters, `content` never reads the element.
    var element_reference: DomAbstractElement = ---
    return css_computed_properties_content(style, &element_reference, level)

## The strings of a ContentData, concatenated.
func tcp2_content_text(data: const CssContentData*) -> ak.String:
    var builder = ak.string_builder_new()
    for item in ak.vector_span_const(&data.data):
        match item:
            .string(text):
                ak.string_builder_append_string_view(&builder, ak.string_bytes_as_string_view(&text))
            _:
                ak.string_builder_append(&builder, "<image>")
    return ak.string_builder_to_string_without_validation(&builder)

test "ComputedProperties::content: quotes, nesting and the keywords":
    web_initialize_strings()
    let (vm, realm) = web_test_create_vm_and_settings_realm()
    _ = realm
    defer web_test_destroy_vm(vm)
    let allocator: memory.Allocator = vm_heap(vm)
    with allocator:
        # quotes: auto (the initial value): “x” then a close-quote at depth 0 is ignored; no-open-quote and open-quote
        # nest to depth 2 and the second level's quote is ‘.
        let auto_quotes = tcp2_content(vm, none, 0)
        assert(auto_quotes.content_data.type_ == CssContentDataType.list)
        assert(tcp2_is(tcp2_content_text(&auto_quotes.content_data), "“x”‘"))
        assert(auto_quotes.final_quote_nesting_level == 2)
        # Starting nested: the first open-quote is a second-level one and both close-quotes count.
        let nested = tcp2_content(vm, none, 1)
        assert(tcp2_is(tcp2_content_text(&nested.content_data), "‘x’”‘"))
        assert(nested.final_quote_nesting_level == 2)
        # quotes: none renders no quote marks but still nests.
        let none_quotes = tcp2_content(vm, css_keyword_style_value_create(css_data.Keyword.none_).css_style_value(), 0)
        assert(tcp2_is(tcp2_content_text(&none_quotes.content_data), "x"))
        assert(none_quotes.final_quote_nesting_level == 2)
        # Specified pairs: the last pair repeats when nesting deeper.
        var strings: ak.Vector[CssStyleValue*]
        ak.vector_append_t(&strings, css_string_style_value_create(ak.fly_string_from_str("<")).css_style_value())
        ak.vector_append_t(&strings, css_string_style_value_create(ak.fly_string_from_str(">")).css_style_value())
        let pairs = css_style_value_list_create(strings, CssStyleValueListSeparator.space).css_style_value()
        let specified = tcp2_content(vm, pairs, 3)
        assert(tcp2_is(tcp2_content_text(&specified.content_data), "<x>><"))
        assert(specified.final_quote_nesting_level == 4)

        # content: none / normal, and the initial value (normal).
        let style = tcp2_initial_style(vm)
        var element_reference: DomAbstractElement = ---
        let normal = css_computed_properties_content(style, &element_reference, 4)
        assert(normal.content_data.type_ == CssContentDataType.normal and normal.final_quote_nesting_level == 4)
        css_computed_properties_set_property(style, css_data.PropertyId.content, css_keyword_style_value_create(css_data.Keyword.none_).css_style_value())
        let none_content = css_computed_properties_content(style, &element_reference, 0)
        assert(none_content.content_data.type_ == CssContentDataType.none_ and ak.vector_is_empty(&none_content.content_data.data))

# mark: ComputedValues ==========================================================================

test "MutableComputedValues: setters, border radii, inherit_from and clone_inherited_values":
    let values = css_computed_values_create()
    let mutable_values = (CssMutableComputedValues*)values
    css_mutable_computed_values_set_font_size(mutable_values, gfx.css_pixels_from_int(20))
    css_mutable_computed_values_set_color(mutable_values, gfx.color_new_named_color(gfx.ColorNamedColor.red))
    css_mutable_computed_values_set_opacity(mutable_values, 0.5)
    css_mutable_computed_values_set_direction(mutable_values, css_data.Direction.rtl)
    assert(gfx.eq_css_pixels_int(css_computed_values_font_size(values), 20))
    assert(css_computed_values_opacity(values) == 0.5)
    assert(css_computed_values_inline_axis_is_reverse(values) and not css_computed_values_block_axis_is_reverse(values))
    css_mutable_computed_values_set_writing_mode(mutable_values, css_data.WritingMode.sideways_lr)
    assert(not css_computed_values_inline_axis_is_reverse(values))
    css_mutable_computed_values_set_writing_mode(mutable_values, css_data.WritingMode.vertical_rl)
    assert(css_computed_values_block_axis_is_reverse(values))

    # An initial radius on values without non-initial radii is not stored; a non-initial one sets the flag, after
    # which initial radii are stored too.
    css_mutable_computed_values_set_border_top_left_radius(mutable_values, css_border_radius_data_new())
    assert(not css_computed_values_has_noninitial_border_radii(values))
    let radius = CssBorderRadiusData(horizontal_radius = css_length_percentage_new_css_length(css_length_make_px_f64(4.0)), vertical_radius = css_length_percentage_new_css_percentage(css_percentage_new(10.0)))
    assert(not css_border_radius_data_is_initial(&radius))
    css_mutable_computed_values_set_border_top_left_radius(mutable_values, radius)
    assert(css_computed_values_has_noninitial_border_radii(values))
    css_mutable_computed_values_set_border_bottom_right_radius(mutable_values, css_border_radius_data_new())
    let stored = css_computed_values_border_top_left_radius(values)
    assert(not css_border_radius_data_is_initial(&stored))
    # A radius of 0em is not initial (the length must be px).
    let em_radius = CssBorderRadiusData(horizontal_radius = css_length_percentage_new_css_length(css_length_new(0.0, css_data.LengthUnit.em)), vertical_radius = css_length_percentage_new_css_length(css_length_make_px_f64(0.0)))
    assert(not css_border_radius_data_is_initial(&em_radius))

    # The mutable border accessors.
    css_mutable_computed_values_border_top(mutable_values).width = gfx.css_pixels_from_int(3)
    assert(gfx.eq_css_pixels_int(css_computed_values_border_top(values).width, 3))

    # inherit_from copies only the inherited values; clone_inherited_values makes a fresh ComputedValues with them.
    let other = css_computed_values_create()
    css_mutable_computed_values_inherit_from((CssMutableComputedValues*)other, values)
    assert(gfx.eq_css_pixels_int(css_computed_values_font_size(other), 20) and css_computed_values_opacity(other) == 1.0)
    let clone = css_computed_values_clone_inherited_values(values)
    assert(css_computed_values_direction(clone) == css_data.Direction.rtl and css_computed_values_opacity(clone) == 1.0)
    let clone_color = css_computed_values_color(clone)
    assert(gfx.color_value(&clone_color) == 0xffff0000)

    # Donor bug kept: rx() answers ry.
    css_mutable_computed_values_set_ry(mutable_values, css_length_percentage_or_auto_new(css_length_percentage_new_css_length(css_length_make_px_f64(7.0))))
    let rx = css_computed_values_rx(values)
    assert(tcp2_is(css_length_percentage_or_auto_to_string(&rx, css_syntax.SerializationMode.normal), "7px"))

test "ComputedValues.h value structs: SVGPaint, references, WillChange, containment, touch-action, scaling":
    let url = css_url_new(ak.string_from_str("#paint"))
    var paint: CssSvgPaint = ---
    css_svg_paint_construct_css_url(&paint, url, gfx.color_new_named_color(gfx.ColorNamedColor.blue))
    assert(css_svg_paint_is_url(&paint) and not css_svg_paint_is_color(&paint))
    let paint_url = css_svg_paint_as_url(&paint)
    assert(ak.string_eq_string_view(&paint_url.m_url, ak.sv("#paint")))
    assert(css_svg_paint_fallback_color(&paint) != none)
    css_svg_paint_construct_color(&paint, gfx.color_new_named_color(gfx.ColorNamedColor.green))
    let paint_color = css_svg_paint_as_color(&paint)
    assert(css_svg_paint_is_color(&paint) and gfx.color_value(&paint_color) == 0xff00ff00 and css_svg_paint_fallback_color(&paint) == none)

    var mask: CssMaskReference = ---
    css_mask_reference_construct(&mask, url)
    let mask_url = css_mask_reference_url(&mask)
    assert(ak.string_eq_string_view(&mask_url.m_url, ak.sv("#paint")))
    var clip_path: CssClipPathReference = ---
    css_clip_path_reference_construct_css_url(&clip_path, url)
    assert(css_clip_path_reference_is_url(&clip_path) and not css_clip_path_reference_is_basic_shape(&clip_path))
    let clip_path_url = css_clip_path_reference_url(&clip_path)
    assert(ak.string_eq_string_view(&clip_path_url.m_url, ak.sv("#paint")))

    var entries: ak.Vector[CssWillChangeWillChangeEntry]
    ak.vector_append_t(&entries, CssWillChangeWillChangeEntry.property_id(css_data.PropertyId.opacity))
    var will_change: CssWillChange = ---
    css_will_change_construct_vector(&will_change, entries)
    assert(not css_will_change_is_auto(&will_change) and css_will_change_has_property(&will_change, css_data.PropertyId.opacity))
    assert(not css_will_change_has_contents(&will_change) and not css_will_change_has_property(&will_change, css_data.PropertyId.transform))

    var containment = CssContainment()
    assert(css_containment_is_empty(&containment))
    containment.paint_containment = true
    assert(not css_containment_is_empty(&containment))
    var container_type = CssContainerType()
    assert(css_container_type_is_empty(&container_type))
    container_type.is_scroll_state_container = true
    assert(not css_container_type_is_empty(&container_type))

    let none_ = css_touch_action_data_none()
    let all = css_touch_action_data_new()
    assert(not none_.allow_left and not none_.allow_other and all.allow_left and all.allow_pinch_zoom and all.allow_other)

    let source = gfx.IntSize(m_width = 100, m_height = 100)
    let smaller = gfx.IntSize(m_width = 50, m_height = 50)
    let wider = gfx.IntSize(m_width = 200, m_height = 50)
    assert(css_to_gfx_scaling_mode(css_data.ImageRendering.auto, source, smaller) == gfx.ScalingMode.bilinear_mipmap)
    assert(css_to_gfx_scaling_mode(css_data.ImageRendering.smooth, source, wider) == gfx.ScalingMode.bilinear)
    assert(css_to_gfx_scaling_mode(css_data.ImageRendering.high_quality, source, source) == gfx.ScalingMode.bilinear)
    assert(css_to_gfx_scaling_mode(css_data.ImageRendering.pixelated, source, smaller) == gfx.ScalingMode.nearest_neighbor)
    assert(css_to_gfx_scaling_mode(css_data.ImageRendering.crisp_edges, source, wider) == gfx.ScalingMode.nearest_neighbor)

test "StyleProperty::operator==: importance, property and value":
    with memory.heap:
        let a = CssStyleProperty(property_id = css_data.PropertyId.width, value = tcp2_px(1.0))
        let b = CssStyleProperty(property_id = css_data.PropertyId.width, value = tcp2_px(1.0))
        assert(css_style_property_eq(&a, b))
        assert(not css_style_property_eq(&a, CssStyleProperty(important = css_syntax.Important.yes, property_id = css_data.PropertyId.width, value = tcp2_px(1.0))))
        assert(not css_style_property_eq(&a, CssStyleProperty(property_id = css_data.PropertyId.height, value = tcp2_px(1.0))))
        assert(not css_style_property_eq(&a, CssStyleProperty(property_id = css_data.PropertyId.width, value = tcp2_px(2.0))))