Simon Glass | 7d02645 | 2022-03-04 08:43:01 -0700 | [diff] [blame] | 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 <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 | |
| 16 | struct test_state { |
| 17 | struct udevice *dev; |
| 18 | int val; |
| 19 | }; |
| 20 | |
Simon Glass | ba5e3e1 | 2023-08-21 21:16:48 -0600 | [diff] [blame] | 21 | static bool called; |
| 22 | |
Simon Glass | 7d02645 | 2022-03-04 08:43:01 -0700 | [diff] [blame] | 23 | static 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 Glass | ba5e3e1 | 2023-08-21 21:16:48 -0600 | [diff] [blame] | 33 | static int h_adder_simple(void) |
| 34 | { |
| 35 | called = true; |
| 36 | |
| 37 | return 0; |
| 38 | } |
| 39 | EVENT_SPY_SIMPLE(EVT_TEST, h_adder_simple); |
| 40 | |
Simon Glass | 7d02645 | 2022-03-04 08:43:01 -0700 | [diff] [blame] | 41 | static 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 | } |
| 57 | COMMON_TEST(test_event_base, 0); |
Simon Glass | 5b896ed | 2022-03-04 08:43:03 -0700 | [diff] [blame] | 58 | |
Simon Glass | ba5e3e1 | 2023-08-21 21:16:48 -0600 | [diff] [blame] | 59 | static 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 | } |
| 69 | COMMON_TEST(test_event_simple, 0); |
| 70 | |
Simon Glass | 5b896ed | 2022-03-04 08:43:03 -0700 | [diff] [blame] | 71 | static 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 | |
| 90 | static 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 | } |
| 107 | COMMON_TEST(test_event_probe, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |