testautomation_events.c 6.2 KB

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