diff --git a/test/test_wifimanager/test_main.cpp b/test/test_wifimanager/test_main.cpp index ffc32da..31157f6 100644 --- a/test/test_wifimanager/test_main.cpp +++ b/test/test_wifimanager/test_main.cpp @@ -5,7 +5,101 @@ // Test case array TestCase tests[] = { + // Basic tests TEST_ENTRY(test_basic_wifimanager_instantiation), + TEST_ENTRY(test_reset_settings), + TEST_ENTRY(test_disconnect), + + // Configuration tests + TEST_ENTRY(test_set_title), + TEST_ENTRY(test_set_config_portal_timeout), + TEST_ENTRY(test_set_connect_timeout), + TEST_ENTRY(test_set_http_port), + TEST_ENTRY(test_set_minimum_signal_quality), + TEST_ENTRY(test_set_remove_duplicate_aps), + TEST_ENTRY(test_set_show_static_fields), + TEST_ENTRY(test_set_show_dns_fields), + TEST_ENTRY(test_set_config_portal_blocking), + TEST_ENTRY(test_set_captive_portal_enable), + TEST_ENTRY(test_set_hostname), + TEST_ENTRY(test_set_wifi_ap_channel), + TEST_ENTRY(test_set_wifi_ap_hidden), + TEST_ENTRY(test_set_clean_connect), + TEST_ENTRY(test_set_country), + TEST_ENTRY(test_set_wifi_auto_reconnect), + TEST_ENTRY(test_get_default_ap_name), + TEST_ENTRY(test_get_wifi_status_string), + TEST_ENTRY(test_get_mode_string), + TEST_ENTRY(test_get_rssi_as_quality), + + // Portal lifecycle tests + TEST_ENTRY(test_start_config_portal), + TEST_ENTRY(test_start_config_portal_auto_name), + TEST_ENTRY(test_stop_config_portal), + TEST_ENTRY(test_config_portal_infrastructure), + TEST_ENTRY(test_start_web_portal), + TEST_ENTRY(test_stop_web_portal), + TEST_ENTRY(test_config_portal_multiple_start_stop), + TEST_ENTRY(test_config_portal_already_active), + TEST_ENTRY(test_get_config_portal_ssid), + + // Non-blocking tests + TEST_ENTRY(test_nonblocking_mode_setup), + TEST_ENTRY(test_nonblocking_process), + TEST_ENTRY(test_ap_client_check), + TEST_ENTRY(test_web_portal_client_check), + TEST_ENTRY(test_nonblocking_timeout_behavior), + + // Callback tests + TEST_ENTRY(test_ap_callback), + TEST_ENTRY(test_web_server_callback), + TEST_ENTRY(test_config_reset_callback), + TEST_ENTRY(test_save_config_callback_registration), + TEST_ENTRY(test_save_params_callback_registration), + TEST_ENTRY(test_config_portal_timeout_callback_registration), + TEST_ENTRY(test_multiple_callbacks), + + // Parameter tests + TEST_ENTRY(test_create_parameter), + TEST_ENTRY(test_add_parameter), + TEST_ENTRY(test_get_parameters), + TEST_ENTRY(test_parameter_with_custom_html), + TEST_ENTRY(test_parameter_value_length), + TEST_ENTRY(test_parameter_label_placement), + TEST_ENTRY(test_multiple_parameters), + TEST_ENTRY(test_parameter_placeholder), + + // Static IP tests + TEST_ENTRY(test_set_ap_static_ip_config), + TEST_ENTRY(test_set_sta_static_ip_config), + TEST_ENTRY(test_set_sta_static_ip_config_with_dns), + TEST_ENTRY(test_show_static_fields), + TEST_ENTRY(test_show_dns_fields), + TEST_ENTRY(test_ap_static_ip_application), + + // Advanced options tests + TEST_ENTRY(test_set_break_after_config), + TEST_ENTRY(test_set_save_connect), + TEST_ENTRY(test_set_restore_persistent), + TEST_ENTRY(test_set_show_password), + TEST_ENTRY(test_set_scan_disp_perc), + TEST_ENTRY(test_set_enable_config_portal), + TEST_ENTRY(test_set_disable_config_portal), + TEST_ENTRY(test_set_connect_retries), + TEST_ENTRY(test_set_save_connect_timeout), + TEST_ENTRY(test_set_show_info_erase), + TEST_ENTRY(test_set_show_info_update), + TEST_ENTRY(test_set_custom_head_element), + TEST_ENTRY(test_set_custom_body_header), + TEST_ENTRY(test_set_custom_body_footer), + TEST_ENTRY(test_set_custom_menu_html), + TEST_ENTRY(test_set_menu), + TEST_ENTRY(test_set_params_page), + TEST_ENTRY(test_set_dark_mode), + TEST_ENTRY(test_set_class), + TEST_ENTRY(test_preload_wifi), + TEST_ENTRY(test_debug_soft_ap_config), + TEST_ENTRY(test_debug_platform_info), }; const size_t TEST_COUNT = sizeof(tests) / sizeof(TestCase); diff --git a/test/test_wifimanager/test_main.h b/test/test_wifimanager/test_main.h index 7dfeb7f..1454128 100644 --- a/test/test_wifimanager/test_main.h +++ b/test/test_wifimanager/test_main.h @@ -15,8 +15,101 @@ struct TestCase { #define TEST_ENTRY(fn) { #fn, fn, __LINE__ } -// Test function declarations +// Basic tests void test_basic_wifimanager_instantiation(); +void test_reset_settings(); +void test_disconnect(); + +// Configuration tests +void test_set_title(); +void test_set_config_portal_timeout(); +void test_set_connect_timeout(); +void test_set_http_port(); +void test_set_minimum_signal_quality(); +void test_set_remove_duplicate_aps(); +void test_set_show_static_fields(); +void test_set_show_dns_fields(); +void test_set_config_portal_blocking(); +void test_set_captive_portal_enable(); +void test_set_hostname(); +void test_set_wifi_ap_channel(); +void test_set_wifi_ap_hidden(); +void test_set_clean_connect(); +void test_set_country(); +void test_set_wifi_auto_reconnect(); +void test_get_default_ap_name(); +void test_get_wifi_status_string(); +void test_get_mode_string(); +void test_get_rssi_as_quality(); + +// Portal lifecycle tests +void test_start_config_portal(); +void test_start_config_portal_auto_name(); +void test_stop_config_portal(); +void test_config_portal_infrastructure(); +void test_start_web_portal(); +void test_stop_web_portal(); +void test_config_portal_multiple_start_stop(); +void test_config_portal_already_active(); +void test_get_config_portal_ssid(); + +// Non-blocking tests +void test_nonblocking_mode_setup(); +void test_nonblocking_process(); +void test_ap_client_check(); +void test_web_portal_client_check(); +void test_nonblocking_timeout_behavior(); + +// Callback tests +void test_ap_callback(); +void test_web_server_callback(); +void test_config_reset_callback(); +void test_save_config_callback_registration(); +void test_save_params_callback_registration(); +void test_config_portal_timeout_callback_registration(); +void test_multiple_callbacks(); + +// Parameter tests +void test_create_parameter(); +void test_add_parameter(); +void test_get_parameters(); +void test_parameter_with_custom_html(); +void test_parameter_value_length(); +void test_parameter_label_placement(); +void test_multiple_parameters(); +void test_parameter_placeholder(); + +// Static IP tests +void test_set_ap_static_ip_config(); +void test_set_sta_static_ip_config(); +void test_set_sta_static_ip_config_with_dns(); +void test_show_static_fields(); +void test_show_dns_fields(); +void test_ap_static_ip_application(); + +// Advanced options tests +void test_set_break_after_config(); +void test_set_save_connect(); +void test_set_restore_persistent(); +void test_set_show_password(); +void test_set_scan_disp_perc(); +void test_set_enable_config_portal(); +void test_set_disable_config_portal(); +void test_set_connect_retries(); +void test_set_save_connect_timeout(); +void test_set_show_info_erase(); +void test_set_show_info_update(); +void test_set_custom_head_element(); +void test_set_custom_body_header(); +void test_set_custom_body_footer(); +void test_set_custom_menu_html(); +void test_set_menu(); +void test_set_params_page(); +void test_set_dark_mode(); +void test_set_class(); +void test_preload_wifi(); +void test_debug_soft_ap_config(); +void test_debug_platform_info(); #endif // TEST_MAIN_H diff --git a/test/test_wifimanager/tests/test_advanced_options.cpp b/test/test_wifimanager/tests/test_advanced_options.cpp new file mode 100644 index 0000000..cb364b7 --- /dev/null +++ b/test/test_wifimanager/tests/test_advanced_options.cpp @@ -0,0 +1,305 @@ +#include +#include +#include + +// Test advanced configuration options +void test_set_break_after_config() { + Serial.println("[TEST] Testing setBreakAfterConfig()..."); + + WiFiManager wm; + + wm.setBreakAfterConfig(true); + wm.setBreakAfterConfig(false); + + TEST_ASSERT_TRUE_MESSAGE(true, "setBreakAfterConfig() executed without crash"); + + Serial.println("[TEST] setBreakAfterConfig() test completed successfully"); +} + +void test_set_save_connect() { + Serial.println("[TEST] Testing setSaveConnect()..."); + + WiFiManager wm; + + wm.setSaveConnect(true); + wm.setSaveConnect(false); + + TEST_ASSERT_TRUE_MESSAGE(true, "setSaveConnect() executed without crash"); + + Serial.println("[TEST] setSaveConnect() test completed successfully"); +} + +void test_set_restore_persistent() { + Serial.println("[TEST] Testing setRestorePersistent()..."); + + WiFiManager wm; + + wm.setRestorePersistent(true); + wm.setRestorePersistent(false); + + TEST_ASSERT_TRUE_MESSAGE(true, "setRestorePersistent() executed without crash"); + + Serial.println("[TEST] setRestorePersistent() test completed successfully"); +} + +void test_set_show_password() { + Serial.println("[TEST] Testing setShowPassword()..."); + + WiFiManager wm; + + wm.setShowPassword(true); + wm.setShowPassword(false); + + TEST_ASSERT_TRUE_MESSAGE(true, "setShowPassword() executed without crash"); + + Serial.println("[TEST] setShowPassword() test completed successfully"); +} + +void test_set_scan_disp_perc() { + Serial.println("[TEST] Testing setScanDispPerc()..."); + + WiFiManager wm; + + wm.setScanDispPerc(true); + wm.setScanDispPerc(false); + + TEST_ASSERT_TRUE_MESSAGE(true, "setScanDispPerc() executed without crash"); + + Serial.println("[TEST] setScanDispPerc() test completed successfully"); +} + +void test_set_enable_config_portal() { + Serial.println("[TEST] Testing setEnableConfigPortal()..."); + + WiFiManager wm; + + wm.setEnableConfigPortal(true); + wm.setEnableConfigPortal(false); + + TEST_ASSERT_TRUE_MESSAGE(true, "setEnableConfigPortal() executed without crash"); + + Serial.println("[TEST] setEnableConfigPortal() test completed successfully"); +} + +void test_set_disable_config_portal() { + Serial.println("[TEST] Testing setDisableConfigPortal()..."); + + WiFiManager wm; + + wm.setDisableConfigPortal(true); + wm.setDisableConfigPortal(false); + + TEST_ASSERT_TRUE_MESSAGE(true, "setDisableConfigPortal() executed without crash"); + + Serial.println("[TEST] setDisableConfigPortal() test completed successfully"); +} + +void test_set_connect_retries() { + Serial.println("[TEST] Testing setConnectRetries()..."); + + WiFiManager wm; + + wm.setConnectRetries(1); + wm.setConnectRetries(3); + wm.setConnectRetries(5); + + TEST_ASSERT_TRUE_MESSAGE(true, "setConnectRetries() executed without crash"); + + Serial.println("[TEST] setConnectRetries() test completed successfully"); +} + +void test_set_save_connect_timeout() { + Serial.println("[TEST] Testing setSaveConnectTimeout()..."); + + WiFiManager wm; + + wm.setSaveConnectTimeout(0); + wm.setSaveConnectTimeout(10); + wm.setSaveConnectTimeout(30); + + TEST_ASSERT_TRUE_MESSAGE(true, "setSaveConnectTimeout() executed without crash"); + + Serial.println("[TEST] setSaveConnectTimeout() test completed successfully"); +} + +void test_set_show_info_erase() { + Serial.println("[TEST] Testing setShowInfoErase()..."); + + WiFiManager wm; + + wm.setShowInfoErase(true); + wm.setShowInfoErase(false); + + TEST_ASSERT_TRUE_MESSAGE(true, "setShowInfoErase() executed without crash"); + + Serial.println("[TEST] setShowInfoErase() test completed successfully"); +} + +void test_set_show_info_update() { + Serial.println("[TEST] Testing setShowInfoUpdate()..."); + + WiFiManager wm; + + wm.setShowInfoUpdate(true); + wm.setShowInfoUpdate(false); + + TEST_ASSERT_TRUE_MESSAGE(true, "setShowInfoUpdate() executed without crash"); + + Serial.println("[TEST] setShowInfoUpdate() test completed successfully"); +} + +void test_set_custom_head_element() { + Serial.println("[TEST] Testing setCustomHeadElement()..."); + + WiFiManager wm; + + wm.setCustomHeadElement(""); + wm.setCustomHeadElement(""); + + TEST_ASSERT_TRUE_MESSAGE(true, "setCustomHeadElement() executed without crash"); + + Serial.println("[TEST] setCustomHeadElement() test completed successfully"); +} + +void test_set_custom_body_header() { + Serial.println("[TEST] Testing setCustomBodyHeader()..."); + + WiFiManager wm; + + wm.setCustomBodyHeader("
Custom Header
"); + wm.setCustomBodyHeader(""); + + TEST_ASSERT_TRUE_MESSAGE(true, "setCustomBodyHeader() executed without crash"); + + Serial.println("[TEST] setCustomBodyHeader() test completed successfully"); +} + +void test_set_custom_body_footer() { + Serial.println("[TEST] Testing setCustomBodyFooter()..."); + + WiFiManager wm; + + wm.setCustomBodyFooter("
Custom Footer
"); + wm.setCustomBodyFooter(""); + + TEST_ASSERT_TRUE_MESSAGE(true, "setCustomBodyFooter() executed without crash"); + + Serial.println("[TEST] setCustomBodyFooter() test completed successfully"); +} + +void test_set_custom_menu_html() { + Serial.println("[TEST] Testing setCustomMenuHTML()..."); + + WiFiManager wm; + + wm.setCustomMenuHTML("
Custom Menu
"); + wm.setCustomMenuHTML(""); + + TEST_ASSERT_TRUE_MESSAGE(true, "setCustomMenuHTML() executed without crash"); + + Serial.println("[TEST] setCustomMenuHTML() test completed successfully"); +} + +void test_set_menu() { + Serial.println("[TEST] Testing setMenu()..."); + + WiFiManager wm; + + // Test with array + const char* menu[] = {"wifi", "info", "exit"}; + wm.setMenu(menu, 3); + + // Test with vector + std::vector menuVec = {"wifi", "info", "param", "exit"}; + wm.setMenu(menuVec); + + TEST_ASSERT_TRUE_MESSAGE(true, "setMenu() executed without crash"); + + Serial.println("[TEST] setMenu() test completed successfully"); +} + +void test_set_params_page() { + Serial.println("[TEST] Testing setParamsPage()..."); + + WiFiManager wm; + + wm.setParamsPage(true); + wm.setParamsPage(false); + + TEST_ASSERT_TRUE_MESSAGE(true, "setParamsPage() executed without crash"); + + Serial.println("[TEST] setParamsPage() test completed successfully"); +} + +void test_set_dark_mode() { + Serial.println("[TEST] Testing setDarkMode()..."); + + WiFiManager wm; + + wm.setDarkMode(true); + wm.setDarkMode(false); + + TEST_ASSERT_TRUE_MESSAGE(true, "setDarkMode() executed without crash"); + + Serial.println("[TEST] setDarkMode() test completed successfully"); +} + +void test_set_class() { + Serial.println("[TEST] Testing setClass()..."); + + WiFiManager wm; + + wm.setClass("invert"); + wm.setClass("custom-class"); + wm.setClass(""); + + TEST_ASSERT_TRUE_MESSAGE(true, "setClass() executed without crash"); + + Serial.println("[TEST] setClass() test completed successfully"); +} + +void test_preload_wifi() { + Serial.println("[TEST] Testing preloadWiFi()..."); + + WiFiManager wm; + + // Preload WiFi credentials (without actually connecting) + wm.preloadWiFi("TestSSID", "TestPassword"); + + // Should return true/false, just verify no crash + TEST_ASSERT_TRUE_MESSAGE(true, "preloadWiFi() executed without crash"); + + Serial.println("[TEST] preloadWiFi() test completed successfully"); +} + +void test_debug_soft_ap_config() { + Serial.println("[TEST] Testing debugSoftAPConfig()..."); + + WiFiManager wm; + wm.setConfigPortalTimeout(5); + + wm.startConfigPortal("TestAP"); + + delay(100); + + // Debug output should not crash + wm.debugSoftAPConfig(); + + wm.stopConfigPortal(); + + Serial.println("[TEST] debugSoftAPConfig() test completed successfully"); +} + +void test_debug_platform_info() { + Serial.println("[TEST] Testing debugPlatformInfo()..."); + + WiFiManager wm; + + // Debug output should not crash + wm.debugPlatformInfo(); + + TEST_ASSERT_TRUE_MESSAGE(true, "debugPlatformInfo() executed without crash"); + + Serial.println("[TEST] debugPlatformInfo() test completed successfully"); +} + diff --git a/test/test_wifimanager/tests/test_basic.cpp b/test/test_wifimanager/tests/test_basic.cpp index 674b382..d2506a1 100644 --- a/test/test_wifimanager/tests/test_basic.cpp +++ b/test/test_wifimanager/tests/test_basic.cpp @@ -1,21 +1,56 @@ #include #include #include +#include "../test_main.h" // Test basic WiFiManager instantiation void test_basic_wifimanager_instantiation() { Serial.println("[TEST] Testing basic WiFiManager instantiation..."); - // Test that we can create a WiFiManager instance + // Test default constructor WiFiManager wm; - // Test that the instance was created (basic sanity check) + // Verify instance created (basic sanity check) TEST_ASSERT_TRUE_MESSAGE(true, "WiFiManager instance created successfully"); // Test that we can call basic methods without crashing - // Note: This is a very basic test - more detailed tests can be added later - TEST_ASSERT_TRUE_MESSAGE(true, "Basic WiFiManager functionality verified"); + String defaultName = wm.getDefaultAPName(); + TEST_ASSERT_GREATER_THAN(0, defaultName.length()); + + // Test default state - portal should not be active + TEST_ASSERT_FALSE(wm.getConfigPortalActive()); + TEST_ASSERT_FALSE(wm.getWebPortalActive()); Serial.println("[TEST] Basic WiFiManager instantiation test completed successfully"); } +// Test resetSettings clears credentials +void test_reset_settings() { + Serial.println("[TEST] Testing resetSettings()..."); + + WiFiManager wm; + + // Reset settings (should not crash) + wm.resetSettings(); + + // Verify WiFi is not saved after reset + TEST_ASSERT_FALSE(wm.getWiFiIsSaved()); + + Serial.println("[TEST] resetSettings() test completed successfully"); +} + +// Test disconnect without erasing credentials +void test_disconnect() { + Serial.println("[TEST] Testing disconnect()..."); + + WiFiManager wm; + + // Disconnect should not crash even if not connected + wm.disconnect(); + + // Disconnect returns true/false, just verify no crash + TEST_ASSERT_TRUE_MESSAGE(true, "disconnect() executed without crash"); + + Serial.println("[TEST] disconnect() test completed successfully"); +} + diff --git a/test/test_wifimanager/tests/test_callbacks.cpp b/test/test_wifimanager/tests/test_callbacks.cpp new file mode 100644 index 0000000..850e111 --- /dev/null +++ b/test/test_wifimanager/tests/test_callbacks.cpp @@ -0,0 +1,208 @@ +#include +#include +#include + +// Callback flags +bool ap_callback_fired = false; +bool web_server_callback_fired = false; +bool save_config_callback_fired = false; +bool pre_save_config_callback_fired = false; +bool save_params_callback_fired = false; +bool pre_save_params_callback_fired = false; +bool config_reset_callback_fired = false; +bool config_portal_timeout_callback_fired = false; + +// Callback functions +void ap_callback(WiFiManager* wm) { + ap_callback_fired = true; +} + +void web_server_callback() { + web_server_callback_fired = true; +} + +void save_config_callback() { + save_config_callback_fired = true; +} + +void pre_save_config_callback() { + pre_save_config_callback_fired = true; +} + +void save_params_callback() { + save_params_callback_fired = true; +} + +void pre_save_params_callback() { + pre_save_params_callback_fired = true; +} + +void config_reset_callback() { + config_reset_callback_fired = true; +} + +void config_portal_timeout_callback() { + config_portal_timeout_callback_fired = true; +} + +// Reset all callback flags +void reset_callback_flags() { + ap_callback_fired = false; + web_server_callback_fired = false; + save_config_callback_fired = false; + pre_save_config_callback_fired = false; + save_params_callback_fired = false; + pre_save_params_callback_fired = false; + config_reset_callback_fired = false; + config_portal_timeout_callback_fired = false; +} + +void test_ap_callback() { + Serial.println("[TEST] Testing AP callback..."); + + reset_callback_flags(); + + WiFiManager wm; + wm.setAPCallback(ap_callback); + wm.setConfigPortalTimeout(5); + + wm.startConfigPortal("TestAP"); + + // Callback should have fired when AP starts + TEST_ASSERT_TRUE(ap_callback_fired); + + wm.stopConfigPortal(); + + Serial.println("[TEST] AP callback test completed successfully"); +} + +void test_web_server_callback() { + Serial.println("[TEST] Testing web server callback..."); + + reset_callback_flags(); + + WiFiManager wm; + wm.setWebServerCallback(web_server_callback); + wm.setConfigPortalTimeout(5); + + wm.startConfigPortal("TestAP"); + + delay(100); // Give time for server to start + + // Callback should have fired when server starts + TEST_ASSERT_TRUE(web_server_callback_fired); + + wm.stopConfigPortal(); + + Serial.println("[TEST] Web server callback test completed successfully"); +} + +void test_config_reset_callback() { + Serial.println("[TEST] Testing config reset callback..."); + + reset_callback_flags(); + + WiFiManager wm; + wm.setConfigResetCallback(config_reset_callback); + + // Reset settings should trigger callback + wm.resetSettings(); + + // Note: Callback may or may not fire on resetSettings() depending on implementation + // Just verify it doesn't crash + TEST_ASSERT_TRUE_MESSAGE(true, "setConfigResetCallback() executed without crash"); + + Serial.println("[TEST] Config reset callback test completed successfully"); +} + +void test_save_config_callback_registration() { + Serial.println("[TEST] Testing save config callback registration..."); + + reset_callback_flags(); + + WiFiManager wm; + + // Register callback + wm.setSaveConfigCallback(save_config_callback); + wm.setPreSaveConfigCallback(pre_save_config_callback); + + // Note: These callbacks fire when WiFi is saved via web portal + // We can't easily test the actual firing without HTTP requests, + // but we can verify registration doesn't crash + + TEST_ASSERT_TRUE_MESSAGE(true, "Save config callbacks registered without crash"); + + Serial.println("[TEST] Save config callback registration test completed successfully"); +} + +void test_save_params_callback_registration() { + Serial.println("[TEST] Testing save params callback registration..."); + + reset_callback_flags(); + + WiFiManager wm; + + // Register callbacks + wm.setSaveParamsCallback(save_params_callback); + wm.setPreSaveParamsCallback(pre_save_params_callback); + + // Note: These callbacks fire when parameters are saved via web portal + // We can't easily test the actual firing without HTTP requests, + // but we can verify registration doesn't crash + + TEST_ASSERT_TRUE_MESSAGE(true, "Save params callbacks registered without crash"); + + Serial.println("[TEST] Save params callback registration test completed successfully"); +} + +void test_config_portal_timeout_callback_registration() { + Serial.println("[TEST] Testing config portal timeout callback registration..."); + + reset_callback_flags(); + + WiFiManager wm; + + // Register callback + wm.setConfigPortalTimeoutCallback(config_portal_timeout_callback); + + // Note: This callback fires when config portal times out + // We can't easily test the actual firing without waiting for timeout, + // but we can verify registration doesn't crash + + TEST_ASSERT_TRUE_MESSAGE(true, "Config portal timeout callback registered without crash"); + + Serial.println("[TEST] Config portal timeout callback registration test completed successfully"); +} + +void test_multiple_callbacks() { + Serial.println("[TEST] Testing multiple callbacks together..."); + + reset_callback_flags(); + + WiFiManager wm; + + // Register all callbacks + wm.setAPCallback(ap_callback); + wm.setWebServerCallback(web_server_callback); + wm.setSaveConfigCallback(save_config_callback); + wm.setPreSaveConfigCallback(pre_save_config_callback); + wm.setSaveParamsCallback(save_params_callback); + wm.setPreSaveParamsCallback(pre_save_params_callback); + wm.setConfigResetCallback(config_reset_callback); + wm.setConfigPortalTimeoutCallback(config_portal_timeout_callback); + + // Start portal - should fire AP and web server callbacks + wm.setConfigPortalTimeout(5); + wm.startConfigPortal("TestAP"); + + delay(100); + + // Verify AP callback fired + TEST_ASSERT_TRUE(ap_callback_fired); + TEST_ASSERT_TRUE(web_server_callback_fired); + + wm.stopConfigPortal(); + + Serial.println("[TEST] Multiple callbacks test completed successfully"); +} + diff --git a/test/test_wifimanager/tests/test_configuration.cpp b/test/test_wifimanager/tests/test_configuration.cpp new file mode 100644 index 0000000..6824629 --- /dev/null +++ b/test/test_wifimanager/tests/test_configuration.cpp @@ -0,0 +1,306 @@ +#include +#include +#include + +// Test configuration API methods +void test_set_title() { + Serial.println("[TEST] Testing setTitle()..."); + + WiFiManager wm; + String testTitle = "TestTitle"; + + wm.setTitle(testTitle); + + // Note: getTitle() doesn't exist, so we just verify no crash + // The title is used internally when rendering pages + TEST_ASSERT_TRUE_MESSAGE(true, "setTitle() executed without crash"); + + Serial.println("[TEST] setTitle() test completed successfully"); +} + +void test_set_config_portal_timeout() { + Serial.println("[TEST] Testing setConfigPortalTimeout()..."); + + WiFiManager wm; + + // Test various timeout values + wm.setConfigPortalTimeout(0); + wm.setConfigPortalTimeout(30); + wm.setConfigPortalTimeout(300); + + TEST_ASSERT_TRUE_MESSAGE(true, "setConfigPortalTimeout() executed without crash"); + + Serial.println("[TEST] setConfigPortalTimeout() test completed successfully"); +} + +void test_set_connect_timeout() { + Serial.println("[TEST] Testing setConnectTimeout()..."); + + WiFiManager wm; + + wm.setConnectTimeout(0); + wm.setConnectTimeout(20); + wm.setConnectTimeout(60); + + TEST_ASSERT_TRUE_MESSAGE(true, "setConnectTimeout() executed without crash"); + + Serial.println("[TEST] setConnectTimeout() test completed successfully"); +} + +void test_set_http_port() { + Serial.println("[TEST] Testing setHttpPort()..."); + + WiFiManager wm; + + // Test default port + wm.setHttpPort(80); + + // Test custom port + wm.setHttpPort(8080); + + TEST_ASSERT_TRUE_MESSAGE(true, "setHttpPort() executed without crash"); + + Serial.println("[TEST] setHttpPort() test completed successfully"); +} + +void test_set_minimum_signal_quality() { + Serial.println("[TEST] Testing setMinimumSignalQuality()..."); + + WiFiManager wm; + + wm.setMinimumSignalQuality(-1); // Disable filter + wm.setMinimumSignalQuality(0); + wm.setMinimumSignalQuality(50); + wm.setMinimumSignalQuality(100); + + TEST_ASSERT_TRUE_MESSAGE(true, "setMinimumSignalQuality() executed without crash"); + + Serial.println("[TEST] setMinimumSignalQuality() test completed successfully"); +} + +void test_set_remove_duplicate_aps() { + Serial.println("[TEST] Testing setRemoveDuplicateAPs()..."); + + WiFiManager wm; + + wm.setRemoveDuplicateAPs(true); + wm.setRemoveDuplicateAPs(false); + + TEST_ASSERT_TRUE_MESSAGE(true, "setRemoveDuplicateAPs() executed without crash"); + + Serial.println("[TEST] setRemoveDuplicateAPs() test completed successfully"); +} + +void test_set_show_static_fields() { + Serial.println("[TEST] Testing setShowStaticFields()..."); + + WiFiManager wm; + + wm.setShowStaticFields(true); + wm.setShowStaticFields(false); + + TEST_ASSERT_TRUE_MESSAGE(true, "setShowStaticFields() executed without crash"); + + Serial.println("[TEST] setShowStaticFields() test completed successfully"); +} + +void test_set_show_dns_fields() { + Serial.println("[TEST] Testing setShowDnsFields()..."); + + WiFiManager wm; + + wm.setShowDnsFields(true); + wm.setShowDnsFields(false); + + TEST_ASSERT_TRUE_MESSAGE(true, "setShowDnsFields() executed without crash"); + + Serial.println("[TEST] setShowDnsFields() test completed successfully"); +} + +void test_set_config_portal_blocking() { + Serial.println("[TEST] Testing setConfigPortalBlocking()..."); + + WiFiManager wm; + + wm.setConfigPortalBlocking(true); + wm.setConfigPortalBlocking(false); + + TEST_ASSERT_TRUE_MESSAGE(true, "setConfigPortalBlocking() executed without crash"); + + Serial.println("[TEST] setConfigPortalBlocking() test completed successfully"); +} + +void test_set_captive_portal_enable() { + Serial.println("[TEST] Testing setCaptivePortalEnable()..."); + + WiFiManager wm; + + wm.setCaptivePortalEnable(true); + wm.setCaptivePortalEnable(false); + + TEST_ASSERT_TRUE_MESSAGE(true, "setCaptivePortalEnable() executed without crash"); + + Serial.println("[TEST] setCaptivePortalEnable() test completed successfully"); +} + +void test_set_hostname() { + Serial.println("[TEST] Testing setHostname()..."); + + WiFiManager wm; + + // Test with char* + wm.setHostname("test-hostname"); + + // Test with String + wm.setHostname(String("test-hostname-2")); + + // Test getting hostname + String hostname = wm.getWiFiHostname(); + + TEST_ASSERT_TRUE_MESSAGE(true, "setHostname() and getWiFiHostname() executed without crash"); + + Serial.println("[TEST] setHostname() test completed successfully"); +} + +void test_set_wifi_ap_channel() { + Serial.println("[TEST] Testing setWiFiAPChannel()..."); + + WiFiManager wm; + + wm.setWiFiAPChannel(0); // Auto + wm.setWiFiAPChannel(1); + wm.setWiFiAPChannel(11); + + TEST_ASSERT_TRUE_MESSAGE(true, "setWiFiAPChannel() executed without crash"); + + Serial.println("[TEST] setWiFiAPChannel() test completed successfully"); +} + +void test_set_wifi_ap_hidden() { + Serial.println("[TEST] Testing setWiFiAPHidden()..."); + + WiFiManager wm; + + wm.setWiFiAPHidden(true); + wm.setWiFiAPHidden(false); + + TEST_ASSERT_TRUE_MESSAGE(true, "setWiFiAPHidden() executed without crash"); + + Serial.println("[TEST] setWiFiAPHidden() test completed successfully"); +} + +void test_set_clean_connect() { + Serial.println("[TEST] Testing setCleanConnect()..."); + + WiFiManager wm; + + wm.setCleanConnect(true); + wm.setCleanConnect(false); + + TEST_ASSERT_TRUE_MESSAGE(true, "setCleanConnect() executed without crash"); + + Serial.println("[TEST] setCleanConnect() test completed successfully"); +} + +void test_set_country() { + Serial.println("[TEST] Testing setCountry()..."); + + WiFiManager wm; + + wm.setCountry("US"); + wm.setCountry("CN"); + + TEST_ASSERT_TRUE_MESSAGE(true, "setCountry() executed without crash"); + + Serial.println("[TEST] setCountry() test completed successfully"); +} + +void test_set_wifi_auto_reconnect() { + Serial.println("[TEST] Testing setWiFiAutoReconnect()..."); + + WiFiManager wm; + + wm.setWiFiAutoReconnect(true); + wm.setWiFiAutoReconnect(false); + + TEST_ASSERT_TRUE_MESSAGE(true, "setWiFiAutoReconnect() executed without crash"); + + Serial.println("[TEST] setWiFiAutoReconnect() test completed successfully"); +} + +void test_get_default_ap_name() { + Serial.println("[TEST] Testing getDefaultAPName()..."); + + WiFiManager wm; + + String apName = wm.getDefaultAPName(); + + // Should return a non-empty string (chip ID based) + TEST_ASSERT_GREATER_THAN(0, apName.length()); + + Serial.println("[TEST] getDefaultAPName() test completed successfully"); +} + +void test_get_wifi_status_string() { + Serial.println("[TEST] Testing getWLStatusString()..."); + + WiFiManager wm; + + // Test with status code + String status1 = wm.getWLStatusString(WL_IDLE_STATUS); + String status2 = wm.getWLStatusString(WL_CONNECTED); + String status3 = wm.getWLStatusString(WL_DISCONNECTED); + + // All should return non-empty strings + TEST_ASSERT_GREATER_THAN(0, status1.length()); + TEST_ASSERT_GREATER_THAN(0, status2.length()); + TEST_ASSERT_GREATER_THAN(0, status3.length()); + + // Test without parameter (uses current WiFi.status()) + String status4 = wm.getWLStatusString(); + TEST_ASSERT_GREATER_THAN(0, status4.length()); + + Serial.println("[TEST] getWLStatusString() test completed successfully"); +} + +void test_get_mode_string() { + Serial.println("[TEST] Testing getModeString()..."); + + WiFiManager wm; + + String mode1 = wm.getModeString(WIFI_OFF); + String mode2 = wm.getModeString(WIFI_STA); + String mode3 = wm.getModeString(WIFI_AP); + String mode4 = wm.getModeString(WIFI_AP_STA); + + // All should return non-empty strings + TEST_ASSERT_GREATER_THAN(0, mode1.length()); + TEST_ASSERT_GREATER_THAN(0, mode2.length()); + TEST_ASSERT_GREATER_THAN(0, mode3.length()); + TEST_ASSERT_GREATER_THAN(0, mode4.length()); + + Serial.println("[TEST] getModeString() test completed successfully"); +} + +void test_get_rssi_as_quality() { + Serial.println("[TEST] Testing getRSSIasQuality()..."); + + WiFiManager wm; + + // Test various RSSI values + int quality1 = wm.getRSSIasQuality(-30); // Very strong + int quality2 = wm.getRSSIasQuality(-70); // Good + int quality3 = wm.getRSSIasQuality(-90); // Weak + + // Quality should be 0-100 + TEST_ASSERT_GREATER_OR_EQUAL(0, quality1); + TEST_ASSERT_LESS_OR_EQUAL(100, quality1); + TEST_ASSERT_GREATER_OR_EQUAL(0, quality2); + TEST_ASSERT_LESS_OR_EQUAL(100, quality2); + + // Stronger signal should have higher quality + TEST_ASSERT_GREATER_THAN(quality3, quality1); + + Serial.println("[TEST] getRSSIasQuality() test completed successfully"); +} + diff --git a/test/test_wifimanager/tests/test_nonblocking.cpp b/test/test_wifimanager/tests/test_nonblocking.cpp new file mode 100644 index 0000000..b25bbfe --- /dev/null +++ b/test/test_wifimanager/tests/test_nonblocking.cpp @@ -0,0 +1,155 @@ +#include +#include +#include + +// Test non-blocking configuration portal +void test_nonblocking_mode_setup() { + Serial.println("[TEST] Testing non-blocking mode setup..."); + + WiFiManager wm; + + // Enable non-blocking mode + wm.setConfigPortalBlocking(false); + wm.setConfigPortalTimeout(10); + + // Start portal + wm.startConfigPortal("TestAP"); + + // Verify portal is active + TEST_ASSERT_TRUE(wm.getConfigPortalActive()); + + // Process should not block + unsigned long start = millis(); + wm.process(); + unsigned long elapsed = millis() - start; + + // Should return quickly (< 200ms) + TEST_ASSERT_LESS_THAN(200, elapsed); + + wm.stopConfigPortal(); + + Serial.println("[TEST] Non-blocking mode setup test completed successfully"); +} + +void test_nonblocking_process() { + Serial.println("[TEST] Testing non-blocking process() calls..."); + + WiFiManager wm; + + wm.setConfigPortalBlocking(false); + wm.setConfigPortalTimeout(10); + wm.startConfigPortal("TestAP"); + + // Call process multiple times - should not block + for (int i = 0; i < 10; i++) { + unsigned long start = millis(); + wm.process(); + unsigned long elapsed = millis() - start; + + // Each call should be fast + TEST_ASSERT_LESS_THAN(100, elapsed); + + delay(10); + } + + // Portal should still be active + TEST_ASSERT_TRUE(wm.getConfigPortalActive()); + + wm.stopConfigPortal(); + + Serial.println("[TEST] Non-blocking process() test completed successfully"); +} + +void test_ap_client_check() { + Serial.println("[TEST] Testing setAPClientCheck()..."); + + WiFiManager wm; + + // Test enabling client check + wm.setAPClientCheck(true); + + // Test disabling client check + wm.setAPClientCheck(false); + + // Use non-blocking mode + wm.setConfigPortalBlocking(false); + wm.setConfigPortalTimeout(10); + + // Start portal + // Note: In non-blocking mode, startConfigPortal() returns false even when successful + wm.startConfigPortal("TestAP"); + + delay(100); + + // Should not crash and portal should be active + TEST_ASSERT_TRUE(wm.getConfigPortalActive()); + + wm.stopConfigPortal(); + + Serial.println("[TEST] setAPClientCheck() test completed successfully"); +} + +void test_web_portal_client_check() { + Serial.println("[TEST] Testing setWebPortalClientCheck()..."); + + WiFiManager wm; + + // Test enabling client check + wm.setWebPortalClientCheck(true); + + // Test disabling client check + wm.setWebPortalClientCheck(false); + + // Start web portal + wm.setConfigPortalTimeout(5); + wm.startWebPortal(); + + // Should not crash + TEST_ASSERT_TRUE(wm.getWebPortalActive()); + + wm.stopWebPortal(); + + Serial.println("[TEST] setWebPortalClientCheck() test completed successfully"); +} + +void test_nonblocking_timeout_behavior() { + Serial.println("[TEST] Testing non-blocking timeout behavior..."); + + WiFiManager wm; + + wm.setConfigPortalBlocking(false); + wm.setConfigPortalTimeout(2); // 2 second timeout + wm.startConfigPortal("TestAP"); + + unsigned long start = millis(); + bool stillActive = true; + + // Process until timeout or max wait + while (stillActive && (millis() - start < 5000)) { + wm.process(); + + // Check if portal is still active (may become inactive after timeout) + if (!wm.getConfigPortalActive()) { + stillActive = false; + break; // Exit loop if portal has closed + } + + delay(50); + } + + // After timeout, portal should be inactive (may have auto-closed) + // Just verify we can check the state without crashing + bool portalState = wm.getConfigPortalActive(); + + // Only call stopConfigPortal if portal is still active + // If it auto-closed on timeout, calling stopConfigPortal may crash + if (portalState) { + wm.stopConfigPortal(); + } + + // Test completed without crash + TEST_ASSERT_TRUE_MESSAGE(true, "Non-blocking timeout test completed without crash"); + + Serial.println("[TEST] Non-blocking timeout behavior test completed successfully"); +} + diff --git a/test/test_wifimanager/tests/test_parameters.cpp b/test/test_wifimanager/tests/test_parameters.cpp new file mode 100644 index 0000000..c296b92 --- /dev/null +++ b/test/test_wifimanager/tests/test_parameters.cpp @@ -0,0 +1,151 @@ +#include +#include +#include + +// Test custom parameters +void test_create_parameter() { + Serial.println("[TEST] Testing WiFiManagerParameter creation..."); + + // Create parameter with basic constructor + WiFiManagerParameter param("test_id", "Test Label", "default_value", 40); + + // Verify basic getters work + TEST_ASSERT_EQUAL_STRING("test_id", param.getID()); + TEST_ASSERT_EQUAL_STRING("Test Label", param.getLabel()); + TEST_ASSERT_EQUAL_STRING("default_value", param.getValue()); + TEST_ASSERT_EQUAL(40, param.getValueLength()); + + Serial.println("[TEST] WiFiManagerParameter creation test completed successfully"); +} + +void test_add_parameter() { + Serial.println("[TEST] Testing addParameter()..."); + + WiFiManager wm; + + WiFiManagerParameter param1("param1", "Parameter 1", "value1", 40); + WiFiManagerParameter param2("param2", "Parameter 2", "value2", 40); + + // Add parameters + bool result1 = wm.addParameter(¶m1); + bool result2 = wm.addParameter(¶m2); + + TEST_ASSERT_TRUE(result1); + TEST_ASSERT_TRUE(result2); + + // Verify parameters are stored + TEST_ASSERT_EQUAL(2, wm.getParametersCount()); + + Serial.println("[TEST] addParameter() test completed successfully"); +} + +void test_get_parameters() { + Serial.println("[TEST] Testing getParameters() and getParametersCount()..."); + + WiFiManager wm; + + WiFiManagerParameter param1("param1", "Parameter 1", "value1", 40); + WiFiManagerParameter param2("param2", "Parameter 2", "value2", 40); + WiFiManagerParameter param3("param3", "Parameter 3", "value3", 40); + + wm.addParameter(¶m1); + wm.addParameter(¶m2); + wm.addParameter(¶m3); + + // Get count + int count = wm.getParametersCount(); + TEST_ASSERT_EQUAL(3, count); + + // Get parameters array + WiFiManagerParameter** params = wm.getParameters(); + TEST_ASSERT_NOT_NULL(params); + + // Verify we can access parameters + TEST_ASSERT_EQUAL_STRING("param1", params[0]->getID()); + TEST_ASSERT_EQUAL_STRING("param2", params[1]->getID()); + TEST_ASSERT_EQUAL_STRING("param3", params[2]->getID()); + + Serial.println("[TEST] getParameters() test completed successfully"); +} + +void test_parameter_with_custom_html() { + Serial.println("[TEST] Testing WiFiManagerParameter with custom HTML..."); + + // Create parameter with custom HTML + const char* custom_html = ""; + WiFiManagerParameter param(custom_html); + + // Verify custom HTML is accessible + const char* html = param.getCustomHTML(); + TEST_ASSERT_NOT_NULL(html); + + Serial.println("[TEST] Parameter with custom HTML test completed successfully"); +} + +void test_parameter_value_length() { + Serial.println("[TEST] Testing WiFiManagerParameter value length..."); + + WiFiManagerParameter param("test_id", "Test Label", "default", 10); + + TEST_ASSERT_EQUAL(10, param.getValueLength()); + + // Try to set value within length + param.setValue("short", 10); + TEST_ASSERT_EQUAL_STRING("short", param.getValue()); + + Serial.println("[TEST] Parameter value length test completed successfully"); +} + +void test_parameter_label_placement() { + Serial.println("[TEST] Testing WiFiManagerParameter label placement..."); + + WiFiManagerParameter param("test_id", "Test Label", "default", 40, "", WFM_LABEL_BEFORE); + + TEST_ASSERT_EQUAL(WFM_LABEL_BEFORE, param.getLabelPlacement()); + + WiFiManagerParameter param2("test_id2", "Test Label 2", "default", 40, "", WFM_LABEL_AFTER); + + TEST_ASSERT_EQUAL(WFM_LABEL_AFTER, param2.getLabelPlacement()); + + Serial.println("[TEST] Parameter label placement test completed successfully"); +} + +void test_multiple_parameters() { + Serial.println("[TEST] Testing multiple parameters..."); + + WiFiManager wm; + + // Add several parameters + WiFiManagerParameter p1("mqtt_server", "MQTT Server", "192.168.1.1", 40); + WiFiManagerParameter p2("mqtt_port", "MQTT Port", "1883", 6); + WiFiManagerParameter p3("api_key", "API Key", "default_key", 32); + + wm.addParameter(&p1); + wm.addParameter(&p2); + wm.addParameter(&p3); + + TEST_ASSERT_EQUAL(3, wm.getParametersCount()); + + WiFiManagerParameter** params = wm.getParameters(); + + // Verify all parameters are accessible + TEST_ASSERT_EQUAL_STRING("mqtt_server", params[0]->getID()); + TEST_ASSERT_EQUAL_STRING("mqtt_port", params[1]->getID()); + TEST_ASSERT_EQUAL_STRING("api_key", params[2]->getID()); + + Serial.println("[TEST] Multiple parameters test completed successfully"); +} + +void test_parameter_placeholder() { + Serial.println("[TEST] Testing WiFiManagerParameter placeholder (deprecated)..."); + + WiFiManagerParameter param("test_id", "Test Label", "default", 40); + + // getPlaceholder() is deprecated but should still work + const char* placeholder = param.getPlaceholder(); + // May return label or empty, just verify no crash + TEST_ASSERT_NOT_NULL(placeholder); + + Serial.println("[TEST] Parameter placeholder test completed successfully"); +} + diff --git a/test/test_wifimanager/tests/test_portal_lifecycle.cpp b/test/test_wifimanager/tests/test_portal_lifecycle.cpp new file mode 100644 index 0000000..9e7e465 --- /dev/null +++ b/test/test_wifimanager/tests/test_portal_lifecycle.cpp @@ -0,0 +1,254 @@ +#include +#include +#include + +// Test config portal lifecycle +void test_start_config_portal() { + Serial.println("[TEST] Testing startConfigPortal()..."); + + WiFiManager wm; + + // Use non-blocking mode so we can control when to stop + wm.setConfigPortalBlocking(false); + wm.setConfigPortalTimeout(10); // Longer timeout + + // Start portal with SSID only + // Note: In non-blocking mode, startConfigPortal() returns false even when successful + // So we check getConfigPortalActive() instead of the return value + wm.startConfigPortal("TestAP"); + TEST_ASSERT_TRUE(wm.getConfigPortalActive()); + + delay(100); // Give it time to initialize + + wm.stopConfigPortal(); + TEST_ASSERT_FALSE(wm.getConfigPortalActive()); + delay(100); + + // Start portal with SSID and password + // Note: In non-blocking mode, startConfigPortal() returns false even when successful + wm.startConfigPortal("TestAP2", "password123"); + TEST_ASSERT_TRUE(wm.getConfigPortalActive()); + + delay(100); + + wm.stopConfigPortal(); + TEST_ASSERT_FALSE(wm.getConfigPortalActive()); + + Serial.println("[TEST] startConfigPortal() test completed successfully"); +} + +void test_start_config_portal_auto_name() { + Serial.println("[TEST] Testing startConfigPortal() with auto-generated name..."); + + WiFiManager wm; + + // Use non-blocking mode + wm.setConfigPortalBlocking(false); + wm.setConfigPortalTimeout(10); + + // Start portal without SSID (uses chip ID) + // Note: In non-blocking mode, startConfigPortal() returns false even when successful + wm.startConfigPortal(); + TEST_ASSERT_TRUE(wm.getConfigPortalActive()); + + delay(100); // Give it time to initialize + + // Verify we can get the SSID + String ssid = wm.getConfigPortalSSID(); + TEST_ASSERT_GREATER_THAN(0, ssid.length()); + + wm.stopConfigPortal(); + TEST_ASSERT_FALSE(wm.getConfigPortalActive()); + + Serial.println("[TEST] startConfigPortal() auto-name test completed successfully"); +} + +void test_stop_config_portal() { + Serial.println("[TEST] Testing stopConfigPortal()..."); + + WiFiManager wm; + + // Use non-blocking mode + wm.setConfigPortalBlocking(false); + wm.setConfigPortalTimeout(10); + + // Start portal + // Note: In non-blocking mode, startConfigPortal() returns false even when successful + wm.startConfigPortal("TestAP"); + TEST_ASSERT_TRUE(wm.getConfigPortalActive()); + + delay(100); + + // Stop portal + bool stopped = wm.stopConfigPortal(); + TEST_ASSERT_TRUE(stopped); + TEST_ASSERT_FALSE(wm.getConfigPortalActive()); + + Serial.println("[TEST] stopConfigPortal() test completed successfully"); +} + +void test_config_portal_infrastructure() { + Serial.println("[TEST] Testing config portal infrastructure..."); + + WiFiManager wm; + + // Use non-blocking mode + wm.setConfigPortalBlocking(false); + wm.setConfigPortalTimeout(10); + + // Note: In non-blocking mode, startConfigPortal() returns false even when successful + wm.startConfigPortal("TestAP"); + TEST_ASSERT_TRUE(wm.getConfigPortalActive()); + + delay(200); // Give time for initialization + + // Verify server object exists (must check while portal is active) + TEST_ASSERT_NOT_NULL_MESSAGE(wm.server, "Server object should exist when portal is active"); + + // Verify DNS server exists + TEST_ASSERT_NOT_NULL_MESSAGE(wm.dnsServer, "DNS server object should exist when portal is active"); + + // Verify WiFi is in AP mode + WiFiMode_t mode = WiFi.getMode(); + TEST_ASSERT_TRUE_MESSAGE((mode & WIFI_AP) != 0, "WiFi should be in AP mode"); + + // Verify AP has an IP + IPAddress apIP = WiFi.softAPIP(); + TEST_ASSERT_NOT_EQUAL(IPAddress(0,0,0,0), apIP); + + // Verify AP SSID matches + String apSSID = WiFi.softAPSSID(); + TEST_ASSERT_EQUAL_STRING("TestAP", apSSID.c_str()); + + wm.stopConfigPortal(); + + Serial.println("[TEST] Config portal infrastructure test completed successfully"); +} + +void test_start_web_portal() { + Serial.println("[TEST] Testing startWebPortal()..."); + + WiFiManager wm; + wm.setConfigPortalTimeout(5); + + // Note: Web portal requires STA mode, but for testing we can just verify it starts + // In real usage, WiFi would need to be connected first + wm.startWebPortal(); + + TEST_ASSERT_TRUE(wm.getWebPortalActive()); + + // Verify server object exists + TEST_ASSERT_NOT_NULL(wm.server); + + wm.stopWebPortal(); + + Serial.println("[TEST] startWebPortal() test completed successfully"); +} + +void test_stop_web_portal() { + Serial.println("[TEST] Testing stopWebPortal()..."); + + WiFiManager wm; + wm.setConfigPortalTimeout(5); + + wm.startWebPortal(); + TEST_ASSERT_TRUE(wm.getWebPortalActive()); + + delay(100); + + wm.stopWebPortal(); + TEST_ASSERT_FALSE(wm.getWebPortalActive()); + + Serial.println("[TEST] stopWebPortal() test completed successfully"); +} + +void test_config_portal_multiple_start_stop() { + Serial.println("[TEST] Testing multiple config portal start/stop cycles..."); + + WiFiManager wm; + + // Use non-blocking mode + wm.setConfigPortalBlocking(false); + wm.setConfigPortalTimeout(10); + + // First cycle + // Note: In non-blocking mode, startConfigPortal() returns false even when successful + wm.startConfigPortal("TestAP1"); + TEST_ASSERT_TRUE(wm.getConfigPortalActive()); + delay(100); + wm.stopConfigPortal(); + TEST_ASSERT_FALSE(wm.getConfigPortalActive()); + delay(200); + + // Second cycle + wm.startConfigPortal("TestAP2"); + TEST_ASSERT_TRUE(wm.getConfigPortalActive()); + delay(100); + wm.stopConfigPortal(); + TEST_ASSERT_FALSE(wm.getConfigPortalActive()); + delay(200); + + // Third cycle + wm.startConfigPortal("TestAP3"); + TEST_ASSERT_TRUE(wm.getConfigPortalActive()); + delay(100); + wm.stopConfigPortal(); + TEST_ASSERT_FALSE(wm.getConfigPortalActive()); + + Serial.println("[TEST] Multiple config portal cycles test completed successfully"); +} + +void test_config_portal_already_active() { + Serial.println("[TEST] Testing startConfigPortal() when already active..."); + + WiFiManager wm; + + // Use non-blocking mode + wm.setConfigPortalBlocking(false); + wm.setConfigPortalTimeout(10); + + // Start portal + // Note: In non-blocking mode, startConfigPortal() returns false even when successful + wm.startConfigPortal("TestAP"); + TEST_ASSERT_TRUE(wm.getConfigPortalActive()); + + delay(100); + + // Try to start again (should fail/return false) + bool started2 = wm.startConfigPortal("TestAP2"); + TEST_ASSERT_FALSE(started2); // Should return false when already active + TEST_ASSERT_TRUE(wm.getConfigPortalActive()); // Still active + + wm.stopConfigPortal(); + + Serial.println("[TEST] Config portal already active test completed successfully"); +} + +void test_get_config_portal_ssid() { + Serial.println("[TEST] Testing getConfigPortalSSID()..."); + + WiFiManager wm; + + // Use non-blocking mode + wm.setConfigPortalBlocking(false); + wm.setConfigPortalTimeout(10); + + // Start with known SSID + // Note: In non-blocking mode, startConfigPortal() returns false even when successful + wm.startConfigPortal("MyTestAP"); + TEST_ASSERT_TRUE(wm.getConfigPortalActive()); + + delay(100); + + String ssid = wm.getConfigPortalSSID(); + TEST_ASSERT_EQUAL_STRING("MyTestAP", ssid.c_str()); + + wm.stopConfigPortal(); + + // After stop, should return empty or default + String ssid2 = wm.getConfigPortalSSID(); + // Note: Behavior may vary, just verify it doesn't crash + + Serial.println("[TEST] getConfigPortalSSID() test completed successfully"); +} + diff --git a/test/test_wifimanager/tests/test_static_ip.cpp b/test/test_wifimanager/tests/test_static_ip.cpp new file mode 100644 index 0000000..ca92687 --- /dev/null +++ b/test/test_wifimanager/tests/test_static_ip.cpp @@ -0,0 +1,141 @@ +#include +#include +#include + +// Test static IP configuration +void test_set_ap_static_ip_config() { + Serial.println("[TEST] Testing setAPStaticIPConfig()..."); + + WiFiManager wm; + + IPAddress ip(192, 168, 4, 1); + IPAddress gw(192, 168, 4, 1); + IPAddress sn(255, 255, 255, 0); + + // Set AP static IP + wm.setAPStaticIPConfig(ip, gw, sn); + + // Verify no crash + TEST_ASSERT_TRUE_MESSAGE(true, "setAPStaticIPConfig() executed without crash"); + + // Start portal and verify IP is set + // Use non-blocking mode + wm.setConfigPortalBlocking(false); + wm.setConfigPortalTimeout(10); + + // Note: In non-blocking mode, startConfigPortal() returns false even when successful + wm.startConfigPortal("TestAP"); + TEST_ASSERT_TRUE(wm.getConfigPortalActive()); + + delay(200); // Give time for IP to be configured + + IPAddress apIP = WiFi.softAPIP(); + TEST_ASSERT_EQUAL(ip, apIP); + + wm.stopConfigPortal(); + + Serial.println("[TEST] setAPStaticIPConfig() test completed successfully"); +} + +void test_set_sta_static_ip_config() { + Serial.println("[TEST] Testing setSTAStaticIPConfig()..."); + + WiFiManager wm; + + IPAddress ip(192, 168, 1, 100); + IPAddress gw(192, 168, 1, 1); + IPAddress sn(255, 255, 255, 0); + + // Set STA static IP + wm.setSTAStaticIPConfig(ip, gw, sn); + + // Verify no crash + TEST_ASSERT_TRUE_MESSAGE(true, "setSTAStaticIPConfig() executed without crash"); + + Serial.println("[TEST] setSTAStaticIPConfig() test completed successfully"); +} + +void test_set_sta_static_ip_config_with_dns() { + Serial.println("[TEST] Testing setSTAStaticIPConfig() with DNS..."); + + WiFiManager wm; + + IPAddress ip(192, 168, 1, 100); + IPAddress gw(192, 168, 1, 1); + IPAddress sn(255, 255, 255, 0); + IPAddress dns(8, 8, 8, 8); + + // Set STA static IP with DNS + wm.setSTAStaticIPConfig(ip, gw, sn, dns); + + // Verify no crash + TEST_ASSERT_TRUE_MESSAGE(true, "setSTAStaticIPConfig() with DNS executed without crash"); + + Serial.println("[TEST] setSTAStaticIPConfig() with DNS test completed successfully"); +} + +void test_show_static_fields() { + Serial.println("[TEST] Testing setShowStaticFields()..."); + + WiFiManager wm; + + // Test enabling static fields + wm.setShowStaticFields(true); + + // Test disabling static fields + wm.setShowStaticFields(false); + + // Verify no crash + TEST_ASSERT_TRUE_MESSAGE(true, "setShowStaticFields() executed without crash"); + + Serial.println("[TEST] setShowStaticFields() test completed successfully"); +} + +void test_show_dns_fields() { + Serial.println("[TEST] Testing setShowDnsFields()..."); + + WiFiManager wm; + + // Test enabling DNS fields + wm.setShowDnsFields(true); + + // Test disabling DNS fields + wm.setShowDnsFields(false); + + // Verify no crash + TEST_ASSERT_TRUE_MESSAGE(true, "setShowDnsFields() executed without crash"); + + Serial.println("[TEST] setShowDnsFields() test completed successfully"); +} + +void test_ap_static_ip_application() { + Serial.println("[TEST] Testing AP static IP application..."); + + WiFiManager wm; + + IPAddress customIP(10, 0, 1, 1); + IPAddress customGW(10, 0, 1, 1); + IPAddress customSN(255, 255, 255, 0); + + wm.setAPStaticIPConfig(customIP, customGW, customSN); + + // Use non-blocking mode + wm.setConfigPortalBlocking(false); + wm.setConfigPortalTimeout(10); + + // Note: In non-blocking mode, startConfigPortal() returns false even when successful + wm.startConfigPortal("TestAP"); + TEST_ASSERT_TRUE(wm.getConfigPortalActive()); + + delay(200); // Give time for AP to configure + + IPAddress apIP = WiFi.softAPIP(); + + // AP should have the configured IP + TEST_ASSERT_EQUAL(customIP, apIP); + + wm.stopConfigPortal(); + + Serial.println("[TEST] AP static IP application test completed successfully"); +} +