[client,sdl] add a simple test for dialogs

This commit is contained in:
Armin Novak 2023-07-19 17:20:23 +02:00 committed by akallabeth
parent 1a62103a08
commit 06fd58264e
4 changed files with 104 additions and 112 deletions

View File

@ -22,9 +22,12 @@ set(SRCS
)
set(LIBS
${SDL2_LIBRARIES}
${SDL2TTF_LIBRARY}
font
winpr
)
add_subdirectory(font)
add_library(dialogs STATIC
@ -32,3 +35,7 @@ add_library(dialogs STATIC
)
target_link_libraries(dialogs PRIVATE ${LIBS})
if(BUILD_TESTING)
add_subdirectory(test)
endif()

View File

@ -4,7 +4,7 @@ set(MODULE_PREFIX "TEST_SDL")
set(DRIVER ${MODULE_NAME}.c)
set(TEST_SRCS
TestSDLDialogs.c
TestSDLDialogs.cpp
)
create_test_sourcelist(SRCS
@ -13,7 +13,9 @@ create_test_sourcelist(SRCS
add_executable(${MODULE_NAME} ${SRCS})
set(LIBS ${LIBS} freerdp-client freerdp SDL_client)
list(APPEND LIBS
dialogs
)
target_link_libraries(${MODULE_NAME} ${LIBS})

View File

@ -0,0 +1,93 @@
#include "../sdl_selectlist.hpp"
#include "../sdl_input_widgets.hpp"
#include <winpr/wlog.h>
BOOL sdl_log_error_ex(Uint32 res, wLog* log, const char* what, const char* file, size_t line,
const char* fkt)
{
return FALSE;
}
static bool test_input_dialog()
{
const auto title = "sometitle";
std::vector<std::string> labels;
std::vector<std::string> initial;
std::vector<Uint32> flags;
for (size_t x = 0; x < 12; x++)
{
labels.push_back("label" + std::to_string(x));
initial.push_back(std::to_string(x));
Uint32 flag = 0;
if ((x % 2) != 0)
flag |= SdlInputWidget::SDL_INPUT_MASK;
if ((x % 3) == 0)
flag |= SdlInputWidget::SDL_INPUT_READONLY;
flags.push_back(flag);
}
SdlInputWidgetList list{ title, labels, initial, flags };
std::vector<std::string> result;
auto rc = list.run(result);
if (rc < 0)
{
return false;
}
if (result.size() != labels.size())
{
return false;
}
return true;
}
static bool test_select_dialog()
{
const auto title = "sometitle";
std::vector<std::string> labels;
for (size_t x = 0; x < 12; x++)
{
labels.push_back("label" + std::to_string(x));
}
SdlSelectList list{ title, labels };
auto rc = list.run();
if (rc < 0)
{
return false;
}
if (static_cast<size_t>(rc) >= labels.size())
return false;
return true;
}
extern "C" int TestSDLDialogs(int argc, char* argv[])
{
int rc = 0;
(void)argc;
(void)argv;
#if 0
SDL_Init(SDL_INIT_VIDEO);
try
{
#if 1
if (!test_input_dialog())
throw -1;
#endif
#if 1
if (!test_select_dialog())
throw -2;
#endif
}
catch (int e)
{
rc = e;
}
SDL_Quit();
#endif
return rc;
}

View File

@ -1,110 +0,0 @@
#include <freerdp/config.h>
#include "../sdl_dialogs.h"
#include "../sdl_select.h"
#include "../sdl_input.h"
#include "../sdl_utils.h"
static void array_free(char** array, size_t count)
{
if (!array)
return;
for (size_t x = 0; x < count; x++)
free(array[x]);
free(array);
}
static char* allocate_random_str(void)
{
size_t len = (rand() % 32) + 3;
char* str = calloc(len + 1, sizeof(char));
if (!str)
return NULL;
for (size_t x = 0; x < len; x++)
{
char cur = 'a' + rand() % 26;
str[x] = cur;
}
return str;
}
static char** allocate_random(size_t count)
{
char** array = calloc(count, sizeof(char*));
if (!array)
return NULL;
for (size_t x = 0; x < count; x++)
array[x] = allocate_random_str();
return array;
}
static BOOL test_select_dialog(wLog* log)
{
BOOL res = FALSE;
const size_t count = 7;
char** labels = allocate_random(count);
if (!labels)
goto fail;
const int irc = SDL_Init(SDL_INIT_VIDEO);
if (sdl_log_error(irc, log, "SDL_Init"))
goto fail;
const int rc = sdl_select_get("sometitle", count, (const char**)labels);
if (rc < 0)
goto fail;
res = TRUE;
fail:
array_free(labels, count);
SDL_Quit();
return res;
}
static BOOL test_input_dialog(wLog* log)
{
BOOL res = FALSE;
const size_t count = 7;
char** labels = allocate_random(count);
char** initial = allocate_random(count);
Uint32* flags = calloc(count, sizeof(Uint32));
char** result = calloc(count, sizeof(char*));
if (!labels || !initial || !flags || !result)
goto fail;
flags[0] = SDL_INPUT_MASK;
const int irc = SDL_Init(SDL_INIT_VIDEO);
if (sdl_log_error(irc, log, "SDL_Init"))
goto fail;
const int rc = sdl_input_get("sometitle", count, (const char**)labels, (const char**)initial,
flags, result);
if (rc < 0)
goto fail;
res = TRUE;
fail:
array_free(labels, count);
array_free(initial, count);
array_free(result, count);
free(flags);
SDL_Quit();
return res;
}
int TestSDLDialogs(int argc, char* argv[])
{
#if 0
wLog* log = WLog_Get("TestSDLDialogs");
if (!test_input_dialog(log))
return -1;
if (!test_select_dialog(log))
return -1;
#endif
return 0;
}