feat(obj_name): add auto-indexing with names like 'mybtn_#'

This commit is contained in:
Gabor Kiss-Vamosi
2025-05-11 08:11:00 +02:00
committed by Liam Howatt
parent 5652914e14
commit e4bbc4f0fa
10 changed files with 382 additions and 35 deletions
+43 -2
View File
@@ -209,6 +209,8 @@ void test_obj_get_by_name(void)
lv_obj_t * cont3 = lv_obj_create(lv_screen_active());
lv_obj_set_name_static(cont3, "third_static");
lv_obj_set_name_static(cont3, "third");
lv_obj_t * cont4 = lv_obj_create(lv_screen_active());
lv_obj_t * root_label = lv_label_create(lv_screen_active());
lv_label_set_text(root_label, "Root");
@@ -223,6 +225,19 @@ void test_obj_get_by_name(void)
lv_label_set_text(hello_label, "Hello");
lv_obj_set_name(hello_label, "my_label"); /*Same name as ofr the other label*/
/*Test auto indexing*/
lv_obj_t * label0 = lv_label_create(cont3);
lv_obj_set_name(label0, "title_#");
lv_obj_t * label1 = lv_label_create(cont3);
lv_obj_set_name(label1, "title_#");
lv_obj_t * label2 = lv_label_create(cont3); //lv_label_0
lv_obj_t * label3 = lv_label_create(cont3);
lv_obj_set_name(label3, "title_#?");
lv_obj_t * label4 = lv_label_create(cont3); //lv_label_1
lv_obj_t * label5 = lv_label_create(cont3);
lv_obj_set_name(label5, "title_#");
lv_obj_t * found_obj;
@@ -233,9 +248,12 @@ void test_obj_get_by_name(void)
found_obj = lv_obj_get_child_by_name(lv_screen_active(), "first");
TEST_ASSERT_EQUAL(cont1, found_obj);
found_obj = lv_obj_get_child_by_name(lv_screen_active(), "lv_obj_2");
found_obj = lv_obj_get_child_by_name(lv_screen_active(), "lv_obj_0");
TEST_ASSERT_EQUAL(cont2, found_obj);
found_obj = lv_obj_get_child_by_name(lv_screen_active(), "lv_obj_1");
TEST_ASSERT_EQUAL(cont4, found_obj);
found_obj = lv_obj_get_child_by_name(lv_screen_active(), "fifth");
TEST_ASSERT_EQUAL(NULL, found_obj);
@@ -258,11 +276,30 @@ void test_obj_get_by_name(void)
found_obj = lv_obj_get_child_by_name(lv_screen_active(), "");
TEST_ASSERT_EQUAL(NULL, found_obj);
/* Test auto indexed names*/
found_obj = lv_obj_get_child_by_name(lv_screen_active(), "third/title_0");
TEST_ASSERT_EQUAL(label0, found_obj);
found_obj = lv_obj_get_child_by_name(lv_screen_active(), "third/title_1");
TEST_ASSERT_EQUAL(label1, found_obj);
found_obj = lv_obj_get_child_by_name(lv_screen_active(), "third/lv_label_0");
TEST_ASSERT_EQUAL(label2, found_obj);
found_obj = lv_obj_get_child_by_name(lv_screen_active(), "third/title_#?");
TEST_ASSERT_EQUAL(label3, found_obj);
found_obj = lv_obj_get_child_by_name(lv_screen_active(), "third/lv_label_1");
TEST_ASSERT_EQUAL(label4, found_obj);
found_obj = lv_obj_get_child_by_name(lv_screen_active(), "third/title_2");
TEST_ASSERT_EQUAL(label5, found_obj);
/*-------------
* Find by name
*------------*/
found_obj = lv_obj_find_by_name(lv_screen_active(), "lv_obj_2");
found_obj = lv_obj_find_by_name(lv_screen_active(), "lv_obj_0");
TEST_ASSERT_EQUAL(cont2, found_obj);
found_obj = lv_obj_find_by_name(lv_screen_active(), "my_label");
@@ -270,6 +307,10 @@ void test_obj_get_by_name(void)
found_obj = lv_obj_find_by_name(cont1, "my_label");
TEST_ASSERT_EQUAL(hello_label, found_obj);
found_obj = lv_obj_find_by_name(lv_screen_active(), "title_2");
TEST_ASSERT_EQUAL(label5, found_obj);
}
#endif
+17 -16
View File
@@ -228,22 +228,23 @@ void test_xml_error_resilience_syntax_ok(void)
{
const char * my_btn_xml =
"<component>"
"<consts>"
"<int name=\"abc\" value=\"0xff0000\"/>"
"<not_a_type name=\"xyz\" value=\"0xff0000\"/>"
"<int not_a_prop=\"abc\" value=\"0xff0000\"/>"
"<int name=\"abc\" not_a_value=\"0xff0000\"/>"
"</consts>"
"<styles>"
"<style name=\"rel_style\" bg_color=\"0xff0000\" not_a_prop=\"0xff0000\"/>"
"<inv_style name=\"rel_style\" bg_color=\"0x800000\"/>"
"<style bg_color=\"0x800000\"/>"
"</styles>"
"<view extends=\"not_a_widget\" style_text_color=\"0x0000ff\" style_text_color:checked=\"0x8080ff\" styles=\"rel_style pr_style:checked\">"
"<unknown/>"
"<lv_label not_an_attr=\"40\"/>"
"</view>"
" <consts>"
" <int name=\"abc\" value=\"0xff0000\"/>"
" <not_a_type name=\"xyz\" value=\"0xff0000\"/>"
" <int not_a_prop=\"abc\" value=\"0xff0000\"/>"
" <int name=\"abc\" not_a_value=\"0xff0000\"/>"
" </consts>"
""
" <styles>"
" <style name=\"rel_style\" bg_color=\"0xff0000\" not_a_prop=\"0xff0000\"/>"
" <inv_style name=\"rel_style\" bg_color=\"0x800000\"/>"
" <style bg_color=\"0x800000\"/>"
" </styles>"
""
" <view extends=\"not_a_widget\" style_text_color=\"0x0000ff\" style_text_color:checked=\"0x8080ff\" styles=\"rel_style pr_style:checked\">"
" <unknown/>"
" <lv_label not_an_attr=\"40\"/>"
" </view>"
"</component>";
lv_xml_component_register_from_data("my_btn", my_btn_xml);
+107
View File
@@ -0,0 +1,107 @@
#if LV_BUILD_TEST || 1
#include "../lvgl.h"
#include "unity/unity.h"
void setUp(void)
{
/* Function run before every test */
}
void tearDown(void)
{
/* Function run after every test */
lv_obj_clean(lv_screen_active());
}
void test_xml_names(void)
{
const char * base_label_xml =
"<component>"
" <view extends=\"lv_label\"></view>"
"</component>";
const char * my_label_xml =
"<component>"
" <view extends=\"base_label\"></view>"
"</component>";
const char * my_btn_xml =
"<component>"
" <view extends=\"lv_button\">"
" <my_label name=\"first_label\"/>"
" <my_label/>"
" <my_label name=\"third_label\"/>"
" <my_label/>"
" </view>"
"</component>";
const char * main_screen_xml =
"<screen>"
" <view name=\"main_screen\">"
" <my_btn/>"
" <my_btn name=\"first_btn\"/>"
" <lv_button/>"
" <my_btn/>"
" <my_btn name=\"lv_button_#\"/>"
" <lv_button/>"
" </view>"
"</screen>";
lv_xml_component_register_from_data("my_btn", my_btn_xml);
lv_xml_component_register_from_data("base_label", base_label_xml);
lv_xml_component_register_from_data("my_label", my_label_xml);
lv_xml_component_register_from_data("main_screen", main_screen_xml);
lv_obj_t * main_screen = lv_xml_create(NULL, "main_screen", NULL);
char buf[128];
lv_obj_t * btn;
btn = lv_obj_get_child(main_screen, 0);
lv_obj_get_name_resolved(btn, buf, 128);
TEST_ASSERT_EQUAL_STRING("my_btn_0", buf);
btn = lv_obj_get_child(main_screen, 1);
lv_obj_get_name_resolved(btn, buf, 128);
TEST_ASSERT_EQUAL_STRING("first_btn", buf);
btn = lv_obj_get_child(main_screen, 2);
lv_obj_get_name_resolved(btn, buf, 128);
TEST_ASSERT_EQUAL_STRING("lv_button_0", buf);
btn = lv_obj_get_child(main_screen, 3);
lv_obj_get_name_resolved(btn, buf, 128);
TEST_ASSERT_EQUAL_STRING("my_btn_1", buf);
btn = lv_obj_get_child(main_screen, 4);
lv_obj_get_name_resolved(btn, buf, 128);
TEST_ASSERT_EQUAL_STRING("lv_button_1", buf);
btn = lv_obj_get_child(main_screen, 5);
lv_obj_get_name_resolved(btn, buf, 128);
TEST_ASSERT_EQUAL_STRING("lv_button_2", buf);
btn = lv_obj_get_child(main_screen, 3);
lv_obj_t * label;
label = lv_obj_get_child(btn, 0);
lv_obj_get_name_resolved(label, buf, 128);
TEST_ASSERT_EQUAL_STRING("first_label", buf);
label = lv_obj_get_child(btn, 1);
lv_obj_get_name_resolved(label, buf, 128);
TEST_ASSERT_EQUAL_STRING("my_label_0", buf);
label = lv_obj_get_child(btn, 2);
lv_obj_get_name_resolved(label, buf, 128);
TEST_ASSERT_EQUAL_STRING("third_label", buf);
label = lv_obj_get_child(btn, 3);
lv_obj_get_name_resolved(label, buf, 128);
TEST_ASSERT_EQUAL_STRING("my_label_1", buf);
}
#endif