testautomation_events.c 6.1 KB

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