diff options
author | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-11-27 14:25:32 +0100 |
---|---|---|
committer | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-11-27 14:25:32 +0100 |
commit | 7070f22d86057eafca3b82321d4146958c14a33e (patch) | |
tree | 9355ce7728a5ab9a9dbf964461b854591e7018d9 /src/test | |
parent | ad4de3ee60f699595bfd1950f88e5417fa29a0d5 (diff) |
pr feedback
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/inputTest.cpp | 45 |
1 files changed, 26 insertions, 19 deletions
diff --git a/src/test/inputTest.cpp b/src/test/inputTest.cpp index 3a9d341..0e3e097 100644 --- a/src/test/inputTest.cpp +++ b/src/test/inputTest.cpp @@ -20,12 +20,14 @@ using namespace crepe; class InputTest : public ::testing::Test { public: ComponentManager mgr{}; - InputSystem input_system{mgr}; + InputSystem input_system{mgr}; // Initializes the InputSystem with the ComponentManager - EventManager & event_manager = EventManager::get_instance(); + EventManager& event_manager = EventManager::get_instance(); protected: - void SetUp() override { event_manager.clear(); } + void SetUp() override { + event_manager.clear(); + } void simulate_mouse_click(int mouse_x, int mouse_y, Uint8 mouse_button) { SDL_Event event; @@ -37,6 +39,8 @@ protected: 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; @@ -48,7 +52,7 @@ protected: TEST_F(InputTest, MouseDown) { bool mouse_triggered = false; - EventHandler<MousePressEvent> on_mouse_down = [&](const MousePressEvent & event) { + EventHandler<MousePressEvent> on_mouse_down = [&](const MousePressEvent& event) { mouse_triggered = true; EXPECT_EQ(event.mouse_x, 10); EXPECT_EQ(event.mouse_y, 10); @@ -64,6 +68,7 @@ TEST_F(InputTest, MouseDown) { event.button.y = 10; event.button.button = SDL_BUTTON_LEFT; SDL_PushEvent(&event); + input_system.update(); event_manager.dispatch_events(); EXPECT_TRUE(mouse_triggered); @@ -71,8 +76,7 @@ TEST_F(InputTest, MouseDown) { TEST_F(InputTest, MouseUp) { bool function_triggered = false; - EventHandler<MouseReleaseEvent> on_mouse_release = [&](const MouseReleaseEvent & e) { - // Handle the mouse click event here + EventHandler<MouseReleaseEvent> on_mouse_release = [&](const MouseReleaseEvent& e) { function_triggered = true; EXPECT_EQ(e.mouse_x, 10); EXPECT_EQ(e.mouse_y, 10); @@ -88,6 +92,7 @@ TEST_F(InputTest, MouseUp) { event.button.y = 10; event.button.button = SDL_BUTTON_LEFT; SDL_PushEvent(&event); + input_system.update(); event_manager.dispatch_events(); EXPECT_TRUE(function_triggered); @@ -95,8 +100,7 @@ TEST_F(InputTest, MouseUp) { TEST_F(InputTest, MouseMove) { bool function_triggered = false; - EventHandler<MouseMoveEvent> on_mouse_move = [&](const MouseMoveEvent & e) { - // Handle the mouse click event here + EventHandler<MouseMoveEvent> on_mouse_move = [&](const MouseMoveEvent& e) { function_triggered = true; EXPECT_EQ(e.mouse_x, 10); EXPECT_EQ(e.mouse_y, 10); @@ -114,6 +118,7 @@ TEST_F(InputTest, MouseMove) { event.motion.xrel = 10; event.motion.yrel = 10; SDL_PushEvent(&event); + input_system.update(); event_manager.dispatch_events(); EXPECT_TRUE(function_triggered); @@ -123,7 +128,7 @@ TEST_F(InputTest, KeyDown) { bool function_triggered = false; // Define event handler for KeyPressEvent - EventHandler<KeyPressEvent> on_key_press = [&](const KeyPressEvent & event) { + EventHandler<KeyPressEvent> 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 @@ -148,7 +153,7 @@ TEST_F(InputTest, KeyDown) { TEST_F(InputTest, KeyUp) { bool function_triggered = false; - EventHandler<KeyReleaseEvent> on_key_release = [&](const KeyReleaseEvent & event) { + EventHandler<KeyReleaseEvent> on_key_release = [&](const KeyReleaseEvent& event) { function_triggered = true; EXPECT_EQ(event.key, Keycode::B); return false; @@ -160,6 +165,7 @@ TEST_F(InputTest, KeyUp) { 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); @@ -167,7 +173,7 @@ TEST_F(InputTest, KeyUp) { TEST_F(InputTest, MouseClick) { bool on_click_triggered = false; - EventHandler<MouseClickEvent> on_mouse_click = [&](const MouseClickEvent & event) { + EventHandler<MouseClickEvent> on_mouse_click = [&](const MouseClickEvent& event) { on_click_triggered = true; EXPECT_EQ(event.button, MouseButton::LEFT_MOUSE); EXPECT_EQ(event.mouse_x, 10); @@ -184,13 +190,11 @@ TEST_F(InputTest, MouseClick) { TEST_F(InputTest, testButtonClick) { GameObject obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1); - auto & button = obj.add_component<Button>(); + + auto& button = obj.add_component<Button>(100,100); bool button_clicked = false; bool hover = false; button.active = true; - button.interactable = true; - button.width = 100; - button.height = 100; std::function<void()> on_click = [&]() { button_clicked = true; }; button.on_click = on_click; button.is_pressed = false; @@ -199,6 +203,7 @@ TEST_F(InputTest, testButtonClick) { input_system.update(); event_manager.dispatch_events(); EXPECT_FALSE(button_clicked); + this->simulate_mouse_click(10, 10, SDL_BUTTON_LEFT); input_system.update(); event_manager.dispatch_events(); @@ -207,15 +212,15 @@ TEST_F(InputTest, testButtonClick) { TEST_F(InputTest, testButtonHover) { GameObject obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1); - auto & button = obj.add_component<Button>(); + auto& button = obj.add_component<Button>(100,100); bool button_clicked = false; button.active = true; - button.interactable = true; button.width = 100; button.height = 100; button.is_pressed = false; button.is_toggle = false; - //mouse not on button + + // Mouse not on button SDL_Event event; SDL_zero(event); event.type = SDL_MOUSEMOTION; @@ -228,7 +233,8 @@ TEST_F(InputTest, testButtonHover) { input_system.update(); event_manager.dispatch_events(); EXPECT_FALSE(button.hover); - //mouse on button + + // Mouse on button SDL_Event hover_event; SDL_zero(hover_event); hover_event.type = SDL_MOUSEMOTION; @@ -237,6 +243,7 @@ TEST_F(InputTest, testButtonHover) { hover_event.motion.xrel = 10; hover_event.motion.yrel = 10; SDL_PushEvent(&hover_event); + input_system.update(); event_manager.dispatch_events(); EXPECT_TRUE(button.hover); |