🛠️🐜 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.

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