From: Peter Hutterer Date: Tue, 10 Dec 2013 03:30:52 +0000 (+1000) Subject: test: add tests for libinput_create_from_path X-Git-Tag: 0.1.0~81 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=caaf170f4de21345338e534a02da0b67c326f36b;p=platform%2Fupstream%2Flibinput.git test: add tests for libinput_create_from_path Signed-off-by: Peter Hutterer squashme path test --- diff --git a/test/Makefile.am b/test/Makefile.am index 3128a80e..75b14122 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -14,7 +14,7 @@ liblitest_la_SOURCES = \ litest-trackpoint.c \ litest.c -run_tests = test-udev +run_tests = test-udev test-path build_tests = test-build-linker test-build-pedantic-c99 test-build-std-gnuc90 noinst_PROGRAMS = $(build_tests) $(run_tests) @@ -25,6 +25,11 @@ test_udev_CFLAGS = $(AM_CPPFLAGS) test_udev_LDADD = $(TEST_LIBS) test_udev_LDFLAGS = -static +test_path_SOURCES = path.c +test_path_CFLAGS = $(AM_CPPFLAGS) +test_path_LDADD = $(TEST_LIBS) +test_path_LDFLAGS = -static + # build-test only test_build_pedantic_c99_SOURCES = build-pedantic.c test_build_pedantic_c99_CFLAGS = $(AM_CPPFLAGS) -std=c99 -pedantic -Werror diff --git a/test/path.c b/test/path.c new file mode 100644 index 00000000..778e9147 --- /dev/null +++ b/test/path.c @@ -0,0 +1,201 @@ + +/* + * Copyright © 2013 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 + +#include +#include +#include +#include +#include +#include + +#include "litest.h" + +static int open_func_count = 0; +static int close_func_count = 0; + +static int open_restricted(const char *path, int flags, void *data) +{ + int fd; + open_func_count++; + fd = open(path, flags); + return fd < 0 ? -errno : fd; +} +static void close_restricted(int fd, void *data) +{ + close_func_count++; + close(fd); +} + +const struct libinput_interface simple_interface = { + .open_restricted = open_restricted, + .close_restricted = close_restricted, +}; + + +START_TEST(path_create_NULL) +{ + struct libinput *li; + const struct libinput_interface interface; + const char *path = "foo"; + + open_func_count = 0; + close_func_count = 0; + + li = libinput_create_from_path(NULL, NULL, NULL); + ck_assert(li == NULL); + li = libinput_create_from_path(&interface, NULL, NULL); + ck_assert(li == NULL); + li = libinput_create_from_path(NULL, NULL, path); + ck_assert(li == NULL); + + ck_assert_int_eq(open_func_count, 0); + ck_assert_int_eq(close_func_count, 0); +} +END_TEST + +START_TEST(path_create_invalid) +{ + struct libinput *li; + const char *path = "/tmp"; + + open_func_count = 0; + close_func_count = 0; + + li = libinput_create_from_path(&simple_interface, NULL, path); + ck_assert(li == NULL); + + ck_assert_int_eq(open_func_count, 1); + ck_assert_int_eq(close_func_count, 0); + + libinput_destroy(li); + ck_assert_int_eq(close_func_count, 0); +} +END_TEST + +START_TEST(path_create_destroy) +{ + struct libinput *li; + struct libevdev *evdev; + struct libevdev_uinput *uinput; + int rc; + void *userdata = &rc; + + evdev = libevdev_new(); + ck_assert(evdev != NULL); + + libevdev_set_name(evdev, "test 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); + libevdev_free(evdev); + + li = libinput_create_from_path(&simple_interface, userdata, + libevdev_uinput_get_devnode(uinput)); + ck_assert(li != NULL); + ck_assert(libinput_get_user_data(li) == userdata); + ck_assert_int_eq(open_func_count, 1); + + libevdev_uinput_destroy(uinput); + libinput_destroy(li); + ck_assert_int_eq(close_func_count, 1); +} +END_TEST + +START_TEST(path_added_seat) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event *event; + struct libinput_event_added_seat *seat_event; + struct libinput_seat *seat; + const char *seat_name; + enum libinput_event_type type; + + libinput_dispatch(li); + + event = libinput_get_event(li); + ck_assert(event != NULL); + + type = libinput_event_get_type(event); + ck_assert_int_eq(type, LIBINPUT_EVENT_ADDED_SEAT); + + seat_event = (struct libinput_event_added_seat*)event; + seat = libinput_event_added_seat_get_seat(seat_event); + ck_assert(seat != NULL); + + seat_name = libinput_seat_get_name(seat); + ck_assert_int_eq(strcmp(seat_name, "default"), 0); + + libinput_event_destroy(event); +} +END_TEST + +START_TEST(path_added_device) +{ + struct litest_device *dev = litest_current_device(); + struct libinput *li = dev->libinput; + struct libinput_event *event; + struct libinput_event_added_device *device_event = NULL; + struct libinput_device *device; + + libinput_dispatch(li); + + while ((event = libinput_get_event(li))) { + enum libinput_event_type type; + type = libinput_event_get_type(event); + + if (type == LIBINPUT_EVENT_ADDED_DEVICE) { + device_event = (struct libinput_event_added_device*)event; + break; + } + + libinput_event_destroy(event); + } + + ck_assert(device_event != NULL); + + device = libinput_event_added_device_get_device(device_event); + ck_assert(device != NULL); + + libinput_event_destroy(event); +} +END_TEST + +int main (int argc, char **argv) { + + litest_add("path:create", path_create_NULL, LITEST_ANY, LITEST_ANY); + litest_add("path:create", path_create_invalid, LITEST_ANY, LITEST_ANY); + litest_add("path:create", path_create_destroy, LITEST_ANY, LITEST_ANY); + litest_add("path:seat events", path_added_seat, LITEST_ANY, LITEST_ANY); + litest_add("path:device events", path_added_device, LITEST_ANY, LITEST_ANY); + + return litest_run(argc, argv); +}