diff options
Diffstat (limited to 'zephyr/test/drivers/default/src/console_cmd/accelrange.c')
-rw-r--r-- | zephyr/test/drivers/default/src/console_cmd/accelrange.c | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/zephyr/test/drivers/default/src/console_cmd/accelrange.c b/zephyr/test/drivers/default/src/console_cmd/accelrange.c new file mode 100644 index 0000000000..ff9d03bfe2 --- /dev/null +++ b/zephyr/test/drivers/default/src/console_cmd/accelrange.c @@ -0,0 +1,118 @@ +/* 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/shell/shell.h> +#include <zephyr/devicetree.h> +#include <zephyr/ztest.h> + +#include "console.h" +#include "driver/accel_bma2x2.h" +#include "ec_commands.h" +#include "emul/emul_bma255.h" +#include "emul/emul_common_i2c.h" +#include "i2c.h" +#include "motion_sense.h" +#include "test/drivers/test_state.h" + +#define EMUL_NODE DT_NODELABEL(bma_emul) + +#define BMA_ORD DT_DEP_ORD(EMUL_LABEL) + +static void console_cmd_accelrange_after(void *fixture) +{ + const struct emul *emul = EMUL_DT_GET(EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_bma_get_i2c_common_data(emul); + + ARG_UNUSED(fixture); + shell_execute_cmd(get_ec_shell(), "accelrange 0 2"); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); +} + +ZTEST_SUITE(console_cmd_accelrange, drivers_predicate_post_main, NULL, NULL, + console_cmd_accelrange_after, NULL); + +ZTEST_USER(console_cmd_accelrange, test_num_args) +{ + int rv; + + rv = shell_execute_cmd(get_ec_shell(), "accelrange"); + zassert_equal(rv, EC_ERROR_PARAM_COUNT, "Expected %d, but got %d", + EC_ERROR_PARAM_COUNT, rv); + + rv = shell_execute_cmd(get_ec_shell(), "accelrange 0 1 2 3"); + zassert_equal(rv, EC_ERROR_PARAM_COUNT, "Expected %d, but got %d", + EC_ERROR_PARAM_COUNT, rv); +} + +ZTEST_USER(console_cmd_accelrange, test_bad_sensor_num) +{ + int rv; + + rv = shell_execute_cmd(get_ec_shell(), "accelrange t"); + zassert_equal(rv, EC_ERROR_PARAM1, "Expected %d, but got %d", + EC_ERROR_PARAM1, rv); + + rv = shell_execute_cmd(get_ec_shell(), "accelrange -1"); + zassert_equal(rv, EC_ERROR_PARAM1, "Expected %d, but got %d", + EC_ERROR_PARAM1, rv); + + rv = shell_execute_cmd(get_ec_shell(), "accelrange 100"); + zassert_equal(rv, EC_ERROR_PARAM1, "Expected %d, but got %d", + EC_ERROR_PARAM1, rv); +} + +ZTEST_USER(console_cmd_accelrange, test_print_range) +{ + zassert_ok(shell_execute_cmd(get_ec_shell(), "accelrange 0"), NULL); +} + +ZTEST_USER(console_cmd_accelrange, test_set_invalid_range) +{ + int rv = shell_execute_cmd(get_ec_shell(), "accelrange 0 t"); + + zassert_equal(rv, EC_ERROR_PARAM2, "Expected %d, but got %d", + EC_ERROR_PARAM2, rv); +} + +ZTEST_USER(console_cmd_accelrange, test_set_range_round_up_implicit) +{ + zassert_ok(shell_execute_cmd(get_ec_shell(), "accelrange 0 3"), NULL); + zassert_equal(motion_sensors[0].current_range, 4, + "Expected 4, but got %d", + motion_sensors[0].current_range); +} + +ZTEST_USER(console_cmd_accelrange, test_set_range_round_up_explicit) +{ + zassert_ok(shell_execute_cmd(get_ec_shell(), "accelrange 0 3 1"), NULL); + zassert_equal(motion_sensors[0].current_range, 4, + "Expected 4, but got %d", + motion_sensors[0].current_range); +} + +ZTEST_USER(console_cmd_accelrange, test_set_range_round_down) +{ + zassert_ok(shell_execute_cmd(get_ec_shell(), "accelrange 0 5 0"), NULL); + zassert_equal(motion_sensors[0].current_range, 4, + "Expected 4, but got %d", + motion_sensors[0].current_range); +} + +ZTEST_USER(console_cmd_accelrange, test_i2c_error) +{ + const struct emul *emul = EMUL_DT_GET(EMUL_NODE); + struct i2c_common_emul_data *common_data = + emul_bma_get_i2c_common_data(emul); + int rv; + + i2c_common_emul_set_read_fail_reg(common_data, + BMA2x2_RANGE_SELECT_ADDR); + + rv = shell_execute_cmd(get_ec_shell(), "accelrange 0 3"); + zassert_equal(rv, EC_ERROR_PARAM2, "Expected %d, but got %d", + EC_ERROR_PARAM2, rv); +} |