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,
50 START_TEST(udev_create_NULL)
53 const struct libinput_interface interface;
58 li = libinput_udev_create_context(NULL, NULL, NULL);
59 ck_assert(li == NULL);
61 li = libinput_udev_create_context(&interface, NULL, NULL);
62 ck_assert(li == NULL);
64 li = libinput_udev_create_context(NULL, NULL, udev);
65 ck_assert(li == NULL);
67 li = libinput_udev_create_context(&interface, NULL, udev);
68 ck_assert(li != NULL);
69 ck_assert_int_eq(libinput_udev_assign_seat(li, NULL), -1);
76 START_TEST(udev_create_seat0)
79 struct libinput_event *event;
84 ck_assert(udev != NULL);
86 li = libinput_udev_create_context(&simple_interface, NULL, udev);
87 ck_assert(li != NULL);
88 ck_assert_int_eq(libinput_udev_assign_seat(li, "seat0"), 0);
90 fd = libinput_get_fd(li);
91 ck_assert_int_ge(fd, 0);
93 /* expect at least one event */
94 libinput_dispatch(li);
95 event = libinput_get_event(li);
96 ck_assert(event != NULL);
98 libinput_event_destroy(event);
104 START_TEST(udev_create_empty_seat)
107 struct libinput_event *event;
112 ck_assert(udev != NULL);
114 /* expect a libinput reference, but no events */
115 li = libinput_udev_create_context(&simple_interface, NULL, udev);
116 ck_assert(li != NULL);
117 ck_assert_int_eq(libinput_udev_assign_seat(li, "seatdoesntexist"), 0);
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);
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_context(&simple_interface, NULL, udev);
151 ck_assert(li != NULL);
152 ck_assert_int_eq(libinput_udev_assign_seat(li, "seat0"), 0);
153 libinput_dispatch(li);
155 while (!default_seat_found && (event = libinput_get_event(li))) {
156 type = libinput_event_get_type(event);
157 if (type != LIBINPUT_EVENT_DEVICE_ADDED) {
158 libinput_event_destroy(event);
162 device = libinput_event_get_device(event);
163 seat = libinput_device_get_seat(device);
164 ck_assert(seat != NULL);
166 seat_name = libinput_seat_get_logical_name(seat);
167 default_seat_found = !strcmp(seat_name, "default");
168 libinput_event_destroy(event);
171 ck_assert(default_seat_found);
178 START_TEST(udev_double_suspend)
181 struct libinput_event *event;
186 ck_assert(udev != NULL);
188 li = libinput_udev_create_context(&simple_interface, NULL, udev);
189 ck_assert(li != NULL);
190 ck_assert_int_eq(libinput_udev_assign_seat(li, "seat0"), 0);
192 fd = libinput_get_fd(li);
193 ck_assert_int_ge(fd, 0);
195 /* expect at least one event */
196 ck_assert_int_ge(libinput_dispatch(li), 0);
197 event = libinput_get_event(li);
198 ck_assert(event != NULL);
200 libinput_suspend(li);
201 libinput_suspend(li);
204 libinput_event_destroy(event);
210 START_TEST(udev_double_resume)
213 struct libinput_event *event;
218 ck_assert(udev != NULL);
220 li = libinput_udev_create_context(&simple_interface, NULL, udev);
221 ck_assert(li != NULL);
222 ck_assert_int_eq(libinput_udev_assign_seat(li, "seat0"), 0);
224 fd = libinput_get_fd(li);
225 ck_assert_int_ge(fd, 0);
227 /* expect at least one event */
228 ck_assert_int_ge(libinput_dispatch(li), 0);
229 event = libinput_get_event(li);
230 ck_assert(event != NULL);
232 libinput_suspend(li);
236 libinput_event_destroy(event);
243 process_events_count_devices(struct libinput *li, int *device_count)
245 struct libinput_event *event;
247 while ((event = libinput_get_event(li))) {
248 switch (libinput_event_get_type(event)) {
249 case LIBINPUT_EVENT_DEVICE_ADDED:
252 case LIBINPUT_EVENT_DEVICE_REMOVED:
258 libinput_event_destroy(event);
262 START_TEST(udev_suspend_resume)
270 ck_assert(udev != NULL);
272 li = libinput_udev_create_context(&simple_interface, NULL, udev);
273 ck_assert(li != NULL);
274 ck_assert_int_eq(libinput_udev_assign_seat(li, "seat0"), 0);
276 fd = libinput_get_fd(li);
277 ck_assert_int_ge(fd, 0);
279 /* Check that at least one device was discovered after creation. */
280 ck_assert_int_ge(libinput_dispatch(li), 0);
281 process_events_count_devices(li, &num_devices);
282 ck_assert_int_gt(num_devices, 0);
284 /* Check that after a suspend, no devices are left. */
285 libinput_suspend(li);
286 ck_assert_int_ge(libinput_dispatch(li), 0);
287 process_events_count_devices(li, &num_devices);
288 ck_assert_int_eq(num_devices, 0);
290 /* Check that after a resume, at least one device is discovered. */
292 ck_assert_int_ge(libinput_dispatch(li), 0);
293 process_events_count_devices(li, &num_devices);
294 ck_assert_int_gt(num_devices, 0);
301 START_TEST(udev_device_sysname)
304 struct libinput_event *ev;
305 struct libinput_device *device;
310 ck_assert(udev != NULL);
312 li = libinput_udev_create_context(&simple_interface, NULL, udev);
313 ck_assert(li != NULL);
314 ck_assert_int_eq(libinput_udev_assign_seat(li, "seat0"), 0);
316 libinput_dispatch(li);
318 while ((ev = libinput_get_event(li))) {
319 if (libinput_event_get_type(ev) != LIBINPUT_EVENT_DEVICE_ADDED)
322 device = libinput_event_get_device(ev);
323 sysname = libinput_device_get_sysname(device);
324 ck_assert(sysname != NULL && strlen(sysname) > 1);
325 ck_assert(strchr(sysname, '/') == NULL);
326 ck_assert_int_eq(strncmp(sysname, "event", 5), 0);
327 libinput_event_destroy(ev);
335 START_TEST(udev_seat_recycle)
339 struct libinput_event *ev;
340 struct libinput_device *device;
341 struct libinput_seat *saved_seat = NULL;
342 struct libinput_seat *seat;
348 ck_assert(udev != NULL);
350 li = libinput_udev_create_context(&simple_interface, NULL, udev);
351 ck_assert(li != NULL);
352 ck_assert_int_eq(libinput_udev_assign_seat(li, "seat0"), 0);
354 libinput_dispatch(li);
355 while ((ev = libinput_get_event(li))) {
356 switch (libinput_event_get_type(ev)) {
357 case LIBINPUT_EVENT_DEVICE_ADDED:
361 device = libinput_event_get_device(ev);
362 ck_assert(device != NULL);
363 saved_seat = libinput_device_get_seat(device);
364 libinput_seat_set_user_data(saved_seat, &data);
365 libinput_seat_ref(saved_seat);
371 libinput_event_destroy(ev);
374 ck_assert(saved_seat != NULL);
376 libinput_suspend(li);
378 litest_drain_events(li);
382 libinput_dispatch(li);
383 while ((ev = libinput_get_event(li))) {
384 switch (libinput_event_get_type(ev)) {
385 case LIBINPUT_EVENT_DEVICE_ADDED:
386 device = libinput_event_get_device(ev);
387 ck_assert(device != NULL);
389 seat = libinput_device_get_seat(device);
390 user_data = libinput_seat_get_user_data(seat);
391 if (user_data == &data) {
393 ck_assert(seat == saved_seat);
400 libinput_event_destroy(ev);
403 ck_assert(found == 1);
410 int main (int argc, char **argv) {
412 litest_add_no_device("udev:create", udev_create_NULL);
413 litest_add_no_device("udev:create", udev_create_seat0);
414 litest_add_no_device("udev:create", udev_create_empty_seat);
416 litest_add_no_device("udev:seat events", udev_added_seat_default);
418 litest_add("udev:suspend", udev_double_suspend, LITEST_ANY, LITEST_ANY);
419 litest_add("udev:suspend", udev_double_resume, LITEST_ANY, LITEST_ANY);
420 litest_add("udev:suspend", udev_suspend_resume, LITEST_ANY, LITEST_ANY);
421 litest_add("udev:device events", udev_device_sysname, LITEST_ANY, LITEST_ANY);
422 litest_add("udev:seat", udev_seat_recycle, LITEST_ANY, LITEST_ANY);
424 return litest_run(argc, argv);