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