🛠️🐜 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.

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