From 3d2a1c9fb1ebb38a0e5ca291e237d569da3bb904 Mon Sep 17 00:00:00 2001 From: Tristan Honscheid Date: Fri, 2 Sep 2022 16:16:25 -0600 Subject: zephyr: tests: Test handling of special key combos in `keyboard_scan.c` Test that VOL_UP + ALT + {R|H} cause AP reboot and hibernate, respectively. BRANCH=None BUG=b:244606945 TEST=./twister Signed-off-by: Tristan Honscheid Change-Id: Ic5d927fe830acbd9f6f33a0e436d6ed7c4301fce Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3872728 Reviewed-by: Yuval Peress Code-Coverage: Zoss --- common/system.c | 3 +- power/qcom.c | 2 +- .../test/drivers/keyboard_scan/src/keyboard_scan.c | 62 +++++++++++++++++++++- 3 files changed, 64 insertions(+), 3 deletions(-) diff --git a/common/system.c b/common/system.c index acb31c3774..b47cd62cb3 100644 --- a/common/system.c +++ b/common/system.c @@ -1042,7 +1042,8 @@ static int handle_pending_reboot(enum ec_reboot_cmd cmd) } } -void system_enter_hibernate(uint32_t seconds, uint32_t microseconds) +test_mockable void system_enter_hibernate(uint32_t seconds, + uint32_t microseconds) { if (!IS_ENABLED(CONFIG_HIBERNATE)) return; diff --git a/power/qcom.c b/power/qcom.c index 6a98350814..f144468970 100644 --- a/power/qcom.c +++ b/power/qcom.c @@ -887,7 +887,7 @@ static void check_for_warm_reset_event(void) } } -void chipset_reset(enum chipset_shutdown_reason reason) +test_mockable void chipset_reset(enum chipset_shutdown_reason reason) { CPRINTS("%s(%d)", __func__, reason); report_ap_reset(reason); diff --git a/zephyr/test/drivers/keyboard_scan/src/keyboard_scan.c b/zephyr/test/drivers/keyboard_scan/src/keyboard_scan.c index f18c0e0e3c..fe049490c0 100644 --- a/zephyr/test/drivers/keyboard_scan/src/keyboard_scan.c +++ b/zephyr/test/drivers/keyboard_scan/src/keyboard_scan.c @@ -339,6 +339,64 @@ ZTEST(keyboard_scan, host_command_simulate__key_press) zassert_false(key_state_changed_fake.arg2_history[1], NULL); } +FAKE_VOID_FUNC(system_enter_hibernate, uint32_t, uint32_t); +FAKE_VOID_FUNC(chipset_reset, int); + +ZTEST(keyboard_scan, special_key_combos) +{ + system_is_locked_fake.return_val = 0; + zassume_false(system_is_locked(), "Expecting unlocked system."); + + /* Set the volume up key coordinates to something arbitrary */ + int vol_up_col = 1; + int vol_up_row = 2; + + set_vol_up_key(vol_up_row, vol_up_col); + + /* Vol up and the alt keys must be in different columns */ + zassume_false(vol_up_col == KEYBOARD_COL_LEFT_ALT, NULL); + + /* Hold down volume up, left alt (either alt key works), and R */ + zassert_ok(send_keypress_host_command(vol_up_col, vol_up_row, 1), NULL); + zassert_ok(send_keypress_host_command(KEYBOARD_COL_LEFT_ALT, + KEYBOARD_ROW_LEFT_ALT, 1), + NULL); + zassert_ok(send_keypress_host_command(KEYBOARD_COL_KEY_R, + KEYBOARD_ROW_KEY_R, 1), + NULL); + + k_sleep(K_MSEC(100)); + + /* Release R and the press H */ + zassert_ok(send_keypress_host_command(KEYBOARD_COL_KEY_R, + KEYBOARD_ROW_KEY_R, 0), + NULL); + zassert_ok(send_keypress_host_command(KEYBOARD_COL_KEY_H, + KEYBOARD_ROW_KEY_H, 1), + NULL); + + k_sleep(K_MSEC(100)); + + /* Release all */ + zassert_ok(send_keypress_host_command(vol_up_col, vol_up_row, 0), NULL); + zassert_ok(send_keypress_host_command(KEYBOARD_COL_LEFT_ALT, + KEYBOARD_ROW_LEFT_ALT, 0), + NULL); + zassert_ok(send_keypress_host_command(KEYBOARD_COL_KEY_H, + KEYBOARD_ROW_KEY_H, 0), + NULL); + + /* Check that a reboot was requested (VOLUP + ALT + R) */ + zassert_equal(1, chipset_reset_fake.call_count, + "Did not try to reboot"); + zassert_equal(CHIPSET_RESET_KB_WARM_REBOOT, + chipset_reset_fake.arg0_history[0], NULL); + + /* Check that we called system_enter_hibernate (VOLUP + ALT + H) */ + zassert_equal(1, system_enter_hibernate_fake.call_count, + "Did not enter hibernate"); +} + static void reset_keyboard(void *data) { ARG_UNUSED(data); @@ -351,9 +409,11 @@ static void reset_keyboard(void *data) /* Turn off key state change printing */ keyboard_scan_set_print_state_changes(0); - /* Reset the key state callback and system locked mocks */ + /* Reset all mocks. */ RESET_FAKE(key_state_changed); RESET_FAKE(system_is_locked); + RESET_FAKE(system_enter_hibernate); + RESET_FAKE(chipset_reset); /* Be locked by default */ system_is_locked_fake.return_val = 1; -- cgit v1.2.1