diff options
author | Tristan Honscheid <honscheid@google.com> | 2022-09-14 14:50:07 -0600 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2022-09-20 17:46:28 +0000 |
commit | c2f08360e5abc78f9d4f99b39151a6e7f0f7222e (patch) | |
tree | 5b72aee03411f75d18972c999da3d2f38affd7c2 | |
parent | b968ed738a004d70e7afb144fca1f63956ef0a09 (diff) | |
download | chrome-ec-c2f08360e5abc78f9d4f99b39151a6e7f0f7222e.tar.gz |
zephyr: test: Test `kblight` console command
Test the above console command in `common/keyboard_backlight.c`
BRANCH=None
BUG=b:246577858
TEST=./twister
Signed-off-by: Tristan Honscheid <honscheid@google.com>
Change-Id: Ic1d23c08f9a91fd7812e50f053aa5a8d79af2c5f
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3892156
Commit-Queue: Tomasz Michalec <tmichalec@google.com>
Reviewed-by: Tomasz Michalec <tmichalec@google.com>
Code-Coverage: Zoss <zoss-cl-coverage@prod.google.com>
-rw-r--r-- | common/keyboard_backlight.c | 7 | ||||
-rw-r--r-- | include/keyboard_backlight.h | 12 | ||||
-rw-r--r-- | zephyr/test/drivers/keyboard_scan/src/keyboard_backlight.c | 53 |
3 files changed, 72 insertions, 0 deletions
diff --git a/common/keyboard_backlight.c b/common/keyboard_backlight.c index 3109062cf4..fde20de7ef 100644 --- a/common/keyboard_backlight.c +++ b/common/keyboard_backlight.c @@ -199,3 +199,10 @@ hc_set_keyboard_backlight(struct host_cmd_handler_args *args) } DECLARE_HOST_COMMAND(EC_CMD_PWM_SET_KEYBOARD_BACKLIGHT, hc_set_keyboard_backlight, EC_VER_MASK(0)); + +#ifdef TEST_BUILD +uint8_t kblight_get_current_enable(void) +{ + return current_enable; +} +#endif /* TEST_BUILD */ diff --git a/include/keyboard_backlight.h b/include/keyboard_backlight.h index a898300dc3..cf68805235 100644 --- a/include/keyboard_backlight.h +++ b/include/keyboard_backlight.h @@ -94,4 +94,16 @@ int kblight_register(const struct kblight_drv *drv); extern const struct kblight_drv kblight_pwm; +#ifdef TEST_BUILD +/** + * @brief Get internal backlight enabled state. The value reported by + * kblight_get_enabled() can be outdated due to a deferred function call + * being required to update it. Using this function in tests improves + * reliability and reduces the need to sleep. + * + * @return uint8_t 0 if disabled, 1 otherwise. + */ +uint8_t kblight_get_current_enable(void); +#endif /* TEST_BUILD */ + #endif /* __CROS_EC_KEYBOARD_BACKLIGHT_H */ diff --git a/zephyr/test/drivers/keyboard_scan/src/keyboard_backlight.c b/zephyr/test/drivers/keyboard_scan/src/keyboard_backlight.c index ae186bf84f..149f25dfdd 100644 --- a/zephyr/test/drivers/keyboard_scan/src/keyboard_backlight.c +++ b/zephyr/test/drivers/keyboard_scan/src/keyboard_backlight.c @@ -4,10 +4,13 @@ */ #include <stdint.h> +#include <string.h> #include <zephyr/ztest.h> #include <zephyr/kernel.h> +#include <zephyr/shell/shell_dummy.h> #include <zephyr/ztest_assert.h> +#include "console.h" #include "host_command.h" #include "keyboard_backlight.h" #include "test/drivers/test_state.h" @@ -68,6 +71,56 @@ ZTEST(keyboard_backlight, host_command_get_backlight__normal) zassert_equal(1, response.enabled, "Got 0x%02x", response.enabled); } +ZTEST(keyboard_backlight, console_command__noargs) +{ + /* Command should print current status. Set backlight on and to 70% */ + + const char *outbuffer; + size_t buffer_size; + + zassume_ok(set_backlight_percent_helper(70), NULL); + k_sleep(K_MSEC(50)); + + /* With no args, print current state */ + shell_backend_dummy_clear_output(get_ec_shell()); + zassert_ok(shell_execute_cmd(get_ec_shell(), "kblight"), NULL); + outbuffer = + shell_backend_dummy_get_output(get_ec_shell(), &buffer_size); + + zassert_ok(!strstr(outbuffer, "Keyboard backlight: 70% enabled: 1"), + "Actual string: `%s`", outbuffer); +} + +ZTEST(keyboard_backlight, console_command__set_on) +{ + /* Command should enable backlight to given intensity */ + + zassert_ok(shell_execute_cmd(get_ec_shell(), "kblight 65"), NULL); + zassert_equal(65, kblight_get(), NULL); + zassert_equal(1, kblight_get_current_enable(), NULL); +} + +ZTEST(keyboard_backlight, console_command__set_off) +{ + zassume_ok(set_backlight_percent_helper(40), NULL); + k_sleep(K_MSEC(50)); + + /* Turn back off */ + zassert_ok(shell_execute_cmd(get_ec_shell(), "kblight 0"), NULL); + zassert_equal(0, kblight_get(), NULL); + zassert_equal(0, kblight_get_current_enable(), NULL); +} + +ZTEST(keyboard_backlight, console_command__bad_params) +{ + zassert_equal(EC_ERROR_PARAM1, + shell_execute_cmd(get_ec_shell(), "kblight NaN"), NULL); + zassert_equal(EC_ERROR_PARAM1, + shell_execute_cmd(get_ec_shell(), "kblight -1"), NULL); + zassert_equal(EC_ERROR_PARAM1, + shell_execute_cmd(get_ec_shell(), "kblight 101"), NULL); +} + static void reset(void *data) { ARG_UNUSED(data); |