1 // 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 <dm.h>
10 #include <event.h>
11 #include <test/common.h>
12 #include <test/test.h>
13 #include <test/ut.h>
14
15 struct test_state {
16 struct udevice *dev;
17 int val;
18 };
19
20 static bool called;
21
h_adder(void * ctx,struct event * event)22 static int h_adder(void *ctx, struct event *event)
23 {
24 struct event_data_test *data = &event->data.test;
25 struct test_state *test_state = ctx;
26
27 test_state->val += data->signal;
28
29 return 0;
30 }
31
h_adder_simple(void)32 static int h_adder_simple(void)
33 {
34 called = true;
35
36 return 0;
37 }
38 EVENT_SPY_SIMPLE(EVT_TEST, h_adder_simple);
39
test_event_base(struct unit_test_state * uts)40 static int test_event_base(struct unit_test_state *uts)
41 {
42 struct test_state state;
43 int signal;
44
45 state.val = 12;
46 ut_assertok(event_register("wibble", EVT_TEST, h_adder, &state));
47
48 signal = 17;
49
50 /* Check that the handler is called */
51 ut_assertok(event_notify(EVT_TEST, &signal, sizeof(signal)));
52 ut_asserteq(12 + 17, state.val);
53
54 return 0;
55 }
56 COMMON_TEST(test_event_base, 0);
57
test_event_simple(struct unit_test_state * uts)58 static int test_event_simple(struct unit_test_state *uts)
59 {
60 called = false;
61
62 /* Check that the handler is called */
63 ut_assertok(event_notify_null(EVT_TEST));
64 ut_assert(called);
65
66 return 0;
67 }
68 COMMON_TEST(test_event_simple, 0);
69
h_probe(void * ctx,struct event * event)70 static int h_probe(void *ctx, struct event *event)
71 {
72 struct test_state *test_state = ctx;
73
74 test_state->dev = event->data.dm.dev;
75 switch (event->type) {
76 case EVT_DM_PRE_PROBE:
77 test_state->val |= 1;
78 break;
79 case EVT_DM_POST_PROBE:
80 test_state->val |= 2;
81 break;
82 default:
83 break;
84 }
85
86 return 0;
87 }
88
test_event_probe(struct unit_test_state * uts)89 static int test_event_probe(struct unit_test_state *uts)
90 {
91 struct test_state state;
92 struct udevice *dev;
93
94 if (!IS_ENABLED(CONFIG_SANDBOX))
95 return -EAGAIN;
96
97 state.val = 0;
98 ut_assertok(event_register("pre", EVT_DM_PRE_PROBE, h_probe, &state));
99 ut_assertok(event_register("post", EVT_DM_POST_PROBE, h_probe, &state));
100
101 /* Probe a device */
102 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
103
104 /* Check that the handler is called */
105 ut_asserteq(3, state.val);
106
107 return 0;
108 }
109 COMMON_TEST(test_event_probe, UTF_DM | UTF_SCAN_FDT);
110