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