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 #include <string.h>
8
9 #define OPTION_BUFFER_SZ (20U)
10 #define OPTION_SMALL_BUFFER_SZ (3U)
11
12 static lv_obj_t * active_screen = NULL;
13 static lv_obj_t * roller = NULL;
14 static lv_obj_t * roller_infinite = NULL;
15 static lv_obj_t * roller_mouse = NULL;
16 static lv_group_t * g = NULL;
17 static lv_group_t * encoder_g = NULL;
18 static lv_group_t * mouse_g = NULL;
19
20 static const char * default_roller_options = "One\nTwo\nThree";
21 static const char * default_infinite_roller_options = "One\nTwo\nThree\nFour\nFive\nSix\nSeven\nEight\nNine\nTen";
22
setUp(void)23 void setUp(void)
24 {
25 active_screen = lv_screen_active();
26 roller = lv_roller_create(active_screen);
27 roller_infinite = lv_roller_create(active_screen);
28 roller_mouse = lv_roller_create(active_screen);
29
30 lv_roller_set_options(roller, default_roller_options, LV_ROLLER_MODE_NORMAL);
31 lv_roller_set_options(roller_infinite, default_infinite_roller_options, LV_ROLLER_MODE_INFINITE);
32 lv_roller_set_options(roller_mouse, default_roller_options, LV_ROLLER_MODE_NORMAL);
33
34 g = lv_group_create();
35 lv_indev_set_group(lv_test_keypad_indev, g);
36
37 encoder_g = lv_group_create();
38 lv_indev_set_group(lv_test_encoder_indev, encoder_g);
39
40 mouse_g = lv_group_create();
41 lv_indev_set_group(lv_test_mouse_indev, mouse_g);
42
43 lv_group_add_obj(g, roller);
44 lv_group_add_obj(encoder_g, roller_infinite);
45 lv_group_add_obj(mouse_g, roller_mouse);
46 }
47
tearDown(void)48 void tearDown(void)
49 {
50 lv_group_remove_obj(roller);
51 lv_group_remove_obj(roller_infinite);
52 lv_group_remove_obj(roller_mouse);
53 lv_obj_clean(active_screen);
54 }
55
test_roller_get_options(void)56 void test_roller_get_options(void)
57 {
58 TEST_ASSERT_EQUAL_STRING(default_roller_options, lv_roller_get_options(roller));
59 }
60
test_roller_get_selected_option(void)61 void test_roller_get_selected_option(void)
62 {
63 char actual_str[OPTION_BUFFER_SZ] = {0x00};
64 int16_t expected_index = 2;
65 int16_t actual_index = 0;
66 char * expected_index_str = "Three";
67
68 /* Select the last option, index starts at 0 */
69 uint16_t option_count = lv_roller_get_option_count(roller);
70 lv_roller_set_selected(roller, option_count - 1, LV_ANIM_OFF);
71
72 actual_index = lv_roller_get_selected(roller);
73 TEST_ASSERT_EQUAL(expected_index, actual_index);
74
75 /* Get the index string */
76 lv_roller_get_selected_str(roller, actual_str, OPTION_BUFFER_SZ);
77
78 TEST_ASSERT_EQUAL_STRING(expected_index_str, actual_str);
79 }
80
test_roller_get_selected_option_truncated_buffer(void)81 void test_roller_get_selected_option_truncated_buffer(void)
82 {
83 char actual_str[OPTION_SMALL_BUFFER_SZ] = {0x00};
84 char * expected_index_str = "Th";
85
86 /* Select the last option, index starts at 0 */
87 uint16_t option_count = lv_roller_get_option_count(roller);
88 lv_roller_set_selected(roller, option_count - 1, LV_ANIM_OFF);
89
90 /* Get the index string */
91 lv_roller_get_selected_str(roller, actual_str, OPTION_SMALL_BUFFER_SZ);
92
93 TEST_ASSERT_EQUAL_STRING(expected_index_str, actual_str);
94 }
95
test_roller_infinite_mode_get_selected_option(void)96 void test_roller_infinite_mode_get_selected_option(void)
97 {
98 char actual_str[OPTION_BUFFER_SZ] = {0x00};
99 int16_t expected_index = 9;
100 int16_t actual_index = 0;
101
102 /* Select the last option of page 2 */
103 uint16_t option_count = lv_roller_get_option_count(roller_infinite);
104 option_count = (option_count * 2) - 1;
105 lv_roller_set_selected(roller_infinite, option_count, LV_ANIM_OFF);
106
107 actual_index = lv_roller_get_selected(roller_infinite);
108 TEST_ASSERT_EQUAL(expected_index, actual_index);
109
110 /* Get the index string */
111 lv_roller_get_selected_str(roller_infinite, actual_str, OPTION_BUFFER_SZ);
112
113 TEST_ASSERT_EQUAL_STRING("Ten", actual_str);
114 memset(actual_str, 0x00, OPTION_BUFFER_SZ);
115
116 /* Select the second option of page */
117 lv_roller_set_selected(roller_infinite, 1, LV_ANIM_OFF);
118
119 actual_index = lv_roller_get_selected(roller_infinite);
120 TEST_ASSERT_EQUAL(1, actual_index);
121
122 /* Get the index string */
123 lv_roller_get_selected_str(roller_infinite, actual_str, OPTION_BUFFER_SZ);
124
125 TEST_ASSERT_EQUAL_STRING("Two", actual_str);
126 }
127
test_roller_set_selected_option_str(void)128 void test_roller_set_selected_option_str(void)
129 {
130 bool selected;
131 TEST_ASSERT_EQUAL(0, lv_roller_get_selected(roller));
132
133 /* Test an item that exists in the roller */
134 selected = lv_roller_set_selected_str(roller, "Two", LV_ANIM_OFF);
135 TEST_ASSERT_TRUE(selected);
136
137 TEST_ASSERT_EQUAL(1, lv_roller_get_selected(roller));
138
139 /* Try to select an item that does not exist in the roller */
140 selected = lv_roller_set_selected_str(roller, "No", LV_ANIM_OFF);
141 TEST_ASSERT_FALSE(selected);
142
143 /* Make sure that the selection did not change */
144 TEST_ASSERT_EQUAL(1, lv_roller_get_selected(roller));
145 }
146
test_roller_keypad_events(void)147 void test_roller_keypad_events(void)
148 {
149 int16_t expected_index = 1;
150 int16_t actual_index = 0;
151
152 lv_test_indev_wait(20);
153
154 return;
155
156 /* Select option index 1 with LV_KEY_RIGHT event */
157 lv_roller_set_selected(roller, 0, LV_ANIM_OFF);
158 lv_test_key_hit(LV_KEY_RIGHT);
159
160 actual_index = lv_roller_get_selected(roller);
161 TEST_ASSERT_EQUAL(expected_index, actual_index);
162
163 /* Select next option with LV_KEY_DOWN */
164 expected_index = 2;
165 lv_test_key_hit(LV_KEY_DOWN);
166
167 actual_index = lv_roller_get_selected(roller);
168 TEST_ASSERT_EQUAL(expected_index, actual_index);
169
170 /* Select previous option with LV_KEY_LEFT */
171 expected_index = 1;
172 lv_test_key_hit(LV_KEY_LEFT);
173
174 actual_index = lv_roller_get_selected(roller);
175 TEST_ASSERT_EQUAL(expected_index, actual_index);
176
177 /* Select previous option with LV_KEY_UP */
178 expected_index = 0;
179 lv_test_key_hit(LV_KEY_UP);
180
181 actual_index = lv_roller_get_selected(roller);
182 TEST_ASSERT_EQUAL(expected_index, actual_index);
183 }
184
test_roller_with_overlay_and_bubble_events_enabled(void)185 void test_roller_with_overlay_and_bubble_events_enabled(void)
186 {
187 lv_obj_t * overlay = lv_obj_create(roller);
188 lv_obj_add_flag(overlay, LV_OBJ_FLAG_EVENT_BUBBLE);
189
190 lv_obj_send_event(overlay, LV_EVENT_PRESSED, NULL);
191 }
192
193 //void test_roller_infinite_mode_first_option_gets_selected_after_last_option(void)
194 //{
195 // char actual_str[OPTION_BUFFER_SZ] = {0x00};
196 //
197 // lv_group_remove_obj(roller);
198 // lv_group_add_obj(g, roller_infinite);
199 //
200 // /* Select the last option of page 2 */
201 // uint16_t option_count = lv_roller_get_option_count(roller_infinite);
202 // option_count = (option_count * 2) - 1;
203 // lv_roller_set_selected(roller_infinite, option_count, LV_ANIM_OFF);
204 //
205 // /* Get the index string */
206 // lv_roller_get_selected_str(roller_infinite, actual_str, OPTION_BUFFER_SZ);
207 //
208 // TEST_ASSERT_EQUAL_STRING("Ten", actual_str);
209 // memset(actual_str, 0x00, OPTION_BUFFER_SZ);
210 //
211 // lv_test_key_hit(LV_KEY_DOWN);
212 //
213 // /* Get the index string */
214 // lv_roller_get_selected_str(roller_infinite, actual_str, OPTION_BUFFER_SZ);
215 // TEST_ASSERT_EQUAL_STRING("One", actual_str);
216 //
217 // lv_group_remove_obj(roller_infinite);
218 //}
219
220 //void test_roller_rendering_test(void)
221 //{
222 //#if LV_FONT_MONTSERRAT_24
223 // static lv_style_t style_sel;
224 // lv_style_init(&style_sel);
225 // lv_style_set_text_font(&style_sel, &lv_font_montserrat_24);
226 // lv_style_set_bg_color(&style_sel, lv_color_hex3(0xf88));
227 // lv_style_set_border_width(&style_sel, 2);
228 // lv_style_set_border_color(&style_sel, lv_color_hex3(0xf00));
229 //
230 // lv_obj_add_style(roller, &style_sel, LV_PART_SELECTED);
231 // lv_obj_set_style_text_align(roller, LV_TEXT_ALIGN_RIGHT, 0);
232 // lv_roller_set_options(roller, "One\nTwo\nThree\nFour\nFive", LV_ROLLER_MODE_NORMAL);
233 // lv_roller_set_selected(roller, 1, LV_ANIM_OFF);
234 // lv_obj_center(roller);
235 //
236 // TEST_ASSERT_EQUAL_SCREENSHOT("widgets/roller_1.png");
237 //#else
238 // TEST_PASS();
239 //#endif
240 //}
241 //
242 //void test_roller_select_option_with_click(void)
243 //{
244 // char actual_str[OPTION_BUFFER_SZ] = {0x00};
245 //
246 // lv_test_encoder_click();
247 // lv_test_encoder_turn(1);
248 //
249 // /* Get the index string */
250 // lv_roller_get_selected_str(roller_infinite, actual_str, OPTION_BUFFER_SZ);
251 //
252 // TEST_ASSERT_EQUAL_STRING("Two", actual_str);
253 // memset(actual_str, 0x00, OPTION_BUFFER_SZ);
254 //}
255 //
256 //void test_roller_release_handler_pointer_indev(void)
257 //{
258 // /* Click in the widget */
259 // lv_test_mouse_click_at(roller_mouse->coords.x1 + 5, roller_mouse->coords.y1 + 5);
260 // /* Check which is the selected option */
261 // TEST_ASSERT_EQUAL(0, lv_roller_get_selected(roller_mouse));
262 //
263 // /* Click further down the roller */
264 // lv_test_mouse_click_at(roller_mouse->coords.x1 + 5, roller_mouse->coords.y1 + 100);
265 // /* Check which is the selected option */
266 // TEST_ASSERT_NOT_EQUAL(0, lv_roller_get_selected(roller_mouse));
267 //}
268
test_roller_appearance(void)269 void test_roller_appearance(void)
270 {
271 /* use a number, a symbol, a high letter, a low letter */
272 const char * opts =
273 "0@Tg\n0@Tg\n0@Tg\n0@Tg\n0@Tg\n0@Tg\n0@Tg\n0@Tg\n0@Tg\n0@Tg\n0@Tg\n0@Tg\n0@Tg\n0@Tg\n0@Tg\n0@Tg\n0@Tg\n0@Tg\n0@Tg\n0@Tg\n0@Tg";
274
275 lv_obj_add_flag(roller_mouse, LV_OBJ_FLAG_HIDDEN);
276
277 lv_obj_t * rollers[10] = {roller, roller_infinite};
278 uint32_t i = 2;
279
280 /* a normal and infinite roller with the same size font for the main and selected parts */
281 lv_obj_set_pos(roller, 20, 20);
282 lv_roller_set_options(roller, opts, LV_ROLLER_MODE_NORMAL);
283 lv_obj_set_pos(roller_infinite, 20, 200);
284 lv_roller_set_options(roller_infinite, opts, LV_ROLLER_MODE_INFINITE);
285
286 /* a normal and infinite roller with slightly different size fonts for the main and selected parts */
287 lv_obj_t * r = lv_roller_create(active_screen);
288 lv_obj_set_pos(r, 130, 20);
289 lv_roller_set_options(r, opts, LV_ROLLER_MODE_NORMAL);
290 lv_obj_set_style_text_font(r, &lv_font_montserrat_16, LV_PART_MAIN);
291 lv_obj_set_style_text_font(r, &lv_font_montserrat_24, LV_PART_SELECTED);
292 rollers[i++] = r;
293 r = lv_roller_create(active_screen);
294 lv_obj_set_pos(r, 130, 200);
295 lv_roller_set_options(r, opts, LV_ROLLER_MODE_INFINITE);
296 lv_obj_set_style_text_font(r, &lv_font_montserrat_16, LV_PART_MAIN);
297 lv_obj_set_style_text_font(r, &lv_font_montserrat_24, LV_PART_SELECTED);
298 rollers[i++] = r;
299
300 /* same as previous pair but the fonts are swapped for the main and selected parts */
301 r = lv_roller_create(active_screen);
302 lv_obj_set_pos(r, 270, 20);
303 lv_roller_set_options(r, opts, LV_ROLLER_MODE_NORMAL);
304 lv_obj_set_style_text_font(r, &lv_font_montserrat_24, LV_PART_MAIN);
305 lv_obj_set_style_text_font(r, &lv_font_montserrat_16, LV_PART_SELECTED);
306 rollers[i++] = r;
307 r = lv_roller_create(active_screen);
308 lv_obj_set_pos(r, 270, 200);
309 lv_roller_set_options(r, opts, LV_ROLLER_MODE_INFINITE);
310 lv_obj_set_style_text_font(r, &lv_font_montserrat_24, LV_PART_MAIN);
311 lv_obj_set_style_text_font(r, &lv_font_montserrat_16, LV_PART_SELECTED);
312 rollers[i++] = r;
313
314 /* a normal and infinite roller with extremely different size fonts for the main and selected parts */
315 r = lv_roller_create(active_screen);
316 lv_obj_set_pos(r, 410, 20);
317 lv_roller_set_options(r, opts, LV_ROLLER_MODE_NORMAL);
318 lv_obj_set_style_text_font(r, &lv_font_montserrat_8, LV_PART_MAIN);
319 lv_obj_set_style_text_font(r, &lv_font_montserrat_36, LV_PART_SELECTED);
320 rollers[i++] = r;
321 r = lv_roller_create(active_screen);
322 lv_obj_set_pos(r, 410, 200);
323 lv_roller_set_options(r, opts, LV_ROLLER_MODE_INFINITE);
324 lv_obj_set_style_text_font(r, &lv_font_montserrat_8, LV_PART_MAIN);
325 lv_obj_set_style_text_font(r, &lv_font_montserrat_36, LV_PART_SELECTED);
326 rollers[i++] = r;
327
328 /* same as previous pair but the fonts are swapped for the main and selected parts */
329 r = lv_roller_create(active_screen);
330 lv_obj_set_pos(r, 580, 20);
331 lv_roller_set_options(r, opts, LV_ROLLER_MODE_NORMAL);
332 lv_obj_set_style_text_font(r, &lv_font_montserrat_36, LV_PART_MAIN);
333 lv_obj_set_style_text_font(r, &lv_font_montserrat_8, LV_PART_SELECTED);
334 rollers[i++] = r;
335 r = lv_roller_create(active_screen);
336 lv_obj_set_pos(r, 580, 200);
337 lv_roller_set_options(r, opts, LV_ROLLER_MODE_INFINITE);
338 lv_obj_set_style_text_font(r, &lv_font_montserrat_36, LV_PART_MAIN);
339 lv_obj_set_style_text_font(r, &lv_font_montserrat_8, LV_PART_SELECTED);
340 rollers[i++] = r;
341
342 TEST_ASSERT_EQUAL_SCREENSHOT("widgets/roller_2.png");
343
344 /* test that the selected label stays in sync with the main label for scrolling */
345 for(i = 0; i < 10; i++) {
346 lv_roller_set_selected(rollers[i], lv_roller_get_option_count(rollers[i]) - 1, LV_ANIM_OFF);
347 }
348
349 TEST_ASSERT_EQUAL_SCREENSHOT("widgets/roller_3.png");
350 }
351
test_roller_properties(void)352 void test_roller_properties(void)
353 {
354 #if LV_USE_OBJ_PROPERTY
355 lv_obj_t * obj = lv_roller_create(lv_screen_active());
356 lv_property_t prop = { };
357
358 prop.id = LV_PROPERTY_ROLLER_OPTIONS;
359 prop.arg1.ptr = "One\nTwo\nThree";
360 prop.arg2.num = LV_ROLLER_MODE_NORMAL;
361 TEST_ASSERT_TRUE(lv_obj_set_property(obj, &prop) == LV_RESULT_OK);
362 TEST_ASSERT_EQUAL_STRING("One\nTwo\nThree", lv_roller_get_options(obj));
363 TEST_ASSERT_EQUAL_STRING("One\nTwo\nThree", lv_obj_get_property(obj, LV_PROPERTY_ROLLER_OPTIONS).ptr);
364
365 prop.id = LV_PROPERTY_ROLLER_SELECTED;
366 prop.arg1.num = 1;
367 prop.arg2.enable = LV_ANIM_ON;
368 TEST_ASSERT_TRUE(lv_obj_set_property(obj, &prop) == LV_RESULT_OK);
369 TEST_ASSERT_EQUAL_INT(1, lv_roller_get_selected(obj));
370 TEST_ASSERT_EQUAL_INT(1, lv_obj_get_property(obj, LV_PROPERTY_ROLLER_SELECTED).num);
371 #endif
372 }
373
374 #endif
375