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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
#include <gtest/gtest.h>
#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 <SDL2/SDL.h>
#include <SDL2/SDL_keycode.h>
#include <crepe/api/Button.h>
#include <crepe/api/Camera.h>
#include <crepe/api/GameObject.h>
#include <crepe/api/Metadata.h>
#include <crepe/api/Transform.h>
#include <crepe/api/Vector2.h>
#include <gmock/gmock.h>
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 { 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<Camera>(
ivec2{0, 0}, vec2{500, 500}, Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f});
camera.active = true;
bool mouse_triggered = false;
EventHandler<MousePressEvent> on_mouse_down = [&](const MousePressEvent & event) {
mouse_triggered = true;
//middle of the screen = 0,0
EXPECT_EQ(event.mouse_pos.x, 0);
EXPECT_EQ(event.mouse_pos.y, 0);
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;
// 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<Camera>(
ivec2{0, 0}, vec2{500, 500}, Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f});
camera.active = true;
bool function_triggered = false;
EventHandler<MouseReleaseEvent> on_mouse_release = [&](const MouseReleaseEvent & e) {
function_triggered = true;
EXPECT_EQ(e.mouse_pos.x, 0);
EXPECT_EQ(e.mouse_pos.y, 0);
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 = 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<Camera>(
ivec2{0, 0}, vec2{500, 500}, Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f});
camera.active = true;
bool function_triggered = false;
EventHandler<MouseMoveEvent> on_mouse_move = [&](const MouseMoveEvent & e) {
function_triggered = true;
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);
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<Camera>(
ivec2{0, 0}, vec2{500, 500}, Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f});
camera.active = true;
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) {
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 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);
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<Camera>(
ivec2{0, 0}, vec2{500, 500}, Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f});
camera.active = true;
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_pos.x, 0);
EXPECT_EQ(event.mouse_pos.y, 0);
return false;
};
event_manager.subscribe<MouseClickEvent>(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<Camera>(
ivec2{0, 0}, vec2{500, 500}, 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<void()> on_click = [&]() { button_clicked = true; };
auto & button
= button_obj.add_component<Button>(vec2{100, 100}, vec2{0, 0}, on_click, false);
bool hover = false;
button.active = true;
this->simulate_mouse_click(999, 999, SDL_BUTTON_LEFT);
input_system.update();
event_manager.dispatch_events();
EXPECT_FALSE(button_clicked);
this->simulate_mouse_click(250, 250, SDL_BUTTON_LEFT);
input_system.update();
event_manager.dispatch_events();
EXPECT_TRUE(button_clicked);
}
TEST_F(InputTest, testButtonHover) {
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;
GameObject button_obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1);
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.active = true;
// Mouse not on button
SDL_Event event;
SDL_zero(event);
event.type = SDL_MOUSEMOTION;
event.motion.x = 700;
event.motion.y = 700;
event.motion.xrel = 10;
event.motion.yrel = 10;
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 = 250;
hover_event.motion.y = 250;
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);
}
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);
}
TEST_F(InputTest, KeyboardStateTest) {
SDLContext& sdl_context = this->mediator.sdl_context;
// Simulate pressing a key
SDL_Event key_down_event;
SDL_zero(key_down_event);
key_down_event.type = SDL_KEYDOWN;
key_down_event.key.keysym.scancode = SDL_SCANCODE_A;
key_down_event.key.keysym.sym = SDL_SCANCODE_A;
SDL_PushEvent(&key_down_event);
// Check the keyboard state
auto keyboard_state = sdl_context.get_keyboard_state();
// Verify the state of the 'A' key
EXPECT_TRUE(keyboard_state[Keycode::A]);
// Simulate releasing a key
SDL_Event key_up_event;
SDL_zero(key_up_event);
key_up_event.type = SDL_KEYUP;
key_up_event.key.keysym.scancode = SDL_SCANCODE_A; // Simulate releasing 'A' key
SDL_PushEvent(&key_up_event);
// Check the keyboard state again
keyboard_state = sdl_context.get_keyboard_state();
// Verify the state of the 'A' key
EXPECT_FALSE(keyboard_state[Keycode::A]);
}
|