From 987fc16a8ae124548e2074d6f46806387c03e161 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Sun, 24 Nov 2024 22:56:20 +0100 Subject: creating inputSystem tests --- src/test/inputTest.cpp | 139 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 132 insertions(+), 7 deletions(-) (limited to 'src/test') diff --git a/src/test/inputTest.cpp b/src/test/inputTest.cpp index 0f02410..6b820ee 100644 --- a/src/test/inputTest.cpp +++ b/src/test/inputTest.cpp @@ -1,10 +1,18 @@ +#include +#define protected public #include #include #include "system/InputSystem.h" +#include #include "api/EventManager.h" #include "api/KeyCodes.h" #include -#include +#include +#include +#include +#include +#include +#include using namespace std; using namespace std::chrono_literals; @@ -12,14 +20,17 @@ using namespace crepe; class InputTest : public ::testing::Test { public: -InputSystem input_system; +ComponentManager mgr{}; +InputSystem input_system{mgr}; + EventManager& event_manager = EventManager::get_instance(); protected: void SetUp() override { + event_manager.clear(); } void TearDown() override { - + event_manager.clear(); } void simulate_mouse_click(int mouse_x, int mouse_y, Uint8 mouse_button) { SDL_Event event; @@ -41,13 +52,127 @@ void simulate_mouse_click(int mouse_x, int mouse_y, Uint8 mouse_button) { }; -TEST_F(InputTest, KeyDown) { - +TEST_F(InputTest, MouseDown) { + bool mouse_triggered = false; + EventHandler on_mouse_click = [&](const MousePressEvent& event) { + // Handle the mouse click event here + mouse_triggered = true; + EXPECT_EQ(event.mouse_x, 10); + EXPECT_EQ(event.mouse_y, 10); + EXPECT_EQ(event.button, MouseButton::LEFT_MOUSE); + return false; + }; + event_manager.subscribe(on_mouse_click); SDL_Event event; SDL_zero(event); event.type = SDL_MOUSEBUTTONDOWN; event.button.x = 10; event.button.y = 10; - event.button.button = mouse_bu; - SDL_PushEvent(&event); // Push event into the SDL event queue + event.button.button = SDL_BUTTON_LEFT; + SDL_PushEvent(&event); + EXPECT_TRUE(mouse_triggered); +} + +TEST_F(InputTest, MouseUp) { + EventHandler on_mouse_click = [&](const MouseReleaseEvent& event) { + // Handle the mouse click event here + EXPECT_EQ(event.mouse_x, 10); + EXPECT_EQ(event.mouse_y, 10); + EXPECT_EQ(event.button, MouseButton::LEFT_MOUSE); + return false; + }; + event_manager.subscribe(on_mouse_click); + SDL_Event event; + SDL_zero(event); + event.type = SDL_MOUSEBUTTONUP; + event.button.x = 10; + event.button.y = 10; + event.button.button = SDL_BUTTON_LEFT; + SDL_PushEvent(&event); +} +TEST_F(InputTest, MouseUp) { + EventHandler on_mouse_click = [&](const MouseReleaseEvent& event) { + // Handle the mouse click event here + EXPECT_EQ(event.mouse_x, 10); + EXPECT_EQ(event.mouse_y, 10); + EXPECT_EQ(event.button, MouseButton::LEFT_MOUSE); + return false; + }; + event_manager.subscribe(on_mouse_click); + SDL_Event event; + SDL_zero(event); + event.type = SDL_MOUSEBUTTONUP; + event.button.x = 10; + event.button.y = 10; + event.button.button = SDL_BUTTON_LEFT; + SDL_PushEvent(&event); +} +TEST_F(InputTest, KeyDown) { + EventHandler on_mouse_click = [&](const KeyPressEvent& event) { + // Handle the mouse click event here + EXPECT_EQ(event.key, Keycode::B); + EXPECT_EQ(event.repeat, true); + return false; + }; + event_manager.subscribe(on_mouse_click); + SDL_Event event; + SDL_zero(event); + event.type = SDL_KEYUP; + event.button.button = SDL_BUTTON_LEFT; + event.key.repeat = 1; + SDL_PushEvent(&event); +} +TEST_F(InputTest, KeyUp) { + EventHandler on_mouse_click = [&](const KeyReleaseEvent& event) { + // Handle the mouse click event here + EXPECT_EQ(event.key, Keycode::B); + return false; + }; + event_manager.subscribe(on_mouse_click); + SDL_Event event; + SDL_zero(event); + event.type = SDL_KEYUP; + event.button.button = SDL_BUTTON_LEFT; + SDL_PushEvent(&event); +} +TEST_F(InputTest, KeyClick) { + GameObject obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1); + Button test_button; + bool button_clicked = false; + test_button.active = true; + test_button.width = 100; + test_button.height = 100; + std::function on_click = [&]() { + button_clicked = true; + }; + test_button.on_click = on_click; + test_button.is_pressed = false; + test_button.is_toggle = false; + obj.add_component