🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

266 lines
9.0 KiB

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