diff options
author | Tristan Honscheid <honscheid@google.com> | 2022-09-19 14:43:08 -0600 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2022-09-21 22:29:48 +0000 |
commit | 00723fbf9789cdeb4e4cc5dca373236197a6a4b3 (patch) | |
tree | 98bb02553ef62aa2941e0c975c2624b46a092868 | |
parent | ceffe8d3fc0005ddfc1974cbfccbc51f948b201a (diff) | |
download | chrome-ec-00723fbf9789cdeb4e4cc5dca373236197a6a4b3.tar.gz |
zephyr: tests: Add tests for `common/led_common.c`
Add tests for the host command in the above file
BRANCH=None
BUG=b:247025800
TEST=./twister
Signed-off-by: Tristan Honscheid <honscheid@google.com>
Change-Id: I1c9da7326b508d0336828e7d3344626ef07ffc8f
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3904929
Reviewed-by: Al Semjonovs <asemjonovs@google.com>
Code-Coverage: Zoss <zoss-cl-coverage@prod.google.com>
-rw-r--r-- | zephyr/test/drivers/led_driver/CMakeLists.txt | 5 | ||||
-rw-r--r-- | zephyr/test/drivers/led_driver/src/led_common.c | 143 |
2 files changed, 147 insertions, 1 deletions
diff --git a/zephyr/test/drivers/led_driver/CMakeLists.txt b/zephyr/test/drivers/led_driver/CMakeLists.txt index 333785e4df..c378d34425 100644 --- a/zephyr/test/drivers/led_driver/CMakeLists.txt +++ b/zephyr/test/drivers/led_driver/CMakeLists.txt @@ -7,4 +7,7 @@ target_include_directories(app PRIVATE "${PLATFORM_EC}/zephyr/shim/src/led_driver") # Add source files -target_sources(app PRIVATE src/led.c) +target_sources(app PRIVATE + src/led_common.c + src/led.c +) diff --git a/zephyr/test/drivers/led_driver/src/led_common.c b/zephyr/test/drivers/led_driver/src/led_common.c new file mode 100644 index 0000000000..e11a1f1952 --- /dev/null +++ b/zephyr/test/drivers/led_driver/src/led_common.c @@ -0,0 +1,143 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ +#include <zephyr/ztest.h> +#include <zephyr/fff.h> + +#include "host_command.h" +#include "led_common.h" +#include "test/drivers/test_state.h" + +ZTEST(led_common, host_command__query) +{ + /* Gets the brightness range for an LED */ + + int ret; + struct ec_response_led_control response; + struct ec_params_led_control params = { + .led_id = EC_LED_ID_RIGHT_LED, + .flags = EC_LED_FLAGS_QUERY, + }; + + /* Expected brightness levels per color channel for this LED */ + uint8_t expected_brightness_ranges[] = { + [EC_LED_COLOR_RED] = 0, [EC_LED_COLOR_GREEN] = 0, + [EC_LED_COLOR_BLUE] = 1, [EC_LED_COLOR_YELLOW] = 0, + [EC_LED_COLOR_WHITE] = 1, [EC_LED_COLOR_AMBER] = 0, + }; + + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_LED_CONTROL, 1, response, params); + + ret = host_command_process(&args); + + zassert_ok(ret, "Host command returned %d", ret); + zassert_mem_equal(expected_brightness_ranges, response.brightness_range, + sizeof(expected_brightness_ranges), NULL); +} + +ZTEST(led_common, host_command__invalid_led) +{ + /* Try accessing info on a non-existent LED */ + + int ret; + struct ec_response_led_control response; + struct ec_params_led_control params = { + .led_id = EC_LED_ID_COUNT, /* Non-existent */ + .flags = EC_LED_FLAGS_QUERY, + }; + + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_LED_CONTROL, 1, response, params); + + ret = host_command_process(&args); + + zassert_equal(EC_RES_INVALID_PARAM, ret, "Host command returned %d", + ret); +} + +ZTEST(led_common, host_command__supported_channel) +{ + /* Try setting brightness on a color channel that is not supported */ + + int ret; + struct ec_response_led_control response; + struct ec_params_led_control params = { + .led_id = EC_LED_ID_RIGHT_LED, + .flags = 0x00, + .brightness = { + /* This LED does not have a red channel */ + [EC_LED_COLOR_RED] = 100, + }, + }; + + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_LED_CONTROL, 1, response, params); + + ret = host_command_process(&args); + + zassert_equal(EC_RES_INVALID_PARAM, ret, "Host command returned %d", + ret); +} + +ZTEST(led_common, host_command__manual_control) +{ + /* Set brightness for an LED directly */ + + int ret; + struct ec_response_led_control response; + struct ec_params_led_control params = { + .led_id = EC_LED_ID_RIGHT_LED, + .flags = 0x00, + .brightness = { + [EC_LED_COLOR_BLUE] = 1, + /* All other color channels off */ + }, + }; + + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_LED_CONTROL, 1, response, params); + + ret = host_command_process(&args); + + zassert_equal(EC_RES_SUCCESS, ret, "Host command returned %d", ret); + zassert_true( + gpio_pin_get_dt(GPIO_DT_FROM_NODELABEL(gpio_ec_chg_led_y_c0)), + "LED blue channel is not on"); + zassert_false( + gpio_pin_get_dt(GPIO_DT_FROM_NODELABEL(gpio_ec_chg_led_w_c0)), + "LED white channel is not off"); +} + +FAKE_VOID_FUNC(board_led_auto_control); + +ZTEST(led_common, host_command__auto_control) +{ + /* Configure an LED for automatic control */ + + int ret; + struct ec_response_led_control response; + struct ec_params_led_control params = { + .led_id = EC_LED_ID_RIGHT_LED, + .flags = EC_LED_FLAGS_AUTO, + }; + + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_LED_CONTROL, 1, response, params); + + ret = host_command_process(&args); + + zassert_equal(EC_RES_SUCCESS, ret, "Host command returned %d", ret); + zassert_equal(1, board_led_auto_control_fake.call_count, + "Did not call auto control function."); +} + +static void reset(void *data) +{ + ARG_UNUSED(data); + + RESET_FAKE(board_led_auto_control); +} + +ZTEST_SUITE(led_common, drivers_predicate_post_main, NULL, reset, reset, NULL); |