blob: c0912a3437bb00e4f40a040073161f1df58ba125 [file] [log] [blame]
Simon Glass7d026452022-03-04 08:43:01 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Unit tests for event handling
4 *
5 * Copyright 2021 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
7 */
8
9#include <common.h>
10#include <dm.h>
11#include <event.h>
12#include <test/common.h>
13#include <test/test.h>
14#include <test/ut.h>
15
16struct test_state {
17 struct udevice *dev;
18 int val;
19};
20
Simon Glassba5e3e12023-08-21 21:16:48 -060021static bool called;
22
Simon Glass7d026452022-03-04 08:43:01 -070023static int h_adder(void *ctx, struct event *event)
24{
25 struct event_data_test *data = &event->data.test;
26 struct test_state *test_state = ctx;
27
28 test_state->val += data->signal;
29
30 return 0;
31}
32
Simon Glassba5e3e12023-08-21 21:16:48 -060033static int h_adder_simple(void)
34{
35 called = true;
36
37 return 0;
38}
39EVENT_SPY_SIMPLE(EVT_TEST, h_adder_simple);
40
Simon Glass7d026452022-03-04 08:43:01 -070041static int test_event_base(struct unit_test_state *uts)
42{
43 struct test_state state;
44 int signal;
45
46 state.val = 12;
47 ut_assertok(event_register("wibble", EVT_TEST, h_adder, &state));
48
49 signal = 17;
50
51 /* Check that the handler is called */
52 ut_assertok(event_notify(EVT_TEST, &signal, sizeof(signal)));
53 ut_asserteq(12 + 17, state.val);
54
55 return 0;
56}
57COMMON_TEST(test_event_base, 0);
Simon Glass5b896ed2022-03-04 08:43:03 -070058
Simon Glassba5e3e12023-08-21 21:16:48 -060059static int test_event_simple(struct unit_test_state *uts)
60{
61 called = false;
62
63 /* Check that the handler is called */
64 ut_assertok(event_notify_null(EVT_TEST));
65 ut_assert(called);
66
67 return 0;
68}
69COMMON_TEST(test_event_simple, 0);
70
Simon Glass5b896ed2022-03-04 08:43:03 -070071static int h_probe(void *ctx, struct event *event)
72{
73 struct test_state *test_state = ctx;
74
75 test_state->dev = event->data.dm.dev;
76 switch (event->type) {
77 case EVT_DM_PRE_PROBE:
78 test_state->val |= 1;
79 break;
80 case EVT_DM_POST_PROBE:
81 test_state->val |= 2;
82 break;
83 default:
84 break;
85 }
86
87 return 0;
88}
89
90static int test_event_probe(struct unit_test_state *uts)
91{
92 struct test_state state;
93 struct udevice *dev;
94
95 state.val = 0;
96 ut_assertok(event_register("pre", EVT_DM_PRE_PROBE, h_probe, &state));
97 ut_assertok(event_register("post", EVT_DM_POST_PROBE, h_probe, &state));
98
99 /* Probe a device */
100 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
101
102 /* Check that the handler is called */
103 ut_asserteq(3, state.val);
104
105 return 0;
106}
107COMMON_TEST(test_event_probe, UT_TESTF_DM | UT_TESTF_SCAN_FDT);