diff options
author | Yuval Peress <peress@google.com> | 2022-04-25 21:18:55 -0600 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2022-04-26 18:01:34 +0000 |
commit | e01a81c85e2d6ac78f72f184a772710c48fcdc70 (patch) | |
tree | 9e7bc6e919e9c6e73d8b86230428a10847503462 | |
parent | fbafae5b529ff829958f40807ebe00aaa072fa66 (diff) | |
download | chrome-ec-e01a81c85e2d6ac78f72f184a772710c48fcdc70.tar.gz |
test: add tests for motionsense range command
BRANCH=none
BUG=b:224614211
TEST=zmake test test-drivers
Signed-off-by: Yuval Peress <peress@google.com>
Change-Id: Ie7d8f6fa183617d64a1f23e42ea5c843cbf8f6bc
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3607055
Reviewed-by: Denis Brockus <dbrockus@chromium.org>
-rw-r--r-- | zephyr/test/drivers/include/test/drivers/utils.h | 17 | ||||
-rw-r--r-- | zephyr/test/drivers/src/host_cmd/motion_sense.c | 105 | ||||
-rw-r--r-- | zephyr/test/drivers/src/utils.c | 18 |
3 files changed, 138 insertions, 2 deletions
diff --git a/zephyr/test/drivers/include/test/drivers/utils.h b/zephyr/test/drivers/include/test/drivers/utils.h index 3674cd70cc..7e01e0f164 100644 --- a/zephyr/test/drivers/include/test/drivers/utils.h +++ b/zephyr/test/drivers/include/test/drivers/utils.h @@ -293,6 +293,23 @@ int host_cmd_motion_sense_odr(uint8_t sensor_num, int32_t odr, bool round_up, struct ec_response_motion_sense *response); /** + * @brief Call the host command MOTION_SENSE with the sensor range sub-command + * + * This function attempts to set the sensor range and returns the range value. + * If the range value is EC_MOTION_SENSE_NO_VALUE, then the host command will + * not attempt to update the range. + * + * @param sensor_num The sensor index in the motion_sensors array to query + * @param range The new range to set + * @param round_up Whether or not to round up the range. + * @param response Pointer to the response data structure to fill on success + * @return The result code from the host command + */ +int host_cmd_motion_sense_range(uint8_t sensor_num, int32_t range, + bool round_up, + struct ec_response_motion_sense *response); + +/** * Run the host command to get the PD discovery responses. * * @param port The USB-C port number diff --git a/zephyr/test/drivers/src/host_cmd/motion_sense.c b/zephyr/test/drivers/src/host_cmd/motion_sense.c index e6151e67a3..0b26b9ceb8 100644 --- a/zephyr/test/drivers/src/host_cmd/motion_sense.c +++ b/zephyr/test/drivers/src/host_cmd/motion_sense.c @@ -3,6 +3,7 @@ * found in the LICENSE file. */ +#include <fff.h> #include <ztest.h> #include "driver/accel_bma2x2.h" @@ -10,6 +11,8 @@ #include "test/drivers/test_state.h" #include "test/drivers/utils.h" +FAKE_VALUE_FUNC(int, mock_set_range, struct motion_sensor_t *, int, int); + /** * Get the size needed for a struct ec_response_motion_sense */ @@ -17,14 +20,43 @@ (sizeof(struct ec_response_motion_sense) + \ n * sizeof(struct ec_response_motion_sensor_data)) +struct host_cmd_motion_sense_fixture { + const struct accelgyro_drv *sensor_0_drv; + struct accelgyro_drv mock_drv; +}; + +static struct host_cmd_motion_sense_fixture fixture = { + .mock_drv = { + .set_range = mock_set_range, + }, +}; + +static void *host_cmd_motion_sense_setup(void) +{ + fixture.sensor_0_drv = motion_sensors[0].drv; + + return &fixture; +} + static void host_cmd_motion_sense_before(void *state) { + ARG_UNUSED(state); + RESET_FAKE(mock_set_range); + FFF_RESET_HISTORY(); + motion_sensors[0].config[SENSOR_CONFIG_AP].odr = 0; motion_sensors[0].config[SENSOR_CONFIG_AP].ec_rate = 1000 * MSEC; } -ZTEST_SUITE(host_cmd_motion_sense, drivers_predicate_post_main, NULL, - host_cmd_motion_sense_before, NULL, NULL); +static void host_cmd_motion_sense_after(void *state) +{ + ARG_UNUSED(state); + motion_sensors[0].drv = fixture.sensor_0_drv; +} + +ZTEST_SUITE(host_cmd_motion_sense, drivers_predicate_post_main, + host_cmd_motion_sense_setup, host_cmd_motion_sense_before, + host_cmd_motion_sense_after, NULL); ZTEST_USER(host_cmd_motion_sense, test_dump) { @@ -245,3 +277,72 @@ ZTEST_USER(host_cmd_motion_sense, test_odr_set) BMA2x2_REG_TO_BW(BMA2x2_BW_7_81HZ), response.sensor_odr.ret); } + +ZTEST_USER(host_cmd_motion_sense, test_range_invalid_sensor_num) +{ + struct ec_response_motion_sense response; + + zassert_equal(EC_RES_INVALID_PARAM, + host_cmd_motion_sense_range( + /*sensor_num=*/0xff, + /*range=*/EC_MOTION_SENSE_NO_VALUE, + /*round_up=*/false, &response), + NULL); +} + +ZTEST_USER(host_cmd_motion_sense, test_get_range) +{ + struct ec_response_motion_sense response; + + zassert_ok(host_cmd_motion_sense_range( + /*sensor_num=*/0, /*range=*/EC_MOTION_SENSE_NO_VALUE, + /*round_up=*/false, &response), + NULL); + zassert_equal(motion_sensors[0].current_range, + response.sensor_range.ret, "Expected %d, but got %d", + motion_sensors[0].current_range, + response.sensor_range.ret); +} + +ZTEST_USER(host_cmd_motion_sense, test_null_set_range_in_driver) +{ + struct ec_response_motion_sense response; + struct accelgyro_drv drv = { 0 }; + + motion_sensors[0].drv = &drv; + zassert_equal(EC_RES_INVALID_COMMAND, + host_cmd_motion_sense_range(/*sensor_num=*/0, /*range=*/4, + /*round_up=*/false, + &response), + NULL); +} + +ZTEST_USER_F(host_cmd_motion_sense, test_set_range_error) +{ + struct ec_response_motion_sense response; + + mock_set_range_fake.return_val = 1; + motion_sensors[0].drv = &this->mock_drv; + + zassert_equal(EC_RES_INVALID_PARAM, + host_cmd_motion_sense_range(/*sensor_num=*/0, /*range=*/4, + /*round_up=*/false, + &response), + NULL); + zassert_equal(1, mock_set_range_fake.call_count, NULL); +} + +ZTEST_USER_F(host_cmd_motion_sense, test_set_range) +{ + struct ec_response_motion_sense response; + + mock_set_range_fake.return_val = 0; + motion_sensors[0].drv = &this->mock_drv; + + zassert_ok(host_cmd_motion_sense_range(/*sensor_num=*/0, /*range=*/4, + /*round_up=*/false, &response), + NULL); + zassert_equal(1, mock_set_range_fake.call_count, NULL); + zassert_equal(4, mock_set_range_fake.arg1_history[0], NULL); + zassert_equal(0, mock_set_range_fake.arg2_history[0], NULL); +} diff --git a/zephyr/test/drivers/src/utils.c b/zephyr/test/drivers/src/utils.c index c6cdd0a101..4b528246c6 100644 --- a/zephyr/test/drivers/src/utils.c +++ b/zephyr/test/drivers/src/utils.c @@ -175,6 +175,24 @@ int host_cmd_motion_sense_odr(uint8_t sensor_num, int32_t odr, bool round_up, return host_command_process(&args); } +int host_cmd_motion_sense_range(uint8_t sensor_num, int32_t range, + bool round_up, + struct ec_response_motion_sense *response) +{ + struct ec_params_motion_sense params = { + .cmd = MOTIONSENSE_CMD_SENSOR_RANGE, + .sensor_range = { + .sensor_num = sensor_num, + .data = range, + .roundup = round_up, + }, + }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND( + EC_CMD_MOTION_SENSE_CMD, 1, *response, params); + + return host_command_process(&args); +} + void host_cmd_typec_discovery(int port, enum typec_partner_type partner_type, void *response, size_t response_size) { |