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 "lv_test_helpers.h"
8 
9 static void create_ui(void);
10 static void chart_type_observer_cb(lv_observer_t * observer, lv_subject_t * subject);
11 static void buttonmatrix_event_cb(lv_event_t * e);
12 static lv_obj_t * list_button_create(lv_obj_t * parent);
13 static void opa_anim_cb(void * var, int32_t v);
14 static void draw_to_canvas(lv_obj_t * canvas);
15 
setUp(void)16 void setUp(void)
17 {
18     /* Function run before every test */
19 }
20 
tearDown(void)21 void tearDown(void)
22 {
23     /* Function run after every test */
24 }
25 
test_binding(void)26 void test_binding(void)
27 {
28     create_ui();
29 
30     /*Wait for the animation*/
31     lv_test_indev_wait(500);
32 
33     TEST_ASSERT_EQUAL_SCREENSHOT("binding.png");
34 }
35 
create_ui(void)36 static void create_ui(void)
37 {
38     /*Create a colors*/
39     lv_color_t c1 = lv_color_hex(0xff0000);
40     lv_color_t c2 = lv_palette_darken(LV_PALETTE_BLUE, 2);
41     lv_color_t c3 = lv_color_mix(c1, c2, LV_OPA_60);
42 
43     /*Create a style*/
44     static lv_style_t style_big_font;
45     lv_style_init(&style_big_font);
46 
47     /*Use a built-in font*/
48     lv_style_set_text_font(&style_big_font, &lv_font_montserrat_24);
49 
50     /*Get the active screen*/
51     lv_obj_t * scr = lv_screen_active();
52 
53     /*Declare static array of integers, and test grid setting options*/
54     static int32_t gird_cols[] = {300, LV_GRID_FR(3), LV_GRID_FR(2), LV_GRID_TEMPLATE_LAST};
55     static int32_t gird_rows[] = {100, LV_GRID_FR(1), LV_GRID_CONTENT, LV_GRID_TEMPLATE_LAST};
56     lv_obj_set_grid_dsc_array(scr, gird_cols, gird_rows);
57 
58     static lv_subject_t chart_type_subject;
59     lv_subject_init_int(&chart_type_subject, 0);
60 
61     /*Create a widget*/
62     lv_obj_t * dropdown = lv_dropdown_create(scr);
63 
64     /*Pass a string as argument*/
65     lv_dropdown_set_options(dropdown, "Lines\nBars");
66 
67     /*Use grid align options*/
68     lv_obj_set_grid_cell(dropdown, LV_GRID_ALIGN_CENTER, 0, 1, LV_GRID_ALIGN_CENTER, 0, 1);
69 
70     /*Bind to a subject*/
71     lv_dropdown_bind_value(dropdown, &chart_type_subject);
72 
73     /*Create a chart with an external array of points*/
74     lv_obj_t * chart = lv_chart_create(lv_screen_active());
75     lv_obj_set_grid_cell(chart, LV_GRID_ALIGN_STRETCH, 0, 1, LV_GRID_ALIGN_CENTER, 1, 1);
76 
77     lv_chart_series_t * series = lv_chart_add_series(chart, c3, LV_CHART_AXIS_PRIMARY_X);
78 
79     static int32_t chart_y_array[10] = {10, 25, 50, 40, 30, 35, 60, 65, 70, 75};
80     lv_chart_set_ext_y_array(chart, series, chart_y_array);
81 
82     /*Add custom observer callback*/
83     lv_subject_add_observer_obj(&chart_type_subject, chart_type_observer_cb, chart, NULL);
84 
85     /*Manually set the subject's value*/
86     lv_subject_set_int(&chart_type_subject, 1);
87 
88     lv_obj_t * label = lv_label_create(scr);
89     lv_obj_set_grid_cell(label, LV_GRID_ALIGN_START, 1, 1, LV_GRID_ALIGN_CENTER, 0, 1);
90 
91     /*Apply styles on main part and default state*/
92     lv_obj_set_style_bg_opa(label, LV_OPA_70, 0);
93     lv_obj_set_style_bg_color(label, c1, 0);
94     lv_obj_set_style_text_color(label, c2, 0);
95     lv_obj_add_style(label, &style_big_font, 0);
96 
97     /*Declare an array of strings*/
98     static const char * btnmatrix_options[] = {
99         "First", "Second", "\n",
100         "Third", ""
101     };
102 
103     static const lv_buttonmatrix_ctrl_t btnmatrix_ctrl[] = {
104         LV_BUTTONMATRIX_CTRL_DISABLED, 2 | LV_BUTTONMATRIX_CTRL_CHECKED,
105         1,
106     };
107 
108     lv_obj_t * btnmatrix = lv_buttonmatrix_create(scr);
109     lv_obj_set_grid_cell(btnmatrix, LV_GRID_ALIGN_STRETCH, 1, 1, LV_GRID_ALIGN_STRETCH, 1, 1);
110     /*Pass string and enum arrays*/
111     lv_buttonmatrix_set_map(btnmatrix, btnmatrix_options);
112     lv_buttonmatrix_set_ctrl_map(btnmatrix, btnmatrix_ctrl);
113     /*Add style to non main part and non default state*/
114     lv_obj_add_style(btnmatrix, &style_big_font, LV_PART_ITEMS | LV_STATE_CHECKED);
115 
116     lv_buttonmatrix_set_selected_button(btnmatrix, 1);
117     lv_obj_add_event_cb(btnmatrix, buttonmatrix_event_cb, LV_EVENT_VALUE_CHANGED, label);
118     lv_obj_send_event(btnmatrix, LV_EVENT_VALUE_CHANGED, NULL);
119 
120     /*Create a base object*/
121     lv_obj_t * cont = lv_obj_create(scr);
122     /*Span 2 rows*/
123     lv_obj_set_grid_cell(cont, LV_GRID_ALIGN_STRETCH, 2, 1, LV_GRID_ALIGN_STRETCH, 0, 2);
124 
125     /*Apply flex layout*/
126     lv_obj_set_flex_flow(cont, LV_FLEX_FLOW_COLUMN);
127 
128     /*For loop*/
129     uint32_t i;
130     for(i = 0; i < 10; i++) {
131         /*Call a function implemented in the binding which returns a widget*/
132         lv_obj_t * btn = list_button_create(cont);
133 
134         if(i == 0) {
135             /*Start an animation*/
136             lv_anim_t a;
137             lv_anim_init(&a);
138             lv_anim_set_var(&a, btn);
139             lv_anim_set_values(&a, LV_OPA_COVER, LV_OPA_50);
140             lv_anim_set_exec_cb(&a, opa_anim_cb);   /*Pass a callback*/
141             lv_anim_set_duration(&a, 300);
142             lv_anim_set_path_cb(&a, lv_anim_path_ease_out);
143             lv_anim_start(&a);
144         }
145         if(i == 1) {
146             /*Use flags*/
147             lv_obj_add_flag(btn, LV_OBJ_FLAG_HIDDEN);
148         }
149         if(i == 2) {
150             lv_obj_t * btn_label = lv_obj_get_child(btn, 0);
151             lv_label_set_text(btn_label, "A multi-line text with a ° symbol");
152             lv_obj_set_width(btn_label, lv_pct(100));
153         }
154 
155         if(i == 3) {
156             /*Start an infinite animation and delete this button later*/
157             lv_anim_t a;
158             lv_anim_init(&a);
159             lv_anim_set_var(&a, btn);
160             lv_anim_set_values(&a, LV_OPA_COVER, LV_OPA_50);
161             lv_anim_set_exec_cb(&a, opa_anim_cb);   /*Pass a callback*/
162             lv_anim_set_duration(&a, 300);
163             lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);
164             lv_anim_start(&a);
165         }
166     }
167 
168     /*Wait and delete the button with the animation*/
169     lv_test_wait(300);
170     lv_obj_delete(lv_obj_get_child(cont, 3));
171 
172     /*Large byte array*/
173     static uint8_t canvas_buf[LV_CANVAS_BUF_SIZE(400, 100, 16, LV_DRAW_BUF_STRIDE_ALIGN)];
174 
175     lv_obj_t * canvas = lv_canvas_create(scr);
176     lv_obj_set_grid_cell(canvas, LV_GRID_ALIGN_START, 0, 2, LV_GRID_ALIGN_START, 2, 1);
177     /*Test RGB565 rendering*/
178     lv_canvas_set_buffer(canvas, lv_draw_buf_align(canvas_buf, LV_COLOR_FORMAT_RGB565), 400, 100, LV_COLOR_FORMAT_RGB565);
179     lv_canvas_fill_bg(canvas, c2, LV_OPA_COVER);
180     draw_to_canvas(canvas);
181 
182     LV_IMAGE_DECLARE(test_img_lvgl_logo_jpg);
183     lv_obj_t * img;
184     img = lv_image_create(scr);
185     lv_image_set_src(img, &test_img_lvgl_logo_jpg);
186     lv_obj_align(img, LV_ALIGN_BOTTOM_RIGHT, -20, -20);
187     lv_obj_add_flag(img, LV_OBJ_FLAG_IGNORE_LAYOUT);
188 
189     img = lv_image_create(scr);
190     lv_image_set_src(img, "A:src/test_assets/test_img_lvgl_logo.png");
191     lv_obj_set_pos(img, 500, 420);
192     lv_obj_add_flag(img, LV_OBJ_FLAG_IGNORE_LAYOUT);
193     lv_image_set_rotation(img, 200);
194     lv_image_set_scale_x(img, 400);
195 }
196 
chart_type_observer_cb(lv_observer_t * observer,lv_subject_t * subject)197 static void chart_type_observer_cb(lv_observer_t * observer, lv_subject_t * subject)
198 {
199     int32_t v = lv_subject_get_int(subject);
200     lv_obj_t * chart = lv_observer_get_target(observer);
201     lv_chart_set_type(chart, v == 0 ? LV_CHART_TYPE_LINE : LV_CHART_TYPE_BAR);
202 }
203 
buttonmatrix_event_cb(lv_event_t * e)204 static void buttonmatrix_event_cb(lv_event_t * e)
205 {
206     lv_obj_t * label = lv_event_get_user_data(e);
207     lv_obj_t * buttonmatrix = lv_event_get_target(e);
208 
209     uint32_t idx = lv_buttonmatrix_get_selected_button(buttonmatrix);
210     const char * text = lv_buttonmatrix_get_button_text(buttonmatrix, idx);
211     lv_label_set_text(label, text);
212 }
213 
list_button_create(lv_obj_t * parent)214 static lv_obj_t * list_button_create(lv_obj_t * parent)
215 {
216     lv_obj_t * btn = lv_button_create(parent);
217     lv_obj_set_size(btn, lv_pct(100), LV_SIZE_CONTENT);
218 
219     /*Get an integer*/
220     uint32_t idx = lv_obj_get_index(btn);
221 
222     /*Formatted string for label*/
223     lv_obj_t * label = lv_label_create(btn);
224     lv_label_set_text_fmt(label, LV_SYMBOL_FILE " Item %d", idx);
225 
226     return btn;
227 }
228 
opa_anim_cb(void * var,int32_t v)229 static void opa_anim_cb(void * var, int32_t v)
230 {
231     lv_obj_set_style_opa(var, v, 0);
232 }
233 
draw_to_canvas(lv_obj_t * canvas)234 static void draw_to_canvas(lv_obj_t * canvas)
235 {
236     lv_layer_t layer;
237     lv_canvas_init_layer(canvas, &layer);
238 
239     /*Use draw descriptors*/
240     LV_IMAGE_DECLARE(test_img_lvgl_logo_png);
241     lv_draw_image_dsc_t image_draw_dsc;
242     lv_draw_image_dsc_init(&image_draw_dsc);
243     image_draw_dsc.src = &test_img_lvgl_logo_png;
244 
245     lv_area_t coords = {10, 10, 10 + test_img_lvgl_logo_png.header.w - 1, 10 + test_img_lvgl_logo_png.header.h - 1};
246     lv_draw_image(&layer, &image_draw_dsc, &coords);
247 
248     /*Reuse the draw descriptor*/
249     lv_area_move(&coords, 40, 40);
250     image_draw_dsc.opa = LV_OPA_50;
251     lv_draw_image(&layer, &image_draw_dsc, &coords);
252 
253     lv_draw_line_dsc_t line_draw_dsc;
254     lv_draw_line_dsc_init(&line_draw_dsc);
255     line_draw_dsc.color = lv_color_hex3(0xCA8);
256     line_draw_dsc.width = 8;
257     line_draw_dsc.round_end = 1;
258     line_draw_dsc.round_start = 1;
259     lv_point_precise_set(&line_draw_dsc.p1, 150, 30);
260     lv_point_precise_set(&line_draw_dsc.p2, 350, 55);
261     lv_draw_line(&layer, &line_draw_dsc);
262 
263     lv_canvas_finish_layer(canvas, &layer);
264 
265     lv_color_t c = lv_color_hex(0xff0000);
266     uint32_t i;
267     for(i = 0; i < 50; i++) {
268         lv_canvas_set_px(canvas, 100 + i * 2, 10, c, LV_OPA_COVER);
269     }
270 }
271 
272 #endif
273