Debian Codegraph @

input/driver_windows_test.go

raw
1 package input
2
3 import (
4 "encoding/binary"
5 "image/color"
6 "reflect"
7 "testing"
8 "unicode/utf16"
9
10 "github.com/charmbracelet/x/ansi"
11 xwindows "github.com/charmbracelet/x/windows"
12 "golang.org/x/sys/windows"
13 )
14
15 func TestWindowsInputEvents(t *testing.T) {
16 cases := []struct {
17 name string
18 events []xwindows.InputRecord
19 expected []Event
20 sequence bool // indicates that the input events are ANSI sequence or utf16
21 }{
22 {
23 name: "single key event",
24 events: []xwindows.InputRecord{
25 encodeKeyEvent(xwindows.KeyEventRecord{
26 KeyDown: true,
27 Char: 'a',
28 VirtualKeyCode: 'A',
29 }),
30 },
31 expected: []Event{KeyPressEvent{Code: 'a', BaseCode: 'a', Text: "a"}},
32 },
33 {
34 name: "single key event with control key",
35 events: []xwindows.InputRecord{
36 encodeKeyEvent(xwindows.KeyEventRecord{
37 KeyDown: true,
38 Char: 'a',
39 VirtualKeyCode: 'A',
40 ControlKeyState: xwindows.LEFT_CTRL_PRESSED,
41 }),
42 },
43 expected: []Event{KeyPressEvent{Code: 'a', BaseCode: 'a', Mod: ModCtrl}},
44 },
45 {
46 name: "escape alt key event",
47 events: []xwindows.InputRecord{
48 encodeKeyEvent(xwindows.KeyEventRecord{
49 KeyDown: true,
50 Char: ansi.ESC,
51 VirtualKeyCode: ansi.ESC,
52 ControlKeyState: xwindows.LEFT_ALT_PRESSED,
53 }),
54 },
55 expected: []Event{KeyPressEvent{Code: ansi.ESC, BaseCode: ansi.ESC, Mod: ModAlt}},
56 },
57 {
58 name: "single shifted key event",
59 events: []xwindows.InputRecord{
60 encodeKeyEvent(xwindows.KeyEventRecord{
61 KeyDown: true,
62 Char: 'A',
63 VirtualKeyCode: 'A',
64 ControlKeyState: xwindows.SHIFT_PRESSED,
65 }),
66 },
67 expected: []Event{KeyPressEvent{Code: 'A', BaseCode: 'a', Text: "A", Mod: ModShift}},
68 },
69 {
70 name: "utf16 rune",
71 events: encodeUtf16Rune('😊'), // smiley emoji '😊'
72 expected: []Event{
73 KeyPressEvent{Code: '😊', Text: "😊"},
74 },
75 sequence: true,
76 },
77 {
78 name: "background color response",
79 events: encodeSequence("\x1b]11;rgb:ff/ff/ff\x07"),
80 expected: []Event{BackgroundColorEvent{Color: color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff}}},
81 sequence: true,
82 },
83 {
84 name: "st terminated background color response",
85 events: encodeSequence("\x1b]11;rgb:ffff/ffff/ffff\x1b\\"),
86 expected: []Event{BackgroundColorEvent{Color: color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff}}},
87 sequence: true,
88 },
89 {
90 name: "simple mouse event",
91 events: []xwindows.InputRecord{
92 encodeMouseEvent(xwindows.MouseEventRecord{
93 MousePositon: windows.Coord{X: 10, Y: 20},
94 ButtonState: xwindows.FROM_LEFT_1ST_BUTTON_PRESSED,
95 EventFlags: 0,
96 }),
97 encodeMouseEvent(xwindows.MouseEventRecord{
98 MousePositon: windows.Coord{X: 10, Y: 20},
99 EventFlags: 0,
100 }),
101 },
102 expected: []Event{
103 MouseClickEvent{Button: MouseLeft, X: 10, Y: 20},
104 MouseReleaseEvent{Button: MouseLeft, X: 10, Y: 20},
105 },
106 },
107 {
108 name: "focus event",
109 events: []xwindows.InputRecord{
110 encodeFocusEvent(xwindows.FocusEventRecord{
111 SetFocus: true,
112 }),
113 encodeFocusEvent(xwindows.FocusEventRecord{
114 SetFocus: false,
115 }),
116 },
117 expected: []Event{
118 FocusEvent{},
119 BlurEvent{},
120 },
121 },
122 {
123 name: "window size event",
124 events: []xwindows.InputRecord{
125 encodeWindowBufferSizeEvent(xwindows.WindowBufferSizeRecord{
126 Size: windows.Coord{X: 10, Y: 20},
127 }),
128 },
129 expected: []Event{
130 WindowSizeEvent{Width: 10, Height: 20},
131 },
132 },
133 }
134
135 // p is the parser to parse the input events
136 var p Parser
137
138 // keep track of the state of the driver to handle ANSI sequences and utf16
139 var state win32InputState
140 for _, tc := range cases {
141 t.Run(tc.name, func(t *testing.T) {
142 if tc.sequence {
143 var Event Event
144 for _, ev := range tc.events {
145 if ev.EventType != xwindows.KEY_EVENT {
146 t.Fatalf("expected key event, got %v", ev.EventType)
147 }
148
149 key := ev.KeyEvent()
150 Event = p.parseWin32InputKeyEvent(&state, key.VirtualKeyCode, key.VirtualScanCode, key.Char, key.KeyDown, key.ControlKeyState, key.RepeatCount)
151 }
152 if len(tc.expected) != 1 {
153 t.Fatalf("expected 1 event, got %d", len(tc.expected))
154 }
155 if !reflect.DeepEqual(Event, tc.expected[0]) {
156 t.Errorf("expected %v, got %v", tc.expected[0], Event)
157 }
158 } else {
159 if len(tc.events) != len(tc.expected) {
160 t.Fatalf("expected %d events, got %d", len(tc.expected), len(tc.events))
161 }
162 for j, ev := range tc.events {
163 Event := p.parseConInputEvent(ev, &state)
164 if !reflect.DeepEqual(Event, tc.expected[j]) {
165 t.Errorf("expected %#v, got %#v", tc.expected[j], Event)
166 }
167 }
168 }
169 })
170 }
171 }
172
173 func boolToUint32(b bool) uint32 {
174 if b {
175 return 1
176 }
177 return 0
178 }
179
180 func encodeMenuEvent(menu xwindows.MenuEventRecord) xwindows.InputRecord {
181 var bts [16]byte
182 binary.LittleEndian.PutUint32(bts[0:4], menu.CommandID)
183 return xwindows.InputRecord{
184 EventType: xwindows.MENU_EVENT,
185 Event: bts,
186 }
187 }
188
189 func encodeWindowBufferSizeEvent(size xwindows.WindowBufferSizeRecord) xwindows.InputRecord {
190 var bts [16]byte
191 binary.LittleEndian.PutUint16(bts[0:2], uint16(size.Size.X))
192 binary.LittleEndian.PutUint16(bts[2:4], uint16(size.Size.Y))
193 return xwindows.InputRecord{
194 EventType: xwindows.WINDOW_BUFFER_SIZE_EVENT,
195 Event: bts,
196 }
197 }
198
199 func encodeFocusEvent(focus xwindows.FocusEventRecord) xwindows.InputRecord {
200 var bts [16]byte
201 if focus.SetFocus {
202 bts[0] = 1
203 }
204 return xwindows.InputRecord{
205 EventType: xwindows.FOCUS_EVENT,
206 Event: bts,
207 }
208 }
209
210 func encodeMouseEvent(mouse xwindows.MouseEventRecord) xwindows.InputRecord {
211 var bts [16]byte
212 binary.LittleEndian.PutUint16(bts[0:2], uint16(mouse.MousePositon.X))
213 binary.LittleEndian.PutUint16(bts[2:4], uint16(mouse.MousePositon.Y))
214 binary.LittleEndian.PutUint32(bts[4:8], mouse.ButtonState)
215 binary.LittleEndian.PutUint32(bts[8:12], mouse.ControlKeyState)
216 binary.LittleEndian.PutUint32(bts[12:16], mouse.EventFlags)
217 return xwindows.InputRecord{
218 EventType: xwindows.MOUSE_EVENT,
219 Event: bts,
220 }
221 }
222
223 func encodeKeyEvent(key xwindows.KeyEventRecord) xwindows.InputRecord {
224 var bts [16]byte
225 binary.LittleEndian.PutUint32(bts[0:4], boolToUint32(key.KeyDown))
226 binary.LittleEndian.PutUint16(bts[4:6], key.RepeatCount)
227 binary.LittleEndian.PutUint16(bts[6:8], key.VirtualKeyCode)
228 binary.LittleEndian.PutUint16(bts[8:10], key.VirtualScanCode)
229 binary.LittleEndian.PutUint16(bts[10:12], uint16(key.Char))
230 binary.LittleEndian.PutUint32(bts[12:16], key.ControlKeyState)
231 return xwindows.InputRecord{
232 EventType: xwindows.KEY_EVENT,
233 Event: bts,
234 }
235 }
236
237 // encodeSequence encodes a string of ANSI escape sequences into a slice of
238 // Windows input key records.
239 func encodeSequence(s string) (evs []xwindows.InputRecord) {
240 var state byte
241 for len(s) > 0 {
242 seq, _, n, newState := ansi.DecodeSequence(s, state, nil)
243 for i := 0; i < n; i++ {
244 evs = append(evs, encodeKeyEvent(xwindows.KeyEventRecord{
245 KeyDown: true,
246 Char: rune(seq[i]),
247 }))
248 }
249 state = newState
250 s = s[n:]
251 }
252 return evs
253 }
254
255 func encodeUtf16Rune(r rune) []xwindows.InputRecord {
256 r1, r2 := utf16.EncodeRune(r)
257 return encodeUtf16Pair(r1, r2)
258 }
259
260 func encodeUtf16Pair(r1, r2 rune) []xwindows.InputRecord {
261 return []xwindows.InputRecord{
262 encodeKeyEvent(xwindows.KeyEventRecord{
263 KeyDown: true,
264 Char: r1,
265 }),
266 encodeKeyEvent(xwindows.KeyEventRecord{
267 KeyDown: true,
268 Char: r2,
269 }),
270 }
271 }