1 | /** |
2 | * Keyboard test suite |
3 | */ |
4 | |
5 | #include <stdio.h> |
6 | #include <limits.h> |
7 | |
8 | #include "SDL_config.h" |
9 | #include "SDL.h" |
10 | #include "SDL_test.h" |
11 | |
12 | /* ================= Test Case Implementation ================== */ |
13 | |
14 | /* Test case functions */ |
15 | |
16 | /** |
17 | * @brief Check call to SDL_GetKeyboardState with and without numkeys reference. |
18 | * |
19 | * @sa http://wiki.libsdl.org/SDL_GetKeyboardState |
20 | */ |
21 | int |
22 | keyboard_getKeyboardState(void *arg) |
23 | { |
24 | int numkeys; |
25 | Uint8 *state; |
26 | |
27 | /* Case where numkeys pointer is NULL */ |
28 | state = (Uint8 *)SDL_GetKeyboardState(NULL); |
29 | SDLTest_AssertPass("Call to SDL_GetKeyboardState(NULL)" ); |
30 | SDLTest_AssertCheck(state != NULL, "Validate that return value from SDL_GetKeyboardState is not NULL" ); |
31 | |
32 | /* Case where numkeys pointer is not NULL */ |
33 | numkeys = -1; |
34 | state = (Uint8 *)SDL_GetKeyboardState(&numkeys); |
35 | SDLTest_AssertPass("Call to SDL_GetKeyboardState(&numkeys)" ); |
36 | SDLTest_AssertCheck(state != NULL, "Validate that return value from SDL_GetKeyboardState is not NULL" ); |
37 | SDLTest_AssertCheck(numkeys >= 0, "Validate that value of numkeys is >= 0, got: %i" , numkeys); |
38 | |
39 | return TEST_COMPLETED; |
40 | } |
41 | |
42 | /** |
43 | * @brief Check call to SDL_GetKeyboardFocus |
44 | * |
45 | * @sa http://wiki.libsdl.org/SDL_GetKeyboardFocus |
46 | */ |
47 | int |
48 | keyboard_getKeyboardFocus(void *arg) |
49 | { |
50 | /* Call, but ignore return value */ |
51 | SDL_GetKeyboardFocus(); |
52 | SDLTest_AssertPass("Call to SDL_GetKeyboardFocus()" ); |
53 | |
54 | return TEST_COMPLETED; |
55 | } |
56 | |
57 | /** |
58 | * @brief Check call to SDL_GetKeyFromName for known, unknown and invalid name. |
59 | * |
60 | * @sa http://wiki.libsdl.org/SDL_GetKeyFromName |
61 | */ |
62 | int |
63 | keyboard_getKeyFromName(void *arg) |
64 | { |
65 | SDL_Keycode result; |
66 | |
67 | /* Case where Key is known, 1 character input */ |
68 | result = SDL_GetKeyFromName("A" ); |
69 | SDLTest_AssertPass("Call to SDL_GetKeyFromName(known/single)" ); |
70 | SDLTest_AssertCheck(result == SDLK_a, "Verify result from call, expected: %i, got: %i" , SDLK_a, result); |
71 | |
72 | /* Case where Key is known, 2 character input */ |
73 | result = SDL_GetKeyFromName("F1" ); |
74 | SDLTest_AssertPass("Call to SDL_GetKeyFromName(known/double)" ); |
75 | SDLTest_AssertCheck(result == SDLK_F1, "Verify result from call, expected: %i, got: %i" , SDLK_F1, result); |
76 | |
77 | /* Case where Key is known, 3 character input */ |
78 | result = SDL_GetKeyFromName("End" ); |
79 | SDLTest_AssertPass("Call to SDL_GetKeyFromName(known/triple)" ); |
80 | SDLTest_AssertCheck(result == SDLK_END, "Verify result from call, expected: %i, got: %i" , SDLK_END, result); |
81 | |
82 | /* Case where Key is known, 4 character input */ |
83 | result = SDL_GetKeyFromName("Find" ); |
84 | SDLTest_AssertPass("Call to SDL_GetKeyFromName(known/quad)" ); |
85 | SDLTest_AssertCheck(result == SDLK_FIND, "Verify result from call, expected: %i, got: %i" , SDLK_FIND, result); |
86 | |
87 | /* Case where Key is known, multiple character input */ |
88 | result = SDL_GetKeyFromName("AudioStop" ); |
89 | SDLTest_AssertPass("Call to SDL_GetKeyFromName(known/multi)" ); |
90 | SDLTest_AssertCheck(result == SDLK_AUDIOSTOP, "Verify result from call, expected: %i, got: %i" , SDLK_AUDIOSTOP, result); |
91 | |
92 | /* Case where Key is unknown */ |
93 | result = SDL_GetKeyFromName("NotThere" ); |
94 | SDLTest_AssertPass("Call to SDL_GetKeyFromName(unknown)" ); |
95 | SDLTest_AssertCheck(result == SDLK_UNKNOWN, "Verify result from call is UNKNOWN, expected: %i, got: %i" , SDLK_UNKNOWN, result); |
96 | |
97 | /* Case where input is NULL/invalid */ |
98 | result = SDL_GetKeyFromName(NULL); |
99 | SDLTest_AssertPass("Call to SDL_GetKeyFromName(NULL)" ); |
100 | SDLTest_AssertCheck(result == SDLK_UNKNOWN, "Verify result from call is UNKNOWN, expected: %i, got: %i" , SDLK_UNKNOWN, result); |
101 | |
102 | return TEST_COMPLETED; |
103 | } |
104 | |
105 | /* |
106 | * Local helper to check for the invalid scancode error message |
107 | */ |
108 | void |
109 | _checkInvalidScancodeError() |
110 | { |
111 | const char *expectedError = "Parameter 'scancode' is invalid" ; |
112 | const char *error; |
113 | error = SDL_GetError(); |
114 | SDLTest_AssertPass("Call to SDL_GetError()" ); |
115 | SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL" ); |
116 | if (error != NULL) { |
117 | SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0, |
118 | "Validate error message, expected: '%s', got: '%s'" , expectedError, error); |
119 | SDL_ClearError(); |
120 | SDLTest_AssertPass("Call to SDL_ClearError()" ); |
121 | } |
122 | } |
123 | |
124 | /** |
125 | * @brief Check call to SDL_GetKeyFromScancode |
126 | * |
127 | * @sa http://wiki.libsdl.org/SDL_GetKeyFromScancode |
128 | */ |
129 | int |
130 | keyboard_getKeyFromScancode(void *arg) |
131 | { |
132 | SDL_Keycode result; |
133 | |
134 | /* Case where input is valid */ |
135 | result = SDL_GetKeyFromScancode(SDL_SCANCODE_A); |
136 | SDLTest_AssertPass("Call to SDL_GetKeyFromScancode(valid)" ); |
137 | SDLTest_AssertCheck(result == SDLK_a, "Verify result from call, expected: %i, got: %i" , SDLK_a, result); |
138 | |
139 | /* Case where input is zero */ |
140 | result = SDL_GetKeyFromScancode(0); |
141 | SDLTest_AssertPass("Call to SDL_GetKeyFromScancode(0)" ); |
142 | SDLTest_AssertCheck(result == SDLK_UNKNOWN, "Verify result from call is UNKNOWN, expected: %i, got: %i" , SDLK_UNKNOWN, result); |
143 | |
144 | /* Clear error message */ |
145 | SDL_ClearError(); |
146 | SDLTest_AssertPass("Call to SDL_ClearError()" ); |
147 | |
148 | /* Case where input is invalid (too small) */ |
149 | result = SDL_GetKeyFromScancode(-999); |
150 | SDLTest_AssertPass("Call to SDL_GetKeyFromScancode(-999)" ); |
151 | SDLTest_AssertCheck(result == SDLK_UNKNOWN, "Verify result from call is UNKNOWN, expected: %i, got: %i" , SDLK_UNKNOWN, result); |
152 | _checkInvalidScancodeError(); |
153 | |
154 | /* Case where input is invalid (too big) */ |
155 | result = SDL_GetKeyFromScancode(999); |
156 | SDLTest_AssertPass("Call to SDL_GetKeyFromScancode(999)" ); |
157 | SDLTest_AssertCheck(result == SDLK_UNKNOWN, "Verify result from call is UNKNOWN, expected: %i, got: %i" , SDLK_UNKNOWN, result); |
158 | _checkInvalidScancodeError(); |
159 | |
160 | return TEST_COMPLETED; |
161 | } |
162 | |
163 | /** |
164 | * @brief Check call to SDL_GetKeyName |
165 | * |
166 | * @sa http://wiki.libsdl.org/SDL_GetKeyName |
167 | */ |
168 | int |
169 | keyboard_getKeyName(void *arg) |
170 | { |
171 | char *result; |
172 | char *expected; |
173 | |
174 | /* Case where key has a 1 character name */ |
175 | expected = "3" ; |
176 | result = (char *)SDL_GetKeyName(SDLK_3); |
177 | SDLTest_AssertPass("Call to SDL_GetKeyName()" ); |
178 | SDLTest_AssertCheck(result != NULL, "Verify result from call is not NULL" ); |
179 | SDLTest_AssertCheck(SDL_strcmp(result, expected) == 0, "Verify result from call is valid, expected: %s, got: %s" , expected, result); |
180 | |
181 | /* Case where key has a 2 character name */ |
182 | expected = "F1" ; |
183 | result = (char *)SDL_GetKeyName(SDLK_F1); |
184 | SDLTest_AssertPass("Call to SDL_GetKeyName()" ); |
185 | SDLTest_AssertCheck(result != NULL, "Verify result from call is not NULL" ); |
186 | SDLTest_AssertCheck(SDL_strcmp(result, expected) == 0, "Verify result from call is valid, expected: %s, got: %s" , expected, result); |
187 | |
188 | /* Case where key has a 3 character name */ |
189 | expected = "Cut" ; |
190 | result = (char *)SDL_GetKeyName(SDLK_CUT); |
191 | SDLTest_AssertPass("Call to SDL_GetKeyName()" ); |
192 | SDLTest_AssertCheck(result != NULL, "Verify result from call is not NULL" ); |
193 | SDLTest_AssertCheck(SDL_strcmp(result, expected) == 0, "Verify result from call is valid, expected: %s, got: %s" , expected, result); |
194 | |
195 | /* Case where key has a 4 character name */ |
196 | expected = "Down" ; |
197 | result = (char *)SDL_GetKeyName(SDLK_DOWN); |
198 | SDLTest_AssertPass("Call to SDL_GetKeyName()" ); |
199 | SDLTest_AssertCheck(result != NULL, "Verify result from call is not NULL" ); |
200 | SDLTest_AssertCheck(SDL_strcmp(result, expected) == 0, "Verify result from call is valid, expected: %s, got: %s" , expected, result); |
201 | |
202 | /* Case where key has a N character name */ |
203 | expected = "BrightnessUp" ; |
204 | result = (char *)SDL_GetKeyName(SDLK_BRIGHTNESSUP); |
205 | SDLTest_AssertPass("Call to SDL_GetKeyName()" ); |
206 | SDLTest_AssertCheck(result != NULL, "Verify result from call is not NULL" ); |
207 | SDLTest_AssertCheck(SDL_strcmp(result, expected) == 0, "Verify result from call is valid, expected: %s, got: %s" , expected, result); |
208 | |
209 | /* Case where key has a N character name with space */ |
210 | expected = "Keypad MemStore" ; |
211 | result = (char *)SDL_GetKeyName(SDLK_KP_MEMSTORE); |
212 | SDLTest_AssertPass("Call to SDL_GetKeyName()" ); |
213 | SDLTest_AssertCheck(result != NULL, "Verify result from call is not NULL" ); |
214 | SDLTest_AssertCheck(SDL_strcmp(result, expected) == 0, "Verify result from call is valid, expected: %s, got: %s" , expected, result); |
215 | |
216 | return TEST_COMPLETED; |
217 | } |
218 | |
219 | /** |
220 | * @brief SDL_GetScancodeName negative cases |
221 | * |
222 | * @sa http://wiki.libsdl.org/SDL_GetScancodeName |
223 | */ |
224 | int |
225 | keyboard_getScancodeNameNegative(void *arg) |
226 | { |
227 | SDL_Scancode scancode; |
228 | char *result; |
229 | char *expected = "" ; |
230 | |
231 | /* Clear error message */ |
232 | SDL_ClearError(); |
233 | SDLTest_AssertPass("Call to SDL_ClearError()" ); |
234 | |
235 | /* Out-of-bounds scancode */ |
236 | scancode = (SDL_Scancode)SDL_NUM_SCANCODES; |
237 | result = (char *)SDL_GetScancodeName(scancode); |
238 | SDLTest_AssertPass("Call to SDL_GetScancodeName(%d/large)" , scancode); |
239 | SDLTest_AssertCheck(result != NULL, "Verify result from call is not NULL" ); |
240 | SDLTest_AssertCheck(SDL_strcmp(result, expected) == 0, "Verify result from call is valid, expected: '%s', got: '%s'" , expected, result); |
241 | _checkInvalidScancodeError(); |
242 | |
243 | return TEST_COMPLETED; |
244 | } |
245 | |
246 | /** |
247 | * @brief SDL_GetKeyName negative cases |
248 | * |
249 | * @sa http://wiki.libsdl.org/SDL_GetKeyName |
250 | */ |
251 | int |
252 | keyboard_getKeyNameNegative(void *arg) |
253 | { |
254 | SDL_Keycode keycode; |
255 | char *result; |
256 | char *expected = "" ; |
257 | |
258 | /* Unknown keycode */ |
259 | keycode = SDLK_UNKNOWN; |
260 | result = (char *)SDL_GetKeyName(keycode); |
261 | SDLTest_AssertPass("Call to SDL_GetKeyName(%d/unknown)" , keycode); |
262 | SDLTest_AssertCheck(result != NULL, "Verify result from call is not NULL" ); |
263 | SDLTest_AssertCheck(SDL_strcmp(result, expected) == 0, "Verify result from call is valid, expected: '%s', got: '%s'" , expected, result); |
264 | |
265 | /* Clear error message */ |
266 | SDL_ClearError(); |
267 | SDLTest_AssertPass("Call to SDL_ClearError()" ); |
268 | |
269 | /* Negative keycode */ |
270 | keycode = (SDL_Keycode)SDLTest_RandomIntegerInRange(-255, -1); |
271 | result = (char *)SDL_GetKeyName(keycode); |
272 | SDLTest_AssertPass("Call to SDL_GetKeyName(%d/negative)" , keycode); |
273 | SDLTest_AssertCheck(result != NULL, "Verify result from call is not NULL" ); |
274 | SDLTest_AssertCheck(SDL_strcmp(result, expected) == 0, "Verify result from call is valid, expected: '%s', got: '%s'" , expected, result); |
275 | _checkInvalidScancodeError(); |
276 | |
277 | SDL_ClearError(); |
278 | SDLTest_AssertPass("Call to SDL_ClearError()" ); |
279 | |
280 | return TEST_COMPLETED; |
281 | } |
282 | |
283 | /** |
284 | * @brief Check call to SDL_GetModState and SDL_SetModState |
285 | * |
286 | * @sa http://wiki.libsdl.org/SDL_GetModState |
287 | * @sa http://wiki.libsdl.org/SDL_SetModState |
288 | */ |
289 | int |
290 | keyboard_getSetModState(void *arg) |
291 | { |
292 | SDL_Keymod result; |
293 | SDL_Keymod currentState; |
294 | SDL_Keymod newState; |
295 | SDL_Keymod allStates = |
296 | KMOD_NONE | |
297 | KMOD_LSHIFT | |
298 | KMOD_RSHIFT | |
299 | KMOD_LCTRL | |
300 | KMOD_RCTRL | |
301 | KMOD_LALT | |
302 | KMOD_RALT | |
303 | KMOD_LGUI | |
304 | KMOD_RGUI | |
305 | KMOD_NUM | |
306 | KMOD_CAPS | |
307 | KMOD_MODE | |
308 | KMOD_RESERVED; |
309 | |
310 | /* Get state, cache for later reset */ |
311 | result = SDL_GetModState(); |
312 | SDLTest_AssertPass("Call to SDL_GetModState()" ); |
313 | SDLTest_AssertCheck(/*result >= 0 &&*/ result <= allStates, "Verify result from call is valid, expected: 0 <= result <= %i, got: %i" , allStates, result); |
314 | currentState = result; |
315 | |
316 | /* Set random state */ |
317 | newState = SDLTest_RandomIntegerInRange(0, allStates); |
318 | SDL_SetModState(newState); |
319 | SDLTest_AssertPass("Call to SDL_SetModState(%i)" , newState); |
320 | result = SDL_GetModState(); |
321 | SDLTest_AssertPass("Call to SDL_GetModState()" ); |
322 | SDLTest_AssertCheck(result == newState, "Verify result from call is valid, expected: %i, got: %i" , newState, result); |
323 | |
324 | /* Set zero state */ |
325 | SDL_SetModState(0); |
326 | SDLTest_AssertPass("Call to SDL_SetModState(0)" ); |
327 | result = SDL_GetModState(); |
328 | SDLTest_AssertPass("Call to SDL_GetModState()" ); |
329 | SDLTest_AssertCheck(result == 0, "Verify result from call is valid, expected: 0, got: %i" , result); |
330 | |
331 | /* Revert back to cached current state if needed */ |
332 | if (currentState != 0) { |
333 | SDL_SetModState(currentState); |
334 | SDLTest_AssertPass("Call to SDL_SetModState(%i)" , currentState); |
335 | result = SDL_GetModState(); |
336 | SDLTest_AssertPass("Call to SDL_GetModState()" ); |
337 | SDLTest_AssertCheck(result == currentState, "Verify result from call is valid, expected: %i, got: %i" , currentState, result); |
338 | } |
339 | |
340 | return TEST_COMPLETED; |
341 | } |
342 | |
343 | |
344 | /** |
345 | * @brief Check call to SDL_StartTextInput and SDL_StopTextInput |
346 | * |
347 | * @sa http://wiki.libsdl.org/SDL_StartTextInput |
348 | * @sa http://wiki.libsdl.org/SDL_StopTextInput |
349 | */ |
350 | int |
351 | keyboard_startStopTextInput(void *arg) |
352 | { |
353 | /* Start-Stop */ |
354 | SDL_StartTextInput(); |
355 | SDLTest_AssertPass("Call to SDL_StartTextInput()" ); |
356 | SDL_StopTextInput(); |
357 | SDLTest_AssertPass("Call to SDL_StopTextInput()" ); |
358 | |
359 | /* Stop-Start */ |
360 | SDL_StartTextInput(); |
361 | SDLTest_AssertPass("Call to SDL_StartTextInput()" ); |
362 | |
363 | /* Start-Start */ |
364 | SDL_StartTextInput(); |
365 | SDLTest_AssertPass("Call to SDL_StartTextInput()" ); |
366 | |
367 | /* Stop-Stop */ |
368 | SDL_StopTextInput(); |
369 | SDLTest_AssertPass("Call to SDL_StopTextInput()" ); |
370 | SDL_StopTextInput(); |
371 | SDLTest_AssertPass("Call to SDL_StopTextInput()" ); |
372 | |
373 | return TEST_COMPLETED; |
374 | } |
375 | |
376 | /* Internal function to test SDL_SetTextInputRect */ |
377 | void _testSetTextInputRect(SDL_Rect refRect) |
378 | { |
379 | SDL_Rect testRect; |
380 | |
381 | testRect = refRect; |
382 | SDL_SetTextInputRect(&testRect); |
383 | SDLTest_AssertPass("Call to SDL_SetTextInputRect with refRect(x:%i,y:%i,w:%i,h:%i)" , refRect.x, refRect.y, refRect.w, refRect.h); |
384 | SDLTest_AssertCheck( |
385 | (refRect.x == testRect.x) && (refRect.y == testRect.y) && (refRect.w == testRect.w) && (refRect.h == testRect.h), |
386 | "Check that input data was not modified, expected: x:%i,y:%i,w:%i,h:%i, got: x:%i,y:%i,w:%i,h:%i" , |
387 | refRect.x, refRect.y, refRect.w, refRect.h, |
388 | testRect.x, testRect.y, testRect.w, testRect.h); |
389 | } |
390 | |
391 | /** |
392 | * @brief Check call to SDL_SetTextInputRect |
393 | * |
394 | * @sa http://wiki.libsdl.org/SDL_SetTextInputRect |
395 | */ |
396 | int |
397 | keyboard_setTextInputRect(void *arg) |
398 | { |
399 | SDL_Rect refRect; |
400 | |
401 | /* Normal visible refRect, origin inside */ |
402 | refRect.x = SDLTest_RandomIntegerInRange(1, 50); |
403 | refRect.y = SDLTest_RandomIntegerInRange(1, 50); |
404 | refRect.w = SDLTest_RandomIntegerInRange(10, 50); |
405 | refRect.h = SDLTest_RandomIntegerInRange(10, 50); |
406 | _testSetTextInputRect(refRect); |
407 | |
408 | /* Normal visible refRect, origin 0,0 */ |
409 | refRect.x = 0; |
410 | refRect.y = 0; |
411 | refRect.w = SDLTest_RandomIntegerInRange(10, 50); |
412 | refRect.h = SDLTest_RandomIntegerInRange(10, 50); |
413 | _testSetTextInputRect(refRect); |
414 | |
415 | /* 1Pixel refRect */ |
416 | refRect.x = SDLTest_RandomIntegerInRange(10, 50); |
417 | refRect.y = SDLTest_RandomIntegerInRange(10, 50); |
418 | refRect.w = 1; |
419 | refRect.h = 1; |
420 | _testSetTextInputRect(refRect); |
421 | |
422 | /* 0pixel refRect */ |
423 | refRect.x = 1; |
424 | refRect.y = 1; |
425 | refRect.w = 1; |
426 | refRect.h = 0; |
427 | _testSetTextInputRect(refRect); |
428 | |
429 | /* 0pixel refRect */ |
430 | refRect.x = 1; |
431 | refRect.y = 1; |
432 | refRect.w = 0; |
433 | refRect.h = 1; |
434 | _testSetTextInputRect(refRect); |
435 | |
436 | /* 0pixel refRect */ |
437 | refRect.x = 1; |
438 | refRect.y = 1; |
439 | refRect.w = 0; |
440 | refRect.h = 0; |
441 | _testSetTextInputRect(refRect); |
442 | |
443 | /* 0pixel refRect */ |
444 | refRect.x = 0; |
445 | refRect.y = 0; |
446 | refRect.w = 0; |
447 | refRect.h = 0; |
448 | _testSetTextInputRect(refRect); |
449 | |
450 | /* negative refRect */ |
451 | refRect.x = SDLTest_RandomIntegerInRange(-200, -100); |
452 | refRect.y = SDLTest_RandomIntegerInRange(-200, -100); |
453 | refRect.w = 50; |
454 | refRect.h = 50; |
455 | _testSetTextInputRect(refRect); |
456 | |
457 | /* oversized refRect */ |
458 | refRect.x = SDLTest_RandomIntegerInRange(1, 50); |
459 | refRect.y = SDLTest_RandomIntegerInRange(1, 50); |
460 | refRect.w = 5000; |
461 | refRect.h = 5000; |
462 | _testSetTextInputRect(refRect); |
463 | |
464 | /* NULL refRect */ |
465 | SDL_SetTextInputRect(NULL); |
466 | SDLTest_AssertPass("Call to SDL_SetTextInputRect(NULL)" ); |
467 | |
468 | return TEST_COMPLETED; |
469 | } |
470 | |
471 | /** |
472 | * @brief Check call to SDL_SetTextInputRect with invalid data |
473 | * |
474 | * @sa http://wiki.libsdl.org/SDL_SetTextInputRect |
475 | */ |
476 | int |
477 | keyboard_setTextInputRectNegative(void *arg) |
478 | { |
479 | /* Some platforms set also an error message; prepare for checking it */ |
480 | #if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_ANDROID || SDL_VIDEO_DRIVER_COCOA |
481 | const char *expectedError = "Parameter 'rect' is invalid" ; |
482 | const char *error; |
483 | |
484 | SDL_ClearError(); |
485 | SDLTest_AssertPass("Call to SDL_ClearError()" ); |
486 | #endif |
487 | |
488 | /* NULL refRect */ |
489 | SDL_SetTextInputRect(NULL); |
490 | SDLTest_AssertPass("Call to SDL_SetTextInputRect(NULL)" ); |
491 | |
492 | /* Some platforms set also an error message; so check it */ |
493 | #if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_ANDROID || SDL_VIDEO_DRIVER_COCOA |
494 | error = SDL_GetError(); |
495 | SDLTest_AssertPass("Call to SDL_GetError()" ); |
496 | SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL" ); |
497 | if (error != NULL) { |
498 | SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0, |
499 | "Validate error message, expected: '%s', got: '%s'" , expectedError, error); |
500 | } |
501 | |
502 | SDL_ClearError(); |
503 | SDLTest_AssertPass("Call to SDL_ClearError()" ); |
504 | #endif |
505 | |
506 | return TEST_COMPLETED; |
507 | } |
508 | |
509 | /** |
510 | * @brief Check call to SDL_GetScancodeFromKey |
511 | * |
512 | * @sa http://wiki.libsdl.org/SDL_GetScancodeFromKey |
513 | * @sa http://wiki.libsdl.org/SDL_Keycode |
514 | */ |
515 | int |
516 | keyboard_getScancodeFromKey(void *arg) |
517 | { |
518 | SDL_Scancode scancode; |
519 | |
520 | /* Regular key */ |
521 | scancode = SDL_GetScancodeFromKey(SDLK_4); |
522 | SDLTest_AssertPass("Call to SDL_GetScancodeFromKey(SDLK_4)" ); |
523 | SDLTest_AssertCheck(scancode == SDL_SCANCODE_4, "Validate return value from SDL_GetScancodeFromKey, expected: %i, got: %i" , SDL_SCANCODE_4, scancode); |
524 | |
525 | /* Virtual key */ |
526 | scancode = SDL_GetScancodeFromKey(SDLK_PLUS); |
527 | SDLTest_AssertPass("Call to SDL_GetScancodeFromKey(SDLK_PLUS)" ); |
528 | SDLTest_AssertCheck(scancode == 0, "Validate return value from SDL_GetScancodeFromKey, expected: 0, got: %i" , scancode); |
529 | |
530 | return TEST_COMPLETED; |
531 | } |
532 | |
533 | /** |
534 | * @brief Check call to SDL_GetScancodeFromName |
535 | * |
536 | * @sa http://wiki.libsdl.org/SDL_GetScancodeFromName |
537 | * @sa http://wiki.libsdl.org/SDL_Keycode |
538 | */ |
539 | int |
540 | keyboard_getScancodeFromName(void *arg) |
541 | { |
542 | SDL_Scancode scancode; |
543 | |
544 | /* Regular key, 1 character, first name in list */ |
545 | scancode = SDL_GetScancodeFromName("A" ); |
546 | SDLTest_AssertPass("Call to SDL_GetScancodeFromName('A')" ); |
547 | SDLTest_AssertCheck(scancode == SDL_SCANCODE_A, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i" , SDL_SCANCODE_A, scancode); |
548 | |
549 | /* Regular key, 1 character */ |
550 | scancode = SDL_GetScancodeFromName("4" ); |
551 | SDLTest_AssertPass("Call to SDL_GetScancodeFromName('4')" ); |
552 | SDLTest_AssertCheck(scancode == SDL_SCANCODE_4, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i" , SDL_SCANCODE_4, scancode); |
553 | |
554 | /* Regular key, 2 characters */ |
555 | scancode = SDL_GetScancodeFromName("F1" ); |
556 | SDLTest_AssertPass("Call to SDL_GetScancodeFromName('F1')" ); |
557 | SDLTest_AssertCheck(scancode == SDL_SCANCODE_F1, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i" , SDL_SCANCODE_F1, scancode); |
558 | |
559 | /* Regular key, 3 characters */ |
560 | scancode = SDL_GetScancodeFromName("End" ); |
561 | SDLTest_AssertPass("Call to SDL_GetScancodeFromName('End')" ); |
562 | SDLTest_AssertCheck(scancode == SDL_SCANCODE_END, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i" , SDL_SCANCODE_END, scancode); |
563 | |
564 | /* Regular key, 4 characters */ |
565 | scancode = SDL_GetScancodeFromName("Find" ); |
566 | SDLTest_AssertPass("Call to SDL_GetScancodeFromName('Find')" ); |
567 | SDLTest_AssertCheck(scancode == SDL_SCANCODE_FIND, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i" , SDL_SCANCODE_FIND, scancode); |
568 | |
569 | /* Regular key, several characters */ |
570 | scancode = SDL_GetScancodeFromName("Backspace" ); |
571 | SDLTest_AssertPass("Call to SDL_GetScancodeFromName('Backspace')" ); |
572 | SDLTest_AssertCheck(scancode == SDL_SCANCODE_BACKSPACE, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i" , SDL_SCANCODE_BACKSPACE, scancode); |
573 | |
574 | /* Regular key, several characters with space */ |
575 | scancode = SDL_GetScancodeFromName("Keypad Enter" ); |
576 | SDLTest_AssertPass("Call to SDL_GetScancodeFromName('Keypad Enter')" ); |
577 | SDLTest_AssertCheck(scancode == SDL_SCANCODE_KP_ENTER, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i" , SDL_SCANCODE_KP_ENTER, scancode); |
578 | |
579 | /* Regular key, last name in list */ |
580 | scancode = SDL_GetScancodeFromName("Sleep" ); |
581 | SDLTest_AssertPass("Call to SDL_GetScancodeFromName('Sleep')" ); |
582 | SDLTest_AssertCheck(scancode == SDL_SCANCODE_SLEEP, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i" , SDL_SCANCODE_SLEEP, scancode); |
583 | |
584 | return TEST_COMPLETED; |
585 | } |
586 | |
587 | /* |
588 | * Local helper to check for the invalid scancode error message |
589 | */ |
590 | void |
591 | _checkInvalidNameError() |
592 | { |
593 | const char *expectedError = "Parameter 'name' is invalid" ; |
594 | const char *error; |
595 | error = SDL_GetError(); |
596 | SDLTest_AssertPass("Call to SDL_GetError()" ); |
597 | SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL" ); |
598 | if (error != NULL) { |
599 | SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0, |
600 | "Validate error message, expected: '%s', got: '%s'" , expectedError, error); |
601 | SDL_ClearError(); |
602 | SDLTest_AssertPass("Call to SDL_ClearError()" ); |
603 | } |
604 | } |
605 | |
606 | /** |
607 | * @brief Check call to SDL_GetScancodeFromName with invalid data |
608 | * |
609 | * @sa http://wiki.libsdl.org/SDL_GetScancodeFromName |
610 | * @sa http://wiki.libsdl.org/SDL_Keycode |
611 | */ |
612 | int |
613 | keyboard_getScancodeFromNameNegative(void *arg) |
614 | { |
615 | char *name; |
616 | SDL_Scancode scancode; |
617 | |
618 | /* Clear error message */ |
619 | SDL_ClearError(); |
620 | SDLTest_AssertPass("Call to SDL_ClearError()" ); |
621 | |
622 | /* Random string input */ |
623 | name = SDLTest_RandomAsciiStringOfSize(32); |
624 | SDLTest_Assert(name != NULL, "Check that random name is not NULL" ); |
625 | if (name == NULL) { |
626 | return TEST_ABORTED; |
627 | } |
628 | scancode = SDL_GetScancodeFromName((const char *)name); |
629 | SDLTest_AssertPass("Call to SDL_GetScancodeFromName('%s')" , name); |
630 | SDL_free(name); |
631 | SDLTest_AssertCheck(scancode == SDL_SCANCODE_UNKNOWN, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i" , SDL_SCANCODE_UNKNOWN, scancode); |
632 | _checkInvalidNameError(); |
633 | |
634 | /* Zero length string input */ |
635 | name = "" ; |
636 | scancode = SDL_GetScancodeFromName((const char *)name); |
637 | SDLTest_AssertPass("Call to SDL_GetScancodeFromName(NULL)" ); |
638 | SDLTest_AssertCheck(scancode == SDL_SCANCODE_UNKNOWN, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i" , SDL_SCANCODE_UNKNOWN, scancode); |
639 | _checkInvalidNameError(); |
640 | |
641 | /* NULL input */ |
642 | name = NULL; |
643 | scancode = SDL_GetScancodeFromName((const char *)name); |
644 | SDLTest_AssertPass("Call to SDL_GetScancodeFromName(NULL)" ); |
645 | SDLTest_AssertCheck(scancode == SDL_SCANCODE_UNKNOWN, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i" , SDL_SCANCODE_UNKNOWN, scancode); |
646 | _checkInvalidNameError(); |
647 | |
648 | return TEST_COMPLETED; |
649 | } |
650 | |
651 | |
652 | |
653 | /* ================= Test References ================== */ |
654 | |
655 | /* Keyboard test cases */ |
656 | static const SDLTest_TestCaseReference keyboardTest1 = |
657 | { (SDLTest_TestCaseFp)keyboard_getKeyboardState, "keyboard_getKeyboardState" , "Check call to SDL_GetKeyboardState with and without numkeys reference" , TEST_ENABLED }; |
658 | |
659 | static const SDLTest_TestCaseReference keyboardTest2 = |
660 | { (SDLTest_TestCaseFp)keyboard_getKeyboardFocus, "keyboard_getKeyboardFocus" , "Check call to SDL_GetKeyboardFocus" , TEST_ENABLED }; |
661 | |
662 | static const SDLTest_TestCaseReference keyboardTest3 = |
663 | { (SDLTest_TestCaseFp)keyboard_getKeyFromName, "keyboard_getKeyFromName" , "Check call to SDL_GetKeyFromName for known, unknown and invalid name" , TEST_ENABLED }; |
664 | |
665 | static const SDLTest_TestCaseReference keyboardTest4 = |
666 | { (SDLTest_TestCaseFp)keyboard_getKeyFromScancode, "keyboard_getKeyFromScancode" , "Check call to SDL_GetKeyFromScancode" , TEST_ENABLED }; |
667 | |
668 | static const SDLTest_TestCaseReference keyboardTest5 = |
669 | { (SDLTest_TestCaseFp)keyboard_getKeyName, "keyboard_getKeyName" , "Check call to SDL_GetKeyName" , TEST_ENABLED }; |
670 | |
671 | static const SDLTest_TestCaseReference keyboardTest6 = |
672 | { (SDLTest_TestCaseFp)keyboard_getSetModState, "keyboard_getSetModState" , "Check call to SDL_GetModState and SDL_SetModState" , TEST_ENABLED }; |
673 | |
674 | static const SDLTest_TestCaseReference keyboardTest7 = |
675 | { (SDLTest_TestCaseFp)keyboard_startStopTextInput, "keyboard_startStopTextInput" , "Check call to SDL_StartTextInput and SDL_StopTextInput" , TEST_ENABLED }; |
676 | |
677 | static const SDLTest_TestCaseReference keyboardTest8 = |
678 | { (SDLTest_TestCaseFp)keyboard_setTextInputRect, "keyboard_setTextInputRect" , "Check call to SDL_SetTextInputRect" , TEST_ENABLED }; |
679 | |
680 | static const SDLTest_TestCaseReference keyboardTest9 = |
681 | { (SDLTest_TestCaseFp)keyboard_setTextInputRectNegative, "keyboard_setTextInputRectNegative" , "Check call to SDL_SetTextInputRect with invalid data" , TEST_ENABLED }; |
682 | |
683 | static const SDLTest_TestCaseReference keyboardTest10 = |
684 | { (SDLTest_TestCaseFp)keyboard_getScancodeFromKey, "keyboard_getScancodeFromKey" , "Check call to SDL_GetScancodeFromKey" , TEST_ENABLED }; |
685 | |
686 | static const SDLTest_TestCaseReference keyboardTest11 = |
687 | { (SDLTest_TestCaseFp)keyboard_getScancodeFromName, "keyboard_getScancodeFromName" , "Check call to SDL_GetScancodeFromName" , TEST_ENABLED }; |
688 | |
689 | static const SDLTest_TestCaseReference keyboardTest12 = |
690 | { (SDLTest_TestCaseFp)keyboard_getScancodeFromNameNegative, "keyboard_getScancodeFromNameNegative" , "Check call to SDL_GetScancodeFromName with invalid data" , TEST_ENABLED }; |
691 | |
692 | static const SDLTest_TestCaseReference keyboardTest13 = |
693 | { (SDLTest_TestCaseFp)keyboard_getKeyNameNegative, "keyboard_getKeyNameNegative" , "Check call to SDL_GetKeyName with invalid data" , TEST_ENABLED }; |
694 | |
695 | static const SDLTest_TestCaseReference keyboardTest14 = |
696 | { (SDLTest_TestCaseFp)keyboard_getScancodeNameNegative, "keyboard_getScancodeNameNegative" , "Check call to SDL_GetScancodeName with invalid data" , TEST_ENABLED }; |
697 | |
698 | /* Sequence of Keyboard test cases */ |
699 | static const SDLTest_TestCaseReference *keyboardTests[] = { |
700 | &keyboardTest1, &keyboardTest2, &keyboardTest3, &keyboardTest4, &keyboardTest5, &keyboardTest6, |
701 | &keyboardTest7, &keyboardTest8, &keyboardTest9, &keyboardTest10, &keyboardTest11, &keyboardTest12, |
702 | &keyboardTest13, &keyboardTest14, NULL |
703 | }; |
704 | |
705 | /* Keyboard test suite (global) */ |
706 | SDLTest_TestSuiteReference keyboardTestSuite = { |
707 | "Keyboard" , |
708 | NULL, |
709 | keyboardTests, |
710 | NULL |
711 | }; |
712 | |