testautomation_hints.c 8.5 KB

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