Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / layout / tests_layout_geometry.lucb

#==============================================================================================
#
#   tests_layout_geometry - block layout geometry of parsed documents, pinned to the reference
#                           build
#
#   DESCRIPTION:
#       Runs the cases of tests_layout_geometry_cases (a local oracle over the reference build's
#       LibWeb, luce-browser-tools/oracles/luce-browser-engine/layout_geometry; the first 19 are
#       Ladybird's own Tests/LibWeb/Layout block-and-inline tests without text, whose expected
#       layout dumps the oracle reproduces) through the port: a document is parsed and styled as
#       tests_layout_tree does, then the real Document::update_layout runs (region r51b; before
#       it, the harness ran the layout steps and LayoutState::commit by hand): the layout tree,
#       the overflow propagated to the viewport, layout indices, containing blocks, contained
#       abspos children, the root BlockFormattingContext, commit, the scroll frames, the
#       accumulated visual contexts and the paint and hit testing properties.
#
#       The expected text is the whole expectation of a Layout test: Dump's dump_tree of the
#       layout tree and of the paint tree, and the stacking context tree.
#
#       The inline harness's "lines" mode (tests_layout_inline) reads the line boxes of the
#       layout state before commit, which update_layout keeps to itself: it runs the layout steps
#       with `test_layout_geometry_lay_out`.
#
#==============================================================================================

# mark: Running the layout ======================================================================

## Document::update_layout's steps from the overflow propagation to the root formatting context, before commit, on
## a tree tests_layout_tree built: answers the layout state the tree was laid out in (tests_layout_inline's "lines").
noinline func test_layout_geometry_lay_out(document: DomDocument*, layout_state: LayoutState*):
    let layout_root = dom_document_unsafe_layout_node(document) else trap("no layout tree")
    let document_element = dom_document_document_element(document)
    if let root_element = document_element:
        if dom_element_unsafe_layout_node(root_element) != none:
            dom_propagate_overflow_to_viewport(root_element, layout_root)
            dom_propagate_scrollbar_width_to_viewport(root_element, layout_root)

    var layout_index_counter: u32 = 0
    for layout_node in layout_node_inclusive_subtree(layout_root.layout_node()):
        if let node_with_style = as_if_layout_node_with_style(layout_node):
            layout_node_with_style_set_layout_index(node_with_style, layout_index_counter)
            layout_index_counter += 1

        layout_node_recompute_containing_block(layout_node)

        let box = as_if_layout_box(layout_node) else continue

        layout_box_clear_contained_abspos_children(box)

        if not layout_node_is_absolutely_positioned(box.layout_node()):
            continue

        if let containing_block = layout_node_containing_block(box.layout_node()):
            var closest_box_that_establishes_formatting_context: LayoutBox*? = containing_block
            while let candidate = closest_box_that_establishes_formatting_context:
                if candidate == layout_root.layout_box():
                    break
                if layout_formatting_context_formatting_context_type_created_by_box(candidate) != none:
                    break
                closest_box_that_establishes_formatting_context = layout_node_containing_block(candidate.layout_node())
            let establishing_box = closest_box_that_establishes_formatting_context else trap("VERIFY: no box establishes a formatting context")
            layout_box_add_contained_abspos_child(establishing_box, box.layout_node())

    layout_state_ensure_capacity(layout_state, layout_index_counter)

    let viewport_rect = gfx.CssPixelRect(gfx.CssPixelPoint(gfx.CssPixels.from_int(0), gfx.CssPixels.from_int(0)), gfx.CssPixelSize(gfx.CssPixels.from_int(800), gfx.CssPixels.from_int(600)))
    let viewport_state = layout_state_get_mutable(layout_state, layout_root.layout_node_with_style())
    layout_state_used_values_set_content_width(viewport_state, gfx.css_pixel_rect_width(&viewport_rect))
    layout_state_used_values_set_content_height(viewport_state, gfx.css_pixel_rect_height(&viewport_rect))

    if let root_element = document_element:
        if let root_layout_node = dom_element_unsafe_layout_node(root_element):
            let icb_state = layout_state_get_mutable(layout_state, as_layout_node_with_style_and_box_model_metrics(root_layout_node).layout_node_with_style())
            layout_state_used_values_set_content_width(icb_state, gfx.css_pixel_rect_width(&viewport_rect))

    let available_space = LayoutAvailableSpace(width = layout_available_size_make_definite(gfx.css_pixel_rect_width(&viewport_rect)),
                                               height = layout_available_size_make_definite(gfx.css_pixel_rect_height(&viewport_rect)))

    var root_formatting_context: LayoutBlockFormattingContext = ---
    layout_block_formatting_context_construct(&root_formatting_context, layout_state, LayoutMode.normal, layout_root.layout_block_container(), none)
    # ~BlockFormattingContext() runs when the context goes out of scope.
    defer layout_block_formatting_context_destruct(&root_formatting_context)
    layout_formatting_context_run(&root_formatting_context.formatting_context, available_space)

# mark: Committing and dumping =====================================================================

## Document::update_layout, the real one (region r51b): the layout tree, the layout, LayoutState::commit, the scroll
## frames, the accumulated visual contexts and the paint and hit testing properties of a parsed and styled document.
noinline func test_layout_geometry_update_layout(document: DomDocument*):
    dom_document_update_layout(document, DomUpdateLayoutReason.debugging)

## The three dumps of a Layout test's expectation (test-web's LayoutTree | PaintTree | StackingContextTree):
## Dump's dump_tree of the layout tree and of the paint tree, and StackingContext::dump of the stacking context tree,
## separated by blank lines.
func test_layout_geometry_dump(builder: ak.StringBuilder*, document: DomDocument*):
    let layout_root = dom_document_unsafe_layout_node(document) else trap("no layout tree")
    dump_tree_string_builder_layout_node(builder, layout_root.layout_node())

    ak.string_builder_append(builder, "\n")
    let viewport_paintable = dom_document_unsafe_paintable(document) else trap("no paint tree")
    dump_tree_string_builder_paint_paintable(builder, viewport_paintable.paint_paintable())

    ak.string_builder_append(builder, "\n")
    paint_viewport_paintable_build_stacking_context_tree_if_needed(viewport_paintable)
    if let stacking_context = paint_paintable_box_stacking_context(viewport_paintable.paint_paintable_box()):
        paint_stacking_context_dump(stacking_context, builder)

## Runs one case and answers its output: the three dumps, right-trimmed, each line indented by two spaces (the
## oracle's put_lines).
func test_layout_geometry_run_case(heap: gc.Heap*, realm: Realm*, test_case: const TestLayoutGeometryCase*) -> ak.String:
    var out = ak.string_builder_new()
    var (document, navigable) = test_layout_tree_parse_and_style(heap, realm, test_case.html, test_case.css)
    defer gc.root_release(&navigable)
    test_layout_geometry_update_layout(document)
    gc.heap_collect_garbage(heap)
    var builder = ak.string_builder_new()
    test_layout_geometry_dump(&builder, document)
    test_layout_tree_put_lines(&out, ak.string_builder_string_view(&builder))
    return ak.string_builder_to_string_without_validation(&out)

# mark: Tests ===================================================================================

test "block layout geometry (widths, heights, margins and their collapsing, floats, clearance, positioned boxes, min/max sizes, quirks) matches the reference build":
    web_initialize_strings()
    test_fonts_setup()
    let vm = web_test_create_vm()
    defer web_test_destroy_vm(vm)
    var (realm, realm_root) = test_layout_tree_window_realm(vm)
    defer gc.root_release(&realm_root)
    let heap = vm_heap(vm)
    var passed: usize = 0
    var failed: usize = 0
    let allocator: memory.Allocator = heap
    with allocator:
        test_page_client_color_scheme = CssPreferredColorScheme.light
        defer test_page_client_color_scheme = CssPreferredColorScheme.auto
        test_media_invalidation_install_user_agent_sheets(realm)
        defer test_style_computer_release_user_agent_sheets()
        for test_case in &test_layout_geometry_cases:
            let actual = test_layout_geometry_run_case(heap, realm, test_case)
            let actual_view = ak.string_bytes_as_string_view(&actual)
            if ak.string_view_equals(&actual_view, test_case.expected):
                passed += 1
            else:
                failed += 1
                print(f"layout geometry case mismatch: {test_case.html} / {test_case.css}")
                print(f"expected:\n{test_case.expected}actual:\n{ak.string_view_text(actual_view)}")
    print(f"layout geometry: {passed} passed, {failed} failed")
    assert(failed == 0)
    assert(passed > 0)