testautomation_keyboard.c 26 KB

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