Files
LCUI/tests/integration/test_widget_event.c
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

131 lines
4.9 KiB
C

/*
* tests/cases/test_widget_event.c
*
* Copyright (c) 2023-2026, 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 <LCUI.h>
#include <ui.h>
#include <ctest-custom.h>
void test_widget_mouse_event(void)
{
ui_widget_t *root;
ui_widget_t *parent, *child;
ui_event_t ev;
lcui_init();
root = ui_root();
parent = ui_create_widget("button");
child = ui_create_widget("text");
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();
ev.type = UI_EVENT_MOUSEMOVE;
ev.mouse.x = 150;
ev.mouse.y = 150;
ui_dispatch_event(&ev);
ui_update();
ctest_equal_bool("should mark root as hover when mouse is at (150,150)",
ui_widget_has_status(root, "hover"), true);
ctest_equal_bool(
"should not mark parent as hover when mouse is at (150,150)",
ui_widget_has_status(parent, "hover"), false);
ctest_equal_bool(
"should not mark child as hover when mouse is at (150,150)",
ui_widget_has_status(child, "hover"), false);
ev.mouse.x = 80;
ev.mouse.y = 80;
ui_dispatch_event(&ev);
ui_update();
ctest_equal_bool("should mark root as hover when mouse is at (80,80)",
ui_widget_has_status(root, "hover"), true);
ctest_equal_bool("should mark parent as hover when mouse is at (80,80)",
ui_widget_has_status(parent, "hover"), true);
ctest_equal_bool(
"should not mark child as hover when mouse is at (80,80)",
ui_widget_has_status(child, "hover"), false);
ev.mouse.x = 40;
ev.mouse.y = 40;
ui_dispatch_event(&ev);
ui_update();
ctest_equal_bool("should mark root as hover when mouse is at (40,40)",
ui_widget_has_status(root, "hover"), true);
ctest_equal_bool("should mark parent as hover when mouse is at (40,40)",
ui_widget_has_status(parent, "hover"), true);
ctest_equal_bool("should mark child as hover when mouse is at (40,40)",
ui_widget_has_status(child, "hover"), true);
ev.type = UI_EVENT_MOUSEDOWN;
ev.mouse.x = 40;
ev.mouse.y = 40;
ev.mouse.button = MOUSE_BUTTON_LEFT;
ui_dispatch_event(&ev);
ui_update();
ctest_equal_bool("should mark root as active on mousedown",
ui_widget_has_status(root, "active"), true);
ctest_equal_bool("should mark parent as active on mousedown",
ui_widget_has_status(parent, "active"), true);
ctest_equal_bool("should mark child as active on mousedown",
ui_widget_has_status(child, "active"), true);
ev.type = UI_EVENT_MOUSEUP;
ui_dispatch_event(&ev);
ui_update();
ctest_equal_bool("should clear root active state on mouseup",
ui_widget_has_status(root, "active"), false);
ctest_equal_bool("should clear parent active state on mouseup",
ui_widget_has_status(parent, "active"), false);
ctest_equal_bool("should clear child active state on mouseup",
ui_widget_has_status(child, "active"), false);
ev.type = UI_EVENT_MOUSEMOVE;
ev.mouse.x = 80;
ev.mouse.y = 80;
ui_dispatch_event(&ev);
ui_update();
ctest_equal_bool(
"should keep root as hover when mouse moves back to (80,80)",
ui_widget_has_status(root, "hover"), true);
ctest_equal_bool(
"should keep parent as hover when mouse moves back to (80,80)",
ui_widget_has_status(parent, "hover"), true);
ctest_equal_bool(
"should clear child hover state when mouse moves back to (80,80)",
ui_widget_has_status(child, "hover"), false);
ev.mouse.x = 150;
ev.mouse.y = 150;
ui_dispatch_event(&ev);
ui_update();
ctest_equal_bool(
"should keep root as hover when mouse leaves to (150,150)",
ui_widget_has_status(root, "hover"), true);
ctest_equal_bool(
"should clear parent hover state when mouse leaves to (150,150)",
ui_widget_has_status(parent, "hover"), false);
ctest_equal_bool(
"should clear child hover state when mouse leaves to (150,150)",
ui_widget_has_status(child, "hover"), false);
lcui_destroy();
}
void test_widget_event(void)
{
ctest_describe("widget mouse event", test_widget_mouse_event);
}