2015-06-21 23:33:46 +08:00
/**
* Events test suite
*/
2022-11-27 12:43:38 +08:00
# include <SDL3/SDL.h>
# include <SDL3/SDL_test.h>
2023-03-08 23:12:45 +08:00
# include "testautomation_suites.h"
2015-06-21 23:33:46 +08:00
/* ================= Test Case Implementation ================== */
/* Test case functions */
/* Flag indicating if the userdata should be checked */
2022-12-30 05:58:16 +08:00
static int g_userdataCheck = 0 ;
2015-06-21 23:33:46 +08:00
/* Userdata value to check */
2022-12-30 05:58:16 +08:00
static int g_userdataValue = 0 ;
2015-06-21 23:33:46 +08:00
/* Flag indicating that the filter was called */
2022-12-30 05:58:16 +08:00
static int g_eventFilterCalled = 0 ;
2015-06-21 23:33:46 +08:00
/* Userdata values for event */
2022-12-30 05:58:16 +08:00
static int g_userdataValue1 = 1 ;
static int g_userdataValue2 = 2 ;
2015-06-21 23:33:46 +08:00
/* Event filter that sets some flags and optionally checks userdata */
2022-12-30 05:58:16 +08:00
static int SDLCALL events_sampleNullEventFilter ( void * userdata , SDL_Event * event )
2015-06-21 23:33:46 +08:00
{
2022-12-30 05:58:16 +08:00
g_eventFilterCalled = 1 ;
2015-06-21 23:33:46 +08:00
2022-12-30 05:58:16 +08:00
if ( g_userdataCheck ! = 0 ) {
2022-12-01 04:51:59 +08:00
SDLTest_AssertCheck ( userdata ! = NULL , " Check userdata pointer, expected: non-NULL, got: %s " , ( userdata ! = NULL ) ? " non-NULL " : " NULL " ) ;
if ( userdata ! = NULL ) {
2022-12-30 05:58:16 +08:00
SDLTest_AssertCheck ( * ( int * ) userdata = = g_userdataValue , " Check userdata value, expected: %i, got: %i " , g_userdataValue , * ( int * ) userdata ) ;
2022-12-01 04:51:59 +08:00
}
}
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
return 0 ;
2015-06-21 23:33:46 +08:00
}
/**
2023-11-06 23:26:06 +08:00
* Test pumping and peeking events .
2015-06-21 23:33:46 +08:00
*
2023-02-02 07:21:53 +08:00
* \ sa SDL_PumpEvents
* \ sa SDL_PollEvent
2015-06-21 23:33:46 +08:00
*/
2023-03-08 23:12:45 +08:00
static int events_pushPumpAndPollUserevent ( void * arg )
2015-06-21 23:33:46 +08:00
{
2022-12-01 04:51:59 +08:00
SDL_Event event1 ;
SDL_Event event2 ;
int result ;
/* Create user event */
2023-01-24 09:54:09 +08:00
event1 . type = SDL_EVENT_USER ;
2023-06-14 05:00:00 +08:00
event1 . common . timestamp = 0 ;
2022-12-01 04:51:59 +08:00
event1 . user . code = SDLTest_RandomSint32 ( ) ;
2022-12-30 05:58:16 +08:00
event1 . user . data1 = ( void * ) & g_userdataValue1 ;
event1 . user . data2 = ( void * ) & g_userdataValue2 ;
2022-12-01 04:51:59 +08:00
/* Push a user event onto the queue and force queue update */
SDL_PushEvent ( & event1 ) ;
SDLTest_AssertPass ( " Call to SDL_PushEvent() " ) ;
SDL_PumpEvents ( ) ;
SDLTest_AssertPass ( " Call to SDL_PumpEvents() " ) ;
/* Poll for user event */
result = SDL_PollEvent ( & event2 ) ;
SDLTest_AssertPass ( " Call to SDL_PollEvent() " ) ;
SDLTest_AssertCheck ( result = = 1 , " Check result from SDL_PollEvent, expected: 1, got: %d " , result ) ;
2023-03-21 19:11:57 +08:00
/* Need to finish getting all events and sentinel, otherwise other tests that rely on event are in bad state */
while ( SDL_PollEvent ( & event2 ) ) {
}
2022-12-01 04:51:59 +08:00
return TEST_COMPLETED ;
2015-06-21 23:33:46 +08:00
}
/**
2023-11-06 23:26:06 +08:00
* Adds and deletes an event watch function with NULL userdata
2015-06-21 23:33:46 +08:00
*
2023-02-02 07:21:53 +08:00
* \ sa SDL_AddEventWatch
* \ sa SDL_DelEventWatch
2015-06-21 23:33:46 +08:00
*
*/
2023-03-08 23:12:45 +08:00
static int events_addDelEventWatch ( void * arg )
2015-06-21 23:33:46 +08:00
{
2022-12-01 04:51:59 +08:00
SDL_Event event ;
/* Create user event */
2023-01-24 09:54:09 +08:00
event . type = SDL_EVENT_USER ;
2023-06-14 05:00:00 +08:00
event . common . timestamp = 0 ;
2022-12-01 04:51:59 +08:00
event . user . code = SDLTest_RandomSint32 ( ) ;
2022-12-30 05:58:16 +08:00
event . user . data1 = ( void * ) & g_userdataValue1 ;
event . user . data2 = ( void * ) & g_userdataValue2 ;
2022-12-01 04:51:59 +08:00
/* Disable userdata check */
2022-12-30 05:58:16 +08:00
g_userdataCheck = 0 ;
2022-12-01 04:51:59 +08:00
/* Reset event filter call tracker */
2022-12-30 05:58:16 +08:00
g_eventFilterCalled = 0 ;
2022-12-01 04:51:59 +08:00
/* Add watch */
2022-12-30 05:58:16 +08:00
SDL_AddEventWatch ( events_sampleNullEventFilter , NULL ) ;
2022-12-01 04:51:59 +08:00
SDLTest_AssertPass ( " Call to SDL_AddEventWatch() " ) ;
/* Push a user event onto the queue and force queue update */
SDL_PushEvent ( & event ) ;
SDLTest_AssertPass ( " Call to SDL_PushEvent() " ) ;
SDL_PumpEvents ( ) ;
SDLTest_AssertPass ( " Call to SDL_PumpEvents() " ) ;
2022-12-30 05:58:16 +08:00
SDLTest_AssertCheck ( g_eventFilterCalled = = 1 , " Check that event filter was called " ) ;
2022-12-01 04:51:59 +08:00
/* Delete watch */
2022-12-30 05:58:16 +08:00
SDL_DelEventWatch ( events_sampleNullEventFilter , NULL ) ;
2022-12-01 04:51:59 +08:00
SDLTest_AssertPass ( " Call to SDL_DelEventWatch() " ) ;
/* Push a user event onto the queue and force queue update */
2022-12-30 05:58:16 +08:00
g_eventFilterCalled = 0 ;
2022-12-01 04:51:59 +08:00
SDL_PushEvent ( & event ) ;
SDLTest_AssertPass ( " Call to SDL_PushEvent() " ) ;
SDL_PumpEvents ( ) ;
SDLTest_AssertPass ( " Call to SDL_PumpEvents() " ) ;
2022-12-30 05:58:16 +08:00
SDLTest_AssertCheck ( g_eventFilterCalled = = 0 , " Check that event filter was NOT called " ) ;
2022-12-01 04:51:59 +08:00
return TEST_COMPLETED ;
2015-06-21 23:33:46 +08:00
}
/**
2023-11-06 23:26:06 +08:00
* Adds and deletes an event watch function with userdata
2015-06-21 23:33:46 +08:00
*
2023-02-02 07:21:53 +08:00
* \ sa SDL_AddEventWatch
* \ sa SDL_DelEventWatch
2015-06-21 23:33:46 +08:00
*
*/
2023-03-08 23:12:45 +08:00
static int events_addDelEventWatchWithUserdata ( void * arg )
2015-06-21 23:33:46 +08:00
{
2022-12-01 04:51:59 +08:00
SDL_Event event ;
/* Create user event */
2023-01-24 09:54:09 +08:00
event . type = SDL_EVENT_USER ;
2023-06-14 05:00:00 +08:00
event . common . timestamp = 0 ;
2022-12-01 04:51:59 +08:00
event . user . code = SDLTest_RandomSint32 ( ) ;
2022-12-30 05:58:16 +08:00
event . user . data1 = ( void * ) & g_userdataValue1 ;
event . user . data2 = ( void * ) & g_userdataValue2 ;
2022-12-01 04:51:59 +08:00
/* Enable userdata check and set a value to check */
2022-12-30 05:58:16 +08:00
g_userdataCheck = 1 ;
g_userdataValue = SDLTest_RandomIntegerInRange ( - 1024 , 1024 ) ;
2022-12-01 04:51:59 +08:00
/* Reset event filter call tracker */
2022-12-30 05:58:16 +08:00
g_eventFilterCalled = 0 ;
2022-12-01 04:51:59 +08:00
/* Add watch */
2022-12-30 05:58:16 +08:00
SDL_AddEventWatch ( events_sampleNullEventFilter , ( void * ) & g_userdataValue ) ;
2022-12-01 04:51:59 +08:00
SDLTest_AssertPass ( " Call to SDL_AddEventWatch() " ) ;
/* Push a user event onto the queue and force queue update */
SDL_PushEvent ( & event ) ;
SDLTest_AssertPass ( " Call to SDL_PushEvent() " ) ;
SDL_PumpEvents ( ) ;
SDLTest_AssertPass ( " Call to SDL_PumpEvents() " ) ;
2022-12-30 05:58:16 +08:00
SDLTest_AssertCheck ( g_eventFilterCalled = = 1 , " Check that event filter was called " ) ;
2022-12-01 04:51:59 +08:00
/* Delete watch */
2022-12-30 05:58:16 +08:00
SDL_DelEventWatch ( events_sampleNullEventFilter , ( void * ) & g_userdataValue ) ;
2022-12-01 04:51:59 +08:00
SDLTest_AssertPass ( " Call to SDL_DelEventWatch() " ) ;
/* Push a user event onto the queue and force queue update */
2022-12-30 05:58:16 +08:00
g_eventFilterCalled = 0 ;
2022-12-01 04:51:59 +08:00
SDL_PushEvent ( & event ) ;
SDLTest_AssertPass ( " Call to SDL_PushEvent() " ) ;
SDL_PumpEvents ( ) ;
SDLTest_AssertPass ( " Call to SDL_PumpEvents() " ) ;
2022-12-30 05:58:16 +08:00
SDLTest_AssertCheck ( g_eventFilterCalled = = 0 , " Check that event filter was NOT called " ) ;
2022-12-01 04:51:59 +08:00
return TEST_COMPLETED ;
2015-06-21 23:33:46 +08:00
}
/* ================= Test References ================== */
/* Events test cases */
2022-12-01 04:51:59 +08:00
static const SDLTest_TestCaseReference eventsTest1 = {
( SDLTest_TestCaseFp ) events_pushPumpAndPollUserevent , " events_pushPumpAndPollUserevent " , " Pushes, pumps and polls a user event " , TEST_ENABLED
} ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
static const SDLTest_TestCaseReference eventsTest2 = {
( SDLTest_TestCaseFp ) events_addDelEventWatch , " events_addDelEventWatch " , " Adds and deletes an event watch function with NULL userdata " , TEST_ENABLED
} ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
static const SDLTest_TestCaseReference eventsTest3 = {
( SDLTest_TestCaseFp ) events_addDelEventWatchWithUserdata , " events_addDelEventWatchWithUserdata " , " Adds and deletes an event watch function with userdata " , TEST_ENABLED
} ;
2015-06-21 23:33:46 +08:00
/* Sequence of Events test cases */
2022-12-01 04:51:59 +08:00
static const SDLTest_TestCaseReference * eventsTests [ ] = {
2015-06-21 23:33:46 +08:00
& eventsTest1 , & eventsTest2 , & eventsTest3 , NULL
} ;
/* Events test suite (global) */
SDLTest_TestSuiteReference eventsTestSuite = {
" Events " ,
NULL ,
eventsTests ,
NULL
} ;