From 666292e33d6997a61d9eaff31e8180a602656474 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Sat, 7 Dec 2024 20:39:19 +0100 Subject: save --- src/test/EventTest.cpp | 39 ++-- src/test/InputTest.cpp | 586 ++++++++++++++++++++++++------------------------- 2 files changed, 311 insertions(+), 314 deletions(-) (limited to 'src/test') diff --git a/src/test/EventTest.cpp b/src/test/EventTest.cpp index 4a4872d..53c2469 100644 --- a/src/test/EventTest.cpp +++ b/src/test/EventTest.cpp @@ -63,16 +63,15 @@ TEST_F(EventManagerTest, EventManagerTest_trigger_all_channels) { EventHandler mouse_handler = [&](const MouseClickEvent & e) { triggered = true; - EXPECT_EQ(e.mouse_x, 100); - EXPECT_EQ(e.mouse_y, 200); + EXPECT_EQ(e.mouse_pos.x, 100); + EXPECT_EQ(e.mouse_pos.y, 200); EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); return false; }; EventManager::get_instance().subscribe(mouse_handler, EventManager::CHANNEL_ALL); - MouseClickEvent click_event{ - .mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}; + MouseClickEvent click_event{.mouse_pos = {100,200}, .button = MouseButton::LEFT_MOUSE}; EventManager::get_instance().trigger_event(click_event, EventManager::CHANNEL_ALL); @@ -83,15 +82,14 @@ TEST_F(EventManagerTest, EventManagerTest_trigger_one_channel) { int test_channel = 1; EventHandler mouse_handler = [&](const MouseClickEvent & e) { triggered = true; - EXPECT_EQ(e.mouse_x, 100); - EXPECT_EQ(e.mouse_y, 200); + EXPECT_EQ(e.mouse_pos.x, 100); + EXPECT_EQ(e.mouse_pos.y, 200); EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); return false; }; EventManager::get_instance().subscribe(mouse_handler, test_channel); - MouseClickEvent click_event{ - .mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}; + MouseClickEvent click_event{.mouse_pos = {100,200}, .button = MouseButton::LEFT_MOUSE}; EventManager::get_instance().trigger_event(click_event, EventManager::CHANNEL_ALL); @@ -109,23 +107,22 @@ TEST_F(EventManagerTest, EventManagerTest_callback_propagation) { // Handlers EventHandler mouse_handler_true = [&](const MouseClickEvent & e) { triggered_true = true; - EXPECT_EQ(e.mouse_x, 100); - EXPECT_EQ(e.mouse_y, 200); + EXPECT_EQ(e.mouse_pos.x, 100); + EXPECT_EQ(e.mouse_pos.y, 200); EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); return true; // Stops propagation }; EventHandler mouse_handler_false = [&](const MouseClickEvent & e) { triggered_false = true; - EXPECT_EQ(e.mouse_x, 100); - EXPECT_EQ(e.mouse_y, 200); + EXPECT_EQ(e.mouse_pos.x, 100); + EXPECT_EQ(e.mouse_pos.y, 200); EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); return false; // Allows propagation }; // Test event - MouseClickEvent click_event{ - .mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}; + MouseClickEvent click_event{.mouse_pos = {100,200}, .button = MouseButton::LEFT_MOUSE}; event_manager.subscribe(mouse_handler_true, EventManager::CHANNEL_ALL); event_manager.subscribe(mouse_handler_false, EventManager::CHANNEL_ALL); @@ -201,16 +198,16 @@ TEST_F(EventManagerTest, EventManagerTest_unsubscribe) { // Define EventHandlers EventHandler mouse_handler1 = [&](const MouseClickEvent & e) { triggered1 = true; - EXPECT_EQ(e.mouse_x, 100); - EXPECT_EQ(e.mouse_y, 200); + EXPECT_EQ(e.mouse_pos.x, 100); + EXPECT_EQ(e.mouse_pos.y, 200); EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); return false; // Allows propagation }; EventHandler mouse_handler2 = [&](const MouseClickEvent & e) { triggered2 = true; - EXPECT_EQ(e.mouse_x, 100); - EXPECT_EQ(e.mouse_y, 200); + EXPECT_EQ(e.mouse_pos.x, 100); + EXPECT_EQ(e.mouse_pos.y, 200); EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); return false; // Allows propagation }; @@ -220,7 +217,7 @@ TEST_F(EventManagerTest, EventManagerTest_unsubscribe) { // Queue events event_manager.queue_event( - MouseClickEvent{.mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}); + MouseClickEvent{.mouse_pos = {100,200}, .button = MouseButton::LEFT_MOUSE}); // Dispatch events - both handlers should be triggered event_manager.dispatch_events(); @@ -236,7 +233,7 @@ TEST_F(EventManagerTest, EventManagerTest_unsubscribe) { // Queue the same event again event_manager.queue_event( - MouseClickEvent{.mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}); + MouseClickEvent{.mouse_pos = {100,200}, .button = MouseButton::LEFT_MOUSE}); // Dispatch events - only handler 2 should be triggered, handler 1 should NOT event_manager.dispatch_events(); @@ -251,7 +248,7 @@ TEST_F(EventManagerTest, EventManagerTest_unsubscribe) { // Queue the event again event_manager.queue_event( - MouseClickEvent{.mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}); + MouseClickEvent{.mouse_pos = {100,200}, .button = MouseButton::LEFT_MOUSE}); // Dispatch events - no handler should be triggered event_manager.dispatch_events(); diff --git a/src/test/InputTest.cpp b/src/test/InputTest.cpp index a7c0157..0b48d5b 100644 --- a/src/test/InputTest.cpp +++ b/src/test/InputTest.cpp @@ -1,293 +1,293 @@ -#include -#define protected public -#define private public -#include "api/KeyCodes.h" -#include "manager/ComponentManager.h" -#include "manager/EventManager.h" -#include "manager/Mediator.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: - Mediator mediator; - ComponentManager mgr{mediator}; - - InputSystem input_system{mediator}; - - EventManager & event_manager = EventManager::get_instance(); - //GameObject camera; - -protected: - void SetUp() override { - mediator.event_manager = event_manager; - mediator.component_manager = mgr; - 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) { - GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1); - auto & camera = obj.add_component( - ivec2{100, 100}, vec2{100, 100}, Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f}); - camera.active = true; - bool mouse_triggered = false; - EventHandler on_mouse_down = [&](const MousePressEvent & event) { - mouse_triggered = true; - //middle of the screen = 0,0 - EXPECT_EQ(event.mouse_x, 0); - EXPECT_EQ(event.mouse_y, 0); - 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; - // middle of the screen of a 500*500 camera = 250*250 - event.button.x = 250; - event.button.y = 250; - event.button.button = SDL_BUTTON_LEFT; - SDL_PushEvent(&event); - - input_system.update(); - event_manager.dispatch_events(); - EXPECT_TRUE(mouse_triggered); -} - -TEST_F(InputTest, MouseUp) { - GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1); - auto & camera = obj.add_component( - ivec2{100, 100}, vec2{100, 100}, Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f}); - camera.active = true; - bool function_triggered = false; - EventHandler on_mouse_release = [&](const MouseReleaseEvent & e) { - function_triggered = true; - EXPECT_EQ(e.mouse_x, 0); - EXPECT_EQ(e.mouse_y, 0); - 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 = 250; - event.button.y = 250; - event.button.button = SDL_BUTTON_LEFT; - SDL_PushEvent(&event); - - input_system.update(); - event_manager.dispatch_events(); - EXPECT_TRUE(function_triggered); -} - -TEST_F(InputTest, MouseMove) { - GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1); - auto & camera = obj.add_component( - ivec2{100, 100}, vec2{100, 100}, Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f}); - camera.active = true; - bool function_triggered = false; - EventHandler on_mouse_move = [&](const MouseMoveEvent & e) { - function_triggered = true; - EXPECT_EQ(e.mouse_x, 0); - EXPECT_EQ(e.mouse_y, 0); - EXPECT_EQ(e.delta_x, 10); - EXPECT_EQ(e.delta_y, 10); - return false; - }; - event_manager.subscribe(on_mouse_move); - - SDL_Event event; - SDL_zero(event); - event.type = SDL_MOUSEMOTION; - event.motion.x = 250; - event.motion.y = 250; - 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) { - GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1); - auto & camera = obj.add_component( - ivec2{100, 100}, vec2{100, 100}, Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f}); - camera.active = true; - 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) { - GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1); - auto & camera = obj.add_component( - ivec2{100, 100}, vec2{100, 100}, Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f}); - camera.active = true; - 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) { - GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1); - auto & camera = obj.add_component( - ivec2{100, 100}, vec2{100, 100}, Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f}); - camera.active = true; - 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, 0); - EXPECT_EQ(event.mouse_y, 0); - return false; - }; - event_manager.subscribe(on_mouse_click); - - this->simulate_mouse_click(250, 250, SDL_BUTTON_LEFT); - input_system.update(); - event_manager.dispatch_events(); - EXPECT_TRUE(on_click_triggered); -} - -TEST_F(InputTest, testButtonClick) { - GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1); - auto & camera = obj.add_component( - ivec2{100, 100}, vec2{100, 100}, Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f}); - camera.active = true; - GameObject button_obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1); - bool button_clicked = false; - std::function on_click = [&]() { button_clicked = true; }; - auto & button - = button_obj.add_component