tizen 2.4 release
[framework/uifw/libevdev.git] / test / test-libevdev-init.c
1 /*
2  * Copyright © 2013 Red Hat, Inc.
3  *
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.
13  *
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
20  * OF THIS SOFTWARE.
21  */
22
23 #include <config.h>
24 #include <errno.h>
25 #include <inttypes.h>
26 #include <unistd.h>
27 #include <time.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31
32 #include <libevdev/libevdev-uinput.h>
33 #include "test-common.h"
34
35 START_TEST(test_new_device)
36 {
37         struct libevdev *dev;
38
39         dev = libevdev_new();
40         ck_assert(dev != NULL);
41         libevdev_free(dev);
42 }
43 END_TEST
44
45 START_TEST(test_free_device)
46 {
47         libevdev_free(NULL);
48 }
49 END_TEST
50
51 START_TEST(test_init_from_invalid_fd)
52 {
53         int rc;
54         struct libevdev *dev = NULL;
55
56         rc = libevdev_new_from_fd(-1, &dev);
57
58         ck_assert(dev == NULL);
59         ck_assert_int_eq(rc, -EBADF);
60
61         rc = libevdev_new_from_fd(STDIN_FILENO, &dev);
62         ck_assert(dev == NULL);
63         ck_assert_int_eq(rc, -ENOTTY);
64 }
65 END_TEST
66
67 START_TEST(test_init_and_change_fd)
68 {
69         struct uinput_device* uidev;
70         struct libevdev *dev;
71         int rc;
72
73         dev = libevdev_new();
74         ck_assert(dev != NULL);
75         ck_assert_int_eq(libevdev_set_fd(dev, -1), -EBADF);
76
77         libevdev_set_log_function(test_logfunc_ignore_error, NULL);
78         ck_assert_int_eq(libevdev_change_fd(dev, -1), -1);
79         libevdev_set_log_function(test_logfunc_abort_on_error, NULL);
80
81         rc = uinput_device_new_with_events(&uidev,
82                                            TEST_DEVICE_NAME, DEFAULT_IDS,
83                                            EV_SYN, SYN_REPORT,
84                                            EV_REL, REL_X,
85                                            EV_REL, REL_Y,
86                                            EV_REL, REL_WHEEL,
87                                            EV_KEY, BTN_LEFT,
88                                            EV_KEY, BTN_MIDDLE,
89                                            EV_KEY, BTN_RIGHT,
90                                            -1);
91         ck_assert_msg(rc == 0, "Failed to create uinput device: %s", strerror(-rc));
92         ck_assert_int_eq(libevdev_set_fd(dev, uinput_device_get_fd(uidev)), 0);
93
94         libevdev_set_log_function(test_logfunc_ignore_error, NULL);
95         ck_assert_int_eq(libevdev_set_fd(dev, 0), -EBADF);
96         libevdev_set_log_function(test_logfunc_abort_on_error, NULL);
97
98         ck_assert_int_eq(libevdev_get_fd(dev), uinput_device_get_fd(uidev));
99
100         ck_assert_int_eq(libevdev_change_fd(dev, 0), 0);
101         ck_assert_int_eq(libevdev_get_fd(dev), 0);
102
103         uinput_device_free(uidev);
104         libevdev_free(dev);
105 }
106 END_TEST
107
108 static int log_fn_called = 0;
109 static char *logdata = "test";
110 static void logfunc(enum libevdev_log_priority priority,
111                     void *data,
112                     const char *file, int line, const char *func,
113                     const char *f, va_list args) {
114         ck_assert_int_eq(strcmp(logdata, data), 0);
115         log_fn_called++;
116 }
117
118 START_TEST(test_log_init)
119 {
120         struct libevdev *dev = NULL;
121         enum libevdev_log_priority old;
122
123         old = libevdev_get_log_priority();
124
125         libevdev_set_log_priority(LIBEVDEV_LOG_DEBUG);
126
127         libevdev_set_log_function(logfunc, NULL);
128         libevdev_set_log_function(NULL, NULL);
129
130         dev = libevdev_new();
131         ck_assert(dev != NULL);
132
133         libevdev_set_log_function(logfunc, logdata);
134         libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL);
135
136         libevdev_set_log_function(NULL, NULL);
137         libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL);
138
139         libevdev_set_log_function(logfunc, logdata);
140         libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL);
141
142         /* libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL) should
143            trigger a log message. We called it three times, but only twice
144            with the logfunc set, thus, ensure we only called the logfunc
145            twice */
146         ck_assert_int_eq(log_fn_called, 2);
147
148         libevdev_free(dev);
149
150         libevdev_set_log_function(test_logfunc_abort_on_error, NULL);
151
152         log_fn_called = 0;
153
154         libevdev_set_log_priority(old);
155 }
156 END_TEST
157
158 START_TEST(test_log_default_priority)
159 {
160         ck_assert_int_eq(libevdev_get_log_priority(), LIBEVDEV_LOG_INFO);
161 }
162 END_TEST
163
164 START_TEST(test_log_set_get_priority)
165 {
166         enum libevdev_log_priority pri;
167         enum libevdev_log_priority old;
168
169         old = libevdev_get_log_priority();
170
171         pri = LIBEVDEV_LOG_DEBUG;
172         libevdev_set_log_priority(pri);
173         ck_assert_int_eq(libevdev_get_log_priority(), pri);
174
175         pri = LIBEVDEV_LOG_INFO;
176         libevdev_set_log_priority(pri);
177         ck_assert_int_eq(libevdev_get_log_priority(), pri);
178
179         pri = LIBEVDEV_LOG_ERROR;
180         libevdev_set_log_priority(pri);
181         ck_assert_int_eq(libevdev_get_log_priority(), pri);
182
183         /* debug and above is clamped */
184         pri = LIBEVDEV_LOG_DEBUG + 1;
185         libevdev_set_log_priority(pri);
186         ck_assert_int_eq(libevdev_get_log_priority(), LIBEVDEV_LOG_DEBUG);
187
188         /*  error and below is not clamped, we need this for another test */
189         pri = LIBEVDEV_LOG_ERROR - 1;
190         libevdev_set_log_priority(pri);
191         ck_assert_int_eq(libevdev_get_log_priority(), pri);
192
193         libevdev_set_log_priority(old);
194 }
195 END_TEST
196
197 START_TEST(test_log_priority)
198 {
199         struct libevdev *dev = NULL;
200         enum libevdev_log_priority old;
201
202         old = libevdev_get_log_priority();
203
204         libevdev_set_log_function(logfunc, logdata);
205
206         dev = libevdev_new();
207         ck_assert(dev != NULL);
208
209         libevdev_set_log_priority(LIBEVDEV_LOG_DEBUG);
210         libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL);
211         ck_assert_int_eq(log_fn_called, 1);
212
213         libevdev_set_log_priority(LIBEVDEV_LOG_INFO);
214         libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL);
215         ck_assert_int_eq(log_fn_called, 2);
216
217         libevdev_set_log_priority(LIBEVDEV_LOG_ERROR);
218         libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL);
219         ck_assert_int_eq(log_fn_called, 3);
220
221         /* we don't have any log msgs > ERROR at the moment, so test it by
222            setting an invalid priority. */
223         libevdev_set_log_priority(LIBEVDEV_LOG_ERROR - 1);
224         libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL);
225         ck_assert_int_eq(log_fn_called, 3);
226
227         libevdev_free(dev);
228
229         libevdev_set_log_function(test_logfunc_abort_on_error, NULL);
230
231         log_fn_called = 0;
232
233         libevdev_set_log_priority(old);
234 }
235 END_TEST
236
237 static char *logdata_1 = "foo";
238 static char *logdata_2 = "bar";
239 static int log_data_fn_called = 0;
240 static void logfunc_data(enum libevdev_log_priority priority,
241                          void *data,
242                          const char *file, int line, const char *func,
243                          const char *f, va_list args) {
244         switch(log_data_fn_called) {
245                 case 0: ck_assert(data == logdata_1); break;
246                 case 1: ck_assert(data == logdata_2); break;
247                 case 2: ck_assert(data == NULL); break;
248                 default:
249                         ck_abort();
250         }
251         log_data_fn_called++;
252 }
253
254 START_TEST(test_log_data)
255 {
256         struct libevdev *dev = NULL;
257
258         dev = libevdev_new();
259         ck_assert(dev != NULL);
260
261         libevdev_set_log_function(logfunc_data, logdata_1);
262         libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL);
263
264         libevdev_set_log_function(logfunc_data, logdata_2);
265         libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL);
266
267         libevdev_set_log_function(logfunc_data, NULL);
268         libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, NULL);
269
270         libevdev_free(dev);
271 }
272 END_TEST
273
274 START_TEST(test_device_init)
275 {
276         struct uinput_device* uidev;
277         struct libevdev *dev;
278         int rc;
279
280         rc = uinput_device_new_with_events(&uidev,
281                                            TEST_DEVICE_NAME, DEFAULT_IDS,
282                                            EV_SYN, SYN_REPORT,
283                                            EV_REL, REL_X,
284                                            EV_REL, REL_Y,
285                                            EV_REL, REL_WHEEL,
286                                            EV_KEY, BTN_LEFT,
287                                            EV_KEY, BTN_MIDDLE,
288                                            EV_KEY, BTN_RIGHT,
289                                            -1);
290         ck_assert_msg(rc == 0, "Failed to create uinput device: %s", strerror(-rc));
291
292         dev = libevdev_new();
293         ck_assert(dev != NULL);
294         rc = libevdev_set_fd(dev, uinput_device_get_fd(uidev));
295         ck_assert_msg(rc == 0, "Failed to init device: %s", strerror(-rc));;
296
297         uinput_device_free(uidev);
298         libevdev_free(dev);
299 }
300 END_TEST
301
302 START_TEST(test_device_init_from_fd)
303 {
304         struct uinput_device* uidev;
305         struct libevdev *dev;
306         int rc;
307
308         rc = uinput_device_new_with_events(&uidev,
309                                            TEST_DEVICE_NAME, DEFAULT_IDS,
310                                            EV_SYN, SYN_REPORT,
311                                            EV_REL, REL_X,
312                                            EV_REL, REL_Y,
313                                            EV_REL, REL_WHEEL,
314                                            EV_KEY, BTN_LEFT,
315                                            EV_KEY, BTN_MIDDLE,
316                                            EV_KEY, BTN_RIGHT,
317                                            -1);
318         ck_assert_msg(rc == 0, "Failed to create uinput device: %s", strerror(-rc));
319
320         rc = libevdev_new_from_fd(uinput_device_get_fd(uidev), &dev);
321         ck_assert_msg(rc == 0, "Failed to init device: %s", strerror(-rc));;
322
323         uinput_device_free(uidev);
324         libevdev_free(dev);
325 }
326 END_TEST
327
328 START_TEST(test_device_grab)
329 {
330         struct uinput_device* uidev;
331         struct libevdev *dev;
332         int rc;
333
334         test_create_device(&uidev, &dev,
335                            EV_SYN, SYN_REPORT,
336                            EV_REL, REL_X,
337                            EV_REL, REL_Y,
338                            EV_REL, REL_WHEEL,
339                            EV_KEY, BTN_LEFT,
340                            EV_KEY, BTN_MIDDLE,
341                            EV_KEY, BTN_RIGHT,
342                            -1);
343
344         libevdev_set_log_function(test_logfunc_ignore_error, NULL);
345         rc = libevdev_grab(dev, 0);
346         ck_assert_int_eq(rc, -EINVAL);
347         rc = libevdev_grab(dev, 1);
348         ck_assert_int_eq(rc, -EINVAL);
349         libevdev_set_log_function(test_logfunc_abort_on_error, NULL);
350
351         rc = libevdev_grab(dev, LIBEVDEV_UNGRAB);
352         ck_assert_int_eq(rc, 0);
353         rc = libevdev_grab(dev, LIBEVDEV_GRAB);
354         ck_assert_int_eq(rc, 0);
355         rc = libevdev_grab(dev, LIBEVDEV_GRAB);
356         ck_assert_int_eq(rc, 0);
357         rc = libevdev_grab(dev, LIBEVDEV_UNGRAB);
358         ck_assert_int_eq(rc, 0);
359
360         uinput_device_free(uidev);
361         libevdev_free(dev);
362 }
363 END_TEST
364
365 START_TEST(test_set_clock_id)
366 {
367         struct uinput_device* uidev;
368         struct libevdev *dev;
369         int rc;
370
371         test_create_device(&uidev, &dev,
372                            EV_SYN, SYN_REPORT,
373                            EV_REL, REL_X,
374                            EV_REL, REL_Y,
375                            EV_REL, REL_WHEEL,
376                            EV_KEY, BTN_LEFT,
377                            EV_KEY, BTN_MIDDLE,
378                            EV_KEY, BTN_RIGHT,
379                            -1);
380
381         rc = libevdev_set_clock_id(dev, CLOCK_REALTIME);
382         ck_assert_int_eq(rc, 0);
383
384         rc = libevdev_set_clock_id(dev, CLOCK_MONOTONIC);
385         ck_assert_int_eq(rc, 0);
386
387         rc = libevdev_set_clock_id(dev, CLOCK_MONOTONIC_RAW);
388         ck_assert_int_eq(rc, -EINVAL);
389
390         uinput_device_free(uidev);
391         libevdev_free(dev);
392 }
393 END_TEST
394
395 START_TEST(test_clock_id_events)
396 {
397         struct uinput_device* uidev;
398         struct libevdev *dev, *dev2;
399         int rc, fd;
400         struct input_event ev1, ev2;
401         struct timespec t1_real, t2_real;
402         struct timespec t1_mono, t2_mono;
403         int64_t t1, t2;
404
405         test_create_device(&uidev, &dev,
406                            EV_SYN, SYN_REPORT,
407                            EV_REL, REL_X,
408                            EV_REL, REL_Y,
409                            EV_REL, REL_WHEEL,
410                            EV_KEY, BTN_LEFT,
411                            EV_KEY, BTN_MIDDLE,
412                            EV_KEY, BTN_RIGHT,
413                            -1);
414
415         fd = open(uinput_device_get_devnode(uidev), O_RDONLY);
416         ck_assert_int_gt(fd, -1);
417
418         rc = libevdev_new_from_fd(fd, &dev2);
419         ck_assert_msg(rc == 0, "Failed to create second device: %s", strerror(-rc));
420
421         rc = libevdev_set_clock_id(dev2, CLOCK_MONOTONIC);
422         ck_assert_int_eq(rc, 0);
423
424         clock_gettime(CLOCK_REALTIME, &t1_real);
425         clock_gettime(CLOCK_MONOTONIC, &t1_mono);
426         uinput_device_event(uidev, EV_REL, REL_X, 1);
427         uinput_device_event(uidev, EV_SYN, SYN_REPORT, 0);
428         clock_gettime(CLOCK_REALTIME, &t2_real);
429         clock_gettime(CLOCK_MONOTONIC, &t2_mono);
430
431         rc = libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, &ev1);
432         ck_assert_int_eq(rc, LIBEVDEV_READ_STATUS_SUCCESS);
433
434         rc = libevdev_next_event(dev2, LIBEVDEV_READ_FLAG_NORMAL, &ev2);
435         ck_assert_int_eq(rc, LIBEVDEV_READ_STATUS_SUCCESS);
436
437         ck_assert_int_eq(ev1.type, ev2.type);
438         ck_assert_int_eq(ev1.code, ev2.code);
439         ck_assert_int_eq(ev1.value, ev2.value);
440
441         t1 = ev1.time.tv_sec * 1000000LL + ev1.time.tv_usec;
442         t2 = ev2.time.tv_sec * 1000000LL + ev2.time.tv_usec;
443         ck_assert_int_ne(t1, t2);
444
445         ck_assert_int_ge(ev1.time.tv_sec, t1_real.tv_sec);
446         ck_assert_int_ge(ev1.time.tv_usec, t1_real.tv_nsec/1000);
447         ck_assert_int_le(ev1.time.tv_sec, t2_real.tv_sec);
448         ck_assert_int_le(ev1.time.tv_usec, t2_real.tv_nsec/1000);
449
450         ck_assert_int_ge(ev2.time.tv_sec, t1_mono.tv_sec);
451         ck_assert_int_ge(ev2.time.tv_usec, t1_mono.tv_nsec/1000);
452         ck_assert_int_le(ev2.time.tv_sec, t2_mono.tv_sec);
453         ck_assert_int_le(ev2.time.tv_usec, t2_mono.tv_nsec/1000);
454
455         uinput_device_free(uidev);
456         libevdev_free(dev);
457         libevdev_free(dev2);
458         close(fd);
459 }
460 END_TEST
461
462 Suite *
463 libevdev_init_test(void)
464 {
465         Suite *s = suite_create("libevdev init tests");
466
467         TCase *tc = tcase_create("device init");
468         tcase_add_test(tc, test_new_device);
469         tcase_add_test(tc, test_free_device);
470         tcase_add_test(tc, test_init_from_invalid_fd);
471         tcase_add_test(tc, test_init_and_change_fd);
472         suite_add_tcase(s, tc);
473
474         tc = tcase_create("log init");
475         tcase_add_test(tc, test_log_init);
476         tcase_add_test(tc, test_log_priority);
477         tcase_add_test(tc, test_log_set_get_priority);
478         tcase_add_test(tc, test_log_default_priority);
479         tcase_add_test(tc, test_log_data);
480         suite_add_tcase(s, tc);
481
482         tc = tcase_create("device fd init");
483         tcase_add_test(tc, test_device_init);
484         tcase_add_test(tc, test_device_init_from_fd);
485         suite_add_tcase(s, tc);
486
487         tc = tcase_create("device grab");
488         tcase_add_test(tc, test_device_grab);
489         suite_add_tcase(s, tc);
490
491         tc = tcase_create("clock id");
492         tcase_add_test(tc, test_set_clock_id);
493         tcase_add_test(tc, test_clock_id_events);
494         suite_add_tcase(s, tc);
495
496         return s;
497 }