diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index e8a9cfc..b1f63e3 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -5,6 +5,7 @@ SET(CLI_TESTS test_error test_eventdispatching test_eventhandler + test_eventpolling test_optional test_pointrect test_pointrect_constexpr diff --git a/tests/test_eventpolling.cc b/tests/test_eventpolling.cc new file mode 100644 index 0000000..fbe24d9 --- /dev/null +++ b/tests/test_eventpolling.cc @@ -0,0 +1,76 @@ +#include + +#include +#include + +#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()