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)
64 li = libinput_path_create_context(NULL, NULL);
65 ck_assert(li == NULL);
66 li = libinput_path_create_context(&simple_interface, NULL);
67 ck_assert(li != NULL);
70 ck_assert_int_eq(open_func_count, 0);
71 ck_assert_int_eq(close_func_count, 0);
78 START_TEST(path_create_invalid)
81 struct libinput_device *device;
82 const char *path = "/tmp";
87 li = libinput_path_create_context(&simple_interface, NULL);
88 ck_assert(li != NULL);
89 device = libinput_path_add_device(li, path);
90 ck_assert(device == NULL);
92 ck_assert_int_eq(open_func_count, 0);
93 ck_assert_int_eq(close_func_count, 0);
96 ck_assert_int_eq(close_func_count, 0);
103 START_TEST(path_create_destroy)
106 struct libinput_device *device;
107 struct libevdev_uinput *uinput;
109 void *userdata = &rc;
111 uinput = litest_create_uinput_device("test device", NULL,
118 li = libinput_path_create_context(&simple_interface, userdata);
119 ck_assert(li != NULL);
120 ck_assert(libinput_get_user_data(li) == userdata);
122 device = libinput_path_add_device(li,
123 libevdev_uinput_get_devnode(uinput));
124 ck_assert(device != NULL);
126 ck_assert_int_eq(open_func_count, 1);
128 libevdev_uinput_destroy(uinput);
130 ck_assert_int_eq(close_func_count, 1);
133 close_func_count = 0;
137 START_TEST(path_added_seat)
139 struct litest_device *dev = litest_current_device();
140 struct libinput *li = dev->libinput;
141 struct libinput_event *event;
142 struct libinput_device *device;
143 struct libinput_seat *seat;
144 const char *seat_name;
145 enum libinput_event_type type;
147 libinput_dispatch(li);
149 event = libinput_get_event(li);
150 ck_assert(event != NULL);
152 type = libinput_event_get_type(event);
153 ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_ADDED);
155 device = libinput_event_get_device(event);
156 seat = libinput_device_get_seat(device);
157 ck_assert(seat != NULL);
159 seat_name = libinput_seat_get_logical_name(seat);
160 ck_assert_str_eq(seat_name, "default");
162 libinput_event_destroy(event);
166 START_TEST(path_added_device)
168 struct litest_device *dev = litest_current_device();
169 struct libinput *li = dev->libinput;
170 struct libinput_event *event;
171 struct libinput_device *device;
173 libinput_dispatch(li);
175 while ((event = libinput_get_event(li))) {
176 enum libinput_event_type type;
177 type = libinput_event_get_type(event);
179 if (type == LIBINPUT_EVENT_DEVICE_ADDED) {
183 libinput_event_destroy(event);
186 ck_assert(event != NULL);
188 device = libinput_event_get_device(event);
189 ck_assert(device != NULL);
191 libinput_event_destroy(event);
195 START_TEST(path_add_device)
197 struct litest_device *dev = litest_current_device();
198 struct libinput *li = dev->libinput;
199 struct libinput_event *event;
200 struct libinput_device *device;
201 const char *sysname1 = NULL, *sysname2 = NULL;
203 libinput_dispatch(li);
205 while ((event = libinput_get_event(li))) {
206 enum libinput_event_type type;
207 type = libinput_event_get_type(event);
209 if (type == LIBINPUT_EVENT_DEVICE_ADDED) {
210 ck_assert(sysname1 == NULL);
211 device = libinput_event_get_device(event);
212 ck_assert(device != NULL);
213 sysname1 = libinput_device_get_sysname(device);
216 libinput_event_destroy(event);
219 device = libinput_path_add_device(li,
220 libevdev_uinput_get_devnode(dev->uinput));
221 ck_assert(device != NULL);
223 libinput_dispatch(li);
225 while ((event = libinput_get_event(li))) {
226 enum libinput_event_type type;
227 type = libinput_event_get_type(event);
229 if (type == LIBINPUT_EVENT_DEVICE_ADDED) {
230 ck_assert(sysname2 == NULL);
231 device = libinput_event_get_device(event);
232 ck_assert(device != NULL);
233 sysname2 = libinput_device_get_sysname(device);
236 libinput_event_destroy(event);
239 ck_assert_str_eq(sysname1, sysname2);
241 libinput_event_destroy(event);
245 START_TEST(path_add_invalid_path)
247 struct litest_device *dev = litest_current_device();
248 struct libinput *li = dev->libinput;
249 struct libinput_event *event;
250 struct libinput_device *device;
252 litest_drain_events(li);
254 device = libinput_path_add_device(li, "/tmp/");
255 ck_assert(device == NULL);
257 libinput_dispatch(li);
259 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);
496 libinput_suspend(li);
497 libinput_dispatch(li);
500 while ((event = libinput_get_event(li))) {
501 enum libinput_event_type type;
502 type = libinput_event_get_type(event);
503 ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_REMOVED);
504 libinput_event_destroy(event);
508 ck_assert_int_eq(nevents, 2);
511 libinput_dispatch(li);
514 while ((event = libinput_get_event(li))) {
515 enum libinput_event_type type;
516 type = libinput_event_get_type(event);
517 ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_ADDED);
518 libinput_event_destroy(event);
522 ck_assert_int_eq(nevents, 2);
524 libevdev_uinput_destroy(uinput1);
525 libevdev_uinput_destroy(uinput2);
529 close_func_count = 0;
533 START_TEST(path_add_device_suspend_resume_fail)
536 struct libinput_device *device;
537 struct libinput_event *event;
538 struct libevdev_uinput *uinput1, *uinput2;
541 void *userdata = &rc;
543 uinput1 = litest_create_uinput_device("test device", NULL,
549 uinput2 = litest_create_uinput_device("test device 2", NULL,
556 li = libinput_path_create_context(&simple_interface, userdata);
557 ck_assert(li != NULL);
559 device = libinput_path_add_device(li,
560 libevdev_uinput_get_devnode(uinput1));
561 ck_assert(device != NULL);
562 device = libinput_path_add_device(li,
563 libevdev_uinput_get_devnode(uinput2));
564 ck_assert(device != NULL);
566 libinput_dispatch(li);
569 while ((event = libinput_get_event(li))) {
570 enum libinput_event_type type;
571 type = libinput_event_get_type(event);
572 ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_ADDED);
573 libinput_event_destroy(event);
577 ck_assert_int_eq(nevents, 2);
580 libinput_suspend(li);
581 libinput_dispatch(li);
584 while ((event = libinput_get_event(li))) {
585 enum libinput_event_type type;
586 type = libinput_event_get_type(event);
587 ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_REMOVED);
588 libinput_event_destroy(event);
592 ck_assert_int_eq(nevents, 2);
594 /* now drop one of the devices */
595 libevdev_uinput_destroy(uinput1);
596 rc = libinput_resume(li);
597 ck_assert_int_eq(rc, -1);
599 libinput_dispatch(li);
602 while ((event = libinput_get_event(li))) {
603 enum libinput_event_type type;
604 type = libinput_event_get_type(event);
605 /* We expect one device being added, second one fails,
606 * causing a removed event for the first one */
607 if (type != LIBINPUT_EVENT_DEVICE_ADDED &&
608 type != LIBINPUT_EVENT_DEVICE_REMOVED)
610 libinput_event_destroy(event);
614 ck_assert_int_eq(nevents, 2);
616 libevdev_uinput_destroy(uinput2);
620 close_func_count = 0;
624 START_TEST(path_add_device_suspend_resume_remove_device)
627 struct libinput_device *device;
628 struct libinput_event *event;
629 struct libevdev_uinput *uinput1, *uinput2;
632 void *userdata = &rc;
634 uinput1 = litest_create_uinput_device("test device", NULL,
640 uinput2 = litest_create_uinput_device("test device 2", NULL,
647 li = libinput_path_create_context(&simple_interface, userdata);
648 ck_assert(li != NULL);
650 device = libinput_path_add_device(li,
651 libevdev_uinput_get_devnode(uinput1));
652 ck_assert(device != NULL);
653 device = libinput_path_add_device(li,
654 libevdev_uinput_get_devnode(uinput2));
656 libinput_device_ref(device);
657 libinput_dispatch(li);
660 while ((event = libinput_get_event(li))) {
661 enum libinput_event_type type;
662 type = libinput_event_get_type(event);
663 ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_ADDED);
664 libinput_event_destroy(event);
668 ck_assert_int_eq(nevents, 2);
671 libinput_suspend(li);
672 libinput_dispatch(li);
675 while ((event = libinput_get_event(li))) {
676 enum libinput_event_type type;
677 type = libinput_event_get_type(event);
678 ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_REMOVED);
679 libinput_event_destroy(event);
683 ck_assert_int_eq(nevents, 2);
685 /* now drop and remove one of the devices */
686 libevdev_uinput_destroy(uinput2);
687 libinput_path_remove_device(device);
688 libinput_device_unref(device);
690 rc = libinput_resume(li);
691 ck_assert_int_eq(rc, 0);
693 libinput_dispatch(li);
696 while ((event = libinput_get_event(li))) {
697 enum libinput_event_type type;
698 type = libinput_event_get_type(event);
699 ck_assert_int_eq(type, LIBINPUT_EVENT_DEVICE_ADDED);
700 libinput_event_destroy(event);
704 ck_assert_int_eq(nevents, 1);
706 libevdev_uinput_destroy(uinput1);
710 close_func_count = 0;
714 START_TEST(path_seat_recycle)
717 struct libevdev_uinput *uinput;
719 void *userdata = &rc;
720 struct libinput_event *ev;
721 struct libinput_device *device;
722 struct libinput_seat *saved_seat = NULL;
723 struct libinput_seat *seat;
728 uinput = litest_create_uinput_device("test device", NULL,
735 li = libinput_path_create_context(&simple_interface, userdata);
736 ck_assert(li != NULL);
738 device = libinput_path_add_device(li,
739 libevdev_uinput_get_devnode(uinput));
740 ck_assert(device != NULL);
742 libinput_dispatch(li);
743 while ((ev = libinput_get_event(li))) {
744 switch (libinput_event_get_type(ev)) {
745 case LIBINPUT_EVENT_DEVICE_ADDED:
749 device = libinput_event_get_device(ev);
750 ck_assert(device != NULL);
751 saved_seat = libinput_device_get_seat(device);
752 libinput_seat_set_user_data(saved_seat, &data);
753 libinput_seat_ref(saved_seat);
759 libinput_event_destroy(ev);
762 ck_assert(saved_seat != NULL);
764 libinput_suspend(li);
766 litest_drain_events(li);
770 libinput_dispatch(li);
771 while ((ev = libinput_get_event(li))) {
772 switch (libinput_event_get_type(ev)) {
773 case LIBINPUT_EVENT_DEVICE_ADDED:
774 device = libinput_event_get_device(ev);
775 ck_assert(device != NULL);
777 seat = libinput_device_get_seat(device);
778 user_data = libinput_seat_get_user_data(seat);
779 if (user_data == &data) {
781 ck_assert(seat == saved_seat);
788 libinput_event_destroy(ev);
791 ck_assert(found == 1);
795 libevdev_uinput_destroy(uinput);
799 int main (int argc, char **argv) {
801 litest_add("path:create", path_create_NULL, LITEST_ANY, LITEST_ANY);
802 litest_add("path:create", path_create_invalid, LITEST_ANY, LITEST_ANY);
803 litest_add("path:create", path_create_destroy, LITEST_ANY, LITEST_ANY);
804 litest_add("path:suspend", path_suspend, LITEST_ANY, LITEST_ANY);
805 litest_add("path:suspend", path_double_suspend, LITEST_ANY, LITEST_ANY);
806 litest_add("path:suspend", path_double_resume, LITEST_ANY, LITEST_ANY);
807 litest_add("path:suspend", path_add_device_suspend_resume, LITEST_ANY, LITEST_ANY);
808 litest_add("path:suspend", path_add_device_suspend_resume_fail, LITEST_ANY, LITEST_ANY);
809 litest_add("path:suspend", path_add_device_suspend_resume_remove_device, LITEST_ANY, LITEST_ANY);
810 litest_add("path:seat events", path_added_seat, LITEST_ANY, LITEST_ANY);
811 litest_add("path:device events", path_added_device, LITEST_ANY, LITEST_ANY);
812 litest_add("path:device events", path_device_sysname, LITEST_ANY, LITEST_ANY);
813 litest_add("path:device events", path_add_device, LITEST_ANY, LITEST_ANY);
814 litest_add("path:device events", path_add_invalid_path, LITEST_ANY, LITEST_ANY);
815 litest_add("path:device events", path_remove_device, LITEST_ANY, LITEST_ANY);
816 litest_add("path:device events", path_double_remove_device, LITEST_ANY, LITEST_ANY);
817 litest_add("path:seat", path_seat_recycle,
818 LITEST_DISABLE_DEVICE, LITEST_DISABLE_DEVICE);
820 return litest_run(argc, argv);