Skip to content

Commit

Permalink
event: Add a simple test
Browse files Browse the repository at this point in the history
Add a test for event registration and activation.

Signed-off-by: Simon Glass <[email protected]>
  • Loading branch information
sjg20 authored and trini committed Mar 10, 2022
1 parent 87a5d1b commit 7d02645
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions MAINTAINERS
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,7 @@ M: Simon Glass <[email protected]>
S: Maintained
F: common/event.c
F: include/event.h
F: test/common/event.c

FASTBOOT
S: Orphaned
Expand Down
1 change: 1 addition & 0 deletions test/common/Makefile
Original file line number Diff line number Diff line change
@@ -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
47 changes: 47 additions & 0 deletions test/common/event.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Unit tests for event handling
*
* Copyright 2021 Google LLC
* Written by Simon Glass <[email protected]>
*/

#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);
4 changes: 4 additions & 0 deletions test/test-main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 7d02645

Please sign in to comment.