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,
56 START_TEST(path_create_NULL)
63 li = libinput_path_create_context(NULL, NULL);
64 ck_assert(li == NULL);
65 li = libinput_path_create_context(&simple_interface, NULL);
66 ck_assert(li != NULL);
69 ck_assert_int_eq(open_func_count, 0);
70 ck_assert_int_eq(close_func_count, 0);
77 START_TEST(path_create_invalid)
80 struct libinput_device *device;
81 const char *path = "/tmp";
86 li = libinput_path_create_context(&simple_interface, NULL);
87 ck_assert(li != NULL);
88 device = libinput_path_add_device(li, path);
89 ck_assert(device == NULL);
91 ck_assert_int_eq(open_func_count, 0);
92 ck_assert_int_eq(close_func_count, 0);
95 ck_assert_int_eq(close_func_count, 0);
102 START_TEST(path_create_destroy)
105 struct libinput_device *device;
106 struct libevdev_uinput *uinput;
108 void *userdata = &rc;
110 uinput = litest_create_uinput_device("test device", NULL,
117 li = libinput_path_create_context(&simple_interface, userdata);
118 ck_assert(li != NULL);
119 ck_assert(libinput_get_user_data(li) == userdata);
121 device = libinput_path_add_device(li,
122 libevdev_uinput_get_devnode(uinput));
123 ck_assert(device != NULL);
125 ck_assert_int_eq(open_func_count, 1);
127 libevdev_uinput_destroy(uinput);
129 ck_assert_int_eq(close_func_count, 1);
132 close_func_count = 0;
136 START_TEST(path_added_seat)
138 struct litest_device *dev = litest_current_device();
139 struct libinput *li = dev->libinput;
140 struct libinput_event *event;
141 struct libinput_device *device;
142 struct libinput_seat *seat;
143 const char *seat_name;
144 enum libinput_event_type type;
146 libinput_dispatch(li);
148 event = libinput_get_event(li);
149 ck_assert(event != NULL);
151 type = libinput_event_get_type(event);
152 ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_ADDED);
154 device = libinput_event_get_device(event);
155 seat = libinput_device_get_seat(device);
156 ck_assert(seat != NULL);
158 seat_name = libinput_seat_get_logical_name(seat);
159 ck_assert_str_eq(seat_name, "default");
161 libinput_event_destroy(event);
165 START_TEST(path_added_device)
167 struct litest_device *dev = litest_current_device();
168 struct libinput *li = dev->libinput;
169 struct libinput_event *event;
170 struct libinput_device *device;
172 libinput_dispatch(li);
174 while ((event = libinput_get_event(li))) {
175 enum libinput_event_type type;
176 type = libinput_event_get_type(event);
178 if (type == LIBINPUT_EVENT_DEVICE_ADDED) {
182 libinput_event_destroy(event);
185 ck_assert(event != NULL);
187 device = libinput_event_get_device(event);
188 ck_assert(device != NULL);
190 libinput_event_destroy(event);
194 START_TEST(path_add_device)
196 struct litest_device *dev = litest_current_device();
197 struct libinput *li = dev->libinput;
198 struct libinput_event *event;
199 struct libinput_device *device;
200 const char *sysname1 = NULL, *sysname2 = NULL;
202 libinput_dispatch(li);
204 while ((event = libinput_get_event(li))) {
205 enum libinput_event_type type;
206 type = libinput_event_get_type(event);
208 if (type == LIBINPUT_EVENT_DEVICE_ADDED) {
209 ck_assert(sysname1 == NULL);
210 device = libinput_event_get_device(event);
211 ck_assert(device != NULL);
212 sysname1 = libinput_device_get_sysname(device);
215 libinput_event_destroy(event);
218 device = libinput_path_add_device(li,
219 libevdev_uinput_get_devnode(dev->uinput));
220 ck_assert(device != NULL);
222 libinput_dispatch(li);
224 while ((event = libinput_get_event(li))) {
225 enum libinput_event_type type;
226 type = libinput_event_get_type(event);
228 if (type == LIBINPUT_EVENT_DEVICE_ADDED) {
229 ck_assert(sysname2 == NULL);
230 device = libinput_event_get_device(event);
231 ck_assert(device != NULL);
232 sysname2 = libinput_device_get_sysname(device);
235 libinput_event_destroy(event);
238 ck_assert_str_eq(sysname1, sysname2);
240 libinput_event_destroy(event);
244 START_TEST(path_add_invalid_path)
247 struct libinput_event *event;
248 struct libinput_device *device;
250 li = litest_create_context();
252 device = libinput_path_add_device(li, "/tmp/");
253 ck_assert(device == NULL);
255 libinput_dispatch(li);
257 while ((event = libinput_get_event(li)))
264 START_TEST(path_device_sysname)
266 struct litest_device *dev = litest_current_device();
267 struct libinput_event *ev;
268 struct libinput_device *device;
271 libinput_dispatch(dev->libinput);
273 while ((ev = libinput_get_event(dev->libinput))) {
274 if (libinput_event_get_type(ev) != LIBINPUT_EVENT_DEVICE_ADDED)
277 device = libinput_event_get_device(ev);
278 sysname = libinput_device_get_sysname(device);
279 ck_assert(sysname != NULL && strlen(sysname) > 1);
280 ck_assert(strchr(sysname, '/') == NULL);
281 ck_assert_int_eq(strncmp(sysname, "event", 5), 0);
283 libinput_event_destroy(ev);
288 START_TEST(path_remove_device)
290 struct litest_device *dev = litest_current_device();
291 struct libinput *li = dev->libinput;
292 struct libinput_event *event;
293 struct libinput_device *device;
294 int remove_event = 0;
296 device = libinput_path_add_device(li,
297 libevdev_uinput_get_devnode(dev->uinput));
298 ck_assert(device != NULL);
299 litest_drain_events(li);
301 libinput_path_remove_device(device);
302 libinput_dispatch(li);
304 while ((event = libinput_get_event(li))) {
305 enum libinput_event_type type;
306 type = libinput_event_get_type(event);
308 if (type == LIBINPUT_EVENT_DEVICE_REMOVED)
311 libinput_event_destroy(event);
314 ck_assert_int_eq(remove_event, 1);
318 START_TEST(path_double_remove_device)
320 struct litest_device *dev = litest_current_device();
321 struct libinput *li = dev->libinput;
322 struct libinput_event *event;
323 struct libinput_device *device;
324 int remove_event = 0;
326 device = libinput_path_add_device(li,
327 libevdev_uinput_get_devnode(dev->uinput));
328 ck_assert(device != NULL);
329 litest_drain_events(li);
331 libinput_path_remove_device(device);
332 libinput_path_remove_device(device);
333 libinput_dispatch(li);
335 while ((event = libinput_get_event(li))) {
336 enum libinput_event_type type;
337 type = libinput_event_get_type(event);
339 if (type == LIBINPUT_EVENT_DEVICE_REMOVED)
342 libinput_event_destroy(event);
345 ck_assert_int_eq(remove_event, 1);
349 START_TEST(path_suspend)
352 struct libinput_device *device;
353 struct libevdev_uinput *uinput;
355 void *userdata = &rc;
357 uinput = litest_create_uinput_device("test device", NULL,
364 li = libinput_path_create_context(&simple_interface, userdata);
365 ck_assert(li != NULL);
367 device = libinput_path_add_device(li,
368 libevdev_uinput_get_devnode(uinput));
369 ck_assert(device != NULL);
371 libinput_suspend(li);
374 libevdev_uinput_destroy(uinput);
378 close_func_count = 0;
382 START_TEST(path_double_suspend)
385 struct libinput_device *device;
386 struct libevdev_uinput *uinput;
388 void *userdata = &rc;
390 uinput = litest_create_uinput_device("test device", NULL,
397 li = libinput_path_create_context(&simple_interface, userdata);
398 ck_assert(li != NULL);
400 device = libinput_path_add_device(li,
401 libevdev_uinput_get_devnode(uinput));
402 ck_assert(device != NULL);
404 libinput_suspend(li);
405 libinput_suspend(li);
408 libevdev_uinput_destroy(uinput);
412 close_func_count = 0;
416 START_TEST(path_double_resume)
419 struct libinput_device *device;
420 struct libevdev_uinput *uinput;
422 void *userdata = &rc;
424 uinput = litest_create_uinput_device("test device", NULL,
431 li = libinput_path_create_context(&simple_interface, userdata);
432 ck_assert(li != NULL);
434 device = libinput_path_add_device(li,
435 libevdev_uinput_get_devnode(uinput));
436 ck_assert(device != NULL);
438 libinput_suspend(li);
442 libevdev_uinput_destroy(uinput);
446 close_func_count = 0;
450 START_TEST(path_add_device_suspend_resume)
453 struct libinput_device *device;
454 struct libinput_event *event;
455 struct libevdev_uinput *uinput1, *uinput2;
458 void *userdata = &rc;
460 uinput1 = litest_create_uinput_device("test device", NULL,
466 uinput2 = litest_create_uinput_device("test device 2", NULL,
473 li = libinput_path_create_context(&simple_interface, userdata);
474 ck_assert(li != NULL);
476 device = libinput_path_add_device(li,
477 libevdev_uinput_get_devnode(uinput1));
478 ck_assert(device != NULL);
479 device = libinput_path_add_device(li,
480 libevdev_uinput_get_devnode(uinput2));
482 libinput_dispatch(li);
485 while ((event = libinput_get_event(li))) {
486 enum libinput_event_type type;
487 type = libinput_event_get_type(event);
488 ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_ADDED);
489 libinput_event_destroy(event);
493 ck_assert_int_eq(nevents, 2);
495 libinput_suspend(li);
496 libinput_dispatch(li);
499 while ((event = libinput_get_event(li))) {
500 enum libinput_event_type type;
501 type = libinput_event_get_type(event);
502 ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_REMOVED);
503 libinput_event_destroy(event);
507 ck_assert_int_eq(nevents, 2);
510 libinput_dispatch(li);
513 while ((event = libinput_get_event(li))) {
514 enum libinput_event_type type;
515 type = libinput_event_get_type(event);
516 ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_ADDED);
517 libinput_event_destroy(event);
521 ck_assert_int_eq(nevents, 2);
523 libevdev_uinput_destroy(uinput1);
524 libevdev_uinput_destroy(uinput2);
528 close_func_count = 0;
532 START_TEST(path_add_device_suspend_resume_fail)
535 struct libinput_device *device;
536 struct libinput_event *event;
537 struct libevdev_uinput *uinput1, *uinput2;
540 void *userdata = &rc;
542 uinput1 = litest_create_uinput_device("test device", NULL,
548 uinput2 = litest_create_uinput_device("test device 2", NULL,
555 li = libinput_path_create_context(&simple_interface, userdata);
556 ck_assert(li != NULL);
558 device = libinput_path_add_device(li,
559 libevdev_uinput_get_devnode(uinput1));
560 ck_assert(device != NULL);
561 device = libinput_path_add_device(li,
562 libevdev_uinput_get_devnode(uinput2));
563 ck_assert(device != NULL);
565 libinput_dispatch(li);
568 while ((event = libinput_get_event(li))) {
569 enum libinput_event_type type;
570 type = libinput_event_get_type(event);
571 ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_ADDED);
572 libinput_event_destroy(event);
576 ck_assert_int_eq(nevents, 2);
578 libinput_suspend(li);
579 libinput_dispatch(li);
582 while ((event = libinput_get_event(li))) {
583 enum libinput_event_type type;
584 type = libinput_event_get_type(event);
585 ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_REMOVED);
586 libinput_event_destroy(event);
590 ck_assert_int_eq(nevents, 2);
592 /* now drop one of the devices */
593 libevdev_uinput_destroy(uinput1);
594 rc = libinput_resume(li);
595 ck_assert_int_eq(rc, -1);
597 libinput_dispatch(li);
600 while ((event = libinput_get_event(li))) {
601 enum libinput_event_type type;
602 type = libinput_event_get_type(event);
603 /* We expect one device being added, second one fails,
604 * causing a removed event for the first one */
605 if (type != LIBINPUT_EVENT_DEVICE_ADDED &&
606 type != LIBINPUT_EVENT_DEVICE_REMOVED)
608 libinput_event_destroy(event);
612 ck_assert_int_eq(nevents, 2);
614 libevdev_uinput_destroy(uinput2);
618 close_func_count = 0;
622 START_TEST(path_add_device_suspend_resume_remove_device)
625 struct libinput_device *device;
626 struct libinput_event *event;
627 struct libevdev_uinput *uinput1, *uinput2;
630 void *userdata = &rc;
632 uinput1 = litest_create_uinput_device("test device", NULL,
638 uinput2 = litest_create_uinput_device("test device 2", NULL,
645 li = libinput_path_create_context(&simple_interface, userdata);
646 ck_assert(li != NULL);
648 device = libinput_path_add_device(li,
649 libevdev_uinput_get_devnode(uinput1));
650 ck_assert(device != NULL);
651 device = libinput_path_add_device(li,
652 libevdev_uinput_get_devnode(uinput2));
654 libinput_device_ref(device);
655 libinput_dispatch(li);
658 while ((event = libinput_get_event(li))) {
659 enum libinput_event_type type;
660 type = libinput_event_get_type(event);
661 ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_ADDED);
662 libinput_event_destroy(event);
666 ck_assert_int_eq(nevents, 2);
668 libinput_suspend(li);
669 libinput_dispatch(li);
672 while ((event = libinput_get_event(li))) {
673 enum libinput_event_type type;
674 type = libinput_event_get_type(event);
675 ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_REMOVED);
676 libinput_event_destroy(event);
680 ck_assert_int_eq(nevents, 2);
682 /* now drop and remove one of the devices */
683 libevdev_uinput_destroy(uinput2);
684 libinput_path_remove_device(device);
685 libinput_device_unref(device);
687 rc = libinput_resume(li);
688 ck_assert_int_eq(rc, 0);
690 libinput_dispatch(li);
693 while ((event = libinput_get_event(li))) {
694 enum libinput_event_type type;
695 type = libinput_event_get_type(event);
696 ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_ADDED);
697 libinput_event_destroy(event);
701 ck_assert_int_eq(nevents, 1);
703 libevdev_uinput_destroy(uinput1);
707 close_func_count = 0;
711 START_TEST(path_seat_recycle)
714 struct libevdev_uinput *uinput;
716 void *userdata = &rc;
717 struct libinput_event *ev;
718 struct libinput_device *device;
719 struct libinput_seat *saved_seat = NULL;
720 struct libinput_seat *seat;
725 uinput = litest_create_uinput_device("test device", NULL,
732 li = libinput_path_create_context(&simple_interface, userdata);
733 ck_assert(li != NULL);
735 device = libinput_path_add_device(li,
736 libevdev_uinput_get_devnode(uinput));
737 ck_assert(device != NULL);
739 libinput_dispatch(li);
740 while ((ev = libinput_get_event(li))) {
741 switch (libinput_event_get_type(ev)) {
742 case LIBINPUT_EVENT_DEVICE_ADDED:
746 device = libinput_event_get_device(ev);
747 ck_assert(device != NULL);
748 saved_seat = libinput_device_get_seat(device);
749 libinput_seat_set_user_data(saved_seat, &data);
750 libinput_seat_ref(saved_seat);
756 libinput_event_destroy(ev);
759 ck_assert(saved_seat != NULL);
761 libinput_suspend(li);
763 litest_drain_events(li);
767 libinput_dispatch(li);
768 while ((ev = libinput_get_event(li))) {
769 switch (libinput_event_get_type(ev)) {
770 case LIBINPUT_EVENT_DEVICE_ADDED:
771 device = libinput_event_get_device(ev);
772 ck_assert(device != NULL);
774 seat = libinput_device_get_seat(device);
775 user_data = libinput_seat_get_user_data(seat);
776 if (user_data == &data) {
778 ck_assert(seat == saved_seat);
785 libinput_event_destroy(ev);
788 ck_assert(found == 1);
792 libevdev_uinput_destroy(uinput);
797 main(int argc, char **argv)
799 litest_add_no_device("path:create", path_create_NULL);
800 litest_add_no_device("path:create", path_create_invalid);
801 litest_add_no_device("path:create", path_create_destroy);
802 litest_add_no_device("path:suspend", path_suspend);
803 litest_add_no_device("path:suspend", path_double_suspend);
804 litest_add_no_device("path:suspend", path_double_resume);
805 litest_add_no_device("path:suspend", path_add_device_suspend_resume);
806 litest_add_no_device("path:suspend", path_add_device_suspend_resume_fail);
807 litest_add_no_device("path:suspend", path_add_device_suspend_resume_remove_device);
808 litest_add_for_device("path:seat events", path_added_seat, LITEST_SYNAPTICS_CLICKPAD);
809 litest_add("path:device events", path_added_device, LITEST_ANY, LITEST_ANY);
810 litest_add("path:device events", path_device_sysname, LITEST_ANY, LITEST_ANY);
811 litest_add_for_device("path:device events", path_add_device, LITEST_SYNAPTICS_CLICKPAD);
812 litest_add_no_device("path:device events", path_add_invalid_path);
813 litest_add_for_device("path:device events", path_remove_device, LITEST_SYNAPTICS_CLICKPAD);
814 litest_add_for_device("path:device events", path_double_remove_device, LITEST_SYNAPTICS_CLICKPAD);
815 litest_add_no_device("path:seat", path_seat_recycle);
817 return litest_run(argc, argv);