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 24, 2024
1 parent dec82e5 commit 3d4fd9a
Show file tree
Hide file tree
Showing 2 changed files with 21 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
13 changes: 2 additions & 11 deletions tests/test-umockdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -2009,8 +2009,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 @@ -2025,20 +2023,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

0 comments on commit 3d4fd9a

Please sign in to comment.