test/testautomation_events.c
changeset 6800 26c6795ec1a4
child 7191 75360622e65f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testautomation_events.c	Sun Jan 06 15:17:59 2013 -0800
     1.3 @@ -0,0 +1,201 @@
     1.4 +/**
     1.5 + * Events test suite
     1.6 + */
     1.7 +
     1.8 +#include <stdio.h>
     1.9 +
    1.10 +#include "SDL.h"
    1.11 +#include "SDL_test.h"
    1.12 +
    1.13 +/* ================= Test Case Implementation ================== */
    1.14 +
    1.15 +/* Test case functions */
    1.16 +
    1.17 +/* Flag indicating if the userdata should be checked */
    1.18 +int _userdataCheck = 0;
    1.19 +
    1.20 +/* Userdata value to check */
    1.21 +int _userdataValue = 0;
    1.22 +
    1.23 +/* Flag indicating that the filter was called */
    1.24 +int _eventFilterCalled = 0;
    1.25 +
    1.26 +/* Userdata values for event */
    1.27 +int _userdataValue1 = 1;
    1.28 +int _userdataValue2 = 2;
    1.29 +
    1.30 +/* Event filter that sets some flags and optionally checks userdata */
    1.31 +int _events_sampleNullEventFilter(void *userdata, SDL_Event *event)
    1.32 +{
    1.33 +   _eventFilterCalled = 1;
    1.34 +   
    1.35 +   if (_userdataCheck != 0) {
    1.36 +       SDLTest_AssertCheck(userdata != NULL, "Check userdata pointer, expected: non-NULL, got: %s", (userdata != NULL) ? "non-NULL" : "NULL");
    1.37 +       if (userdata != NULL) {
    1.38 +          SDLTest_AssertCheck(*(int *)userdata == _userdataValue, "Check userdata value, expected: %i, got: %i", _userdataValue, *(int *)userdata);
    1.39 +       }
    1.40 +   }
    1.41 +   
    1.42 +   return 0;
    1.43 +}
    1.44 +
    1.45 +/**
    1.46 + * @brief Test pumping and peeking events.
    1.47 + *
    1.48 + * @sa http://wiki.libsdl.org/moin.cgi/SDL_PumpEvents
    1.49 + * @sa http://wiki.libsdl.org/moin.cgi/SDL_PollEvent
    1.50 + */
    1.51 +int
    1.52 +events_pushPumpAndPollUserevent(void *arg)
    1.53 +{
    1.54 +   SDL_Event event1;
    1.55 +   SDL_Event event2;
    1.56 +   int result;
    1.57 +   
    1.58 +   /* Create user event */
    1.59 +   event1.type = SDL_USEREVENT;
    1.60 +   event1.user.code = SDLTest_RandomSint32();
    1.61 +   event1.user.data1 = (void *)&_userdataValue1;
    1.62 +   event1.user.data2 = (void *)&_userdataValue2;
    1.63 +   
    1.64 +   /* Push a user event onto the queue and force queue update*/
    1.65 +   SDL_PushEvent(&event1);
    1.66 +   SDLTest_AssertPass("Call to SDL_PushEvent()");
    1.67 +   SDL_PumpEvents();
    1.68 +   SDLTest_AssertPass("Call to SDL_PumpEvents()");
    1.69 +   
    1.70 +   /* Poll for user event */
    1.71 +   result = SDL_PollEvent(&event2);
    1.72 +   SDLTest_AssertPass("Call to SDL_PollEvent()");
    1.73 +   SDLTest_AssertCheck(result == 1, "Check result from SDL_PollEvent, expected: 1, got: %d", result);
    1.74 +   
    1.75 +   return TEST_COMPLETED;
    1.76 +}
    1.77 +
    1.78 +
    1.79 +/**
    1.80 + * @brief Adds and deletes an event watch function with NULL userdata
    1.81 + *
    1.82 + * @sa http://wiki.libsdl.org/moin.cgi/SDL_AddEventWatch
    1.83 + * @sa http://wiki.libsdl.org/moin.cgi/SDL_DelEventWatch
    1.84 + *
    1.85 + */
    1.86 +int
    1.87 +events_addDelEventWatch(void *arg)
    1.88 +{
    1.89 +   SDL_Event event;
    1.90 +   
    1.91 +   /* Create user event */
    1.92 +   event.type = SDL_USEREVENT;
    1.93 +   event.user.code = SDLTest_RandomSint32();;
    1.94 +   event.user.data1 = (void *)&_userdataValue1;
    1.95 +   event.user.data2 = (void *)&_userdataValue2;
    1.96 +
    1.97 +   /* Disable userdata check */
    1.98 +   _userdataCheck = 0;
    1.99 +
   1.100 +   /* Reset event filter call tracker */
   1.101 +   _eventFilterCalled = 0;
   1.102 +
   1.103 +   /* Add watch */      
   1.104 +   SDL_AddEventWatch(_events_sampleNullEventFilter, NULL);
   1.105 +   SDLTest_AssertPass("Call to SDL_AddEventWatch()");
   1.106 +   
   1.107 +   /* Push a user event onto the queue and force queue update*/
   1.108 +   SDL_PushEvent(&event);
   1.109 +   SDLTest_AssertPass("Call to SDL_PushEvent()");
   1.110 +   SDL_PumpEvents();
   1.111 +   SDLTest_AssertPass("Call to SDL_PumpEvents()");
   1.112 +   SDLTest_AssertCheck(_eventFilterCalled == 1, "Check that event filter was called");
   1.113 +
   1.114 +   /* Delete watch */
   1.115 +   SDL_DelEventWatch(_events_sampleNullEventFilter, NULL);
   1.116 +   SDLTest_AssertPass("Call to SDL_DelEventWatch()");
   1.117 +
   1.118 +   /* Push a user event onto the queue and force queue update*/
   1.119 +   _eventFilterCalled = 0;
   1.120 +   SDL_PushEvent(&event);
   1.121 +   SDLTest_AssertPass("Call to SDL_PushEvent()");
   1.122 +   SDL_PumpEvents();
   1.123 +   SDLTest_AssertPass("Call to SDL_PumpEvents()");
   1.124 +   SDLTest_AssertCheck(_eventFilterCalled == 0, "Check that event filter was NOT called");
   1.125 +
   1.126 +   return TEST_COMPLETED;
   1.127 +}
   1.128 +
   1.129 +/**
   1.130 + * @brief Adds and deletes an event watch function with userdata
   1.131 + *
   1.132 + * @sa http://wiki.libsdl.org/moin.cgi/SDL_AddEventWatch
   1.133 + * @sa http://wiki.libsdl.org/moin.cgi/SDL_DelEventWatch
   1.134 + *
   1.135 + */
   1.136 +int
   1.137 +events_addDelEventWatchWithUserdata(void *arg)
   1.138 +{
   1.139 +   SDL_Event event;
   1.140 +   
   1.141 +   /* Create user event */
   1.142 +   event.type = SDL_USEREVENT;
   1.143 +   event.user.code = SDLTest_RandomSint32();;
   1.144 +   event.user.data1 = (void *)&_userdataValue1;
   1.145 +   event.user.data2 = (void *)&_userdataValue2;
   1.146 +   
   1.147 +   /* Enable userdata check and set a value to check */
   1.148 +   _userdataCheck = 1;
   1.149 +   _userdataValue = SDLTest_RandomIntegerInRange(-1024, 1024);
   1.150 +
   1.151 +   /* Reset event filter call tracker */
   1.152 +   _eventFilterCalled = 0;
   1.153 +
   1.154 +   /* Add watch */    
   1.155 +   SDL_AddEventWatch(_events_sampleNullEventFilter, (void *)&_userdataValue);
   1.156 +   SDLTest_AssertPass("Call to SDL_AddEventWatch()");
   1.157 +
   1.158 +   /* Push a user event onto the queue and force queue update*/
   1.159 +   SDL_PushEvent(&event);
   1.160 +   SDLTest_AssertPass("Call to SDL_PushEvent()");
   1.161 +   SDL_PumpEvents();
   1.162 +   SDLTest_AssertPass("Call to SDL_PumpEvents()");
   1.163 +   SDLTest_AssertCheck(_eventFilterCalled == 1, "Check that event filter was called");
   1.164 +
   1.165 +   /* Delete watch */
   1.166 +   SDL_DelEventWatch(_events_sampleNullEventFilter, (void *)&_userdataValue);
   1.167 +   SDLTest_AssertPass("Call to SDL_DelEventWatch()");
   1.168 +     
   1.169 +   /* Push a user event onto the queue and force queue update*/
   1.170 +   _eventFilterCalled = 0;
   1.171 +   SDL_PushEvent(&event);
   1.172 +   SDLTest_AssertPass("Call to SDL_PushEvent()");
   1.173 +   SDL_PumpEvents();
   1.174 +   SDLTest_AssertPass("Call to SDL_PumpEvents()");
   1.175 +   SDLTest_AssertCheck(_eventFilterCalled == 0, "Check that event filter was NOT called");
   1.176 +   
   1.177 +   return TEST_COMPLETED;
   1.178 +}
   1.179 +
   1.180 +
   1.181 +/* ================= Test References ================== */
   1.182 +
   1.183 +/* Events test cases */
   1.184 +static const SDLTest_TestCaseReference eventsTest1 =
   1.185 +		{ (SDLTest_TestCaseFp)events_pushPumpAndPollUserevent, "events_pushPumpAndPollUserevent", "Pushes, pumps and polls a user event", TEST_ENABLED };
   1.186 +
   1.187 +static const SDLTest_TestCaseReference eventsTest2 =
   1.188 +		{ (SDLTest_TestCaseFp)events_addDelEventWatch, "events_addDelEventWatch", "Adds and deletes an event watch function with NULL userdata", TEST_ENABLED };
   1.189 +
   1.190 +static const SDLTest_TestCaseReference eventsTest3 =
   1.191 +		{ (SDLTest_TestCaseFp)events_addDelEventWatchWithUserdata, "events_addDelEventWatchWithUserdata", "Adds and deletes an event watch function with userdata", TEST_ENABLED };
   1.192 +
   1.193 +/* Sequence of Events test cases */
   1.194 +static const SDLTest_TestCaseReference *eventsTests[] =  {
   1.195 +	&eventsTest1, &eventsTest2, &eventsTest3, NULL
   1.196 +};
   1.197 +
   1.198 +/* Events test suite (global) */
   1.199 +SDLTest_TestSuiteReference eventsTestSuite = {
   1.200 +	"Events",
   1.201 +	NULL,
   1.202 +	eventsTests,
   1.203 +	NULL
   1.204 +};