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

199 lines
6.0 KiB

  1. /**
  2. * Events test suite
  3. */
  4. #include <stdio.h>
  5. #include "SDL.h"
  6. #include "SDL_test.h"
  7. /* ================= Test Case Implementation ================== */
  8. /* Test case functions */
  9. /* Flag indicating if the userdata should be checked */
  10. int _userdataCheck = 0;
  11. /* Userdata value to check */
  12. int _userdataValue = 0;
  13. /* Flag indicating that the filter was called */
  14. int _eventFilterCalled = 0;
  15. /* Userdata values for event */
  16. int _userdataValue1 = 1;
  17. int _userdataValue2 = 2;
  18. /* Event filter that sets some flags and optionally checks userdata */
  19. int SDLCALL _events_sampleNullEventFilter(void *userdata, SDL_Event *event)
  20. {
  21. _eventFilterCalled = 1;
  22. if (_userdataCheck != 0) {
  23. SDLTest_AssertCheck(userdata != NULL, "Check userdata pointer, expected: non-NULL, got: %s", (userdata != NULL) ? "non-NULL" : "NULL");
  24. if (userdata != NULL) {
  25. SDLTest_AssertCheck(*(int *)userdata == _userdataValue, "Check userdata value, expected: %i, got: %i", _userdataValue, *(int *)userdata);
  26. }
  27. }
  28. return 0;
  29. }
  30. /**
  31. * @brief Test pumping and peeking events.
  32. *
  33. * @sa http://wiki.libsdl.org/SDL_PumpEvents
  34. * @sa http://wiki.libsdl.org/SDL_PollEvent
  35. */
  36. int events_pushPumpAndPollUserevent(void *arg)
  37. {
  38. SDL_Event event1;
  39. SDL_Event event2;
  40. int result;
  41. /* Create user event */
  42. event1.type = SDL_USEREVENT;
  43. event1.user.code = SDLTest_RandomSint32();
  44. event1.user.data1 = (void *)&_userdataValue1;
  45. event1.user.data2 = (void *)&_userdataValue2;
  46. /* Push a user event onto the queue and force queue update */
  47. SDL_PushEvent(&event1);
  48. SDLTest_AssertPass("Call to SDL_PushEvent()");
  49. SDL_PumpEvents();
  50. SDLTest_AssertPass("Call to SDL_PumpEvents()");
  51. /* Poll for user event */
  52. result = SDL_PollEvent(&event2);
  53. SDLTest_AssertPass("Call to SDL_PollEvent()");
  54. SDLTest_AssertCheck(result == 1, "Check result from SDL_PollEvent, expected: 1, got: %d", result);
  55. return TEST_COMPLETED;
  56. }
  57. /**
  58. * @brief Adds and deletes an event watch function with NULL userdata
  59. *
  60. * @sa http://wiki.libsdl.org/SDL_AddEventWatch
  61. * @sa http://wiki.libsdl.org/SDL_DelEventWatch
  62. *
  63. */
  64. int events_addDelEventWatch(void *arg)
  65. {
  66. SDL_Event event;
  67. /* Create user event */
  68. event.type = SDL_USEREVENT;
  69. event.user.code = SDLTest_RandomSint32();
  70. event.user.data1 = (void *)&_userdataValue1;
  71. event.user.data2 = (void *)&_userdataValue2;
  72. /* Disable userdata check */
  73. _userdataCheck = 0;
  74. /* Reset event filter call tracker */
  75. _eventFilterCalled = 0;
  76. /* Add watch */
  77. SDL_AddEventWatch(_events_sampleNullEventFilter, NULL);
  78. SDLTest_AssertPass("Call to SDL_AddEventWatch()");
  79. /* Push a user event onto the queue and force queue update */
  80. SDL_PushEvent(&event);
  81. SDLTest_AssertPass("Call to SDL_PushEvent()");
  82. SDL_PumpEvents();
  83. SDLTest_AssertPass("Call to SDL_PumpEvents()");
  84. SDLTest_AssertCheck(_eventFilterCalled == 1, "Check that event filter was called");
  85. /* Delete watch */
  86. SDL_DelEventWatch(_events_sampleNullEventFilter, NULL);
  87. SDLTest_AssertPass("Call to SDL_DelEventWatch()");
  88. /* Push a user event onto the queue and force queue update */
  89. _eventFilterCalled = 0;
  90. SDL_PushEvent(&event);
  91. SDLTest_AssertPass("Call to SDL_PushEvent()");
  92. SDL_PumpEvents();
  93. SDLTest_AssertPass("Call to SDL_PumpEvents()");
  94. SDLTest_AssertCheck(_eventFilterCalled == 0, "Check that event filter was NOT called");
  95. return TEST_COMPLETED;
  96. }
  97. /**
  98. * @brief Adds and deletes an event watch function with userdata
  99. *
  100. * @sa http://wiki.libsdl.org/SDL_AddEventWatch
  101. * @sa http://wiki.libsdl.org/SDL_DelEventWatch
  102. *
  103. */
  104. int events_addDelEventWatchWithUserdata(void *arg)
  105. {
  106. SDL_Event event;
  107. /* Create user event */
  108. event.type = SDL_USEREVENT;
  109. event.user.code = SDLTest_RandomSint32();
  110. event.user.data1 = (void *)&_userdataValue1;
  111. event.user.data2 = (void *)&_userdataValue2;
  112. /* Enable userdata check and set a value to check */
  113. _userdataCheck = 1;
  114. _userdataValue = SDLTest_RandomIntegerInRange(-1024, 1024);
  115. /* Reset event filter call tracker */
  116. _eventFilterCalled = 0;
  117. /* Add watch */
  118. SDL_AddEventWatch(_events_sampleNullEventFilter, (void *)&_userdataValue);
  119. SDLTest_AssertPass("Call to SDL_AddEventWatch()");
  120. /* Push a user event onto the queue and force queue update */
  121. SDL_PushEvent(&event);
  122. SDLTest_AssertPass("Call to SDL_PushEvent()");
  123. SDL_PumpEvents();
  124. SDLTest_AssertPass("Call to SDL_PumpEvents()");
  125. SDLTest_AssertCheck(_eventFilterCalled == 1, "Check that event filter was called");
  126. /* Delete watch */
  127. SDL_DelEventWatch(_events_sampleNullEventFilter, (void *)&_userdataValue);
  128. SDLTest_AssertPass("Call to SDL_DelEventWatch()");
  129. /* Push a user event onto the queue and force queue update */
  130. _eventFilterCalled = 0;
  131. SDL_PushEvent(&event);
  132. SDLTest_AssertPass("Call to SDL_PushEvent()");
  133. SDL_PumpEvents();
  134. SDLTest_AssertPass("Call to SDL_PumpEvents()");
  135. SDLTest_AssertCheck(_eventFilterCalled == 0, "Check that event filter was NOT called");
  136. return TEST_COMPLETED;
  137. }
  138. /* ================= Test References ================== */
  139. /* Events test cases */
  140. static const SDLTest_TestCaseReference eventsTest1 = {
  141. (SDLTest_TestCaseFp)events_pushPumpAndPollUserevent, "events_pushPumpAndPollUserevent", "Pushes, pumps and polls a user event", TEST_ENABLED
  142. };
  143. static const SDLTest_TestCaseReference eventsTest2 = {
  144. (SDLTest_TestCaseFp)events_addDelEventWatch, "events_addDelEventWatch", "Adds and deletes an event watch function with NULL userdata", TEST_ENABLED
  145. };
  146. static const SDLTest_TestCaseReference eventsTest3 = {
  147. (SDLTest_TestCaseFp)events_addDelEventWatchWithUserdata, "events_addDelEventWatchWithUserdata", "Adds and deletes an event watch function with userdata", TEST_ENABLED
  148. };
  149. /* Sequence of Events test cases */
  150. static const SDLTest_TestCaseReference *eventsTests[] = {
  151. &eventsTest1, &eventsTest2, &eventsTest3, NULL
  152. };
  153. /* Events test suite (global) */
  154. SDLTest_TestSuiteReference eventsTestSuite = {
  155. "Events",
  156. NULL,
  157. eventsTests,
  158. NULL
  159. };