event.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #include <common.h>
  9. #include <dm.h>
  10. #include <event.h>
  11. #include <test/common.h>
  12. #include <test/test.h>
  13. #include <test/ut.h>
  14. struct test_state {
  15. struct udevice *dev;
  16. int val;
  17. };
  18. static int h_adder(void *ctx, struct event *event)
  19. {
  20. struct event_data_test *data = &event->data.test;
  21. struct test_state *test_state = ctx;
  22. test_state->val += data->signal;
  23. return 0;
  24. }
  25. static int test_event_base(struct unit_test_state *uts)
  26. {
  27. struct test_state state;
  28. int signal;
  29. state.val = 12;
  30. ut_assertok(event_register("wibble", EVT_TEST, h_adder, &state));
  31. signal = 17;
  32. /* Check that the handler is called */
  33. ut_assertok(event_notify(EVT_TEST, &signal, sizeof(signal)));
  34. ut_asserteq(12 + 17, state.val);
  35. return 0;
  36. }
  37. COMMON_TEST(test_event_base, 0);
  38. static int h_probe(void *ctx, struct event *event)
  39. {
  40. struct test_state *test_state = ctx;
  41. test_state->dev = event->data.dm.dev;
  42. switch (event->type) {
  43. case EVT_DM_PRE_PROBE:
  44. test_state->val |= 1;
  45. break;
  46. case EVT_DM_POST_PROBE:
  47. test_state->val |= 2;
  48. break;
  49. default:
  50. break;
  51. }
  52. return 0;
  53. }
  54. static int test_event_probe(struct unit_test_state *uts)
  55. {
  56. struct test_state state;
  57. struct udevice *dev;
  58. state.val = 0;
  59. ut_assertok(event_register("pre", EVT_DM_PRE_PROBE, h_probe, &state));
  60. ut_assertok(event_register("post", EVT_DM_POST_PROBE, h_probe, &state));
  61. /* Probe a device */
  62. ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
  63. /* Check that the handler is called */
  64. ut_asserteq(3, state.val);
  65. return 0;
  66. }
  67. COMMON_TEST(test_event_probe, UT_TESTF_DM | UT_TESTF_SCAN_FDT);