mirror of
https://github.com/lc-soft/LCUI.git
synced 2026-11-30 16:48:21 +01:00
228 lines
7.8 KiB
C
228 lines
7.8 KiB
C
/*
|
|
* tests/integration/test_widget_rect.c
|
|
*
|
|
* Copyright (c) 2023-2026, Liu Chao <hello@lcui.dev> 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>
|
|
|
|
static const char *widget_rect_test_css = "\
|
|
.widget-rect-test {\
|
|
background-color: #ffffff;\
|
|
}\
|
|
.widget-rect-test:hover {\
|
|
background-color: #eeeeee;\
|
|
}\
|
|
.widget-rect-test:active {\
|
|
position: relative;\
|
|
top: 1px;\
|
|
background-color: #dddddd;\
|
|
}\
|
|
";
|
|
|
|
static bool rects_contain(list_t *rects, const pd_rect_t *expected)
|
|
{
|
|
list_node_t *node;
|
|
|
|
for (list_each(node, rects)) {
|
|
if (memcmp(node->data, expected, sizeof(pd_rect_t)) == 0) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
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;
|
|
pd_rect_t expected_shift_rect;
|
|
ui_rect_t expected_ui_rect;
|
|
list_t rects;
|
|
float offset_x, offset_y;
|
|
|
|
lcui_init();
|
|
ui_load_css_string(widget_rect_test_css, __FILE__);
|
|
root = ui_root();
|
|
parent = ui_create_widget(NULL);
|
|
child = ui_create_widget("text");
|
|
|
|
ui_widget_add_class(parent, "widget-rect-test");
|
|
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();
|
|
|
|
ui_widget_get_offset(child, root, &offset_x, &offset_y);
|
|
ctest_equal_float("should calculate child offset from border boxes",
|
|
offset_x, child->border_box.x + parent->border_box.x);
|
|
ctest_equal_float("should calculate child y offset from border boxes",
|
|
offset_y, child->border_box.y + parent->border_box.y);
|
|
|
|
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 after mouse enters at (80,80)",
|
|
&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 two dirty rects on mousedown at (40,40)",
|
|
rects.length == 2, 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);
|
|
expected_ui_rect.y = 1;
|
|
ui_compute_rect(&expected_shift_rect, &expected_ui_rect);
|
|
ctest_equal_bool("should dirty the original area on mousedown",
|
|
rects_contain(&rects, &expected_rect), true);
|
|
ctest_equal_bool("should dirty the shifted area on mousedown",
|
|
rects_contain(&rects, &expected_shift_rect), true);
|
|
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 two dirty rects on mouseup at (40,40)",
|
|
rects.length == 2, true);
|
|
expected_ui_rect.y = 0;
|
|
ui_compute_rect(&expected_rect, &expected_ui_rect);
|
|
expected_ui_rect.y = 1;
|
|
ui_compute_rect(&expected_shift_rect, &expected_ui_rect);
|
|
ctest_equal_bool("should dirty the original area on mouseup",
|
|
rects_contain(&rects, &expected_rect), true);
|
|
ctest_equal_bool("should dirty the shifted area on mouseup",
|
|
rects_contain(&rects, &expected_shift_rect), true);
|
|
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 after mouse leaves at (150,150)",
|
|
&rect, &expected_rect);
|
|
list_destroy(&rects, free);
|
|
|
|
expected_ui_rect.x = 0;
|
|
expected_ui_rect.y = 0;
|
|
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 after child is destroyed", &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 after parent is destroyed", &rect,
|
|
&expected_rect);
|
|
list_destroy(&rects, free);
|
|
|
|
lcui_destroy();
|
|
}
|