1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
#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 <crepe/ComponentManager.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>
using namespace std;
using namespace std::chrono_literals;
using namespace crepe;
class InputTest : public ::testing::Test {
public:
ComponentManager mgr{};
InputSystem input_system{mgr};
EventManager& event_manager = EventManager::get_instance();
protected:
void SetUp() override {
event_manager.clear();
}
void TearDown() 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_click = [&](const MousePressEvent& event) {
// Handle the mouse click event here
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_click);
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);
EXPECT_TRUE(mouse_triggered);
}
TEST_F(InputTest, MouseUp) {
EventHandler<MouseReleaseEvent> on_mouse_click = [&](const MouseReleaseEvent& event) {
// Handle the mouse click event here
EXPECT_EQ(event.mouse_x, 10);
EXPECT_EQ(event.mouse_y, 10);
EXPECT_EQ(event.button, MouseButton::LEFT_MOUSE);
return false;
};
event_manager.subscribe<MouseReleaseEvent>(on_mouse_click);
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);
}
TEST_F(InputTest, MouseUp) {
EventHandler<MouseReleaseEvent> on_mouse_click = [&](const MouseReleaseEvent& event) {
// Handle the mouse click event here
EXPECT_EQ(event.mouse_x, 10);
EXPECT_EQ(event.mouse_y, 10);
EXPECT_EQ(event.button, MouseButton::LEFT_MOUSE);
return false;
};
event_manager.subscribe<MouseReleaseEvent>(on_mouse_click);
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);
}
TEST_F(InputTest, KeyDown) {
EventHandler<KeyPressEvent> on_mouse_click = [&](const KeyPressEvent& event) {
// Handle the mouse click event here
EXPECT_EQ(event.key, Keycode::B);
EXPECT_EQ(event.repeat, true);
return false;
};
event_manager.subscribe<KeyPressEvent>(on_mouse_click);
SDL_Event event;
SDL_zero(event);
event.type = SDL_KEYUP;
event.button.button = SDL_BUTTON_LEFT;
event.key.repeat = 1;
SDL_PushEvent(&event);
}
TEST_F(InputTest, KeyUp) {
EventHandler<KeyReleaseEvent> on_mouse_click = [&](const KeyReleaseEvent& event) {
// Handle the mouse click event here
EXPECT_EQ(event.key, Keycode::B);
return false;
};
event_manager.subscribe<KeyReleaseEvent>(on_mouse_click);
SDL_Event event;
SDL_zero(event);
event.type = SDL_KEYUP;
event.button.button = SDL_BUTTON_LEFT;
SDL_PushEvent(&event);
}
TEST_F(InputTest, KeyClick) {
GameObject obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1);
Button test_button;
bool button_clicked = false;
test_button.active = true;
test_button.width = 100;
test_button.height = 100;
std::function<void()> on_click = [&]() {
button_clicked = true;
};
test_button.on_click = on_click;
test_button.is_pressed = false;
test_button.is_toggle = false;
obj.add_component<Button>();
EventHandler<KeyReleaseEvent> on_mouse_click = [&](const KeyReleaseEvent& event) {
EXPECT_EQ(event.key, Keycode::B);
return false;
};
event_manager.subscribe<KeyReleaseEvent>(on_mouse_click);
SDL_Event event;
SDL_zero(event);
event.type = SDL_KEYUP;
event.button.button = SDL_BUTTON_LEFT;
SDL_PushEvent(&event);
}
TEST_F(InputTest, testButton) {
GameObject obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1);
Button test_button;
bool button_clicked = false;
test_button.active = true;
test_button.width = 100;
test_button.height = 100;
std::function<void()> on_click = [&]() {
button_clicked = true;
};
test_button.on_click = on_click;
test_button.is_pressed = false;
test_button.is_toggle = false;
obj.add_component<Button>();
this->simulate_mouse_click(10,10,MouseButton::LEFT_MOUSE);
}
|