diff --git a/app/meson.build b/app/meson.build index cf619557..f4570234 100644 --- a/app/meson.build +++ b/app/meson.build @@ -35,6 +35,7 @@ executable('scrcpy', src, dependencies: dependencies) tests = [ ['test_control_event_queue', ['tests/test_control_event_queue.c', 'src/controlevent.c']], + ['test_control_event_serialize', ['tests/test_control_event_serialize.c', 'src/controlevent.c']], ['test_strutil', ['tests/test_strutil.c', 'src/strutil.c']], ] diff --git a/app/tests/test_control_event_serialize.c b/app/tests/test_control_event_serialize.c new file mode 100644 index 00000000..fd309156 --- /dev/null +++ b/app/tests/test_control_event_serialize.c @@ -0,0 +1,104 @@ +#include + +#include "controlevent.h" + +static void test_serialize_keycode_event() { + struct control_event event = { + .type = CONTROL_EVENT_TYPE_KEYCODE, + .keycode_event = { + .action = AKEY_EVENT_ACTION_UP, + .keycode = AKEYCODE_ENTER, + .metastate = AMETA_SHIFT_ON | AMETA_SHIFT_LEFT_ON, + }, + }; + + unsigned char buf[SERIALIZED_EVENT_MAX_SIZE]; + int size = control_event_serialize(&event, buf); + assert(size == 10); + + const unsigned char expected[] = { + 0x00, // CONTROL_EVENT_TYPE_KEYCODE + 0x01, // AKEY_EVENT_ACTION_UP + 0x00, 0x00, 0x00, 0x42, // AKEYCODE_ENTER + 0x00, 0x00, 0x00, 0x41, // AMETA_SHIFT_ON | AMETA_SHIFT_LEFT_ON + }; + assert(!memcmp(buf, expected, sizeof(expected))); +} + +static void test_serialize_text_event() { + struct control_event event = { + .type = CONTROL_EVENT_TYPE_TEXT, + .text_event = { + .text = "hello, world!", + }, + }; + + unsigned char buf[SERIALIZED_EVENT_MAX_SIZE]; + int size = control_event_serialize(&event, buf); + assert(size == 15); + + const unsigned char expected[] = { + 0x01, // CONTROL_EVENT_TYPE_KEYCODE + 0x0d, // text length + 'h', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!', // text + }; + assert(!memcmp(buf, expected, sizeof(expected))); +} + +static void test_serialize_mouse_event() { + struct control_event event = { + .type = CONTROL_EVENT_TYPE_MOUSE, + .mouse_event = { + .action = AMOTION_EVENT_ACTION_DOWN, + .buttons = AMOTION_EVENT_BUTTON_PRIMARY, + .x = 260, + .y = 1026, + }, + }; + + unsigned char buf[SERIALIZED_EVENT_MAX_SIZE]; + int size = control_event_serialize(&event, buf); + assert(size == 14); + + const unsigned char expected[] = { + 0x02, // CONTROL_EVENT_TYPE_MOUSE + 0x00, // AKEY_EVENT_ACTION_DOWN + 0x00, 0x00, 0x00, 0x01, // AMOTION_EVENT_BUTTON_PRIMARY + 0x00, 0x00, 0x01, 0x04, // 260 + 0x00, 0x00, 0x04, 0x02, // 1026 + }; + assert(!memcmp(buf, expected, sizeof(expected))); +} + +static void test_serialize_scroll_event() { + struct control_event event = { + .type = CONTROL_EVENT_TYPE_SCROLL, + .scroll_event = { + .x = 260, + .y = 1026, + .hscroll = 1, + .vscroll = -1, + }, + }; + + unsigned char buf[SERIALIZED_EVENT_MAX_SIZE]; + int size = control_event_serialize(&event, buf); + assert(size == 17); + + const unsigned char expected[] = { + 0x03, // CONTROL_EVENT_TYPE_SCROLL + 0x00, 0x00, 0x01, 0x04, // 260 + 0x00, 0x00, 0x04, 0x02, // 1026 + 0x00, 0x00, 0x00, 0x01, // 1 + 0xFF, 0xFF, 0xFF, 0xFF, // -1 + }; + assert(!memcmp(buf, expected, sizeof(expected))); +} + +int main() { + test_serialize_keycode_event(); + test_serialize_text_event(); + test_serialize_mouse_event(); + test_serialize_scroll_event(); + return 0; +}