testautomation_hints.c 8.5 KB

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