diff options
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/EventTest.cpp | 57 | ||||
-rw-r--r-- | src/test/InputTest.cpp | 78 |
2 files changed, 89 insertions, 46 deletions
diff --git a/src/test/EventTest.cpp b/src/test/EventTest.cpp index 82272b5..f30f15a 100644 --- a/src/test/EventTest.cpp +++ b/src/test/EventTest.cpp @@ -48,16 +48,16 @@ TEST_F(EventManagerTest, EventManagerTest_trigger_all_channels) { EventHandler<MouseClickEvent> 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; }; event_mgr.subscribe<MouseClickEvent>(mouse_handler, EventManager::CHANNEL_ALL); - MouseClickEvent click_event{ - .mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}; - event_mgr.trigger_event<MouseClickEvent>(click_event, EventManager::CHANNEL_ALL); + MouseClickEvent click_event{.mouse_pos = {100, 200}, .button = MouseButton::LEFT_MOUSE}; + event_mgr.trigger_event<MouseClickEvent>(click_event, + EventManager::CHANNEL_ALL); EXPECT_TRUE(triggered); } @@ -66,16 +66,16 @@ TEST_F(EventManagerTest, EventManagerTest_trigger_one_channel) { int test_channel = 1; EventHandler<MouseClickEvent> 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; }; event_mgr.subscribe<MouseClickEvent>(mouse_handler, test_channel); - MouseClickEvent click_event{ - .mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}; - event_mgr.trigger_event<MouseClickEvent>(click_event, EventManager::CHANNEL_ALL); + MouseClickEvent click_event{.mouse_pos = {100, 200}, .button = MouseButton::LEFT_MOUSE}; + event_mgr.trigger_event<MouseClickEvent>(click_event, + EventManager::CHANNEL_ALL); EXPECT_FALSE(triggered); event_mgr.trigger_event<MouseClickEvent>(click_event, test_channel); @@ -90,23 +90,22 @@ TEST_F(EventManagerTest, EventManagerTest_callback_propagation) { // Handlers EventHandler<MouseClickEvent> 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<MouseClickEvent> 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_mgr.subscribe<MouseClickEvent>(mouse_handler_true, EventManager::CHANNEL_ALL); event_mgr.subscribe<MouseClickEvent>(mouse_handler_false, EventManager::CHANNEL_ALL); @@ -138,15 +137,15 @@ TEST_F(EventManagerTest, EventManagerTest_queue_dispatch) { int test_channel = 1; EventHandler<MouseClickEvent> 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<MouseClickEvent> 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 }; @@ -154,9 +153,9 @@ TEST_F(EventManagerTest, EventManagerTest_queue_dispatch) { event_mgr.subscribe<MouseClickEvent>(mouse_handler2, test_channel); event_mgr.queue_event<MouseClickEvent>( - MouseClickEvent{.mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}); + MouseClickEvent{.mouse_pos = {100, 200}, .button = MouseButton::LEFT_MOUSE}); event_mgr.queue_event<MouseClickEvent>( - MouseClickEvent{.mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}, + MouseClickEvent{.mouse_pos = {100, 200}, .button = MouseButton::LEFT_MOUSE}, test_channel); event_mgr.dispatch_events(); EXPECT_TRUE(triggered1); @@ -172,16 +171,16 @@ TEST_F(EventManagerTest, EventManagerTest_unsubscribe) { // Define EventHandlers EventHandler<MouseClickEvent> 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<MouseClickEvent> 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 }; @@ -191,7 +190,7 @@ TEST_F(EventManagerTest, EventManagerTest_unsubscribe) { // Queue events event_mgr.queue_event<MouseClickEvent>( - 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_mgr.dispatch_events(); @@ -207,7 +206,7 @@ TEST_F(EventManagerTest, EventManagerTest_unsubscribe) { // Queue the same event again event_mgr.queue_event<MouseClickEvent>( - 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_mgr.dispatch_events(); @@ -222,7 +221,7 @@ TEST_F(EventManagerTest, EventManagerTest_unsubscribe) { // Queue the event again event_mgr.queue_event<MouseClickEvent>( - 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_mgr.dispatch_events(); diff --git a/src/test/InputTest.cpp b/src/test/InputTest.cpp index 8b40cea..7a28882 100644 --- a/src/test/InputTest.cpp +++ b/src/test/InputTest.cpp @@ -70,8 +70,8 @@ TEST_F(InputTest, MouseDown) { EventHandler<MousePressEvent> 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.mouse_pos.x, 0); + EXPECT_EQ(event.mouse_pos.y, 0); EXPECT_EQ(event.button, MouseButton::LEFT_MOUSE); return false; }; @@ -95,8 +95,8 @@ TEST_F(InputTest, MouseUp) { bool function_triggered = false; EventHandler<MouseReleaseEvent> on_mouse_release = [&](const MouseReleaseEvent & e) { function_triggered = true; - EXPECT_EQ(e.mouse_x, 0); - EXPECT_EQ(e.mouse_y, 0); + EXPECT_EQ(e.mouse_pos.x, 0); + EXPECT_EQ(e.mouse_pos.y, 0); EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); return false; }; @@ -119,10 +119,10 @@ TEST_F(InputTest, MouseMove) { bool function_triggered = false; EventHandler<MouseMoveEvent> 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); + EXPECT_EQ(e.mouse_pos.x, 0); + EXPECT_EQ(e.mouse_pos.y, 0); + EXPECT_EQ(e.mouse_delta.x, 10); + EXPECT_EQ(e.mouse_delta.y, 10); return false; }; event_manager.subscribe<MouseMoveEvent>(on_mouse_move); @@ -193,8 +193,8 @@ TEST_F(InputTest, MouseClick) { EventHandler<MouseClickEvent> 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); + EXPECT_EQ(event.mouse_pos.x, 0); + EXPECT_EQ(event.mouse_pos.y, 0); return false; }; event_manager.subscribe<MouseClickEvent>(on_mouse_click); @@ -210,13 +210,10 @@ TEST_F(InputTest, testButtonClick) { bool button_clicked = false; std::function<void()> on_click = [&]() { button_clicked = true; }; auto & button - = button_obj.add_component<Button>(vec2{100, 100}, vec2{0, 0}, on_click, false); + = button_obj.add_component<Button>(vec2{100, 100}, vec2{0, 0}, on_click); bool hover = false; button.active = true; - - button.is_pressed = false; - button.is_toggle = false; this->simulate_mouse_click(999, 999, SDL_BUTTON_LEFT); input_system.update(); event_manager.dispatch_events(); @@ -233,10 +230,8 @@ TEST_F(InputTest, testButtonHover) { bool button_clicked = false; std::function<void()> on_click = [&]() { button_clicked = true; }; auto & button - = button_obj.add_component<Button>(vec2{100, 100}, vec2{0, 0}, on_click, false); + = button_obj.add_component<Button>(vec2{100, 100}, vec2{0, 0}, on_click); button.active = true; - button.is_pressed = false; - button.is_toggle = false; // Mouse not on button SDL_Event event; @@ -266,3 +261,52 @@ TEST_F(InputTest, testButtonHover) { event_manager.dispatch_events(); EXPECT_TRUE(button.hover); } + +TEST_F(InputTest, WindowResizeTest) { + GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1); + auto & camera = obj.add_component<Camera>( + ivec2{0, 0}, vec2{500, 500}, Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f}); + camera.active = true; + bool callback_triggered = false; + EventHandler<WindowResizeEvent> on_window_resize = [&](const WindowResizeEvent & event) { + callback_triggered = true; + EXPECT_EQ(event.dimensions.x, 800); + EXPECT_EQ(event.dimensions.y, 600); + return false; + }; + event_manager.subscribe<WindowResizeEvent>(on_window_resize); + SDL_Event resize_event; + SDL_zero(resize_event); + resize_event.type = SDL_WINDOWEVENT; + resize_event.window.event = SDL_WINDOWEVENT_RESIZED; + resize_event.window.data1 = 800; // new width + resize_event.window.data2 = 600; // new height + SDL_PushEvent(&resize_event); + input_system.update(); + event_manager.dispatch_events(); + EXPECT_TRUE(callback_triggered); +} +TEST_F(InputTest, WindowMoveTest) { + GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1); + auto & camera = obj.add_component<Camera>( + ivec2{0, 0}, vec2{500, 500}, Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f}); + camera.active = true; + bool callback_triggered = false; + EventHandler<WindowMoveEvent> on_window_move = [&](const WindowMoveEvent & event) { + callback_triggered = true; + EXPECT_EQ(event.delta_move.x, 800); + EXPECT_EQ(event.delta_move.y, 600); + return false; + }; + event_manager.subscribe<WindowMoveEvent>(on_window_move); + SDL_Event resize_event; + SDL_zero(resize_event); + resize_event.type = SDL_WINDOWEVENT; + resize_event.window.event = SDL_WINDOWEVENT_MOVED; + resize_event.window.data1 = 800; // new width + resize_event.window.data2 = 600; // new height + SDL_PushEvent(&resize_event); + input_system.update(); + event_manager.dispatch_events(); + EXPECT_TRUE(callback_triggered); +} |