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)
246 struct litest_device *dev = litest_current_device();
247 struct libinput *li = dev->libinput;
248 struct libinput_event *event;
249 struct libinput_device *device;
251 litest_drain_events(li);
253 device = libinput_path_add_device(li, "/tmp/");
254 ck_assert(device == NULL);
256 libinput_dispatch(li);
258 while ((event = libinput_get_event(li)))
263 START_TEST(path_device_sysname)
265 struct litest_device *dev = litest_current_device();
266 struct libinput_event *ev;
267 struct libinput_device *device;
270 libinput_dispatch(dev->libinput);
272 while ((ev = libinput_get_event(dev->libinput))) {
273 if (libinput_event_get_type(ev) != LIBINPUT_EVENT_DEVICE_ADDED)
276 device = libinput_event_get_device(ev);
277 sysname = libinput_device_get_sysname(device);
278 ck_assert(sysname != NULL && strlen(sysname) > 1);
279 ck_assert(strchr(sysname, '/') == NULL);
280 ck_assert_int_eq(strncmp(sysname, "event", 5), 0);
282 libinput_event_destroy(ev);
287 START_TEST(path_remove_device)
289 struct litest_device *dev = litest_current_device();
290 struct libinput *li = dev->libinput;
291 struct libinput_event *event;
292 struct libinput_device *device;
293 int remove_event = 0;
295 device = libinput_path_add_device(li,
296 libevdev_uinput_get_devnode(dev->uinput));
297 ck_assert(device != NULL);
298 litest_drain_events(li);
300 libinput_path_remove_device(device);
301 libinput_dispatch(li);
303 while ((event = libinput_get_event(li))) {
304 enum libinput_event_type type;
305 type = libinput_event_get_type(event);
307 if (type == LIBINPUT_EVENT_DEVICE_REMOVED)
310 libinput_event_destroy(event);
313 ck_assert_int_eq(remove_event, 1);
317 START_TEST(path_double_remove_device)
319 struct litest_device *dev = litest_current_device();
320 struct libinput *li = dev->libinput;
321 struct libinput_event *event;
322 struct libinput_device *device;
323 int remove_event = 0;
325 device = libinput_path_add_device(li,
326 libevdev_uinput_get_devnode(dev->uinput));
327 ck_assert(device != NULL);
328 litest_drain_events(li);
330 libinput_path_remove_device(device);
331 libinput_path_remove_device(device);
332 libinput_dispatch(li);
334 while ((event = libinput_get_event(li))) {
335 enum libinput_event_type type;
336 type = libinput_event_get_type(event);
338 if (type == LIBINPUT_EVENT_DEVICE_REMOVED)
341 libinput_event_destroy(event);
344 ck_assert_int_eq(remove_event, 1);
348 START_TEST(path_suspend)
351 struct libinput_device *device;
352 struct libevdev_uinput *uinput;
354 void *userdata = &rc;
356 uinput = litest_create_uinput_device("test device", NULL,
363 li = libinput_path_create_context(&simple_interface, userdata);
364 ck_assert(li != NULL);
366 device = libinput_path_add_device(li,
367 libevdev_uinput_get_devnode(uinput));
368 ck_assert(device != NULL);
370 libinput_suspend(li);
373 libevdev_uinput_destroy(uinput);
377 close_func_count = 0;
381 START_TEST(path_double_suspend)
384 struct libinput_device *device;
385 struct libevdev_uinput *uinput;
387 void *userdata = &rc;
389 uinput = litest_create_uinput_device("test device", NULL,
396 li = libinput_path_create_context(&simple_interface, userdata);
397 ck_assert(li != NULL);
399 device = libinput_path_add_device(li,
400 libevdev_uinput_get_devnode(uinput));
401 ck_assert(device != NULL);
403 libinput_suspend(li);
404 libinput_suspend(li);
407 libevdev_uinput_destroy(uinput);
411 close_func_count = 0;
415 START_TEST(path_double_resume)
418 struct libinput_device *device;
419 struct libevdev_uinput *uinput;
421 void *userdata = &rc;
423 uinput = litest_create_uinput_device("test device", NULL,
430 li = libinput_path_create_context(&simple_interface, userdata);
431 ck_assert(li != NULL);
433 device = libinput_path_add_device(li,
434 libevdev_uinput_get_devnode(uinput));
435 ck_assert(device != NULL);
437 libinput_suspend(li);
441 libevdev_uinput_destroy(uinput);
445 close_func_count = 0;
449 START_TEST(path_add_device_suspend_resume)
452 struct libinput_device *device;
453 struct libinput_event *event;
454 struct libevdev_uinput *uinput1, *uinput2;
457 void *userdata = &rc;
459 uinput1 = litest_create_uinput_device("test device", NULL,
465 uinput2 = litest_create_uinput_device("test device 2", NULL,
472 li = libinput_path_create_context(&simple_interface, userdata);
473 ck_assert(li != NULL);
475 device = libinput_path_add_device(li,
476 libevdev_uinput_get_devnode(uinput1));
477 ck_assert(device != NULL);
478 device = libinput_path_add_device(li,
479 libevdev_uinput_get_devnode(uinput2));
481 libinput_dispatch(li);
484 while ((event = libinput_get_event(li))) {
485 enum libinput_event_type type;
486 type = libinput_event_get_type(event);
487 ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_ADDED);
488 libinput_event_destroy(event);
492 ck_assert_int_eq(nevents, 2);
494 libinput_suspend(li);
495 libinput_dispatch(li);
498 while ((event = libinput_get_event(li))) {
499 enum libinput_event_type type;
500 type = libinput_event_get_type(event);
501 ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_REMOVED);
502 libinput_event_destroy(event);
506 ck_assert_int_eq(nevents, 2);
509 libinput_dispatch(li);
512 while ((event = libinput_get_event(li))) {
513 enum libinput_event_type type;
514 type = libinput_event_get_type(event);
515 ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_ADDED);
516 libinput_event_destroy(event);
520 ck_assert_int_eq(nevents, 2);
522 libevdev_uinput_destroy(uinput1);
523 libevdev_uinput_destroy(uinput2);
527 close_func_count = 0;
531 START_TEST(path_add_device_suspend_resume_fail)
534 struct libinput_device *device;
535 struct libinput_event *event;
536 struct libevdev_uinput *uinput1, *uinput2;
539 void *userdata = &rc;
541 uinput1 = litest_create_uinput_device("test device", NULL,
547 uinput2 = litest_create_uinput_device("test device 2", NULL,
554 li = libinput_path_create_context(&simple_interface, userdata);
555 ck_assert(li != NULL);
557 device = libinput_path_add_device(li,
558 libevdev_uinput_get_devnode(uinput1));
559 ck_assert(device != NULL);
560 device = libinput_path_add_device(li,
561 libevdev_uinput_get_devnode(uinput2));
562 ck_assert(device != NULL);
564 libinput_dispatch(li);
567 while ((event = libinput_get_event(li))) {
568 enum libinput_event_type type;
569 type = libinput_event_get_type(event);
570 ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_ADDED);
571 libinput_event_destroy(event);
575 ck_assert_int_eq(nevents, 2);
577 libinput_suspend(li);
578 libinput_dispatch(li);
581 while ((event = libinput_get_event(li))) {
582 enum libinput_event_type type;
583 type = libinput_event_get_type(event);
584 ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_REMOVED);
585 libinput_event_destroy(event);
589 ck_assert_int_eq(nevents, 2);
591 /* now drop one of the devices */
592 libevdev_uinput_destroy(uinput1);
593 rc = libinput_resume(li);
594 ck_assert_int_eq(rc, -1);
596 libinput_dispatch(li);
599 while ((event = libinput_get_event(li))) {
600 enum libinput_event_type type;
601 type = libinput_event_get_type(event);
602 /* We expect one device being added, second one fails,
603 * causing a removed event for the first one */
604 if (type != LIBINPUT_EVENT_DEVICE_ADDED &&
605 type != LIBINPUT_EVENT_DEVICE_REMOVED)
607 libinput_event_destroy(event);
611 ck_assert_int_eq(nevents, 2);
613 libevdev_uinput_destroy(uinput2);
617 close_func_count = 0;
621 START_TEST(path_add_device_suspend_resume_remove_device)
624 struct libinput_device *device;
625 struct libinput_event *event;
626 struct libevdev_uinput *uinput1, *uinput2;
629 void *userdata = &rc;
631 uinput1 = litest_create_uinput_device("test device", NULL,
637 uinput2 = litest_create_uinput_device("test device 2", NULL,
644 li = libinput_path_create_context(&simple_interface, userdata);
645 ck_assert(li != NULL);
647 device = libinput_path_add_device(li,
648 libevdev_uinput_get_devnode(uinput1));
649 ck_assert(device != NULL);
650 device = libinput_path_add_device(li,
651 libevdev_uinput_get_devnode(uinput2));
653 libinput_device_ref(device);
654 libinput_dispatch(li);
657 while ((event = libinput_get_event(li))) {
658 enum libinput_event_type type;
659 type = libinput_event_get_type(event);
660 ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_ADDED);
661 libinput_event_destroy(event);
665 ck_assert_int_eq(nevents, 2);
667 libinput_suspend(li);
668 libinput_dispatch(li);
671 while ((event = libinput_get_event(li))) {
672 enum libinput_event_type type;
673 type = libinput_event_get_type(event);
674 ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_REMOVED);
675 libinput_event_destroy(event);
679 ck_assert_int_eq(nevents, 2);
681 /* now drop and remove one of the devices */
682 libevdev_uinput_destroy(uinput2);
683 libinput_path_remove_device(device);
684 libinput_device_unref(device);
686 rc = libinput_resume(li);
687 ck_assert_int_eq(rc, 0);
689 libinput_dispatch(li);
692 while ((event = libinput_get_event(li))) {
693 enum libinput_event_type type;
694 type = libinput_event_get_type(event);
695 ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_ADDED);
696 libinput_event_destroy(event);
700 ck_assert_int_eq(nevents, 1);
702 libevdev_uinput_destroy(uinput1);
706 close_func_count = 0;
710 START_TEST(path_seat_recycle)
713 struct libevdev_uinput *uinput;
715 void *userdata = &rc;
716 struct libinput_event *ev;
717 struct libinput_device *device;
718 struct libinput_seat *saved_seat = NULL;
719 struct libinput_seat *seat;
724 uinput = litest_create_uinput_device("test device", NULL,
731 li = libinput_path_create_context(&simple_interface, userdata);
732 ck_assert(li != NULL);
734 device = libinput_path_add_device(li,
735 libevdev_uinput_get_devnode(uinput));
736 ck_assert(device != NULL);
738 libinput_dispatch(li);
739 while ((ev = libinput_get_event(li))) {
740 switch (libinput_event_get_type(ev)) {
741 case LIBINPUT_EVENT_DEVICE_ADDED:
745 device = libinput_event_get_device(ev);
746 ck_assert(device != NULL);
747 saved_seat = libinput_device_get_seat(device);
748 libinput_seat_set_user_data(saved_seat, &data);
749 libinput_seat_ref(saved_seat);
755 libinput_event_destroy(ev);
758 ck_assert(saved_seat != NULL);
760 libinput_suspend(li);
762 litest_drain_events(li);
766 libinput_dispatch(li);
767 while ((ev = libinput_get_event(li))) {
768 switch (libinput_event_get_type(ev)) {
769 case LIBINPUT_EVENT_DEVICE_ADDED:
770 device = libinput_event_get_device(ev);
771 ck_assert(device != NULL);
773 seat = libinput_device_get_seat(device);
774 user_data = libinput_seat_get_user_data(seat);
775 if (user_data == &data) {
777 ck_assert(seat == saved_seat);
784 libinput_event_destroy(ev);
787 ck_assert(found == 1);
791 libevdev_uinput_destroy(uinput);
795 int main (int argc, char **argv) {
797 litest_add("path:create", path_create_NULL, LITEST_ANY, LITEST_ANY);
798 litest_add("path:create", path_create_invalid, LITEST_ANY, LITEST_ANY);
799 litest_add("path:create", path_create_destroy, LITEST_ANY, LITEST_ANY);
800 litest_add("path:suspend", path_suspend, LITEST_ANY, LITEST_ANY);
801 litest_add("path:suspend", path_double_suspend, LITEST_ANY, LITEST_ANY);
802 litest_add("path:suspend", path_double_resume, LITEST_ANY, LITEST_ANY);
803 litest_add("path:suspend", path_add_device_suspend_resume, LITEST_ANY, LITEST_ANY);
804 litest_add("path:suspend", path_add_device_suspend_resume_fail, LITEST_ANY, LITEST_ANY);
805 litest_add("path:suspend", path_add_device_suspend_resume_remove_device, LITEST_ANY, LITEST_ANY);
806 litest_add("path:seat events", path_added_seat, LITEST_ANY, LITEST_ANY);
807 litest_add("path:device events", path_added_device, LITEST_ANY, LITEST_ANY);
808 litest_add("path:device events", path_device_sysname, LITEST_ANY, LITEST_ANY);
809 litest_add("path:device events", path_add_device, LITEST_ANY, LITEST_ANY);
810 litest_add("path:device events", path_add_invalid_path, LITEST_ANY, LITEST_ANY);
811 litest_add("path:device events", path_remove_device, LITEST_ANY, LITEST_ANY);
812 litest_add("path:device events", path_double_remove_device, LITEST_ANY, LITEST_ANY);
813 litest_add("path:seat", path_seat_recycle,
814 LITEST_DISABLE_DEVICE, LITEST_DISABLE_DEVICE);
816 return litest_run(argc, argv);