1 #if LV_BUILD_TEST
2 #include "../lvgl.h"
3 #include "../../lvgl_private.h"
4 #include "unity/unity.h"
5
6 static lv_obj_t * active_screen = NULL;
7 static lv_obj_t * win = NULL;
8 static lv_obj_t * header = NULL;
9 static lv_obj_t * content = NULL;
10
11 const char * dummy_text = "Hello LVGL!";
12
setUp(void)13 void setUp(void)
14 {
15 active_screen = lv_screen_active();
16 }
17
tearDown(void)18 void tearDown(void)
19 {
20 lv_obj_clean(active_screen);
21 }
22
test_win_should_have_valid_documented_default_values(void)23 void test_win_should_have_valid_documented_default_values(void)
24 {
25 // Create the win object and update layout
26 win = lv_win_create(active_screen);
27 lv_obj_update_layout(win);
28
29 // Check that the window has been created
30 TEST_ASSERT_NOT_NULL(win);
31
32 // Check that the correct children have been created
33 TEST_ASSERT_EQUAL(2, lv_obj_get_child_count(win));
34
35 header = lv_win_get_header(win);
36 content = lv_win_get_content(win);
37
38 TEST_ASSERT_EQUAL(header, lv_obj_get_child(win, 0));
39 TEST_ASSERT_EQUAL(content, lv_obj_get_child(win, 1));
40
41 // Check that the header is correctly sized and empty
42 TEST_ASSERT_EQUAL(lv_display_get_dpi(lv_obj_get_display(win)) / 2, lv_obj_get_height(header));
43 TEST_ASSERT_EQUAL(0, lv_obj_get_child_count(header));
44
45 // Check that the content is empty
46 TEST_ASSERT_EQUAL(0, lv_obj_get_child_count(content));
47 }
48
test_win_add_title_single(void)49 void test_win_add_title_single(void)
50 {
51 // Create the win object, get the header and update layout
52 win = lv_win_create(active_screen);
53 header = lv_win_get_header(win);
54 lv_obj_update_layout(win);
55
56 // Add a title to the window
57 lv_win_add_title(win, dummy_text);
58
59 // Check that no additional children have been created under win
60 // Instead the child should be created under header
61 TEST_ASSERT_EQUAL(2, lv_obj_get_child_count(win));
62 TEST_ASSERT_EQUAL(1, lv_obj_get_child_count(header));
63
64 // Check that the title is a label and has been created properly
65 lv_obj_t * title = lv_obj_get_child(header, 0);
66 TEST_ASSERT_EQUAL_STRING(dummy_text, lv_label_get_text(title));
67 TEST_ASSERT_EQUAL(1, lv_label_get_long_mode(title));
68 }
69
test_win_add_title_multiple(void)70 void test_win_add_title_multiple(void)
71 {
72 // Create the win object, get the header and update layout
73 win = lv_win_create(active_screen);
74 header = lv_win_get_header(win);
75 lv_obj_update_layout(win);
76
77 // Add two titles to the window
78 lv_win_add_title(win, dummy_text);
79 lv_win_add_title(win, dummy_text);
80
81 // Check that no additional children have been created under win
82 // Instead the child should be created under header
83 TEST_ASSERT_EQUAL(2, lv_obj_get_child_count(win));
84 TEST_ASSERT_EQUAL(2, lv_obj_get_child_count(header));
85 }
86
test_win_add_button(void)87 void test_win_add_button(void)
88 {
89 int win_button_width = 50;
90
91 // Create the win object, get the header and update layout
92 win = lv_win_create(active_screen);
93 header = lv_win_get_header(win);
94 lv_obj_update_layout(win);
95
96 // Add a button to the window header
97 lv_win_add_button(win, LV_SYMBOL_OK, win_button_width);
98 lv_obj_update_layout(win);
99
100 // Check that no additional children have been created under win
101 // Instead the child should be created under header
102 TEST_ASSERT_EQUAL(2, lv_obj_get_child_count(win));
103 TEST_ASSERT_EQUAL(1, lv_obj_get_child_count(header));
104
105 // Check that the button has been created properly
106 lv_obj_t * btn = lv_obj_get_child(header, 0);
107 TEST_ASSERT_EQUAL(1, lv_obj_get_child_count(btn));
108 TEST_ASSERT_EQUAL(win_button_width, lv_obj_get_width(btn));
109
110 // Check the output remains visually consistent
111 TEST_ASSERT_EQUAL_SCREENSHOT("widgets/win_01.png");
112 }
113
test_win_add_multiple_elements(void)114 void test_win_add_multiple_elements(void)
115 {
116 lv_obj_t * btn;
117 lv_obj_t * title;
118
119 int win_button_width = 50;
120 int win_button_close_width = 60;
121
122 // Create the win object, get the header and update layout
123 win = lv_win_create(active_screen);
124 lv_win_add_button(win, LV_SYMBOL_LEFT, win_button_width);
125 lv_win_add_title(win, dummy_text);
126 lv_win_add_button(win, LV_SYMBOL_RIGHT, win_button_width);
127 lv_win_add_button(win, LV_SYMBOL_CLOSE, win_button_close_width);
128
129 header = lv_win_get_header(win);
130 lv_obj_update_layout(win);
131
132 // Check that no additional children have been created under win
133 // Instead the child should be created under header
134 TEST_ASSERT_EQUAL(2, lv_obj_get_child_count(win));
135 TEST_ASSERT_EQUAL(4, lv_obj_get_child_count(header));
136
137 // Check that the left button has been created properly
138 btn = lv_obj_get_child(header, 0);
139 TEST_ASSERT_EQUAL(1, lv_obj_get_child_count(btn));
140 TEST_ASSERT_EQUAL(win_button_width, lv_obj_get_width(btn));
141
142 // Check that the title is a label and has been created properly
143 title = lv_obj_get_child(header, 1);
144 TEST_ASSERT_EQUAL_STRING(dummy_text, lv_label_get_text(title));
145 TEST_ASSERT_EQUAL(1, lv_label_get_long_mode(title));
146
147 // Check that the right button has been created properly
148 btn = lv_obj_get_child(header, 2);
149 TEST_ASSERT_EQUAL(1, lv_obj_get_child_count(btn));
150 TEST_ASSERT_EQUAL(win_button_width, lv_obj_get_width(btn));
151
152 // Check that the close button has been created properly
153 btn = lv_obj_get_child(header, 3);
154 TEST_ASSERT_EQUAL(1, lv_obj_get_child_count(btn));
155 TEST_ASSERT_EQUAL(win_button_close_width, lv_obj_get_width(btn));
156
157 // Check the output remains visually consistent
158 TEST_ASSERT_EQUAL_SCREENSHOT("widgets/win_02.png");
159 }
160
161 #endif
162