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 * active_screen = NULL;
8 
setUp(void)9 void setUp(void)
10 {
11     active_screen = lv_screen_active();
12 }
13 
tearDown(void)14 void tearDown(void)
15 {
16     lv_obj_clean(active_screen);
17 }
18 
obj_create(lv_obj_t * parent,lv_color_t color)19 lv_obj_t * obj_create(lv_obj_t * parent, lv_color_t color)
20 {
21     lv_obj_t * obj = lv_obj_create(parent);
22     lv_obj_set_style_bg_color(obj, color, LV_PART_MAIN);
23     lv_obj_set_width(obj, 150);
24     lv_obj_set_height(obj, 150);
25 
26     return obj;
27 }
28 
obj_set_margin(lv_obj_t * obj,int32_t left,int32_t top,int32_t right,int32_t bottom)29 void obj_set_margin(lv_obj_t * obj, int32_t left, int32_t top, int32_t right, int32_t bottom)
30 {
31     lv_obj_set_style_margin_left(obj, left, LV_PART_MAIN);
32     lv_obj_set_style_margin_top(obj, top, LV_PART_MAIN);
33     lv_obj_set_style_margin_right(obj, right, LV_PART_MAIN);
34     lv_obj_set_style_margin_bottom(obj, bottom, LV_PART_MAIN);
35 }
36 
test_grid(void)37 void test_grid(void)
38 {
39 #define M 5
40 #define N 8
41 
42     lv_obj_t * obj0 = active_screen;
43 
44     static int32_t row_dsc[M + 1];
45     static int32_t col_dsc[N + 1];
46 
47     row_dsc[M] = LV_GRID_TEMPLATE_LAST;
48     col_dsc[N] = LV_GRID_TEMPLATE_LAST;
49     for(int i = 0; i < M; ++i) {
50         row_dsc[i] = 80;
51     }
52     for(int i = 0; i < N; ++i) {
53         col_dsc[i] = 80;
54     }
55 
56     static lv_palette_t colors[] = {
57         LV_PALETTE_BLUE,
58         LV_PALETTE_PURPLE,
59         LV_PALETTE_GREEN,
60         LV_PALETTE_PINK,
61         LV_PALETTE_AMBER,
62         LV_PALETTE_BLUE_GREY,
63         LV_PALETTE_INDIGO
64     };
65 
66     lv_obj_set_layout(obj0, LV_LAYOUT_GRID);
67     lv_obj_set_grid_align(obj0, LV_GRID_ALIGN_CENTER, LV_GRID_ALIGN_CENTER);
68     lv_obj_set_grid_dsc_array(obj0, col_dsc, row_dsc);
69 
70     lv_obj_set_style_pad_gap(obj0, 0, LV_PART_MAIN);
71     lv_obj_set_style_pad_row(obj0, 0, LV_PART_MAIN);
72     lv_obj_set_style_pad_column(obj0, 0, LV_PART_MAIN);
73 
74     lv_obj_t * obj0s[M][N];
75 
76     for(int i = 0; i < M; ++i) {
77         for(int j = 0; j < N; ++j) {
78             obj0s[i][j] = obj_create(obj0,
79                                      lv_palette_main(colors[(i * M + j) % ((sizeof colors) / (sizeof(lv_palette_t)))]));
80         }
81     }
82 
83     lv_obj_t * o;
84     for(int i = 0; i < M; i++) {
85         for(int j = 0; j < N; ++j) {
86             o = obj0s[i][j];
87             lv_obj_set_style_radius(o, 0, LV_PART_MAIN);
88             lv_obj_set_style_border_width(o, 0, LV_PART_MAIN);
89             lv_obj_set_scrollbar_mode(o, LV_SCROLLBAR_MODE_OFF);
90             lv_obj_set_width(o, 30);
91 
92             lv_obj_set_grid_cell(o, LV_GRID_ALIGN_CENTER, j, 1, LV_GRID_ALIGN_STRETCH, i, 1);
93 
94             lv_obj_set_style_margin_top(o, i * 5, LV_PART_MAIN);
95             lv_obj_set_style_margin_left(o, i * 5 + j * 5, LV_PART_MAIN);
96             lv_obj_set_style_margin_right(o, i * 5, LV_PART_MAIN);
97             lv_obj_set_style_margin_bottom(o, i * 5 + j * 5, LV_PART_MAIN);
98         }
99     }
100 
101     lv_obj_update_layout(obj0);
102     TEST_ASSERT_EQUAL_SCREENSHOT("margin_grid_0.png");
103 
104 #undef M
105 #undef N
106 }
107 #endif
108