forked from go-rod/rod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyboard_test.go
148 lines (125 loc) · 3.2 KB
/
keyboard_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
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
package input_test
import (
"testing"
"github.com/go-rod/rod/lib/input"
"github.com/go-rod/rod/lib/proto"
"github.com/ysmood/got"
"github.com/ysmood/got/lib/gop"
"github.com/ysmood/gson"
)
func TestKeyMap(t *testing.T) {
g := got.T(t)
k := input.Key('a')
g.Eq(k.Info(), input.KeyInfo{
Key: "a",
Code: "KeyA",
KeyCode: 65,
Location: 0,
})
k = input.Key('A')
g.Eq(k.Info(), input.KeyInfo{
Key: "A",
Code: "KeyA",
KeyCode: 65,
Location: 0,
})
g.True(k.Printable())
k = input.Enter
g.Eq(k.Info(), input.KeyInfo{
Key: "\r",
Code: "Enter",
KeyCode: 13,
Location: 0,
})
k = input.ShiftLeft
g.Eq(k.Info(), input.KeyInfo /* len=4 */ {
Key: "Shift",
Code: "ShiftLeft",
KeyCode: 16,
Location: 1,
})
g.False(k.Printable())
k = input.ShiftRight
g.Eq(k.Info(), input.KeyInfo /* len=4 */ {
Key: "Shift",
Code: "ShiftRight",
KeyCode: 16,
Location: 2,
})
k, has := input.Digit1.Shift()
g.True(has)
g.Eq(k.Info().Key, "!")
_, has = input.Enter.Shift()
g.False(has)
g.Panic(func() {
input.Key('\n').Info()
})
}
func TestKeyModifier(t *testing.T) {
g := got.T(t)
check := func(k input.Key, m int) {
g.Helper()
g.Eq(k.Modifier(), m)
}
check(input.KeyA, 0)
check(input.AltLeft, 1)
check(input.ControlLeft, 2)
check(input.MetaLeft, 4)
check(input.ShiftLeft, 8)
}
func TestKeyEncode(t *testing.T) {
g := got.T(t)
g.Eq(input.Key('a').Encode(proto.InputDispatchKeyEventTypeKeyDown, 0), &proto.InputDispatchKeyEvent{
Type: "keyDown",
Text: "a",
UnmodifiedText: "a",
Code: "KeyA",
Key: "a",
WindowsVirtualKeyCode: 65,
Location: gson.Int(0),
})
g.Eq(input.Key('a').Encode(proto.InputDispatchKeyEventTypeKeyUp, 0), &proto.InputDispatchKeyEvent{
Type: "keyUp",
Text: "a",
UnmodifiedText: "a",
Code: "KeyA",
Key: "a",
WindowsVirtualKeyCode: 65,
Location: gson.Int(0),
})
g.Eq(input.AltLeft.Encode(proto.InputDispatchKeyEventTypeKeyDown, 0), &proto.InputDispatchKeyEvent{
Type: "rawKeyDown",
Code: "AltLeft",
Key: "Alt",
WindowsVirtualKeyCode: 18,
Location: gson.Int(1),
})
g.Eq(input.Numpad1.Encode(proto.InputDispatchKeyEventTypeKeyDown, 0), &proto.InputDispatchKeyEvent{
Type: "keyDown",
Code: "Numpad1",
Key: "1",
Text: "1",
UnmodifiedText: "1",
WindowsVirtualKeyCode: 35,
IsKeypad: true,
})
}
func TestMac(t *testing.T) {
g := got.T(t)
old := input.IsMac
input.IsMac = true
defer func() { input.IsMac = old }()
g.Eq(input.ArrowDown.Encode(proto.InputDispatchKeyEventTypeKeyDown, 0), &proto.InputDispatchKeyEvent{
Type: "rawKeyDown",
Code: "ArrowDown",
Key: "ArrowDown",
WindowsVirtualKeyCode: 40,
AutoRepeat: false,
IsKeypad: false,
IsSystemKey: false,
Location: gop.Ptr(0).(*int),
Commands: []string{
"moveDown",
},
})
}