aboutsummaryrefslogtreecommitdiff
path: root/tests/src/test_cases/widgets/test_lottie.c
blob: 72ce2e3129e954f8560a2e0bfc416d3682ae6765 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#if LV_BUILD_TEST
#include "../lvgl.h"

#include "unity/unity.h"
#include "lv_test_helpers.h"

static uint32_t buf[CANVAS_WIDTH_TO_STRIDE(100, 4) * 100 + LV_DRAW_BUF_ALIGN];
extern const uint8_t test_lottie_approve[];
extern const size_t test_lottie_approve_size;

/*Due to different floating point precision
 *the rendered images are slightly different on different architectures
 *So compare the screenshots only on AMD64*/
#ifdef NON_AMD64_BUILD
    #undef TEST_ASSERT_EQUAL_SCREENSHOT
    #define TEST_ASSERT_EQUAL_SCREENSHOT(path) (void) path
#endif

void setUp(void)
{

}

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

void test_lottie_simple(void)
{
    lv_obj_t * lottie = lv_lottie_create(lv_screen_active());
    lv_lottie_set_buffer(lottie, 100, 100, lv_draw_buf_align(buf, LV_COLOR_FORMAT_ARGB8888));
    lv_lottie_set_src_data(lottie, test_lottie_approve, test_lottie_approve_size);
    lv_obj_center(lottie);

    TEST_ASSERT_EQUAL_SCREENSHOT("widgets/lottie_1.png");

    /*Wait a little*/
    lv_test_wait(200);
    TEST_ASSERT_EQUAL_SCREENSHOT("widgets/lottie_2.png");

    /*Should be the last frame*/
    lv_test_wait(750);
    TEST_ASSERT_EQUAL_SCREENSHOT("widgets/lottie_3.png");

    /*Setting a source should reset the animation*/
    lv_lottie_set_src_data(lottie, test_lottie_approve, test_lottie_approve_size);

    /*Should reset automatically*/
    lv_test_wait(200);
    TEST_ASSERT_EQUAL_SCREENSHOT("widgets/lottie_2.png");
}

void test_lottie_load_from_file(void)
{
    lv_obj_t * lottie = lv_lottie_create(lv_screen_active());
    lv_lottie_set_buffer(lottie, 100, 100, lv_draw_buf_align(buf, LV_COLOR_FORMAT_ARGB8888));
    lv_lottie_set_src_file(lottie, "src/test_assets/test_lottie_approve.json");
    lv_obj_center(lottie);
    TEST_ASSERT_EQUAL_SCREENSHOT("widgets/lottie_1.png");

    /*Wait a little*/
    lv_test_wait(200);
    TEST_ASSERT_EQUAL_SCREENSHOT("widgets/lottie_2.png");

    /*Should be the last frame*/
    lv_test_wait(750);
    TEST_ASSERT_EQUAL_SCREENSHOT("widgets/lottie_3.png");

    /*Setting a source should reset the animation*/
    lv_lottie_set_src_data(lottie, test_lottie_approve, test_lottie_approve_size);

    /*Should reset automatically*/
    lv_test_wait(200);
    TEST_ASSERT_EQUAL_SCREENSHOT("widgets/lottie_2.png");
}

void test_lottie_missing_settings(void)
{
    uint32_t tmp_buf[CANVAS_WIDTH_TO_STRIDE(100, 4) * 100 + LV_DRAW_BUF_ALIGN];

    lv_obj_t * lottie1 = lv_lottie_create(lv_screen_active());
    lv_lottie_set_buffer(lottie1, 100, 100, lv_draw_buf_align(tmp_buf, LV_COLOR_FORMAT_ARGB8888));

    /*Shouldn't crash without source*/
    lv_timer_handler();

    lv_obj_t * lottie2 = lv_lottie_create(lv_screen_active());
    /*Set the source first*/
    lv_lottie_set_src_data(lottie2, test_lottie_approve, test_lottie_approve_size);

    /*Shouldn't crash without buffer*/
    lv_timer_handler();

    lv_lottie_set_buffer(lottie2, 100, 100, lv_draw_buf_align(buf, LV_COLOR_FORMAT_ARGB8888));

    lv_obj_center(lottie2);
    lv_test_wait(950);
    TEST_ASSERT_EQUAL_SCREENSHOT("widgets/lottie_3.png");
}

void test_lottie_rescale(void)
{
    lv_obj_t * lottie = lv_lottie_create(lv_screen_active());
    lv_lottie_set_buffer(lottie, 100, 100, lv_draw_buf_align(buf, LV_COLOR_FORMAT_ARGB8888));
    lv_lottie_set_src_data(lottie, test_lottie_approve, test_lottie_approve_size);
    lv_obj_center(lottie);

    /*Wait a little*/
    lv_test_wait(200);
    TEST_ASSERT_EQUAL_SCREENSHOT("widgets/lottie_2.png");

    lv_lottie_set_buffer(lottie, 50, 50, lv_draw_buf_align(buf, LV_COLOR_FORMAT_ARGB8888));
    TEST_ASSERT_EQUAL_SCREENSHOT("widgets/lottie_2_small.png");

    /*Should be the last frame*/
    lv_test_wait(750);
    TEST_ASSERT_EQUAL_SCREENSHOT("widgets/lottie_3_small.png");
}


void test_lottie_non_uniform_shape(void)
{
    lv_obj_t * lottie = lv_lottie_create(lv_screen_active());
    lv_lottie_set_buffer(lottie, 50, 200, lv_draw_buf_align(buf, LV_COLOR_FORMAT_ARGB8888));
    lv_lottie_set_src_data(lottie, test_lottie_approve, test_lottie_approve_size);
    lv_obj_center(lottie);

    lv_test_wait(950);
    TEST_ASSERT_EQUAL_SCREENSHOT("widgets/lottie_4.png");
}

void test_lottie_memory_leak(void)
{
    size_t mem_before = lv_test_get_free_mem();

    uint32_t i;
    for(i = 0; i < 32; i++) {
        lv_obj_t * lottie = lv_lottie_create(lv_screen_active());
        lv_lottie_set_buffer(lottie, 100, 100, lv_draw_buf_align(buf, LV_COLOR_FORMAT_ARGB8888));
        lv_lottie_set_src_data(lottie, test_lottie_approve, test_lottie_approve_size);
        lv_obj_center(lottie);
        lv_test_wait(753 * i); /*Render a random frame*/
        lv_timer_handler();
        lv_obj_delete(lottie);
    }
    TEST_ASSERT_MEM_LEAK_LESS_THAN(mem_before, 16);
}

void test_lottie_no_jump_when_visible_again(void)
{
    lv_obj_t * lottie = lv_lottie_create(lv_screen_active());
    lv_lottie_set_buffer(lottie, 100, 100, lv_draw_buf_align(buf, LV_COLOR_FORMAT_ARGB8888));
    lv_lottie_set_src_data(lottie, test_lottie_approve, test_lottie_approve_size);
    lv_obj_center(lottie);

    /*Wait a little*/
    lv_test_wait(200);
    TEST_ASSERT_EQUAL_SCREENSHOT("widgets/lottie_2.png");

    lv_obj_add_flag(lottie, LV_OBJ_FLAG_HIDDEN);
    lv_test_wait(300);
    TEST_ASSERT_EQUAL_SCREENSHOT("widgets/lottie_1.png"); /*Empty screen*/

    /*Should be on the same frame*/
    lv_obj_clear_flag(lottie, LV_OBJ_FLAG_HIDDEN);
    TEST_ASSERT_EQUAL_SCREENSHOT("widgets/lottie_2.png");

    lv_test_wait(750);
    TEST_ASSERT_EQUAL_SCREENSHOT("widgets/lottie_3.png");

}

#endif