testautomation_keyboard.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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 SDLCALL keyboard_getKeyboardState(void *arg)
  15. {
  16. int numkeys;
  17. const bool *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: %d", numkeys);
  28. return TEST_COMPLETED;
  29. }
  30. /**
  31. * Check call to SDL_GetKeyboardFocus
  32. *
  33. * \sa SDL_GetKeyboardFocus
  34. */
  35. static int SDLCALL 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 SDLCALL 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('A', true)");
  53. SDLTest_AssertCheck(result == SDLK_A, "Verify result from call, expected: %d, got: %" SDL_PRIu32, 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: %d, got: %" SDL_PRIu32, 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: %d, got: %" SDL_PRIu32, 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: %d, got: %" SDL_PRIu32, SDLK_FIND, result);
  66. /* Case where Key is known, multiple character input */
  67. result = SDL_GetKeyFromName("MediaStop");
  68. SDLTest_AssertPass("Call to SDL_GetKeyFromName(known/multi)");
  69. SDLTest_AssertCheck(result == SDLK_MEDIA_STOP, "Verify result from call, expected: %d, got: %" SDL_PRIu32, SDLK_MEDIA_STOP, 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: %d, got: %" SDL_PRIu32, 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: %d, got: %" SDL_PRIu32, 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 SDLCALL keyboard_getKeyFromScancode(void *arg)
  103. {
  104. SDL_Keycode result;
  105. /* Case where input is valid */
  106. result = SDL_GetKeyFromScancode(SDL_SCANCODE_SPACE, SDL_KMOD_NONE, false);
  107. SDLTest_AssertPass("Call to SDL_GetKeyFromScancode(valid)");
  108. SDLTest_AssertCheck(result == SDLK_SPACE, "Verify result from call, expected: %d, got: %" SDL_PRIu32, SDLK_SPACE, result);
  109. /* Case where input is zero */
  110. result = SDL_GetKeyFromScancode(SDL_SCANCODE_UNKNOWN, SDL_KMOD_NONE, false);
  111. SDLTest_AssertPass("Call to SDL_GetKeyFromScancode(0)");
  112. SDLTest_AssertCheck(result == SDLK_UNKNOWN, "Verify result from call is UNKNOWN, expected: %d, got: %" SDL_PRIu32, 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((SDL_Scancode)-999, SDL_KMOD_NONE, false);
  118. SDLTest_AssertPass("Call to SDL_GetKeyFromScancode(-999)");
  119. SDLTest_AssertCheck(result == SDLK_UNKNOWN, "Verify result from call is UNKNOWN, expected: %d, got: %" SDL_PRIu32, SDLK_UNKNOWN, result);
  120. checkInvalidScancodeError();
  121. /* Case where input is invalid (too big) */
  122. result = SDL_GetKeyFromScancode((SDL_Scancode)999, SDL_KMOD_NONE, false);
  123. SDLTest_AssertPass("Call to SDL_GetKeyFromScancode(999)");
  124. SDLTest_AssertCheck(result == SDLK_UNKNOWN, "Verify result from call is UNKNOWN, expected: %d, got: %" SDL_PRIu32, 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 SDLCALL 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 = "MediaPlay";
  163. result = SDL_GetKeyName(SDLK_MEDIA_PLAY);
  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 SDLCALL 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_SCANCODE_COUNT;
  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 SDLCALL 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_PRIu32 "/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_PRIu32 "/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 SDLCALL 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 <= 0x%.4x, got: 0x%.4x", allStates, result);
  256. currentState = result;
  257. /* Set random state */
  258. newState = (SDL_Keymod)SDLTest_RandomIntegerInRange(0, allStates);
  259. SDL_SetModState(newState);
  260. SDLTest_AssertPass("Call to SDL_SetModState(0x%.4x)", newState);
  261. result = SDL_GetModState();
  262. SDLTest_AssertPass("Call to SDL_GetModState()");
  263. SDLTest_AssertCheck(result == newState, "Verify result from call is valid, expected: 0x%.4x, got: 0x%.4x", 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: 0x%.4x", 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(0x%.4x)", currentState);
  274. result = SDL_GetModState();
  275. SDLTest_AssertPass("Call to SDL_GetModState()");
  276. SDLTest_AssertCheck(result == currentState, "Verify result from call is valid, expected: 0x%.4x, got: 0x%.4x", 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 SDLCALL keyboard_startStopTextInput(void *arg)
  287. {
  288. SDL_Window *window = SDL_GetKeyboardFocus();
  289. /* Start-Stop */
  290. SDL_StartTextInput(window);
  291. SDLTest_AssertPass("Call to SDL_StartTextInput()");
  292. SDL_StopTextInput(window);
  293. SDLTest_AssertPass("Call to SDL_StopTextInput()");
  294. /* Stop-Start */
  295. SDL_StartTextInput(window);
  296. SDLTest_AssertPass("Call to SDL_StartTextInput()");
  297. /* Start-Start */
  298. SDL_StartTextInput(window);
  299. SDLTest_AssertPass("Call to SDL_StartTextInput()");
  300. /* Stop-Stop */
  301. SDL_StopTextInput(window);
  302. SDLTest_AssertPass("Call to SDL_StopTextInput()");
  303. SDL_StopTextInput(window);
  304. SDLTest_AssertPass("Call to SDL_StopTextInput()");
  305. return TEST_COMPLETED;
  306. }
  307. /* Internal function to test SDL_SetTextInputArea */
  308. static void testSetTextInputArea(SDL_Window *window, SDL_Rect refRect)
  309. {
  310. SDL_Rect testRect;
  311. testRect = refRect;
  312. SDL_SetTextInputArea(window, &testRect, 0);
  313. SDLTest_AssertPass("Call to SDL_SetTextInputArea with refRect(x:%d,y:%d,w:%d,h:%d)", refRect.x, refRect.y, refRect.w, refRect.h);
  314. SDLTest_AssertCheck(
  315. (refRect.x == testRect.x) && (refRect.y == testRect.y) && (refRect.w == testRect.w) && (refRect.h == testRect.h),
  316. "Check that input data was not modified, expected: x:%d,y:%d,w:%d,h:%d, got: x:%d,y:%d,w:%d,h:%d",
  317. refRect.x, refRect.y, refRect.w, refRect.h,
  318. testRect.x, testRect.y, testRect.w, testRect.h);
  319. }
  320. /**
  321. * Check call to SDL_SetTextInputArea
  322. *
  323. * \sa SDL_SetTextInputArea
  324. */
  325. static int SDLCALL keyboard_setTextInputArea(void *arg)
  326. {
  327. SDL_Window *window = SDL_GetKeyboardFocus();
  328. SDL_Rect refRect;
  329. /* Normal visible refRect, origin inside */
  330. refRect.x = SDLTest_RandomIntegerInRange(1, 50);
  331. refRect.y = SDLTest_RandomIntegerInRange(1, 50);
  332. refRect.w = SDLTest_RandomIntegerInRange(10, 50);
  333. refRect.h = SDLTest_RandomIntegerInRange(10, 50);
  334. testSetTextInputArea(window, refRect);
  335. /* Normal visible refRect, origin 0,0 */
  336. refRect.x = 0;
  337. refRect.y = 0;
  338. refRect.w = SDLTest_RandomIntegerInRange(10, 50);
  339. refRect.h = SDLTest_RandomIntegerInRange(10, 50);
  340. testSetTextInputArea(window, refRect);
  341. /* 1Pixel refRect */
  342. refRect.x = SDLTest_RandomIntegerInRange(10, 50);
  343. refRect.y = SDLTest_RandomIntegerInRange(10, 50);
  344. refRect.w = 1;
  345. refRect.h = 1;
  346. testSetTextInputArea(window, refRect);
  347. /* 0pixel refRect */
  348. refRect.x = 1;
  349. refRect.y = 1;
  350. refRect.w = 1;
  351. refRect.h = 0;
  352. testSetTextInputArea(window, refRect);
  353. /* 0pixel refRect */
  354. refRect.x = 1;
  355. refRect.y = 1;
  356. refRect.w = 0;
  357. refRect.h = 1;
  358. testSetTextInputArea(window, refRect);
  359. /* 0pixel refRect */
  360. refRect.x = 1;
  361. refRect.y = 1;
  362. refRect.w = 0;
  363. refRect.h = 0;
  364. testSetTextInputArea(window, refRect);
  365. /* 0pixel refRect */
  366. refRect.x = 0;
  367. refRect.y = 0;
  368. refRect.w = 0;
  369. refRect.h = 0;
  370. testSetTextInputArea(window, refRect);
  371. /* negative refRect */
  372. refRect.x = SDLTest_RandomIntegerInRange(-200, -100);
  373. refRect.y = SDLTest_RandomIntegerInRange(-200, -100);
  374. refRect.w = 50;
  375. refRect.h = 50;
  376. testSetTextInputArea(window, refRect);
  377. /* oversized refRect */
  378. refRect.x = SDLTest_RandomIntegerInRange(1, 50);
  379. refRect.y = SDLTest_RandomIntegerInRange(1, 50);
  380. refRect.w = 5000;
  381. refRect.h = 5000;
  382. testSetTextInputArea(window, refRect);
  383. /* NULL refRect */
  384. SDL_SetTextInputArea(window, NULL, 0);
  385. SDLTest_AssertPass("Call to SDL_SetTextInputArea(NULL)");
  386. return TEST_COMPLETED;
  387. }
  388. /**
  389. * Check call to SDL_SetTextInputArea with invalid data
  390. *
  391. * \sa SDL_SetTextInputArea
  392. */
  393. static int SDLCALL keyboard_setTextInputAreaNegative(void *arg)
  394. {
  395. /* Some platforms set also an error message; prepare for checking it */
  396. #if defined(SDL_VIDEO_DRIVER_WINDOWS) || defined(SDL_VIDEO_DRIVER_ANDROID) || defined(SDL_VIDEO_DRIVER_COCOA)
  397. const char *expectedError = "Parameter 'rect' is invalid";
  398. const char *error;
  399. SDL_ClearError();
  400. SDLTest_AssertPass("Call to SDL_ClearError()");
  401. #endif
  402. /* NULL refRect */
  403. SDL_SetTextInputArea(SDL_GetKeyboardFocus(), NULL, 0);
  404. SDLTest_AssertPass("Call to SDL_SetTextInputArea(NULL)");
  405. /* Some platforms set also an error message; so check it */
  406. #if defined(SDL_VIDEO_DRIVER_WINDOWS) || defined(SDL_VIDEO_DRIVER_ANDROID) || defined(SDL_VIDEO_DRIVER_COCOA)
  407. error = SDL_GetError();
  408. SDLTest_AssertPass("Call to SDL_GetError()");
  409. SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
  410. if (error != NULL) {
  411. SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0,
  412. "Validate error message, expected: '%s', got: '%s'", expectedError, error);
  413. }
  414. SDL_ClearError();
  415. SDLTest_AssertPass("Call to SDL_ClearError()");
  416. #endif
  417. return TEST_COMPLETED;
  418. }
  419. /**
  420. * Check call to SDL_GetScancodeFromName
  421. *
  422. * \sa SDL_GetScancodeFromName
  423. * \sa SDL_Keycode
  424. */
  425. static int SDLCALL keyboard_getScancodeFromName(void *arg)
  426. {
  427. SDL_Scancode scancode;
  428. /* Regular key, 1 character, first name in list */
  429. scancode = SDL_GetScancodeFromName("A");
  430. SDLTest_AssertPass("Call to SDL_GetScancodeFromName('A')");
  431. SDLTest_AssertCheck(scancode == SDL_SCANCODE_A, "Validate return value from SDL_GetScancodeFromName, expected: %d, got: %d", SDL_SCANCODE_A, scancode);
  432. /* Regular key, 1 character */
  433. scancode = SDL_GetScancodeFromName("4");
  434. SDLTest_AssertPass("Call to SDL_GetScancodeFromName('4')");
  435. SDLTest_AssertCheck(scancode == SDL_SCANCODE_4, "Validate return value from SDL_GetScancodeFromName, expected: %d, got: %d", SDL_SCANCODE_4, scancode);
  436. /* Regular key, 2 characters */
  437. scancode = SDL_GetScancodeFromName("F1");
  438. SDLTest_AssertPass("Call to SDL_GetScancodeFromName('F1')");
  439. SDLTest_AssertCheck(scancode == SDL_SCANCODE_F1, "Validate return value from SDL_GetScancodeFromName, expected: %d, got: %d", SDL_SCANCODE_F1, scancode);
  440. /* Regular key, 3 characters */
  441. scancode = SDL_GetScancodeFromName("End");
  442. SDLTest_AssertPass("Call to SDL_GetScancodeFromName('End')");
  443. SDLTest_AssertCheck(scancode == SDL_SCANCODE_END, "Validate return value from SDL_GetScancodeFromName, expected: %d, got: %d", SDL_SCANCODE_END, scancode);
  444. /* Regular key, 4 characters */
  445. scancode = SDL_GetScancodeFromName("Find");
  446. SDLTest_AssertPass("Call to SDL_GetScancodeFromName('Find')");
  447. SDLTest_AssertCheck(scancode == SDL_SCANCODE_FIND, "Validate return value from SDL_GetScancodeFromName, expected: %d, got: %d", SDL_SCANCODE_FIND, scancode);
  448. /* Regular key, several characters */
  449. scancode = SDL_GetScancodeFromName("Backspace");
  450. SDLTest_AssertPass("Call to SDL_GetScancodeFromName('Backspace')");
  451. SDLTest_AssertCheck(scancode == SDL_SCANCODE_BACKSPACE, "Validate return value from SDL_GetScancodeFromName, expected: %d, got: %d", SDL_SCANCODE_BACKSPACE, scancode);
  452. /* Regular key, several characters with space */
  453. scancode = SDL_GetScancodeFromName("Keypad Enter");
  454. SDLTest_AssertPass("Call to SDL_GetScancodeFromName('Keypad Enter')");
  455. SDLTest_AssertCheck(scancode == SDL_SCANCODE_KP_ENTER, "Validate return value from SDL_GetScancodeFromName, expected: %d, got: %d", SDL_SCANCODE_KP_ENTER, scancode);
  456. /* Regular key, last name in list */
  457. scancode = SDL_GetScancodeFromName("Sleep");
  458. SDLTest_AssertPass("Call to SDL_GetScancodeFromName('Sleep')");
  459. SDLTest_AssertCheck(scancode == SDL_SCANCODE_SLEEP, "Validate return value from SDL_GetScancodeFromName, expected: %d, got: %d", SDL_SCANCODE_SLEEP, scancode);
  460. return TEST_COMPLETED;
  461. }
  462. /*
  463. * Local helper to check for the invalid scancode error message
  464. */
  465. static void checkInvalidNameError(void)
  466. {
  467. const char *expectedError = "Parameter 'name' is invalid";
  468. const char *error;
  469. error = SDL_GetError();
  470. SDLTest_AssertPass("Call to SDL_GetError()");
  471. SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
  472. if (error != NULL) {
  473. SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0,
  474. "Validate error message, expected: '%s', got: '%s'", expectedError, error);
  475. SDL_ClearError();
  476. SDLTest_AssertPass("Call to SDL_ClearError()");
  477. }
  478. }
  479. /**
  480. * Check call to SDL_GetScancodeFromName with invalid data
  481. *
  482. * \sa SDL_GetScancodeFromName
  483. * \sa SDL_Keycode
  484. */
  485. static int SDLCALL keyboard_getScancodeFromNameNegative(void *arg)
  486. {
  487. char *name;
  488. SDL_Scancode scancode;
  489. /* Clear error message */
  490. SDL_ClearError();
  491. SDLTest_AssertPass("Call to SDL_ClearError()");
  492. /* Random string input */
  493. name = SDLTest_RandomAsciiStringOfSize(32);
  494. SDLTest_Assert(name != NULL, "Check that random name is not NULL");
  495. if (name == NULL) {
  496. return TEST_ABORTED;
  497. }
  498. scancode = SDL_GetScancodeFromName(name);
  499. SDLTest_AssertPass("Call to SDL_GetScancodeFromName('%s')", name);
  500. SDL_free(name);
  501. SDLTest_AssertCheck(scancode == SDL_SCANCODE_UNKNOWN, "Validate return value from SDL_GetScancodeFromName, expected: %d, got: %d", SDL_SCANCODE_UNKNOWN, scancode);
  502. checkInvalidNameError();
  503. /* Zero length string input */
  504. name = "";
  505. scancode = SDL_GetScancodeFromName(name);
  506. SDLTest_AssertPass("Call to SDL_GetScancodeFromName(NULL)");
  507. SDLTest_AssertCheck(scancode == SDL_SCANCODE_UNKNOWN, "Validate return value from SDL_GetScancodeFromName, expected: %d, got: %d", SDL_SCANCODE_UNKNOWN, scancode);
  508. checkInvalidNameError();
  509. /* NULL input */
  510. name = NULL;
  511. scancode = SDL_GetScancodeFromName(name);
  512. SDLTest_AssertPass("Call to SDL_GetScancodeFromName(NULL)");
  513. SDLTest_AssertCheck(scancode == SDL_SCANCODE_UNKNOWN, "Validate return value from SDL_GetScancodeFromName, expected: %d, got: %d", SDL_SCANCODE_UNKNOWN, scancode);
  514. checkInvalidNameError();
  515. return TEST_COMPLETED;
  516. }
  517. /* ================= Test References ================== */
  518. /* Keyboard test cases */
  519. static const SDLTest_TestCaseReference keyboardTestGetKeyboardState = {
  520. keyboard_getKeyboardState, "keyboard_getKeyboardState", "Check call to SDL_GetKeyboardState with and without numkeys reference", TEST_ENABLED
  521. };
  522. static const SDLTest_TestCaseReference keyboardTestGetKeyboardFocus = {
  523. keyboard_getKeyboardFocus, "keyboard_getKeyboardFocus", "Check call to SDL_GetKeyboardFocus", TEST_ENABLED
  524. };
  525. static const SDLTest_TestCaseReference keyboardTestGetKeyFromName = {
  526. keyboard_getKeyFromName, "keyboard_getKeyFromName", "Check call to SDL_GetKeyFromName for known, unknown and invalid name", TEST_ENABLED
  527. };
  528. static const SDLTest_TestCaseReference keyboardTestGetKeyFromScancode = {
  529. keyboard_getKeyFromScancode, "keyboard_getKeyFromScancode", "Check call to SDL_GetKeyFromScancode", TEST_ENABLED
  530. };
  531. static const SDLTest_TestCaseReference keyboardTestGetKeyName = {
  532. keyboard_getKeyName, "keyboard_getKeyName", "Check call to SDL_GetKeyName", TEST_ENABLED
  533. };
  534. static const SDLTest_TestCaseReference keyboardTestGetSetModState = {
  535. keyboard_getSetModState, "keyboard_getSetModState", "Check call to SDL_GetModState and SDL_SetModState", TEST_ENABLED
  536. };
  537. static const SDLTest_TestCaseReference keyboardTestStartStopTextInput = {
  538. keyboard_startStopTextInput, "keyboard_startStopTextInput", "Check call to SDL_StartTextInput and SDL_StopTextInput", TEST_ENABLED
  539. };
  540. static const SDLTest_TestCaseReference keyboardTestSetTextInputArea = {
  541. keyboard_setTextInputArea, "keyboard_setTextInputArea", "Check call to SDL_SetTextInputArea", TEST_ENABLED
  542. };
  543. static const SDLTest_TestCaseReference keyboardTestSetTextInputAreaNegative = {
  544. keyboard_setTextInputAreaNegative, "keyboard_setTextInputAreaNegative", "Check call to SDL_SetTextInputArea with invalid data", TEST_ENABLED
  545. };
  546. static const SDLTest_TestCaseReference keyboardTestGetScancodeFromName = {
  547. keyboard_getScancodeFromName, "keyboard_getScancodeFromName", "Check call to SDL_GetScancodeFromName", TEST_ENABLED
  548. };
  549. static const SDLTest_TestCaseReference keyboardTestGetScancodeFromNameNegative = {
  550. keyboard_getScancodeFromNameNegative, "keyboard_getScancodeFromNameNegative", "Check call to SDL_GetScancodeFromName with invalid data", TEST_ENABLED
  551. };
  552. static const SDLTest_TestCaseReference keyboardTestGetKeyNameNegative = {
  553. keyboard_getKeyNameNegative, "keyboard_getKeyNameNegative", "Check call to SDL_GetKeyName with invalid data", TEST_ENABLED
  554. };
  555. static const SDLTest_TestCaseReference keyboardTestGetScancodeNameNegative = {
  556. keyboard_getScancodeNameNegative, "keyboard_getScancodeNameNegative", "Check call to SDL_GetScancodeName with invalid data", TEST_ENABLED
  557. };
  558. /* Sequence of Keyboard test cases */
  559. static const SDLTest_TestCaseReference *keyboardTests[] = {
  560. &keyboardTestGetKeyboardState,
  561. &keyboardTestGetKeyboardFocus,
  562. &keyboardTestGetKeyFromName,
  563. &keyboardTestGetKeyFromScancode,
  564. &keyboardTestGetKeyName,
  565. &keyboardTestGetSetModState,
  566. &keyboardTestStartStopTextInput,
  567. &keyboardTestSetTextInputArea,
  568. &keyboardTestSetTextInputAreaNegative,
  569. &keyboardTestGetScancodeFromName,
  570. &keyboardTestGetScancodeFromNameNegative,
  571. &keyboardTestGetKeyNameNegative,
  572. &keyboardTestGetScancodeNameNegative,
  573. NULL
  574. };
  575. /* Keyboard test suite (global) */
  576. SDLTest_TestSuiteReference keyboardTestSuite = {
  577. "Keyboard",
  578. NULL,
  579. keyboardTests,
  580. NULL
  581. };