1 #if LV_BUILD_TEST
2 #include "../lvgl.h"
3 #include "../../lvgl_private.h"
4 
5 #include "unity/unity.h"
6 
7 /* This function runs before each test */
8 void setUp(void);
9 /* This function runs after every test */
10 void tearDown(void);
11 
12 void test_calendar_creation_successful(void);
13 void test_calendar_set_today_date(void);
14 void test_calendar_set_today_date_gui(void);
15 void test_calendar_set_showed_date_gui(void);
16 void test_calendar_set_highlighted_dates(void);
17 void test_calendar_set_highlighted_dates_gui(void);
18 void test_calendar_set_day_names_gui(void);
19 void test_calendar_get_highlighted_dates_num(void);
20 void test_calendar_header_dropdown_create_gui(void);
21 void test_calendar_header_arrow_create_gui(void);
22 void test_calendar_event_key_down_gui(void);
23 void test_calendar_get_pressed_date_null(void);
24 void test_calendar_get_btnmatrix(void);
25 
26 static lv_obj_t * g_active_screen = NULL;
27 static lv_obj_t * g_calendar = NULL;
28 
setUp(void)29 void setUp(void)
30 {
31     g_active_screen = lv_screen_active();
32     g_calendar = lv_calendar_create(g_active_screen);
33 }
34 
tearDown(void)35 void tearDown(void)
36 {
37     lv_obj_clean(g_active_screen);
38 }
39 
test_calendar_creation_successful(void)40 void test_calendar_creation_successful(void)
41 {
42     TEST_ASSERT_NOT_NULL(g_calendar);
43 }
44 
test_calendar_set_today_date(void)45 void test_calendar_set_today_date(void)
46 {
47     /* Work with 2022-09-21 as today (start of spring in Southern hemisphere) */
48     lv_calendar_date_t today;
49     today.year = 2022;
50     today.month = 9;
51     today.day = 21;
52 
53     lv_calendar_set_today_date(g_calendar, today.year, today.month, today.day);
54 
55     const lv_calendar_date_t * date_after_test = lv_calendar_get_today_date(g_calendar);
56 
57     TEST_ASSERT_EQUAL_INT16(today.year, date_after_test->year);
58     TEST_ASSERT_EQUAL_INT16(today.month, date_after_test->month);
59     TEST_ASSERT_EQUAL_INT16(today.day, date_after_test->day);
60 }
61 
test_calendar_set_today_date_gui(void)62 void test_calendar_set_today_date_gui(void)
63 {
64     /* Work with 2022-09-21 as today (start of spring in Southern hemisphere) */
65     lv_calendar_date_t today;
66     today.year = 2022;
67     today.month = 9;
68     today.day = 21;
69 
70     lv_calendar_set_today_date(g_calendar, today.year, today.month, today.day);
71     lv_calendar_set_month_shown(g_calendar, 2022, 9);
72 
73     TEST_ASSERT_EQUAL_SCREENSHOT("widgets/calendar_01.png");
74 }
75 
test_calendar_set_showed_date_gui(void)76 void test_calendar_set_showed_date_gui(void)
77 {
78     lv_calendar_set_month_shown(g_calendar, 2022, 9);
79 
80     TEST_ASSERT_EQUAL_SCREENSHOT("widgets/calendar_02.png");
81 }
82 
test_calendar_set_highlighted_dates(void)83 void test_calendar_set_highlighted_dates(void)
84 {
85     /*Highlight a few days*/
86     static lv_calendar_date_t highlighted_days[3];       /*Only its pointer will be saved so should be static*/
87     highlighted_days[0].year = 2022;
88     highlighted_days[0].month = 2;
89     highlighted_days[0].day = 6;
90 
91     highlighted_days[1].year = 2022;
92     highlighted_days[1].month = 2;
93     highlighted_days[1].day = 11;
94 
95     highlighted_days[2].year = 2022;
96     highlighted_days[2].month = 2;
97     highlighted_days[2].day = 22;
98 
99     lv_calendar_set_highlighted_dates(g_calendar, highlighted_days, 3);
100 
101     const lv_calendar_date_t * highlighted_days_after_test = lv_calendar_get_highlighted_dates(g_calendar);
102 
103     for(int i = 0; i < 3; i++) {
104         TEST_ASSERT_EQUAL_INT16(highlighted_days[i].year, highlighted_days_after_test[i].year);
105         TEST_ASSERT_EQUAL_INT16(highlighted_days[i].month, highlighted_days_after_test[i].month);
106         TEST_ASSERT_EQUAL_INT16(highlighted_days[i].day, highlighted_days_after_test[i].day);
107     }
108 }
109 
test_calendar_set_highlighted_dates_gui(void)110 void test_calendar_set_highlighted_dates_gui(void)
111 {
112     /*Highlight a few days*/
113     static lv_calendar_date_t highlighted_days[3];       /*Only its pointer will be saved so should be static*/
114     highlighted_days[0].year = 2022;
115     highlighted_days[0].month = 2;
116     highlighted_days[0].day = 6;
117 
118     highlighted_days[1].year = 2022;
119     highlighted_days[1].month = 2;
120     highlighted_days[1].day = 11;
121 
122     highlighted_days[2].year = 2022;
123     highlighted_days[2].month = 2;
124     highlighted_days[2].day = 22;
125 
126     lv_calendar_set_highlighted_dates(g_calendar, highlighted_days, 3);
127 
128     lv_calendar_set_month_shown(g_calendar, 2022, 2);
129 
130     TEST_ASSERT_EQUAL_SCREENSHOT("widgets/calendar_03.png");
131 }
132 
test_calendar_set_day_names_gui(void)133 void test_calendar_set_day_names_gui(void)
134 {
135     static const char * day_names[7] = {"Do", "Lu", "Ma", "Mi", "Ju", "Vi", "Sa"};
136 
137     lv_calendar_set_day_names(g_calendar, day_names);
138 
139     lv_calendar_set_month_shown(g_calendar, 2022, 9);
140 
141     TEST_ASSERT_EQUAL_SCREENSHOT("widgets/calendar_04.png");
142 }
143 
test_calendar_get_highlighted_dates_num(void)144 void test_calendar_get_highlighted_dates_num(void)
145 {
146     /*Highlight a few days*/
147     static lv_calendar_date_t highlighted_days[3];       /*Only its pointer will be saved so should be static*/
148     highlighted_days[0].year = 2022;
149     highlighted_days[0].month = 2;
150     highlighted_days[0].day = 6;
151 
152     highlighted_days[1].year = 2022;
153     highlighted_days[1].month = 2;
154     highlighted_days[1].day = 11;
155 
156     highlighted_days[2].year = 2022;
157     highlighted_days[2].month = 2;
158     highlighted_days[2].day = 22;
159 
160     lv_calendar_set_highlighted_dates(g_calendar, highlighted_days, 3);
161 
162     TEST_ASSERT_EQUAL_INT16(3, lv_calendar_get_highlighted_dates_num(g_calendar));
163 }
164 
test_calendar_header_dropdown_create_gui(void)165 void test_calendar_header_dropdown_create_gui(void)
166 {
167     lv_calendar_header_dropdown_create(g_calendar);
168 
169     lv_calendar_set_month_shown(g_calendar, 2022, 9);
170 
171     TEST_ASSERT_EQUAL_SCREENSHOT("widgets/calendar_05.png");
172 }
173 
test_calendar_header_arrow_create_gui(void)174 void test_calendar_header_arrow_create_gui(void)
175 {
176     lv_calendar_header_arrow_create(g_calendar);
177 
178     lv_calendar_set_month_shown(g_calendar, 2022, 10);    // Use October to avoid month name sliding
179 
180     TEST_ASSERT_EQUAL_SCREENSHOT("widgets/calendar_06.png");
181 }
182 
test_calendar_event_key_down_gui(void)183 void test_calendar_event_key_down_gui(void)
184 {
185     uint32_t key = LV_KEY_DOWN;
186 
187     lv_calendar_set_month_shown(g_calendar, 2022, 9);
188 
189     lv_obj_send_event(g_calendar, LV_EVENT_KEY, (void *) &key);
190 
191     TEST_ASSERT_EQUAL_SCREENSHOT("widgets/calendar_07.png");
192 }
193 
test_calendar_get_pressed_date_null(void)194 void test_calendar_get_pressed_date_null(void)
195 {
196     lv_calendar_set_month_shown(g_calendar, 2022, 9);
197 
198     lv_calendar_date_t pressed_date;
199 
200     lv_result_t result = lv_calendar_get_pressed_date(g_calendar, &pressed_date);
201 
202     TEST_ASSERT_EQUAL(result, LV_RESULT_INVALID);
203 }
204 
test_calendar_get_btnmatrix(void)205 void test_calendar_get_btnmatrix(void)
206 {
207     lv_obj_t * btnm = lv_calendar_get_btnmatrix(g_calendar);
208 
209     TEST_ASSERT_NOT_NULL(btnm);
210 }
211 
test_calendar_custom_year_list(void)212 void test_calendar_custom_year_list(void)
213 {
214     lv_obj_t  * calendar = lv_calendar_create(lv_screen_active());
215 
216     lv_calendar_header_dropdown_create(calendar);
217 
218     const char * years = "2024\n2023\n2022\n2021\n2020\n2019";
219     lv_calendar_header_dropdown_set_year_list(calendar, years);
220 
221     TEST_ASSERT_EQUAL_SCREENSHOT("widgets/calendar_08.png");
222 }
223 
test_calendar_chinese_calendar(void)224 void test_calendar_chinese_calendar(void)
225 {
226     lv_obj_set_size(g_calendar, 400, 350);
227     lv_obj_center(g_calendar);
228     lv_calendar_set_today_date(g_calendar, 2024, 03, 22);
229     lv_calendar_set_month_shown(g_calendar, 2024, 03);
230 
231     lv_obj_set_style_text_font(g_calendar, &lv_font_simsun_14_cjk, LV_PART_MAIN);
232     lv_calendar_set_chinese_mode(g_calendar, true);
233 
234     TEST_ASSERT_EQUAL_SCREENSHOT("widgets/calendar_09.png");
235 }
236 
237 #endif  /* LV_BUILD_TEST */
238