1 #if LV_BUILD_TEST
2 #include "../lvgl.h"
3 #include "../../lvgl_private.h"
4
5 #include "unity/unity.h"
6
7 static lv_obj_t * scr = NULL;
8 static lv_obj_t * table = NULL;
9
setUp(void)10 void setUp(void)
11 {
12 scr = lv_screen_active();
13 table = lv_table_create(scr);
14 }
15
tearDown(void)16 void tearDown(void)
17 {
18 lv_obj_clean(lv_screen_active());
19 }
20
test_table_should_set_row_count_to_zero(void)21 void test_table_should_set_row_count_to_zero(void)
22 {
23 lv_table_set_row_count(table, 0);
24
25 TEST_ASSERT_EQUAL_UINT32(0, lv_table_get_row_count(table));
26 }
27
test_table_should_return_assigned_cell_value(void)28 void test_table_should_return_assigned_cell_value(void)
29 {
30 uint16_t row = 0;
31 uint16_t column = 0;
32 const char * value = "LVGL";
33
34 lv_table_set_cell_value(table, row, column, value);
35
36 TEST_ASSERT_EQUAL_STRING(value, lv_table_get_cell_value(table, row, column));
37 }
38
test_table_should_grow_columns_automatically_when_setting_formatted_cell_value(void)39 void test_table_should_grow_columns_automatically_when_setting_formatted_cell_value(void)
40 {
41 /* Newly created tables have 1 column and 1 row */
42 uint16_t original_column_count = lv_table_get_column_count(table);
43 TEST_ASSERT_EQUAL_UINT16(1, original_column_count);
44
45 /* Table currently only has a cell at 0,0 (row, column) */
46 lv_table_set_cell_value_fmt(table, 0, 1, "LVGL %s", "Rocks!");
47
48 /* Table now should have cells at 0,0 and 0,1, so 2 columns */
49 uint16_t expected_column_count = original_column_count + 1;
50 TEST_ASSERT_EQUAL_UINT16(expected_column_count, lv_table_get_column_count(table));
51 }
52
test_table_should_identify_cell_with_ctrl(void)53 void test_table_should_identify_cell_with_ctrl(void)
54 {
55 bool has_ctrl = false;
56
57 has_ctrl = lv_table_has_cell_ctrl(table, 0, 0, LV_TABLE_CELL_CTRL_MERGE_RIGHT);
58
59 TEST_ASSERT_FALSE(has_ctrl);
60
61 lv_table_add_cell_ctrl(table, 0, 0, LV_TABLE_CELL_CTRL_MERGE_RIGHT);
62 has_ctrl = lv_table_has_cell_ctrl(table, 0, 0, LV_TABLE_CELL_CTRL_MERGE_RIGHT);
63 TEST_ASSERT_TRUE(has_ctrl);
64 }
65
test_table_should_clear_selected_cell_ctrl(void)66 void test_table_should_clear_selected_cell_ctrl(void)
67 {
68 bool has_ctrl = false;
69
70 lv_table_add_cell_ctrl(table, 0, 0, LV_TABLE_CELL_CTRL_MERGE_RIGHT);
71 has_ctrl = lv_table_has_cell_ctrl(table, 0, 0, LV_TABLE_CELL_CTRL_MERGE_RIGHT);
72 TEST_ASSERT_TRUE(has_ctrl);
73
74 lv_table_clear_cell_ctrl(table, 0, 0, LV_TABLE_CELL_CTRL_MERGE_RIGHT);
75 has_ctrl = lv_table_has_cell_ctrl(table, 0, 0, LV_TABLE_CELL_CTRL_MERGE_RIGHT);
76 TEST_ASSERT_FALSE(has_ctrl);
77 }
78
test_table_should_keep_not_selected_cell_ctrl(void)79 void test_table_should_keep_not_selected_cell_ctrl(void)
80 {
81 bool has_ctrl = false;
82
83 lv_table_add_cell_ctrl(table, 0, 0, LV_TABLE_CELL_CTRL_MERGE_RIGHT | LV_TABLE_CELL_CTRL_TEXT_CROP);
84
85 lv_table_clear_cell_ctrl(table, 0, 0, LV_TABLE_CELL_CTRL_MERGE_RIGHT);
86 has_ctrl = lv_table_has_cell_ctrl(table, 0, 0, LV_TABLE_CELL_CTRL_MERGE_RIGHT);
87 TEST_ASSERT_FALSE(has_ctrl);
88
89 has_ctrl = lv_table_has_cell_ctrl(table, 0, 0, LV_TABLE_CELL_CTRL_TEXT_CROP);
90 TEST_ASSERT_TRUE(has_ctrl);
91 }
92
93 /* We're using a newly created table */
test_table_cell_value_should_return_empty_string_when_cell_is_empty(void)94 void test_table_cell_value_should_return_empty_string_when_cell_is_empty(void)
95 {
96 TEST_ASSERT_EQUAL_STRING("", lv_table_get_cell_value(table, 0, 0));
97 }
98
test_table_row_height_should_increase_with_multiline_cell_value(void)99 void test_table_row_height_should_increase_with_multiline_cell_value(void)
100 {
101 lv_table_t * table_ptr = (lv_table_t *) table;
102 const char * singleline_value = "LVGL";
103 const char * multiline_value = "LVGL\nRocks";
104
105 lv_table_set_cell_value(table, 0, 0, singleline_value);
106 int32_t singleline_row_height = table_ptr->row_h[0];
107
108 lv_table_set_cell_value(table, 0, 0, multiline_value);
109 int32_t multiline_row_height = table_ptr->row_h[0];
110
111 TEST_ASSERT_GREATER_THAN(singleline_row_height, multiline_row_height);
112 }
113
test_table_should_wrap_long_texts(void)114 void test_table_should_wrap_long_texts(void)
115 {
116 lv_table_t * table_ptr = (lv_table_t *) table;
117 const char * long_text = "Testing automatic text wrap with a very long text";
118 const char * small_text = "Hi";
119
120 lv_table_set_column_width(table, 0, 50);
121
122 lv_table_set_cell_value(table, 0, 0, small_text);
123 int32_t row_height = table_ptr->row_h[0];
124
125 lv_table_set_cell_value(table, 0, 0, long_text);
126 int32_t wrapped_row_height = table_ptr->row_h[0];
127
128 /* Row height on cells with wrapped text is bigger than cells with small texts */
129 TEST_ASSERT_GREATER_THAN(row_height, wrapped_row_height);
130 }
131
draw_part_event_cb(lv_event_t * e)132 static void draw_part_event_cb(lv_event_t * e)
133 {
134 lv_draw_task_t * draw_task = lv_event_get_draw_task(e);
135 lv_draw_dsc_base_t * base_dsc = draw_task->draw_dsc;
136 /*If the cells are drawn...*/
137 if(base_dsc->part == LV_PART_ITEMS) {
138 uint32_t row = base_dsc->id1;
139 uint32_t col = base_dsc->id2;
140
141 /*Make the texts in the first cell center aligned*/
142 if(row == 0) {
143 lv_draw_label_dsc_t * label_draw_dsc = lv_draw_task_get_label_dsc(draw_task);
144 if(label_draw_dsc) {
145 label_draw_dsc->align = LV_TEXT_ALIGN_CENTER;
146 }
147 lv_draw_fill_dsc_t * fill_draw_dsc = lv_draw_task_get_fill_dsc(draw_task);
148 if(fill_draw_dsc) {
149 fill_draw_dsc->color = lv_color_mix(lv_palette_main(LV_PALETTE_BLUE), fill_draw_dsc->color, LV_OPA_20);
150 fill_draw_dsc->opa = LV_OPA_COVER;
151 }
152 }
153 /*In the first column align the texts to the right*/
154 else if(col == 0) {
155 lv_draw_label_dsc_t * label_draw_dsc = lv_draw_task_get_label_dsc(draw_task);
156 if(label_draw_dsc) {
157 label_draw_dsc->align = LV_TEXT_ALIGN_RIGHT;
158 }
159 }
160
161 /*Make every 2nd row grayish*/
162 if((row != 0 && row % 2) == 0) {
163 lv_draw_fill_dsc_t * fill_draw_dsc = lv_draw_task_get_fill_dsc(draw_task);
164 if(fill_draw_dsc) {
165 fill_draw_dsc->color = lv_color_mix(lv_palette_main(LV_PALETTE_GREY), fill_draw_dsc->color, LV_OPA_10);
166 fill_draw_dsc->opa = LV_OPA_COVER;
167 }
168 }
169 }
170 }
171
test_table_rendering(void)172 void test_table_rendering(void)
173 {
174 lv_obj_center(table);
175 lv_obj_add_event_cb(table, draw_part_event_cb, LV_EVENT_DRAW_TASK_ADDED, NULL);
176 lv_obj_add_flag(table, LV_OBJ_FLAG_SEND_DRAW_TASK_EVENTS);
177 lv_obj_set_style_border_side(table, LV_BORDER_SIDE_FULL, LV_PART_ITEMS);
178 lv_obj_set_style_pad_all(table, 10, LV_PART_ITEMS);
179 lv_obj_set_style_border_width(table, 5, LV_PART_ITEMS);
180 lv_table_set_column_count(table, 5);
181 lv_table_set_row_count(table, 5);
182 lv_table_set_column_width(table, 1, 60);
183 lv_table_set_column_width(table, 2, 100);
184
185 lv_table_add_cell_ctrl(table, 0, 1, LV_TABLE_CELL_CTRL_MERGE_RIGHT);
186 lv_table_set_cell_value(table, 0, 1, "2 cells are merged");
187
188 lv_table_add_cell_ctrl(table, 1, 0, LV_TABLE_CELL_CTRL_MERGE_RIGHT);
189 lv_table_add_cell_ctrl(table, 1, 1, LV_TABLE_CELL_CTRL_MERGE_RIGHT);
190 lv_table_add_cell_ctrl(table, 1, 2, LV_TABLE_CELL_CTRL_MERGE_RIGHT);
191 lv_table_add_cell_ctrl(table, 1, 3, LV_TABLE_CELL_CTRL_MERGE_RIGHT);
192 lv_table_set_cell_value(table, 1, 0, "5 cells are merged");
193
194 uint32_t i;
195 for(i = 0; i < 5; i++) {
196 lv_table_set_cell_value_fmt(table, 3, i, "%d", i);
197 }
198
199 lv_table_set_cell_value_fmt(table, 2, 3, "Multi\nline text");
200 lv_table_set_cell_value_fmt(table, 2, 4, "Very long text wrapped automatically");
201
202 lv_table_add_cell_ctrl(table, 4, 3, LV_TABLE_CELL_CTRL_TEXT_CROP);
203 lv_table_set_cell_value_fmt(table, 4, 3, "crop crop crop crop crop crop crop crop ");
204
205 TEST_ASSERT_EQUAL_SCREENSHOT("widgets/table_1.png");
206 }
207
208 /* See #3120 for context */
test_table_should_reduce_cells(void)209 void test_table_should_reduce_cells(void)
210 {
211 const uint16_t initial_col_num = 8;
212 const uint16_t initial_row_num = 1;
213 const uint16_t final_col_num = 4;
214 const uint16_t final_row_num = 1;
215
216 lv_obj_center(table);
217
218 lv_table_set_column_count(table, initial_col_num);
219 lv_table_set_row_count(table, initial_row_num);
220
221 uint32_t row_idx, col_idx;
222 for(row_idx = 0; row_idx < initial_row_num; row_idx++) {
223 for(col_idx = 0; col_idx < initial_col_num; col_idx++) {
224 lv_table_set_cell_value(table, row_idx, col_idx, "00");
225 }
226 }
227
228 lv_table_set_column_count(table, final_col_num);
229 lv_table_set_row_count(table, final_row_num);
230
231 for(row_idx = 0; row_idx < final_row_num; row_idx++) {
232 for(col_idx = 0; col_idx < final_col_num; col_idx++) {
233 lv_table_set_cell_value(table, row_idx, col_idx, "00");
234 }
235 }
236 }
237
238 /* See #3120 for context */
test_table_should_reduce_cells_with_more_than_one_row(void)239 void test_table_should_reduce_cells_with_more_than_one_row(void)
240 {
241 const uint16_t initial_col_num = 8;
242 const uint16_t initial_row_num = 2;
243 const uint16_t final_col_num = 4;
244 const uint16_t final_row_num = 1;
245
246 lv_obj_center(table);
247
248 lv_table_set_column_count(table, initial_col_num);
249 lv_table_set_row_count(table, initial_row_num);
250
251 uint32_t row_idx, col_idx;
252 for(row_idx = 0; row_idx < initial_row_num; row_idx++) {
253 for(col_idx = 0; col_idx < initial_col_num; col_idx++) {
254 lv_table_set_cell_value(table, row_idx, col_idx, "00");
255 }
256 }
257
258 lv_table_set_column_count(table, final_col_num);
259 lv_table_set_row_count(table, final_row_num);
260
261 for(row_idx = 0; row_idx < final_row_num; row_idx++) {
262 for(col_idx = 0; col_idx < final_col_num; col_idx++) {
263 lv_table_set_cell_value(table, row_idx, col_idx, "00");
264 }
265 }
266 }
267
test_table_should_set_selected_cell(void)268 void test_table_should_set_selected_cell(void)
269 {
270 lv_table_set_row_count(table, 2);
271 lv_table_set_column_count(table, 2);
272
273 lv_table_set_selected_cell(table, 1, 1);
274
275 uint32_t selected_row = 0;
276 uint32_t selected_column = 0;
277
278 lv_table_get_selected_cell(table, &selected_row, &selected_column);
279
280 TEST_ASSERT_EQUAL_UINT32(1, selected_row);
281 TEST_ASSERT_EQUAL_UINT32(1, selected_column);
282 }
283
test_table_cell_select_should_not_exceed_table_bounds(void)284 void test_table_cell_select_should_not_exceed_table_bounds(void)
285 {
286 lv_table_set_row_count(table, 2);
287 lv_table_set_column_count(table, 2);
288
289 lv_table_set_selected_cell(table, 2, 2);
290
291 uint32_t selected_row = 0;
292 uint32_t selected_column = 0;
293
294 lv_table_get_selected_cell(table, &selected_row, &selected_column);
295
296 TEST_ASSERT_EQUAL_UINT32(1, selected_row);
297 TEST_ASSERT_EQUAL_UINT32(1, selected_column);
298 }
299
test_table_cell_select_should_not_allow_set_on_table_with_no_rows(void)300 void test_table_cell_select_should_not_allow_set_on_table_with_no_rows(void)
301 {
302 lv_table_set_row_count(table, 0);
303 lv_table_set_column_count(table, 5);
304
305 lv_table_set_selected_cell(table, 4, 4);
306
307 uint32_t selected_row = 0;
308 uint32_t selected_column = 0;
309
310 lv_table_get_selected_cell(table, &selected_row, &selected_column);
311
312 TEST_ASSERT_EQUAL_UINT32(LV_TABLE_CELL_NONE, selected_row);
313 TEST_ASSERT_EQUAL_UINT32(LV_TABLE_CELL_NONE, selected_column);
314 }
315
316 #endif
317