diff options
author | Yuval Peress <peress@google.com> | 2022-07-24 21:31:52 -0600 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2022-07-25 20:51:37 +0000 |
commit | c16f255ef203b67eb31cb727a98f96e3fbce1d4b (patch) | |
tree | 7f6b5f11e5221b9588795cd56dfd6db8dad90cc9 /zephyr/test/drivers/default/src/console_cmd/accelread.c | |
parent | 3524a1f7eb23533a4a6cffd4c69bd07cf1b43129 (diff) | |
download | chrome-ec-c16f255ef203b67eb31cb727a98f96e3fbce1d4b.tar.gz |
zephyr: test: migrate drivers test to twister
1. Move the common test logic to drivers/common with its own
CMakeLists.txt
2. Move the drivers test suites to drivers/default with its own
CMakeLists.txt
3. Add a Kconfig for each suite that can enable it, use Zephyr's
add_subdirectory_ifdef to control which suites get added to the
biary and verify that at least 1 suite was added.
4. Add a check for ZMAKE_PROJECT_NAME to still allow zmake commands
5. Add testcase.yaml which runs all the tests
BRANCH=none
BUG=b:240093007
TEST=./twister --coverage -p native_posix -T zephyr/test/drivers
Signed-off-by: Yuval Peress <peress@google.com>
Change-Id: Ib9d4ea4a8431539391cc5349a7a8fd7a7d21f120
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3784043
Reviewed-by: Tristan Honscheid <honscheid@google.com>
Commit-Queue: Tristan Honscheid <honscheid@google.com>
Diffstat (limited to 'zephyr/test/drivers/default/src/console_cmd/accelread.c')
-rw-r--r-- | zephyr/test/drivers/default/src/console_cmd/accelread.c | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/zephyr/test/drivers/default/src/console_cmd/accelread.c b/zephyr/test/drivers/default/src/console_cmd/accelread.c new file mode 100644 index 0000000000..1dcc4168a4 --- /dev/null +++ b/zephyr/test/drivers/default/src/console_cmd/accelread.c @@ -0,0 +1,124 @@ +/* Copyright 2022 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <fff.h> +#include <zephyr/shell/shell.h> +#include <ztest.h> + +#include "accelgyro.h" +#include "console.h" +#include "ec_commands.h" +#include "motion_sense.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" + +FAKE_VALUE_FUNC(int, mock_read, const struct motion_sensor_t *, int *); +FAKE_VALUE_FUNC(int, mock_set_data_rate, const struct motion_sensor_t *, int, + int); +FAKE_VALUE_FUNC(int, mock_get_data_rate, const struct motion_sensor_t *); + +struct console_cmd_accelread_fixture { + const struct accelgyro_drv *sensor_0_drv; + struct accelgyro_drv mock_drv; +}; + +static void *console_cmd_accelread_setup(void) +{ + static struct console_cmd_accelread_fixture fixture = { + .mock_drv = { + .read = mock_read, + /* + * Data rate functions are required so that motion_sense + * task doesn't segfault. + */ + .set_data_rate = mock_set_data_rate, + .get_data_rate = mock_get_data_rate, + }, + }; + fixture.sensor_0_drv = motion_sensors[0].drv; + + return &fixture; +} + +static void console_cmd_accelread_before(void *fixture) +{ + ARG_UNUSED(fixture); + RESET_FAKE(mock_read); + RESET_FAKE(mock_set_data_rate); + RESET_FAKE(mock_get_data_rate); + FFF_RESET_HISTORY(); +} + +static void console_cmd_accelread_after(void *fixture) +{ + struct console_cmd_accelread_fixture *this = fixture; + + motion_sensors[0].drv = this->sensor_0_drv; +} + +ZTEST_SUITE(console_cmd_accelread, drivers_predicate_post_main, + console_cmd_accelread_setup, console_cmd_accelread_before, + console_cmd_accelread_after, NULL); + +ZTEST_USER(console_cmd_accelread, test_too_few_arguments) +{ + int rv = shell_execute_cmd(get_ec_shell(), "accelread"); + + zassert_equal(EC_ERROR_PARAM_COUNT, rv, "Expected %d, but got %d", + EC_ERROR_PARAM_COUNT, rv); +} + +ZTEST_USER(console_cmd_accelread, test_invalid_sensor_num) +{ + int rv; + + rv = shell_execute_cmd(get_ec_shell(), "accelread f"); + zassert_equal(EC_ERROR_PARAM1, rv, "Expected %d, but got %d", + EC_ERROR_PARAM1, rv); + + rv = shell_execute_cmd(get_ec_shell(), "accelread -1"); + zassert_equal(EC_ERROR_PARAM1, rv, "Expected %d, but got %d", + EC_ERROR_PARAM1, rv); + + rv = shell_execute_cmd(get_ec_shell(), "accelread 100"); + zassert_equal(EC_ERROR_PARAM1, rv, "Expected %d, but got %d", + EC_ERROR_PARAM1, rv); +} + +static struct console_cmd_accelread_fixture *current_fixture; + +int mock_read_call_super(const struct motion_sensor_t *s, int *v) +{ + return current_fixture->sensor_0_drv->read(s, v); +} + +ZTEST_USER_F(console_cmd_accelread, test_read) +{ + current_fixture = fixture; + mock_read_fake.custom_fake = mock_read_call_super; + mock_get_data_rate_fake.return_val = 100; + motion_sensors[0].drv = &fixture->mock_drv; + + zassert_ok(shell_execute_cmd(get_ec_shell(), "accelread 0"), NULL); + zassert_equal(1, mock_read_fake.call_count, + "Expected only 1 call to read, but got %d", + mock_read_fake.call_count); + + zassert_ok(shell_execute_cmd(get_ec_shell(), "accelread 0 2"), NULL); + zassert_equal(3, mock_read_fake.call_count, + "Expected only 3 call to read, but got %d", + mock_read_fake.call_count); +} + +ZTEST_USER_F(console_cmd_accelread, test_read_fail) +{ + mock_read_fake.return_val = 1; + motion_sensors[0].drv = &fixture->mock_drv; + + zassert_ok(shell_execute_cmd(get_ec_shell(), "accelread 0"), NULL); + zassert_equal(1, mock_read_fake.call_count, + "Expected only 1 call to read, but got %d", + mock_read_fake.call_count); +} |