Files
LCUI/tests/integration/test_widget_rect.c
T
Liu faa7058837 test: reorganize test suite with xmake test integration
Restructure the test infrastructure with these changes:

* Extend libctest with ctest_suite_t, ctest_parse_args, ctest_run_suites and CTEST_MAIN macro to standardize test entry points; support --grep / --grep= / --list / --interactive flags; add ctest-tests for self-tests.

* Regroup tests by lib boundary: sink unit tests into lib/<name>/tests/ with <lib>_<topic> entry function names, move fixtures into lib/<name>/tests/fixtures/, rename top-level tests/cases/ to tests/integration/ for cross-lib integration tests.

* Add tests/scenes/ for reusable widget tree builders shared by integration cases and visual preview binaries; add tests/previews/ with 4 diagnostic binaries (flex_layout, block_layout, widget_opacity, scrollbar).

* Rewrite all describe/equal description strings using 'should ...' style or noun topic names; drop redundant lcui_quit/lcui_main calls from integration cases.

* Integrate xmake test via add_tests(\default\) and a shared tests.runnable rule defined in the root xmake.lua; the rule wraps the test binary with drmemory (Windows) or valgrind (Linux) when option memcheck=y.

* Drop the lcui-tests aggregation: lcui-tests now contains only integration cases and scenes, not the union of lib tests. Use xmake test to run everything across libs.

* Fix .clang-format: InsertNewlineAtEndOfFile -> InsertNewlineAtEOF (correct key for clang-format 16+).

* Update AGENTS.md with the new test layout, naming rules, description style and run commands.

* Update CI to use xmake test and xmake f --memcheck=y instead of per-lib runs.
2026-05-24 17:56:23 +08:00

181 lines
6.1 KiB
C

/*
* tests/cases/test_widget_rect.c
*
* Copyright (c) 2023, Liu Chao <i@lc-soft.io> All rights reserved.
*
* SPDX-License-Identifier: MIT
*
* This file is part of LCUI, distributed under the MIT License found in the
* LICENSE.TXT file in the root directory of this source tree.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <LCUI.h>
#include <ui.h>
#include <ctest-custom.h>
void test_widget_rect(void)
{
ui_widget_t *root;
ui_widget_t *parent, *child;
ui_event_t e = { 0 };
pd_rect_t rect = { 0 };
pd_rect_t expected_rect;
ui_rect_t expected_ui_rect;
list_t rects;
lcui_init();
root = ui_root();
parent = ui_create_widget("button");
child = ui_create_widget("text");
ui_widget_set_style_keyword_value(parent, css_prop_box_sizing,
CSS_KEYWORD_BORDER_BOX);
ui_widget_resize(root, 200, 200);
ui_widget_resize(parent, 100, 100);
ui_widget_resize(child, 50, 50);
ui_widget_append(parent, child);
ui_widget_append(root, parent);
ui_update();
list_create(&rects);
ui_widget_get_dirty_rects(root, &rects);
list_destroy(&rects, free);
e.type = UI_EVENT_MOUSEMOVE;
e.mouse.x = 150;
e.mouse.y = 150;
ui_dispatch_event(&e);
ui_update();
ui_widget_get_dirty_rects(root, &rects);
ctest_equal_bool("should not produce dirty rects when mouse hovers "
"outside (150,150)",
rects.length == 0, true);
e.mouse.x = 80;
e.mouse.y = 80;
ui_dispatch_event(&e);
ui_update();
ui_widget_get_dirty_rects(root, &rects);
ctest_equal_bool(
"should produce one dirty rect when mouse enters parent at (80,80)",
rects.length == 1, true);
expected_ui_rect.x = 0;
expected_ui_rect.y = 0;
expected_ui_rect.width = 100;
expected_ui_rect.height = 100;
ui_compute_rect(&expected_rect, &expected_ui_rect);
if (rects.length == 1) {
rect = *(pd_rect_t *)list_get(&rects, 0);
}
ctest_equal_pd_rect("should match expected dirty rect", &rect,
&expected_rect);
list_destroy(&rects, free);
e.mouse.x = 40;
e.mouse.y = 40;
ui_dispatch_event(&e);
ui_update();
ui_widget_get_dirty_rects(root, &rects);
ctest_equal_bool("should not produce dirty rects when mouse moves to "
"child at (40,40)",
rects.length == 0, true);
e.type = UI_EVENT_MOUSEDOWN;
e.mouse.x = 40;
e.mouse.y = 40;
e.mouse.button = MOUSE_BUTTON_LEFT;
ui_dispatch_event(&e);
ui_update();
ui_widget_get_dirty_rects(root, &rects);
ctest_equal_bool(
"should produce one dirty rect on mousedown at (40,40)",
rects.length == 1, true);
if (rects.length == 1) {
rect = *(pd_rect_t *)list_get(&rects, 0);
}
ctest_equal_pd_rect("should match expected dirty rect", &rect,
&expected_rect);
list_destroy(&rects, free);
e.type = UI_EVENT_MOUSEUP;
ui_dispatch_event(&e);
ui_update();
ui_widget_get_dirty_rects(root, &rects);
ctest_equal_bool("should produce one dirty rect on mouseup at (40,40)",
rects.length == 1, true);
if (rects.length == 1) {
rect = *(pd_rect_t *)list_get(&rects, 0);
}
ctest_equal_pd_rect("should match expected dirty rect", &rect,
&expected_rect);
list_destroy(&rects, free);
e.type = UI_EVENT_MOUSEMOVE;
e.mouse.x = 80;
e.mouse.y = 80;
ui_dispatch_event(&e);
ui_update();
ui_widget_get_dirty_rects(root, &rects);
ctest_equal_bool("should not produce dirty rects when mouse moves back "
"to parent at (80,80)",
rects.length == 0, true);
e.mouse.x = 150;
e.mouse.y = 150;
ui_dispatch_event(&e);
ui_update();
ui_widget_get_dirty_rects(root, &rects);
ctest_equal_bool("should produce one dirty rect when mouse leaves "
"parent at (150,150)",
rects.length == 1, true);
if (rects.length == 1) {
rect = *(pd_rect_t *)list_get(&rects, 0);
}
ctest_equal_pd_rect("should match expected dirty rect", &rect,
&expected_rect);
list_destroy(&rects, free);
expected_ui_rect.x = 21;
expected_ui_rect.y = 11;
expected_ui_rect.width = 50;
expected_ui_rect.height = 50;
ui_compute_rect(&expected_rect, &expected_ui_rect);
ui_widget_remove(child);
ui_update();
ui_widget_get_dirty_rects(root, &rects);
ctest_equal_bool(
"should produce one dirty rect when child is destroyed",
rects.length == 1, true);
if (rects.length == 1) {
rect = *(pd_rect_t *)list_get(&rects, 0);
}
ctest_equal_pd_rect("should match expected dirty rect", &rect,
&expected_rect);
list_destroy(&rects, free);
expected_ui_rect.x = 0;
expected_ui_rect.y = 0;
expected_ui_rect.width = 100;
expected_ui_rect.height = 100;
ui_compute_rect(&expected_rect, &expected_ui_rect);
ui_widget_remove(parent);
ui_update();
ui_widget_get_dirty_rects(root, &rects);
ctest_equal_bool(
"should produce one dirty rect when parent is destroyed",
rects.length == 1, true);
if (rects.length == 1) {
rect = *(pd_rect_t *)list_get(&rects, 0);
}
ctest_equal_pd_rect("should match expected dirty rect", &rect,
&expected_rect);
list_destroy(&rects, free);
lcui_destroy();
}