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
|
package tui
import (
"testing"
)
type mockFocusable struct {
id string
focusable bool
focused bool
focusCalls int
blurCalls int
lastEvent Event
handled bool
}
func newMockFocusable(id string, focusable bool) *mockFocusable {
return &mockFocusable{
id: id,
focusable: focusable,
}
}
func (m *mockFocusable) IsFocusable() bool {
return m.focusable
}
func (m *mockFocusable) IsTabStop() bool {
return m.focusable
}
func (m *mockFocusable) HandleEvent(event Event) bool {
m.lastEvent = event
return m.handled
}
func (m *mockFocusable) Focus() {
m.focused = true
m.focusCalls++
}
func (m *mockFocusable) Blur() {
m.focused = false
m.blurCalls++
}
func (m *mockFocusable) IsFocused() bool {
return m.focused
}
func registerAll(fm *focusManager, elements ...*mockFocusable) {
for _, elem := range elements {
fm.Register(elem)
}
}
func TestFocusManager_IsFocused(t *testing.T) {
type tc struct {
elements []*mockFocusable
focusIndex int
checkIndex int
expectFocused bool
}
tests := map[string]tc{
"focused element returns true": {
elements: []*mockFocusable{
newMockFocusable("a", true),
newMockFocusable("b", true),
},
focusIndex: 0,
checkIndex: 0,
expectFocused: true,
},
"unfocused element returns false": {
elements: []*mockFocusable{
newMockFocusable("a", true),
newMockFocusable("b", true),
},
focusIndex: 0,
checkIndex: 1,
expectFocused: false,
},
"no focus returns false": {
elements: []*mockFocusable{
newMockFocusable("a", true),
},
focusIndex: -1,
checkIndex: 0,
expectFocused: false,
},
"unregistered element returns false": {
elements: []*mockFocusable{
newMockFocusable("a", true),
},
focusIndex: 0,
checkIndex: -1,
expectFocused: false,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
fm := newFocusManager()
registerAll(fm, tt.elements...)
if tt.focusIndex >= 0 {
fm.SetFocus(tt.elements[tt.focusIndex])
}
var check Focusable
if tt.checkIndex >= 0 {
check = tt.elements[tt.checkIndex]
} else {
check = newMockFocusable("unregistered", true)
}
got := fm.IsFocused(check)
if got != tt.expectFocused {
t.Errorf("IsFocused() = %v, want %v", got, tt.expectFocused)
}
})
}
}
func TestFocusManager_NoAutoFocus(t *testing.T) {
type tc struct {
elements []*mockFocusable
}
tests := map[string]tc{
"single focusable element": {
elements: []*mockFocusable{
newMockFocusable("a", true),
},
},
"multiple focusable elements": {
elements: []*mockFocusable{
newMockFocusable("a", true),
newMockFocusable("b", true),
newMockFocusable("c", true),
},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
fm := newFocusManager()
registerAll(fm, tt.elements...)
if fm.Focused() != nil {
t.Error("Focused() should return nil after registration (no auto-focus)")
}
for _, elem := range tt.elements {
if elem.focusCalls != 0 {
t.Errorf("Element %q should not have Focus() called, got %d calls", elem.id, elem.focusCalls)
}
}
})
}
}
func TestNewFocusManager_NoFocusableElements(t *testing.T) {
type tc struct {
elements []*mockFocusable
}
tests := map[string]tc{
"empty": {
elements: []*mockFocusable{},
},
"all non-focusable": {
elements: []*mockFocusable{
newMockFocusable("a", false),
newMockFocusable("b", false),
},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
fm := newFocusManager()
registerAll(fm, tt.elements...)
if fm.Focused() != nil {
t.Error("Focused() should return nil when no focusable elements")
}
})
}
}
func TestFocusManager_Next(t *testing.T) {
type tc struct {
elements []*mockFocusable
nextCalls int
expectedFocusedID string
}
tests := map[string]tc{
"first Next focuses first element": {
elements: []*mockFocusable{
newMockFocusable("a", true),
newMockFocusable("b", true),
newMockFocusable("c", true),
},
nextCalls: 1,
expectedFocusedID: "a",
},
"next from first to second": {
elements: []*mockFocusable{
newMockFocusable("a", true),
newMockFocusable("b", true),
newMockFocusable("c", true),
},
nextCalls: 2,
expectedFocusedID: "b",
},
"wraps to beginning": {
elements: []*mockFocusable{
newMockFocusable("a", true),
newMockFocusable("b", true),
},
nextCalls: 3,
expectedFocusedID: "a",
},
"skips non-focusable": {
elements: []*mockFocusable{
newMockFocusable("a", true),
newMockFocusable("b", false),
newMockFocusable("c", true),
},
nextCalls: 2,
expectedFocusedID: "c",
},
"full cycle through all": {
elements: []*mockFocusable{
newMockFocusable("a", true),
newMockFocusable("b", true),
newMockFocusable("c", true),
},
nextCalls: 4,
expectedFocusedID: "a",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
fm := newFocusManager()
registerAll(fm, tt.elements...)
for i := 0; i < tt.nextCalls; i++ {
fm.Next()
}
focused := fm.Focused()
if focused == nil {
t.Fatal("Focused() returned nil")
}
mf := focused.(*mockFocusable)
if mf.id != tt.expectedFocusedID {
t.Errorf("After %d Next() calls, focused = %q, want %q", tt.nextCalls, mf.id, tt.expectedFocusedID)
}
})
}
}
func TestFocusManager_Prev(t *testing.T) {
type tc struct {
elements []*mockFocusable
prevCalls int
expectedFocusedID string
}
tests := map[string]tc{
"first Prev wraps to last": {
elements: []*mockFocusable{
newMockFocusable("a", true),
newMockFocusable("b", true),
newMockFocusable("c", true),
},
prevCalls: 1,
expectedFocusedID: "c",
},
"prev twice from none": {
elements: []*mockFocusable{
newMockFocusable("a", true),
newMockFocusable("b", true),
newMockFocusable("c", true),
},
prevCalls: 2,
expectedFocusedID: "b",
},
"skips non-focusable backward": {
elements: []*mockFocusable{
newMockFocusable("a", true),
newMockFocusable("b", false),
newMockFocusable("c", true),
},
prevCalls: 2,
expectedFocusedID: "a",
},
"full cycle backwards": {
elements: []*mockFocusable{
newMockFocusable("a", true),
newMockFocusable("b", true),
newMockFocusable("c", true),
},
prevCalls: 4,
expectedFocusedID: "c",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
fm := newFocusManager()
registerAll(fm, tt.elements...)
for i := 0; i < tt.prevCalls; i++ {
fm.Prev()
}
focused := fm.Focused()
if focused == nil {
t.Fatal("Focused() returned nil")
}
mf := focused.(*mockFocusable)
if mf.id != tt.expectedFocusedID {
t.Errorf("After %d Prev() calls, focused = %q, want %q", tt.prevCalls, mf.id, tt.expectedFocusedID)
}
})
}
}
func TestFocusManager_SetFocus(t *testing.T) {
type tc struct {
elements []*mockFocusable
focusIndex int
expectedFocusedID string
}
tests := map[string]tc{
"set focus to second": {
elements: []*mockFocusable{
newMockFocusable("a", true),
newMockFocusable("b", true),
newMockFocusable("c", true),
},
focusIndex: 1,
expectedFocusedID: "b",
},
"set focus to last": {
elements: []*mockFocusable{
newMockFocusable("a", true),
newMockFocusable("b", true),
newMockFocusable("c", true),
},
focusIndex: 2,
expectedFocusedID: "c",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
fm := newFocusManager()
registerAll(fm, tt.elements...)
fm.SetFocus(tt.elements[tt.focusIndex])
focused := fm.Focused()
if focused == nil {
t.Fatal("Focused() returned nil")
}
mf := focused.(*mockFocusable)
if mf.id != tt.expectedFocusedID {
t.Errorf("SetFocus() focused = %q, want %q", mf.id, tt.expectedFocusedID)
}
if !mf.focused {
t.Error("SetFocus() target should have focused=true")
}
})
}
}
func TestFocusManager_SetFocusNonFocusable(t *testing.T) {
a := newMockFocusable("a", true)
b := newMockFocusable("b", false)
fm := newFocusManager()
fm.Register(a)
fm.Register(b)
fm.SetFocus(b)
if fm.Focused() != nil {
t.Error("SetFocus() on non-focusable should not set focus")
}
}
func TestFocusManager_Scope(t *testing.T) {
root := New()
scopeParent := New()
child1 := New(WithFocusable(true))
child2 := New(WithFocusable(true))
outside := New(WithFocusable(true))
scopeParent.AddChild(child1)
scopeParent.AddChild(child2)
root.AddChild(scopeParent)
root.AddChild(outside)
fm := newFocusManager()
fm.Register(child1)
fm.Register(child2)
fm.Register(outside)
fm.Next()
if fm.Focused() != child1 {
t.Fatal("expected child1 focused first")
}
fm.Next()
if fm.Focused() != child2 {
t.Fatal("expected child2 focused second")
}
fm.Next()
if fm.Focused() != outside {
t.Fatal("expected outside focused third")
}
fm.ClearFocus()
fm.ScopeTo(scopeParent)
fm.Next()
if fm.Focused() != child1 {
t.Fatal("expected child1 when scoped")
}
fm.Next()
if fm.Focused() != child2 {
t.Fatal("expected child2 when scoped")
}
fm.Next()
if fm.Focused() != child1 {
t.Fatalf("expected child1 after wrap, got %v", fm.Focused())
}
fm.ClearScope()
fm.ClearFocus()
fm.Next()
fm.Next()
fm.Next()
if fm.Focused() != outside {
t.Fatal("expected outside reachable after ClearScope")
}
}
func TestFocusManager_Register(t *testing.T) {
type tc struct {
initialElements []*mockFocusable
registerElement *mockFocusable
}
tests := map[string]tc{
"register to empty manager does not auto-focus": {
initialElements: []*mockFocusable{},
registerElement: newMockFocusable("new", true),
},
"register to existing does not change focus": {
initialElements: []*mockFocusable{
newMockFocusable("a", true),
},
registerElement: newMockFocusable("new", true),
},
"register non-focusable to empty does not focus": {
initialElements: []*mockFocusable{},
registerElement: newMockFocusable("new", false),
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
fm := newFocusManager()
registerAll(fm, tt.initialElements...)
fm.Register(tt.registerElement)
if fm.Focused() != nil {
t.Error("Register() should not auto-focus any element")
}
})
}
}
|