2 * Copyright © 2013 Red Hat, Inc.
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
28 #include <sys/types.h>
32 #include <libevdev/libevdev-uinput.h>
33 #include "test-common.h"
35 START_TEST(test_new_device)
40 ck_assert(dev != NULL);
45 START_TEST(test_free_device)
51 START_TEST(test_init_from_invalid_fd)
54 struct libevdev *dev = NULL;
56 rc = libevdev_new_from_fd(-1, &dev);
58 ck_assert(dev == NULL);
59 ck_assert_int_eq(rc, -EBADF);
61 rc = libevdev_new_from_fd(STDIN_FILENO, &dev);
62 ck_assert(dev == NULL);
63 ck_assert_int_eq(rc, -ENOTTY);
67 START_TEST(test_init_and_change_fd)
69 struct uinput_device* uidev;
74 ck_assert(dev != NULL);
75 ck_assert_int_eq(libevdev_set_fd(dev, -1), -EBADF);
76 ck_assert_int_eq(libevdev_change_fd(dev, -1), -1);
78 rc = uinput_device_new_with_events(&uidev,
79 TEST_DEVICE_NAME, DEFAULT_IDS,
88 ck_assert_msg(rc == 0, "Failed to create uinput device: %s", strerror(-rc));
89 ck_assert_int_eq(libevdev_set_fd(dev, uinput_device_get_fd(uidev)), 0);
90 ck_assert_int_eq(libevdev_set_fd(dev, 0), -EBADF);
92 ck_assert_int_eq(libevdev_get_fd(dev), uinput_device_get_fd(uidev));
94 ck_assert_int_eq(libevdev_change_fd(dev, 0), 0);
95 ck_assert_int_eq(libevdev_get_fd(dev), 0);
97 uinput_device_free(uidev);
103 static int log_fn_called = 0;
104 static char *logdata = "test";
105 static void logfunc(enum libevdev_log_priority priority,
107 const char *file, int line, const char *func,
108 const char *f, va_list args) {
109 ck_assert_int_eq(strcmp(logdata, data), 0);
113 START_TEST(test_log_init)
115 struct libevdev *dev = NULL;
117 libevdev_set_log_function(logfunc, NULL);
118 libevdev_set_log_function(NULL, NULL);
120 dev = libevdev_new();
121 ck_assert(dev != NULL);
123 libevdev_set_log_function(logfunc, logdata);
124 libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL);
126 libevdev_set_log_function(NULL, NULL);
127 libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL);
129 libevdev_set_log_function(logfunc, logdata);
130 libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL);
132 /* libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL) should
133 trigger a log message. We called it three times, but only twice
134 with the logfunc set, thus, ensure we only called the logfunc
136 ck_assert_int_eq(log_fn_called, 2);
142 static char *logdata_1 = "foo";
143 static char *logdata_2 = "bar";
144 static int log_data_fn_called = 0;
145 static void logfunc_data(enum libevdev_log_priority priority,
147 const char *file, int line, const char *func,
148 const char *f, va_list args) {
149 switch(log_data_fn_called) {
150 case 0: ck_assert(data == logdata_1); break;
151 case 1: ck_assert(data == logdata_2); break;
152 case 2: ck_assert(data == NULL); break;
156 log_data_fn_called++;
159 START_TEST(test_log_data)
161 struct libevdev *dev = NULL;
163 dev = libevdev_new();
164 ck_assert(dev != NULL);
166 libevdev_set_log_function(logfunc_data, logdata_1);
167 libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL);
169 libevdev_set_log_function(logfunc_data, logdata_2);
170 libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL);
172 libevdev_set_log_function(logfunc_data, NULL);
173 libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL);
179 START_TEST(test_device_init)
181 struct uinput_device* uidev;
182 struct libevdev *dev;
185 rc = uinput_device_new_with_events(&uidev,
186 TEST_DEVICE_NAME, DEFAULT_IDS,
195 ck_assert_msg(rc == 0, "Failed to create uinput device: %s", strerror(-rc));
197 dev = libevdev_new();
198 ck_assert(dev != NULL);
199 rc = libevdev_set_fd(dev, uinput_device_get_fd(uidev));
200 ck_assert_msg(rc == 0, "Failed to init device: %s", strerror(-rc));;
202 uinput_device_free(uidev);
207 START_TEST(test_device_init_from_fd)
209 struct uinput_device* uidev;
210 struct libevdev *dev;
213 rc = uinput_device_new_with_events(&uidev,
214 TEST_DEVICE_NAME, DEFAULT_IDS,
223 ck_assert_msg(rc == 0, "Failed to create uinput device: %s", strerror(-rc));
225 rc = libevdev_new_from_fd(uinput_device_get_fd(uidev), &dev);
226 ck_assert_msg(rc == 0, "Failed to init device: %s", strerror(-rc));;
228 uinput_device_free(uidev);
233 START_TEST(test_device_grab)
235 struct uinput_device* uidev;
236 struct libevdev *dev;
239 rc = test_create_device(&uidev, &dev,
248 ck_assert_msg(rc == 0, "Failed to create device: %s", strerror(-rc));
250 rc = libevdev_grab(dev, 0);
251 ck_assert_int_eq(rc, -EINVAL);
252 rc = libevdev_grab(dev, 1);
253 ck_assert_int_eq(rc, -EINVAL);
255 rc = libevdev_grab(dev, LIBEVDEV_UNGRAB);
256 ck_assert_int_eq(rc, 0);
257 rc = libevdev_grab(dev, LIBEVDEV_GRAB);
258 ck_assert_int_eq(rc, 0);
259 rc = libevdev_grab(dev, LIBEVDEV_GRAB);
260 ck_assert_int_eq(rc, 0);
261 rc = libevdev_grab(dev, LIBEVDEV_UNGRAB);
262 ck_assert_int_eq(rc, 0);
264 uinput_device_free(uidev);
269 START_TEST(test_set_clock_id)
271 struct uinput_device* uidev;
272 struct libevdev *dev;
275 rc = test_create_device(&uidev, &dev,
284 ck_assert_msg(rc == 0, "Failed to create device: %s", strerror(-rc));
286 rc = libevdev_set_clock_id(dev, CLOCK_REALTIME);
287 ck_assert_int_eq(rc, 0);
289 rc = libevdev_set_clock_id(dev, CLOCK_MONOTONIC);
290 ck_assert_int_eq(rc, 0);
292 rc = libevdev_set_clock_id(dev, CLOCK_MONOTONIC_RAW);
293 ck_assert_int_eq(rc, -EINVAL);
295 uinput_device_free(uidev);
300 START_TEST(test_clock_id_events)
302 struct uinput_device* uidev;
303 struct libevdev *dev, *dev2;
305 struct input_event ev1, ev2;
306 struct timespec t1_real, t2_real;
307 struct timespec t1_mono, t2_mono;
310 rc = test_create_device(&uidev, &dev,
319 ck_assert_msg(rc == 0, "Failed to create device: %s", strerror(-rc));
321 fd = open(uinput_device_get_devnode(uidev), O_RDONLY);
322 ck_assert_int_gt(fd, -1);
324 rc = libevdev_new_from_fd(fd, &dev2);
325 ck_assert_msg(rc == 0, "Failed to create second device: %s", strerror(-rc));
327 rc = libevdev_set_clock_id(dev2, CLOCK_MONOTONIC);
328 ck_assert_int_eq(rc, 0);
330 clock_gettime(CLOCK_REALTIME, &t1_real);
331 clock_gettime(CLOCK_MONOTONIC, &t1_mono);
332 uinput_device_event(uidev, EV_REL, REL_X, 1);
333 uinput_device_event(uidev, EV_SYN, SYN_REPORT, 0);
334 clock_gettime(CLOCK_REALTIME, &t2_real);
335 clock_gettime(CLOCK_MONOTONIC, &t2_mono);
337 rc = libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, &ev1);
338 ck_assert_int_eq(rc, LIBEVDEV_READ_STATUS_SUCCESS);
340 rc = libevdev_next_event(dev2, LIBEVDEV_READ_FLAG_NORMAL, &ev2);
341 ck_assert_int_eq(rc, LIBEVDEV_READ_STATUS_SUCCESS);
343 ck_assert_int_eq(ev1.type, ev2.type);
344 ck_assert_int_eq(ev1.code, ev2.code);
345 ck_assert_int_eq(ev1.value, ev2.value);
347 t1 = ev1.time.tv_sec * 1000000LL + ev1.time.tv_usec;
348 t2 = ev2.time.tv_sec * 1000000LL + ev2.time.tv_usec;
349 ck_assert_int_ne(t1, t2);
351 ck_assert_int_ge(ev1.time.tv_sec, t1_real.tv_sec);
352 ck_assert_int_ge(ev1.time.tv_usec, t1_real.tv_nsec/1000);
353 ck_assert_int_le(ev1.time.tv_sec, t2_real.tv_sec);
354 ck_assert_int_le(ev1.time.tv_usec, t2_real.tv_nsec/1000);
356 ck_assert_int_ge(ev2.time.tv_sec, t1_mono.tv_sec);
357 ck_assert_int_ge(ev2.time.tv_usec, t1_mono.tv_nsec/1000);
358 ck_assert_int_le(ev2.time.tv_sec, t2_mono.tv_sec);
359 ck_assert_int_le(ev2.time.tv_usec, t2_mono.tv_nsec/1000);
361 uinput_device_free(uidev);
370 libevdev_init_test(void)
372 Suite *s = suite_create("libevdev init tests");
374 TCase *tc = tcase_create("device init");
375 tcase_add_test(tc, test_new_device);
376 tcase_add_test(tc, test_free_device);
377 tcase_add_test(tc, test_init_from_invalid_fd);
378 tcase_add_test(tc, test_init_and_change_fd);
379 suite_add_tcase(s, tc);
381 tc = tcase_create("log init");
382 tcase_add_test(tc, test_log_init);
383 tcase_add_test(tc, test_log_data);
384 suite_add_tcase(s, tc);
386 tc = tcase_create("device fd init");
387 tcase_add_test(tc, test_device_init);
388 tcase_add_test(tc, test_device_init_from_fd);
389 suite_add_tcase(s, tc);
391 tc = tcase_create("device grab");
392 tcase_add_test(tc, test_device_grab);
393 suite_add_tcase(s, tc);
395 tc = tcase_create("clock id");
396 tcase_add_test(tc, test_set_clock_id);
397 tcase_add_test(tc, test_clock_id_events);
398 suite_add_tcase(s, tc);