Implements KeyboardEventCheckTest for libinput haltests
authordyamy-lee <dyamy.lee@samsung.com>
Wed, 21 Apr 2021 07:48:43 +0000 (16:48 +0900)
committerJihoon Kim <jihoon48.kim@samsung.com>
Mon, 4 Dec 2023 10:31:00 +0000 (19:31 +0900)
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
haltests/test_hal_libinput_eventgen.cpp
haltests/test_hal_libinput_eventgen.h
haltests/test_hal_libinput_info.cpp
haltests/test_hal_libinput_info.h

index 2001f7ec35a0cfb8d72357569d5a1ff9e56c0fef..217220fb27cc3936d298caf118a453bcae33e33b 100644 (file)
@@ -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)
index c0741c8133a3516c48c451a397695897e3d3475c..17d3377d6394d0d9cdaae189de0218af9c4bf98b 100644 (file)
@@ -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;
+}
index 9f787b83ecd529a18c22411142f528ee034f4080..0b0c37ff38e564a672e42cec09dab6a374375c66 100644 (file)
@@ -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
 }
index 214195f7faf4fed81d03e45cd7c341fc04edb446..35d74f71b80179952bdb671c1e98eaa39344a121 100644 (file)
@@ -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<n; ++i)
+               {
+                       rc_e.k = k[i];
+
+                       if(input_keyboard_event_gen(fd, k[i].key, k[i].state) < 0) {
+                               LOG("No device\n");
+                               continue;
+                       }
+                       queue_record_event[queue_idx++] = rc_e;
+               }
+
+               libinput_dispatch(li);
+               close(fd);
+       }
+       else
+               return -1;
+       return 0;
+}
+
 bool validate_mouse_event(struct libinput *li)
 {
        bool val = false;
@@ -395,3 +464,30 @@ bool validate_mouse_event(struct libinput *li)
        }
        return val;
 }
+
+bool validate_keyboard_event(struct libinput *li)
+{
+       bool val = false;
+       bool checked = false;
+       int ret = 0;
+
+       for(int i=0; i< devices_cnt; ++i) {
+               if(devices[i].cap == LIBINPUT_DEVICE_CAP_KEYBOARD) {
+                       LOG("LIBINPUT_DEVICE_CAP_KEYBOARD, path = %s\n", devices[i].path);
+                       checked = true;
+                       queue_idx = 0;
+                       pop_idx = -1;
+                       create_keyboard_event(li, i);
+                       ret = handle_libinput_event(li);
+                       if(queue_idx == (pop_idx+1) && !ret) {
+                               val = true;
+                       }
+               }
+               if(val) return true;
+       }
+       if(!val && !checked){
+               LOG("No Devices\n");
+               return true;
+       }
+       return val;
+}
index b0b99c07833d1847ba269859b2b6115a03e6e500..86c8ca8b779019cd05bd23739d8b5fa8aec7143a 100644 (file)
@@ -41,6 +41,7 @@ extern "C" {
 int handle_libinput_add_event(struct libinput *li);
 
 bool validate_mouse_event(struct libinput *li);
+bool validate_keyboard_event(struct libinput *li);
 
 struct record_device {
        enum libinput_device_capability cap;