testautomation_clipboard.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /**
  2. * New/updated tests: aschiffler at ferzkopp dot net
  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. * \brief Check call to SDL_HasClipboardText
  11. *
  12. * \sa SDL_HasClipboardText
  13. */
  14. static int clipboard_testHasClipboardText(void *arg)
  15. {
  16. SDL_HasClipboardText();
  17. SDLTest_AssertPass("Call to SDL_HasClipboardText succeeded");
  18. return TEST_COMPLETED;
  19. }
  20. /**
  21. * \brief Check call to SDL_HasClipboardData
  22. *
  23. * \sa SDL_HasClipboardData
  24. */
  25. static int clipboard_testHasClipboardData(void *arg)
  26. {
  27. SDL_HasClipboardData("image/png");
  28. SDLTest_AssertPass("Call to SDL_HasClipboardText succeeded");
  29. return TEST_COMPLETED;
  30. }
  31. /**
  32. * \brief Check call to SDL_HasPrimarySelectionText
  33. *
  34. * \sa SDL_HasPrimarySelectionText
  35. */
  36. static int clipboard_testHasPrimarySelectionText(void *arg)
  37. {
  38. SDL_HasPrimarySelectionText();
  39. SDLTest_AssertPass("Call to SDL_HasPrimarySelectionText succeeded");
  40. return TEST_COMPLETED;
  41. }
  42. /**
  43. * \brief Check call to SDL_GetClipboardText
  44. *
  45. * \sa SDL_GetClipboardText
  46. */
  47. static int clipboard_testGetClipboardText(void *arg)
  48. {
  49. char *charResult;
  50. charResult = SDL_GetClipboardText();
  51. SDLTest_AssertPass("Call to SDL_GetClipboardText succeeded");
  52. SDL_free(charResult);
  53. return TEST_COMPLETED;
  54. }
  55. /**
  56. * \brief Check call to SDL_GetClipboardData
  57. *
  58. * \sa SDL_GetClipboardText
  59. */
  60. static int clipboard_testGetClipboardData(void *arg)
  61. {
  62. void *buffer = NULL;
  63. size_t length;
  64. buffer = SDL_GetClipboardData(&length, "image/png");
  65. SDLTest_AssertPass("Call to SDL_GetClipboardData succeeded");
  66. if (buffer != NULL) {
  67. SDL_free(buffer);
  68. }
  69. return TEST_COMPLETED;
  70. }
  71. /**
  72. * \brief Check call to SDL_GetPrimarySelectionText
  73. *
  74. * \sa SDL_GetPrimarySelectionText
  75. */
  76. static int clipboard_testGetPrimarySelectionText(void *arg)
  77. {
  78. char *charResult;
  79. charResult = SDL_GetPrimarySelectionText();
  80. SDLTest_AssertPass("Call to SDL_GetPrimarySelectionText succeeded");
  81. SDL_free(charResult);
  82. return TEST_COMPLETED;
  83. }
  84. /**
  85. * \brief Check call to SDL_SetClipboardText
  86. * \sa SDL_SetClipboardText
  87. */
  88. static int clipboard_testSetClipboardText(void *arg)
  89. {
  90. char *textRef = SDLTest_RandomAsciiString();
  91. char *text = SDL_strdup(textRef);
  92. int result;
  93. result = SDL_SetClipboardText((const char *)text);
  94. SDLTest_AssertPass("Call to SDL_SetClipboardText succeeded");
  95. SDLTest_AssertCheck(
  96. result == 0,
  97. "Validate SDL_SetClipboardText result, expected 0, got %i",
  98. result);
  99. SDLTest_AssertCheck(
  100. SDL_strcmp(textRef, text) == 0,
  101. "Verify SDL_SetClipboardText did not modify input string, expected '%s', got '%s'",
  102. textRef, text);
  103. /* Cleanup */
  104. SDL_free(textRef);
  105. SDL_free(text);
  106. return TEST_COMPLETED;
  107. }
  108. /**
  109. * \brief Check call to SDL_SetClipboardData
  110. * \sa SDL_SetClipboardText
  111. */
  112. static int clipboard_testSetClipboardData(void *arg)
  113. {
  114. int result = -1;
  115. result = SDL_SetClipboardData(NULL, 0, NULL, NULL);
  116. SDLTest_AssertPass("Call to SDL_SetClipboardData succeeded");
  117. SDLTest_AssertCheck(
  118. result == 0,
  119. "Validate SDL_SetClipboardData result, expected 0, got %i",
  120. result);
  121. SDL_GetClipboardUserdata();
  122. SDLTest_AssertPass("Call to SDL_GetClipboardUserdata succeeded");
  123. return TEST_COMPLETED;
  124. }
  125. /**
  126. * \brief Check call to SDL_SetPrimarySelectionText
  127. * \sa SDL_SetPrimarySelectionText
  128. */
  129. static int clipboard_testSetPrimarySelectionText(void *arg)
  130. {
  131. char *textRef = SDLTest_RandomAsciiString();
  132. char *text = SDL_strdup(textRef);
  133. int result;
  134. result = SDL_SetPrimarySelectionText((const char *)text);
  135. SDLTest_AssertPass("Call to SDL_SetPrimarySelectionText succeeded");
  136. SDLTest_AssertCheck(
  137. result == 0,
  138. "Validate SDL_SetPrimarySelectionText result, expected 0, got %i",
  139. result);
  140. SDLTest_AssertCheck(
  141. SDL_strcmp(textRef, text) == 0,
  142. "Verify SDL_SetPrimarySelectionText did not modify input string, expected '%s', got '%s'",
  143. textRef, text);
  144. /* Cleanup */
  145. SDL_free(textRef);
  146. SDL_free(text);
  147. return TEST_COMPLETED;
  148. }
  149. /**
  150. * \brief End-to-end test of SDL_xyzClipboardText functions
  151. * \sa SDL_HasClipboardText
  152. * \sa SDL_GetClipboardText
  153. * \sa SDL_SetClipboardText
  154. */
  155. static int clipboard_testClipboardTextFunctions(void *arg)
  156. {
  157. char *textRef = SDLTest_RandomAsciiString();
  158. char *text = SDL_strdup(textRef);
  159. SDL_bool boolResult;
  160. int intResult;
  161. char *charResult;
  162. /* Clear clipboard text state */
  163. boolResult = SDL_HasClipboardText();
  164. SDLTest_AssertPass("Call to SDL_HasClipboardText succeeded");
  165. if (boolResult == SDL_TRUE) {
  166. intResult = SDL_SetClipboardText((const char *)NULL);
  167. SDLTest_AssertPass("Call to SDL_SetClipboardText(NULL) succeeded");
  168. SDLTest_AssertCheck(
  169. intResult == 0,
  170. "Verify result from SDL_SetClipboardText(NULL), expected 0, got %i",
  171. intResult);
  172. charResult = SDL_GetClipboardText();
  173. SDLTest_AssertPass("Call to SDL_GetClipboardText succeeded");
  174. SDL_free(charResult);
  175. boolResult = SDL_HasClipboardText();
  176. SDLTest_AssertPass("Call to SDL_HasClipboardText succeeded");
  177. SDLTest_AssertCheck(
  178. boolResult == SDL_FALSE,
  179. "Verify SDL_HasClipboardText returned SDL_FALSE, got %s",
  180. (boolResult) ? "SDL_TRUE" : "SDL_FALSE");
  181. }
  182. /* Empty clipboard */
  183. charResult = SDL_GetClipboardText();
  184. SDLTest_AssertPass("Call to SDL_GetClipboardText succeeded");
  185. SDLTest_Assert(
  186. charResult != NULL,
  187. "Verify SDL_GetClipboardText did not return NULL");
  188. SDLTest_AssertCheck(
  189. charResult[0] == '\0', /* NOLINT(clang-analyzer-core.NullDereference): Checked for NULL above */
  190. "Verify SDL_GetClipboardText returned string with length 0, got length %i",
  191. (int)SDL_strlen(charResult));
  192. intResult = SDL_SetClipboardText((const char *)text);
  193. SDLTest_AssertPass("Call to SDL_SetClipboardText succeeded");
  194. SDLTest_AssertCheck(
  195. intResult == 0,
  196. "Verify result from SDL_SetClipboardText(NULL), expected 0, got %i",
  197. intResult);
  198. SDLTest_AssertCheck(
  199. SDL_strcmp(textRef, text) == 0,
  200. "Verify SDL_SetClipboardText did not modify input string, expected '%s', got '%s'",
  201. textRef, text);
  202. boolResult = SDL_HasClipboardText();
  203. SDLTest_AssertPass("Call to SDL_HasClipboardText succeeded");
  204. SDLTest_AssertCheck(
  205. boolResult == SDL_TRUE,
  206. "Verify SDL_HasClipboardText returned SDL_TRUE, got %s",
  207. (boolResult) ? "SDL_TRUE" : "SDL_FALSE");
  208. SDL_free(charResult);
  209. charResult = SDL_GetClipboardText();
  210. SDLTest_AssertPass("Call to SDL_GetClipboardText succeeded");
  211. SDLTest_AssertCheck(
  212. SDL_strcmp(textRef, charResult) == 0,
  213. "Verify SDL_GetClipboardText returned correct string, expected '%s', got '%s'",
  214. textRef, charResult);
  215. /* Cleanup */
  216. SDL_free(textRef);
  217. SDL_free(text);
  218. SDL_free(charResult);
  219. return TEST_COMPLETED;
  220. }
  221. /**
  222. * \brief End-to-end test of SDL_xyzPrimarySelectionText functions
  223. * \sa SDL_HasPrimarySelectionText
  224. * \sa SDL_GetPrimarySelectionText
  225. * \sa SDL_SetPrimarySelectionText
  226. */
  227. static int clipboard_testPrimarySelectionTextFunctions(void *arg)
  228. {
  229. char *textRef = SDLTest_RandomAsciiString();
  230. char *text = SDL_strdup(textRef);
  231. SDL_bool boolResult;
  232. int intResult;
  233. char *charResult;
  234. /* Clear primary selection text state */
  235. boolResult = SDL_HasPrimarySelectionText();
  236. SDLTest_AssertPass("Call to SDL_HasPrimarySelectionText succeeded");
  237. if (boolResult == SDL_TRUE) {
  238. intResult = SDL_SetPrimarySelectionText((const char *)NULL);
  239. SDLTest_AssertPass("Call to SDL_SetPrimarySelectionText(NULL) succeeded");
  240. SDLTest_AssertCheck(
  241. intResult == 0,
  242. "Verify result from SDL_SetPrimarySelectionText(NULL), expected 0, got %i",
  243. intResult);
  244. charResult = SDL_GetPrimarySelectionText();
  245. SDLTest_AssertPass("Call to SDL_GetPrimarySelectionText succeeded");
  246. SDL_free(charResult);
  247. boolResult = SDL_HasPrimarySelectionText();
  248. SDLTest_AssertPass("Call to SDL_HasPrimarySelectionText succeeded");
  249. SDLTest_AssertCheck(
  250. boolResult == SDL_FALSE,
  251. "Verify SDL_HasPrimarySelectionText returned SDL_FALSE, got %s",
  252. (boolResult) ? "SDL_TRUE" : "SDL_FALSE");
  253. }
  254. /* Empty primary selection */
  255. charResult = SDL_GetPrimarySelectionText();
  256. SDLTest_AssertPass("Call to SDL_GetPrimarySelectionText succeeded");
  257. SDLTest_Assert(
  258. charResult != NULL,
  259. "Verify SDL_GetPrimarySelectionText did not return NULL");
  260. SDLTest_AssertCheck(
  261. charResult[0] == '\0', /* NOLINT(clang-analyzer-core.NullDereference): Checked for NULL above */
  262. "Verify SDL_GetPrimarySelectionText returned string with length 0, got length %i",
  263. (int)SDL_strlen(charResult));
  264. intResult = SDL_SetPrimarySelectionText((const char *)text);
  265. SDLTest_AssertPass("Call to SDL_SetPrimarySelectionText succeeded");
  266. SDLTest_AssertCheck(
  267. intResult == 0,
  268. "Verify result from SDL_SetPrimarySelectionText(NULL), expected 0, got %i",
  269. intResult);
  270. SDLTest_AssertCheck(
  271. SDL_strcmp(textRef, text) == 0,
  272. "Verify SDL_SetPrimarySelectionText did not modify input string, expected '%s', got '%s'",
  273. textRef, text);
  274. boolResult = SDL_HasPrimarySelectionText();
  275. SDLTest_AssertPass("Call to SDL_HasPrimarySelectionText succeeded");
  276. SDLTest_AssertCheck(
  277. boolResult == SDL_TRUE,
  278. "Verify SDL_HasPrimarySelectionText returned SDL_TRUE, got %s",
  279. (boolResult) ? "SDL_TRUE" : "SDL_FALSE");
  280. SDL_free(charResult);
  281. charResult = SDL_GetPrimarySelectionText();
  282. SDLTest_AssertPass("Call to SDL_GetPrimarySelectionText succeeded");
  283. SDLTest_AssertCheck(
  284. SDL_strcmp(textRef, charResult) == 0,
  285. "Verify SDL_GetPrimarySelectionText returned correct string, expected '%s', got '%s'",
  286. textRef, charResult);
  287. /* Cleanup */
  288. SDL_free(textRef);
  289. SDL_free(text);
  290. SDL_free(charResult);
  291. return TEST_COMPLETED;
  292. }
  293. /* ================= Test References ================== */
  294. /* Clipboard test cases */
  295. static const SDLTest_TestCaseReference clipboardTest1 = {
  296. (SDLTest_TestCaseFp)clipboard_testHasClipboardText, "clipboard_testHasClipboardText", "Check call to SDL_HasClipboardText", TEST_ENABLED
  297. };
  298. static const SDLTest_TestCaseReference clipboardTest2 = {
  299. (SDLTest_TestCaseFp)clipboard_testHasPrimarySelectionText, "clipboard_testHasPrimarySelectionText", "Check call to SDL_HasPrimarySelectionText", TEST_ENABLED
  300. };
  301. static const SDLTest_TestCaseReference clipboardTest3 = {
  302. (SDLTest_TestCaseFp)clipboard_testGetClipboardText, "clipboard_testGetClipboardText", "Check call to SDL_GetClipboardText", TEST_ENABLED
  303. };
  304. static const SDLTest_TestCaseReference clipboardTest4 = {
  305. (SDLTest_TestCaseFp)clipboard_testGetPrimarySelectionText, "clipboard_testGetPrimarySelectionText", "Check call to SDL_GetPrimarySelectionText", TEST_ENABLED
  306. };
  307. static const SDLTest_TestCaseReference clipboardTest5 = {
  308. (SDLTest_TestCaseFp)clipboard_testSetClipboardText, "clipboard_testSetClipboardText", "Check call to SDL_SetClipboardText", TEST_ENABLED
  309. };
  310. static const SDLTest_TestCaseReference clipboardTest6 = {
  311. (SDLTest_TestCaseFp)clipboard_testSetPrimarySelectionText, "clipboard_testSetPrimarySelectionText", "Check call to SDL_SetPrimarySelectionText", TEST_ENABLED
  312. };
  313. static const SDLTest_TestCaseReference clipboardTest7 = {
  314. (SDLTest_TestCaseFp)clipboard_testClipboardTextFunctions, "clipboard_testClipboardTextFunctions", "End-to-end test of SDL_xyzClipboardText functions", TEST_ENABLED
  315. };
  316. static const SDLTest_TestCaseReference clipboardTest8 = {
  317. (SDLTest_TestCaseFp)clipboard_testPrimarySelectionTextFunctions, "clipboard_testPrimarySelectionTextFunctions", "End-to-end test of SDL_xyzPrimarySelectionText functions", TEST_ENABLED
  318. };
  319. static const SDLTest_TestCaseReference clipboardTest9 = {
  320. (SDLTest_TestCaseFp)clipboard_testGetClipboardData, "clipboard_testGetClipboardData", "Check call to SDL_GetClipboardData", TEST_ENABLED
  321. };
  322. static const SDLTest_TestCaseReference clipboardTest10 = {
  323. (SDLTest_TestCaseFp)clipboard_testSetClipboardData, "clipboard_testSetClipboardData", "Check call to SDL_SetClipboardData", TEST_ENABLED
  324. };
  325. static const SDLTest_TestCaseReference clipboardTest11 = {
  326. (SDLTest_TestCaseFp)clipboard_testHasClipboardData, "clipboard_testHasClipboardData", "Check call to SDL_HasClipboardData", TEST_ENABLED
  327. };
  328. /* Sequence of Clipboard test cases */
  329. static const SDLTest_TestCaseReference *clipboardTests[] = {
  330. &clipboardTest1, &clipboardTest2, &clipboardTest3, &clipboardTest4, &clipboardTest5, &clipboardTest6, &clipboardTest7, &clipboardTest8, &clipboardTest9, &clipboardTest10, &clipboardTest11, NULL
  331. };
  332. /* Clipboard test suite (global) */
  333. SDLTest_TestSuiteReference clipboardTestSuite = {
  334. "Clipboard",
  335. NULL,
  336. clipboardTests,
  337. NULL
  338. };