From 327154fd428e0798eea544d9f073f8e1293aa158 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 25 Nov 2024 10:38:18 +0100 Subject: most test working --- src/test/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) (limited to 'src/test/CMakeLists.txt') diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index d310f6a..ac4d73a 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -12,4 +12,5 @@ target_sources(test_main PUBLIC ValueBrokerTest.cpp DBTest.cpp Vector2Test.cpp + inputTest.cpp ) -- cgit v1.2.3 From e8b93d0ffaccf523139f75ff9b9dc43647e90746 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Wed, 27 Nov 2024 14:41:49 +0100 Subject: test file rename --- src/test/CMakeLists.txt | 2 +- src/test/InputTest.cpp | 250 ++++++++++++++++++++++++++++++++++++++++++++++++ src/test/inputTest.cpp | 250 ------------------------------------------------ 3 files changed, 251 insertions(+), 251 deletions(-) create mode 100644 src/test/InputTest.cpp delete mode 100644 src/test/inputTest.cpp (limited to 'src/test/CMakeLists.txt') diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index ac4d73a..dd4a754 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -12,5 +12,5 @@ target_sources(test_main PUBLIC ValueBrokerTest.cpp DBTest.cpp Vector2Test.cpp - inputTest.cpp + InputTest.cpp ) diff --git a/src/test/InputTest.cpp b/src/test/InputTest.cpp new file mode 100644 index 0000000..0e3e097 --- /dev/null +++ b/src/test/InputTest.cpp @@ -0,0 +1,250 @@ +#include +#define protected public +#include "api/EventManager.h" +#include "api/KeyCodes.h" +#include "system/InputSystem.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace std::chrono_literals; +using namespace crepe; + +class InputTest : public ::testing::Test { +public: + ComponentManager mgr{}; + InputSystem input_system{mgr}; // Initializes the InputSystem with the ComponentManager + + EventManager& event_manager = EventManager::get_instance(); + +protected: + void SetUp() override { + event_manager.clear(); + } + + void simulate_mouse_click(int mouse_x, int mouse_y, Uint8 mouse_button) { + SDL_Event event; + + // Simulate Mouse Button Down event + SDL_zero(event); + event.type = SDL_MOUSEBUTTONDOWN; + event.button.x = mouse_x; + event.button.y = mouse_y; + event.button.button = mouse_button; + SDL_PushEvent(&event); + + // Simulate Mouse Button Up event + SDL_zero(event); + event.type = SDL_MOUSEBUTTONUP; + event.button.x = mouse_x; + event.button.y = mouse_y; + event.button.button = mouse_button; + SDL_PushEvent(&event); + } +}; + +TEST_F(InputTest, MouseDown) { + bool mouse_triggered = false; + EventHandler on_mouse_down = [&](const MousePressEvent& event) { + 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_down); + + SDL_Event event; + SDL_zero(event); + event.type = SDL_MOUSEBUTTONDOWN; + event.button.x = 10; + event.button.y = 10; + event.button.button = SDL_BUTTON_LEFT; + SDL_PushEvent(&event); + + input_system.update(); + event_manager.dispatch_events(); + EXPECT_TRUE(mouse_triggered); +} + +TEST_F(InputTest, MouseUp) { + bool function_triggered = false; + EventHandler on_mouse_release = [&](const MouseReleaseEvent& e) { + function_triggered = true; + EXPECT_EQ(e.mouse_x, 10); + EXPECT_EQ(e.mouse_y, 10); + EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); + return false; + }; + event_manager.subscribe(on_mouse_release); + + 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); + + input_system.update(); + event_manager.dispatch_events(); + EXPECT_TRUE(function_triggered); +} + +TEST_F(InputTest, MouseMove) { + bool function_triggered = false; + EventHandler on_mouse_move = [&](const MouseMoveEvent& e) { + function_triggered = true; + EXPECT_EQ(e.mouse_x, 10); + EXPECT_EQ(e.mouse_y, 10); + EXPECT_EQ(e.rel_x, 10); + EXPECT_EQ(e.rel_y, 10); + return false; + }; + event_manager.subscribe(on_mouse_move); + + SDL_Event event; + SDL_zero(event); + event.type = SDL_MOUSEMOTION; + event.motion.x = 10; + event.motion.y = 10; + event.motion.xrel = 10; + event.motion.yrel = 10; + SDL_PushEvent(&event); + + input_system.update(); + event_manager.dispatch_events(); + EXPECT_TRUE(function_triggered); +} + +TEST_F(InputTest, KeyDown) { + bool function_triggered = false; + + // Define event handler for KeyPressEvent + EventHandler on_key_press = [&](const KeyPressEvent& event) { + function_triggered = true; + EXPECT_EQ(event.key, Keycode::B); // Validate the key is 'B' + EXPECT_EQ(event.repeat, true); // Validate repeat flag + return false; + }; + + event_manager.subscribe(on_key_press); + + // Simulate SDL_KEYDOWN event + SDL_Event test_event; + SDL_zero(test_event); + test_event.type = SDL_KEYDOWN; // Key down event + test_event.key.keysym.scancode = SDL_SCANCODE_B; // Set scancode for 'B' + test_event.key.repeat = 1; // Set repeat flag + SDL_PushEvent(&test_event); + + input_system.update(); // Process the event + event_manager.dispatch_events(); // Dispatch events to handlers + + EXPECT_TRUE(function_triggered); // Check if the handler was triggered +} + +TEST_F(InputTest, KeyUp) { + bool function_triggered = false; + EventHandler on_key_release = [&](const KeyReleaseEvent& event) { + function_triggered = true; + EXPECT_EQ(event.key, Keycode::B); + return false; + }; + event_manager.subscribe(on_key_release); + + SDL_Event event; + SDL_zero(event); + event.type = SDL_KEYUP; + event.key.keysym.scancode = SDL_SCANCODE_B; + SDL_PushEvent(&event); + + input_system.update(); + event_manager.dispatch_events(); + EXPECT_TRUE(function_triggered); +} + +TEST_F(InputTest, MouseClick) { + bool on_click_triggered = false; + EventHandler on_mouse_click = [&](const MouseClickEvent& event) { + on_click_triggered = true; + EXPECT_EQ(event.button, MouseButton::LEFT_MOUSE); + EXPECT_EQ(event.mouse_x, 10); + EXPECT_EQ(event.mouse_y, 10); + return false; + }; + event_manager.subscribe(on_mouse_click); + + this->simulate_mouse_click(10, 10, SDL_BUTTON_LEFT); + input_system.update(); + event_manager.dispatch_events(); + EXPECT_TRUE(on_click_triggered); +} + +TEST_F(InputTest, testButtonClick) { + GameObject obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1); + + auto& button = obj.add_component