aboutsummaryrefslogtreecommitdiff
path: root/tests/src/test_cases/test_margin_grid.c
blob: 57e8392ca09fbf427ae7c733b7a878e03a0592e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#if LV_BUILD_TEST
#include "../lvgl.h"
#include "../../lvgl_private.h"

#include "unity/unity.h"

static lv_obj_t * active_screen = NULL;

void setUp(void)
{
    active_screen = lv_screen_active();
}

void tearDown(void)
{
    lv_obj_clean(active_screen);
}

lv_obj_t * obj_create(lv_obj_t * parent, lv_color_t color)
{
    lv_obj_t * obj = lv_obj_create(parent);
    lv_obj_set_style_bg_color(obj, color, LV_PART_MAIN);
    lv_obj_set_width(obj, 150);
    lv_obj_set_height(obj, 150);

    return obj;
}

void obj_set_margin(lv_obj_t * obj, int32_t left, int32_t top, int32_t right, int32_t bottom)
{
    lv_obj_set_style_margin_left(obj, left, LV_PART_MAIN);
    lv_obj_set_style_margin_top(obj, top, LV_PART_MAIN);
    lv_obj_set_style_margin_right(obj, right, LV_PART_MAIN);
    lv_obj_set_style_margin_bottom(obj, bottom, LV_PART_MAIN);
}

void test_grid(void)
{
#define M 5
#define N 8

    lv_obj_t * obj0 = active_screen;

    static int32_t row_dsc[M + 1];
    static int32_t col_dsc[N + 1];

    row_dsc[M] = LV_GRID_TEMPLATE_LAST;
    col_dsc[N] = LV_GRID_TEMPLATE_LAST;
    for(int i = 0; i < M; ++i) {
        row_dsc[i] = 80;
    }
    for(int i = 0; i < N; ++i) {
        col_dsc[i] = 80;
    }

    static lv_palette_t colors[] = {
        LV_PALETTE_BLUE,
        LV_PALETTE_PURPLE,
        LV_PALETTE_GREEN,
        LV_PALETTE_PINK,
        LV_PALETTE_AMBER,
        LV_PALETTE_BLUE_GREY,
        LV_PALETTE_INDIGO
    };

    lv_obj_set_layout(obj0, LV_LAYOUT_GRID);
    lv_obj_set_grid_align(obj0, LV_GRID_ALIGN_CENTER, LV_GRID_ALIGN_CENTER);
    lv_obj_set_grid_dsc_array(obj0, col_dsc, row_dsc);

    lv_obj_set_style_pad_gap(obj0, 0, LV_PART_MAIN);
    lv_obj_set_style_pad_row(obj0, 0, LV_PART_MAIN);
    lv_obj_set_style_pad_column(obj0, 0, LV_PART_MAIN);

    lv_obj_t * obj0s[M][N];

    for(int i = 0; i < M; ++i) {
        for(int j = 0; j < N; ++j) {
            obj0s[i][j] = obj_create(obj0,
                                     lv_palette_main(colors[(i * M + j) % ((sizeof colors) / (sizeof(lv_palette_t)))]));
        }
    }

    lv_obj_t * o;
    for(int i = 0; i < M; i++) {
        for(int j = 0; j < N; ++j) {
            o = obj0s[i][j];
            lv_obj_set_style_radius(o, 0, LV_PART_MAIN);
            lv_obj_set_style_border_width(o, 0, LV_PART_MAIN);
            lv_obj_set_scrollbar_mode(o, LV_SCROLLBAR_MODE_OFF);
            lv_obj_set_width(o, 30);

            lv_obj_set_grid_cell(o, LV_GRID_ALIGN_CENTER, j, 1, LV_GRID_ALIGN_STRETCH, i, 1);

            lv_obj_set_style_margin_top(o, i * 5, LV_PART_MAIN);
            lv_obj_set_style_margin_left(o, i * 5 + j * 5, LV_PART_MAIN);
            lv_obj_set_style_margin_right(o, i * 5, LV_PART_MAIN);
            lv_obj_set_style_margin_bottom(o, i * 5 + j * 5, LV_PART_MAIN);
        }
    }

    lv_obj_update_layout(obj0);
    TEST_ASSERT_EQUAL_SCREENSHOT("margin_grid_0.png");

#undef M
#undef N
}
#endif