testautomation_events.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. /* Need to finish getting all events and sentinel, otherwise other tests that rely on event are in bad state */
  56. while (SDL_PollEvent(&event2)) {
  57. }
  58. return TEST_COMPLETED;
  59. }
  60. /**
  61. * @brief Adds and deletes an event watch function with NULL userdata
  62. *
  63. * @sa http://wiki.libsdl.org/SDL_AddEventWatch
  64. * @sa http://wiki.libsdl.org/SDL_DelEventWatch
  65. *
  66. */
  67. int events_addDelEventWatch(void *arg)
  68. {
  69. SDL_Event event;
  70. /* Create user event */
  71. event.type = SDL_USEREVENT;
  72. event.user.code = SDLTest_RandomSint32();
  73. event.user.data1 = (void *)&_userdataValue1;
  74. event.user.data2 = (void *)&_userdataValue2;
  75. /* Disable userdata check */
  76. _userdataCheck = 0;
  77. /* Reset event filter call tracker */
  78. _eventFilterCalled = 0;
  79. /* Add watch */
  80. SDL_AddEventWatch(_events_sampleNullEventFilter, NULL);
  81. SDLTest_AssertPass("Call to SDL_AddEventWatch()");
  82. /* Push a user event onto the queue and force queue update */
  83. SDL_PushEvent(&event);
  84. SDLTest_AssertPass("Call to SDL_PushEvent()");
  85. SDL_PumpEvents();
  86. SDLTest_AssertPass("Call to SDL_PumpEvents()");
  87. SDLTest_AssertCheck(_eventFilterCalled == 1, "Check that event filter was called");
  88. /* Delete watch */
  89. SDL_DelEventWatch(_events_sampleNullEventFilter, NULL);
  90. SDLTest_AssertPass("Call to SDL_DelEventWatch()");
  91. /* Push a user event onto the queue and force queue update */
  92. _eventFilterCalled = 0;
  93. SDL_PushEvent(&event);
  94. SDLTest_AssertPass("Call to SDL_PushEvent()");
  95. SDL_PumpEvents();
  96. SDLTest_AssertPass("Call to SDL_PumpEvents()");
  97. SDLTest_AssertCheck(_eventFilterCalled == 0, "Check that event filter was NOT called");
  98. return TEST_COMPLETED;
  99. }
  100. /**
  101. * @brief Adds and deletes an event watch function with userdata
  102. *
  103. * @sa http://wiki.libsdl.org/SDL_AddEventWatch
  104. * @sa http://wiki.libsdl.org/SDL_DelEventWatch
  105. *
  106. */
  107. int events_addDelEventWatchWithUserdata(void *arg)
  108. {
  109. SDL_Event event;
  110. /* Create user event */
  111. event.type = SDL_USEREVENT;
  112. event.user.code = SDLTest_RandomSint32();
  113. event.user.data1 = (void *)&_userdataValue1;
  114. event.user.data2 = (void *)&_userdataValue2;
  115. /* Enable userdata check and set a value to check */
  116. _userdataCheck = 1;
  117. _userdataValue = SDLTest_RandomIntegerInRange(-1024, 1024);
  118. /* Reset event filter call tracker */
  119. _eventFilterCalled = 0;
  120. /* Add watch */
  121. SDL_AddEventWatch(_events_sampleNullEventFilter, (void *)&_userdataValue);
  122. SDLTest_AssertPass("Call to SDL_AddEventWatch()");
  123. /* Push a user event onto the queue and force queue update */
  124. SDL_PushEvent(&event);
  125. SDLTest_AssertPass("Call to SDL_PushEvent()");
  126. SDL_PumpEvents();
  127. SDLTest_AssertPass("Call to SDL_PumpEvents()");
  128. SDLTest_AssertCheck(_eventFilterCalled == 1, "Check that event filter was called");
  129. /* Delete watch */
  130. SDL_DelEventWatch(_events_sampleNullEventFilter, (void *)&_userdataValue);
  131. SDLTest_AssertPass("Call to SDL_DelEventWatch()");
  132. /* Push a user event onto the queue and force queue update */
  133. _eventFilterCalled = 0;
  134. SDL_PushEvent(&event);
  135. SDLTest_AssertPass("Call to SDL_PushEvent()");
  136. SDL_PumpEvents();
  137. SDLTest_AssertPass("Call to SDL_PumpEvents()");
  138. SDLTest_AssertCheck(_eventFilterCalled == 0, "Check that event filter was NOT called");
  139. return TEST_COMPLETED;
  140. }
  141. /* ================= Test References ================== */
  142. /* Events test cases */
  143. static const SDLTest_TestCaseReference eventsTest1 = {
  144. (SDLTest_TestCaseFp)events_pushPumpAndPollUserevent, "events_pushPumpAndPollUserevent", "Pushes, pumps and polls a user event", TEST_ENABLED
  145. };
  146. static const SDLTest_TestCaseReference eventsTest2 = {
  147. (SDLTest_TestCaseFp)events_addDelEventWatch, "events_addDelEventWatch", "Adds and deletes an event watch function with NULL userdata", TEST_ENABLED
  148. };
  149. static const SDLTest_TestCaseReference eventsTest3 = {
  150. (SDLTest_TestCaseFp)events_addDelEventWatchWithUserdata, "events_addDelEventWatchWithUserdata", "Adds and deletes an event watch function with userdata", TEST_ENABLED
  151. };
  152. /* Sequence of Events test cases */
  153. static const SDLTest_TestCaseReference *eventsTests[] = {
  154. &eventsTest1, &eventsTest2, &eventsTest3, NULL
  155. };
  156. /* Events test suite (global) */
  157. SDLTest_TestSuiteReference eventsTestSuite = {
  158. "Events",
  159. NULL,
  160. eventsTests,
  161. NULL
  162. };