🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

706 lines
26 KiB

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