aboutsummaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
Diffstat (limited to 'src/test')
-rw-r--r--src/test/EventTest.cpp74
-rw-r--r--src/test/inputTest.cpp307
-rw-r--r--src/test/loopTimerTest.cpp25
3 files changed, 199 insertions, 207 deletions
diff --git a/src/test/EventTest.cpp b/src/test/EventTest.cpp
index 4af0830..c75bcc1 100644
--- a/src/test/EventTest.cpp
+++ b/src/test/EventTest.cpp
@@ -154,44 +154,44 @@ TEST_F(EventManagerTest, EventManagerTest_callback_propagation) {
}
TEST_F(EventManagerTest, EventManagerTest_queue_dispatch) {
- EventManager & event_manager = EventManager::get_instance();
- bool triggered1 = false;
- bool triggered2 = false;
- int test_channel = 1;
-
- // Adjusted to use KeyPressEvent with repeat as the first variable
- EventHandler<KeyPressEvent> key_handler1 = [&](const KeyPressEvent & e) {
- triggered1 = true;
- EXPECT_EQ(e.repeat, false); // Expecting repeat to be false
- EXPECT_EQ(e.key, Keycode::A); // Adjust expected key code
- return false; // Allows propagation
- };
-
- EventHandler<KeyPressEvent> key_handler2 = [&](const KeyPressEvent & e) {
- triggered2 = true;
- EXPECT_EQ(e.repeat, false); // Expecting repeat to be false
- EXPECT_EQ(e.key, Keycode::A); // Adjust expected key code
- return false; // Allows propagation
- };
-
- // Subscribe handlers to KeyPressEvent
- event_manager.subscribe<KeyPressEvent>(key_handler1);
- event_manager.subscribe<KeyPressEvent>(key_handler2, test_channel);
-
- // Queue a KeyPressEvent instead of KeyDownEvent
- event_manager.queue_event<KeyPressEvent>(
- KeyPressEvent{.repeat = false, .key = Keycode::A}); // Adjust event with repeat flag first
-
- event_manager.queue_event<KeyPressEvent>(
- KeyPressEvent{.repeat = false, .key = Keycode::A}, // Adjust event for second subscription
- test_channel);
-
- event_manager.dispatch_events();
-
- EXPECT_TRUE(triggered1);
- EXPECT_TRUE(triggered2);
-}
+ EventManager & event_manager = EventManager::get_instance();
+ bool triggered1 = false;
+ bool triggered2 = false;
+ int test_channel = 1;
+ // Adjusted to use KeyPressEvent with repeat as the first variable
+ EventHandler<KeyPressEvent> key_handler1 = [&](const KeyPressEvent & e) {
+ triggered1 = true;
+ EXPECT_EQ(e.repeat, false); // Expecting repeat to be false
+ EXPECT_EQ(e.key, Keycode::A); // Adjust expected key code
+ return false; // Allows propagation
+ };
+
+ EventHandler<KeyPressEvent> key_handler2 = [&](const KeyPressEvent & e) {
+ triggered2 = true;
+ EXPECT_EQ(e.repeat, false); // Expecting repeat to be false
+ EXPECT_EQ(e.key, Keycode::A); // Adjust expected key code
+ return false; // Allows propagation
+ };
+
+ // Subscribe handlers to KeyPressEvent
+ event_manager.subscribe<KeyPressEvent>(key_handler1);
+ event_manager.subscribe<KeyPressEvent>(key_handler2, test_channel);
+
+ // Queue a KeyPressEvent instead of KeyDownEvent
+ event_manager.queue_event<KeyPressEvent>(KeyPressEvent{
+ .repeat = false, .key = Keycode::A}); // Adjust event with repeat flag first
+
+ event_manager.queue_event<KeyPressEvent>(
+ KeyPressEvent{.repeat = false,
+ .key = Keycode::A}, // Adjust event for second subscription
+ test_channel);
+
+ event_manager.dispatch_events();
+
+ EXPECT_TRUE(triggered1);
+ EXPECT_TRUE(triggered2);
+}
TEST_F(EventManagerTest, EventManagerTest_unsubscribe) {
EventManager & event_manager = EventManager::get_instance();
diff --git a/src/test/inputTest.cpp b/src/test/inputTest.cpp
index 8f3eb48..3a9d341 100644
--- a/src/test/inputTest.cpp
+++ b/src/test/inputTest.cpp
@@ -1,18 +1,17 @@
#include <gtest/gtest.h>
#define protected public
-#include <SDL2/SDL.h>
-#include <SDL2/SDL_keycode.h>
-#include "system/InputSystem.h"
-#include <crepe/ComponentManager.h>
#include "api/EventManager.h"
#include "api/KeyCodes.h"
-#include <gmock/gmock.h>
+#include "system/InputSystem.h"
+#include <SDL2/SDL.h>
+#include <SDL2/SDL_keycode.h>
#include <crepe/ComponentManager.h>
+#include <crepe/api/Button.h>
#include <crepe/api/GameObject.h>
#include <crepe/api/Metadata.h>
#include <crepe/api/Transform.h>
#include <crepe/api/Vector2.h>
-#include <crepe/api/Button.h>
+#include <gmock/gmock.h>
using namespace std;
using namespace std::chrono_literals;
@@ -20,77 +19,75 @@ using namespace crepe;
class InputTest : public ::testing::Test {
public:
- ComponentManager mgr{};
- InputSystem input_system{mgr};
+ ComponentManager mgr{};
+ InputSystem input_system{mgr};
- EventManager& event_manager = EventManager::get_instance();
+ 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);
- 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);
- }
+ 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);
+ 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<MousePressEvent> 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<MousePressEvent>(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);
+ bool mouse_triggered = false;
+ EventHandler<MousePressEvent> 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<MousePressEvent>(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);
+ EXPECT_TRUE(mouse_triggered);
}
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) {
+ // Handle the mouse click event here
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<MouseReleaseEvent>(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);
+ EXPECT_EQ(e.mouse_x, 10);
+ EXPECT_EQ(e.mouse_y, 10);
+ EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE);
+ return false;
+ };
+ event_manager.subscribe<MouseReleaseEvent>(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);
@@ -98,150 +95,148 @@ 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) {
+ // Handle the mouse click event here
function_triggered = true;
- EXPECT_EQ(e.mouse_x, 10);
- EXPECT_EQ(e.mouse_y, 10);
+ 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<MouseMoveEvent>(on_mouse_move);
-
- SDL_Event event;
- SDL_zero(event);
- event.type = SDL_MOUSEMOTION;
- event.motion.x = 10;
- event.motion.y = 10;
+ EXPECT_EQ(e.rel_y, 10);
+ return false;
+ };
+ event_manager.subscribe<MouseMoveEvent>(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);
+ 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<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
- return false;
- };
-
- event_manager.subscribe<KeyPressEvent>(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
+ bool function_triggered = false;
+
+ // Define event handler for KeyPressEvent
+ 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
+ return false;
+ };
+
+ event_manager.subscribe<KeyPressEvent>(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<KeyReleaseEvent> on_key_release = [&](const KeyReleaseEvent& event) {
- function_triggered = true;
- EXPECT_EQ(event.key, Keycode::B);
- return false;
- };
- event_manager.subscribe<KeyReleaseEvent>(on_key_release);
-
- SDL_Event event;
- SDL_zero(event);
- event.type = SDL_KEYUP;
- event.key.keysym.scancode = SDL_SCANCODE_B;
- SDL_PushEvent(&event);
+ EventHandler<KeyReleaseEvent> on_key_release = [&](const KeyReleaseEvent & event) {
+ function_triggered = true;
+ EXPECT_EQ(event.key, Keycode::B);
+ return false;
+ };
+ event_manager.subscribe<KeyReleaseEvent>(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<MouseClickEvent> 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<MouseClickEvent>(on_mouse_click);
-
- this->simulate_mouse_click(10,10,SDL_BUTTON_LEFT);
+ bool on_click_triggered = false;
+ 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);
+ EXPECT_EQ(event.mouse_y, 10);
+ return false;
+ };
+ event_manager.subscribe<MouseClickEvent>(on_mouse_click);
+
+ this->simulate_mouse_click(10, 10, SDL_BUTTON_LEFT);
input_system.update();
event_manager.dispatch_events();
- EXPECT_TRUE(on_click_triggered);
+ 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<Button>();
+ GameObject obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1);
+ auto & button = obj.add_component<Button>();
bool button_clicked = false;
bool hover = false;
- button.active = true;
+ button.active = true;
button.interactable = true;
- button.width = 100;
- button.height = 100;
- std::function<void()> on_click = [&]() {
- button_clicked = true;
- };
+ button.width = 100;
+ button.height = 100;
+ std::function<void()> on_click = [&]() { button_clicked = true; };
button.on_click = on_click;
- button.is_pressed = false;
- button.is_toggle = false;
- this->simulate_mouse_click(101,101, SDL_BUTTON_LEFT);
+ button.is_pressed = false;
+ button.is_toggle = false;
+ this->simulate_mouse_click(101, 101, SDL_BUTTON_LEFT);
input_system.update();
event_manager.dispatch_events();
EXPECT_FALSE(button_clicked);
- this->simulate_mouse_click(10,10, SDL_BUTTON_LEFT);
+ this->simulate_mouse_click(10, 10, SDL_BUTTON_LEFT);
input_system.update();
event_manager.dispatch_events();
EXPECT_TRUE(button_clicked);
}
TEST_F(InputTest, testButtonHover) {
- GameObject obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1);
- auto& button = obj.add_component<Button>();
+ GameObject obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1);
+ auto & button = obj.add_component<Button>();
bool button_clicked = false;
- button.active = true;
+ button.active = true;
button.interactable = true;
- button.width = 100;
- button.height = 100;
- button.is_pressed = false;
- button.is_toggle = false;
+ button.width = 100;
+ button.height = 100;
+ button.is_pressed = false;
+ button.is_toggle = false;
//mouse not on button
SDL_Event event;
- SDL_zero(event);
- event.type = SDL_MOUSEMOTION;
- event.motion.x = 200;
- event.motion.y = 200;
+ SDL_zero(event);
+ event.type = SDL_MOUSEMOTION;
+ event.motion.x = 200;
+ event.motion.y = 200;
event.motion.xrel = 10;
event.motion.yrel = 10;
- SDL_PushEvent(&event);
+ SDL_PushEvent(&event);
input_system.update();
event_manager.dispatch_events();
EXPECT_FALSE(button.hover);
//mouse on button
SDL_Event hover_event;
- SDL_zero(hover_event);
- hover_event.type = SDL_MOUSEMOTION;
- hover_event.motion.x = 10;
- hover_event.motion.y = 10;
+ SDL_zero(hover_event);
+ hover_event.type = SDL_MOUSEMOTION;
+ hover_event.motion.x = 10;
+ hover_event.motion.y = 10;
hover_event.motion.xrel = 10;
hover_event.motion.yrel = 10;
- SDL_PushEvent(&hover_event);
+ SDL_PushEvent(&hover_event);
input_system.update();
event_manager.dispatch_events();
EXPECT_TRUE(button.hover);
diff --git a/src/test/loopTimerTest.cpp b/src/test/loopTimerTest.cpp
index a3b1646..327e577 100644
--- a/src/test/loopTimerTest.cpp
+++ b/src/test/loopTimerTest.cpp
@@ -1,7 +1,7 @@
#define private public
#define protected public
-#include "api/LoopManager.h"
#include "api/LoopTimer.h"
+#include "api/LoopManager.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
@@ -11,22 +11,19 @@ using namespace crepe;
class LoopTimerTest : public ::testing::Test {
public:
-LoopTimer loop_timer = LoopTimer::get_instance();
+ LoopTimer loop_timer = LoopTimer::get_instance();
+
protected:
- void SetUp() override {
- loop_timer.start();
- }
+ void SetUp() override { loop_timer.start(); }
- void TearDown() override {
-
- }
+ void TearDown() override {}
};
TEST_F(LoopTimerTest, TestDeltaTime) {
- auto start_time = std::chrono::steady_clock::now();
-
- loop_timer.update();
- double delta_time = loop_timer.get_delta_time();
+ auto start_time = std::chrono::steady_clock::now();
+
+ loop_timer.update();
+ double delta_time = loop_timer.get_delta_time();
- auto elapsed_time = std::chrono::steady_clock::now() - start_time;
- EXPECT_LE(delta_time, std::chrono::duration<double>(elapsed_time).count());
+ auto elapsed_time = std::chrono::steady_clock::now() - start_time;
+ EXPECT_LE(delta_time, std::chrono::duration<double>(elapsed_time).count());
}