test(obj): Add initial tests for lv_obj_move_to_index (#5284)

Co-authored-by: Gabor Kiss-Vamosi <kisvegabor@gmail.com>
This commit is contained in:
Carlos Diaz
2024-01-19 09:53:35 +01:00
committed by GitHub
co-authored by Gabor Kiss-Vamosi
parent 1a81b836a8
commit 481d867cc3
2 changed files with 121 additions and 8 deletions
+19 -8
View File
@@ -202,22 +202,31 @@ void lv_obj_move_to_index(lv_obj_t * obj, int32_t index)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
/* Check parent validity */
lv_obj_t * parent = lv_obj_get_parent(obj);
if(!parent) {
LV_LOG_WARN("parent is NULL");
return;
}
const uint32_t parent_child_count = lv_obj_get_child_count(parent);
/* old_index only can be 0 or greater, this point can not be reached if the parent is not null */
const int32_t old_index = lv_obj_get_index(obj);
LV_ASSERT(0 <= old_index);
if(index < 0) {
index = lv_obj_get_child_count(parent) + index;
index += parent_child_count;
}
const int32_t old_index = lv_obj_get_index(obj);
/* Index was negative and the absolute value is greater than parent child count */
if((index < 0)
/* Index is same or bigger than parent child count */
|| (index >= (int32_t) parent_child_count)
/* If both previous and new index are the same */
|| (index == old_index)) {
if(index < 0) return;
if(index >= (int32_t) lv_obj_get_child_count(parent)) return;
if(index == old_index) return;
return;
}
int32_t i = old_index;
if(index < old_index) {
@@ -409,14 +418,16 @@ int32_t lv_obj_get_index(const lv_obj_t * obj)
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_obj_t * parent = lv_obj_get_parent(obj);
if(parent == NULL) return 0xFFFFFFFF;
if(parent == NULL) return -1;
int32_t i = 0;
for(i = 0; i < parent->spec_attr->child_cnt; i++) {
if(parent->spec_attr->children[i] == obj) return i;
}
return -1; /*Shouldn't happen*/
/*Shouldn't reach this point*/
LV_ASSERT(0);
return -1;
}
int32_t lv_obj_get_index_by_type(const lv_obj_t * obj, const lv_obj_class_t * class_p)
@@ -80,4 +80,106 @@ void test_obj_tree_3(void)
TEST_ASSERT_EQUAL(lv_obj_get_child(parent2, 0), child1);
}
/** lv_obj_move_to_index **/
void test_obj_move_to_index_move_to_the_background(void)
{
lv_obj_t * parent = NULL;
lv_obj_t * child1 = NULL;
lv_obj_t * child2 = NULL;
parent = lv_obj_create(lv_screen_active());
/* index is 0 */
child1 = lv_obj_create(parent);
/* index is 1 */
child2 = lv_obj_create(parent);
lv_obj_move_to_index(child2, 0);
TEST_ASSERT_EQUAL(1, lv_obj_get_index(child1));
TEST_ASSERT_EQUAL(0, lv_obj_get_index(child2));
}
void test_obj_move_to_index_move_forward(void)
{
lv_obj_t * parent = NULL;
lv_obj_t * child1 = NULL;
lv_obj_t * child2 = NULL;
parent = lv_obj_create(lv_screen_active());
/* index is 0 */
child1 = lv_obj_create(parent);
/* index is 1 */
child2 = lv_obj_create(parent);
lv_obj_move_to_index(child1, lv_obj_get_index(child1) - 1);
TEST_ASSERT_EQUAL(1, lv_obj_get_index(child1));
TEST_ASSERT_EQUAL(0, lv_obj_get_index(child2));
}
/* Tests scenarios when no operation is performed */
void test_obj_move_to_index_no_operation_when_parent_is_null(void)
{
lv_obj_t * parent = NULL;
lv_obj_t * child1 = NULL;
/* index is 0 */
child1 = lv_obj_create(parent);
lv_obj_move_to_index(child1, 0);
TEST_ASSERT_EQUAL_INT32(0xFFFFFFFF, lv_obj_get_index(child1));
}
void test_obj_move_to_index_no_operation_when_index_is_same_or_bigger_than_parent_child_count(void)
{
lv_obj_t * parent = NULL;
lv_obj_t * child1 = NULL;
parent = lv_obj_create(lv_screen_active());
/* index is 0 */
child1 = lv_obj_create(parent);
lv_obj_move_to_index(child1, 3U);
TEST_ASSERT_EQUAL(0, lv_obj_get_index(child1));
}
void test_obj_move_to_index_no_operation_when_new_index_is_the_same_as_previous_index(void)
{
lv_obj_t * parent = NULL;
lv_obj_t * child1 = NULL;
lv_obj_t * child2 = NULL;
parent = lv_obj_create(lv_screen_active());
/* index is 0 */
child1 = lv_obj_create(parent);
/* index is 1 */
child2 = lv_obj_create(parent);
lv_obj_move_to_index(child2, 1U);
TEST_ASSERT_EQUAL(0, lv_obj_get_index(child1));
TEST_ASSERT_EQUAL(1, lv_obj_get_index(child2));
}
void test_obj_move_to_index_no_operation_when_requested_negative_index_is_greater_than_child_count(void)
{
lv_obj_t * parent = NULL;
lv_obj_t * child1 = NULL;
lv_obj_t * child2 = NULL;
parent = lv_obj_create(lv_screen_active());
/* index is 0 */
child1 = lv_obj_create(parent);
/* index is 1 */
child2 = lv_obj_create(parent);
lv_obj_move_to_index(child1, -4);
TEST_ASSERT_EQUAL(0, lv_obj_get_index(child1));
TEST_ASSERT_EQUAL(1, lv_obj_get_index(child2));
}
#endif