From c16ab8e531c8b7cbe3c368c6864f799d00ddb519 Mon Sep 17 00:00:00 2001 From: dyamy-lee Date: Wed, 21 Apr 2021 16:48:43 +0900 Subject: [PATCH] Implements KeyboardEventCheckTest for libinput haltests It's same way of checking MouseEventCheckTest. For test, get lists of libinput devices and check validation of created keyboard event. It generates libinput keyboard event and saves sequencely. For example, key down, up, key down up. It checkes devices capability which is keyboard, and checkes matching of events. If one of them checked successfully same events as created events, other keyboard devices can be skipped. Change-Id: I8d20d9c324331c759bcce8a14096819b681e36ff --- haltests/test_hal_libinput.cpp | 3 +- haltests/test_hal_libinput_eventgen.cpp | 44 ++++++++++++ haltests/test_hal_libinput_eventgen.h | 1 + haltests/test_hal_libinput_info.cpp | 96 +++++++++++++++++++++++++ haltests/test_hal_libinput_info.h | 1 + 5 files changed, 144 insertions(+), 1 deletion(-) diff --git a/haltests/test_hal_libinput.cpp b/haltests/test_hal_libinput.cpp index 2001f7ec..217220fb 100644 --- a/haltests/test_hal_libinput.cpp +++ b/haltests/test_hal_libinput.cpp @@ -54,7 +54,8 @@ TEST_F(LibInputHalTest, MouseEventCheckTest) TEST_F(LibInputHalTest, KeyboardEventCheckTest) { - EXPECT_TRUE(true); + EXPECT_TRUE(handle_libinput_add_event(li) != -1); + EXPECT_TRUE(validate_keyboard_event(li)); } TEST_F(LibInputHalTest, TouchEventCheckTest) diff --git a/haltests/test_hal_libinput_eventgen.cpp b/haltests/test_hal_libinput_eventgen.cpp index c0741c81..17d3377d 100644 --- a/haltests/test_hal_libinput_eventgen.cpp +++ b/haltests/test_hal_libinput_eventgen.cpp @@ -71,6 +71,11 @@ static void _pointer_gen_y(int value) _write_event_to_device_node(EV_REL, REL_Y, value); } +static void _key_gen(int keycode, int value) +{ + _write_event_to_device_node(EV_KEY, keycode, value); +} + static void _button_gen_down(int button) { if(button <3) @@ -96,6 +101,18 @@ static void _pointer_gen_move(int x, int y) _sync_gen(); } +static void _key_gen_down(int key_code) +{ + _key_gen(key_code, 1); + _sync_gen(); +} + +static void _key_gen_up(int key_code) +{ + _key_gen(key_code, 0); + _sync_gen(); +} + static void _input_mousegen(int button, int x, int y, int mouse_state) { switch(mouse_state) { @@ -127,6 +144,24 @@ static void _input_mousegen(int button, int x, int y, int mouse_state) } } +static void _input_keygen(int key_code, int key_state) +{ + switch(key_state) { + case EVENT_STATE_PRESS: + _key_gen_down(key_code); + break; + case EVENT_STATE_RELEASE: + _key_gen_up(key_code); + break; + case EVENT_STATE_ALL: + _key_gen_down(key_code); + _key_gen_up(key_code); + break; + default: + return; + } +} + int input_mouse_event_gen(int _fd, int button, int x, int y, int state) { if (_fd < 0) return -1; @@ -135,3 +170,12 @@ int input_mouse_event_gen(int _fd, int button, int x, int y, int state) return 0; } + +int input_keyboard_event_gen(int _fd, int code, int state) +{ + if(_fd < 0) return -1; + fd = _fd; + _input_keygen(code, state); + + return 0; +} diff --git a/haltests/test_hal_libinput_eventgen.h b/haltests/test_hal_libinput_eventgen.h index 9f787b83..0b0c37ff 100644 --- a/haltests/test_hal_libinput_eventgen.h +++ b/haltests/test_hal_libinput_eventgen.h @@ -36,6 +36,7 @@ typedef enum } Button; int input_mouse_event_gen(int _fd, int button, int x, int y, int state); +int input_keyboard_event_gen(int _fd, int code, int state); #ifdef __cplusplus } diff --git a/haltests/test_hal_libinput_info.cpp b/haltests/test_hal_libinput_info.cpp index 214195f7..35d74f71 100644 --- a/haltests/test_hal_libinput_info.cpp +++ b/haltests/test_hal_libinput_info.cpp @@ -219,6 +219,33 @@ handle_pointer_button_event(struct libinput_event *ev) static int handle_key_event(struct libinput_event *ev) { + struct libinput_event_keyboard *k = libinput_event_get_keyboard_event(ev); + enum libinput_key_state state; + uint32_t key; + const char *keyname; + enum libinput_event_type type; + + switch(libinput_event_get_type(ev)) { + case LIBINPUT_EVENT_KEYBOARD_KEY: + type = LIBINPUT_EVENT_KEYBOARD_KEY; + break; + default: + abort(); + } + + state = libinput_event_keyboard_get_key_state(k); + key = libinput_event_keyboard_get_key(k); + + keyname = libevdev_event_code_get_name(EV_KEY, key); + keyname = keyname ? keyname : "???"; + + LOG("%s (%d) %s\n", keyname, key, state == LIBINPUT_KEY_STATE_PRESSED ? "pressed" : "released"); + LOG("type = %d, key = %d, state = %d\n", queue_record_event[pop_idx].k.event_type, queue_record_event[pop_idx].k.key, + queue_record_event[pop_idx].k.state); + + EXPECT_EQ(queue_record_event[pop_idx].k.event_type, type); + EXPECT_EQ(queue_record_event[pop_idx].k.key, key); + EXPECT_EQ(queue_record_event[pop_idx].k.state, state); return 0; } @@ -369,6 +396,48 @@ int create_mouse_event(struct libinput *li, int idx) return 0; } +int create_keyboard_event(struct libinput *li, int idx) +{ + int fd = -1; + int n = 0; + + struct record_event rc_e; + struct record_libinput_event_keyboard k[] = { + {LIBINPUT_EVENT_KEYBOARD_KEY, 1, 1}, // keycode 1, press + {LIBINPUT_EVENT_KEYBOARD_KEY, 1, 0}, // keycode 1, release + {LIBINPUT_EVENT_KEYBOARD_KEY, KEY_A, 1}, // keycode KEY_A, press + {LIBINPUT_EVENT_KEYBOARD_KEY, KEY_A, 0}, // keycode KEY_A, release + }; + + if(devices[idx].cap == LIBINPUT_DEVICE_CAP_KEYBOARD){ + LOG("create_keyboard_event\n"); + fd = open(devices[idx].path, O_RDWR); + if (fd < 0) { + LOGE("ERROR: could not open device\n"); + return -1; + } + + n = sizeof(k)/sizeof(struct record_libinput_event_keyboard); + rc_e.device = LIBINPUT_DEVICE_CAP_KEYBOARD; + for(int i=0; i