blob: de433d34f22b0452c7c734ecd4ffe6b53b72adf0 [file] [log] [blame]
Simon Glassa128b1b2022-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
Simon Glassa128b1b2022-03-04 08:43:01 -07009#include <dm.h>
10#include <event.h>
11#include <test/common.h>
12#include <test/test.h>
13#include <test/ut.h>
14
15struct test_state {
16 struct udevice *dev;
17 int val;
18};
19
Simon Glass3bfbedd2023-08-21 21:16:48 -060020static bool called;
21
Simon Glassa128b1b2022-03-04 08:43:01 -070022static 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
Simon Glass3bfbedd2023-08-21 21:16:48 -060032static int h_adder_simple(void)
33{
34 called = true;
35
36 return 0;
37}
38EVENT_SPY_SIMPLE(EVT_TEST, h_adder_simple);
39
Simon Glassa128b1b2022-03-04 08:43:01 -070040static 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}
56COMMON_TEST(test_event_base, 0);
Simon Glass3ee337a2022-03-04 08:43:03 -070057
Simon Glass3bfbedd2023-08-21 21:16:48 -060058static 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}
68COMMON_TEST(test_event_simple, 0);
69
Simon Glass3ee337a2022-03-04 08:43:03 -070070static 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
89static int test_event_probe(struct unit_test_state *uts)
90{
91 struct test_state state;
92 struct udevice *dev;
93
Simon Glass127e9462023-10-01 19:15:20 -060094 if (!IS_ENABLED(SANDBOX))
95 return -EAGAIN;
96
Simon Glass3ee337a2022-03-04 08:43:03 -070097 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}
109COMMON_TEST(test_event_probe, UT_TESTF_DM | UT_TESTF_SCAN_FDT);