3 * Copyright © 2013 Red Hat, Inc.
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
35 static int open_func_count = 0;
36 static int close_func_count = 0;
38 static int open_restricted(const char *path, int flags, void *data)
42 fd = open(path, flags);
43 return fd < 0 ? -errno : fd;
45 static void close_restricted(int fd, void *data)
51 const struct libinput_interface simple_interface = {
52 .open_restricted = open_restricted,
53 .close_restricted = close_restricted,
57 START_TEST(path_create_NULL)
60 const struct libinput_interface interface;
65 li = libinput_path_create_context(NULL, NULL);
66 ck_assert(li == NULL);
67 li = libinput_path_create_context(&interface, NULL);
68 ck_assert(li != NULL);
71 ck_assert_int_eq(open_func_count, 0);
72 ck_assert_int_eq(close_func_count, 0);
79 START_TEST(path_create_invalid)
82 struct libinput_device *device;
83 const char *path = "/tmp";
88 li = libinput_path_create_context(&simple_interface, NULL);
89 ck_assert(li != NULL);
90 device = libinput_path_add_device(li, path);
91 ck_assert(device == NULL);
93 ck_assert_int_eq(open_func_count, 0);
94 ck_assert_int_eq(close_func_count, 0);
97 ck_assert_int_eq(close_func_count, 0);
100 close_func_count = 0;
104 START_TEST(path_create_destroy)
107 struct libinput_device *device;
108 struct libevdev_uinput *uinput;
110 void *userdata = &rc;
112 uinput = litest_create_uinput_device("test device", NULL,
119 li = libinput_path_create_context(&simple_interface, userdata);
120 ck_assert(li != NULL);
121 ck_assert(libinput_get_user_data(li) == userdata);
123 device = libinput_path_add_device(li,
124 libevdev_uinput_get_devnode(uinput));
125 ck_assert(device != NULL);
127 ck_assert_int_eq(open_func_count, 1);
129 libevdev_uinput_destroy(uinput);
130 libinput_destroy(li);
131 ck_assert_int_eq(close_func_count, 1);
134 close_func_count = 0;
138 START_TEST(path_added_seat)
140 struct litest_device *dev = litest_current_device();
141 struct libinput *li = dev->libinput;
142 struct libinput_event *event;
143 struct libinput_device *device;
144 struct libinput_seat *seat;
145 const char *seat_name;
146 enum libinput_event_type type;
148 libinput_dispatch(li);
150 event = libinput_get_event(li);
151 ck_assert(event != NULL);
153 type = libinput_event_get_type(event);
154 ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_ADDED);
156 device = libinput_event_get_device(event);
157 seat = libinput_device_get_seat(device);
158 ck_assert(seat != NULL);
160 seat_name = libinput_seat_get_logical_name(seat);
161 ck_assert_int_eq(strcmp(seat_name, "default"), 0);
163 libinput_event_destroy(event);
167 START_TEST(path_added_device)
169 struct litest_device *dev = litest_current_device();
170 struct libinput *li = dev->libinput;
171 struct libinput_event *event;
172 struct libinput_device *device;
174 libinput_dispatch(li);
176 while ((event = libinput_get_event(li))) {
177 enum libinput_event_type type;
178 type = libinput_event_get_type(event);
180 if (type == LIBINPUT_EVENT_DEVICE_ADDED) {
184 libinput_event_destroy(event);
187 ck_assert(event != NULL);
189 device = libinput_event_get_device(event);
190 ck_assert(device != NULL);
192 libinput_event_destroy(event);
196 START_TEST(path_add_device)
198 struct litest_device *dev = litest_current_device();
199 struct libinput *li = dev->libinput;
200 struct libinput_event *event;
201 struct libinput_device *device;
202 const char *sysname1 = NULL, *sysname2 = NULL;
204 libinput_dispatch(li);
206 while ((event = libinput_get_event(li))) {
207 enum libinput_event_type type;
208 type = libinput_event_get_type(event);
210 if (type == LIBINPUT_EVENT_DEVICE_ADDED) {
211 ck_assert(sysname1 == NULL);
212 device = libinput_event_get_device(event);
213 ck_assert(device != NULL);
214 sysname1 = libinput_device_get_sysname(device);
217 libinput_event_destroy(event);
220 device = libinput_path_add_device(li,
221 libevdev_uinput_get_devnode(dev->uinput));
222 ck_assert(device != NULL);
224 libinput_dispatch(li);
226 while ((event = libinput_get_event(li))) {
227 enum libinput_event_type type;
228 type = libinput_event_get_type(event);
230 if (type == LIBINPUT_EVENT_DEVICE_ADDED) {
231 ck_assert(sysname2 == NULL);
232 device = libinput_event_get_device(event);
233 ck_assert(device != NULL);
234 sysname2 = libinput_device_get_sysname(device);
237 libinput_event_destroy(event);
240 ck_assert_int_eq(strcmp(sysname1, sysname2), 0);
242 libinput_event_destroy(event);
246 START_TEST(path_add_invalid_path)
248 struct litest_device *dev = litest_current_device();
249 struct libinput *li = dev->libinput;
250 struct libinput_event *event;
251 struct libinput_device *device;
253 litest_drain_events(li);
255 device = libinput_path_add_device(li, "/tmp/");
256 ck_assert(device == NULL);
258 libinput_dispatch(li);
260 while ((event = libinput_get_event(li)))
265 START_TEST(path_device_sysname)
267 struct litest_device *dev = litest_current_device();
268 struct libinput_event *ev;
269 struct libinput_device *device;
272 libinput_dispatch(dev->libinput);
274 while ((ev = libinput_get_event(dev->libinput))) {
275 if (libinput_event_get_type(ev) != LIBINPUT_EVENT_DEVICE_ADDED)
278 device = libinput_event_get_device(ev);
279 sysname = libinput_device_get_sysname(device);
280 ck_assert(sysname != NULL && strlen(sysname) > 1);
281 ck_assert(strchr(sysname, '/') == NULL);
282 ck_assert_int_eq(strncmp(sysname, "event", 5), 0);
284 libinput_event_destroy(ev);
289 START_TEST(path_remove_device)
291 struct litest_device *dev = litest_current_device();
292 struct libinput *li = dev->libinput;
293 struct libinput_event *event;
294 struct libinput_device *device;
295 int remove_event = 0;
297 device = libinput_path_add_device(li,
298 libevdev_uinput_get_devnode(dev->uinput));
299 ck_assert(device != NULL);
300 litest_drain_events(li);
302 libinput_path_remove_device(device);
303 libinput_dispatch(li);
305 while ((event = libinput_get_event(li))) {
306 enum libinput_event_type type;
307 type = libinput_event_get_type(event);
309 if (type == LIBINPUT_EVENT_DEVICE_REMOVED)
312 libinput_event_destroy(event);
315 ck_assert_int_eq(remove_event, 1);
319 START_TEST(path_double_remove_device)
321 struct litest_device *dev = litest_current_device();
322 struct libinput *li = dev->libinput;
323 struct libinput_event *event;
324 struct libinput_device *device;
325 int remove_event = 0;
327 device = libinput_path_add_device(li,
328 libevdev_uinput_get_devnode(dev->uinput));
329 ck_assert(device != NULL);
330 litest_drain_events(li);
332 libinput_path_remove_device(device);
333 libinput_path_remove_device(device);
334 libinput_dispatch(li);
336 while ((event = libinput_get_event(li))) {
337 enum libinput_event_type type;
338 type = libinput_event_get_type(event);
340 if (type == LIBINPUT_EVENT_DEVICE_REMOVED)
343 libinput_event_destroy(event);
346 ck_assert_int_eq(remove_event, 1);
350 START_TEST(path_suspend)
353 struct libinput_device *device;
354 struct libevdev_uinput *uinput;
356 void *userdata = &rc;
358 uinput = litest_create_uinput_device("test device", NULL,
365 li = libinput_path_create_context(&simple_interface, userdata);
366 ck_assert(li != NULL);
368 device = libinput_path_add_device(li,
369 libevdev_uinput_get_devnode(uinput));
370 ck_assert(device != NULL);
372 libinput_suspend(li);
375 libevdev_uinput_destroy(uinput);
376 libinput_destroy(li);
379 close_func_count = 0;
383 START_TEST(path_double_suspend)
386 struct libinput_device *device;
387 struct libevdev_uinput *uinput;
389 void *userdata = &rc;
391 uinput = litest_create_uinput_device("test device", NULL,
398 li = libinput_path_create_context(&simple_interface, userdata);
399 ck_assert(li != NULL);
401 device = libinput_path_add_device(li,
402 libevdev_uinput_get_devnode(uinput));
403 ck_assert(device != NULL);
405 libinput_suspend(li);
406 libinput_suspend(li);
409 libevdev_uinput_destroy(uinput);
410 libinput_destroy(li);
413 close_func_count = 0;
417 START_TEST(path_double_resume)
420 struct libinput_device *device;
421 struct libevdev_uinput *uinput;
423 void *userdata = &rc;
425 uinput = litest_create_uinput_device("test device", NULL,
432 li = libinput_path_create_context(&simple_interface, userdata);
433 ck_assert(li != NULL);
435 device = libinput_path_add_device(li,
436 libevdev_uinput_get_devnode(uinput));
437 ck_assert(device != NULL);
439 libinput_suspend(li);
443 libevdev_uinput_destroy(uinput);
444 libinput_destroy(li);
447 close_func_count = 0;
451 START_TEST(path_add_device_suspend_resume)
454 struct libinput_device *device;
455 struct libinput_event *event;
456 struct libevdev_uinput *uinput1, *uinput2;
459 void *userdata = &rc;
461 uinput1 = litest_create_uinput_device("test device", NULL,
467 uinput2 = litest_create_uinput_device("test device 2", NULL,
474 li = libinput_path_create_context(&simple_interface, userdata);
475 ck_assert(li != NULL);
477 device = libinput_path_add_device(li,
478 libevdev_uinput_get_devnode(uinput1));
479 ck_assert(device != NULL);
480 device = libinput_path_add_device(li,
481 libevdev_uinput_get_devnode(uinput2));
483 libinput_dispatch(li);
486 while ((event = libinput_get_event(li))) {
487 enum libinput_event_type type;
488 type = libinput_event_get_type(event);
489 ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_ADDED);
490 libinput_event_destroy(event);
494 ck_assert_int_eq(nevents, 2);
497 libinput_suspend(li);
498 libinput_dispatch(li);
501 while ((event = libinput_get_event(li))) {
502 enum libinput_event_type type;
503 type = libinput_event_get_type(event);
504 ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_REMOVED);
505 libinput_event_destroy(event);
509 ck_assert_int_eq(nevents, 2);
512 libinput_dispatch(li);
515 while ((event = libinput_get_event(li))) {
516 enum libinput_event_type type;
517 type = libinput_event_get_type(event);
518 ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_ADDED);
519 libinput_event_destroy(event);
523 ck_assert_int_eq(nevents, 2);
525 libevdev_uinput_destroy(uinput1);
526 libevdev_uinput_destroy(uinput2);
527 libinput_destroy(li);
530 close_func_count = 0;
534 START_TEST(path_add_device_suspend_resume_fail)
537 struct libinput_device *device;
538 struct libinput_event *event;
539 struct libevdev_uinput *uinput1, *uinput2;
542 void *userdata = &rc;
544 uinput1 = litest_create_uinput_device("test device", NULL,
550 uinput2 = litest_create_uinput_device("test device 2", NULL,
557 li = libinput_path_create_context(&simple_interface, userdata);
558 ck_assert(li != NULL);
560 device = libinput_path_add_device(li,
561 libevdev_uinput_get_devnode(uinput1));
562 ck_assert(device != NULL);
563 device = libinput_path_add_device(li,
564 libevdev_uinput_get_devnode(uinput2));
565 ck_assert(device != NULL);
567 libinput_dispatch(li);
570 while ((event = libinput_get_event(li))) {
571 enum libinput_event_type type;
572 type = libinput_event_get_type(event);
573 ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_ADDED);
574 libinput_event_destroy(event);
578 ck_assert_int_eq(nevents, 2);
581 libinput_suspend(li);
582 libinput_dispatch(li);
585 while ((event = libinput_get_event(li))) {
586 enum libinput_event_type type;
587 type = libinput_event_get_type(event);
588 ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_REMOVED);
589 libinput_event_destroy(event);
593 ck_assert_int_eq(nevents, 2);
595 /* now drop one of the devices */
596 libevdev_uinput_destroy(uinput1);
597 rc = libinput_resume(li);
598 ck_assert_int_eq(rc, -1);
600 libinput_dispatch(li);
603 while ((event = libinput_get_event(li))) {
604 enum libinput_event_type type;
605 type = libinput_event_get_type(event);
606 /* We expect one device being added, second one fails,
607 * causing a removed event for the first one */
608 if (type != LIBINPUT_EVENT_DEVICE_ADDED &&
609 type != LIBINPUT_EVENT_DEVICE_REMOVED)
611 libinput_event_destroy(event);
615 ck_assert_int_eq(nevents, 2);
617 libevdev_uinput_destroy(uinput2);
618 libinput_destroy(li);
621 close_func_count = 0;
625 START_TEST(path_add_device_suspend_resume_remove_device)
628 struct libinput_device *device;
629 struct libinput_event *event;
630 struct libevdev_uinput *uinput1, *uinput2;
633 void *userdata = &rc;
635 uinput1 = litest_create_uinput_device("test device", NULL,
641 uinput2 = litest_create_uinput_device("test device 2", NULL,
648 li = libinput_path_create_context(&simple_interface, userdata);
649 ck_assert(li != NULL);
651 device = libinput_path_add_device(li,
652 libevdev_uinput_get_devnode(uinput1));
653 ck_assert(device != NULL);
654 device = libinput_path_add_device(li,
655 libevdev_uinput_get_devnode(uinput2));
657 libinput_device_ref(device);
658 libinput_dispatch(li);
661 while ((event = libinput_get_event(li))) {
662 enum libinput_event_type type;
663 type = libinput_event_get_type(event);
664 ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_ADDED);
665 libinput_event_destroy(event);
669 ck_assert_int_eq(nevents, 2);
672 libinput_suspend(li);
673 libinput_dispatch(li);
676 while ((event = libinput_get_event(li))) {
677 enum libinput_event_type type;
678 type = libinput_event_get_type(event);
679 ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_REMOVED);
680 libinput_event_destroy(event);
684 ck_assert_int_eq(nevents, 2);
686 /* now drop and remove one of the devices */
687 libevdev_uinput_destroy(uinput2);
688 libinput_path_remove_device(device);
689 libinput_device_unref(device);
691 rc = libinput_resume(li);
692 ck_assert_int_eq(rc, 0);
694 libinput_dispatch(li);
697 while ((event = libinput_get_event(li))) {
698 enum libinput_event_type type;
699 type = libinput_event_get_type(event);
700 ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_ADDED);
701 libinput_event_destroy(event);
705 ck_assert_int_eq(nevents, 1);
707 libevdev_uinput_destroy(uinput1);
708 libinput_destroy(li);
711 close_func_count = 0;
715 START_TEST(path_seat_recycle)
718 struct libevdev_uinput *uinput;
720 void *userdata = &rc;
721 struct libinput_event *ev;
722 struct libinput_device *device;
723 struct libinput_seat *saved_seat = NULL;
724 struct libinput_seat *seat;
729 uinput = litest_create_uinput_device("test device", NULL,
736 li = libinput_path_create_context(&simple_interface, userdata);
737 ck_assert(li != NULL);
739 device = libinput_path_add_device(li,
740 libevdev_uinput_get_devnode(uinput));
741 ck_assert(device != NULL);
743 libinput_dispatch(li);
744 while ((ev = libinput_get_event(li))) {
745 switch (libinput_event_get_type(ev)) {
746 case LIBINPUT_EVENT_DEVICE_ADDED:
750 device = libinput_event_get_device(ev);
751 ck_assert(device != NULL);
752 saved_seat = libinput_device_get_seat(device);
753 libinput_seat_set_user_data(saved_seat, &data);
754 libinput_seat_ref(saved_seat);
760 libinput_event_destroy(ev);
763 ck_assert(saved_seat != NULL);
765 libinput_suspend(li);
767 litest_drain_events(li);
771 libinput_dispatch(li);
772 while ((ev = libinput_get_event(li))) {
773 switch (libinput_event_get_type(ev)) {
774 case LIBINPUT_EVENT_DEVICE_ADDED:
775 device = libinput_event_get_device(ev);
776 ck_assert(device != NULL);
778 seat = libinput_device_get_seat(device);
779 user_data = libinput_seat_get_user_data(seat);
780 if (user_data == &data) {
782 ck_assert(seat == saved_seat);
789 libinput_event_destroy(ev);
792 ck_assert(found == 1);
794 libinput_destroy(li);
796 libevdev_uinput_destroy(uinput);
800 int main (int argc, char **argv) {
802 litest_add("path:create", path_create_NULL, LITEST_ANY, LITEST_ANY);
803 litest_add("path:create", path_create_invalid, LITEST_ANY, LITEST_ANY);
804 litest_add("path:create", path_create_destroy, LITEST_ANY, LITEST_ANY);
805 litest_add("path:suspend", path_suspend, LITEST_ANY, LITEST_ANY);
806 litest_add("path:suspend", path_double_suspend, LITEST_ANY, LITEST_ANY);
807 litest_add("path:suspend", path_double_resume, LITEST_ANY, LITEST_ANY);
808 litest_add("path:suspend", path_add_device_suspend_resume, LITEST_ANY, LITEST_ANY);
809 litest_add("path:suspend", path_add_device_suspend_resume_fail, LITEST_ANY, LITEST_ANY);
810 litest_add("path:suspend", path_add_device_suspend_resume_remove_device, LITEST_ANY, LITEST_ANY);
811 litest_add("path:seat events", path_added_seat, LITEST_ANY, LITEST_ANY);
812 litest_add("path:device events", path_added_device, LITEST_ANY, LITEST_ANY);
813 litest_add("path:device events", path_device_sysname, LITEST_ANY, LITEST_ANY);
814 litest_add("path:device events", path_add_device, LITEST_ANY, LITEST_ANY);
815 litest_add("path:device events", path_add_invalid_path, LITEST_ANY, LITEST_ANY);
816 litest_add("path:device events", path_remove_device, LITEST_ANY, LITEST_ANY);
817 litest_add("path:device events", path_double_remove_device, LITEST_ANY, LITEST_ANY);
818 litest_add("path:seat", path_seat_recycle,
819 LITEST_DISABLE_DEVICE, LITEST_DISABLE_DEVICE);
821 return litest_run(argc, argv);