1 #if LV_BUILD_TEST
2 #include "../lvgl.h"
3 #include "../../lvgl_private.h"
4 
5 #include "unity/unity.h"
6 
7 void test_obj_tree_1(void);
8 void test_obj_tree_2(void);
9 
test_obj_tree_1(void)10 void test_obj_tree_1(void)
11 {
12     TEST_ASSERT_EQUAL(lv_obj_get_child_count(lv_screen_active()), 0);
13 }
14 
test_obj_tree_2(void)15 void test_obj_tree_2(void)
16 {
17 
18     lv_obj_create(lv_screen_active());
19     lv_obj_t * o2 = lv_obj_create(lv_screen_active());
20     lv_obj_create(lv_screen_active());
21     TEST_ASSERT_EQUAL(lv_obj_get_child_count(lv_screen_active()), 3);
22 
23     lv_obj_delete(o2);
24     TEST_ASSERT_EQUAL(lv_obj_get_child_count(lv_screen_active()), 2);
25 
26     lv_obj_clean(lv_screen_active());
27     TEST_ASSERT_EQUAL(lv_obj_get_child_count(lv_screen_active()), 0);
28 
29     lv_color_t c1 = lv_color_hex(0x444444);
30     lv_color_t c2 = lv_color_hex3(0x444);
31     TEST_ASSERT_EQUAL_COLOR(c1, c2);
32 
33     lv_obj_remove_style_all(lv_screen_active());
34     lv_obj_set_style_bg_color(lv_screen_active(), lv_color_hex(0x112233), 0);
35     lv_obj_set_style_bg_opa(lv_screen_active(), LV_OPA_COVER, 0);
36 
37     //TEST_ASSERT_EQUAL_SCREENSHOT("widgets/scr1.png")
38 }
39 
test_obj_tree_3(void)40 void test_obj_tree_3(void)
41 {
42     /* tests lv_obj_swap */
43     lv_obj_t * parent1 = lv_obj_create(lv_screen_active());
44     lv_obj_t * parent2 = lv_obj_create(lv_screen_active());
45     lv_obj_t * child1 = lv_obj_create(parent1);
46     lv_obj_t * child2 = lv_obj_create(parent2);
47 
48     /* were the parents set correctly for the children? */
49     lv_obj_t * child1_parent_before = lv_obj_get_parent(child1);
50     lv_obj_t * child2_parent_before = lv_obj_get_parent(child2);
51 
52     TEST_ASSERT_EQUAL(child1_parent_before, parent1);
53     TEST_ASSERT_EQUAL(child2_parent_before, parent2);
54 
55     /* were the children set correctly for the parents? */
56     TEST_ASSERT_EQUAL(lv_obj_get_child_count(parent1), 1);
57     TEST_ASSERT_EQUAL(lv_obj_get_index(child1), 0);
58     TEST_ASSERT_EQUAL(lv_obj_get_child(parent1, 0), child1);
59 
60     TEST_ASSERT_EQUAL(lv_obj_get_child_count(parent2), 1);
61     TEST_ASSERT_EQUAL(lv_obj_get_index(child2), 0);
62     TEST_ASSERT_EQUAL(lv_obj_get_child(parent2, 0), child2);
63 
64     /* swap the children */
65     lv_obj_swap(child1, child2);
66 
67     /* test for properly swapped parents */
68     lv_obj_t * child1_parent_after = lv_obj_get_parent(child1);
69     lv_obj_t * child2_parent_after = lv_obj_get_parent(child2);
70 
71     TEST_ASSERT_EQUAL(child1_parent_after, parent2);
72     TEST_ASSERT_EQUAL(child2_parent_after, parent1);
73 
74     /* test for correctly set children */
75     TEST_ASSERT_EQUAL(lv_obj_get_child_count(parent1), 1);
76     TEST_ASSERT_EQUAL(lv_obj_get_index(child2), 0);
77     TEST_ASSERT_EQUAL(lv_obj_get_child(parent1, 0), child2);
78 
79     TEST_ASSERT_EQUAL(lv_obj_get_child_count(parent2), 1);
80     TEST_ASSERT_EQUAL(lv_obj_get_index(child1), 0);
81     TEST_ASSERT_EQUAL(lv_obj_get_child(parent2, 0), child1);
82 }
83 
84 /** lv_obj_move_to_index **/
85 
test_obj_move_to_index_move_to_the_background(void)86 void test_obj_move_to_index_move_to_the_background(void)
87 {
88     lv_obj_t * parent = NULL;
89     lv_obj_t * child1 = NULL;
90     lv_obj_t * child2 = NULL;
91 
92     parent = lv_obj_create(lv_screen_active());
93     /* index is 0 */
94     child1 = lv_obj_create(parent);
95     /* index is 1 */
96     child2 = lv_obj_create(parent);
97 
98     lv_obj_move_to_index(child2, 0);
99 
100     TEST_ASSERT_EQUAL(1, lv_obj_get_index(child1));
101     TEST_ASSERT_EQUAL(0, lv_obj_get_index(child2));
102 }
103 
test_obj_move_to_index_move_forward(void)104 void test_obj_move_to_index_move_forward(void)
105 {
106     lv_obj_t * parent = NULL;
107     lv_obj_t * child1 = NULL;
108     lv_obj_t * child2 = NULL;
109 
110     parent = lv_obj_create(lv_screen_active());
111     /* index is 0 */
112     child1 = lv_obj_create(parent);
113     /* index is 1 */
114     child2 = lv_obj_create(parent);
115 
116     lv_obj_move_to_index(child1, lv_obj_get_index(child1) - 1);
117 
118     TEST_ASSERT_EQUAL(1, lv_obj_get_index(child1));
119     TEST_ASSERT_EQUAL(0, lv_obj_get_index(child2));
120 }
121 
122 /* Tests scenarios when no operation is performed */
test_obj_move_to_index_no_operation_when_parent_is_null(void)123 void test_obj_move_to_index_no_operation_when_parent_is_null(void)
124 {
125     lv_obj_t * parent = NULL;
126     lv_obj_t * child1 = NULL;
127 
128     /* index is 0 */
129     child1 = lv_obj_create(parent);
130 
131     lv_obj_move_to_index(child1, 0);
132 
133     TEST_ASSERT_EQUAL_INT32(0xFFFFFFFF, lv_obj_get_index(child1));
134 }
135 
test_obj_move_to_index_no_operation_when_index_is_same_or_bigger_than_parent_child_count(void)136 void test_obj_move_to_index_no_operation_when_index_is_same_or_bigger_than_parent_child_count(void)
137 {
138     lv_obj_t * parent = NULL;
139     lv_obj_t * child1 = NULL;
140 
141     parent = lv_obj_create(lv_screen_active());
142     /* index is 0 */
143     child1 = lv_obj_create(parent);
144 
145     lv_obj_move_to_index(child1, 3U);
146 
147     TEST_ASSERT_EQUAL(0, lv_obj_get_index(child1));
148 }
149 
test_obj_move_to_index_no_operation_when_new_index_is_the_same_as_previous_index(void)150 void test_obj_move_to_index_no_operation_when_new_index_is_the_same_as_previous_index(void)
151 {
152     lv_obj_t * parent = NULL;
153     lv_obj_t * child1 = NULL;
154     lv_obj_t * child2 = NULL;
155 
156     parent = lv_obj_create(lv_screen_active());
157     /* index is 0 */
158     child1 = lv_obj_create(parent);
159     /* index is 1 */
160     child2 = lv_obj_create(parent);
161 
162     lv_obj_move_to_index(child2, 1U);
163 
164     TEST_ASSERT_EQUAL(0, lv_obj_get_index(child1));
165     TEST_ASSERT_EQUAL(1, lv_obj_get_index(child2));
166 }
167 
test_obj_move_to_index_no_operation_when_requested_negative_index_is_greater_than_child_count(void)168 void test_obj_move_to_index_no_operation_when_requested_negative_index_is_greater_than_child_count(void)
169 {
170     lv_obj_t * parent = NULL;
171     lv_obj_t * child1 = NULL;
172     lv_obj_t * child2 = NULL;
173 
174     parent = lv_obj_create(lv_screen_active());
175     /* index is 0 */
176     child1 = lv_obj_create(parent);
177     /* index is 1 */
178     child2 = lv_obj_create(parent);
179 
180     lv_obj_move_to_index(child1, -4);
181 
182     TEST_ASSERT_EQUAL(0, lv_obj_get_index(child1));
183     TEST_ASSERT_EQUAL(1, lv_obj_get_index(child2));
184 }
185 
186 #endif
187