From 84544e9f269a2f1112a9a04321337515b181b2dd Mon Sep 17 00:00:00 2001 From: Martin Pitt Date: Sat, 21 Dec 2024 12:29:51 +0100 Subject: [PATCH] Add umockdev_testbed_load_evemu_events_from_string() This makes it more comfortable to deal with lots of small hand-crafted evemu events. --- src/umockdev.vala | 24 +++++++++++++++++++----- tests/test-umockdev.c | 31 ++++++++++++++++++++----------- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/src/umockdev.vala b/src/umockdev.vala index 2dfda20..bb3c315 100644 --- a/src/umockdev.vala +++ b/src/umockdev.vala @@ -1174,8 +1174,22 @@ public class Testbed: GLib.Object { public bool load_evemu_events (string? dev, string eventsfile) throws GLib.Error, FileError, IOError, RegexError { - File f_ev = File.new_for_path(eventsfile); - var s_ev = new DataInputStream(f_ev.read()); + return this.load_evemu_events_from_stream(dev, + new DataInputStream(File.new_for_path(eventsfile).read()), + eventsfile); + } + + public bool load_evemu_events_from_string (string? dev, string events) + throws GLib.Error, FileError, IOError, RegexError + { + return this.load_evemu_events_from_stream(dev, + new DataInputStream(new MemoryInputStream.from_data(events.data)), + ""); + } + + private bool load_evemu_events_from_stream (string? dev, DataInputStream events, string eventsname) + throws GLib.Error, IOError, RegexError + { string line; string? recorded_dev = null; size_t len; @@ -1188,7 +1202,7 @@ public class Testbed: GLib.Object { int delay = 0; bool first = true; - while ((line = s_ev.read_line(out len)) != null) { + while ((line = events.read_line(out len)) != null) { if (default_dev_re.match(line, 0, out match)) { recorded_dev = match.fetch(1); continue; @@ -1196,7 +1210,7 @@ public class Testbed: GLib.Object { if (!event_re.match(line, 0, out match)) { if (!line.has_prefix("#")) - warning("Ignoring invalid line in %s: %s", eventsfile, line); + warning("Ignoring invalid line in %s: %s", eventsname, line); continue; } time_t ev_sec = (time_t) uint64.parse(match.fetch(1)); @@ -1223,7 +1237,7 @@ public class Testbed: GLib.Object { string? owned_dev = dev; if (owned_dev == null) { if (recorded_dev == null) - error("null passed for device node, but recording %s has no '# device' header", eventsfile); + error("null passed for device node, but recording %s has no '# device' header", eventsname); owned_dev = recorded_dev; } diff --git a/tests/test-umockdev.c b/tests/test-umockdev.c index 946f9af..01f6de7 100644 --- a/tests/test-umockdev.c +++ b/tests/test-umockdev.c @@ -2022,8 +2022,6 @@ t_testbed_replay_evemu_events(UMockdevTestbedFixture * fixture, UNUSED_DATA) { gboolean success; GError *error = NULL; - int fd; - g_autofree char *tmppath = NULL; struct timeval tv_begin, tv_end; struct input_event ev; static const char* test_data = "E: 1234.500000 0000 0000 0\n" /* SYN */ @@ -2038,20 +2036,13 @@ t_testbed_replay_evemu_events(UMockdevTestbedFixture * fixture, UNUSED_DATA) "E: DEVNAME=/dev/input/event1\nE: SUBSYSTEM=input\n", &error); g_assert_no_error(error); - /* write evemu events file */ - fd = g_file_open_tmp("test_evemu.XXXXXX", &tmppath, &error); - g_assert_no_error(error); - g_assert_cmpint(write(fd, test_data, strlen(test_data)), ==, strlen(test_data)); - close(fd); - /* load it */ - success = umockdev_testbed_load_evemu_events(fixture->testbed, "/dev/input/event1", tmppath, &error); + success = umockdev_testbed_load_evemu_events_from_string(fixture->testbed, "/dev/input/event1", test_data, &error); g_assert_no_error(error); g_assert(success); - g_unlink (tmppath); /* start communication */ - fd = g_open("/dev/input/event1", O_RDONLY, 0); + int fd = g_open("/dev/input/event1", O_RDONLY, 0); g_assert_cmpint(fd, >=, 0); g_assert_cmpint(gettimeofday(&tv_begin, NULL), ==, 0); @@ -2099,6 +2090,24 @@ t_testbed_replay_evemu_events(UMockdevTestbedFixture * fixture, UNUSED_DATA) g_assert_cmpint(ev.value, ==, 1); assert_delta_t(&tv_begin, &tv_end, 550); + /* "flush" the script by waiting for it */ + success = umockdev_testbed_wait_script(fixture->testbed, "/dev/input/event1", &error); + g_assert(success); + g_assert_no_error(error); + + /* can load a new script now */ + success = umockdev_testbed_load_evemu_events_from_string(fixture->testbed, "/dev/input/event1", + "E: 1239.200000 0001 0123 1\n", &error); + g_assert_no_error(error); + g_assert(success); + + g_assert_cmpint(read(fd, &ev, sizeof(ev)), ==, sizeof(ev)); + g_assert_cmpint(ev.input_event_sec, ==, 1239); + g_assert_cmpint(ev.input_event_usec, ==, 200000); + g_assert_cmpint(ev.type, ==, 1); + g_assert_cmpint(ev.code, ==, 0x123); + g_assert_cmpint(ev.value, ==, 1); + close(fd); }