Debian Codegraph @

test/testautomation_events.c

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