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

112 lines
4.0 KiB
C

/*
* tests/cases/test_widget_opacity.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 <stdlib.h>
#include <LCUI.h>
#include <ctest-custom.h>
#include "../scenes/widget_opacity_scene.h"
#define PARENT_OPACITY 0.8f
#define CHILD_OPACITY 0.5f
static widget_opacity_scene_t scene;
static int check_color(pd_color_t a, pd_color_t b)
{
return abs(a.r - b.r) < 2 && abs(a.g - b.g) < 2 && abs(a.b - b.b) < 2;
}
static void check_widget_opactiy(void)
{
pd_canvas_t canvas;
pd_color_t color;
pd_color_t tmp;
pd_color_t expected_color;
pd_color_t child_bgcolor = pd_rgb(0, 255, 0);
pd_color_t child_footer_bgcolor = pd_rgb(255, 255, 255);
pd_color_t parent_bgcolor = pd_rgb(255, 0, 0);
pd_color_t parent_bcolor = pd_rgb(0, 0, 0);
pd_color_t bgcolor = pd_rgb(255, 255, 255);
pd_rect_t rect = { 0, 0, 400, 256 };
pd_context_t paint;
pd_canvas_init(&canvas);
pd_canvas_create(&canvas, rect.width, rect.height);
pd_canvas_fill(&canvas, bgcolor);
ui_metrics.dpi = 96;
paint.with_alpha = false;
paint.rect.width = 400;
paint.rect.height = 256;
paint.rect.x = paint.rect.y = 0;
pd_canvas_quote(&paint.canvas, &canvas, &rect);
ui_widget_set_style_string(scene.parent, "opacity", "0.8");
ui_widget_resize(scene.parent, 512, 256);
ui_widget_request_refresh_style(scene.child);
ui_widget_request_refresh_style(scene.parent);
ui_widget_update(scene.parent);
ui_widget_render(scene.parent, &paint);
expected_color = bgcolor;
color = pd_canvas_get_pixel(&canvas, 10, 10);
pd_blend_pixel(&expected_color, &parent_bcolor,
(int)(PARENT_OPACITY * 255));
ctest_equal_bool("should match parent border color",
check_color(expected_color, color), true);
expected_color = bgcolor;
color = pd_canvas_get_pixel(&canvas, 30, 30);
pd_blend_pixel(&expected_color, &parent_bgcolor,
(int)(PARENT_OPACITY * 255));
ctest_equal_bool("should match parent background color",
check_color(expected_color, color), true);
tmp = parent_bgcolor;
expected_color = bgcolor;
color = pd_canvas_get_pixel(&canvas, 60, 90);
pd_blend_pixel(&tmp, &child_bgcolor, (int)(CHILD_OPACITY * 255));
pd_blend_pixel(&expected_color, &tmp, (int)(PARENT_OPACITY * 255));
ctest_equal_bool("should match child 1 background color",
check_color(expected_color, color), true);
tmp = parent_bgcolor;
expected_color = bgcolor;
color = pd_canvas_get_pixel(&canvas, 60, 120);
pd_blend_pixel(&tmp, &child_footer_bgcolor, (int)(CHILD_OPACITY * 255));
pd_blend_pixel(&expected_color, &tmp, (int)(PARENT_OPACITY * 255));
ctest_equal_bool("should match child 1 footer background color",
check_color(expected_color, color), true);
expected_color = bgcolor;
color = pd_canvas_get_pixel(&canvas, 220, 90);
pd_blend_pixel(&expected_color, &child_bgcolor,
(int)(PARENT_OPACITY * 255));
ctest_equal_bool("should match child 2 background color",
check_color(expected_color, color), true);
expected_color = child_footer_bgcolor;
color = pd_canvas_get_pixel(&canvas, 220, 120);
ctest_equal_bool("should match child 2 footer background color",
check_color(expected_color, color), true);
pd_canvas_destroy(&canvas);
}
void test_widget_opacity(void)
{
lcui_init();
widget_opacity_scene_build(&scene);
ctest_describe("widget opacity", check_widget_opactiy);
lcui_destroy();
}