summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Glass <sjg@chromium.org>2022-03-04 18:43:01 +0300
committerTom Rini <trini@konsulko.com>2022-03-10 16:28:36 +0300
commit7d02645fe4c07efe253f68d2d6134922e7c5323e (patch)
treefc3a8a8a4af25118b174deafd2d1ce25887ab9c8
parent87a5d1b5d012b0663517bfa36f5e01c8028f121a (diff)
downloadu-boot-7d02645fe4c07efe253f68d2d6134922e7c5323e.tar.xz
event: Add a simple test
Add a test for event registration and activation. Signed-off-by: Simon Glass <sjg@chromium.org>
-rw-r--r--MAINTAINERS1
-rw-r--r--test/common/Makefile1
-rw-r--r--test/common/event.c47
-rw-r--r--test/test-main.c4
4 files changed, 53 insertions, 0 deletions
diff --git a/MAINTAINERS b/MAINTAINERS
index b534ad66b9..2786adad4b 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -814,6 +814,7 @@ M: Simon Glass <sjg@chromium.org>
S: Maintained
F: common/event.c
F: include/event.h
+F: test/common/event.c
FASTBOOT
S: Orphaned
diff --git a/test/common/Makefile b/test/common/Makefile
index 24c9145dcc..9087788ba6 100644
--- a/test/common/Makefile
+++ b/test/common/Makefile
@@ -1,3 +1,4 @@
# SPDX-License-Identifier: GPL-2.0+
obj-y += cmd_ut_common.o
obj-$(CONFIG_AUTOBOOT) += test_autoboot.o
+obj-$(CONFIG_EVENT) += event.o
diff --git a/test/common/event.c b/test/common/event.c
new file mode 100644
index 0000000000..dfaa66ea49
--- /dev/null
+++ b/test/common/event.c
@@ -0,0 +1,47 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Unit tests for event handling
+ *
+ * Copyright 2021 Google LLC
+ * Written by Simon Glass <sjg@chromium.org>
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <event.h>
+#include <test/common.h>
+#include <test/test.h>
+#include <test/ut.h>
+
+struct test_state {
+ struct udevice *dev;
+ int val;
+};
+
+static int h_adder(void *ctx, struct event *event)
+{
+ struct event_data_test *data = &event->data.test;
+ struct test_state *test_state = ctx;
+
+ test_state->val += data->signal;
+
+ return 0;
+}
+
+static int test_event_base(struct unit_test_state *uts)
+{
+ struct test_state state;
+ int signal;
+
+ state.val = 12;
+ ut_assertok(event_register("wibble", EVT_TEST, h_adder, &state));
+
+ signal = 17;
+
+ /* Check that the handler is called */
+ ut_assertok(event_notify(EVT_TEST, &signal, sizeof(signal)));
+ ut_asserteq(12 + 17, state.val);
+
+ return 0;
+}
+COMMON_TEST(test_event_base, 0);
diff --git a/test/test-main.c b/test/test-main.c
index 8fcb02ecea..ee38d1faea 100644
--- a/test/test-main.c
+++ b/test/test-main.c
@@ -7,6 +7,7 @@
#include <common.h>
#include <console.h>
#include <dm.h>
+#include <event.h>
#include <dm/root.h>
#include <dm/test.h>
#include <dm/uclass-internal.h>
@@ -218,6 +219,8 @@ static int dm_test_restore(struct device_node *of_root)
*/
static int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
{
+ ut_assertok(event_init());
+
if (test->flags & UT_TESTF_DM)
ut_assertok(dm_test_pre_run(uts));
@@ -260,6 +263,7 @@ static int test_post_run(struct unit_test_state *uts, struct unit_test *test)
ut_unsilence_console(uts);
if (test->flags & UT_TESTF_DM)
ut_assertok(dm_test_post_run(uts));
+ ut_assertok(event_uninit());
return 0;
}