summaryrefslogtreecommitdiff
path: root/zephyr
diff options
context:
space:
mode:
authorTristan Honscheid <honscheid@google.com>2022-09-02 15:23:10 -0600
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2022-09-09 17:59:10 +0000
commit3e456353c4f4f1c1c26c42ed6ac0298d04294971 (patch)
tree5ae7e82648c6b4dda55a8fec6e3e621b033a88a5 /zephyr
parentcea5f5f4ae08d1b200add3943ec76914d9b3a727 (diff)
downloadchrome-ec-3e456353c4f4f1c1c26c42ed6ac0298d04294971.tar.gz
zephyr: tests: Test EC_CMD_MKBP_SIMULATE_KEY host command
Test the EC_CMD_MKBP_SIMULATE_KEY host command implementation in `common/keyboard_scan.c` BRANCH=None BUG=b:244606945 TEST=./twister Signed-off-by: Tristan Honscheid <honscheid@google.com> Change-Id: I55afbed07112b83d1d99e97a46bda34639803ef1 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3872727 Code-Coverage: Zoss <zoss-cl-coverage@prod.google.com> Reviewed-by: Aaron Massey <aaronmassey@google.com>
Diffstat (limited to 'zephyr')
-rw-r--r--zephyr/test/drivers/keyboard_scan/src/keyboard_scan.c93
1 files changed, 92 insertions, 1 deletions
diff --git a/zephyr/test/drivers/keyboard_scan/src/keyboard_scan.c b/zephyr/test/drivers/keyboard_scan/src/keyboard_scan.c
index e5bdc6e90b..f18c0e0e3c 100644
--- a/zephyr/test/drivers/keyboard_scan/src/keyboard_scan.c
+++ b/zephyr/test/drivers/keyboard_scan/src/keyboard_scan.c
@@ -12,6 +12,7 @@
#include <emul/emul_kb_raw.h>
#include "console.h"
+#include "host_command.h"
#include "keyboard_scan.h"
#include "test/drivers/test_mocks.h"
#include "test/drivers/test_state.h"
@@ -252,6 +253,92 @@ ZTEST(keyboard_scan, console_command_kbpress__press_and_release)
zassert_false(key_state_changed_fake.arg2_history[3], NULL);
}
+ZTEST(keyboard_scan, host_command_simulate_key__locked)
+{
+ uint16_t ret;
+
+ zassume_true(system_is_locked(), "Expecting locked system.");
+
+ struct ec_response_keyboard_factory_test response;
+ struct ec_params_mkbp_simulate_key params;
+ struct host_cmd_handler_args args = BUILD_HOST_COMMAND(
+ EC_CMD_MKBP_SIMULATE_KEY, 0, response, params);
+
+ ret = host_command_process(&args);
+ zassert_equal(EC_RES_ACCESS_DENIED, ret, "Command returned %u", ret);
+}
+
+ZTEST(keyboard_scan, host_command_simulate_key__bad_params)
+{
+ uint16_t ret;
+
+ system_is_locked_fake.return_val = 0;
+ zassume_false(system_is_locked(), "Expecting unlocked system.");
+
+ struct ec_response_keyboard_factory_test response;
+ struct ec_params_mkbp_simulate_key params = {
+ .col = KEYBOARD_COLS_MAX,
+ .row = KEYBOARD_ROWS,
+ };
+ struct host_cmd_handler_args args = BUILD_HOST_COMMAND(
+ EC_CMD_MKBP_SIMULATE_KEY, 0, response, params);
+
+ ret = host_command_process(&args);
+ zassert_equal(EC_RES_INVALID_PARAM, ret, "Command returned %u", ret);
+}
+
+/**
+ * @brief Helper function that sends a host command to press or release the
+ * specified key.
+ *
+ * @param col Key column
+ * @param row Key row
+ * @param pressed 1=press, 0=release
+ * @return uint16_t Host command return code.
+ */
+static uint16_t send_keypress_host_command(uint8_t col, uint8_t row,
+ uint8_t pressed)
+{
+ struct ec_params_mkbp_simulate_key params = {
+ .col = col,
+ .row = row,
+ .pressed = pressed,
+ };
+ struct host_cmd_handler_args args =
+ BUILD_HOST_COMMAND_PARAMS(EC_CMD_MKBP_SIMULATE_KEY, 0, params);
+
+ return host_command_process(&args);
+}
+
+ZTEST(keyboard_scan, host_command_simulate__key_press)
+{
+ uint16_t ret;
+
+ system_is_locked_fake.return_val = 0;
+ zassume_false(system_is_locked(), "Expecting unlocked system.");
+
+ ret = send_keypress_host_command(1, 2, 1);
+ zassert_equal(EC_RES_SUCCESS, ret, "Command returned %u", ret);
+
+ /* Release the key */
+ ret = send_keypress_host_command(1, 2, 0);
+ zassert_equal(EC_RES_SUCCESS, ret, "Command returned %u", ret);
+
+ /* Verify key events happened */
+
+ zassert_equal(2, key_state_changed_fake.call_count, NULL);
+
+ /* Press col=1,row=2 (state==1) */
+ zassert_equal(1, key_state_changed_fake.arg1_history[0], NULL);
+ zassert_equal(2, key_state_changed_fake.arg0_history[0], NULL);
+ zassert_true(key_state_changed_fake.arg2_history[0], NULL);
+
+ /* Release col=1,row=2 (state==0) */
+ zassert_equal(1, key_state_changed_fake.arg1_history[1], NULL);
+ zassert_equal(2, key_state_changed_fake.arg0_history[1], NULL);
+ zassert_false(key_state_changed_fake.arg2_history[1], NULL);
+}
+
static void reset_keyboard(void *data)
{
ARG_UNUSED(data);
@@ -264,8 +351,12 @@ static void reset_keyboard(void *data)
/* Turn off key state change printing */
keyboard_scan_set_print_state_changes(0);
- /* Reset the key state callback mock */
+ /* Reset the key state callback and system locked mocks */
RESET_FAKE(key_state_changed);
+ RESET_FAKE(system_is_locked);
+
+ /* Be locked by default */
+ system_is_locked_fake.return_val = 1;
}
ZTEST_SUITE(keyboard_scan, drivers_predicate_post_main, NULL, reset_keyboard,