testautomation_keyboard.c 26 KB

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