diff --git a/components/esp_hid/test/CMakeLists.txt b/components/esp_hid/test/CMakeLists.txt deleted file mode 100644 index fa7a4e5216..0000000000 --- a/components/esp_hid/test/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ -idf_component_register(SRC_DIRS "." - INCLUDE_DIRS "." - REQUIRES cmock test_utils esp_hid) -target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") diff --git a/components/esp_hid/test_apps/.build-test-rules.yml b/components/esp_hid/test_apps/.build-test-rules.yml new file mode 100644 index 0000000000..452d163e2d --- /dev/null +++ b/components/esp_hid/test_apps/.build-test-rules.yml @@ -0,0 +1,8 @@ +# Documentation: .gitlab/ci/README.md#manifest-file-to-control-the-buildtest-apps + +components/esp_hid/test_apps: + enable: + - if: IDF_TARGET in ["esp32", "esp32c3"] + reason: Testing on one chip per architecture is currently enough + depends_components: + - esp_hid diff --git a/components/esp_hid/test_apps/CMakeLists.txt b/components/esp_hid/test_apps/CMakeLists.txt new file mode 100644 index 0000000000..48539b92f2 --- /dev/null +++ b/components/esp_hid/test_apps/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required(VERSION 3.16) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +set(COMPONENTS main) +list(PREPEND SDKCONFIG_DEFAULTS + "$ENV{IDF_PATH}/tools/test_apps/configs/sdkconfig.debug_helpers" + "sdkconfig.defaults") + +project(esp_hid_test) diff --git a/components/esp_hid/test/README.md b/components/esp_hid/test_apps/README.md similarity index 69% rename from components/esp_hid/test/README.md rename to components/esp_hid/test_apps/README.md index 1a3fb7d909..f9ee4bcefb 100644 --- a/components/esp_hid/test/README.md +++ b/components/esp_hid/test_apps/README.md @@ -1,4 +1,28 @@ -### Tests have been generated with the following code +| Supported Targets | ESP32 | ESP32-C3 | +| ----------------- | ----- | -------- | + +# esp_hid unit tests + +The unit tests are currently run only on the chips listed above just to save CI resources. If you are adding some tests which need to run on a different chip, update [.build-test-rules.yml](../.build-test-rules.yml), adding the chip you need. + +When adding new test cases, check if the `depends_components` list in `.build-test-rules.yml` needs to be updated to include additional components. The test app will only be built and tested when these components are modified. + +To build and run this test app, using esp32c3 target for example: + +```bash +idf.py set-target esp32c3 +idf.py build flash monitor +``` + +To run tests using pytest: + +```bash +idf.py set-target esp32c3 +idf.py build +pytest --target=esp32c3 +``` + +### Code used to generate tests ```c diff --git a/components/esp_hid/test_apps/main/CMakeLists.txt b/components/esp_hid/test_apps/main/CMakeLists.txt new file mode 100644 index 0000000000..90b84e9d66 --- /dev/null +++ b/components/esp_hid/test_apps/main/CMakeLists.txt @@ -0,0 +1,4 @@ +idf_component_register(SRCS "test_esp_hid_main.c" + "test_esp_hid.c" + PRIV_REQUIRES unity esp_hid + WHOLE_ARCHIVE) diff --git a/components/esp_hid/test/hid_descriptor.h b/components/esp_hid/test_apps/main/hid_descriptor.h similarity index 96% rename from components/esp_hid/test/hid_descriptor.h rename to components/esp_hid/test_apps/main/hid_descriptor.h index 56142d001a..2b7172ed29 100644 --- a/components/esp_hid/test/hid_descriptor.h +++ b/components/esp_hid/test_apps/main/hid_descriptor.h @@ -1,16 +1,9 @@ -// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* + * SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + #pragma once const unsigned char hidReportMap[] = { diff --git a/components/esp_hid/test/test_esp_hid.c b/components/esp_hid/test_apps/main/test_esp_hid.c similarity index 94% rename from components/esp_hid/test/test_esp_hid.c rename to components/esp_hid/test_apps/main/test_esp_hid.c index db9b0d10af..4f9eb213eb 100644 --- a/components/esp_hid/test/test_esp_hid.c +++ b/components/esp_hid/test_apps/main/test_esp_hid.c @@ -1,16 +1,8 @@ -// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* + * SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #include #include @@ -18,7 +10,6 @@ #include #include "unity.h" #include "unity_test_runner.h" -#include "test_utils.h" #include "esp_log.h" #include "esp_hid_common.h" #include "hid_descriptor.h" diff --git a/components/esp_hid/test_apps/main/test_esp_hid_main.c b/components/esp_hid/test_apps/main/test_esp_hid_main.c new file mode 100644 index 0000000000..62fdec9947 --- /dev/null +++ b/components/esp_hid/test_apps/main/test_esp_hid_main.c @@ -0,0 +1,48 @@ +/* + * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "unity.h" +#include "unity_test_runner.h" +#include "esp_heap_caps.h" + +#define TEST_MEMORY_LEAK_THRESHOLD_DEFAULT 0 +static int leak_threshold = TEST_MEMORY_LEAK_THRESHOLD_DEFAULT; +void set_leak_threshold(int threshold) +{ + leak_threshold = threshold; +} + +static size_t before_free_8bit; +static size_t before_free_32bit; + +static void check_leak(size_t before_free, size_t after_free, const char *type) +{ + ssize_t delta = after_free - before_free; + printf("MALLOC_CAP_%s: Before %u bytes free, After %u bytes free (delta %d)\n", type, before_free, after_free, delta); + TEST_ASSERT_MESSAGE(delta >= leak_threshold, "memory leak"); +} + +void setUp(void) +{ + before_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT); + before_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT); +} + +void tearDown(void) +{ + size_t after_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT); + size_t after_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT); + check_leak(before_free_8bit, after_free_8bit, "8BIT"); + check_leak(before_free_32bit, after_free_32bit, "32BIT"); + + leak_threshold = TEST_MEMORY_LEAK_THRESHOLD_DEFAULT; +} + +void app_main(void) +{ + printf("Running esp_hid component tests\n"); + unity_run_menu(); +} diff --git a/components/esp_hid/test_apps/pytest_esp_hid.py b/components/esp_hid/test_apps/pytest_esp_hid.py new file mode 100644 index 0000000000..664e29bf6f --- /dev/null +++ b/components/esp_hid/test_apps/pytest_esp_hid.py @@ -0,0 +1,12 @@ +# SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD +# SPDX-License-Identifier: CC0-1.0 + +import pytest +from pytest_embedded import Dut + + +@pytest.mark.generic +@pytest.mark.esp32 +@pytest.mark.esp32c3 +def test_esp_hid(dut: Dut) -> None: + dut.run_all_single_board_cases() diff --git a/components/esp_hid/test_apps/sdkconfig.defaults b/components/esp_hid/test_apps/sdkconfig.defaults new file mode 100644 index 0000000000..7b569fa075 --- /dev/null +++ b/components/esp_hid/test_apps/sdkconfig.defaults @@ -0,0 +1 @@ +CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0=n diff --git a/tools/ci/check_copyright_ignore.txt b/tools/ci/check_copyright_ignore.txt index 9b77f5ab63..9405c40e15 100644 --- a/tools/ci/check_copyright_ignore.txt +++ b/tools/ci/check_copyright_ignore.txt @@ -419,8 +419,6 @@ components/esp_hid/private/bt_hidd.h components/esp_hid/private/bt_hidh.h components/esp_hid/private/esp_hidd_private.h components/esp_hid/src/esp_hid_common.c -components/esp_hid/test/hid_descriptor.h -components/esp_hid/test/test_esp_hid.c components/esp_local_ctrl/src/esp_local_ctrl_handler.c components/esp_local_ctrl/src/esp_local_ctrl_priv.h components/esp_local_ctrl/src/esp_local_ctrl_transport_ble.c