summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Honscheid <honscheid@google.com>2022-09-01 13:45:04 -0600
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2022-09-08 22:23:20 +0000
commit1546ac715b2d5b9b7c9711fe290e52306109f7df (patch)
tree721a86fdf1ed19136d6299c8fd5eff5cc8b9f96e
parent38f6076bafcbe25ea8f0fee0d3df0212d0fc480b (diff)
downloadchrome-ec-1546ac715b2d5b9b7c9711fe290e52306109f7df.tar.gz
zephyr: tests: Test command `ksstate` in `common/keyboard_scan.c`
Add coverage for the `ksstate` console command BRANCH=None BUG=b:244606945 TEST=./twister Signed-off-by: Tristan Honscheid <honscheid@google.com> Change-Id: I1dbb754d1a357d162baee1bb909b37b7321b5c4c Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3872725 Reviewed-by: Simon Glass <sjg@chromium.org> Commit-Queue: Simon Glass <sjg@chromium.org> Code-Coverage: Zoss <zoss-cl-coverage@prod.google.com>
-rw-r--r--common/keyboard_scan.c14
-rw-r--r--include/keyboard_scan.h23
-rw-r--r--zephyr/test/drivers/default/src/keyboard_scan.c77
3 files changed, 112 insertions, 2 deletions
diff --git a/common/keyboard_scan.c b/common/keyboard_scan.c
index b57782ecc7..3b5db45903 100644
--- a/common/keyboard_scan.c
+++ b/common/keyboard_scan.c
@@ -138,7 +138,7 @@ static volatile int kbd_polls;
/* If true, we'll force a keyboard poll */
static volatile int force_poll;
-static int keyboard_scan_is_enabled(void)
+test_export_static int keyboard_scan_is_enabled(void)
{
/* NOTE: this is just an instantaneous glimpse of the variable. */
return !disable_scanning_mask;
@@ -1237,3 +1237,15 @@ static int command_keyboard_press(int argc, const char **argv)
DECLARE_CONSOLE_COMMAND(kbpress, command_keyboard_press, "[col row [0 | 1]]",
"Simulate keypress");
#endif
+
+#ifdef TEST_BUILD
+__test_only int keyboard_scan_get_print_state_changes(void)
+{
+ return print_state_changes;
+}
+
+__test_only void keyboard_scan_set_print_state_changes(int val)
+{
+ print_state_changes = val;
+}
+#endif /* TEST_BUILD */
diff --git a/include/keyboard_scan.h b/include/keyboard_scan.h
index 3ce1f74745..9bde457ef3 100644
--- a/include/keyboard_scan.h
+++ b/include/keyboard_scan.h
@@ -183,4 +183,27 @@ struct keyboard_type {
extern struct keyboard_type key_typ;
#endif
+#ifdef TEST_BUILD
+/**
+ * @brief Get the value of print_state_changes
+ *
+ * @return non-zero if state change printing is enabled, zero if not.
+ */
+__test_only int keyboard_scan_get_print_state_changes(void);
+
+/**
+ * @brief Forcibly set the value of print_state_changes
+ *
+ * @param val Value to set
+ */
+__test_only void keyboard_scan_set_print_state_changes(int val);
+
+/**
+ * @brief Checks if keyboard scanning is currently enabled.
+ *
+ * @return int non-zero if enabled, zero otherwise.
+ */
+int keyboard_scan_is_enabled(void);
+#endif /* TEST_BUILD */
+
#endif /* __CROS_EC_KEYBOARD_SCAN_H */
diff --git a/zephyr/test/drivers/default/src/keyboard_scan.c b/zephyr/test/drivers/default/src/keyboard_scan.c
index a0bb3f46ca..ad293aa401 100644
--- a/zephyr/test/drivers/default/src/keyboard_scan.c
+++ b/zephyr/test/drivers/default/src/keyboard_scan.c
@@ -2,12 +2,15 @@
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
+#include <string.h>
+#include <zephyr/shell/shell_dummy.h>
#include <zephyr/ztest.h>
#include <zephyr/drivers/emul.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/gpio/gpio_emul.h>
#include <emul/emul_kb_raw.h>
+#include "console.h"
#include "keyboard_scan.h"
#include "test/drivers/test_mocks.h"
#include "test/drivers/test_state.h"
@@ -112,4 +115,76 @@ ZTEST(keyboard_scan, test_press_enter)
zassert_ok(emulate_keystate(4, 11, false), NULL);
k_sleep(K_MSEC(100));
}
-ZTEST_SUITE(keyboard_scan, drivers_predicate_post_main, NULL, NULL, NULL, NULL);
+
+ZTEST(keyboard_scan, console_command_ksstate__noargs)
+{
+ const char *outbuffer;
+ size_t buffer_size;
+
+ /* With no args, print current state */
+ shell_backend_dummy_clear_output(get_ec_shell());
+ zassert_ok(shell_execute_cmd(get_ec_shell(), "ksstate"), NULL);
+ outbuffer =
+ shell_backend_dummy_get_output(get_ec_shell(), &buffer_size);
+
+ /* Check for some expected lines */
+ zassert_true(buffer_size > 0, NULL);
+ zassert_ok(!strstr(outbuffer, "Keyboard scan disable mask: 0x00000000"),
+ "Output was: `%s`", outbuffer);
+ zassert_ok(!strstr(outbuffer, "Keyboard scan state printing off"),
+ "Output was: `%s`", outbuffer);
+
+ /* Ensure we are still scanning */
+ zassert_true(keyboard_scan_is_enabled(), NULL);
+}
+
+ZTEST(keyboard_scan, console_command_ksstate__force)
+{
+ /* This command forces the keyboard to start scanning (if not already)
+ * and enable state change printing. To test: turn scanning off, run
+ * command, and verify we are scanning and printing state
+ */
+
+ keyboard_scan_enable(false, -1);
+ zassume_false(keyboard_scan_is_enabled(), NULL);
+
+ zassert_ok(shell_execute_cmd(get_ec_shell(), "ksstate force"), NULL);
+
+ zassert_true(keyboard_scan_is_enabled(), NULL);
+ zassert_true(keyboard_scan_get_print_state_changes(), NULL);
+}
+
+ZTEST(keyboard_scan, console_command_ksstate__on_off)
+{
+ /* This command turns state change printing on/off */
+
+ zassume_false(keyboard_scan_get_print_state_changes(), NULL);
+
+ zassert_ok(shell_execute_cmd(get_ec_shell(), "ksstate on"), NULL);
+ zassert_true(keyboard_scan_get_print_state_changes(), NULL);
+
+ zassert_ok(shell_execute_cmd(get_ec_shell(), "ksstate off"), NULL);
+ zassert_false(keyboard_scan_get_print_state_changes(), NULL);
+}
+
+ZTEST(keyboard_scan, console_command_ksstate__invalid)
+{
+ /* Pass a string that cannot be parsed as a bool */
+ zassert_ok(!shell_execute_cmd(get_ec_shell(), "ksstate xyz"), NULL);
+}
+
+static void reset_keyboard(void *data)
+{
+ ARG_UNUSED(data);
+
+ /* Enable scanning and clear all reason bits (reason bits explain why
+ * scanning was disabled -- see `enum kb_scan_disable_masks`)
+ */
+ keyboard_scan_enable(true, -1);
+
+ /* Turn off key state change printing */
+ keyboard_scan_set_print_state_changes(0);
+}
+
+ZTEST_SUITE(keyboard_scan, drivers_predicate_post_main, NULL, reset_keyboard,
+ reset_keyboard, NULL);