blob: b462694fc3b6973f68ad430eca8a09d6515758d7 [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
Simon Glass14f0cc42023-10-01 19:15:20 -060095 if (!IS_ENABLED(SANDBOX))
96 return -EAGAIN;
97
Simon Glass5b896ed2022-03-04 08:43:03 -070098 state.val = 0;
99 ut_assertok(event_register("pre", EVT_DM_PRE_PROBE, h_probe, &state));
100 ut_assertok(event_register("post", EVT_DM_POST_PROBE, h_probe, &state));
101
102 /* Probe a device */
103 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
104
105 /* Check that the handler is called */
106 ut_asserteq(3, state.val);
107
108 return 0;
109}
110COMMON_TEST(test_event_probe, UT_TESTF_DM | UT_TESTF_SCAN_FDT);