Atlas - testautomation_hints.c

Home / ext / SDL / test Lines: 1 | Size: 11707 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/** 2 * Hints test suite 3 */ 4 5#include <SDL3/SDL.h> 6#include <SDL3/SDL_test.h> 7#include "testautomation_suites.h" 8 9static const char *HintsEnum[] = { 10 SDL_HINT_FRAMEBUFFER_ACCELERATION, 11 SDL_HINT_GAMECONTROLLERCONFIG, 12 SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, 13 SDL_HINT_MAC_CTRL_CLICK_EMULATE_RIGHT_CLICK, 14 SDL_HINT_ORIENTATIONS, 15 SDL_HINT_RENDER_DIRECT3D_THREADSAFE, 16 SDL_HINT_RENDER_VSYNC, 17 SDL_HINT_TIMER_RESOLUTION, 18 SDL_HINT_VIDEO_ALLOW_SCREENSAVER, 19 SDL_HINT_VIDEO_MAC_FULLSCREEN_SPACES, 20 SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, 21 SDL_HINT_VIDEO_WIN_D3DCOMPILER, 22 SDL_HINT_VIDEO_X11_XRANDR, 23 SDL_HINT_XINPUT_ENABLED, 24}; 25static const char *HintsVerbose[] = { 26 "SDL_FRAMEBUFFER_ACCELERATION", 27 "SDL_GAMECONTROLLERCONFIG", 28 "SDL_JOYSTICK_ALLOW_BACKGROUND_EVENTS", 29 "SDL_MAC_CTRL_CLICK_EMULATE_RIGHT_CLICK", 30 "SDL_ORIENTATIONS", 31 "SDL_RENDER_DIRECT3D_THREADSAFE", 32 "SDL_RENDER_VSYNC", 33 "SDL_TIMER_RESOLUTION", 34 "SDL_VIDEO_ALLOW_SCREENSAVER", 35 "SDL_VIDEO_MAC_FULLSCREEN_SPACES", 36 "SDL_VIDEO_MINIMIZE_ON_FOCUS_LOSS", 37 "SDL_VIDEO_WIN_D3DCOMPILER", 38 "SDL_VIDEO_X11_XRANDR", 39 "SDL_XINPUT_ENABLED" 40}; 41 42SDL_COMPILE_TIME_ASSERT(HintsEnum, SDL_arraysize(HintsEnum) == SDL_arraysize(HintsVerbose)); 43 44static const int numHintsEnum = SDL_arraysize(HintsEnum); 45 46/* Test case functions */ 47 48/** 49 * Call to SDL_GetHint 50 */ 51static int SDLCALL hints_getHint(void *arg) 52{ 53 const char *result1; 54 const char *result2; 55 int i; 56 57 for (i = 0; i < numHintsEnum; i++) { 58 result1 = SDL_GetHint(HintsEnum[i]); 59 SDLTest_AssertPass("Call to SDL_GetHint(%s) - using define definition", (char *)HintsEnum[i]); 60 result2 = SDL_GetHint(HintsVerbose[i]); 61 SDLTest_AssertPass("Call to SDL_GetHint(%s) - using string definition", (char *)HintsVerbose[i]); 62 SDLTest_AssertCheck( 63 (result1 == NULL && result2 == NULL) || (SDL_strcmp(result1, result2) == 0), 64 "Verify returned values are equal; got: result1='%s' result2='%s", 65 (result1 == NULL) ? "null" : result1, 66 (result2 == NULL) ? "null" : result2); 67 } 68 69 return TEST_COMPLETED; 70} 71 72typedef struct { 73 char *name; 74 char *value; 75 char *oldValue; 76} HintCallbackContext; 77 78static void SDLCALL hints_testHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint) 79{ 80 HintCallbackContext *context = userdata; 81 82 context->name = name ? SDL_strdup(name) : NULL; 83 context->value = hint ? SDL_strdup(hint) : NULL; 84 context->oldValue = oldValue ? SDL_strdup(oldValue) : NULL; 85} 86 87/** 88 * Call to SDL_SetHint 89 */ 90static int SDLCALL hints_setHint(void *arg) 91{ 92 const char *testHint = "SDL_AUTOMATED_TEST_HINT"; 93 const char *originalValue; 94 char *value; 95 const char *testValue; 96 HintCallbackContext callback_data; 97 bool result; 98 int i, j; 99 100 /* Create random values to set */ 101 value = SDLTest_RandomAsciiStringOfSize(10); 102 103 for (i = 0; i < numHintsEnum; i++) { 104 /* Capture current value */ 105 originalValue = SDL_GetHint(HintsEnum[i]); 106 SDLTest_AssertPass("Call to SDL_GetHint(%s)", HintsEnum[i]); 107 108 /* Copy the original value, since it will be freed when we set it again */ 109 originalValue = originalValue ? SDL_strdup(originalValue) : NULL; 110 111 /* Set value (twice) */ 112 for (j = 1; j <= 2; j++) { 113 result = SDL_SetHint(HintsEnum[i], value); 114 SDLTest_AssertPass("Call to SDL_SetHint(%s, %s) (iteration %i)", HintsEnum[i], value, j); 115 SDLTest_AssertCheck( 116 result == true || result == false, 117 "Verify valid result was returned, got: %i", 118 (int)result); 119 testValue = SDL_GetHint(HintsEnum[i]); 120 SDLTest_AssertPass("Call to SDL_GetHint(%s) - using string definition", HintsVerbose[i]); 121 SDLTest_AssertCheck( 122 (SDL_strcmp(value, testValue) == 0), 123 "Verify returned value equals set value; got: testValue='%s' value='%s", 124 (testValue == NULL) ? "null" : testValue, 125 value); 126 } 127 128 /* Reset original value */ 129 result = SDL_SetHint(HintsEnum[i], originalValue); 130 SDLTest_AssertPass("Call to SDL_SetHint(%s, originalValue)", HintsEnum[i]); 131 SDLTest_AssertCheck( 132 result == true || result == false, 133 "Verify valid result was returned, got: %i", 134 (int)result); 135 SDL_free((void *)originalValue); 136 } 137 138 SDL_free(value); 139 140 /* Set default value in environment */ 141 SDL_SetEnvironmentVariable(SDL_GetEnvironment(), testHint, "original", 1); 142 143 SDLTest_AssertPass("Call to SDL_GetHint() after saving and restoring hint"); 144 originalValue = SDL_GetHint(testHint); 145 value = (originalValue == NULL) ? NULL : SDL_strdup(originalValue); 146 result = SDL_SetHint(testHint, "temp"); 147 SDLTest_AssertCheck(!result, "SDL_SetHint(\"%s\", \"temp\") should return false", testHint); 148 result = SDL_SetHint(testHint, value); 149 SDLTest_AssertCheck(!result, "SDL_SetHint(\"%s\", \"%s\" should return false", testHint, value); 150 SDL_free(value); 151 testValue = SDL_GetHint(testHint); 152 SDLTest_AssertCheck( 153 testValue && SDL_strcmp(testValue, "original") == 0, 154 "testValue = %s, expected \"original\"", 155 testValue); 156 157 SDLTest_AssertPass("Call to SDL_SetHintWithPriority(NULL, SDL_HINT_DEFAULT)"); 158 result = SDL_SetHintWithPriority(testHint, NULL, SDL_HINT_DEFAULT); 159 SDLTest_AssertCheck(!result, "SDL_SetHintWithPriority(\"%s\", NULL, SDL_HINT_DEFAULT) should return false", testHint); 160 testValue = SDL_GetHint(testHint); 161 SDLTest_AssertCheck( 162 testValue && SDL_strcmp(testValue, "original") == 0, 163 "testValue = %s, expected \"original\"", 164 testValue); 165 166 SDLTest_AssertPass("Call to SDL_SetHintWithPriority(\"temp\", SDL_HINT_OVERRIDE)"); 167 result = SDL_SetHintWithPriority(testHint, "temp", SDL_HINT_OVERRIDE); 168 SDLTest_AssertCheck(result, "SDL_SetHintWithPriority(\"%s\", \"temp\", SDL_HINT_OVERRIDE) should return true", testHint); 169 testValue = SDL_GetHint(testHint); 170 SDLTest_AssertCheck( 171 testValue && SDL_strcmp(testValue, "temp") == 0, 172 "testValue = %s, expected \"temp\"", 173 testValue); 174 175 SDLTest_AssertPass("Call to SDL_SetHintWithPriority(NULL, SDL_HINT_OVERRIDE)"); 176 result = SDL_SetHintWithPriority(testHint, NULL, SDL_HINT_OVERRIDE); 177 SDLTest_AssertCheck(result, "SDL_SetHintWithPriority(\"%s\", NULL, SDL_HINT_OVERRIDE) should return true", testHint); 178 testValue = SDL_GetHint(testHint); 179 SDLTest_AssertCheck( 180 testValue == NULL, 181 "testValue = %s, expected NULL", 182 testValue); 183 184 SDLTest_AssertPass("Call to SDL_ResetHint()"); 185 SDL_ResetHint(testHint); 186 testValue = SDL_GetHint(testHint); 187 SDLTest_AssertCheck( 188 testValue && SDL_strcmp(testValue, "original") == 0, 189 "testValue = %s, expected \"original\"", 190 testValue); 191 192 /* Make sure callback functionality works past a reset */ 193 SDL_zero(callback_data); 194 SDLTest_AssertPass("Call to SDL_AddHintCallback()"); 195 SDL_AddHintCallback(testHint, hints_testHintChanged, &callback_data); 196 SDLTest_AssertCheck( 197 callback_data.name && SDL_strcmp(callback_data.name, testHint) == 0, 198 "callback_data.name = \"%s\", expected \"%s\"", 199 callback_data.name, testHint); 200 SDLTest_AssertCheck( 201 callback_data.value && SDL_strcmp(callback_data.value, "original") == 0, 202 "callback_data.value = \"%s\", expected \"%s\"", 203 callback_data.value, "original"); 204 SDL_free(callback_data.name); 205 SDL_free(callback_data.value); 206 SDL_free(callback_data.oldValue); 207 SDL_zero(callback_data); 208 209 SDLTest_AssertPass("Call to SDL_ResetHint(), using callback"); 210 SDL_ResetHint(testHint); 211 SDLTest_AssertCheck( 212 callback_data.value && SDL_strcmp(callback_data.value, "original") == 0, 213 "callbackValue = %s, expected \"original\"", 214 callback_data.value); 215 SDL_free(callback_data.name); 216 SDL_free(callback_data.value); 217 SDL_free(callback_data.oldValue); 218 SDL_zero(callback_data); 219 220 SDLTest_AssertPass("Call to SDL_SetHintWithPriority(\"temp\", SDL_HINT_OVERRIDE), using callback after reset"); 221 result = SDL_SetHintWithPriority(testHint, "temp", SDL_HINT_OVERRIDE); 222 SDLTest_AssertCheck(result, "SDL_SetHintWithPriority(\"%s\", \"temp\", SDL_HINT_OVERRIDE) should return true", testHint); 223 SDLTest_AssertCheck( 224 callback_data.value && SDL_strcmp(callback_data.value, "temp") == 0, 225 "callbackValue = %s, expected \"temp\"", 226 callback_data.value); 227 SDL_free(callback_data.name); 228 SDL_free(callback_data.value); 229 SDL_free(callback_data.oldValue); 230 SDL_zero(callback_data); 231 232 SDLTest_AssertPass("Call to SDL_ResetHint(), after clearing callback"); 233 SDL_RemoveHintCallback(testHint, hints_testHintChanged, &callback_data); 234 SDL_ResetHint(testHint); 235 SDLTest_AssertCheck( 236 !callback_data.value, 237 "callbackValue = %s, expected \"(null)\"", 238 callback_data.value); 239 SDL_free(callback_data.name); 240 SDL_free(callback_data.value); 241 SDL_free(callback_data.oldValue); 242 SDL_zero(callback_data); 243 244 /* Make sure callback functionality work with hint renamed in sdl3 */ 245 SDLTest_AssertPass("Call to SDL_AddHintCallback()"); 246 SDL_AddHintCallback(SDL_HINT_WINDOW_ALLOW_TOPMOST, hints_testHintChanged, &callback_data); 247 SDLTest_AssertPass("Call to SDL_SetHintWithPriority(\"temp\", SDL_HINT_OVERRIDE), using callback"); 248 SDLTest_AssertCheck(callback_data.name && SDL_strcmp(callback_data.name, SDL_HINT_WINDOW_ALLOW_TOPMOST) == 0, "callback was called with name \"%s\" (expected \"%s\")", callback_data.name, SDL_HINT_WINDOW_ALLOW_TOPMOST); 249 SDLTest_AssertCheck(!callback_data.value, "callback was called with null value, was %s", callback_data.value); 250 SDLTest_AssertCheck(!callback_data.oldValue, "callback was called with null oldvalue, was %s", callback_data.oldValue); 251 SDL_free(callback_data.name); 252 SDL_free(callback_data.value); 253 SDL_free(callback_data.oldValue); 254 SDL_zero(callback_data); 255 result = SDL_SetHintWithPriority(SDL_HINT_WINDOW_ALLOW_TOPMOST, "temp", SDL_HINT_OVERRIDE); 256 SDLTest_AssertCheck(result, "SDL_SetHintWithPriority(\"%s\", \"temp\", SDL_HINT_OVERRIDE) should return true", testHint); 257 SDLTest_AssertCheck( 258 callback_data.name && SDL_strcmp(callback_data.name, SDL_HINT_WINDOW_ALLOW_TOPMOST) == 0, 259 "callback_data.name = \"%s\", expected \"%s\"", 260 callback_data.name, SDL_HINT_WINDOW_ALLOW_TOPMOST); 261 SDLTest_AssertCheck( 262 callback_data.value && SDL_strcmp(callback_data.value, "temp") == 0, 263 "callback_data.value = \"%s\", expected \"%s\"", 264 callback_data.value, "temp"); 265 SDL_free(callback_data.name); 266 SDL_free(callback_data.value); 267 SDL_free(callback_data.oldValue); 268 SDL_zero(callback_data); 269 SDL_ResetHint(testHint); 270 271 return TEST_COMPLETED; 272} 273 274/* ================= Test References ================== */ 275 276/* Hints test cases */ 277static const SDLTest_TestCaseReference hintsGetHint = { 278 hints_getHint, "hints_getHint", "Call to SDL_GetHint", TEST_ENABLED 279}; 280 281static const SDLTest_TestCaseReference hintsSetHint = { 282 hints_setHint, "hints_setHint", "Call to SDL_SetHint", TEST_ENABLED 283}; 284 285/* Sequence of Hints test cases */ 286static const SDLTest_TestCaseReference *hintsTests[] = { 287 &hintsGetHint, 288 &hintsSetHint, 289 NULL 290}; 291 292/* Hints test suite (global) */ 293SDLTest_TestSuiteReference hintsTestSuite = { 294 "Hints", 295 NULL, 296 hintsTests, 297 NULL 298}; 299
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.