-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
InputHandler_test.go
100 lines (82 loc) · 2.71 KB
/
InputHandler_test.go
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
package giu
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_InputHandler_new(t *testing.T) {
i := newInputHandler()
assert.NotNil(t, i, "input handler wasn't created")
assert.NotNil(t, i.shortcuts, "input handler wasn't created")
}
func Test_InputHandle_RegisterKeyboardShortcuts(t *testing.T) {
tests := []struct {
id string
key Key
mod Modifier
isGlobal ShortcutType
cb func()
}{
{"global shortcut", Key(1), Modifier(2), ShortcutType(true), func() {}},
{"window shortcut", Key(9), Modifier(3), ShortcutType(false), func() {}},
}
for _, tt := range tests {
t.Run(tt.id, func(lt *testing.T) {
a := assert.New(lt)
i := newInputHandler()
i.RegisterKeyboardShortcuts(Shortcut{
Key: tt.key,
Modifier: tt.mod,
Callback: tt.cb,
IsGlobal: tt.isGlobal,
})
combo := keyCombo{
key: tt.key,
modifier: tt.mod,
}
shortcut, exist := i.shortcuts[combo]
a.True(exist, "shortcut wasn't registered in input manager")
if tt.isGlobal {
// TODO: figure out why it doesn't work
// a.Equal(shortcut.global, tt.cb, "wrong shortcut set in input manager")
a.NotNil(shortcut.global, "wrong shortcut set in input manager")
a.Nil(shortcut.window, "wrong shortcut set in input manager")
} else {
// TODO: figure out why it doesn't work
// a.Equal(shortcut.window, tt.cb, "wrong shortcut set in input manager")
a.NotNil(shortcut.window, "wrong shortcut set in input manager")
a.Nil(shortcut.global, "wrong shortcut set in input manager")
}
})
}
}
func Test_InputHandler_UnregisterWindowShortcuts(t *testing.T) {
i := newInputHandler()
sh := []Shortcut{
{Key(5), Modifier(0), func() {}, true},
{Key(8), Modifier(2), func() {}, false},
}
i.RegisterKeyboardShortcuts(sh...)
i.UnregisterWindowShortcuts()
for _, s := range i.shortcuts {
assert.Nil(t, s.window, "some window shortcuts wasn't unregistered")
}
}
func Test_InputHandler_Handle(t *testing.T) {
a := assert.New(t)
i := newInputHandler()
var shortcut1, shortcut2 bool
sh := []Shortcut{
{Key(5), Modifier(0), func() { shortcut1 = true }, true},
{Key(8), Modifier(2), func() { shortcut2 = true }, false},
}
i.RegisterKeyboardShortcuts(sh...)
i.Handle(Key(0), Modifier(0), Press)
a.False(shortcut1, "Shortcut 1 was handled, but shouldn't.")
a.False(shortcut2, "Shortcut 2 was handled, but shouldn't.")
i.Handle(Key(5), Modifier(0), Press)
a.True(shortcut1, "Shortcut 1 was not handled, but should be.")
a.False(shortcut2, "Shortcut 2 was handled, but shouldn't.")
i.Handle(Key(8), Modifier(2), Press)
a.True(shortcut1, "Shortcut 1 was not handled, but should be.")
a.True(shortcut2, "Shortcut 2 was not handled, but should be.")
}