summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Honscheid <honscheid@google.com>2023-04-07 13:09:19 -0600
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2023-04-07 20:47:28 +0000
commit2508ef893b1fb8a0381f18b2aa7007c6217bb219 (patch)
treedea133fe35f1385634ce5593e1be3a7d58bff5a6
parentf12e495e6e72d45254847346feba1f9abb7b205f (diff)
downloadchrome-ec-2508ef893b1fb8a0381f18b2aa7007c6217bb219.tar.gz
zephyr: test: Fix flaky keyboard_scan test
Latent key presses were appearing in a subsequent test, throwing off FFF expectations. Resolved by adding a test-only helper function to `keyboard_scan.c` to wipe out the debouncing state between tests. Also broke out the failing test into two independent ones. BRANCH=None BUG=None TEST=./twister -s drivers/drivers.keyboard_scan Change-Id: I79f2a6891f36e10a7df87865573348312c5b57d5 Signed-off-by: Tristan Honscheid <honscheid@google.com> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/4404706 Reviewed-by: Al Semjonovs <asemjonovs@google.com>
-rw-r--r--common/keyboard_scan.c11
-rw-r--r--include/keyboard_scan.h5
-rw-r--r--zephyr/test/drivers/keyboard_scan/src/keyboard_scan.c50
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);