From 2c78ade6deabe07143840e01e97e2d2db09890e5 Mon Sep 17 00:00:00 2001 From: Aaron Massey Date: Thu, 13 Oct 2022 13:26:16 -0600 Subject: test: host_sleep.c sleep_set_notify Add a test that verifies behavior from sleep_set_notify and correspondly sleep_notify_transition. Scenarios: * Setting notify state and sleep_notify_transition can fire hook * Setting notify state and sleep_notify_transition clears notify state. * sleep_notify_transition can start a hook with NONE sleep notify state Also conducted two small refactor fixes: * Made privately used variable static * Changed an int type to how it's actually used (enum sleep_notify_type) BRANCH=none BUG=b:252887178 TEST=twister -s zephyr/test/drivers/drivers.power_host_sleep Signed-off-by: Aaron Massey Change-Id: I9d63d1377359035b316ac6aa5de87376b3bb203f Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3953317 Reviewed-by: Jack Rosenthal Code-Coverage: Zoss --- include/power.h | 4 +-- power/host_sleep.c | 4 +-- .../power_host_sleep/src/test_power_host_sleep.c | 41 ++++++++++++++++++++++ 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/include/power.h b/include/power.h index 461e69748b..9ba4515a4c 100644 --- a/include/power.h +++ b/include/power.h @@ -304,12 +304,12 @@ enum sleep_notify_type { void sleep_set_notify(enum sleep_notify_type notify); /** - * Notify the given hook is the sleep notify is matched. + * Notify the given hook if the sleep notify is matched. * * @param check_state: The sleep notify to check. * @param hook_id: The hook to notify. */ -void sleep_notify_transition(int check_state, int hook_id); +void sleep_notify_transition(enum sleep_notify_type check_state, int hook_id); /** * Called during the suspend transition, to increase the transition counter. diff --git a/power/host_sleep.c b/power/host_sleep.c index da7189083a..b97fb904c1 100644 --- a/power/host_sleep.c +++ b/power/host_sleep.c @@ -85,7 +85,7 @@ void power_set_host_sleep_state(enum host_sleep_event state) } /* Flag to notify listeners about suspend/resume events. */ -enum sleep_notify_type sleep_notify = SLEEP_NOTIFY_NONE; +static enum sleep_notify_type sleep_notify = SLEEP_NOTIFY_NONE; /* * Note: the following sleep_ functions do not get called in the S3 path on @@ -96,7 +96,7 @@ void sleep_set_notify(enum sleep_notify_type notify) sleep_notify = notify; } -void sleep_notify_transition(int check_state, int hook_id) +void sleep_notify_transition(enum sleep_notify_type check_state, int hook_id) { if (sleep_notify != check_state) return; diff --git a/zephyr/test/drivers/power_host_sleep/src/test_power_host_sleep.c b/zephyr/test/drivers/power_host_sleep/src/test_power_host_sleep.c index a30f4c77b2..2ff7b64e9e 100644 --- a/zephyr/test/drivers/power_host_sleep/src/test_power_host_sleep.c +++ b/zephyr/test/drivers/power_host_sleep/src/test_power_host_sleep.c @@ -8,6 +8,7 @@ #include #include "ec_commands.h" +#include "hooks.h" #include "host_command.h" #include "power.h" #include "test/drivers/test_mocks.h" @@ -215,5 +216,45 @@ ZTEST(power_host_sleep, test_sleep_start_suspend_infinite_timeout) zassert_equal(power_board_handle_sleep_hang_fake.call_count, 0); } +/* Only used in test_sleep_set_notify */ +static bool _test_host_sleep_hook_called; + +static void _test_sleep_notify_hook(void) +{ + _test_host_sleep_hook_called = true; +} +DECLARE_HOOK(HOOK_TEST_1, _test_sleep_notify_hook, HOOK_PRIO_DEFAULT); + +ZTEST(power_host_sleep, test_sleep_set_notify) +{ + /* Init as none */ + sleep_set_notify(SLEEP_NOTIFY_NONE); + + /* Verify hook may be notified for a specific NOTIFY state */ + _test_host_sleep_hook_called = false; + sleep_set_notify(SLEEP_NOTIFY_SUSPEND); + sleep_notify_transition(SLEEP_NOTIFY_SUSPEND, HOOK_TEST_1); + k_sleep(K_SECONDS(1)); + + zassert_true(_test_host_sleep_hook_called); + + /* Verify NOTIFY state is reset after firing hook */ + _test_host_sleep_hook_called = false; + sleep_notify_transition(SLEEP_NOTIFY_SUSPEND, HOOK_TEST_1); + k_sleep(K_SECONDS(1)); + + zassert_false(_test_host_sleep_hook_called); + + /* + * Verify that SLEEP_NOTIFY_NONE is a potential hook state to fire + * TODO(b/253480505) Should this really be allowed? + */ + _test_host_sleep_hook_called = false; + sleep_notify_transition(SLEEP_NOTIFY_NONE, HOOK_TEST_1); + k_sleep(K_SECONDS(1)); + + zassert_true(_test_host_sleep_hook_called); +} + ZTEST_SUITE(power_host_sleep, drivers_predicate_post_main, NULL, power_host_sleep_before_after, power_host_sleep_before_after, NULL); -- cgit v1.2.1