testautomation_keyboard.c 26 KB

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