1 #if LV_BUILD_TEST
2 #include "../lvgl.h"
3
4 #include "unity/unity.h"
5
6 static const char color_cmd = LV_TXT_COLOR_CMD[0];
7
test_txt_should_identify_valid_start_of_command(void)8 void test_txt_should_identify_valid_start_of_command(void)
9 {
10 uint32_t character = color_cmd;
11 lv_text_cmd_state_t state = LV_TEXT_CMD_STATE_WAIT;
12
13 bool is_cmd = _lv_txt_is_cmd(&state, character);
14
15 TEST_ASSERT_TRUE(is_cmd);
16 TEST_ASSERT_EQUAL_UINT8(state, LV_TEXT_CMD_STATE_PAR);
17 }
18
test_txt_should_identify_invalid_start_of_command(void)19 void test_txt_should_identify_invalid_start_of_command(void)
20 {
21 uint32_t character = '$';
22 lv_text_cmd_state_t state = LV_TEXT_CMD_STATE_WAIT;
23
24 bool is_cmd = _lv_txt_is_cmd(&state, character);
25
26 TEST_ASSERT_FALSE(is_cmd);
27 TEST_ASSERT_EQUAL_UINT8(state, LV_TEXT_CMD_STATE_WAIT);
28 }
29
test_txt_should_identify_scaped_command_in_parameter(void)30 void test_txt_should_identify_scaped_command_in_parameter(void)
31 {
32 uint32_t character = color_cmd;
33 lv_text_cmd_state_t state = LV_TEXT_CMD_STATE_PAR;
34
35 bool is_cmd = _lv_txt_is_cmd(&state, character);
36
37 TEST_ASSERT_FALSE(is_cmd);
38 TEST_ASSERT_EQUAL_UINT8(state, LV_TEXT_CMD_STATE_WAIT);
39 }
40
test_txt_should_skip_color_parameter_in_parameter(void)41 void test_txt_should_skip_color_parameter_in_parameter(void)
42 {
43 uint32_t character = '$';
44 lv_text_cmd_state_t state = LV_TEXT_CMD_STATE_PAR;
45
46 bool is_cmd = _lv_txt_is_cmd(&state, character);
47
48 TEST_ASSERT_TRUE(is_cmd);
49 TEST_ASSERT_EQUAL_UINT8(state, LV_TEXT_CMD_STATE_PAR);
50 }
51
test_txt_should_reset_state_when_receiving_color_cmd_while_processing_commands(void)52 void test_txt_should_reset_state_when_receiving_color_cmd_while_processing_commands(void)
53 {
54 uint32_t character = color_cmd;
55 lv_text_cmd_state_t state = LV_TEXT_CMD_STATE_IN;
56
57 bool is_cmd = _lv_txt_is_cmd(&state, character);
58
59 TEST_ASSERT_TRUE(is_cmd);
60 TEST_ASSERT_EQUAL_UINT8(state, LV_TEXT_CMD_STATE_WAIT);
61 }
62
test_txt_should_identify_space_after_parameter(void)63 void test_txt_should_identify_space_after_parameter(void)
64 {
65 uint32_t character = ' ';
66 lv_text_cmd_state_t state = LV_TEXT_CMD_STATE_PAR;
67
68 bool is_cmd = _lv_txt_is_cmd(&state, character);
69
70 TEST_ASSERT_TRUE(is_cmd);
71 TEST_ASSERT_EQUAL_UINT8(state, LV_TEXT_CMD_STATE_IN);
72 }
73
test_txt_should_insert_string_into_another(void)74 void test_txt_should_insert_string_into_another(void)
75 {
76 const char * msg = "Hello ";
77 const char * suffix = "World";
78 char target[20] = {0};
79 size_t msg_len = strlen(msg);
80
81 strcpy(target, msg);
82
83 _lv_txt_ins(target, msg_len, suffix);
84
85 TEST_ASSERT_EQUAL_STRING("Hello World", target);
86 }
87
test_txt_should_handle_null_pointers_when_inserting(void)88 void test_txt_should_handle_null_pointers_when_inserting(void)
89 {
90 const char * msg = "Hello ";
91 char target[20] = {0};
92 size_t msg_len = strlen(msg);
93
94 strcpy(target, msg);
95
96 _lv_txt_ins(target, msg_len, NULL);
97
98 TEST_ASSERT_EQUAL_STRING("Hello ", target);
99 }
100
test_txt_cut_should_handle_null_pointer_to_txt(void)101 void test_txt_cut_should_handle_null_pointer_to_txt(void)
102 {
103 _lv_txt_cut(NULL, 0, 6);
104 }
105
test_txt_cut_happy_path(void)106 void test_txt_cut_happy_path(void)
107 {
108 char msg[] = "Hello World";
109
110 _lv_txt_cut(msg, 0, 6);
111
112 TEST_ASSERT_EQUAL_STRING("World", msg);
113 }
114
test_txt_cut_should_handle_len_longer_than_string_length(void)115 void test_txt_cut_should_handle_len_longer_than_string_length(void)
116 {
117 char msg[] = "Hello World";
118
119 _lv_txt_cut(msg, 0, 30);
120
121 TEST_ASSERT_EQUAL_UINT8(msg[0], 0x00);
122 }
123
test_txt_get_encoded_next_should_decode_valid_ascii(void)124 void test_txt_get_encoded_next_should_decode_valid_ascii(void)
125 {
126 char msg[] = "Hello World!";
127 uint32_t result = 0;
128
129 result = _lv_txt_encoded_next(msg, NULL);
130
131 TEST_ASSERT_EQUAL_UINT32((uint32_t) 'H', result);
132 }
133
test_txt_get_encoded_next_detect_valid_2_byte_input(void)134 void test_txt_get_encoded_next_detect_valid_2_byte_input(void)
135 {
136 char msg[] = "\xc3\xb1";
137 uint32_t result = 0;
138
139 result = _lv_txt_encoded_next(msg, NULL);
140
141 TEST_ASSERT_EQUAL_UINT32(241, result);
142 }
143
test_txt_get_encoded_next_detect_invalid_2_byte_input(void)144 void test_txt_get_encoded_next_detect_invalid_2_byte_input(void)
145 {
146 char msg[] = "\xc3\x28";
147 uint32_t result = 0;
148
149 result = _lv_txt_encoded_next(msg, NULL);
150
151 TEST_ASSERT_EQUAL_UINT32(0, result);
152 }
153
test_txt_get_encoded_next_detect_valid_3_byte_input(void)154 void test_txt_get_encoded_next_detect_valid_3_byte_input(void)
155 {
156 char msg[] = "\xe2\x82\xa1";
157 uint32_t result = 0;
158
159 result = _lv_txt_encoded_next(msg, NULL);
160
161 TEST_ASSERT_EQUAL_UINT32(8353, result);
162 }
163
test_txt_get_encoded_next_detect_invalid_3_byte_input(void)164 void test_txt_get_encoded_next_detect_invalid_3_byte_input(void)
165 {
166 char msg[] = "\xe2\x28\xa1";
167 uint32_t result = 0;
168
169 result = _lv_txt_encoded_next(msg, NULL);
170
171 TEST_ASSERT_EQUAL_UINT32(0, result);
172 }
173
test_txt_get_encoded_next_detect_valid_4_byte_input(void)174 void test_txt_get_encoded_next_detect_valid_4_byte_input(void)
175 {
176 char msg[] = "\xf0\x90\x8c\xbc";
177 uint32_t result = 0;
178
179 result = _lv_txt_encoded_next(msg, NULL);
180
181 TEST_ASSERT_EQUAL_UINT32(66364, result);
182 }
183
test_txt_get_encoded_next_detect_invalid_4_byte_input(void)184 void test_txt_get_encoded_next_detect_invalid_4_byte_input(void)
185 {
186 char msg[] = "\xf0\x28\x8c\x28";
187 uint32_t result = 0;
188
189 result = _lv_txt_encoded_next(msg, NULL);
190
191 TEST_ASSERT_EQUAL_UINT32(0, result);
192 }
193
194 /* See #2615 for more information */
test_txt_next_line_should_handle_empty_string(void)195 void test_txt_next_line_should_handle_empty_string(void)
196 {
197 const lv_font_t * font_ptr = NULL;
198 lv_coord_t letter_space = 0;
199 lv_coord_t max_width = 0;
200 lv_text_flag_t flag = LV_TEXT_FLAG_NONE;
201
202 uint32_t next_line = _lv_txt_get_next_line("", font_ptr, letter_space, max_width, NULL, flag);
203
204 TEST_ASSERT_EQUAL_UINT32(0, next_line);
205 }
206
207 #endif
208