-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyboard_test.go
More file actions
392 lines (363 loc) · 14.2 KB
/
Copy pathkeyboard_test.go
File metadata and controls
392 lines (363 loc) · 14.2 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
package modgadget
import (
"testing"
"time"
)
type fakeKeyboard struct {
events [8]KeyEvent
count int
index int
reads int
}
type fakeVolumeController struct {
ups, downs, toggles int
}
func (controller *fakeVolumeController) VolumeUp() { controller.ups++ }
func (controller *fakeVolumeController) VolumeDown() { controller.downs++ }
func (controller *fakeVolumeController) ToggleMute() { controller.toggles++ }
var _ VolumeController = (*fakeVolumeController)(nil)
var _ Keyboard = (*fakeKeyboard)(nil)
func (keyboard *fakeKeyboard) ReadKeyEvent() (KeyEvent, bool) {
keyboard.reads++
if keyboard.index >= keyboard.count {
return KeyEvent{}, false
}
event := keyboard.events[keyboard.index]
keyboard.index++
return event, true
}
func TestKeyboardOptionalAndDrain(t *testing.T) {
if (KeyEvent{}).Action != KeyActionUnknown || KeyActionUnknown == KeyDown {
t.Fatal("zero KeyEvent has a valid KeyDown action")
}
New(nil).Update(time.Time{})
New(nil, WithKeyboard(nil)).Update(time.Time{})
keyboard := &fakeKeyboard{count: 2}
keyboard.events[0] = KeyEvent{Code: KeyA, Rune: 'A', Action: KeyDown, Modifiers: ModShift}
keyboard.events[1] = KeyEvent{Code: KeyA, Action: KeyUp}
g := New(nil, WithKeyboard(keyboard))
var got [2]KeyEvent
count := 0
g.OnKey(func(event KeyEvent) bool {
got[count] = event
count++
return false
})
g.Update(time.Time{})
if count != 2 || got[0] != keyboard.events[0] || got[1] != keyboard.events[1] {
t.Fatalf("events = %#v count=%d", got, count)
}
if keyboard.reads != 3 {
t.Fatalf("reads = %d, want two events plus empty read", keyboard.reads)
}
if !got[0].Modifiers.Has(ModShift) || got[0].Modifiers.Has(ModControl) {
t.Fatalf("modifiers = %#x", got[0].Modifiers)
}
if !(ModShift | ModControl).Has(ModShift | ModControl) {
t.Fatal("Has did not recognize multiple bits")
}
}
func TestSystemVolumeShortcutsConsumeBeforeHandlers(t *testing.T) {
controller := &fakeVolumeController{}
keyboard := &fakeKeyboard{}
keyboard.events = [8]KeyEvent{}
// fakeKeyboard has room for eight events; exercise the three shortcuts,
// releases, missing Fn, and an unrelated Fn key in those slots.
keyboard.count = 8
keyboard.events[0] = KeyEvent{Code: KeyF12, Action: KeyDown, Modifiers: ModFn}
keyboard.events[1] = KeyEvent{Code: KeyF11, Action: KeyDown, Modifiers: ModFn | ModShift}
keyboard.events[2] = KeyEvent{Code: KeyM, Action: KeyDown, Modifiers: ModFn | ModControl}
keyboard.events[3] = KeyEvent{Code: KeyF12, Action: KeyUp, Modifiers: ModFn}
keyboard.events[4] = KeyEvent{Code: KeyF11, Action: KeyDown}
keyboard.events[5] = KeyEvent{Code: KeyM, Action: KeyUp, Modifiers: ModFn}
keyboard.events[6] = KeyEvent{Code: KeyQ, Action: KeyDown, Modifiers: ModFn}
keyboard.events[7] = KeyEvent{Code: KeyM, Action: KeyDown}
g := New(nil, WithKeyboard(keyboard), WithVolumeController(controller))
delivered := make([]KeyEvent, 0, 5)
g.OnKey(func(event KeyEvent) bool {
delivered = append(delivered, event)
return false
})
g.Update(time.Time{})
if controller.ups != 1 || controller.downs != 1 || controller.toggles != 1 {
t.Fatalf("volume calls up=%d down=%d toggle=%d", controller.ups, controller.downs, controller.toggles)
}
if len(delivered) != 3 {
t.Fatalf("delivered=%#v", delivered)
}
if delivered[1].Code != KeyQ || !delivered[1].Modifiers.Has(ModFn) {
t.Fatalf("unrelated Fn event=%#v", delivered[1])
}
}
func TestSystemVolumeShortcutsWithoutControllerAreDelivered(t *testing.T) {
keyboard := &fakeKeyboard{count: 6}
keyboard.events[0] = KeyEvent{Code: KeyF12, Action: KeyDown, Modifiers: ModFn}
keyboard.events[1] = KeyEvent{Code: KeyF12, Action: KeyUp}
keyboard.events[2] = KeyEvent{Code: KeyF11, Action: KeyDown, Modifiers: ModFn}
keyboard.events[3] = KeyEvent{Code: KeyF11, Action: KeyUp}
keyboard.events[4] = KeyEvent{Code: KeyM, Action: KeyDown, Modifiers: ModFn}
keyboard.events[5] = KeyEvent{Code: KeyM, Action: KeyUp}
g := New(nil, WithKeyboard(keyboard), WithVolumeController(nil))
delivered := 0
g.OnKey(func(KeyEvent) bool { delivered++; return false })
g.Update(time.Time{})
if delivered != 6 {
t.Fatalf("delivered=%d want=6", delivered)
}
}
func TestSystemVolumeShortcutCapturesDownAndUp(t *testing.T) {
tests := []struct {
name string
code KeyCode
want fakeVolumeController
}{
{name: "volume down", code: KeyF11, want: fakeVolumeController{downs: 1}},
{name: "volume up", code: KeyF12, want: fakeVolumeController{ups: 1}},
{name: "mute", code: KeyM, want: fakeVolumeController{toggles: 1}},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
keyboard := &fakeKeyboard{count: 2}
keyboard.events[0] = KeyEvent{Code: test.code, Action: KeyDown, Modifiers: ModFn}
// Fn may be released before this key, so KeyUp has no ModFn.
keyboard.events[1] = KeyEvent{Code: test.code, Action: KeyUp}
controller := &fakeVolumeController{}
g := New(nil, WithKeyboard(keyboard), WithVolumeController(controller))
delivered := 0
g.OnKey(func(KeyEvent) bool { delivered++; return false })
g.Update(time.Time{})
if *controller != test.want {
t.Fatalf("controller=%+v want=%+v", *controller, test.want)
}
if delivered != 0 || g.capturedSystemKeys != 0 {
t.Fatalf("delivered=%d captured=%#x", delivered, g.capturedSystemKeys)
}
})
}
}
func TestSystemVolumeShortcutFnReleasedFirst(t *testing.T) {
keyboard := &fakeKeyboard{count: 4}
keyboard.events[0] = KeyEvent{Code: KeyFn, Action: KeyDown, Modifiers: ModFn}
keyboard.events[1] = KeyEvent{Code: KeyM, Action: KeyDown, Modifiers: ModFn}
keyboard.events[2] = KeyEvent{Code: KeyFn, Action: KeyUp}
keyboard.events[3] = KeyEvent{Code: KeyM, Action: KeyUp}
controller := &fakeVolumeController{}
g := New(nil, WithKeyboard(keyboard), WithVolumeController(controller))
var delivered [2]KeyEvent
count := 0
g.OnKey(func(event KeyEvent) bool {
delivered[count] = event
count++
return false
})
g.Update(time.Time{})
if controller.toggles != 1 || count != 2 {
t.Fatalf("toggles=%d delivered=%#v count=%d", controller.toggles, delivered, count)
}
if delivered[0].Code != KeyFn || delivered[0].Action != KeyDown || delivered[1].Code != KeyFn || delivered[1].Action != KeyUp {
t.Fatalf("delivered=%#v", delivered)
}
}
func TestSystemVolumeShortcutDuplicateDownAndNormalKey(t *testing.T) {
keyboard := &fakeKeyboard{count: 5}
keyboard.events[0] = KeyEvent{Code: KeyM, Action: KeyDown, Modifiers: ModFn}
keyboard.events[1] = KeyEvent{Code: KeyM, Action: KeyDown, Modifiers: ModFn}
keyboard.events[2] = KeyEvent{Code: KeyM, Action: KeyUp}
keyboard.events[3] = KeyEvent{Code: KeyM, Action: KeyDown}
keyboard.events[4] = KeyEvent{Code: KeyM, Action: KeyUp}
controller := &fakeVolumeController{}
g := New(nil, WithKeyboard(keyboard), WithVolumeController(controller))
var delivered [2]KeyEvent
count := 0
g.OnKey(func(event KeyEvent) bool { delivered[count] = event; count++; return false })
g.Update(time.Time{})
if controller.toggles != 1 || count != 2 || delivered[0].Action != KeyDown || delivered[1].Action != KeyUp {
t.Fatalf("toggles=%d delivered=%#v count=%d", controller.toggles, delivered, count)
}
if g.capturedSystemKeys != 0 {
t.Fatalf("captured=%#x", g.capturedSystemKeys)
}
}
func TestSystemVolumeShortcutClearsStaleCapture(t *testing.T) {
keyboard := &fakeKeyboard{count: 2}
keyboard.events[0] = KeyEvent{Code: KeyM, Action: KeyDown}
keyboard.events[1] = KeyEvent{Code: KeyM, Action: KeyUp}
g := New(nil, WithKeyboard(keyboard), WithVolumeController(&fakeVolumeController{}))
g.capturedSystemKeys = systemMuteKey
delivered := 0
g.OnKey(func(KeyEvent) bool { delivered++; return false })
g.Update(time.Time{})
if delivered != 2 || g.capturedSystemKeys != 0 {
t.Fatalf("delivered=%d captured=%#x", delivered, g.capturedSystemKeys)
}
}
func TestSystemVolumeShortcutCompletionLeavesNormalKeysDelivered(t *testing.T) {
for _, code := range []KeyCode{KeyF11, KeyF12, KeyM} {
keyboard := &fakeKeyboard{count: 4}
keyboard.events[0] = KeyEvent{Code: code, Action: KeyDown, Modifiers: ModFn}
keyboard.events[1] = KeyEvent{Code: code, Action: KeyUp}
keyboard.events[2] = KeyEvent{Code: code, Action: KeyDown}
keyboard.events[3] = KeyEvent{Code: code, Action: KeyUp}
g := New(nil, WithKeyboard(keyboard), WithVolumeController(&fakeVolumeController{}))
var delivered [2]KeyEvent
count := 0
g.OnKey(func(event KeyEvent) bool { delivered[count] = event; count++; return false })
g.Update(time.Time{})
if count != 2 || delivered[0].Code != code || delivered[0].Action != KeyDown || delivered[1].Code != code || delivered[1].Action != KeyUp {
t.Fatalf("code=%v delivered=%#v count=%d", code, delivered, count)
}
}
}
func TestSystemVolumeShortcutAllocations(t *testing.T) {
keyboard := &fakeKeyboard{}
controller := &fakeVolumeController{}
g := New(nil, WithKeyboard(keyboard), WithVolumeController(controller))
if allocs := testing.AllocsPerRun(100, func() {
keyboard.index, keyboard.count = 0, 2
keyboard.events[0] = KeyEvent{Code: KeyF12, Action: KeyDown, Modifiers: ModFn}
keyboard.events[1] = KeyEvent{Code: KeyF12, Action: KeyUp}
g.Update(time.Time{})
}); allocs != 0 {
t.Fatalf("system shortcut allocations=%v", allocs)
}
}
type infiniteVolumeKeyboard struct{ reads int }
func (keyboard *infiniteVolumeKeyboard) ReadKeyEvent() (KeyEvent, bool) {
keyboard.reads++
return KeyEvent{Code: KeyF12, Action: KeyDown, Modifiers: ModFn}, true
}
func TestSystemVolumeShortcutKeepsKeyboardEventLimit(t *testing.T) {
keyboard := &infiniteVolumeKeyboard{}
controller := &fakeVolumeController{}
g := New(nil, WithKeyboard(keyboard), WithVolumeController(controller))
g.Update(time.Time{})
if keyboard.reads != maxKeyEventsPerUpdate || controller.ups != 1 || controller.downs != 0 || controller.toggles != 0 {
t.Fatalf("reads=%d up=%d down=%d toggle=%d limit=%d",
keyboard.reads, controller.ups, controller.downs, controller.toggles, maxKeyEventsPerUpdate)
}
}
func TestKeyHandlerOrderConsumeAndRemoval(t *testing.T) {
g := New(nil)
if id := g.OnKey(nil); id != 0 {
t.Fatalf("nil handler ID = %d", id)
}
if id := (*Gadget)(nil).OnKey(func(KeyEvent) bool { return false }); id != 0 {
t.Fatalf("nil Gadget ID = %d", id)
}
var order [3]int
called := 0
id1 := g.OnKey(func(KeyEvent) bool { order[called] = 1; called++; return false })
id2 := g.OnKey(func(KeyEvent) bool { order[called] = 2; called++; return true })
id3 := g.OnKey(func(KeyEvent) bool { order[called] = 3; called++; return false })
if id1 == 0 || id2 == 0 || id3 == 0 || id1 == id2 || id2 == id3 || id1 == id3 {
t.Fatalf("listener IDs = %d, %d, %d", id1, id2, id3)
}
g.dispatchKey(KeyEvent{Action: KeyDown})
if called != 2 || order != [3]int{1, 2, 0} {
t.Fatalf("dispatch order = %v called=%d", order, called)
}
if !g.RemoveListener(id2) || g.RemoveListener(id2) || g.RemoveListener(0) || g.RemoveListener(ListenerID(60000)) {
t.Fatal("unexpected RemoveListener result")
}
if (*Gadget)(nil).RemoveListener(id1) {
t.Fatal("nil Gadget removed listener")
}
called, order = 0, [3]int{}
g.dispatchKey(KeyEvent{Action: KeyDown})
if called != 2 || order != [3]int{1, 3, 0} {
t.Fatalf("after removal order = %v called=%d", order, called)
}
}
func TestKeyDispatchMutationRules(t *testing.T) {
g := New(nil)
var calls [4]int
var self ListenerID
var later ListenerID
self = g.OnKey(func(KeyEvent) bool {
calls[0]++
g.RemoveListener(self)
g.RemoveListener(later)
g.OnKey(func(KeyEvent) bool { calls[3]++; return false })
return false
})
g.OnKey(func(KeyEvent) bool { calls[1]++; return false })
later = g.OnKey(func(KeyEvent) bool { calls[2]++; return false })
g.dispatchKey(KeyEvent{Action: KeyDown})
if calls != [4]int{1, 1, 0, 0} {
t.Fatalf("first event calls = %v", calls)
}
if len(g.keyListeners) != 2 {
t.Fatalf("listeners after dispatch compact = %d", len(g.keyListeners))
}
g.dispatchKey(KeyEvent{Action: KeyDown})
if calls != [4]int{1, 2, 0, 1} {
t.Fatalf("second event calls = %v", calls)
}
}
func TestListenerCompactionDoesNotGrow(t *testing.T) {
g := New(nil)
kept := g.OnKey(func(KeyEvent) bool { return false })
for i := 0; i < 1000; i++ {
id := g.OnKey(func(KeyEvent) bool { return false })
if !g.RemoveListener(id) {
t.Fatalf("remove iteration %d", i)
}
}
if len(g.keyListeners) != 1 || g.keyListeners[0].id != kept {
t.Fatalf("listeners after churn = %#v", g.keyListeners)
}
}
type infiniteKeyboard struct{ reads int }
func (keyboard *infiniteKeyboard) ReadKeyEvent() (KeyEvent, bool) {
keyboard.reads++
return KeyEvent{Code: KeyA, Rune: 'a', Action: KeyDown}, true
}
func TestKeyboardUpdateEventLimitAndViewportProgress(t *testing.T) {
base := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
keyboard := &infiniteKeyboard{}
g := New(&testDisplay{width: 20, height: 10}, WithStyles(testStyles()), WithKeyboard(keyboard))
v := g.Viewport()
if err := v.SetText("aaaa"); err != nil {
t.Fatal(err)
}
v.SetHorizontalScroll(ScrollSpeed(10), ScrollLoop())
calls := 0
g.OnKey(func(KeyEvent) bool { calls++; return false })
g.Update(base)
g.Update(base.Add(time.Second))
if calls != 2*maxKeyEventsPerUpdate || keyboard.reads != 2*maxKeyEventsPerUpdate {
t.Fatalf("calls=%d reads=%d limit=%d", calls, keyboard.reads, maxKeyEventsPerUpdate)
}
if v.offset != 10 {
t.Fatalf("Viewport update did not run, offset=%d", v.offset)
}
}
func TestKeyboardUpdateKeepsScrollBehavior(t *testing.T) {
base := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
keyboard := &fakeKeyboard{}
v := New(&testDisplay{width: 20, height: 10}, WithStyles(testStyles()), WithKeyboard(keyboard)).Viewport()
if err := v.SetText("aaaa"); err != nil {
t.Fatal(err)
}
v.SetHorizontalScroll(ScrollSpeed(10), ScrollLoop())
v.owner.Update(base)
v.owner.Update(base.Add(time.Second))
if v.offset != 10 {
t.Fatalf("scroll offset = %d", v.offset)
}
}
func TestKeyboardUpdateSteadyAllocations(t *testing.T) {
keyboard := &fakeKeyboard{}
g := New(nil, WithKeyboard(keyboard))
if allocs := testing.AllocsPerRun(100, func() { g.Update(time.Time{}) }); allocs != 0 {
t.Fatalf("empty Update allocations = %v", allocs)
}
keyboard.events[0] = KeyEvent{Code: KeyA, Rune: 'a', Action: KeyDown}
g.OnKey(func(KeyEvent) bool { return false })
if allocs := testing.AllocsPerRun(100, func() {
keyboard.index, keyboard.count = 0, 1
g.Update(time.Time{})
}); allocs != 0 {
t.Fatalf("dispatch allocations = %v", allocs)
}
}