From 312303cb447f899fd55ea452774a35942cd38414 Mon Sep 17 00:00:00 2001 From: sparkles43 <112950468+sparkles43@users.noreply.github.com> Date: Thu, 20 Apr 2023 20:29:58 +0200 Subject: [PATCH] fix(obj_tree): update parents of children after swapping (#4150) --- src/core/lv_obj_tree.c | 3 ++ tests/src/test_cases/test_obj_tree.c | 44 ++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/core/lv_obj_tree.c b/src/core/lv_obj_tree.c index 254dd40c9..5d72d76dc 100644 --- a/src/core/lv_obj_tree.c +++ b/src/core/lv_obj_tree.c @@ -245,7 +245,10 @@ void lv_obj_swap(lv_obj_t * obj1, lv_obj_t * obj2) lv_obj_send_event(parent, LV_EVENT_CHILD_DELETED, obj1); parent->spec_attr->children[index1] = obj2; + obj2->parent = parent; + parent2->spec_attr->children[index2] = obj1; + obj1->parent = parent2; lv_obj_send_event(parent, LV_EVENT_CHILD_CHANGED, obj2); lv_obj_send_event(parent, LV_EVENT_CHILD_CREATED, obj2); diff --git a/tests/src/test_cases/test_obj_tree.c b/tests/src/test_cases/test_obj_tree.c index 73bdb0f13..1b0ac45bc 100644 --- a/tests/src/test_cases/test_obj_tree.c +++ b/tests/src/test_cases/test_obj_tree.c @@ -36,4 +36,48 @@ void test_obj_tree_2(void) //TEST_ASSERT_EQUAL_SCREENSHOT("scr1.png") } +void test_obj_tree_3(void) +{ + /* tests lv_obj_swap */ + lv_obj_t * parent1 = lv_obj_create(lv_scr_act()); + lv_obj_t * parent2 = lv_obj_create(lv_scr_act()); + lv_obj_t * child1 = lv_obj_create(parent1); + lv_obj_t * child2 = lv_obj_create(parent2); + + /* were the parents set correctly for the children? */ + lv_obj_t * child1_parent_before = lv_obj_get_parent(child1); + lv_obj_t * child2_parent_before = lv_obj_get_parent(child2); + + TEST_ASSERT_EQUAL(child1_parent_before, parent1); + TEST_ASSERT_EQUAL(child2_parent_before, parent2); + + /* were the children set correctly for the parents? */ + TEST_ASSERT_EQUAL(lv_obj_get_child_cnt(parent1), 1); + TEST_ASSERT_EQUAL(lv_obj_get_index(child1), 0); + TEST_ASSERT_EQUAL(lv_obj_get_child(parent1, 0), child1); + + TEST_ASSERT_EQUAL(lv_obj_get_child_cnt(parent2), 1); + TEST_ASSERT_EQUAL(lv_obj_get_index(child2), 0); + TEST_ASSERT_EQUAL(lv_obj_get_child(parent2, 0), child2); + + /* swap the children */ + lv_obj_swap(child1, child2); + + /* test for properly swapped parents */ + lv_obj_t * child1_parent_after = lv_obj_get_parent(child1); + lv_obj_t * child2_parent_after = lv_obj_get_parent(child2); + + TEST_ASSERT_EQUAL(child1_parent_after, parent2); + TEST_ASSERT_EQUAL(child2_parent_after, parent1); + + /* test for correctly set children */ + TEST_ASSERT_EQUAL(lv_obj_get_child_cnt(parent1), 1); + TEST_ASSERT_EQUAL(lv_obj_get_index(child2), 0); + TEST_ASSERT_EQUAL(lv_obj_get_child(parent1, 0), child2); + + TEST_ASSERT_EQUAL(lv_obj_get_child_cnt(parent2), 1); + TEST_ASSERT_EQUAL(lv_obj_get_index(child1), 0); + TEST_ASSERT_EQUAL(lv_obj_get_child(parent2, 0), child1); +} + #endif