testautomation_keyboard.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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(void)
  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_SPACE);
  109. SDLTest_AssertPass("Call to SDL_GetKeyFromScancode(valid)");
  110. SDLTest_AssertCheck(result == SDLK_SPACE, "Verify result from call, expected: %i, got: %" SDL_PRIs32, SDLK_SPACE, 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. int platform_sets_error_message = SDL_strcmp(SDL_GetCurrentVideoDriver(), "windows") == 0 ||
  396. SDL_strcmp(SDL_GetCurrentVideoDriver(), "android") == 0 ||
  397. SDL_strcmp(SDL_GetCurrentVideoDriver(), "cococa") == 0;
  398. /* Some platforms set also an error message; prepare for checking it */
  399. const char *expectedError = "Parameter 'rect' is invalid";
  400. const char *error;
  401. SDL_ClearError();
  402. SDLTest_AssertPass("Call to SDL_ClearError()");
  403. /* NULL refRect */
  404. SDL_SetTextInputRect(NULL);
  405. SDLTest_AssertPass("Call to SDL_SetTextInputRect(NULL)");
  406. /* Some platforms set also an error message; so check it */
  407. if (platform_sets_error_message) {
  408. error = SDL_GetError();
  409. SDLTest_AssertPass("Call to SDL_GetError()");
  410. SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
  411. if (error != NULL) {
  412. SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0,
  413. "Validate error message, expected: '%s', got: '%s'", expectedError, error);
  414. }
  415. }
  416. SDL_ClearError();
  417. SDLTest_AssertPass("Call to SDL_ClearError()");
  418. return TEST_COMPLETED;
  419. }
  420. /**
  421. * @brief Check call to SDL_GetScancodeFromKey
  422. *
  423. * @sa http://wiki.libsdl.org/SDL_GetScancodeFromKey
  424. * @sa http://wiki.libsdl.org/SDL_Keycode
  425. */
  426. int keyboard_getScancodeFromKey(void *arg)
  427. {
  428. SDL_Scancode scancode;
  429. /* Regular key */
  430. scancode = SDL_GetScancodeFromKey(SDLK_4);
  431. SDLTest_AssertPass("Call to SDL_GetScancodeFromKey(SDLK_4)");
  432. SDLTest_AssertCheck(scancode == SDL_SCANCODE_4, "Validate return value from SDL_GetScancodeFromKey, expected: %i, got: %i", SDL_SCANCODE_4, scancode);
  433. /* Virtual key */
  434. scancode = SDL_GetScancodeFromKey(SDLK_PLUS);
  435. SDLTest_AssertPass("Call to SDL_GetScancodeFromKey(SDLK_PLUS)");
  436. SDLTest_AssertCheck(scancode == 0, "Validate return value from SDL_GetScancodeFromKey, expected: 0, got: %i", scancode);
  437. return TEST_COMPLETED;
  438. }
  439. /**
  440. * @brief Check call to SDL_GetScancodeFromName
  441. *
  442. * @sa http://wiki.libsdl.org/SDL_GetScancodeFromName
  443. * @sa http://wiki.libsdl.org/SDL_Keycode
  444. */
  445. int keyboard_getScancodeFromName(void *arg)
  446. {
  447. SDL_Scancode scancode;
  448. /* Regular key, 1 character, first name in list */
  449. scancode = SDL_GetScancodeFromName("A");
  450. SDLTest_AssertPass("Call to SDL_GetScancodeFromName('A')");
  451. SDLTest_AssertCheck(scancode == SDL_SCANCODE_A, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_A, scancode);
  452. /* Regular key, 1 character */
  453. scancode = SDL_GetScancodeFromName("4");
  454. SDLTest_AssertPass("Call to SDL_GetScancodeFromName('4')");
  455. SDLTest_AssertCheck(scancode == SDL_SCANCODE_4, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_4, scancode);
  456. /* Regular key, 2 characters */
  457. scancode = SDL_GetScancodeFromName("F1");
  458. SDLTest_AssertPass("Call to SDL_GetScancodeFromName('F1')");
  459. SDLTest_AssertCheck(scancode == SDL_SCANCODE_F1, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_F1, scancode);
  460. /* Regular key, 3 characters */
  461. scancode = SDL_GetScancodeFromName("End");
  462. SDLTest_AssertPass("Call to SDL_GetScancodeFromName('End')");
  463. SDLTest_AssertCheck(scancode == SDL_SCANCODE_END, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_END, scancode);
  464. /* Regular key, 4 characters */
  465. scancode = SDL_GetScancodeFromName("Find");
  466. SDLTest_AssertPass("Call to SDL_GetScancodeFromName('Find')");
  467. SDLTest_AssertCheck(scancode == SDL_SCANCODE_FIND, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_FIND, scancode);
  468. /* Regular key, several characters */
  469. scancode = SDL_GetScancodeFromName("Backspace");
  470. SDLTest_AssertPass("Call to SDL_GetScancodeFromName('Backspace')");
  471. SDLTest_AssertCheck(scancode == SDL_SCANCODE_BACKSPACE, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_BACKSPACE, scancode);
  472. /* Regular key, several characters with space */
  473. scancode = SDL_GetScancodeFromName("Keypad Enter");
  474. SDLTest_AssertPass("Call to SDL_GetScancodeFromName('Keypad Enter')");
  475. SDLTest_AssertCheck(scancode == SDL_SCANCODE_KP_ENTER, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_KP_ENTER, scancode);
  476. /* Regular key, last name in list */
  477. scancode = SDL_GetScancodeFromName("Sleep");
  478. SDLTest_AssertPass("Call to SDL_GetScancodeFromName('Sleep')");
  479. SDLTest_AssertCheck(scancode == SDL_SCANCODE_SLEEP, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_SLEEP, scancode);
  480. return TEST_COMPLETED;
  481. }
  482. /*
  483. * Local helper to check for the invalid scancode error message
  484. */
  485. void _checkInvalidNameError(void)
  486. {
  487. const char *expectedError = "Parameter 'name' is invalid";
  488. const char *error;
  489. error = SDL_GetError();
  490. SDLTest_AssertPass("Call to SDL_GetError()");
  491. SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
  492. if (error != NULL) {
  493. SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0,
  494. "Validate error message, expected: '%s', got: '%s'", expectedError, error);
  495. SDL_ClearError();
  496. SDLTest_AssertPass("Call to SDL_ClearError()");
  497. }
  498. }
  499. /**
  500. * @brief Check call to SDL_GetScancodeFromName with invalid data
  501. *
  502. * @sa http://wiki.libsdl.org/SDL_GetScancodeFromName
  503. * @sa http://wiki.libsdl.org/SDL_Keycode
  504. */
  505. int keyboard_getScancodeFromNameNegative(void *arg)
  506. {
  507. const char *name;
  508. SDL_Scancode scancode;
  509. /* Clear error message */
  510. SDL_ClearError();
  511. SDLTest_AssertPass("Call to SDL_ClearError()");
  512. /* Random string input */
  513. name = SDLTest_RandomAsciiStringOfSize(32);
  514. SDLTest_Assert(name != NULL, "Check that random name is not NULL");
  515. if (name == NULL) {
  516. return TEST_ABORTED;
  517. }
  518. scancode = SDL_GetScancodeFromName(name);
  519. SDLTest_AssertPass("Call to SDL_GetScancodeFromName('%s')", name);
  520. SDL_free((void *)name);
  521. SDLTest_AssertCheck(scancode == SDL_SCANCODE_UNKNOWN, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_UNKNOWN, scancode);
  522. _checkInvalidNameError();
  523. /* Zero length string input */
  524. name = "";
  525. scancode = SDL_GetScancodeFromName(name);
  526. SDLTest_AssertPass("Call to SDL_GetScancodeFromName(NULL)");
  527. SDLTest_AssertCheck(scancode == SDL_SCANCODE_UNKNOWN, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_UNKNOWN, scancode);
  528. _checkInvalidNameError();
  529. /* NULL input */
  530. name = NULL;
  531. scancode = SDL_GetScancodeFromName(name);
  532. SDLTest_AssertPass("Call to SDL_GetScancodeFromName(NULL)");
  533. SDLTest_AssertCheck(scancode == SDL_SCANCODE_UNKNOWN, "Validate return value from SDL_GetScancodeFromName, expected: %i, got: %i", SDL_SCANCODE_UNKNOWN, scancode);
  534. _checkInvalidNameError();
  535. return TEST_COMPLETED;
  536. }
  537. /* ================= Test References ================== */
  538. /* Keyboard test cases */
  539. static const SDLTest_TestCaseReference keyboardTest1 = {
  540. (SDLTest_TestCaseFp)keyboard_getKeyboardState, "keyboard_getKeyboardState", "Check call to SDL_GetKeyboardState with and without numkeys reference", TEST_ENABLED
  541. };
  542. static const SDLTest_TestCaseReference keyboardTest2 = {
  543. (SDLTest_TestCaseFp)keyboard_getKeyboardFocus, "keyboard_getKeyboardFocus", "Check call to SDL_GetKeyboardFocus", TEST_ENABLED
  544. };
  545. static const SDLTest_TestCaseReference keyboardTest3 = {
  546. (SDLTest_TestCaseFp)keyboard_getKeyFromName, "keyboard_getKeyFromName", "Check call to SDL_GetKeyFromName for known, unknown and invalid name", TEST_ENABLED
  547. };
  548. static const SDLTest_TestCaseReference keyboardTest4 = {
  549. (SDLTest_TestCaseFp)keyboard_getKeyFromScancode, "keyboard_getKeyFromScancode", "Check call to SDL_GetKeyFromScancode", TEST_ENABLED
  550. };
  551. static const SDLTest_TestCaseReference keyboardTest5 = {
  552. (SDLTest_TestCaseFp)keyboard_getKeyName, "keyboard_getKeyName", "Check call to SDL_GetKeyName", TEST_ENABLED
  553. };
  554. static const SDLTest_TestCaseReference keyboardTest6 = {
  555. (SDLTest_TestCaseFp)keyboard_getSetModState, "keyboard_getSetModState", "Check call to SDL_GetModState and SDL_SetModState", TEST_ENABLED
  556. };
  557. static const SDLTest_TestCaseReference keyboardTest7 = {
  558. (SDLTest_TestCaseFp)keyboard_startStopTextInput, "keyboard_startStopTextInput", "Check call to SDL_StartTextInput and SDL_StopTextInput", TEST_ENABLED
  559. };
  560. static const SDLTest_TestCaseReference keyboardTest8 = {
  561. (SDLTest_TestCaseFp)keyboard_setTextInputRect, "keyboard_setTextInputRect", "Check call to SDL_SetTextInputRect", TEST_ENABLED
  562. };
  563. static const SDLTest_TestCaseReference keyboardTest9 = {
  564. (SDLTest_TestCaseFp)keyboard_setTextInputRectNegative, "keyboard_setTextInputRectNegative", "Check call to SDL_SetTextInputRect with invalid data", TEST_ENABLED
  565. };
  566. static const SDLTest_TestCaseReference keyboardTest10 = {
  567. (SDLTest_TestCaseFp)keyboard_getScancodeFromKey, "keyboard_getScancodeFromKey", "Check call to SDL_GetScancodeFromKey", TEST_ENABLED
  568. };
  569. static const SDLTest_TestCaseReference keyboardTest11 = {
  570. (SDLTest_TestCaseFp)keyboard_getScancodeFromName, "keyboard_getScancodeFromName", "Check call to SDL_GetScancodeFromName", TEST_ENABLED
  571. };
  572. static const SDLTest_TestCaseReference keyboardTest12 = {
  573. (SDLTest_TestCaseFp)keyboard_getScancodeFromNameNegative, "keyboard_getScancodeFromNameNegative", "Check call to SDL_GetScancodeFromName with invalid data", TEST_ENABLED
  574. };
  575. static const SDLTest_TestCaseReference keyboardTest13 = {
  576. (SDLTest_TestCaseFp)keyboard_getKeyNameNegative, "keyboard_getKeyNameNegative", "Check call to SDL_GetKeyName with invalid data", TEST_ENABLED
  577. };
  578. static const SDLTest_TestCaseReference keyboardTest14 = {
  579. (SDLTest_TestCaseFp)keyboard_getScancodeNameNegative, "keyboard_getScancodeNameNegative", "Check call to SDL_GetScancodeName with invalid data", TEST_ENABLED
  580. };
  581. /* Sequence of Keyboard test cases */
  582. static const SDLTest_TestCaseReference *keyboardTests[] = {
  583. &keyboardTest1, &keyboardTest2, &keyboardTest3, &keyboardTest4, &keyboardTest5, &keyboardTest6,
  584. &keyboardTest7, &keyboardTest8, &keyboardTest9, &keyboardTest10, &keyboardTest11, &keyboardTest12,
  585. &keyboardTest13, &keyboardTest14, NULL
  586. };
  587. /* Keyboard test suite (global) */
  588. SDLTest_TestSuiteReference keyboardTestSuite = {
  589. "Keyboard",
  590. NULL,
  591. keyboardTests,
  592. NULL
  593. };