testautomation_keyboard.c 26 KB

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