2018-01-19 23:55:39 +08:00
|
|
|
#include <assert.h>
|
2018-05-13 21:33:13 +08:00
|
|
|
#include <string.h>
|
2018-01-19 23:55:39 +08:00
|
|
|
|
2018-08-15 23:01:54 +08:00
|
|
|
#include "control_event.h"
|
2018-01-19 23:55:39 +08:00
|
|
|
|
2018-10-04 23:03:24 +08:00
|
|
|
static void test_serialize_keycode_event(void) {
|
2018-01-19 23:55:39 +08:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-05-29 14:35:28 +08:00
|
|
|
unsigned char buf[CONTROL_EVENT_SERIALIZED_MAX_SIZE];
|
2018-01-19 23:55:39 +08:00
|
|
|
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)));
|
|
|
|
}
|
|
|
|
|
2018-10-04 23:03:24 +08:00
|
|
|
static void test_serialize_text_event(void) {
|
2018-01-19 23:55:39 +08:00
|
|
|
struct control_event event = {
|
|
|
|
.type = CONTROL_EVENT_TYPE_TEXT,
|
|
|
|
.text_event = {
|
|
|
|
.text = "hello, world!",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-05-29 14:35:28 +08:00
|
|
|
unsigned char buf[CONTROL_EVENT_SERIALIZED_MAX_SIZE];
|
2018-01-19 23:55:39 +08:00
|
|
|
int size = control_event_serialize(&event, buf);
|
2018-03-10 04:47:14 +08:00
|
|
|
assert(size == 16);
|
2018-01-19 23:55:39 +08:00
|
|
|
|
|
|
|
const unsigned char expected[] = {
|
2019-05-31 03:46:25 +08:00
|
|
|
0x01, // CONTROL_EVENT_TYPE_TEXT
|
2018-03-10 04:47:14 +08:00
|
|
|
0x00, 0x0d, // text length
|
2018-01-19 23:55:39 +08:00
|
|
|
'h', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!', // text
|
|
|
|
};
|
|
|
|
assert(!memcmp(buf, expected, sizeof(expected)));
|
|
|
|
}
|
|
|
|
|
2018-10-04 23:03:24 +08:00
|
|
|
static void test_serialize_long_text_event(void) {
|
2018-03-10 04:47:14 +08:00
|
|
|
struct control_event event;
|
|
|
|
event.type = CONTROL_EVENT_TYPE_TEXT;
|
2019-05-29 14:35:28 +08:00
|
|
|
char text[CONTROL_EVENT_TEXT_MAX_LENGTH + 1];
|
2018-03-10 04:47:14 +08:00
|
|
|
memset(text, 'a', sizeof(text));
|
2019-05-29 14:35:28 +08:00
|
|
|
text[CONTROL_EVENT_TEXT_MAX_LENGTH] = '\0';
|
2018-03-10 04:47:14 +08:00
|
|
|
event.text_event.text = text;
|
|
|
|
|
2019-05-29 14:35:28 +08:00
|
|
|
unsigned char buf[CONTROL_EVENT_SERIALIZED_MAX_SIZE];
|
2018-03-10 04:47:14 +08:00
|
|
|
int size = control_event_serialize(&event, buf);
|
2019-05-29 14:35:28 +08:00
|
|
|
assert(size == 3 + CONTROL_EVENT_TEXT_MAX_LENGTH);
|
2018-03-10 04:47:14 +08:00
|
|
|
|
2019-05-29 14:35:28 +08:00
|
|
|
unsigned char expected[3 + CONTROL_EVENT_TEXT_MAX_LENGTH];
|
2018-03-10 04:47:14 +08:00
|
|
|
expected[0] = 0x01; // CONTROL_EVENT_TYPE_KEYCODE
|
|
|
|
expected[1] = 0x01;
|
|
|
|
expected[2] = 0x2c; // text length (16 bits)
|
2019-05-29 14:35:28 +08:00
|
|
|
memset(&expected[3], 'a', CONTROL_EVENT_TEXT_MAX_LENGTH);
|
2018-03-10 04:47:14 +08:00
|
|
|
|
|
|
|
assert(!memcmp(buf, expected, sizeof(expected)));
|
|
|
|
}
|
|
|
|
|
2018-10-04 23:03:24 +08:00
|
|
|
static void test_serialize_mouse_event(void) {
|
2018-01-19 23:55:39 +08:00
|
|
|
struct control_event event = {
|
|
|
|
.type = CONTROL_EVENT_TYPE_MOUSE,
|
|
|
|
.mouse_event = {
|
|
|
|
.action = AMOTION_EVENT_ACTION_DOWN,
|
|
|
|
.buttons = AMOTION_EVENT_BUTTON_PRIMARY,
|
2018-01-29 21:52:22 +08:00
|
|
|
.position = {
|
|
|
|
.point = {
|
2018-01-22 23:50:08 +08:00
|
|
|
.x = 260,
|
|
|
|
.y = 1026,
|
|
|
|
},
|
|
|
|
.screen_size = {
|
|
|
|
.width = 1080,
|
|
|
|
.height = 1920,
|
|
|
|
},
|
|
|
|
},
|
2018-01-19 23:55:39 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-05-29 14:35:28 +08:00
|
|
|
unsigned char buf[CONTROL_EVENT_SERIALIZED_MAX_SIZE];
|
2018-01-19 23:55:39 +08:00
|
|
|
int size = control_event_serialize(&event, buf);
|
2019-01-26 20:04:39 +08:00
|
|
|
assert(size == 18);
|
2018-01-19 23:55:39 +08:00
|
|
|
|
|
|
|
const unsigned char expected[] = {
|
|
|
|
0x02, // CONTROL_EVENT_TYPE_MOUSE
|
|
|
|
0x00, // AKEY_EVENT_ACTION_DOWN
|
|
|
|
0x00, 0x00, 0x00, 0x01, // AMOTION_EVENT_BUTTON_PRIMARY
|
2019-01-26 20:04:39 +08:00
|
|
|
0x00, 0x00, 0x01, 0x04, 0x00, 0x00, 0x04, 0x02, // 260 1026
|
2018-01-22 23:50:08 +08:00
|
|
|
0x04, 0x38, 0x07, 0x80, // 1080 1920
|
2018-01-19 23:55:39 +08:00
|
|
|
};
|
|
|
|
assert(!memcmp(buf, expected, sizeof(expected)));
|
|
|
|
}
|
|
|
|
|
2018-10-04 23:03:24 +08:00
|
|
|
static void test_serialize_scroll_event(void) {
|
2018-01-19 23:55:39 +08:00
|
|
|
struct control_event event = {
|
|
|
|
.type = CONTROL_EVENT_TYPE_SCROLL,
|
|
|
|
.scroll_event = {
|
2018-01-29 21:52:22 +08:00
|
|
|
.position = {
|
|
|
|
.point = {
|
2018-01-22 23:50:08 +08:00
|
|
|
.x = 260,
|
|
|
|
.y = 1026,
|
|
|
|
},
|
|
|
|
.screen_size = {
|
|
|
|
.width = 1080,
|
|
|
|
.height = 1920,
|
|
|
|
},
|
|
|
|
},
|
2018-01-19 23:55:39 +08:00
|
|
|
.hscroll = 1,
|
|
|
|
.vscroll = -1,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-05-29 14:35:28 +08:00
|
|
|
unsigned char buf[CONTROL_EVENT_SERIALIZED_MAX_SIZE];
|
2018-01-19 23:55:39 +08:00
|
|
|
int size = control_event_serialize(&event, buf);
|
2019-01-26 20:04:39 +08:00
|
|
|
assert(size == 21);
|
2018-01-19 23:55:39 +08:00
|
|
|
|
|
|
|
const unsigned char expected[] = {
|
|
|
|
0x03, // CONTROL_EVENT_TYPE_SCROLL
|
2019-01-26 20:04:39 +08:00
|
|
|
0x00, 0x00, 0x01, 0x04, 0x00, 0x00, 0x04, 0x02, // 260 1026
|
2018-01-22 23:50:08 +08:00
|
|
|
0x04, 0x38, 0x07, 0x80, // 1080 1920
|
2018-01-19 23:55:39 +08:00
|
|
|
0x00, 0x00, 0x00, 0x01, // 1
|
|
|
|
0xFF, 0xFF, 0xFF, 0xFF, // -1
|
|
|
|
};
|
|
|
|
assert(!memcmp(buf, expected, sizeof(expected)));
|
|
|
|
}
|
|
|
|
|
2019-05-31 03:27:41 +08:00
|
|
|
static void test_serialize_back_or_screen_on_event(void) {
|
|
|
|
struct control_event event = {
|
|
|
|
.type = CONTROL_EVENT_TYPE_BACK_OR_SCREEN_ON,
|
|
|
|
};
|
|
|
|
|
|
|
|
unsigned char buf[CONTROL_EVENT_SERIALIZED_MAX_SIZE];
|
|
|
|
int size = control_event_serialize(&event, buf);
|
|
|
|
assert(size == 1);
|
|
|
|
|
|
|
|
const unsigned char expected[] = {
|
|
|
|
0x04, // CONTROL_EVENT_TYPE_BACK_OR_SCREEN_ON
|
|
|
|
};
|
|
|
|
assert(!memcmp(buf, expected, sizeof(expected)));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_serialize_expand_notification_panel_event(void) {
|
|
|
|
struct control_event event = {
|
|
|
|
.type = CONTROL_EVENT_TYPE_EXPAND_NOTIFICATION_PANEL,
|
|
|
|
};
|
|
|
|
|
|
|
|
unsigned char buf[CONTROL_EVENT_SERIALIZED_MAX_SIZE];
|
|
|
|
int size = control_event_serialize(&event, buf);
|
|
|
|
assert(size == 1);
|
|
|
|
|
|
|
|
const unsigned char expected[] = {
|
|
|
|
0x05, // CONTROL_EVENT_TYPE_EXPAND_NOTIFICATION_PANEL
|
|
|
|
};
|
|
|
|
assert(!memcmp(buf, expected, sizeof(expected)));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_serialize_collapse_notification_panel_event(void) {
|
|
|
|
struct control_event event = {
|
|
|
|
.type = CONTROL_EVENT_TYPE_COLLAPSE_NOTIFICATION_PANEL,
|
|
|
|
};
|
|
|
|
|
|
|
|
unsigned char buf[CONTROL_EVENT_SERIALIZED_MAX_SIZE];
|
|
|
|
int size = control_event_serialize(&event, buf);
|
|
|
|
assert(size == 1);
|
|
|
|
|
|
|
|
const unsigned char expected[] = {
|
|
|
|
0x06, // CONTROL_EVENT_TYPE_COLLAPSE_NOTIFICATION_PANEL
|
|
|
|
};
|
|
|
|
assert(!memcmp(buf, expected, sizeof(expected)));
|
|
|
|
}
|
|
|
|
|
2019-05-30 21:23:01 +08:00
|
|
|
static void test_serialize_get_clipboard_event(void) {
|
|
|
|
struct control_event event = {
|
|
|
|
.type = CONTROL_EVENT_TYPE_GET_CLIPBOARD,
|
|
|
|
};
|
|
|
|
|
|
|
|
unsigned char buf[CONTROL_EVENT_SERIALIZED_MAX_SIZE];
|
|
|
|
int size = control_event_serialize(&event, buf);
|
|
|
|
assert(size == 1);
|
|
|
|
|
|
|
|
const unsigned char expected[] = {
|
|
|
|
0x07, // CONTROL_EVENT_TYPE_GET_CLIPBOARD
|
|
|
|
};
|
|
|
|
assert(!memcmp(buf, expected, sizeof(expected)));
|
|
|
|
}
|
|
|
|
|
2018-10-04 23:03:24 +08:00
|
|
|
int main(void) {
|
2018-01-19 23:55:39 +08:00
|
|
|
test_serialize_keycode_event();
|
|
|
|
test_serialize_text_event();
|
2018-03-10 04:47:14 +08:00
|
|
|
test_serialize_long_text_event();
|
2018-01-19 23:55:39 +08:00
|
|
|
test_serialize_mouse_event();
|
|
|
|
test_serialize_scroll_event();
|
2019-05-31 03:27:41 +08:00
|
|
|
test_serialize_back_or_screen_on_event();
|
|
|
|
test_serialize_expand_notification_panel_event();
|
|
|
|
test_serialize_collapse_notification_panel_event();
|
2019-05-30 21:23:01 +08:00
|
|
|
test_serialize_get_clipboard_event();
|
2019-05-30 03:58:07 +08:00
|
|
|
return 0;
|
2018-01-19 23:55:39 +08:00
|
|
|
}
|