diff options
Diffstat (limited to 'test/test-switch.c')
-rw-r--r-- | test/test-switch.c | 181 |
1 files changed, 126 insertions, 55 deletions
diff --git a/test/test-switch.c b/test/test-switch.c index e0434100..3c6275a9 100644 --- a/test/test-switch.c +++ b/test/test-switch.c @@ -24,6 +24,7 @@ #include <config.h> #include <check.h> +#include <fcntl.h> #include <libinput.h> #include "libinput-util.h" @@ -113,6 +114,7 @@ START_TEST(switch_toggle) litest_drain_events(li); + litest_grab_device(dev); litest_switch_action(dev, sw, LIBINPUT_SWITCH_STATE_ON); libinput_dispatch(li); @@ -134,6 +136,7 @@ START_TEST(switch_toggle) } litest_assert_empty_queue(li); + litest_ungrab_device(dev); } END_TEST @@ -149,6 +152,7 @@ START_TEST(switch_toggle_double) litest_drain_events(li); + litest_grab_device(dev); litest_switch_action(dev, sw, LIBINPUT_SWITCH_STATE_ON); libinput_dispatch(li); @@ -162,6 +166,7 @@ START_TEST(switch_toggle_double) libinput_dispatch(li); litest_assert_empty_queue(li); + litest_ungrab_device(dev); } END_TEST @@ -194,7 +199,9 @@ START_TEST(switch_down_on_init) if (sw == LIBINPUT_SWITCH_LID && !lid_switch_is_reliable(dev)) return; + litest_grab_device(dev); litest_switch_action(dev, sw, LIBINPUT_SWITCH_STATE_ON); + litest_ungrab_device(dev); /* need separate context to test */ li = litest_create_context(); @@ -237,7 +244,9 @@ START_TEST(switch_not_down_on_init) if (sw == LIBINPUT_SWITCH_LID && lid_switch_is_reliable(dev)) return; + litest_grab_device(dev); litest_switch_action(dev, sw, LIBINPUT_SWITCH_STATE_ON); + litest_ungrab_device(dev); /* need separate context to test */ li = litest_create_context(); @@ -279,6 +288,8 @@ START_TEST(switch_disable_touchpad) litest_disable_tap(touchpad->libinput_device); litest_drain_events(li); + litest_grab_device(sw); + /* switch is on - no events */ litest_switch_action(sw, which, LIBINPUT_SWITCH_STATE_ON); litest_assert_only_typed_events(li, LIBINPUT_EVENT_SWITCH_TOGGLE); @@ -298,6 +309,7 @@ START_TEST(switch_disable_touchpad) litest_assert_only_typed_events(li, LIBINPUT_EVENT_POINTER_MOTION); litest_delete_device(touchpad); + litest_ungrab_device(sw); } END_TEST @@ -319,8 +331,10 @@ START_TEST(switch_disable_touchpad_during_touch) litest_touch_move_to(touchpad, 0, 50, 50, 70, 50, 5); litest_assert_only_typed_events(li, LIBINPUT_EVENT_POINTER_MOTION); + litest_grab_device(sw); litest_switch_action(sw, which, LIBINPUT_SWITCH_STATE_ON); litest_assert_only_typed_events(li, LIBINPUT_EVENT_SWITCH_TOGGLE); + litest_ungrab_device(sw); litest_touch_move_to(touchpad, 0, 70, 50, 50, 50, 5); litest_touch_up(touchpad, 0); @@ -345,8 +359,10 @@ START_TEST(switch_disable_touchpad_edge_scroll) litest_drain_events(li); + litest_grab_device(sw); litest_switch_action(sw, which, LIBINPUT_SWITCH_STATE_ON); litest_assert_only_typed_events(li, LIBINPUT_EVENT_SWITCH_TOGGLE); + litest_ungrab_device(sw); litest_touch_down(touchpad, 0, 99, 20); libinput_dispatch(li); @@ -390,7 +406,9 @@ START_TEST(switch_disable_touchpad_edge_scroll_interrupt) libinput_dispatch(li); litest_assert_only_typed_events(li, LIBINPUT_EVENT_POINTER_AXIS); + litest_grab_device(sw); litest_switch_action(sw, which, LIBINPUT_SWITCH_STATE_ON); + litest_ungrab_device(sw); libinput_dispatch(li); event = libinput_get_event(li); @@ -458,7 +476,9 @@ START_TEST(switch_dont_resume_disabled_touchpad) litest_drain_events(li); /* switch is on - no events */ + litest_grab_device(sw); litest_switch_action(sw, which, LIBINPUT_SWITCH_STATE_ON); + litest_ungrab_device(sw); litest_assert_only_typed_events(li, LIBINPUT_EVENT_SWITCH_TOGGLE); litest_touch_down(touchpad, 0, 50, 50); @@ -502,7 +522,9 @@ START_TEST(switch_dont_resume_disabled_touchpad_external_mouse) litest_assert_empty_queue(li); /* switch is on - no events */ + litest_grab_device(sw); litest_switch_action(sw, which, LIBINPUT_SWITCH_STATE_ON); + litest_ungrab_device(sw); litest_assert_only_typed_events(li, LIBINPUT_EVENT_SWITCH_TOGGLE); litest_touch_down(touchpad, 0, 50, 50); @@ -536,6 +558,7 @@ START_TEST(lid_open_on_key) keyboard = litest_add_device(li, LITEST_KEYBOARD); + litest_grab_device(sw); for (int i = 0; i < 3; i++) { litest_switch_action(sw, LIBINPUT_SWITCH_LID, @@ -561,6 +584,7 @@ START_TEST(lid_open_on_key) LIBINPUT_SWITCH_STATE_OFF); litest_assert_empty_queue(li); } + litest_ungrab_device(sw); litest_delete_device(keyboard); } @@ -578,9 +602,11 @@ START_TEST(lid_open_on_key_touchpad_enabled) keyboard = litest_add_device(li, LITEST_KEYBOARD); touchpad = litest_add_device(li, LITEST_SYNAPTICS_I2C); + litest_grab_device(sw); litest_switch_action(sw, LIBINPUT_SWITCH_LID, LIBINPUT_SWITCH_STATE_ON); + litest_ungrab_device(sw); litest_drain_events(li); litest_event(keyboard, EV_KEY, KEY_A, 1); @@ -627,10 +653,12 @@ START_TEST(switch_suspend_with_keyboard) keyboard = litest_add_device(li, LITEST_KEYBOARD); libinput_dispatch(li); + litest_grab_device(sw); litest_switch_action(sw, which, LIBINPUT_SWITCH_STATE_ON); litest_drain_events(li); litest_switch_action(sw, which, LIBINPUT_SWITCH_STATE_OFF); litest_drain_events(li); + litest_ungrab_device(sw); litest_delete_device(keyboard); litest_drain_events(li); @@ -681,49 +709,54 @@ START_TEST(lid_update_hw_on_key) { struct litest_device *sw = litest_current_device(); struct libinput *li = sw->libinput; - struct libinput *li2; struct litest_device *keyboard; - struct libinput_event *event; + struct libevdev *evdev; + struct input_event event; + int fd; + int rc; if (!switch_has_lid(sw)) return; keyboard = litest_add_device(li, LITEST_KEYBOARD); - /* separate context to listen to the fake hw event */ - li2 = litest_create_context(); - libinput_path_add_device(li2, - libevdev_uinput_get_devnode(sw->uinput)); - litest_drain_events(li2); - + litest_grab_device(sw); litest_switch_action(sw, LIBINPUT_SWITCH_LID, LIBINPUT_SWITCH_STATE_ON); litest_drain_events(li); + litest_ungrab_device(sw); - libinput_dispatch(li2); - event = libinput_get_event(li2); - litest_is_switch_event(event, - LIBINPUT_SWITCH_LID, - LIBINPUT_SWITCH_STATE_ON); - libinput_event_destroy(event); + /* Separate direct libevdev context to check if the HW event goes + * through */ + fd = open(libevdev_uinput_get_devnode(sw->uinput), O_RDONLY|O_NONBLOCK); + ck_assert_int_ge(fd, 0); + ck_assert_int_eq(libevdev_new_from_fd(fd, &evdev), 0); + ck_assert_int_eq(libevdev_get_event_value(evdev, EV_SW, SW_LID), 1); + /* Typing on the keyboard should trigger a lid open event */ litest_event(keyboard, EV_KEY, KEY_A, 1); litest_event(keyboard, EV_SYN, SYN_REPORT, 0); litest_event(keyboard, EV_KEY, KEY_A, 0); litest_event(keyboard, EV_SYN, SYN_REPORT, 0); litest_drain_events(li); - litest_wait_for_event(li2); - event = libinput_get_event(li2); - litest_is_switch_event(event, - LIBINPUT_SWITCH_LID, - LIBINPUT_SWITCH_STATE_OFF); - libinput_event_destroy(event); - litest_assert_empty_queue(li2); + rc = libevdev_next_event(evdev, LIBEVDEV_READ_FLAG_NORMAL, &event); + ck_assert_int_eq(rc, LIBEVDEV_READ_STATUS_SUCCESS); + ck_assert_int_eq(event.type, EV_SW); + ck_assert_int_eq(event.code, SW_LID); + ck_assert_int_eq(event.value, 0); + rc = libevdev_next_event(evdev, LIBEVDEV_READ_FLAG_NORMAL, &event); + ck_assert_int_eq(rc, LIBEVDEV_READ_STATUS_SUCCESS); + ck_assert_int_eq(event.type, EV_SYN); + ck_assert_int_eq(event.code, SYN_REPORT); + ck_assert_int_eq(event.value, 0); + rc = libevdev_next_event(evdev, LIBEVDEV_READ_FLAG_NORMAL, &event); + ck_assert_int_eq(rc, -EAGAIN); - litest_destroy_context(li2); litest_delete_device(keyboard); + close(fd); + libevdev_free(evdev); } END_TEST @@ -733,17 +766,22 @@ START_TEST(lid_update_hw_on_key_closed_on_init) struct libinput *li; struct litest_device *keyboard; struct libevdev *evdev = sw->evdev; - struct input_event ev; + struct input_event event; + int fd; + int rc; + litest_grab_device(sw); litest_switch_action(sw, LIBINPUT_SWITCH_LID, LIBINPUT_SWITCH_STATE_ON); + litest_ungrab_device(sw); - /* Make sure kernel state is right */ - libevdev_next_event(evdev, LIBEVDEV_READ_FLAG_FORCE_SYNC, &ev); - while (libevdev_next_event(evdev, LIBEVDEV_READ_FLAG_SYNC, &ev) >= 0) - ; - ck_assert(libevdev_get_event_value(evdev, EV_SW, SW_LID)); + /* Separate direct libevdev context to check if the HW event goes + * through */ + fd = open(libevdev_uinput_get_devnode(sw->uinput), O_RDONLY|O_NONBLOCK); + ck_assert_int_ge(fd, 0); + ck_assert_int_eq(libevdev_new_from_fd(fd, &evdev), 0); + ck_assert_int_eq(libevdev_get_event_value(evdev, EV_SW, SW_LID), 1); keyboard = litest_add_device(sw->libinput, LITEST_KEYBOARD); @@ -754,7 +792,8 @@ START_TEST(lid_update_hw_on_key_closed_on_init) libinput_path_add_device(li, libevdev_uinput_get_devnode(keyboard->uinput)); - /* don't expect a switch waiting for us */ + /* don't expect a switch waiting for us, this is run for an + * unreliable device */ while (libinput_next_event_type(li) != LIBINPUT_EVENT_NONE) { ck_assert_int_ne(libinput_next_event_type(li), LIBINPUT_EVENT_SWITCH_TOGGLE); @@ -769,13 +808,23 @@ START_TEST(lid_update_hw_on_key_closed_on_init) litest_assert_only_typed_events(li, LIBINPUT_EVENT_KEYBOARD_KEY); /* Make sure kernel state has updated */ - libevdev_next_event(evdev, LIBEVDEV_READ_FLAG_FORCE_SYNC, &ev); - while (libevdev_next_event(evdev, LIBEVDEV_READ_FLAG_SYNC, &ev) >= 0) - ; - ck_assert(!libevdev_get_event_value(evdev, EV_SW, SW_LID)); + rc = libevdev_next_event(evdev, LIBEVDEV_READ_FLAG_NORMAL, &event); + ck_assert_int_eq(rc, LIBEVDEV_READ_STATUS_SUCCESS); + ck_assert_int_eq(event.type, EV_SW); + ck_assert_int_eq(event.code, SW_LID); + ck_assert_int_eq(event.value, 0); + rc = libevdev_next_event(evdev, LIBEVDEV_READ_FLAG_NORMAL, &event); + ck_assert_int_eq(rc, LIBEVDEV_READ_STATUS_SUCCESS); + ck_assert_int_eq(event.type, EV_SYN); + ck_assert_int_eq(event.code, SYN_REPORT); + ck_assert_int_eq(event.value, 0); + rc = libevdev_next_event(evdev, LIBEVDEV_READ_FLAG_NORMAL, &event); + ck_assert_int_eq(rc, -EAGAIN); litest_destroy_context(li); litest_delete_device(keyboard); + close(fd); + libevdev_free(evdev); } END_TEST @@ -783,9 +832,11 @@ START_TEST(lid_update_hw_on_key_multiple_keyboards) { struct litest_device *sw = litest_current_device(); struct libinput *li = sw->libinput; - struct libinput *li2; struct litest_device *keyboard1, *keyboard2; - struct libinput_event *event; + struct libevdev *evdev = sw->evdev; + struct input_event event; + int fd; + int rc; if (!switch_has_lid(sw)) return; @@ -797,41 +848,44 @@ START_TEST(lid_update_hw_on_key_multiple_keyboards) keyboard2 = litest_add_device(li, LITEST_KEYBOARD_BLADE_STEALTH); libinput_dispatch(li); - /* separate context to listen to the fake hw event */ - li2 = litest_create_context(); - libinput_path_add_device(li2, - libevdev_uinput_get_devnode(sw->uinput)); - litest_drain_events(li2); - + litest_grab_device(sw); litest_switch_action(sw, LIBINPUT_SWITCH_LID, LIBINPUT_SWITCH_STATE_ON); litest_drain_events(li); + litest_ungrab_device(sw); - libinput_dispatch(li2); - event = libinput_get_event(li2); - litest_is_switch_event(event, - LIBINPUT_SWITCH_LID, - LIBINPUT_SWITCH_STATE_ON); - libinput_event_destroy(event); + /* Separate direct libevdev context to check if the HW event goes + * through */ + fd = open(libevdev_uinput_get_devnode(sw->uinput), O_RDONLY|O_NONBLOCK); + ck_assert_int_ge(fd, 0); + ck_assert_int_eq(libevdev_new_from_fd(fd, &evdev), 0); + ck_assert_int_eq(libevdev_get_event_value(evdev, EV_SW, SW_LID), 1); + /* Typing on the second keyboard should trigger a lid open event */ litest_event(keyboard2, EV_KEY, KEY_A, 1); litest_event(keyboard2, EV_SYN, SYN_REPORT, 0); litest_event(keyboard2, EV_KEY, KEY_A, 0); litest_event(keyboard2, EV_SYN, SYN_REPORT, 0); litest_drain_events(li); - litest_wait_for_event(li2); - event = libinput_get_event(li2); - litest_is_switch_event(event, - LIBINPUT_SWITCH_LID, - LIBINPUT_SWITCH_STATE_OFF); - libinput_event_destroy(event); - litest_assert_empty_queue(li2); + rc = libevdev_next_event(evdev, LIBEVDEV_READ_FLAG_NORMAL, &event); + ck_assert_int_eq(rc, LIBEVDEV_READ_STATUS_SUCCESS); + ck_assert_int_eq(event.type, EV_SW); + ck_assert_int_eq(event.code, SW_LID); + ck_assert_int_eq(event.value, 0); + rc = libevdev_next_event(evdev, LIBEVDEV_READ_FLAG_NORMAL, &event); + ck_assert_int_eq(rc, LIBEVDEV_READ_STATUS_SUCCESS); + ck_assert_int_eq(event.type, EV_SYN); + ck_assert_int_eq(event.code, SYN_REPORT); + ck_assert_int_eq(event.value, 0); + rc = libevdev_next_event(evdev, LIBEVDEV_READ_FLAG_NORMAL, &event); + ck_assert_int_eq(rc, -EAGAIN); - litest_destroy_context(li2); litest_delete_device(keyboard1); litest_delete_device(keyboard2); + close(fd); + libevdev_free(evdev); } END_TEST @@ -860,6 +914,7 @@ START_TEST(tablet_mode_disable_touchpad_on_init) if (!switch_has_tablet_mode(sw)) return; + litest_grab_device(sw); litest_switch_action(sw, LIBINPUT_SWITCH_TABLET_MODE, LIBINPUT_SWITCH_STATE_ON); @@ -884,6 +939,7 @@ START_TEST(tablet_mode_disable_touchpad_on_init) litest_touch_move_to(touchpad, 0, 50, 50, 70, 50, 10); litest_touch_up(touchpad, 0); litest_assert_only_typed_events(li, LIBINPUT_EVENT_POINTER_MOTION); + litest_ungrab_device(sw); litest_delete_device(touchpad); } @@ -1098,9 +1154,14 @@ START_TEST(tablet_mode_disable_keyboard_on_resume) litest_drain_events(li); libinput_suspend(li); + /* We cannot grab this device because libinput doesn't have an open + * fd to this device, we need an independent grab. + */ + libevdev_grab(sw->evdev, LIBEVDEV_GRAB); litest_switch_action(sw, LIBINPUT_SWITCH_TABLET_MODE, LIBINPUT_SWITCH_STATE_ON); + libevdev_grab(sw->evdev, LIBEVDEV_UNGRAB); litest_drain_events(li); libinput_resume(li); @@ -1131,9 +1192,11 @@ START_TEST(tablet_mode_disable_keyboard_on_resume) litest_keyboard_key(keyboard, KEY_A, false); litest_assert_empty_queue(li); + litest_grab_device(sw); litest_switch_action(sw, LIBINPUT_SWITCH_TABLET_MODE, LIBINPUT_SWITCH_STATE_OFF); + litest_ungrab_device(sw); litest_assert_only_typed_events(li, LIBINPUT_EVENT_SWITCH_TOGGLE); litest_keyboard_key(keyboard, KEY_A, true); @@ -1154,10 +1217,12 @@ START_TEST(tablet_mode_enable_keyboard_on_resume) return; keyboard = litest_add_device(li, LITEST_KEYBOARD); + litest_grab_device(sw); litest_switch_action(sw, LIBINPUT_SWITCH_TABLET_MODE, LIBINPUT_SWITCH_STATE_ON); litest_drain_events(li); + litest_ungrab_device(sw); libinput_suspend(li); litest_drain_events(li); @@ -1204,6 +1269,7 @@ START_TEST(tablet_mode_disable_trackpoint) litest_event(trackpoint, EV_SYN, SYN_REPORT, 0); litest_assert_only_typed_events(li, LIBINPUT_EVENT_POINTER_MOTION); + litest_grab_device(sw); litest_switch_action(sw, LIBINPUT_SWITCH_TABLET_MODE, LIBINPUT_SWITCH_STATE_ON); @@ -1225,6 +1291,7 @@ START_TEST(tablet_mode_disable_trackpoint) litest_event(trackpoint, EV_REL, REL_Y, -1); litest_event(trackpoint, EV_SYN, SYN_REPORT, 0); litest_assert_only_typed_events(li, LIBINPUT_EVENT_POINTER_MOTION); + litest_ungrab_device(sw); litest_delete_device(trackpoint); } @@ -1239,6 +1306,7 @@ START_TEST(tablet_mode_disable_trackpoint_on_init) if (!switch_has_tablet_mode(sw)) return; + litest_grab_device(sw); litest_switch_action(sw, LIBINPUT_SWITCH_TABLET_MODE, LIBINPUT_SWITCH_STATE_ON); @@ -1264,6 +1332,7 @@ START_TEST(tablet_mode_disable_trackpoint_on_init) litest_event(trackpoint, EV_REL, REL_Y, -1); litest_event(trackpoint, EV_SYN, SYN_REPORT, 0); litest_assert_only_typed_events(li, LIBINPUT_EVENT_POINTER_MOTION); + litest_ungrab_device(sw); litest_delete_device(trackpoint); } @@ -1279,11 +1348,13 @@ START_TEST(dock_toggle) litest_drain_events(li); + litest_grab_device(sw); litest_event(sw, EV_SW, SW_DOCK, 1); libinput_dispatch(li); litest_event(sw, EV_SW, SW_DOCK, 0); libinput_dispatch(li); + litest_ungrab_device(sw); litest_assert_empty_queue(li); } |