• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2013 Red Hat, Inc.
4  */
5 
6 #include "config.h"
7 #include "test-common.h"
8 
START_TEST(test_limits)9 START_TEST(test_limits)
10 {
11 	ck_assert(libevdev_event_type_get_name(EV_MAX + 1) == NULL);
12 	ck_assert(libevdev_event_code_get_name(EV_ABS, ABS_MAX + 1) == NULL);
13 	ck_assert(libevdev_event_code_get_name(EV_REL, REL_MAX + 1) == NULL);
14 	ck_assert(libevdev_event_code_get_name(EV_KEY, KEY_MAX + 1) == NULL);
15 	ck_assert(libevdev_event_code_get_name(EV_LED, LED_MAX + 1) == NULL);
16 	ck_assert(libevdev_event_code_get_name(EV_SW, SW_MAX + 1) == NULL);
17 	ck_assert(libevdev_event_code_get_name(EV_MSC, MSC_MAX + 1) == NULL);
18 	ck_assert(libevdev_event_code_get_name(EV_SND, SND_MAX + 1) == NULL);
19 	ck_assert(libevdev_event_code_get_name(EV_REP, REP_MAX + 1) == NULL);
20 	ck_assert(libevdev_event_code_get_name(EV_FF, FF_MAX + 1) == NULL);
21 	ck_assert(libevdev_event_code_get_name(EV_MAX + 1, 0) == NULL);
22 	ck_assert(libevdev_event_value_get_name(EV_ABS, ABS_MT_TOOL_TYPE, MT_TOOL_MAX + 1) == NULL);
23 }
24 END_TEST
25 
START_TEST(test_type_name)26 START_TEST(test_type_name)
27 {
28 	ck_assert_str_eq(libevdev_event_type_get_name(EV_SYN), "EV_SYN");
29 	ck_assert_str_eq(libevdev_event_type_get_name(EV_REL), "EV_REL");
30 	ck_assert_str_eq(libevdev_event_type_get_name(EV_ABS), "EV_ABS");
31 	ck_assert_str_eq(libevdev_event_type_get_name(EV_MSC), "EV_MSC");
32 	ck_assert_str_eq(libevdev_event_type_get_name(EV_SW),  "EV_SW");
33 	ck_assert_str_eq(libevdev_event_type_get_name(EV_LED), "EV_LED");
34 	ck_assert_str_eq(libevdev_event_type_get_name(EV_SND), "EV_SND");
35 	ck_assert_str_eq(libevdev_event_type_get_name(EV_REP), "EV_REP");
36 	ck_assert_str_eq(libevdev_event_type_get_name(EV_FF),  "EV_FF");
37 	ck_assert_str_eq(libevdev_event_type_get_name(EV_PWR), "EV_PWR");
38 	ck_assert_str_eq(libevdev_event_type_get_name(EV_FF_STATUS), "EV_FF_STATUS");
39 	ck_assert_str_eq(libevdev_event_type_get_name(EV_MAX), "EV_MAX");
40 }
41 END_TEST
42 
START_TEST(test_code_abs_name)43 START_TEST(test_code_abs_name)
44 {
45 	/* pick out a few only */
46 	ck_assert_str_eq(libevdev_event_code_get_name(EV_ABS, ABS_X), "ABS_X");
47 	ck_assert_str_eq(libevdev_event_code_get_name(EV_ABS, ABS_Y), "ABS_Y");
48 
49 	ck_assert_str_eq(libevdev_event_code_get_name(EV_ABS, ABS_MT_SLOT), "ABS_MT_SLOT");
50 	ck_assert_str_eq(libevdev_event_code_get_name(EV_ABS, ABS_MISC), "ABS_MISC");
51 	ck_assert_str_eq(libevdev_event_code_get_name(EV_ABS, ABS_MAX), "ABS_MAX");
52 
53 	ck_assert(libevdev_event_code_get_name(EV_ABS, ABS_MAX - 1) == NULL);
54 
55 }
56 END_TEST
57 
START_TEST(test_code_rel_name)58 START_TEST(test_code_rel_name)
59 {
60 	/* pick out a few only */
61 	ck_assert_str_eq(libevdev_event_code_get_name(EV_REL, REL_X), "REL_X");
62 	ck_assert_str_eq(libevdev_event_code_get_name(EV_REL, REL_Y), "REL_Y");
63 	ck_assert_str_eq(libevdev_event_code_get_name(EV_REL, REL_MISC), "REL_MISC");
64 	ck_assert_str_eq(libevdev_event_code_get_name(EV_REL, REL_MAX), "REL_MAX");
65 
66 	ck_assert(libevdev_event_code_get_name(EV_REL, REL_MAX - 1) == NULL);
67 
68 }
69 END_TEST
70 
START_TEST(test_code_key_name)71 START_TEST(test_code_key_name)
72 {
73 	/* pick out a few only */
74 	ck_assert_str_eq(libevdev_event_code_get_name(EV_KEY, KEY_RESERVED), "KEY_RESERVED");
75 	ck_assert_str_eq(libevdev_event_code_get_name(EV_KEY, KEY_ESC), "KEY_ESC");
76 	ck_assert_str_eq(libevdev_event_code_get_name(EV_KEY, KEY_1), "KEY_1");
77 	ck_assert_str_eq(libevdev_event_code_get_name(EV_KEY, KEY_2), "KEY_2");
78 	ck_assert_str_eq(libevdev_event_code_get_name(EV_KEY, KEY_UNKNOWN), "KEY_UNKNOWN");
79 
80 	ck_assert_str_eq(libevdev_event_code_get_name(EV_KEY, BTN_0), "BTN_0");
81 	ck_assert_str_eq(libevdev_event_code_get_name(EV_KEY, BTN_LEFT), "BTN_LEFT");
82 	ck_assert_str_eq(libevdev_event_code_get_name(EV_KEY, BTN_TRIGGER), "BTN_TRIGGER");
83 	ck_assert_str_eq(libevdev_event_code_get_name(EV_KEY, BTN_A), "BTN_SOUTH");
84 	ck_assert_str_eq(libevdev_event_code_get_name(EV_KEY, BTN_TOOL_PEN), "BTN_TOOL_PEN");
85 
86 	ck_assert_str_eq(libevdev_event_code_get_name(EV_KEY, KEY_TOUCHPAD_TOGGLE), "KEY_TOUCHPAD_TOGGLE");
87 	ck_assert_str_eq(libevdev_event_code_get_name(EV_KEY, BTN_TRIGGER_HAPPY), "BTN_TRIGGER_HAPPY1");
88 
89 	ck_assert_str_eq(libevdev_event_code_get_name(EV_KEY, KEY_MAX), "KEY_MAX");
90 	ck_assert(libevdev_event_code_get_name(EV_KEY, KEY_MAX - 1) == NULL);
91 
92 	/* special cases that resolve to something else */
93 	ck_assert_str_eq(libevdev_event_code_get_name(EV_KEY, KEY_HANGUEL), "KEY_HANGEUL");
94 	ck_assert_str_eq(libevdev_event_code_get_name(EV_KEY, KEY_SCREENLOCK), "KEY_COFFEE");
95 	ck_assert_str_eq(libevdev_event_code_get_name(EV_KEY, BTN_MISC), "BTN_0");
96 	ck_assert_str_eq(libevdev_event_code_get_name(EV_KEY, BTN_MOUSE), "BTN_LEFT");
97 	ck_assert_str_eq(libevdev_event_code_get_name(EV_KEY, BTN_JOYSTICK), "BTN_TRIGGER");
98 	ck_assert_str_eq(libevdev_event_code_get_name(EV_KEY, BTN_GAMEPAD), "BTN_SOUTH");
99 	ck_assert_str_eq(libevdev_event_code_get_name(EV_KEY, BTN_DIGI), "BTN_TOOL_PEN");
100 	ck_assert_str_eq(libevdev_event_code_get_name(EV_KEY, BTN_WHEEL), "BTN_GEAR_DOWN");
101 	ck_assert_str_eq(libevdev_event_code_get_name(EV_KEY, BTN_TRIGGER_HAPPY), "BTN_TRIGGER_HAPPY1");
102 
103 }
104 END_TEST
105 
START_TEST(test_code_led_name)106 START_TEST(test_code_led_name)
107 {
108 	/* pick out a few only */
109 	ck_assert_str_eq(libevdev_event_code_get_name(EV_LED, LED_NUML), "LED_NUML");
110 	ck_assert_str_eq(libevdev_event_code_get_name(EV_LED, LED_KANA), "LED_KANA");
111 	ck_assert_str_eq(libevdev_event_code_get_name(EV_LED, LED_MAX), "LED_MAX");
112 
113 	ck_assert(libevdev_event_code_get_name(EV_LED, LED_MAX - 1) == NULL);
114 
115 }
116 END_TEST
117 
START_TEST(test_code_snd_name)118 START_TEST(test_code_snd_name)
119 {
120 	/* pick out a few only */
121 	ck_assert_str_eq(libevdev_event_code_get_name(EV_SND, SND_CLICK), "SND_CLICK");
122 	ck_assert_str_eq(libevdev_event_code_get_name(EV_SND, SND_TONE), "SND_TONE");
123 	ck_assert_str_eq(libevdev_event_code_get_name(EV_SND, SND_MAX), "SND_MAX");
124 
125 	ck_assert(libevdev_event_code_get_name(EV_SND, SND_MAX - 1) == NULL);
126 
127 }
128 END_TEST
129 
START_TEST(test_code_rep_name)130 START_TEST(test_code_rep_name)
131 {
132 	ck_assert_str_eq(libevdev_event_code_get_name(EV_REP, REP_DELAY), "REP_DELAY");
133 	ck_assert_str_eq(libevdev_event_code_get_name(EV_REP, REP_PERIOD), "REP_PERIOD");
134 	ck_assert_str_eq(libevdev_event_code_get_name(EV_REP, REP_MAX), "REP_PERIOD");
135 
136 }
137 END_TEST
138 
START_TEST(test_code_msc_name)139 START_TEST(test_code_msc_name)
140 {
141 	/* pick out a few only */
142 	ck_assert_str_eq(libevdev_event_code_get_name(EV_MSC, MSC_SERIAL), "MSC_SERIAL");
143 	ck_assert_str_eq(libevdev_event_code_get_name(EV_MSC, MSC_RAW), "MSC_RAW");
144 #ifdef MSC_TIMESTAMP
145 	ck_assert_str_eq(libevdev_event_code_get_name(EV_MSC, MSC_TIMESTAMP), "MSC_TIMESTAMP");
146 #endif
147 	ck_assert_str_eq(libevdev_event_code_get_name(EV_MSC, MSC_MAX), "MSC_MAX");
148 
149 	ck_assert(libevdev_event_code_get_name(EV_MSC, MSC_MAX - 1) == NULL);
150 
151 }
152 END_TEST
153 
START_TEST(test_code_sw_name)154 START_TEST(test_code_sw_name)
155 {
156 	/* pick out a few only */
157 	ck_assert_str_eq(libevdev_event_code_get_name(EV_SW, SW_LID), "SW_LID");
158 	ck_assert_str_eq(libevdev_event_code_get_name(EV_SW, SW_RFKILL_ALL), "SW_RFKILL_ALL");
159 	ck_assert_str_eq(libevdev_event_code_get_name(EV_SW, SW_LINEIN_INSERT), "SW_LINEIN_INSERT");
160 	ck_assert_str_eq(libevdev_event_code_get_name(EV_SW, SW_PEN_INSERTED), "SW_PEN_INSERTED");
161 	ck_assert_str_eq(libevdev_event_code_get_name(EV_SW, SW_MAX), "SW_MACHINE_COVER");
162 }
163 END_TEST
164 
START_TEST(test_code_ff_name)165 START_TEST(test_code_ff_name)
166 {
167 	/* pick out a few only */
168 	ck_assert_str_eq(libevdev_event_code_get_name(EV_FF, FF_STATUS_STOPPED), "FF_STATUS_STOPPED");
169 	ck_assert_str_eq(libevdev_event_code_get_name(EV_FF, FF_FRICTION), "FF_FRICTION");
170 	ck_assert_str_eq(libevdev_event_code_get_name(EV_FF, FF_CUSTOM), "FF_CUSTOM");
171 	ck_assert_str_eq(libevdev_event_code_get_name(EV_FF, FF_MAX), "FF_MAX");
172 
173 	ck_assert(libevdev_event_code_get_name(EV_FF, FF_MAX - 1) == NULL);
174 
175 }
176 END_TEST
177 
START_TEST(test_code_syn_name)178 START_TEST(test_code_syn_name)
179 {
180 	ck_assert_str_eq(libevdev_event_code_get_name(EV_SYN, SYN_REPORT), "SYN_REPORT");
181 	ck_assert_str_eq(libevdev_event_code_get_name(EV_SYN, SYN_CONFIG), "SYN_CONFIG");
182 	ck_assert_str_eq(libevdev_event_code_get_name(EV_SYN, SYN_MT_REPORT), "SYN_MT_REPORT");
183 	ck_assert_str_eq(libevdev_event_code_get_name(EV_SYN, SYN_DROPPED), "SYN_DROPPED");
184 	ck_assert_str_eq(libevdev_event_code_get_name(EV_SYN, SYN_MAX), "SYN_MAX");
185 }
186 END_TEST
187 
START_TEST(test_value_name)188 START_TEST(test_value_name)
189 {
190 	unsigned int type, code;
191 	int value;
192 
193 	for (type = 0; type < EV_MAX; type++) {
194 		int max = libevdev_event_type_get_max(type);
195 
196 		if (max == -1)
197 			continue;
198 
199 		for (code = 0; code < (unsigned int)max; code++) {
200 			if (type == EV_ABS && code == ABS_MT_TOOL_TYPE)
201 				continue;
202 
203 			for (value = 0; value < 0xff; value++) {
204 				ck_assert(libevdev_event_value_get_name(type, code, value) == NULL);
205 			}
206 		}
207 	}
208 
209 	ck_assert_str_eq(libevdev_event_value_get_name(EV_ABS, ABS_MT_TOOL_TYPE, MT_TOOL_FINGER), "MT_TOOL_FINGER");
210 	ck_assert_str_eq(libevdev_event_value_get_name(EV_ABS, ABS_MT_TOOL_TYPE, MT_TOOL_PALM), "MT_TOOL_PALM");
211 	ck_assert_str_eq(libevdev_event_value_get_name(EV_ABS, ABS_MT_TOOL_TYPE, MT_TOOL_PEN), "MT_TOOL_PEN");
212 	ck_assert_str_eq(libevdev_event_value_get_name(EV_ABS, ABS_MT_TOOL_TYPE, MT_TOOL_MAX), "MT_TOOL_MAX");
213 	ck_assert(libevdev_event_value_get_name(EV_ABS, ABS_MT_TOOL_TYPE, -1) == NULL);
214 }
215 END_TEST
216 
START_TEST(test_prop_name)217 START_TEST(test_prop_name)
218 {
219 	ck_assert_str_eq(libevdev_property_get_name(INPUT_PROP_POINTER), "INPUT_PROP_POINTER");
220 	ck_assert_str_eq(libevdev_property_get_name(INPUT_PROP_DIRECT), "INPUT_PROP_DIRECT");
221 	ck_assert_str_eq(libevdev_property_get_name(INPUT_PROP_BUTTONPAD), "INPUT_PROP_BUTTONPAD");
222 	ck_assert_str_eq(libevdev_property_get_name(INPUT_PROP_SEMI_MT), "INPUT_PROP_SEMI_MT");
223 	ck_assert_str_eq(libevdev_property_get_name(INPUT_PROP_MAX), "INPUT_PROP_MAX");
224 
225 	ck_assert(libevdev_property_get_name(INPUT_PROP_MAX - 1) == NULL);
226 	ck_assert(libevdev_property_get_name(INPUT_PROP_MAX + 1) == NULL);
227 }
228 END_TEST
229 
START_TEST(test_event_type_max)230 START_TEST(test_event_type_max)
231 {
232 	ck_assert_int_eq(libevdev_event_type_get_max(EV_ABS), ABS_MAX);
233 	ck_assert_int_eq(libevdev_event_type_get_max(EV_REL), REL_MAX);
234 	ck_assert_int_eq(libevdev_event_type_get_max(EV_KEY), KEY_MAX);
235 
236 	ck_assert_int_eq(libevdev_event_type_get_max(EV_MAX - 1), -1);
237 	ck_assert_int_eq(libevdev_event_type_get_max(EV_MAX + 1), -1);
238 
239 }
240 END_TEST
241 
START_TEST(test_event_type)242 START_TEST(test_event_type)
243 {
244 	struct input_event ev;
245 	int i = 0;
246 
247 	ev.type = EV_REL;
248 
249 	ck_assert_int_eq(libevdev_event_is_type(&ev, EV_REL), 1);
250 	for (i = 0; i < EV_CNT; i++) {
251 		if (i == ev.type)
252 			continue;
253 		ck_assert_int_eq(libevdev_event_is_type(&ev, i), 0);
254 	}
255 	ck_assert_int_eq(libevdev_event_is_type(&ev, EV_MAX + 1), 0);
256 }
257 END_TEST
258 
START_TEST(test_event_code)259 START_TEST(test_event_code)
260 {
261 	struct input_event ev;
262 	int i = 0;
263 
264 	ev.type = EV_REL;
265 	ev.code = REL_Y;
266 
267 	ck_assert_int_eq(libevdev_event_is_code(&ev, EV_REL, REL_Y), 1);
268 	for (i = 0; i < EV_CNT; i++) {
269 		int j;
270 		if (i == ev.type || i == EV_SYN)
271 			continue;
272 
273 		for (j = 0; j < libevdev_event_type_get_max(i); i++) {
274 			ck_assert_int_eq(libevdev_event_is_code(&ev, i, j), 0);
275 		}
276 	}
277 	ck_assert_int_eq(libevdev_event_is_code(&ev, EV_MAX + 1, ev.code), 0);
278 	ck_assert_int_eq(libevdev_event_is_code(&ev, EV_REL, REL_MAX + 1), 0);
279 
280 	ev.type = EV_SYN;
281 	ev.code = SYN_REPORT;
282 	ck_assert_int_eq(libevdev_event_is_code(&ev, EV_SYN, SYN_REPORT), 1);
283 	ck_assert_int_eq(libevdev_event_is_code(&ev, EV_SYN, SYN_DROPPED), 0);
284 }
285 END_TEST
286 
TEST_SUITE(event_name_suite)287 TEST_SUITE(event_name_suite)
288 {
289 	Suite *s = suite_create("Event names");
290 
291 	add_test(s, test_limits);
292 	add_test(s, test_event_type_max);
293 
294 	add_test(s, test_type_name);
295 
296 	add_test(s, test_code_abs_name);
297 	add_test(s, test_code_rel_name);
298 	add_test(s, test_code_key_name);
299 	add_test(s, test_code_led_name);
300 	add_test(s, test_code_snd_name);
301 	add_test(s, test_code_rep_name);
302 	add_test(s, test_code_msc_name);
303 	add_test(s, test_code_sw_name);
304 	add_test(s, test_code_ff_name);
305 	add_test(s, test_code_syn_name);
306 
307 	add_test(s, test_value_name);
308 	add_test(s, test_prop_name);
309 
310 	add_test(s, test_event_type);
311 	add_test(s, test_event_code);
312 
313 	return s;
314 }
315