- Removed unnecessary calls to relay_chn_create and g_is_component_initialized in multiple test cases across test_relay_chn_core_single.c, test_relay_chn_listener_multi.c, and test_relay_chn_listener_single.c. - Introduced new test files for NVS functionality: test_relay_chn_nvs_multi.c and test_relay_chn_nvs_single.c, covering initialization, direction setting, invalid parameters, and erase operations. - Updated partition table configuration to support NVS storage, including the addition of a new partition file part_nvs.csv. - Adjusted sdkconfig files to enable NVS support and configure custom partition settings for relay channels.
102 lines
2.6 KiB
C
102 lines
2.6 KiB
C
|
|
#include <stdbool.h>
|
|
#include "esp_log.h"
|
|
#include "esp_system.h"
|
|
#include "unity.h"
|
|
#include "unity_internals.h"
|
|
#include "unity_test_runner.h"
|
|
#include "test_common.h"
|
|
|
|
#if RELAY_CHN_ENABLE_NVS == 1
|
|
#include "nvs_flash.h"
|
|
#include "relay_chn_nvs.h"
|
|
#endif
|
|
|
|
|
|
#ifndef RELAY_CHN_UNITY_TEST_GROUP_TAG
|
|
#warning "RELAY_CHN_UNITY_TEST_GROUP_TAG is not defined, using default 'relay_chn'"
|
|
#define RELAY_CHN_UNITY_TEST_GROUP_TAG "relay_chn"
|
|
#endif
|
|
|
|
void setUp()
|
|
{
|
|
|
|
}
|
|
|
|
void tearDown()
|
|
{
|
|
reset_channels_to_idle_state();
|
|
}
|
|
|
|
static void test_nvs_flash_init(void)
|
|
{
|
|
esp_err_t ret;
|
|
#if RELAY_CHN_NVS_CUSTOM_PARTITION == 1
|
|
ret = nvs_flash_init_partition(RELAY_CHN_NVS_CUSTOM_PARTITION_NAME);
|
|
ESP_LOGI(TEST_TAG, "test_nvs_flash_init: NVS flash init partition return: %s", esp_err_to_name(ret));
|
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
|
// NVS partition is truncated and needs to be erased
|
|
ret = nvs_flash_erase_partition(RELAY_CHN_NVS_CUSTOM_PARTITION_NAME);
|
|
if (ret == ESP_OK) {
|
|
ret = nvs_flash_init_partition(RELAY_CHN_NVS_CUSTOM_PARTITION_NAME);
|
|
}
|
|
}
|
|
#else
|
|
ret = nvs_flash_init();
|
|
ESP_LOGI(TEST_TAG, "test_nvs_flash_init: NVS flash init return: %s", esp_err_to_name(ret));
|
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
|
// NVS partition is truncated and needs to be erased
|
|
ret = nvs_flash_erase();
|
|
if (ret == ESP_OK) {
|
|
ret = nvs_flash_init();
|
|
}
|
|
}
|
|
#endif
|
|
TEST_ESP_OK(ret);
|
|
}
|
|
|
|
static void test_nvs_flash_deinit(void)
|
|
{
|
|
esp_err_t ret;
|
|
#if RELAY_CHN_NVS_CUSTOM_PARTITION == 1
|
|
ret = nvs_flash_deinit_partition(RELAY_CHN_NVS_CUSTOM_PARTITION_NAME);
|
|
#else
|
|
ret = nvs_flash_deinit();
|
|
#endif
|
|
TEST_ESP_OK(ret);
|
|
}
|
|
|
|
void app_main(void)
|
|
{
|
|
// Init NVS once for all tests
|
|
test_nvs_flash_init();
|
|
|
|
// Create relay_chn once for all tests
|
|
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
|
|
|
|
UNITY_BEGIN();
|
|
|
|
// Log general test information
|
|
ESP_LOGI(TEST_TAG, "Available test count: %d", unity_get_test_count());
|
|
ESP_LOGI(TEST_TAG, "Running tests for tag: %s", RELAY_CHN_UNITY_TEST_GROUP_TAG);
|
|
|
|
if (strncmp(RELAY_CHN_UNITY_TEST_GROUP_TAG, "all", strlen("all")) == 0) {
|
|
unity_run_all_tests();
|
|
}
|
|
else {
|
|
unity_run_tests_by_tag(RELAY_CHN_UNITY_TEST_GROUP_TAG, false);
|
|
}
|
|
|
|
UNITY_END();
|
|
|
|
// Destroy relay_chn
|
|
relay_chn_destroy();
|
|
|
|
// Deinit NVS
|
|
test_nvs_flash_deinit();
|
|
|
|
ESP_LOGI(TEST_TAG, "All tests complete.");
|
|
|
|
esp_restart(); // Restart to invoke qemu exit
|
|
}
|