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
|
package tui
import (
"testing"
)
func TestIntegration_MockReaderToFocusManager(t *testing.T) {
type tc struct {
events []Event
expectedHandled []bool
focusedAfter string
focusCycles int
}
tests := map[string]tc{
"single event to focused element": {
events: []Event{KeyEvent{Key: KeyEnter}},
expectedHandled: []bool{true},
focusedAfter: "a",
focusCycles: 1,
},
"multiple events same element": {
events: []Event{
KeyEvent{Key: KeyEnter},
KeyEvent{Key: KeyTab},
KeyEvent{Key: KeyEscape},
},
expectedHandled: []bool{true, true, true},
focusedAfter: "a",
focusCycles: 1,
},
"events after focus change": {
events: []Event{
KeyEvent{Key: KeyEnter},
},
expectedHandled: []bool{true},
focusedAfter: "b",
focusCycles: 2,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
elemA := newMockFocusable("a", true)
elemA.handled = true
elemB := newMockFocusable("b", true)
elemB.handled = true
fm := newFocusManager()
fm.Register(elemA)
fm.Register(elemB)
for i := 0; i < tt.focusCycles; i++ {
fm.Next()
}
reader := NewMockEventReader(tt.events...)
for i, expectedHandled := range tt.expectedHandled {
event, ok := reader.PollEvent(0)
if !ok {
t.Fatalf("Event %d: PollEvent returned false unexpectedly", i)
}
handled := fm.Dispatch(event)
if handled != expectedHandled {
t.Errorf("Event %d: Dispatch() = %v, want %v", i, handled, expectedHandled)
}
}
focused := fm.Focused()
if focused == nil {
t.Fatal("Focused() returned nil")
}
mf := focused.(*mockFocusable)
if mf.id != tt.focusedAfter {
t.Errorf("Final focused element = %q, want %q", mf.id, tt.focusedAfter)
}
})
}
}
func TestIntegration_ResizeEventUpdatesBuffer(t *testing.T) {
type tc struct {
initialWidth int
initialHeight int
resizeWidth int
resizeHeight int
}
tests := map[string]tc{
"grow terminal": {
initialWidth: 80,
initialHeight: 24,
resizeWidth: 120,
resizeHeight: 40,
},
"shrink terminal": {
initialWidth: 120,
initialHeight: 40,
resizeWidth: 80,
resizeHeight: 24,
},
"change width only": {
initialWidth: 80,
initialHeight: 24,
resizeWidth: 100,
resizeHeight: 24,
},
"change height only": {
initialWidth: 80,
initialHeight: 24,
resizeWidth: 80,
resizeHeight: 30,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
buffer := NewBuffer(tt.initialWidth, tt.initialHeight)
app := &App{
buffer: buffer,
focus: newFocusManager(),
}
event := ResizeEvent{Width: tt.resizeWidth, Height: tt.resizeHeight}
handled := app.Dispatch(event)
if !handled {
t.Error("Dispatch(ResizeEvent) should return true")
}
w, h := app.buffer.Size()
if w != tt.resizeWidth || h != tt.resizeHeight {
t.Errorf("Buffer size = (%d, %d), want (%d, %d)", w, h, tt.resizeWidth, tt.resizeHeight)
}
})
}
}
func TestIntegration_EventDispatchToMultipleFocusables(t *testing.T) {
elem1 := newMockFocusable("elem1", true)
elem1.handled = true
elem2 := newMockFocusable("elem2", true)
elem2.handled = true
elem3 := newMockFocusable("elem3", true)
elem3.handled = true
fm := newFocusManager()
fm.Register(elem1)
fm.Register(elem2)
fm.Register(elem3)
fm.SetFocus(elem1)
if fm.Focused().(*mockFocusable).id != "elem1" {
t.Errorf("Focus should be elem1, got %s", fm.Focused().(*mockFocusable).id)
}
event1 := KeyEvent{Key: KeyRune, Rune: 'a'}
fm.Dispatch(event1)
if elem1.lastEvent == nil {
t.Error("elem1 should have received the event")
}
fm.Next()
if fm.Focused().(*mockFocusable).id != "elem2" {
t.Error("Focus should have moved to elem2")
}
event2 := KeyEvent{Key: KeyRune, Rune: 'b'}
fm.Dispatch(event2)
if elem2.lastEvent == nil {
t.Error("elem2 should have received the event")
}
if elem1.lastEvent.(KeyEvent).Rune != 'a' {
t.Error("elem1's last event should still be 'a'")
}
fm.Next()
if fm.Focused().(*mockFocusable).id != "elem3" {
t.Error("Focus should have moved to elem3")
}
fm.Next()
if fm.Focused().(*mockFocusable).id != "elem1" {
t.Error("Focus should have wrapped to elem1")
}
}
func TestIntegration_FocusCycleWithNonFocusable(t *testing.T) {
elem1 := newMockFocusable("elem1", true)
elem2 := newMockFocusable("elem2", false)
elem3 := newMockFocusable("elem3", true)
elem4 := newMockFocusable("elem4", false)
elem5 := newMockFocusable("elem5", true)
fm := newFocusManager()
fm.Register(elem1)
fm.Register(elem2)
fm.Register(elem3)
fm.Register(elem4)
fm.Register(elem5)
focusOrder := []string{}
for range 6 {
fm.Next()
focusOrder = append(focusOrder, fm.Focused().(*mockFocusable).id)
}
expected := []string{"elem1", "elem3", "elem5", "elem1", "elem3", "elem5"}
if len(focusOrder) != len(expected) {
t.Fatalf("Focus order length = %d, want %d", len(focusOrder), len(expected))
}
for i, id := range expected {
if focusOrder[i] != id {
t.Errorf("Focus order[%d] = %q, want %q", i, focusOrder[i], id)
}
}
}
func TestIntegration_EventReaderWithFocusManager(t *testing.T) {
events := []Event{
KeyEvent{Key: KeyRune, Rune: 'h'},
KeyEvent{Key: KeyRune, Rune: 'e'},
KeyEvent{Key: KeyRune, Rune: 'l'},
KeyEvent{Key: KeyRune, Rune: 'l'},
KeyEvent{Key: KeyRune, Rune: 'o'},
KeyEvent{Key: KeyEnter},
}
reader := NewMockEventReader(events...)
elem := newMockFocusable("input", true)
elem.handled = true
fm := newFocusManager()
fm.Register(elem)
fm.SetFocus(elem)
eventCount := 0
for {
event, ok := reader.PollEvent(0)
if !ok {
break
}
fm.Dispatch(event)
eventCount++
}
if eventCount != len(events) {
t.Errorf("Processed %d events, want %d", eventCount, len(events))
}
lastEvent := elem.lastEvent.(KeyEvent)
if lastEvent.Key != KeyEnter {
t.Errorf("Last event key = %v, want KeyEnter", lastEvent.Key)
}
}
func TestIntegration_UnhandledEventPropagation(t *testing.T) {
elem := newMockFocusable("elem", true)
elem.handled = false
fm := newFocusManager()
fm.Register(elem)
fm.SetFocus(elem)
event := KeyEvent{Key: KeyEnter}
handled := fm.Dispatch(event)
if handled {
t.Error("Dispatch should return false when element doesn't handle event")
}
if elem.lastEvent == nil {
t.Error("Event should have been passed to element even if not handled")
}
}
|