Debian Codegraph @

test/testautomation_keyboard.c

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