testautomation_hints.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /**
  2. * Hints test suite
  3. */
  4. #include <SDL3/SDL.h>
  5. #include <SDL3/SDL_test.h>
  6. #include "testautomation_suites.h"
  7. static const char *HintsEnum[] = {
  8. SDL_HINT_FRAMEBUFFER_ACCELERATION,
  9. SDL_HINT_GAMECONTROLLERCONFIG,
  10. SDL_HINT_GRAB_KEYBOARD,
  11. SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS,
  12. SDL_HINT_MAC_CTRL_CLICK_EMULATE_RIGHT_CLICK,
  13. SDL_HINT_MOUSE_RELATIVE_MODE_WARP,
  14. SDL_HINT_ORIENTATIONS,
  15. SDL_HINT_RENDER_DIRECT3D_THREADSAFE,
  16. SDL_HINT_RENDER_DRIVER,
  17. SDL_HINT_RENDER_VSYNC,
  18. SDL_HINT_TIMER_RESOLUTION,
  19. SDL_HINT_VIDEO_ALLOW_SCREENSAVER,
  20. SDL_HINT_VIDEO_MAC_FULLSCREEN_SPACES,
  21. SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS,
  22. SDL_HINT_VIDEO_WIN_D3DCOMPILER,
  23. SDL_HINT_VIDEO_X11_XRANDR,
  24. SDL_HINT_XINPUT_ENABLED,
  25. };
  26. static const char *HintsVerbose[] = {
  27. "SDL_FRAMEBUFFER_ACCELERATION",
  28. "SDL_GAMECONTROLLERCONFIG",
  29. "SDL_GRAB_KEYBOARD",
  30. "SDL_JOYSTICK_ALLOW_BACKGROUND_EVENTS",
  31. "SDL_MAC_CTRL_CLICK_EMULATE_RIGHT_CLICK",
  32. "SDL_MOUSE_RELATIVE_MODE_WARP",
  33. "SDL_ORIENTATIONS",
  34. "SDL_RENDER_DIRECT3D_THREADSAFE",
  35. "SDL_RENDER_DRIVER",
  36. "SDL_RENDER_VSYNC",
  37. "SDL_TIMER_RESOLUTION",
  38. "SDL_VIDEO_ALLOW_SCREENSAVER",
  39. "SDL_VIDEO_MAC_FULLSCREEN_SPACES",
  40. "SDL_VIDEO_MINIMIZE_ON_FOCUS_LOSS",
  41. "SDL_VIDEO_WIN_D3DCOMPILER",
  42. "SDL_VIDEO_X11_XRANDR",
  43. "SDL_XINPUT_ENABLED"
  44. };
  45. SDL_COMPILE_TIME_ASSERT(HintsEnum, SDL_arraysize(HintsEnum) == SDL_arraysize(HintsVerbose));
  46. static const int numHintsEnum = SDL_arraysize(HintsEnum);
  47. /* Test case functions */
  48. /**
  49. * Call to SDL_GetHint
  50. */
  51. static int hints_getHint(void *arg)
  52. {
  53. const char *result1;
  54. const char *result2;
  55. int i;
  56. for (i = 0; i < numHintsEnum; i++) {
  57. result1 = SDL_GetHint(HintsEnum[i]);
  58. SDLTest_AssertPass("Call to SDL_GetHint(%s) - using define definition", (char *)HintsEnum[i]);
  59. result2 = SDL_GetHint(HintsVerbose[i]);
  60. SDLTest_AssertPass("Call to SDL_GetHint(%s) - using string definition", (char *)HintsVerbose[i]);
  61. SDLTest_AssertCheck(
  62. (result1 == NULL && result2 == NULL) || (SDL_strcmp(result1, result2) == 0),
  63. "Verify returned values are equal; got: result1='%s' result2='%s",
  64. (result1 == NULL) ? "null" : result1,
  65. (result2 == NULL) ? "null" : result2);
  66. }
  67. return TEST_COMPLETED;
  68. }
  69. static void SDLCALL hints_testHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  70. {
  71. *(char **)userdata = hint ? SDL_strdup(hint) : NULL;
  72. }
  73. /**
  74. * Call to SDL_SetHint
  75. */
  76. static int hints_setHint(void *arg)
  77. {
  78. const char *testHint = "SDL_AUTOMATED_TEST_HINT";
  79. const char *originalValue;
  80. char *value;
  81. const char *testValue;
  82. char *callbackValue;
  83. SDL_bool result;
  84. int i, j;
  85. /* Create random values to set */
  86. value = SDLTest_RandomAsciiStringOfSize(10);
  87. for (i = 0; i < numHintsEnum; i++) {
  88. /* Capture current value */
  89. originalValue = SDL_GetHint(HintsEnum[i]);
  90. SDLTest_AssertPass("Call to SDL_GetHint(%s)", HintsEnum[i]);
  91. /* Copy the original value, since it will be freed when we set it again */
  92. originalValue = originalValue ? SDL_strdup(originalValue) : NULL;
  93. /* Set value (twice) */
  94. for (j = 1; j <= 2; j++) {
  95. result = SDL_SetHint(HintsEnum[i], value);
  96. SDLTest_AssertPass("Call to SDL_SetHint(%s, %s) (iteration %i)", HintsEnum[i], value, j);
  97. SDLTest_AssertCheck(
  98. result == SDL_TRUE || result == SDL_FALSE,
  99. "Verify valid result was returned, got: %i",
  100. (int)result);
  101. testValue = SDL_GetHint(HintsEnum[i]);
  102. SDLTest_AssertPass("Call to SDL_GetHint(%s) - using string definition", HintsVerbose[i]);
  103. SDLTest_AssertCheck(
  104. (SDL_strcmp(value, testValue) == 0),
  105. "Verify returned value equals set value; got: testValue='%s' value='%s",
  106. (testValue == NULL) ? "null" : testValue,
  107. value);
  108. }
  109. /* Reset original value */
  110. result = SDL_SetHint(HintsEnum[i], originalValue);
  111. SDLTest_AssertPass("Call to SDL_SetHint(%s, originalValue)", HintsEnum[i]);
  112. SDLTest_AssertCheck(
  113. result == SDL_TRUE || result == SDL_FALSE,
  114. "Verify valid result was returned, got: %i",
  115. (int)result);
  116. SDL_free((void *)originalValue);
  117. }
  118. SDL_free(value);
  119. /* Set default value in environment */
  120. SDL_setenv(testHint, "original", 1);
  121. SDLTest_AssertPass("Call to SDL_GetHint() after saving and restoring hint");
  122. originalValue = SDL_GetHint(testHint);
  123. value = (originalValue == NULL) ? NULL : SDL_strdup(originalValue);
  124. SDL_SetHint(testHint, "temp");
  125. SDL_SetHint(testHint, value);
  126. SDL_free(value);
  127. testValue = SDL_GetHint(testHint);
  128. SDLTest_AssertCheck(
  129. testValue && SDL_strcmp(testValue, "original") == 0,
  130. "testValue = %s, expected \"original\"",
  131. testValue);
  132. SDLTest_AssertPass("Call to SDL_SetHintWithPriority(NULL, SDL_HINT_DEFAULT)");
  133. SDL_SetHintWithPriority(testHint, NULL, SDL_HINT_DEFAULT);
  134. testValue = SDL_GetHint(testHint);
  135. SDLTest_AssertCheck(
  136. testValue && SDL_strcmp(testValue, "original") == 0,
  137. "testValue = %s, expected \"original\"",
  138. testValue);
  139. SDLTest_AssertPass("Call to SDL_SetHintWithPriority(\"temp\", SDL_HINT_OVERRIDE)");
  140. SDL_SetHintWithPriority(testHint, "temp", SDL_HINT_OVERRIDE);
  141. testValue = SDL_GetHint(testHint);
  142. SDLTest_AssertCheck(
  143. testValue && SDL_strcmp(testValue, "temp") == 0,
  144. "testValue = %s, expected \"temp\"",
  145. testValue);
  146. SDLTest_AssertPass("Call to SDL_SetHintWithPriority(NULL, SDL_HINT_OVERRIDE)");
  147. SDL_SetHintWithPriority(testHint, NULL, SDL_HINT_OVERRIDE);
  148. testValue = SDL_GetHint(testHint);
  149. SDLTest_AssertCheck(
  150. testValue == NULL,
  151. "testValue = %s, expected NULL",
  152. testValue);
  153. SDLTest_AssertPass("Call to SDL_ResetHint()");
  154. SDL_ResetHint(testHint);
  155. testValue = SDL_GetHint(testHint);
  156. SDLTest_AssertCheck(
  157. testValue && SDL_strcmp(testValue, "original") == 0,
  158. "testValue = %s, expected \"original\"",
  159. testValue);
  160. /* Make sure callback functionality works past a reset */
  161. SDLTest_AssertPass("Call to SDL_AddHintCallback()");
  162. callbackValue = NULL;
  163. SDL_AddHintCallback(testHint, hints_testHintChanged, &callbackValue);
  164. SDLTest_AssertCheck(
  165. callbackValue && SDL_strcmp(callbackValue, "original") == 0,
  166. "callbackValue = %s, expected \"original\"",
  167. callbackValue);
  168. SDL_free(callbackValue);
  169. SDLTest_AssertPass("Call to SDL_SetHintWithPriority(\"temp\", SDL_HINT_OVERRIDE), using callback");
  170. callbackValue = NULL;
  171. SDL_SetHintWithPriority(testHint, "temp", SDL_HINT_OVERRIDE);
  172. SDLTest_AssertCheck(
  173. callbackValue && SDL_strcmp(callbackValue, "temp") == 0,
  174. "callbackValue = %s, expected \"temp\"",
  175. callbackValue);
  176. SDL_free(callbackValue);
  177. SDLTest_AssertPass("Call to SDL_ResetHint(), using callback");
  178. callbackValue = NULL;
  179. SDL_ResetHint(testHint);
  180. SDLTest_AssertCheck(
  181. callbackValue && SDL_strcmp(callbackValue, "original") == 0,
  182. "callbackValue = %s, expected \"original\"",
  183. callbackValue);
  184. SDL_free(callbackValue);
  185. SDLTest_AssertPass("Call to SDL_SetHintWithPriority(\"temp\", SDL_HINT_OVERRIDE), using callback after reset");
  186. callbackValue = NULL;
  187. SDL_SetHintWithPriority(testHint, "temp", SDL_HINT_OVERRIDE);
  188. SDLTest_AssertCheck(
  189. callbackValue && SDL_strcmp(callbackValue, "temp") == 0,
  190. "callbackValue = %s, expected \"temp\"",
  191. callbackValue);
  192. SDL_free(callbackValue);
  193. SDLTest_AssertPass("Call to SDL_ResetHint(), after clearing callback");
  194. callbackValue = NULL;
  195. SDL_DelHintCallback(testHint, hints_testHintChanged, &callbackValue);
  196. SDL_ResetHint(testHint);
  197. SDLTest_AssertCheck(
  198. callbackValue == NULL,
  199. "callbackValue = %s, expected \"(null)\"",
  200. callbackValue);
  201. return TEST_COMPLETED;
  202. }
  203. /* ================= Test References ================== */
  204. /* Hints test cases */
  205. static const SDLTest_TestCaseReference hintsTest1 = {
  206. (SDLTest_TestCaseFp)hints_getHint, "hints_getHint", "Call to SDL_GetHint", TEST_ENABLED
  207. };
  208. static const SDLTest_TestCaseReference hintsTest2 = {
  209. (SDLTest_TestCaseFp)hints_setHint, "hints_setHint", "Call to SDL_SetHint", TEST_ENABLED
  210. };
  211. /* Sequence of Hints test cases */
  212. static const SDLTest_TestCaseReference *hintsTests[] = {
  213. &hintsTest1, &hintsTest2, NULL
  214. };
  215. /* Hints test suite (global) */
  216. SDLTest_TestSuiteReference hintsTestSuite = {
  217. "Hints",
  218. NULL,
  219. hintsTests,
  220. NULL
  221. };