testautomation_hints.c 8.7 KB

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