testautomation_events.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 _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/moin.cgi/SDL_PumpEvents
  34. * @sa http://wiki.libsdl.org/moin.cgi/SDL_PollEvent
  35. */
  36. int
  37. events_pushPumpAndPollUserevent(void *arg)
  38. {
  39. SDL_Event event1;
  40. SDL_Event event2;
  41. int result;
  42. /* Create user event */
  43. event1.type = SDL_USEREVENT;
  44. event1.user.code = SDLTest_RandomSint32();
  45. event1.user.data1 = (void *)&_userdataValue1;
  46. event1.user.data2 = (void *)&_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. return TEST_COMPLETED;
  57. }
  58. /**
  59. * @brief Adds and deletes an event watch function with NULL userdata
  60. *
  61. * @sa http://wiki.libsdl.org/moin.cgi/SDL_AddEventWatch
  62. * @sa http://wiki.libsdl.org/moin.cgi/SDL_DelEventWatch
  63. *
  64. */
  65. int
  66. events_addDelEventWatch(void *arg)
  67. {
  68. SDL_Event event;
  69. /* Create user event */
  70. event.type = SDL_USEREVENT;
  71. event.user.code = SDLTest_RandomSint32();;
  72. event.user.data1 = (void *)&_userdataValue1;
  73. event.user.data2 = (void *)&_userdataValue2;
  74. /* Disable userdata check */
  75. _userdataCheck = 0;
  76. /* Reset event filter call tracker */
  77. _eventFilterCalled = 0;
  78. /* Add watch */
  79. SDL_AddEventWatch(_events_sampleNullEventFilter, NULL);
  80. SDLTest_AssertPass("Call to SDL_AddEventWatch()");
  81. /* Push a user event onto the queue and force queue update */
  82. SDL_PushEvent(&event);
  83. SDLTest_AssertPass("Call to SDL_PushEvent()");
  84. SDL_PumpEvents();
  85. SDLTest_AssertPass("Call to SDL_PumpEvents()");
  86. SDLTest_AssertCheck(_eventFilterCalled == 1, "Check that event filter was called");
  87. /* Delete watch */
  88. SDL_DelEventWatch(_events_sampleNullEventFilter, NULL);
  89. SDLTest_AssertPass("Call to SDL_DelEventWatch()");
  90. /* Push a user event onto the queue and force queue update */
  91. _eventFilterCalled = 0;
  92. SDL_PushEvent(&event);
  93. SDLTest_AssertPass("Call to SDL_PushEvent()");
  94. SDL_PumpEvents();
  95. SDLTest_AssertPass("Call to SDL_PumpEvents()");
  96. SDLTest_AssertCheck(_eventFilterCalled == 0, "Check that event filter was NOT called");
  97. return TEST_COMPLETED;
  98. }
  99. /**
  100. * @brief Adds and deletes an event watch function with userdata
  101. *
  102. * @sa http://wiki.libsdl.org/moin.cgi/SDL_AddEventWatch
  103. * @sa http://wiki.libsdl.org/moin.cgi/SDL_DelEventWatch
  104. *
  105. */
  106. int
  107. 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. static const SDLTest_TestCaseReference eventsTest2 =
  146. { (SDLTest_TestCaseFp)events_addDelEventWatch, "events_addDelEventWatch", "Adds and deletes an event watch function with NULL userdata", TEST_ENABLED };
  147. static const SDLTest_TestCaseReference eventsTest3 =
  148. { (SDLTest_TestCaseFp)events_addDelEventWatchWithUserdata, "events_addDelEventWatchWithUserdata", "Adds and deletes an event watch function with userdata", TEST_ENABLED };
  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. };