Skip to content

Commit

Permalink
Add umockdev_testbed_load_evemu_events_from_string()
Browse files Browse the repository at this point in the history
This makes it more comfortable to deal with lots of small
hand-crafted evemu events.
  • Loading branch information
martinpitt committed Dec 26, 2024
1 parent fd56a35 commit 84544e9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
24 changes: 19 additions & 5 deletions src/umockdev.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
"<string>");
}

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;
Expand All @@ -1188,15 +1202,15 @@ 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;
}

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));
Expand All @@ -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;
}

Expand Down
31 changes: 20 additions & 11 deletions tests/test-umockdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 84544e9

Please sign in to comment.