From 3f97fb59ad11e24aee78dc26d83eaf0a214b1a05 Mon Sep 17 00:00:00 2001 From: Ondrej Holy Date: Mon, 29 Apr 2024 16:01:12 +0200 Subject: [PATCH] [client,sdl] add test case for SDL prefs Let's add test case for SDL prefs functionality. This is to test the JSON wrapper to ensure it doesn't break something. --- client/SDL/CMakeLists.txt | 4 + client/SDL/test/CMakeLists.txt | 36 +++++++++ client/SDL/test/TestSDLPrefs.cpp | 121 +++++++++++++++++++++++++++++++ client/SDL/test/sdl-freerdp.json | 10 +++ 4 files changed, 171 insertions(+) create mode 100644 client/SDL/test/CMakeLists.txt create mode 100644 client/SDL/test/TestSDLPrefs.cpp create mode 100644 client/SDL/test/sdl-freerdp.json diff --git a/client/SDL/CMakeLists.txt b/client/SDL/CMakeLists.txt index 526655b57..c17ccd73e 100644 --- a/client/SDL/CMakeLists.txt +++ b/client/SDL/CMakeLists.txt @@ -119,3 +119,7 @@ set_property(TARGET ${PROJECT_NAME} PROPERTY FOLDER "Client/SDL") install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT client) add_subdirectory(man) + +if(BUILD_TESTING) + add_subdirectory(test) +endif() diff --git a/client/SDL/test/CMakeLists.txt b/client/SDL/test/CMakeLists.txt new file mode 100644 index 000000000..cc336833f --- /dev/null +++ b/client/SDL/test/CMakeLists.txt @@ -0,0 +1,36 @@ +set(MODULE_NAME "TestSDL") +set(MODULE_PREFIX "TEST_SDL") + +set(${MODULE_PREFIX}_DRIVER ${MODULE_NAME}.cpp) + +set(${MODULE_PREFIX}_TESTS TestSDLPrefs.cpp) + +create_test_sourcelist(${MODULE_PREFIX}_SRCS + ${${MODULE_PREFIX}_DRIVER} + ${${MODULE_PREFIX}_TESTS}) + +add_executable(${MODULE_NAME} ${${MODULE_PREFIX}_SRCS}) + +set(${MODULE_PREFIX}_LIBS freerdp winpr sdl_prefs) + +target_link_libraries(${MODULE_NAME} ${${MODULE_PREFIX}_LIBS}) + +set_target_properties(${MODULE_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${TESTING_OUTPUT_DIRECTORY}") + +set(TEST_SRC_AREA "${CMAKE_CURRENT_SOURCE_DIR}") +set(TEST_BIN_AREA "${CMAKE_CURRENT_BINARY_DIR}") + +if (WIN32) + string(REPLACE "\\" "\\\\" TEST_SRC_AREA "${TEST_SRC_AREA}") + string(REPLACE "\\" "\\\\" TEST_BIN_AREA "${TEST_BIN_AREA}") +endif() + +add_compile_definitions(TEST_SRC_AREA="${TEST_SRC_AREA}") +add_compile_definitions(TEST_BIN_AREA="${TEST_BIN_AREA}") + +foreach(test ${${MODULE_PREFIX}_TESTS}) + get_filename_component(TestName ${test} NAME_WE) + add_test(${TestName} ${TESTING_OUTPUT_DIRECTORY}/${MODULE_NAME} ${TestName}) +endforeach() + +set_property(TARGET ${MODULE_NAME} PROPERTY FOLDER "FreeRDP/Client/Test") diff --git a/client/SDL/test/TestSDLPrefs.cpp b/client/SDL/test/TestSDLPrefs.cpp new file mode 100644 index 000000000..db4a75302 --- /dev/null +++ b/client/SDL/test/TestSDLPrefs.cpp @@ -0,0 +1,121 @@ +#include "../sdl_prefs.hpp" + +#include +#include + +#include +#include + +#if __has_include() +#include +namespace fs = std::filesystem; +#elif __has_include() +#include +namespace fs = std::experimental::filesystem; +#else +#error Could not find system header "" or "" +#endif + +static std::shared_ptr instance() +{ +#if !defined(TEST_SRC_AREA) +#error "Please define TEST_SRC_AREA to the source directory of this test case" +#endif + fs::path src(TEST_SRC_AREA); + src /= "sdl-freerdp.json"; + if (!fs::exists(src)) + { + std::cerr << "[ERROR] test configuration file '" << src << "' does not exist" << std::endl; + return nullptr; + } + + return SdlPref::instance(src.string()); +} + +int TestSDLPrefs(int argc, char* argv[]) +{ + WINPR_UNUSED(argc); + WINPR_UNUSED(argv); + +#if defined(WITH_WINPR_JSON) + printf("implementation: json\n"); +#else + printf("implementation: fallback\n"); +#endif + +#if defined(WITH_WINPR_JSON) + printf("config: %s\n", instance()->get_pref_file().c_str()); +#endif + + auto string_value = instance()->get_string("string_key", "cba"); +#if defined(WITH_WINPR_JSON) + if (string_value != "abc") + return -1; +#else + if (string_value != "cba") + return -1; +#endif + + auto string_value_nonexistent = instance()->get_string("string_key_nonexistent", "cba"); + if (string_value_nonexistent != "cba") + return -1; + + auto int_value = instance()->get_int("int_key", 321); +#if defined(WITH_WINPR_JSON) + if (int_value != 123) + return -1; +#else + if (int_value != 321) + return -1; +#endif + + auto int_value_nonexistent = instance()->get_int("int_key_nonexistent", 321); + if (int_value_nonexistent != 321) + return -1; + + auto bool_value = instance()->get_bool("bool_key", false); +#if defined(WITH_WINPR_JSON) + if (!bool_value) + return -1; +#else + if (bool_value) + return -1; +#endif + + auto bool_value_nonexistent = instance()->get_bool("bool_key_nonexistent", false); + if (bool_value_nonexistent) + return -1; + + auto array_value = instance()->get_array("array_key", { "c", "b", "a" }); + if (array_value.size() != 3) + return -1; +#if defined(WITH_WINPR_JSON) + if (array_value[0] != "a") + return -1; + if (array_value[1] != "b") + return -1; + if (array_value[2] != "c") + return -1; +#else + if (array_value[0] != "c") + return -1; + if (array_value[1] != "b") + return -1; + if (array_value[2] != "a") + return -1; +#endif + + auto array_value_nonexistent = + instance()->get_array("array_key_nonexistent", { "c", "b", "a" }); + if (array_value_nonexistent.size() != 3) + return -1; + + if (array_value_nonexistent[0] != "c") + return -1; + if (array_value_nonexistent[1] != "b") + return -1; + if (array_value_nonexistent[2] != "a") + return -1; + + return 0; +} diff --git a/client/SDL/test/sdl-freerdp.json b/client/SDL/test/sdl-freerdp.json new file mode 100644 index 000000000..17facb32e --- /dev/null +++ b/client/SDL/test/sdl-freerdp.json @@ -0,0 +1,10 @@ +{ + "string_key": "abc", + "int_key": 123, + "bool_key": true, + "array_key": [ + "a", + "b", + "c" + ] +} \ No newline at end of file