Skip to content

Commit 177fb9c

Browse files
committed
tests: port failing SDL_Hint tests from pysdl2 to testautomation
1 parent fc536dc commit 177fb9c

File tree

1 file changed

+83
-33
lines changed

1 file changed

+83
-33
lines changed

test/testautomation_hints.c

Lines changed: 83 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,19 @@ int hints_getHint(void *arg)
8888
return TEST_COMPLETED;
8989
}
9090

91+
typedef struct {
92+
char *name;
93+
char *value;
94+
char *oldValue;
95+
} HintCallbackContext;
96+
9197
static void SDLCALL hints_testHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
9298
{
93-
*(char **)userdata = hint ? SDL_strdup(hint) : NULL;
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;
94104
}
95105

96106
/**
@@ -102,7 +112,7 @@ int hints_setHint(void *arg)
102112
const char *originalValue;
103113
char *value;
104114
const char *testValue;
105-
char *callbackValue;
115+
HintCallbackContext callback_data;
106116
SDL_bool result;
107117
int i, j;
108118

@@ -152,8 +162,10 @@ int hints_setHint(void *arg)
152162
SDLTest_AssertPass("Call to SDL_GetHint() after saving and restoring hint");
153163
originalValue = SDL_GetHint(testHint);
154164
value = (originalValue == NULL) ? NULL : SDL_strdup(originalValue);
155-
SDL_SetHint(testHint, "temp");
156-
SDL_SetHint(testHint, value);
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);
157169
SDL_free(value);
158170
testValue = SDL_GetHint(testHint);
159171
SDLTest_AssertCheck(
@@ -162,23 +174,35 @@ int hints_setHint(void *arg)
162174
testValue);
163175

164176
SDLTest_AssertPass("Call to SDL_SetHintWithPriority(NULL, SDL_HINT_DEFAULT)");
165-
SDL_SetHintWithPriority(testHint, 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);
166179
testValue = SDL_GetHint(testHint);
167180
SDLTest_AssertCheck(
168181
testValue && SDL_strcmp(testValue, "original") == 0,
169182
"testValue = %s, expected \"original\"",
170183
testValue);
171184

185+
SDLTest_AssertPass("Call to SDL_SetHint(\"\", \"data\")");
186+
result = SDL_SetHint("", "data");
187+
SDLTest_AssertCheck(result, "SDL_SetHint(\"\", \"data\") should return true");
188+
testValue = SDL_GetHint("");
189+
SDLTest_AssertCheck(
190+
testValue && SDL_strcmp(testValue, "data") == 0,
191+
"testValue = %s, expected \"data\"",
192+
testValue);
193+
172194
SDLTest_AssertPass("Call to SDL_SetHintWithPriority(\"temp\", SDL_HINT_OVERRIDE)");
173-
SDL_SetHintWithPriority(testHint, "temp", SDL_HINT_OVERRIDE);
195+
result = SDL_SetHintWithPriority(testHint, "temp", SDL_HINT_OVERRIDE);
196+
SDLTest_AssertCheck(result, "SDL_SetHintWithPriority(\"%s\", \"temp\", SDL_HINT_OVERRIDE) should return true", testHint);
174197
testValue = SDL_GetHint(testHint);
175198
SDLTest_AssertCheck(
176199
testValue && SDL_strcmp(testValue, "temp") == 0,
177200
"testValue = %s, expected \"temp\"",
178201
testValue);
179202

180203
SDLTest_AssertPass("Call to SDL_SetHintWithPriority(NULL, SDL_HINT_OVERRIDE)");
181-
SDL_SetHintWithPriority(testHint, NULL, SDL_HINT_OVERRIDE);
204+
result = SDL_SetHintWithPriority(testHint, NULL, SDL_HINT_OVERRIDE);
205+
SDLTest_AssertCheck(result, "SDL_SetHintWithPriority(\"%s\", NULL, SDL_HINT_OVERRIDE) should return true", testHint);
182206
testValue = SDL_GetHint(testHint);
183207
SDLTest_AssertCheck(
184208
testValue == NULL,
@@ -194,49 +218,75 @@ int hints_setHint(void *arg)
194218
testValue);
195219

196220
/* Make sure callback functionality works past a reset */
221+
SDL_zero(callback_data);
197222
SDLTest_AssertPass("Call to SDL_AddHintCallback()");
198-
callbackValue = NULL;
199-
SDL_AddHintCallback(testHint, hints_testHintChanged, &callbackValue);
223+
SDL_AddHintCallback(testHint, hints_testHintChanged, &callback_data);
200224
SDLTest_AssertCheck(
201-
callbackValue && SDL_strcmp(callbackValue, "original") == 0,
202-
"callbackValue = %s, expected \"original\"",
203-
callbackValue);
204-
SDL_free(callbackValue);
205-
206-
SDLTest_AssertPass("Call to SDL_SetHintWithPriority(\"temp\", SDL_HINT_OVERRIDE), using callback");
207-
callbackValue = NULL;
208-
SDL_SetHintWithPriority(testHint, "temp", SDL_HINT_OVERRIDE);
225+
callback_data.name && SDL_strcmp(callback_data.name, testHint) == 0,
226+
"callback_data.name = \"%s\", expected \"%s\"",
227+
callback_data.name, testHint);
209228
SDLTest_AssertCheck(
210-
callbackValue && SDL_strcmp(callbackValue, "temp") == 0,
211-
"callbackValue = %s, expected \"temp\"",
212-
callbackValue);
213-
SDL_free(callbackValue);
229+
callback_data.value && SDL_strcmp(callback_data.value, "original") == 0,
230+
"callback_data.value = \"%s\", expected \"%s\"",
231+
callback_data.value, "original");
232+
SDL_free(callback_data.name);
233+
SDL_free(callback_data.value);
234+
SDL_free(callback_data.oldValue);
214235

215236
SDLTest_AssertPass("Call to SDL_ResetHint(), using callback");
216-
callbackValue = NULL;
237+
SDL_zero(callback_data);
217238
SDL_ResetHint(testHint);
218239
SDLTest_AssertCheck(
219-
callbackValue && SDL_strcmp(callbackValue, "original") == 0,
240+
callback_data.value && SDL_strcmp(callback_data.value, "original") == 0,
220241
"callbackValue = %s, expected \"original\"",
221-
callbackValue);
242+
callback_data.value);
243+
SDL_free(callback_data.name);
244+
SDL_free(callback_data.value);
245+
SDL_free(callback_data.oldValue);
222246

223247
SDLTest_AssertPass("Call to SDL_SetHintWithPriority(\"temp\", SDL_HINT_OVERRIDE), using callback after reset");
224-
callbackValue = NULL;
225-
SDL_SetHintWithPriority(testHint, "temp", SDL_HINT_OVERRIDE);
248+
SDL_zero(callback_data);
249+
result = SDL_SetHintWithPriority(testHint, "temp", SDL_HINT_OVERRIDE);
250+
SDLTest_AssertCheck(result, "SDL_SetHintWithPriority(\"%s\", \"temp\", SDL_HINT_OVERRIDE) should return true", testHint);
226251
SDLTest_AssertCheck(
227-
callbackValue && SDL_strcmp(callbackValue, "temp") == 0,
252+
callback_data.value && SDL_strcmp(callback_data.value, "temp") == 0,
228253
"callbackValue = %s, expected \"temp\"",
229-
callbackValue);
230-
SDL_free(callbackValue);
254+
callback_data.value);
255+
SDL_free(callback_data.name);
256+
SDL_free(callback_data.value);
257+
SDL_free(callback_data.oldValue);
231258

232259
SDLTest_AssertPass("Call to SDL_ResetHint(), after clearing callback");
233-
callbackValue = NULL;
234-
SDL_DelHintCallback(testHint, hints_testHintChanged, &callbackValue);
260+
SDL_zero(callback_data);
261+
SDL_DelHintCallback(testHint, hints_testHintChanged, &callback_data);
235262
SDL_ResetHint(testHint);
236263
SDLTest_AssertCheck(
237-
callbackValue == NULL,
264+
!callback_data.value,
238265
"callbackValue = %s, expected \"(null)\"",
239-
callbackValue);
266+
callback_data.value);
267+
SDL_free(callback_data.name);
268+
SDL_free(callback_data.value);
269+
SDL_free(callback_data.oldValue);
270+
271+
/* Make sure callback functionality work with hint renamed in sdl3 */
272+
SDLTest_AssertPass("Call to SDL_AddHintCallback()");
273+
SDL_AddHintCallback(SDL_HINT_ALLOW_TOPMOST, hints_testHintChanged, &callback_data);
274+
SDLTest_AssertPass("Call to SDL_SetHintWithPriority(\"temp\", SDL_HINT_OVERRIDE), using callback");
275+
SDL_zero(callback_data);
276+
result = SDL_SetHintWithPriority(SDL_HINT_ALLOW_TOPMOST, "temp", SDL_HINT_OVERRIDE);
277+
SDLTest_AssertCheck(result, "SDL_SetHintWithPriority(\"%s\", \"temp\", SDL_HINT_OVERRIDE) should return true", testHint);
278+
SDLTest_AssertCheck(
279+
callback_data.name && SDL_strcmp(callback_data.name, SDL_HINT_ALLOW_TOPMOST) == 0,
280+
"callback_data.name = \"%s\", expected \"%s\"",
281+
callback_data.name, SDL_HINT_ALLOW_TOPMOST);
282+
SDLTest_AssertCheck(
283+
callback_data.value && SDL_strcmp(callback_data.value, "temp") == 0,
284+
"callback_data.value = \"%s\", expected \"%s\"",
285+
callback_data.value, "temp");
286+
SDL_free(callback_data.name);
287+
SDL_free(callback_data.value);
288+
SDL_free(callback_data.oldValue);
289+
SDL_ResetHint(testHint);
240290

241291
return TEST_COMPLETED;
242292
}

0 commit comments

Comments
 (0)