Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / layout / tests_layout_grid.lucb

#==============================================================================================
#
#   tests_layout_grid - grid layout of parsed documents, pinned to the reference build
#
#   DESCRIPTION:
#       Runs the cases of tests_layout_grid_cases through the port with the harness of
#       tests_layout_geometry (layout, commit, and the three dumps of a Layout test). They are
#       Ladybird's own Tests/LibWeb/Layout tests whose layout first reached the
#       GridFormattingContext (all of Layout/input/grid and the grid tests around it) and
#       documents written for region r47; the local oracle that wrote the table
#       (luce-browser-tools, oracles/luce-browser-engine/layout_geometry, cases_grid.txt and
#       own_grid_cases.py) runs the reference build's LibWeb on the same documents. They lay
#       out grid containers with fixed, percentage, flexible, intrinsic, fit-content and
#       minmax tracks, gutters, auto-fill / auto-fit repetitions, line numbers, named lines and
#       areas, auto-placement (sparse and dense, rows and columns, order), spanning items,
#       content distribution and self-alignment, auto margins, item min/max sizes and aspect
#       ratios, automatic minimum sizes, the intrinsic sizes of grid containers, absolutely
#       positioned children in the augmented grid, and nested grids.
#
#==============================================================================================

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

test "grid layout (placement, track sizing, alignment, intrinsic sizes, abspos children) 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_grid_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"grid layout 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"grid layout: {passed} passed, {failed} failed")
    assert(failed == 0)
    assert(passed > 0)

test "OccupationGrid: sizes, occupied cells and the auto-placement search in both flows":
    # OccupationGrid(0, 0): `max(0, columns_count - 1)` is AK::max<int>, so the wrapped size_t -1 is clamped to 0.
    var grid: LayoutOccupationGrid = ---
    layout_occupation_grid_construct_usize(&grid, 0, 0)
    assert(layout_occupation_grid_max_column_index(&grid) == 0 and layout_occupation_grid_max_row_index(&grid) == 0)
    assert(layout_occupation_grid_column_count(&grid) == 1 and layout_occupation_grid_row_count(&grid) == 1)

    layout_occupation_grid_construct_usize(&grid, 3, 2)
    assert(layout_occupation_grid_max_column_index(&grid) == 2 and layout_occupation_grid_max_row_index(&grid) == 1)
    assert(layout_occupation_grid_column_count(&grid) == 3 and layout_occupation_grid_row_count(&grid) == 2)

    # Columns [0, 2) of row 0.
    layout_occupation_grid_set_occupied(&grid, 0, 2, 0, 1)
    assert(layout_occupation_grid_is_occupied(&grid, 1, 0))
    assert(not layout_occupation_grid_is_occupied(&grid, 2, 0))
    assert(not layout_occupation_grid_is_occupied(&grid, 0, 1))
    assert(layout_occupation_grid_is_area_occupied(&grid, 1, 0, 2, 1))
    assert(not layout_occupation_grid_is_area_occupied(&grid, 2, 0, 1, 2))

    # Row flow (columns are the minor axis): a 2x1 area fits neither at column 0 or 1 (occupied) nor at column 2
    # (it would overflow the last column), so the search moves to row 1, column 0.
    var column_index: i32 = 0
    var row_index: i32 = 0
    assert(layout_occupation_grid_find_unoccupied_place(&grid, LayoutGridDimension.column, &column_index, &row_index, 2, 1) == LayoutFoundUnoccupiedPlace.yes)
    assert(column_index == 0 and row_index == 1)

    # A cell at a negative index grows the grid towards the start.
    layout_occupation_grid_set_occupied(&grid, -1, 0, -2, -1)
    assert(layout_occupation_grid_min_column_index(&grid) == -1 and layout_occupation_grid_min_row_index(&grid) == -2)
    assert(layout_occupation_grid_column_count(&grid) == 4 and layout_occupation_grid_row_count(&grid) == 4)

    # Column flow (rows are the minor axis): a 1x3 area in column 0 or 1 always crosses row 0, which is occupied there,
    # so the search ends in column 2 at the first row.
    column_index = 0
    row_index = -2
    assert(layout_occupation_grid_find_unoccupied_place(&grid, LayoutGridDimension.row, &column_index, &row_index, 1, 3) == LayoutFoundUnoccupiedPlace.yes)
    assert(column_index == 2 and row_index == -2)

    # A full grid: the cursor ends past the last row, at the first column.
    var full: LayoutOccupationGrid = ---
    layout_occupation_grid_construct(&full)
    layout_occupation_grid_set_occupied(&full, 0, 1, 0, 1)
    column_index = 0
    row_index = 0
    assert(layout_occupation_grid_find_unoccupied_place(&full, LayoutGridDimension.column, &column_index, &row_index, 1, 1) == LayoutFoundUnoccupiedPlace.no)
    assert(column_index == 0 and row_index == 1)

    # set_max_column_index converts the size_t to int.
    layout_occupation_grid_set_max_column_index(&full, 4)
    assert(layout_occupation_grid_column_count(&full) == 5)