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 | |
| 21 | static int h_adder(void *ctx, struct event *event) |
| 22 | { |
| 23 | struct event_data_test *data = &event->data.test; |
| 24 | struct test_state *test_state = ctx; |
| 25 | |
| 26 | test_state->val += data->signal; |
| 27 | |
| 28 | return 0; |
| 29 | } |
| 30 | |
| 31 | static int test_event_base(struct unit_test_state *uts) |
| 32 | { |
| 33 | struct test_state state; |
| 34 | int signal; |
| 35 | |
| 36 | state.val = 12; |
| 37 | ut_assertok(event_register("wibble", EVT_TEST, h_adder, &state)); |
| 38 | |
| 39 | signal = 17; |
| 40 | |
| 41 | /* Check that the handler is called */ |
| 42 | ut_assertok(event_notify(EVT_TEST, &signal, sizeof(signal))); |
| 43 | ut_asserteq(12 + 17, state.val); |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | COMMON_TEST(test_event_base, 0); |
Simon Glass | 5b896ed | 2022-03-04 08:43:03 -0700 | [diff] [blame] | 48 | |
| 49 | static int h_probe(void *ctx, struct event *event) |
| 50 | { |
| 51 | struct test_state *test_state = ctx; |
| 52 | |
| 53 | test_state->dev = event->data.dm.dev; |
| 54 | switch (event->type) { |
| 55 | case EVT_DM_PRE_PROBE: |
| 56 | test_state->val |= 1; |
| 57 | break; |
| 58 | case EVT_DM_POST_PROBE: |
| 59 | test_state->val |= 2; |
| 60 | break; |
| 61 | default: |
| 62 | break; |
| 63 | } |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | static int test_event_probe(struct unit_test_state *uts) |
| 69 | { |
| 70 | struct test_state state; |
| 71 | struct udevice *dev; |
| 72 | |
| 73 | state.val = 0; |
| 74 | ut_assertok(event_register("pre", EVT_DM_PRE_PROBE, h_probe, &state)); |
| 75 | ut_assertok(event_register("post", EVT_DM_POST_PROBE, h_probe, &state)); |
| 76 | |
| 77 | /* Probe a device */ |
| 78 | ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev)); |
| 79 | |
| 80 | /* Check that the handler is called */ |
| 81 | ut_asserteq(3, state.val); |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | COMMON_TEST(test_event_probe, UT_TESTF_DM | UT_TESTF_SCAN_FDT); |