From 3aa5426cad132792321c2cd370b9f9c156f7e901 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Thu, 30 May 2019 21:27:41 +0200 Subject: [PATCH] Add unit tests for control events serialization Add missing tests for serialization and deserialization of control events. --- app/tests/test_control_event_serialize.c | 48 ++++++++++++ .../scrcpy/ControlEventReaderTest.java | 77 +++++++++++++++++++ 2 files changed, 125 insertions(+) diff --git a/app/tests/test_control_event_serialize.c b/app/tests/test_control_event_serialize.c index 0818ea50..6a7720bc 100644 --- a/app/tests/test_control_event_serialize.c +++ b/app/tests/test_control_event_serialize.c @@ -133,11 +133,59 @@ static void test_serialize_scroll_event(void) { assert(!memcmp(buf, expected, sizeof(expected))); } +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))); +} + int main(void) { test_serialize_keycode_event(); test_serialize_text_event(); test_serialize_long_text_event(); test_serialize_mouse_event(); test_serialize_scroll_event(); + test_serialize_back_or_screen_on_event(); + test_serialize_expand_notification_panel_event(); + test_serialize_collapse_notification_panel_event(); return 0; } diff --git a/server/src/test/java/com/genymobile/scrcpy/ControlEventReaderTest.java b/server/src/test/java/com/genymobile/scrcpy/ControlEventReaderTest.java index 3e97096f..8f0724ff 100644 --- a/server/src/test/java/com/genymobile/scrcpy/ControlEventReaderTest.java +++ b/server/src/test/java/com/genymobile/scrcpy/ControlEventReaderTest.java @@ -97,6 +97,83 @@ public class ControlEventReaderTest { Assert.assertEquals(KeyEvent.META_CTRL_ON, event.getMetaState()); } + @Test + @SuppressWarnings("checkstyle:MagicNumber") + public void testParseScrollEvent() throws IOException { + ControlEventReader reader = new ControlEventReader(); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + DataOutputStream dos = new DataOutputStream(bos); + dos.writeByte(ControlEvent.TYPE_SCROLL); + dos.writeInt(260); + dos.writeInt(1026); + dos.writeShort(1080); + dos.writeShort(1920); + dos.writeInt(1); + dos.writeInt(-1); + + byte[] packet = bos.toByteArray(); + + reader.readFrom(new ByteArrayInputStream(packet)); + ControlEvent event = reader.next(); + + Assert.assertEquals(ControlEvent.TYPE_SCROLL, event.getType()); + Assert.assertEquals(260, event.getPosition().getPoint().getX()); + Assert.assertEquals(1026, event.getPosition().getPoint().getY()); + Assert.assertEquals(1080, event.getPosition().getScreenSize().getWidth()); + Assert.assertEquals(1920, event.getPosition().getScreenSize().getHeight()); + Assert.assertEquals(1, event.getHScroll()); + Assert.assertEquals(-1, event.getVScroll()); + } + + @Test + public void testParseBackOrScreenOnEvent() throws IOException { + ControlEventReader reader = new ControlEventReader(); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + DataOutputStream dos = new DataOutputStream(bos); + dos.writeByte(ControlEvent.TYPE_BACK_OR_SCREEN_ON); + + byte[] packet = bos.toByteArray(); + + reader.readFrom(new ByteArrayInputStream(packet)); + ControlEvent event = reader.next(); + + Assert.assertEquals(ControlEvent.TYPE_BACK_OR_SCREEN_ON, event.getType()); + } + + @Test + public void testParseExpandNotificationPanelEvent() throws IOException { + ControlEventReader reader = new ControlEventReader(); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + DataOutputStream dos = new DataOutputStream(bos); + dos.writeByte(ControlEvent.TYPE_EXPAND_NOTIFICATION_PANEL); + + byte[] packet = bos.toByteArray(); + + reader.readFrom(new ByteArrayInputStream(packet)); + ControlEvent event = reader.next(); + + Assert.assertEquals(ControlEvent.TYPE_EXPAND_NOTIFICATION_PANEL, event.getType()); + } + + @Test + public void testParseCollapseNotificationPanelEvent() throws IOException { + ControlEventReader reader = new ControlEventReader(); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + DataOutputStream dos = new DataOutputStream(bos); + dos.writeByte(ControlEvent.TYPE_COLLAPSE_NOTIFICATION_PANEL); + + byte[] packet = bos.toByteArray(); + + reader.readFrom(new ByteArrayInputStream(packet)); + ControlEvent event = reader.next(); + + Assert.assertEquals(ControlEvent.TYPE_COLLAPSE_NOTIFICATION_PANEL, event.getType()); + } + @Test public void testMultiEvents() throws IOException { ControlEventReader reader = new ControlEventReader();