-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_input.cpp
More file actions
560 lines (431 loc) · 16 KB
/
Copy pathtest_input.cpp
File metadata and controls
560 lines (431 loc) · 16 KB
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
#include "catch.hpp"
#include "../src/input/InputManager.hpp"
using namespace Caffeine;
using namespace Caffeine::Input;
// ============================================================================
// ActionState defaults
// ============================================================================
TEST_CASE("ActionState - Default is all false", "[input][action]") {
InputManager mgr;
auto state = mgr.actionState(Action::Jump);
REQUIRE(state.pressed == false);
REQUIRE(state.justPressed == false);
REQUIRE(state.justReleased == false);
}
// ============================================================================
// Key press -> action state transitions
// ============================================================================
TEST_CASE("ActionState - justPressed on first frame of key down", "[input][action]") {
InputManager mgr;
mgr.bind(Action::Jump, Binding::fromKey(Key::Space));
mgr.beginFrame();
mgr.injectKeyDown(Key::Space);
mgr.endFrame();
auto state = mgr.actionState(Action::Jump);
REQUIRE(state.pressed == true);
REQUIRE(state.justPressed == true);
REQUIRE(state.justReleased == false);
}
TEST_CASE("ActionState - pressed but not justPressed on second frame", "[input][action]") {
InputManager mgr;
mgr.bind(Action::Jump, Binding::fromKey(Key::Space));
// Frame 1: press
mgr.beginFrame();
mgr.injectKeyDown(Key::Space);
mgr.endFrame();
// Frame 2: still held
mgr.beginFrame();
mgr.endFrame();
auto state = mgr.actionState(Action::Jump);
REQUIRE(state.pressed == true);
REQUIRE(state.justPressed == false);
REQUIRE(state.justReleased == false);
}
TEST_CASE("ActionState - justReleased on frame after key up", "[input][action]") {
InputManager mgr;
mgr.bind(Action::Jump, Binding::fromKey(Key::Space));
// Frame 1: press
mgr.beginFrame();
mgr.injectKeyDown(Key::Space);
mgr.endFrame();
// Frame 2: release
mgr.beginFrame();
mgr.injectKeyUp(Key::Space);
mgr.endFrame();
auto state = mgr.actionState(Action::Jump);
REQUIRE(state.pressed == false);
REQUIRE(state.justPressed == false);
REQUIRE(state.justReleased == true);
}
TEST_CASE("ActionState - all false after release frame passes", "[input][action]") {
InputManager mgr;
mgr.bind(Action::Jump, Binding::fromKey(Key::Space));
// Frame 1: press
mgr.beginFrame();
mgr.injectKeyDown(Key::Space);
mgr.endFrame();
// Frame 2: release
mgr.beginFrame();
mgr.injectKeyUp(Key::Space);
mgr.endFrame();
// Frame 3: idle
mgr.beginFrame();
mgr.endFrame();
auto state = mgr.actionState(Action::Jump);
REQUIRE(state.pressed == false);
REQUIRE(state.justPressed == false);
REQUIRE(state.justReleased == false);
}
// ============================================================================
// Multiple bindings per action
// ============================================================================
TEST_CASE("Action - Multiple bindings, either activates", "[input][binding]") {
InputManager mgr;
mgr.bind(Action::Jump, Binding::fromKey(Key::Space));
mgr.bind(Action::Jump, Binding::fromKey(Key::W));
mgr.beginFrame();
mgr.injectKeyDown(Key::W);
mgr.endFrame();
auto state = mgr.actionState(Action::Jump);
REQUIRE(state.pressed == true);
REQUIRE(state.justPressed == true);
}
TEST_CASE("Action - Multiple bindings, both held, release one stays pressed", "[input][binding]") {
InputManager mgr;
mgr.bind(Action::Jump, Binding::fromKey(Key::Space));
mgr.bind(Action::Jump, Binding::fromKey(Key::W));
// Frame 1: both down
mgr.beginFrame();
mgr.injectKeyDown(Key::Space);
mgr.injectKeyDown(Key::W);
mgr.endFrame();
// Frame 2: release one
mgr.beginFrame();
mgr.injectKeyUp(Key::W);
mgr.endFrame();
auto state = mgr.actionState(Action::Jump);
REQUIRE(state.pressed == true);
REQUIRE(state.justReleased == false);
}
// ============================================================================
// Mouse button bindings
// ============================================================================
TEST_CASE("Action - Mouse button binding", "[input][mouse]") {
InputManager mgr;
mgr.bind(Action::Attack, Binding::fromMouseButton(MouseButton::Left));
mgr.beginFrame();
mgr.injectMouseButtonDown(MouseButton::Left);
mgr.endFrame();
auto state = mgr.actionState(Action::Attack);
REQUIRE(state.pressed == true);
REQUIRE(state.justPressed == true);
}
TEST_CASE("Mouse button release triggers justReleased", "[input][mouse]") {
InputManager mgr;
mgr.bind(Action::Attack, Binding::fromMouseButton(MouseButton::Left));
mgr.beginFrame();
mgr.injectMouseButtonDown(MouseButton::Left);
mgr.endFrame();
mgr.beginFrame();
mgr.injectMouseButtonUp(MouseButton::Left);
mgr.endFrame();
auto state = mgr.actionState(Action::Attack);
REQUIRE(state.pressed == false);
REQUIRE(state.justReleased == true);
}
// ============================================================================
// Gamepad button bindings
// ============================================================================
TEST_CASE("Action - Gamepad button binding", "[input][gamepad]") {
InputManager mgr;
mgr.bind(Action::Jump, Binding::fromGamepadButton(GamepadButton::A));
mgr.beginFrame();
mgr.injectGamepadButtonDown(GamepadButton::A);
mgr.endFrame();
auto state = mgr.actionState(Action::Jump);
REQUIRE(state.pressed == true);
REQUIRE(state.justPressed == true);
}
// ============================================================================
// Mouse position and delta
// ============================================================================
TEST_CASE("Mouse position tracks injectMouseMove", "[input][mouse]") {
InputManager mgr;
mgr.beginFrame();
mgr.injectMouseMove(100.0f, 200.0f);
mgr.endFrame();
Vec2 pos = mgr.mousePosition();
REQUIRE(pos.x == Approx(100.0f));
REQUIRE(pos.y == Approx(200.0f));
}
TEST_CASE("Mouse delta is difference between frames", "[input][mouse]") {
InputManager mgr;
mgr.beginFrame();
mgr.injectMouseMove(100.0f, 200.0f);
mgr.endFrame();
mgr.beginFrame();
mgr.injectMouseMove(150.0f, 220.0f);
mgr.endFrame();
Vec2 delta = mgr.mouseDelta();
REQUIRE(delta.x == Approx(50.0f));
REQUIRE(delta.y == Approx(20.0f));
}
// ============================================================================
// Axis state (digital keys -> axis value)
// ============================================================================
TEST_CASE("AxisState - Default is zero", "[input][axis]") {
InputManager mgr;
auto state = mgr.axisState(Axis::MoveX);
REQUIRE(state.value == Approx(0.0f));
REQUIRE(state.delta == Approx(0.0f));
}
TEST_CASE("AxisState - Positive binding gives +1.0", "[input][axis]") {
InputManager mgr;
mgr.bindAxis(Axis::MoveX,
Binding::fromKey(Key::A), // negative
Binding::fromKey(Key::D)); // positive
mgr.beginFrame();
mgr.injectKeyDown(Key::D);
mgr.endFrame();
auto state = mgr.axisState(Axis::MoveX);
REQUIRE(state.value == Approx(1.0f));
}
TEST_CASE("AxisState - Negative binding gives -1.0", "[input][axis]") {
InputManager mgr;
mgr.bindAxis(Axis::MoveX,
Binding::fromKey(Key::A),
Binding::fromKey(Key::D));
mgr.beginFrame();
mgr.injectKeyDown(Key::A);
mgr.endFrame();
auto state = mgr.axisState(Axis::MoveX);
REQUIRE(state.value == Approx(-1.0f));
}
TEST_CASE("AxisState - Both keys cancel to 0.0", "[input][axis]") {
InputManager mgr;
mgr.bindAxis(Axis::MoveX,
Binding::fromKey(Key::A),
Binding::fromKey(Key::D));
mgr.beginFrame();
mgr.injectKeyDown(Key::A);
mgr.injectKeyDown(Key::D);
mgr.endFrame();
auto state = mgr.axisState(Axis::MoveX);
REQUIRE(state.value == Approx(0.0f));
}
TEST_CASE("AxisState - Gamepad axis value pass-through", "[input][axis]") {
InputManager mgr;
mgr.bindAxis(Axis::LookX,
Binding::fromGamepadAxis(GamepadAxis::RightX),
Binding::fromGamepadAxis(GamepadAxis::RightX));
mgr.beginFrame();
mgr.injectGamepadAxis(GamepadAxis::RightX, 0.75f);
mgr.endFrame();
auto state = mgr.axisState(Axis::LookX);
REQUIRE(state.value == Approx(0.75f));
}
TEST_CASE("AxisState - Delta tracks change between frames", "[input][axis]") {
InputManager mgr;
mgr.bindAxis(Axis::MoveX,
Binding::fromKey(Key::A),
Binding::fromKey(Key::D));
// Frame 1: idle
mgr.beginFrame();
mgr.endFrame();
// Frame 2: press D -> value goes from 0 to 1
mgr.beginFrame();
mgr.injectKeyDown(Key::D);
mgr.endFrame();
auto state = mgr.axisState(Axis::MoveX);
REQUIRE(state.delta == Approx(1.0f));
}
// ============================================================================
// Binding management
// ============================================================================
TEST_CASE("clearBindings removes action bindings", "[input][binding]") {
InputManager mgr;
mgr.bind(Action::Jump, Binding::fromKey(Key::Space));
mgr.clearBindings(Action::Jump);
mgr.beginFrame();
mgr.injectKeyDown(Key::Space);
mgr.endFrame();
auto state = mgr.actionState(Action::Jump);
REQUIRE(state.pressed == false);
}
TEST_CASE("clearAllBindings removes all action bindings", "[input][binding]") {
InputManager mgr;
mgr.bind(Action::Jump, Binding::fromKey(Key::Space));
mgr.bind(Action::Attack, Binding::fromMouseButton(MouseButton::Left));
mgr.clearAllBindings();
mgr.beginFrame();
mgr.injectKeyDown(Key::Space);
mgr.injectMouseButtonDown(MouseButton::Left);
mgr.endFrame();
REQUIRE(mgr.actionState(Action::Jump).pressed == false);
REQUIRE(mgr.actionState(Action::Attack).pressed == false);
}
TEST_CASE("resetToDefaults restores WASD + arrow defaults", "[input][binding]") {
InputManager mgr;
mgr.clearAllBindings();
mgr.resetToDefaults();
mgr.beginFrame();
mgr.injectKeyDown(Key::W);
mgr.endFrame();
REQUIRE(mgr.actionState(Action::MoveUp).pressed == true);
}
TEST_CASE("bind respects MAX_BINDINGS_PER_ACTION limit", "[input][binding]") {
InputManager mgr;
mgr.clearBindings(Action::Jump);
mgr.bind(Action::Jump, Binding::fromKey(Key::Space));
mgr.bind(Action::Jump, Binding::fromKey(Key::W));
mgr.bind(Action::Jump, Binding::fromKey(Key::Up));
mgr.bind(Action::Jump, Binding::fromKey(Key::Return));
// 5th binding should be silently ignored (MAX=4)
mgr.bind(Action::Jump, Binding::fromKey(Key::A));
mgr.beginFrame();
mgr.injectKeyDown(Key::A);
mgr.endFrame();
REQUIRE(mgr.actionState(Action::Jump).pressed == false);
}
// ============================================================================
// Callbacks (std::function)
// ============================================================================
TEST_CASE("onActionPressed callback fires on justPressed", "[input][callback]") {
InputManager mgr;
mgr.bind(Action::Jump, Binding::fromKey(Key::Space));
Action firedAction = Action::Count;
mgr.onActionPressed = [&](Action a) { firedAction = a; };
mgr.beginFrame();
mgr.injectKeyDown(Key::Space);
mgr.endFrame();
REQUIRE(firedAction == Action::Jump);
}
TEST_CASE("onActionReleased callback fires on justReleased", "[input][callback]") {
InputManager mgr;
mgr.bind(Action::Jump, Binding::fromKey(Key::Space));
Action firedAction = Action::Count;
mgr.onActionReleased = [&](Action a) { firedAction = a; };
mgr.beginFrame();
mgr.injectKeyDown(Key::Space);
mgr.endFrame();
mgr.beginFrame();
mgr.injectKeyUp(Key::Space);
mgr.endFrame();
REQUIRE(firedAction == Action::Jump);
}
// ============================================================================
// Callbacks (virtual interface)
// ============================================================================
class TestInputHandler : public InputManager::IInputCallbacks {
public:
Action lastPressed = Action::Count;
Action lastReleased = Action::Count;
int pressCount = 0;
int releaseCount = 0;
void onActionPressed(Action action) override {
lastPressed = action;
++pressCount;
}
void onActionReleased(Action action) override {
lastReleased = action;
++releaseCount;
}
};
TEST_CASE("IInputCallbacks - onActionPressed fires via interface", "[input][callback]") {
InputManager mgr;
mgr.bind(Action::Attack, Binding::fromKey(Key::E));
TestInputHandler handler;
mgr.setCallbackHandler(&handler);
mgr.beginFrame();
mgr.injectKeyDown(Key::E);
mgr.endFrame();
REQUIRE(handler.lastPressed == Action::Attack);
REQUIRE(handler.pressCount == 1);
}
TEST_CASE("IInputCallbacks - onActionReleased fires via interface", "[input][callback]") {
InputManager mgr;
mgr.bind(Action::Attack, Binding::fromKey(Key::E));
TestInputHandler handler;
mgr.setCallbackHandler(&handler);
mgr.beginFrame();
mgr.injectKeyDown(Key::E);
mgr.endFrame();
mgr.beginFrame();
mgr.injectKeyUp(Key::E);
mgr.endFrame();
REQUIRE(handler.lastReleased == Action::Attack);
REQUIRE(handler.releaseCount == 1);
}
TEST_CASE("Callbacks do not fire when no state transition", "[input][callback]") {
InputManager mgr;
mgr.bind(Action::Jump, Binding::fromKey(Key::Space));
TestInputHandler handler;
mgr.setCallbackHandler(&handler);
// Frame 1: press
mgr.beginFrame();
mgr.injectKeyDown(Key::Space);
mgr.endFrame();
REQUIRE(handler.pressCount == 1);
// Frame 2: still held - no new press callback
mgr.beginFrame();
mgr.endFrame();
REQUIRE(handler.pressCount == 1);
}
// ============================================================================
// Independent actions don't interfere
// ============================================================================
TEST_CASE("Different actions are independent", "[input][action]") {
InputManager mgr;
mgr.bind(Action::Jump, Binding::fromKey(Key::Space));
mgr.bind(Action::Attack, Binding::fromKey(Key::E));
mgr.beginFrame();
mgr.injectKeyDown(Key::Space);
mgr.endFrame();
REQUIRE(mgr.actionState(Action::Jump).pressed == true);
REQUIRE(mgr.actionState(Action::Attack).pressed == false);
}
// ============================================================================
// Default bindings exist after construction
// ============================================================================
TEST_CASE("Default bindings - MoveUp bound to W", "[input][defaults]") {
InputManager mgr;
mgr.beginFrame();
mgr.injectKeyDown(Key::W);
mgr.endFrame();
REQUIRE(mgr.actionState(Action::MoveUp).pressed == true);
}
TEST_CASE("Default bindings - MoveDown bound to S", "[input][defaults]") {
InputManager mgr;
mgr.beginFrame();
mgr.injectKeyDown(Key::S);
mgr.endFrame();
REQUIRE(mgr.actionState(Action::MoveDown).pressed == true);
}
TEST_CASE("Default bindings - MoveLeft bound to A", "[input][defaults]") {
InputManager mgr;
mgr.beginFrame();
mgr.injectKeyDown(Key::A);
mgr.endFrame();
REQUIRE(mgr.actionState(Action::MoveLeft).pressed == true);
}
TEST_CASE("Default bindings - MoveRight bound to D", "[input][defaults]") {
InputManager mgr;
mgr.beginFrame();
mgr.injectKeyDown(Key::D);
mgr.endFrame();
REQUIRE(mgr.actionState(Action::MoveRight).pressed == true);
}
TEST_CASE("Default bindings - Jump bound to Space", "[input][defaults]") {
InputManager mgr;
mgr.beginFrame();
mgr.injectKeyDown(Key::Space);
mgr.endFrame();
REQUIRE(mgr.actionState(Action::Jump).pressed == true);
}
TEST_CASE("Default bindings - Pause bound to Escape", "[input][defaults]") {
InputManager mgr;
mgr.beginFrame();
mgr.injectKeyDown(Key::Escape);
mgr.endFrame();
REQUIRE(mgr.actionState(Action::Pause).pressed == true);
}