Files
LCUI/tests/integration/test_mainloop.c
T
Liu 75dbca815d 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-08-05 22:41:12 +08:00

82 lines
2.0 KiB
C

/*
* tests/cases/test_mainloop.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 <stdlib.h>
#include <LCUI.h>
#include <ctest-custom.h>
static void handle_refresh(void *arg)
{
ui_refresh_style();
}
static void handle_quit(void *arg)
{
lcui_quit();
}
static void handle_btn_click(ui_widget_t *w, ui_event_t *ui_event, void *arg)
{
ptk_set_timeout(0, handle_refresh, NULL);
ptk_set_timeout(1, handle_quit, NULL);
ptk_process_native_events(PTK_PROCESS_EVENTS_UNTIL_QUIT);
lcui_quit();
}
static void handle_trigger_btn_click(void *arg)
{
ptk_event_t e = { 0 };
e.type = PTK_EVENT_MOUSEDOWN;
e.mouse.button = MOUSE_BUTTON_LEFT;
e.mouse.x = 5;
e.mouse.y = 5;
ptk_post_event(&e);
e.type = PTK_EVENT_MOUSEUP;
ptk_post_event(&e);
}
static void observer_thread(void *arg)
{
int i;
bool *exited = arg;
for (i = 0; i < 10 && !*exited; ++i) {
sleep_ms(100);
}
ctest_equal_bool("should exit main loop within 1000ms", *exited, true);
if (!*exited) {
exit(-ctest_finish());
return;
}
thread_exit(NULL);
}
void test_mainloop(void)
{
thread_t tid;
ui_widget_t *btn;
bool exited = false;
lcui_init();
btn = ui_create_widget("button");
ui_button_set_text(btn, "button");
ui_widget_on(btn, "click", handle_btn_click, NULL);
ui_root_append(btn);
/* Observe whether the main loop has exited in a new thread */
thread_create(&tid, observer_thread, &exited);
/* Trigger the click event after the first frame is updated */
ptk_set_timeout(50, handle_trigger_btn_click, btn);
lcui_main();
exited = true;
thread_join(tid, NULL);
}