• 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 <libevdev/libevdev.h>
8 #include <stdbool.h>
9 #include <stdio.h>
10 
11 #include <check.h>
12 
13 #ifndef _TEST_COMMON_H_
14 #define _TEST_COMMON_H_
15 
16 struct libevdev_test {
17 	const char *name;
18 	Suite* (*setup)(void);
19 	bool needs_root_privileges;
20 } __attribute__((aligned(16)));
21 
22 #define _TEST_SUITE(name, root_privs) \
23 	static Suite* (name##_setup)(void); \
24 	static const struct libevdev_test _test \
25 	__attribute__((used)) \
26 	__attribute__((section ("test_section"))) = { \
27 		#name, name##_setup, root_privs \
28 	}; \
29 	static Suite* (name##_setup)(void)
30 
31 #define TEST_SUITE(name) \
32 	_TEST_SUITE(name, false)
33 
34 #define TEST_SUITE_ROOT_PRIVILEGES(name) \
35 	_TEST_SUITE(name, true)
36 
37 #define TEST_DEVICE_NAME "libevdev test device"
38 
39 #define add_test(suite, func) do { \
40 	TCase *tc = tcase_create(#func); \
41 	tcase_add_test(tc, func); \
42 	suite_add_tcase(suite, tc); \
43 } while(0)
44 
45 #include "test-common-uinput.h"
46 
47 #define assert_event(e_, t, c, v) \
48 do { \
49 	const struct input_event *e = (e_); \
50 	ck_assert_int_eq(e->type, (t)); \
51 	ck_assert_int_eq(e->code, (c)); \
52 	ck_assert_int_eq(e->value, (v)); \
53 } while(0)
54 
55 void test_create_device(struct uinput_device **uidev,
56 			struct libevdev **dev,
57 			...);
58 void test_create_abs_device(struct uinput_device **uidev,
59 			    struct libevdev **dev,
60 			    int nabs,
61 			    const struct input_absinfo *abs,
62 			    ...);
63 
64 void test_logfunc_abort_on_error(enum libevdev_log_priority priority,
65 				 void *data,
66 				 const char *file, int line,
67 				 const char *func,
68 				 const char *format, va_list args);
69 void test_logfunc_ignore_error(enum libevdev_log_priority priority,
70 			       void *data,
71 			       const char *file, int line,
72 			       const char *func,
73 			       const char *format, va_list args);
74 
75 static inline void
print_event(const struct input_event * ev)76 print_event(const struct input_event *ev)
77 {
78 	if (ev->type == EV_SYN)
79 		printf("Event: time %ld.%06ld, ++++++++++++++++++++ %s +++++++++++++++\n",
80 			ev->input_event_sec,
81 			ev->input_event_usec,
82 			libevdev_event_type_get_name(ev->type));
83 	else
84 		printf("Event: time %ld.%06ld, type %d (%s), code %d (%s), value %d\n",
85 			ev->input_event_sec,
86 			ev->input_event_usec,
87 			ev->type,
88 			libevdev_event_type_get_name(ev->type),
89 			ev->code,
90 			libevdev_event_code_get_name(ev->type, ev->code),
91 			ev->value);
92 }
93 #endif /* _TEST_COMMON_H_ */
94