[PATCH libinput 5/8] test: add test for device suspend/resume
Peter Hutterer
peter.hutterer at who-t.net
Tue Aug 19 20:18:53 PDT 2014
Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
test/Makefile.am | 7 +-
test/device.c | 250 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 256 insertions(+), 1 deletion(-)
create mode 100644 test/device.c
diff --git a/test/Makefile.am b/test/Makefile.am
index 76d4e8c..4602f9f 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -34,7 +34,8 @@ run_tests = \
test-log \
test-touchpad \
test-misc \
- test-keyboard
+ test-keyboard \
+ test-device
build_tests = \
test-build-cxx \
test-build-linker \
@@ -78,6 +79,10 @@ test_keyboard_SOURCES = keyboard.c
test_keyboard_LDADD = $(TEST_LIBS)
test_keyboard_LDFLAGS = -no-install
+test_device_SOURCES = device.c
+test_device_LDADD = $(TEST_LIBS)
+test_device_LDFLAGS = -no-install
+
# build-test only
test_build_pedantic_c99_SOURCES = build-pedantic.c
test_build_pedantic_c99_CFLAGS = -std=c99 -pedantic -Werror
diff --git a/test/device.c b/test/device.c
new file mode 100644
index 0000000..25e119e
--- /dev/null
+++ b/test/device.c
@@ -0,0 +1,250 @@
+/*
+ * Copyright © 2014 Red Hat, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that the above copyright notice appear in all copies and that both that
+ * copyright notice and this permission notice appear in supporting
+ * documentation, and that the name of the copyright holders not be used in
+ * advertising or publicity pertaining to distribution of the software
+ * without specific, written prior permission. The copyright holders make
+ * no representations about the suitability of this software for any
+ * purpose. It is provided "as is" without express or implied warranty.
+ *
+ * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
+ * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
+ * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
+ * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <config.h>
+
+#include <check.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <libinput.h>
+#include <libudev.h>
+#include <unistd.h>
+
+#include "litest.h"
+
+static int open_restricted(const char *path, int flags, void *data)
+{
+ int fd;
+ fd = open(path, flags);
+ return fd < 0 ? -errno : fd;
+}
+static void close_restricted(int fd, void *data)
+{
+ close(fd);
+}
+
+const struct libinput_interface simple_interface = {
+ .open_restricted = open_restricted,
+ .close_restricted = close_restricted,
+};
+
+
+START_TEST(device_suspend)
+{
+ struct litest_device *dev = litest_current_device();
+ struct libinput *li = dev->libinput;
+ struct libinput_device *device;
+
+ device = dev->libinput_device;
+
+ litest_drain_events(li);
+
+ /* no event from suspending */
+ libinput_device_suspend(device);
+ litest_assert_empty_queue(li);
+
+ /* no event from suspended device */
+ litest_event(dev, EV_REL, REL_X, 10);
+ litest_event(dev, EV_SYN, SYN_REPORT, 0);
+ litest_assert_empty_queue(li);
+
+ /* no event from resuming */
+ libinput_device_resume(device);
+ litest_assert_empty_queue(li);
+}
+END_TEST
+
+START_TEST(device_double_suspend)
+{
+ struct litest_device *dev = litest_current_device();
+ struct libinput *li = dev->libinput;
+ struct libinput_device *device;
+
+ device = dev->libinput_device;
+
+ litest_drain_events(li);
+
+ libinput_device_suspend(device);
+ libinput_device_suspend(device);
+
+ litest_assert_empty_queue(li);
+}
+END_TEST
+
+START_TEST(device_double_resume)
+{
+ struct litest_device *dev = litest_current_device();
+ struct libinput *li = dev->libinput;
+ struct libinput_device *device;
+
+ device = dev->libinput_device;
+
+ litest_drain_events(li);
+
+ libinput_device_suspend(device);
+ libinput_device_resume(device);
+ libinput_device_resume(device);
+
+ litest_assert_empty_queue(li);
+}
+END_TEST
+
+START_TEST(device_resume_invalid_syspath_changed)
+{
+ struct libinput *li;
+ struct libinput_device *device;
+ struct libevdev *evdev;
+ struct libevdev_uinput *uinput;
+ int rc;
+ char *devnode;
+
+ evdev = libevdev_new();
+ ck_assert_notnull(evdev);
+
+ libevdev_set_name(evdev, "litest device");
+ libevdev_enable_event_code(evdev, EV_KEY, BTN_LEFT, NULL);
+ libevdev_enable_event_code(evdev, EV_KEY, BTN_RIGHT, NULL);
+ libevdev_enable_event_code(evdev, EV_REL, REL_X, NULL);
+ libevdev_enable_event_code(evdev, EV_REL, REL_Y, NULL);
+
+ rc = libevdev_uinput_create_from_device(evdev,
+ LIBEVDEV_UINPUT_OPEN_MANAGED,
+ &uinput);
+ ck_assert_int_eq(rc, 0);
+ devnode = strdup(libevdev_uinput_get_devnode(uinput));
+
+ li = libinput_path_create_context(&simple_interface, NULL);
+ ck_assert(li != NULL);
+
+ device = libinput_path_add_device(li, libevdev_uinput_get_devnode(uinput));
+ libinput_device_ref(device);
+ libinput_device_suspend(device);
+ litest_drain_events(li);
+
+ /* now destroy the original device and re-create it */
+ libevdev_uinput_destroy(uinput);
+ libinput_dispatch(li);
+ rc = libevdev_uinput_create_from_device(evdev,
+ LIBEVDEV_UINPUT_OPEN_MANAGED,
+ &uinput);
+ ck_assert_int_eq(rc, 0);
+
+ /* make sure it came back with the same device node */
+ ck_assert_int_eq(strcmp(devnode, libevdev_uinput_get_devnode(uinput)), 0);
+
+ rc = libinput_device_resume(device);
+ ck_assert_int_eq(rc, -ENODEV);
+
+ libinput_device_unref(device);
+ libinput_unref(li);
+ libevdev_uinput_destroy(uinput);
+ libevdev_free(evdev);
+ free(devnode);
+}
+END_TEST
+
+START_TEST(device_resume_invalid_device_removed)
+{
+ struct libinput *li;
+ struct libinput_device *device;
+ struct libevdev *evdev;
+ struct libevdev_uinput *uinput;
+ struct udev *udev;
+ struct libinput_event *event;
+ int rc;
+ char *devnode;
+ const char *name = "litest resume test device";
+
+ evdev = libevdev_new();
+ ck_assert_notnull(evdev);
+
+ libevdev_set_name(evdev, name);
+ libevdev_enable_event_code(evdev, EV_KEY, BTN_LEFT, NULL);
+ libevdev_enable_event_code(evdev, EV_KEY, BTN_RIGHT, NULL);
+ libevdev_enable_event_code(evdev, EV_REL, REL_X, NULL);
+ libevdev_enable_event_code(evdev, EV_REL, REL_Y, NULL);
+
+ rc = libevdev_uinput_create_from_device(evdev,
+ LIBEVDEV_UINPUT_OPEN_MANAGED,
+ &uinput);
+ ck_assert_int_eq(rc, 0);
+ devnode = strdup(libevdev_uinput_get_devnode(uinput));
+
+ udev = udev_new();
+ li = libinput_udev_create_context(&simple_interface, NULL, udev);
+ ck_assert(li != NULL);
+ libinput_udev_assign_seat(li, "seat0");
+
+ libinput_dispatch(li);
+ rc = 0;
+ event = libinput_get_event(li);
+ ck_assert_notnull(event);
+ do {
+ if (libinput_event_get_type(event) == LIBINPUT_EVENT_DEVICE_ADDED) {
+ struct libinput_device *d = libinput_event_get_device(event);
+ if (strcmp(libinput_device_get_name(d), name) == 0) {
+ device = d;
+ rc = 1;
+ libinput_event_destroy(event);
+ break;
+ }
+ }
+ libinput_event_destroy(event);
+ event = libinput_get_event(li);
+ } while (event != NULL);
+
+ ck_assert_int_eq(rc, 1);
+
+ libinput_device_ref(device);
+ libinput_device_suspend(device);
+ litest_drain_events(li);
+
+ /* now destroy the original device and re-create it */
+ libevdev_uinput_destroy(uinput);
+
+ while (libinput_next_event_type(li) != LIBINPUT_EVENT_DEVICE_REMOVED) {
+ event = libinput_get_event(li);
+ libinput_event_destroy(event);
+ libinput_dispatch(li);
+ }
+
+ rc = libinput_device_resume(device);
+ ck_assert_int_eq(rc, -ENODEV);
+
+ libinput_device_unref(device);
+ libinput_unref(li);
+ libevdev_free(evdev);
+ free(devnode);
+ udev_unref(udev);
+}
+END_TEST
+
+int main (int argc, char **argv)
+{
+ litest_add("device:suspend", device_suspend, LITEST_POINTER, LITEST_ANY);
+ litest_add("device:suspend", device_double_suspend, LITEST_ANY, LITEST_ANY);
+ litest_add("device:suspend", device_double_resume, LITEST_ANY, LITEST_ANY);
+ litest_add_no_device("device:suspend", device_resume_invalid_syspath_changed);
+ litest_add_no_device("device:suspend", device_resume_invalid_device_removed);
+
+ return litest_run(argc, argv);
+}
--
1.9.3
More information about the wayland-devel
mailing list