2 * Copyright © 2013 Red Hat, Inc.
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
34 static int open_restricted(const char *path, int flags, void *data)
37 fd = open(path, flags);
38 return fd < 0 ? -errno : fd;
40 static void close_restricted(int fd, void *data)
45 const struct libinput_interface simple_interface = {
46 .open_restricted = open_restricted,
47 .close_restricted = close_restricted,
51 START_TEST(udev_create_NULL)
54 const struct libinput_interface interface;
55 struct udev *udev = (struct udev*)0xdeadbeef;
56 const char *seat = (const char*)0xdeaddead;
58 li = libinput_udev_create_for_seat(NULL, NULL, NULL, NULL);
59 ck_assert(li == NULL);
61 li = libinput_udev_create_for_seat(&interface, NULL, NULL, NULL);
62 ck_assert(li == NULL);
63 li = libinput_udev_create_for_seat(NULL, NULL, udev, NULL);
64 ck_assert(li == NULL);
65 li = libinput_udev_create_for_seat(NULL, NULL, NULL, seat);
66 ck_assert(li == NULL);
68 li = libinput_udev_create_for_seat(&interface, NULL, udev, NULL);
69 ck_assert(li == NULL);
70 li = libinput_udev_create_for_seat(NULL, NULL, udev, seat);
71 ck_assert(li == NULL);
73 li = libinput_udev_create_for_seat(&interface, NULL, NULL, seat);
74 ck_assert(li == NULL);
78 START_TEST(udev_create_seat0)
81 struct libinput_event *event;
86 ck_assert(udev != NULL);
88 li = libinput_udev_create_for_seat(&simple_interface, NULL, udev, "seat0");
89 ck_assert(li != NULL);
91 fd = libinput_get_fd(li);
92 ck_assert_int_ge(fd, 0);
94 /* expect at least one event */
95 libinput_dispatch(li);
96 event = libinput_get_event(li);
97 ck_assert(event != NULL);
99 libinput_event_destroy(event);
100 libinput_destroy(li);
105 START_TEST(udev_create_empty_seat)
108 struct libinput_event *event;
113 ck_assert(udev != NULL);
115 /* expect a libinput reference, but no events */
116 li = libinput_udev_create_for_seat(&simple_interface, NULL, udev, "seatdoesntexist");
117 ck_assert(li != NULL);
119 fd = libinput_get_fd(li);
120 ck_assert_int_ge(fd, 0);
122 libinput_dispatch(li);
123 event = libinput_get_event(li);
124 ck_assert(event == NULL);
126 libinput_event_destroy(event);
127 libinput_destroy(li);
133 * This test only works if there's at least one device in the system that is
134 * assigned the default seat. Should cover the 99% case.
136 START_TEST(udev_added_seat_default)
139 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;
145 int default_seat_found = 0;
148 ck_assert(udev != NULL);
150 li = libinput_udev_create_for_seat(&simple_interface, NULL, udev, "seat0");
151 ck_assert(li != NULL);
152 libinput_dispatch(li);
154 while (!default_seat_found && (event = libinput_get_event(li))) {
155 type = libinput_event_get_type(event);
156 if (type != LIBINPUT_EVENT_DEVICE_ADDED) {
157 libinput_event_destroy(event);
161 device = libinput_event_get_device(event);
162 seat = libinput_device_get_seat(device);
163 ck_assert(seat != NULL);
165 seat_name = libinput_seat_get_logical_name(seat);
166 default_seat_found = !strcmp(seat_name, "default");
167 libinput_event_destroy(event);
170 ck_assert(default_seat_found);
172 libinput_destroy(li);
177 START_TEST(udev_double_suspend)
180 struct libinput_event *event;
185 ck_assert(udev != NULL);
187 li = libinput_udev_create_for_seat(&simple_interface, NULL, udev, "seat0");
188 ck_assert(li != NULL);
190 fd = libinput_get_fd(li);
191 ck_assert_int_ge(fd, 0);
193 /* expect at least one event */
194 ck_assert_int_ge(libinput_dispatch(li), 0);
195 event = libinput_get_event(li);
196 ck_assert(event != NULL);
198 libinput_suspend(li);
199 libinput_suspend(li);
202 libinput_event_destroy(event);
203 libinput_destroy(li);
208 START_TEST(udev_double_resume)
211 struct libinput_event *event;
216 ck_assert(udev != NULL);
218 li = libinput_udev_create_for_seat(&simple_interface, NULL, udev, "seat0");
219 ck_assert(li != NULL);
221 fd = libinput_get_fd(li);
222 ck_assert_int_ge(fd, 0);
224 /* expect at least one event */
225 ck_assert_int_ge(libinput_dispatch(li), 0);
226 event = libinput_get_event(li);
227 ck_assert(event != NULL);
229 libinput_suspend(li);
233 libinput_event_destroy(event);
234 libinput_destroy(li);
240 process_events_count_devices(struct libinput *li, int *device_count)
242 struct libinput_event *event;
244 while ((event = libinput_get_event(li))) {
245 switch (libinput_event_get_type(event)) {
246 case LIBINPUT_EVENT_DEVICE_ADDED:
249 case LIBINPUT_EVENT_DEVICE_REMOVED:
255 libinput_event_destroy(event);
259 START_TEST(udev_suspend_resume)
267 ck_assert(udev != NULL);
269 li = libinput_udev_create_for_seat(&simple_interface, NULL, udev, "seat0");
270 ck_assert(li != NULL);
272 fd = libinput_get_fd(li);
273 ck_assert_int_ge(fd, 0);
275 /* Check that at least one device was discovered after creation. */
276 ck_assert_int_ge(libinput_dispatch(li), 0);
277 process_events_count_devices(li, &num_devices);
278 ck_assert_int_gt(num_devices, 0);
280 /* Check that after a suspend, no devices are left. */
281 libinput_suspend(li);
282 ck_assert_int_ge(libinput_dispatch(li), 0);
283 process_events_count_devices(li, &num_devices);
284 ck_assert_int_eq(num_devices, 0);
286 /* Check that after a resume, at least one device is discovered. */
288 ck_assert_int_ge(libinput_dispatch(li), 0);
289 process_events_count_devices(li, &num_devices);
290 ck_assert_int_gt(num_devices, 0);
292 libinput_destroy(li);
297 START_TEST(udev_device_sysname)
300 struct libinput_event *ev;
301 struct libinput_device *device;
306 ck_assert(udev != NULL);
308 li = libinput_udev_create_for_seat(&simple_interface, NULL, udev, "seat0");
309 ck_assert(li != NULL);
311 libinput_dispatch(li);
313 while ((ev = libinput_get_event(li))) {
314 if (libinput_event_get_type(ev) != LIBINPUT_EVENT_DEVICE_ADDED)
317 device = libinput_event_get_device(ev);
318 sysname = libinput_device_get_sysname(device);
319 ck_assert(sysname != NULL && strlen(sysname) > 1);
320 ck_assert(strchr(sysname, '/') == NULL);
321 ck_assert_int_eq(strncmp(sysname, "event", 5), 0);
322 libinput_event_destroy(ev);
325 libinput_destroy(li);
330 START_TEST(udev_seat_recycle)
334 struct libinput_event *ev;
335 struct libinput_device *device;
336 struct libinput_seat *saved_seat = NULL;
337 struct libinput_seat *seat;
343 ck_assert(udev != NULL);
345 li = libinput_udev_create_for_seat(&simple_interface, NULL, udev, "seat0");
346 ck_assert(li != NULL);
348 libinput_dispatch(li);
349 while ((ev = libinput_get_event(li))) {
350 switch (libinput_event_get_type(ev)) {
351 case LIBINPUT_EVENT_DEVICE_ADDED:
355 device = libinput_event_get_device(ev);
356 ck_assert(device != NULL);
357 saved_seat = libinput_device_get_seat(device);
358 libinput_seat_set_user_data(saved_seat, &data);
359 libinput_seat_ref(saved_seat);
365 libinput_event_destroy(ev);
368 ck_assert(saved_seat != NULL);
370 libinput_suspend(li);
372 litest_drain_events(li);
376 libinput_dispatch(li);
377 while ((ev = libinput_get_event(li))) {
378 switch (libinput_event_get_type(ev)) {
379 case LIBINPUT_EVENT_DEVICE_ADDED:
380 device = libinput_event_get_device(ev);
381 ck_assert(device != NULL);
383 seat = libinput_device_get_seat(device);
384 user_data = libinput_seat_get_user_data(seat);
385 if (user_data == &data) {
387 ck_assert(seat == saved_seat);
394 libinput_event_destroy(ev);
397 ck_assert(found == 1);
399 libinput_destroy(li);
404 int main (int argc, char **argv) {
406 litest_add_no_device("udev:create", udev_create_NULL);
407 litest_add_no_device("udev:create", udev_create_seat0);
408 litest_add_no_device("udev:create", udev_create_empty_seat);
410 litest_add_no_device("udev:seat events", udev_added_seat_default);
412 litest_add("udev:suspend", udev_double_suspend, LITEST_ANY, LITEST_ANY);
413 litest_add("udev:suspend", udev_double_resume, LITEST_ANY, LITEST_ANY);
414 litest_add("udev:suspend", udev_suspend_resume, LITEST_ANY, LITEST_ANY);
415 litest_add("udev:device events", udev_device_sysname, LITEST_ANY, LITEST_ANY);
416 litest_add("udev:seat", udev_seat_recycle, LITEST_ANY, LITEST_ANY);
418 return litest_run(argc, argv);