1 #if LV_BUILD_TEST
2 #include "../lvgl.h"
3 #include "../../lvgl_private.h"
4 
5 #include "unity/unity.h"
6 #include "lv_test_indev.h"
7 
8 /* This function runs before each test */
9 void setUp(void);
10 /* This function runs after every test */
11 void tearDown(void);
12 
13 void test_msgbox_creation_successful_with_close_button(void);
14 void test_msgbox_creation_successful_no_close_button(void);
15 void test_msgbox_creation_successful_modal(void);
16 void test_msgbox_get_title(void);
17 void test_msgbox_get_content(void);
18 void test_msgbox_close(void);
19 void test_msgbox_close_modal(void);
20 void test_msgbox_close_async(void);
21 void test_msgbox_close_async_modal(void);
22 
23 static lv_obj_t * active_screen = NULL;
24 static lv_obj_t * msgbox = NULL;
25 
setUp(void)26 void setUp(void)
27 {
28     active_screen = lv_screen_active();
29 }
30 
tearDown(void)31 void tearDown(void)
32 {
33     lv_obj_clean(active_screen);
34     lv_obj_clean(lv_layer_top()); /*Modal message boxes are created on the top layer*/
35 }
36 
test_msgbox_creation_successful_with_close_button(void)37 void test_msgbox_creation_successful_with_close_button(void)
38 {
39     msgbox = lv_msgbox_create(active_screen);
40     lv_msgbox_add_title(msgbox, "The title");
41     lv_msgbox_add_text(msgbox, "The text");
42     lv_msgbox_add_footer_button(msgbox, "Apply");
43     lv_msgbox_add_footer_button(msgbox, "Close");
44     lv_msgbox_add_header_button(msgbox, LV_SYMBOL_AUDIO);
45     lv_msgbox_add_close_button(msgbox);
46 
47     TEST_ASSERT_NOT_NULL(msgbox);
48 
49     TEST_ASSERT_EQUAL_SCREENSHOT("widgets/msgbox_ok_with_close_btn.png");
50 }
51 
test_msgbox_creation_successful_no_close_button(void)52 void test_msgbox_creation_successful_no_close_button(void)
53 {
54     msgbox = lv_msgbox_create(NULL);
55     lv_msgbox_add_title(msgbox, "The title");
56     lv_msgbox_add_text(msgbox, "The text");
57     lv_msgbox_add_footer_button(msgbox, "Apply");
58     lv_msgbox_add_footer_button(msgbox, "Close");
59     lv_msgbox_add_header_button(msgbox, LV_SYMBOL_AUDIO);
60 
61     TEST_ASSERT_NOT_NULL(msgbox);
62 
63     TEST_ASSERT_EQUAL_SCREENSHOT("widgets/msgbox_ok_no_close_btn.png");
64 }
65 
test_msgbox_creation_successful_modal(void)66 void test_msgbox_creation_successful_modal(void)
67 {
68     // If parent is NULL the message box will be modal
69     msgbox = lv_msgbox_create(NULL);
70     lv_msgbox_add_title(msgbox, "The title");
71     lv_msgbox_add_text(msgbox, "The text");
72     lv_msgbox_add_footer_button(msgbox, "Apply");
73     lv_msgbox_add_footer_button(msgbox, "Close");
74     lv_msgbox_add_header_button(msgbox, LV_SYMBOL_AUDIO);
75     lv_msgbox_add_close_button(msgbox);
76 
77     TEST_ASSERT_NOT_NULL(msgbox);
78 
79     // Since msgbox has no parent, it won´t be clean up at tearDown()
80     lv_obj_clean(msgbox);
81 }
82 
test_msgbox_get_title(void)83 void test_msgbox_get_title(void)
84 {
85     const char * txt_title = "The title";
86     lv_obj_t * lbl_title = NULL;
87 
88     msgbox = lv_msgbox_create(active_screen);
89     lv_msgbox_add_title(msgbox, "The title");
90     lv_msgbox_add_text(msgbox, "The text");
91     lv_msgbox_add_footer_button(msgbox, "Apply");
92     lv_msgbox_add_footer_button(msgbox, "Close");
93     lv_msgbox_add_header_button(msgbox, LV_SYMBOL_AUDIO);
94     lv_msgbox_add_close_button(msgbox);
95 
96     // Msgbox title is a lv_label widget
97     lbl_title = lv_msgbox_get_title(msgbox);
98 
99     TEST_ASSERT_EQUAL_STRING(txt_title, lv_label_get_text(lbl_title));
100 }
101 
test_msgbox_get_content(void)102 void test_msgbox_get_content(void)
103 {
104     msgbox = lv_msgbox_create(active_screen);
105 
106     TEST_ASSERT_NOT_NULL(lv_msgbox_get_content(msgbox));
107 }
108 
test_msgbox_close(void)109 void test_msgbox_close(void)
110 {
111     msgbox = lv_msgbox_create(active_screen);
112     lv_msgbox_add_text(msgbox, "The text");
113 
114     lv_msgbox_close(msgbox);
115 
116     // lv_msgbox_close deletes the message box
117     TEST_ASSERT_NOT_NULL(msgbox);
118 }
119 
test_msgbox_close_modal(void)120 void test_msgbox_close_modal(void)
121 {
122     msgbox = lv_msgbox_create(NULL);
123     lv_msgbox_add_text(msgbox, "The text");
124 
125     lv_msgbox_close(msgbox);
126 
127     // lv_msgbox_close deletes the message box
128     TEST_ASSERT_NOT_NULL(msgbox);
129 }
130 
test_msgbox_close_async(void)131 void test_msgbox_close_async(void)
132 {
133     msgbox = lv_msgbox_create(active_screen);
134     lv_msgbox_add_text(msgbox, "The text");
135 
136     // lv_msgbox_close deletes the message box
137     TEST_ASSERT_NOT_NULL(msgbox);
138 }
139 
test_msgbox_close_async_modal(void)140 void test_msgbox_close_async_modal(void)
141 {
142     msgbox = lv_msgbox_create(NULL);
143     lv_msgbox_add_text(msgbox, "The text");
144 
145     // lv_msgbox_close deletes the message box
146     TEST_ASSERT_NOT_NULL(msgbox);
147 }
148 
test_msgbox_content_auto_height(void)149 void test_msgbox_content_auto_height(void)
150 {
151     /* If parent is NULL the message box will be modal*/
152     msgbox = lv_msgbox_create(NULL);
153     lv_msgbox_add_title(msgbox, "The title");
154     lv_msgbox_add_text(msgbox, "The text");
155     lv_msgbox_add_footer_button(msgbox, "Apply");
156     lv_msgbox_add_footer_button(msgbox, "Close");
157     lv_msgbox_add_header_button(msgbox, LV_SYMBOL_AUDIO);
158     lv_msgbox_add_close_button(msgbox);
159 
160     /* Test1 : msgbox's height is LV_SIZE_CONTENT by default */
161     bool is_height_size_content = (lv_obj_get_style_height(msgbox, 0) == LV_SIZE_CONTENT);
162     TEST_ASSERT_EQUAL(is_height_size_content, 1);
163 
164     lv_obj_update_layout(msgbox);
165     lv_obj_t * header = lv_msgbox_get_header(msgbox);
166     lv_obj_t * footer = lv_msgbox_get_footer(msgbox);
167     lv_obj_t * content = lv_msgbox_get_content(msgbox);
168 
169     int32_t h_header = (header == NULL) ? 0 : lv_obj_get_height(header);
170     int32_t h_footer = (footer == NULL) ? 0 : lv_obj_get_height(footer);
171     int32_t h_content = lv_obj_get_height(content);
172 
173     int32_t h_obj_content = lv_obj_get_content_height(msgbox);
174     int32_t h_msgbox_element_sum  = h_header + h_footer + h_content;
175     /* Default Size : The height of the msgbox's obj-content should be equal to the total height of the msgbox's element. */
176     TEST_ASSERT_EQUAL(h_obj_content, h_msgbox_element_sum);
177 
178     /* Test2 : Now change size of msgbox manually*/
179     lv_obj_set_size(msgbox, lv_pct(80), lv_pct(80));
180 
181     is_height_size_content = (lv_obj_get_style_height(msgbox, 0) == LV_SIZE_CONTENT);
182     TEST_ASSERT_EQUAL(is_height_size_content, 0);
183 
184     lv_obj_update_layout(msgbox);
185     h_header = (header == NULL) ? 0 : lv_obj_get_height(header);
186     h_footer = (footer == NULL) ? 0 : lv_obj_get_height(footer);
187     h_content = lv_obj_get_height(content);
188 
189     h_obj_content = lv_obj_get_content_height(msgbox);
190     h_msgbox_element_sum  = h_header + h_footer + h_content;
191     /* Manual Size : The height of the msgbox's obj-content should also be equal to the total height of the msgbox's element. */
192     TEST_ASSERT_EQUAL(h_obj_content, h_msgbox_element_sum);
193 }
194 
195 #endif
196