diff options
-rw-r--r-- | common/keyboard_scan.c | 11 | ||||
-rw-r--r-- | include/keyboard_scan.h | 5 | ||||
-rw-r--r-- | zephyr/test/drivers/keyboard_scan/src/keyboard_scan.c | 50 |
3 files changed, 49 insertions, 17 deletions
diff --git a/common/keyboard_scan.c b/common/keyboard_scan.c index e78d130289..1568f5ae10 100644 --- a/common/keyboard_scan.c +++ b/common/keyboard_scan.c @@ -1199,4 +1199,15 @@ __test_only void keyboard_scan_set_print_state_changes(int val) { print_state_changes = val; } + +__test_only void test_keyboard_scan_debounce_reset(void) +{ + memset(&debouncing, 0, sizeof(debouncing)); + memset(&debounced_state, 0, sizeof(debounced_state)); + memset(&scan_time, 0, sizeof(scan_time)); + memset(&scan_edge_index, 0, sizeof(scan_edge_index)); + + scan_time_index = 0; + post_scan_clock_us = 0; +} #endif /* TEST_BUILD */ diff --git a/include/keyboard_scan.h b/include/keyboard_scan.h index 4138f11ca5..544f973946 100644 --- a/include/keyboard_scan.h +++ b/include/keyboard_scan.h @@ -185,6 +185,11 @@ __test_only void keyboard_scan_set_print_state_changes(int val); * @return int non-zero if enabled, zero otherwise. */ int keyboard_scan_is_enabled(void); + +/** + * @brief Reset key debouncing logic + */ +void test_keyboard_scan_debounce_reset(void); #endif /* TEST_BUILD */ #endif /* __CROS_EC_KEYBOARD_SCAN_H */ diff --git a/zephyr/test/drivers/keyboard_scan/src/keyboard_scan.c b/zephyr/test/drivers/keyboard_scan/src/keyboard_scan.c index 71961c0a86..30b5e4521c 100644 --- a/zephyr/test/drivers/keyboard_scan/src/keyboard_scan.c +++ b/zephyr/test/drivers/keyboard_scan/src/keyboard_scan.c @@ -195,22 +195,16 @@ ZTEST(keyboard_scan, console_command_kbpress__invalid) */ FAKE_VOID_FUNC(key_state_changed, int, int, uint8_t); -ZTEST(keyboard_scan, console_command_kbpress__press_and_release) +ZTEST(keyboard_scan, test_console_command_kbpress__press) { - /* Pres and release a key */ + /* Press and release a key */ zassert_ok(shell_execute_cmd(get_ec_shell(), "kbpress 1 2")); - /* Hold a key down */ - zassert_ok(shell_execute_cmd(get_ec_shell(), "kbpress 3 4 1")); - - /* Release the key */ - zassert_ok(shell_execute_cmd(get_ec_shell(), "kbpress 3 4 0")); - /* Pause a bit to allow the key scan task to process. */ - k_sleep(K_MSEC(200)); + k_sleep(K_MSEC(500)); - /* Expect four key events */ - zassert_equal(4, key_state_changed_fake.call_count); + /* Expect two key events */ + zassert_equal(2, key_state_changed_fake.call_count); /* Press col=1,row=2 (state==1) */ zassert_equal(1, key_state_changed_fake.arg1_history[0]); @@ -221,16 +215,33 @@ ZTEST(keyboard_scan, console_command_kbpress__press_and_release) zassert_equal(1, key_state_changed_fake.arg1_history[1]); zassert_equal(2, key_state_changed_fake.arg0_history[1]); zassert_false(key_state_changed_fake.arg2_history[1]); +} + +ZTEST(keyboard_scan, test_console_command_kbpress__down_and_up) +{ + /* Hold a key down */ + zassert_ok(shell_execute_cmd(get_ec_shell(), "kbpress 3 4 1")); + + /* Release the key */ + zassert_ok(shell_execute_cmd(get_ec_shell(), "kbpress 3 4 0")); + + /* Pause a bit to allow the key scan task to process. */ + k_sleep(K_MSEC(500)); + + /* Expect two key events */ + zassert_equal(2, key_state_changed_fake.call_count, + "Actual call_count=%d", + key_state_changed_fake.call_count); /* Press col=3,row=4 (state==1) */ - zassert_equal(3, key_state_changed_fake.arg1_history[2]); - zassert_equal(4, key_state_changed_fake.arg0_history[2]); - zassert_true(key_state_changed_fake.arg2_history[2]); + zassert_equal(3, key_state_changed_fake.arg1_history[0]); + zassert_equal(4, key_state_changed_fake.arg0_history[0]); + zassert_true(key_state_changed_fake.arg2_history[0]); /* Release col=3,row=4 (state==0) */ - zassert_equal(3, key_state_changed_fake.arg1_history[3]); - zassert_equal(4, key_state_changed_fake.arg0_history[3]); - zassert_false(key_state_changed_fake.arg2_history[3]); + zassert_equal(3, key_state_changed_fake.arg1_history[1]); + zassert_equal(4, key_state_changed_fake.arg0_history[1]); + zassert_false(key_state_changed_fake.arg2_history[1]); } ZTEST(keyboard_scan, host_command_simulate_key__locked) @@ -380,6 +391,11 @@ static void reset_keyboard(void *data) /* Reset KB emulator */ clear_emulated_keys(); + /* Clear debouncing state to prevent latent key presses from appearing + * in a later test. + */ + test_keyboard_scan_debounce_reset(); + /* Reset all mocks. */ RESET_FAKE(key_state_changed); RESET_FAKE(system_is_locked); |