mirror of
https://github.com/libSDL2pp/libSDL2pp.git
synced 2025-08-03 19:05:59 -04:00
Added tests for event polling
This commit is contained in:
parent
d64bdb3449
commit
a8edb0fe66
@ -5,6 +5,7 @@ SET(CLI_TESTS
|
||||
test_error
|
||||
test_eventdispatching
|
||||
test_eventhandler
|
||||
test_eventpolling
|
||||
test_optional
|
||||
test_pointrect
|
||||
test_pointrect_constexpr
|
||||
|
76
tests/test_eventpolling.cc
Normal file
76
tests/test_eventpolling.cc
Normal file
@ -0,0 +1,76 @@
|
||||
#include <SDL.h>
|
||||
|
||||
#include <SDL2pp/SDL.hh>
|
||||
#include <SDL2pp/EventPolling.hh>
|
||||
|
||||
#include "testing.h"
|
||||
|
||||
using namespace SDL2pp;
|
||||
using namespace SDL2pp::Event;
|
||||
|
||||
BEGIN_TEST(int, char*[])
|
||||
SDL sdl{SDL_INIT_EVENTS};
|
||||
|
||||
// Empty event poll
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event));
|
||||
|
||||
// Poll a single event
|
||||
{
|
||||
event.type = SDL_KEYUP;
|
||||
SDL_PushEvent(&event);
|
||||
|
||||
EXPECT_TRUE(PollEvent());
|
||||
EXPECT_TRUE(!PollEvent()); // Verify no additional events
|
||||
}
|
||||
|
||||
// Poll multiple events single event
|
||||
{
|
||||
event.type = SDL_KEYUP;
|
||||
constexpr auto amountOfEvents = 5;
|
||||
for (auto n = 0; n < amountOfEvents; n++)
|
||||
SDL_PushEvent(&event);
|
||||
|
||||
EXPECT_EQUAL(PollAllEvents(), amountOfEvents);
|
||||
EXPECT_TRUE(!PollEvent()); // Verify no additional events
|
||||
}
|
||||
|
||||
// Poll with an event handler
|
||||
{
|
||||
struct EventHandler {
|
||||
int eventsHandled = 0;
|
||||
int keyboardEventsHandled = 0;
|
||||
bool quitEventHandled = false;
|
||||
|
||||
void HandleEvent(SDL_Event) {
|
||||
eventsHandled++;
|
||||
}
|
||||
|
||||
void HandleEvent(SDL_KeyboardEvent) {
|
||||
keyboardEventsHandled++;
|
||||
}
|
||||
|
||||
void HandleEvent(SDL_QuitEvent) {
|
||||
quitEventHandled = true;
|
||||
}
|
||||
};
|
||||
|
||||
event.type = SDL_KEYUP;
|
||||
SDL_PushEvent(&event);
|
||||
|
||||
event.type = SDL_KEYDOWN;
|
||||
SDL_PushEvent(&event);
|
||||
|
||||
event.type = SDL_QUIT;
|
||||
SDL_PushEvent(&event);
|
||||
|
||||
auto eventHandler = EventHandler{};
|
||||
|
||||
EXPECT_EQUAL(PollAllEvents(eventHandler), 3);
|
||||
EXPECT_TRUE(!PollEvent()); // Verify no additional events
|
||||
|
||||
EXPECT_EQUAL(eventHandler.eventsHandled, 3);
|
||||
EXPECT_EQUAL(eventHandler.keyboardEventsHandled, 2);
|
||||
EXPECT_EQUAL(eventHandler.quitEventHandled, true);
|
||||
}
|
||||
END_TEST()
|
Loading…
x
Reference in New Issue
Block a user