-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmousecontroller_test.go
More file actions
372 lines (310 loc) · 7.66 KB
/
mousecontroller_test.go
File metadata and controls
372 lines (310 loc) · 7.66 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
package main
import (
"testing"
"time"
)
// Legacy macOS key codes for backward compatibility with tests
const (
KeyW int64 = 13
KeyA int64 = 0
KeyS int64 = 1
KeyD int64 = 2
KeyQ int64 = 12
KeyE int64 = 14
KeyZ int64 = 6
KeyX int64 = 7
KeyR int64 = 15
KeyF int64 = 3
KeySpace int64 = 49
)
func TestNewMouseController(t *testing.T) {
mc := NewMouseController()
if mc == nil {
t.Fatal("NewMouseController returned nil")
}
if mc.active {
t.Error("New controller should not be active")
}
}
func TestToggle(t *testing.T) {
mc := NewMouseController()
// Initially inactive
if mc.IsActive() {
t.Error("Controller should start inactive")
}
// Toggle on
mc.Toggle()
if !mc.IsActive() {
t.Error("Controller should be active after toggle")
}
// Toggle off
mc.Toggle()
if mc.IsActive() {
t.Error("Controller should be inactive after second toggle")
}
}
func TestToggleResetsKeyState(t *testing.T) {
mc := NewMouseController()
mc.Toggle() // Activate
// Set some key states
mc.HandleKeyDown(KeyW)
mc.HandleKeyDown(KeyA)
// Toggle off should reset states
mc.Toggle()
// Verify keys are reset
mc.mu.Lock()
if mc.keyW || mc.keyA || mc.keyS || mc.keyD {
t.Error("Toggle off should reset all key states")
}
mc.mu.Unlock()
}
func TestHandleKeyDownWhenInactive(t *testing.T) {
mc := NewMouseController()
// Should return false when inactive
if mc.HandleKeyDown(KeyW) {
t.Error("HandleKeyDown should return false when inactive")
}
}
func TestHandleKeyDownWhenActive(t *testing.T) {
mc := NewMouseController()
mc.Toggle() // Activate
tests := []struct {
keycode int64
name string
}{
{KeyW, "W"},
{KeyA, "A"},
{KeyS, "S"},
{KeyD, "D"},
{KeyQ, "Q"},
{KeyE, "E"},
{KeyZ, "Z"},
{KeyX, "X"},
{KeySpace, "Space"},
{KeyR, "R"},
{KeyF, "F"},
}
for _, tt := range tests {
if !mc.HandleKeyDown(tt.keycode) {
t.Errorf("HandleKeyDown(%s) should return true when active", tt.name)
}
}
}
func TestHandleKeyUpWhenInactive(t *testing.T) {
mc := NewMouseController()
if mc.HandleKeyUp(KeyW) {
t.Error("HandleKeyUp should return false when inactive")
}
}
func TestHandleKeyUpWhenActive(t *testing.T) {
mc := NewMouseController()
mc.Toggle() // Activate
// First press then release
mc.HandleKeyDown(KeyW)
if !mc.HandleKeyUp(KeyW) {
t.Error("HandleKeyUp(W) should return true when active")
}
}
func TestGetMovementWhenInactive(t *testing.T) {
mc := NewMouseController()
dx, dy := mc.GetMovement()
if dx != 0 || dy != 0 {
t.Errorf("GetMovement should return (0,0) when inactive, got (%f,%f)", dx, dy)
}
}
func TestGetMovementNoKeys(t *testing.T) {
mc := NewMouseController()
mc.Toggle() // Activate
dx, dy := mc.GetMovement()
if dx != 0 || dy != 0 {
t.Errorf("GetMovement should return (0,0) with no keys pressed, got (%f,%f)", dx, dy)
}
}
func TestGetMovementCardinalDirections(t *testing.T) {
tests := []struct {
name string
keycode int64
expectDx float64
expectDy float64
}{
{"W - Up", KeyW, 0, -1},
{"S - Down", KeyS, 0, 1},
{"A - Left", KeyA, -1, 0},
{"D - Right", KeyD, 1, 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mc := NewMouseController()
mc.Toggle()
mc.HandleKeyDown(tt.keycode)
dx, dy := mc.GetMovement()
// At base speed, check direction
if tt.expectDx < 0 && dx >= 0 {
t.Errorf("Expected negative dx, got %f", dx)
}
if tt.expectDx > 0 && dx <= 0 {
t.Errorf("Expected positive dx, got %f", dx)
}
if tt.expectDy < 0 && dy >= 0 {
t.Errorf("Expected negative dy, got %f", dy)
}
if tt.expectDy > 0 && dy <= 0 {
t.Errorf("Expected positive dy, got %f", dy)
}
if tt.expectDx == 0 && dx != 0 {
t.Errorf("Expected dx=0, got %f", dx)
}
if tt.expectDy == 0 && dy != 0 {
t.Errorf("Expected dy=0, got %f", dy)
}
})
}
}
func TestGetMovementDiagonalDirections(t *testing.T) {
tests := []struct {
name string
keycode int64
expectDx float64
expectDy float64
}{
{"Q - Up-Left", KeyQ, -1, -1},
{"E - Up-Right", KeyE, 1, -1},
{"Z - Down-Left", KeyZ, -1, 1},
{"X - Down-Right", KeyX, 1, 1},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mc := NewMouseController()
mc.Toggle()
mc.HandleKeyDown(tt.keycode)
dx, dy := mc.GetMovement()
// Check directions (diagonal keys produce ~0.707 factor)
if tt.expectDx < 0 && dx >= 0 {
t.Errorf("Expected negative dx, got %f", dx)
}
if tt.expectDx > 0 && dx <= 0 {
t.Errorf("Expected positive dx, got %f", dx)
}
if tt.expectDy < 0 && dy >= 0 {
t.Errorf("Expected negative dy, got %f", dy)
}
if tt.expectDy > 0 && dy <= 0 {
t.Errorf("Expected positive dy, got %f", dy)
}
})
}
}
func TestGetMovementDiagonalNormalization(t *testing.T) {
mc := NewMouseController()
mc.Toggle()
// Press W and D together (diagonal via cardinal keys)
mc.HandleKeyDown(KeyW)
mc.HandleKeyDown(KeyD)
dx, dy := mc.GetMovement()
// Combined cardinal should be normalized (0.707 factor)
// At base speed = 1.0, diagonal should be ~0.707 each direction
expectedMagnitude := 0.707 * baseSpeed
tolerance := 0.01
if dx < expectedMagnitude-tolerance || dx > expectedMagnitude+tolerance {
t.Errorf("Diagonal dx should be ~%f, got %f", expectedMagnitude, dx)
}
if dy > -expectedMagnitude+tolerance || dy < -expectedMagnitude-tolerance {
t.Errorf("Diagonal dy should be ~%f, got %f", -expectedMagnitude, dy)
}
}
func TestAcceleration(t *testing.T) {
mc := NewMouseController()
mc.Toggle()
mc.HandleKeyDown(KeyD) // Move right
// First call - should be at base speed
dx1, _ := mc.GetMovement()
// Wait and check acceleration
time.Sleep(200 * time.Millisecond)
dx2, _ := mc.GetMovement()
if dx2 <= dx1 {
t.Errorf("Speed should increase over time: initial=%f, after 200ms=%f", dx1, dx2)
}
}
func TestAccelerationResetOnDirectionChange(t *testing.T) {
mc := NewMouseController()
mc.Toggle()
// Move right for a bit
mc.HandleKeyDown(KeyD)
mc.GetMovement()
time.Sleep(100 * time.Millisecond)
dxBefore, _ := mc.GetMovement()
// Change direction
mc.HandleKeyUp(KeyD)
mc.HandleKeyDown(KeyA)
// Speed should reset to base
dxAfter, _ := mc.GetMovement()
// dxAfter should be negative (left) and close to base speed
if dxAfter >= 0 {
t.Error("Direction should have changed to left")
}
if -dxAfter > dxBefore {
t.Errorf("Speed should reset on direction change: before=%f, after=%f", dxBefore, -dxAfter)
}
}
func TestSpaceKeyLeftClick(t *testing.T) {
mc := NewMouseController()
mc.Toggle()
// Press space
mc.HandleKeyDown(KeySpace)
mc.mu.Lock()
leftDown := mc.leftDown
mc.mu.Unlock()
if !leftDown {
t.Error("Space should set leftDown to true")
}
// Release space
mc.HandleKeyUp(KeySpace)
mc.mu.Lock()
leftDown = mc.leftDown
mc.mu.Unlock()
if leftDown {
t.Error("Releasing space should set leftDown to false")
}
}
func TestUnknownKeyReturnsTrue(t *testing.T) {
mc := NewMouseController()
mc.Toggle()
// Unknown key should return false (not handled)
unknownKey := int64(999)
if mc.HandleKeyDown(unknownKey) {
t.Error("Unknown key should return false")
}
}
func TestConcurrentAccess(t *testing.T) {
mc := NewMouseController()
mc.Toggle()
done := make(chan bool)
// Goroutine pressing keys
go func() {
for i := 0; i < 100; i++ {
mc.HandleKeyDown(KeyW)
mc.HandleKeyUp(KeyW)
}
done <- true
}()
// Goroutine getting movement
go func() {
for i := 0; i < 100; i++ {
mc.GetMovement()
}
done <- true
}()
// Goroutine toggling
go func() {
for i := 0; i < 10; i++ {
mc.Toggle()
time.Sleep(time.Millisecond)
}
done <- true
}()
// Wait for all
<-done
<-done
<-done
// If we get here without deadlock or panic, test passes
}