This repository has been archived by the owner on Mar 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
touchpad_test.go
284 lines (246 loc) · 8.38 KB
/
touchpad_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
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
package uinput
import (
"fmt"
"io/ioutil"
"os"
"testing"
)
func TestBasicTouchPadMoves(t *testing.T) {
absDev, err := CreateTouchPad("/dev/uinput", []byte("Test TouchPad"), 0, 1024, 0, 768)
if err != nil {
t.Fatalf("Failed to create the virtual touch pad. Last error was: %s\n", err)
}
defer func(absDev TouchPad) {
err := absDev.Close()
if err != nil {
t.Fatalf("Failed to close device. Last error was: %s\n", err)
}
}(absDev)
err = absDev.MoveTo(0, 0)
if err != nil {
t.Fatalf("Failed to move cursor to initial position. Last error was: %s\n", err)
}
err = absDev.MoveTo(100, 200)
if err != nil {
t.Fatalf("Failed to move cursor to position x:100, y:200. Last error was: %s\n", err)
}
}
func TestTouchPadClicks(t *testing.T) {
absDev, err := CreateTouchPad("/dev/uinput", []byte("Test TouchPad"), 0, 1024, 0, 768)
if err != nil {
t.Fatalf("Failed to create the virtual touch pad. Last error was: %s\n", err)
}
defer func(absDev TouchPad) {
err := absDev.Close()
if err != nil {
t.Fatalf("Failed to close device. Last error was: %s\n", err)
}
}(absDev)
err = absDev.RightClick()
if err != nil {
t.Fatalf("Failed to perform right click. Last error was: %s\n", err)
}
err = absDev.LeftClick()
if err != nil {
t.Fatalf("Failed to perform right click. Last error was: %s\n", err)
}
}
func TestTouchPadButtonPresses(t *testing.T) {
absDev, err := CreateTouchPad("/dev/uinput", []byte("Test TouchPad"), 0, 1024, 0, 768)
if err != nil {
t.Fatalf("Failed to create the virtual touch pad. Last error was: %s\n", err)
}
defer func(absDev TouchPad) {
err := absDev.Close()
if err != nil {
t.Fatalf("Failed to close device. Last error was: %s\n", err)
}
}(absDev)
err = absDev.LeftPress()
if err != nil {
t.Fatalf("Failed to perform left key press. Last error was: %s\n", err)
}
err = absDev.LeftRelease()
if err != nil {
t.Fatalf("Failed to perform left key release. Last error was: %s\n", err)
}
err = absDev.RightPress()
if err != nil {
t.Fatalf("Failed to perform right key press. Last error was: %s\n", err)
}
err = absDev.RightRelease()
if err != nil {
t.Fatalf("Failed to perform right key release. Last error was: %s\n", err)
}
}
func TestTouchPadCreationFailsOnEmptyPath(t *testing.T) {
expected := "device path must not be empty"
_, err := CreateTouchPad("", []byte("TouchDevice"), 0, 1024, 0, 768)
if err.Error() != expected {
t.Fatalf("Expected: %s\nActual: %s", expected, err)
}
}
func TestTouchPadCreationFailsOnNonExistentPathName(t *testing.T) {
path := "/some/bogus/path"
_, err := CreateTouchPad(path, []byte("TouchDevice"), 0, 1024, 0, 768)
if !os.IsNotExist(err) {
t.Fatalf("Expected: os.IsNotExist error\nActual: %s", err)
}
}
func TestTouchPadCreationFailsOnWrongPathName(t *testing.T) {
file, err := ioutil.TempFile(os.TempDir(), "uinput-touchpad-test-")
if err != nil {
t.Fatalf("Failed to setup test. Unable to create tempfile: %v", err)
}
defer file.Close()
expected := "failed to register key device: failed to close device: inappropriate ioctl for device"
_, err = CreateTouchPad(file.Name(), []byte("TouchDevice"), 0, 1024, 0, 768)
if err == nil || !(expected == err.Error()) {
t.Fatalf("Expected: %s\nActual: %s", expected, err)
}
}
func TestTouchPadCreationFailsIfNameIsTooLong(t *testing.T) {
name := "adsfdsferqewoirueworiuejdsfjdfa;ljoewrjeworiewuoruew;rj;kdlfjoeai;jfewoaifjef;das"
expected := fmt.Sprintf("device name %s is too long (maximum of %d characters allowed)", name, uinputMaxNameSize)
_, err := CreateTouchPad("/dev/uinput", []byte(name), 0, 1024, 0, 768)
if err.Error() != expected {
t.Fatalf("Expected: %s\nActual: %s", expected, err)
}
}
func TestTouchPadMoveToFailsOnClosedDevice(t *testing.T) {
absDev, err := CreateTouchPad("/dev/uinput", []byte("Test TouchPad"), 0, 1024, 0, 768)
if err != nil {
t.Fatalf("Failed to create the virtual touch pad. Last error was: %s\n", err)
}
_ = absDev.Close()
err = absDev.MoveTo(1, 1)
if err == nil {
t.Fatalf("Expected error due to closed device, but no error was returned.")
}
}
func TestTouchPadLeftClickFailsOnClosedDevice(t *testing.T) {
absDev, err := CreateTouchPad("/dev/uinput", []byte("Test TouchPad"), 0, 1024, 0, 768)
if err != nil {
t.Fatalf("Failed to create the virtual touch pad. Last error was: %s\n", err)
}
_ = absDev.Close()
err = absDev.LeftClick()
if err == nil {
t.Fatalf("Expected error due to closed device, but no error was returned.")
}
}
func TestTouchPadLeftPressFailsOnClosedDevice(t *testing.T) {
absDev, err := CreateTouchPad("/dev/uinput", []byte("Test TouchPad"), 0, 1024, 0, 768)
if err != nil {
t.Fatalf("Failed to create the virtual touch pad. Last error was: %s\n", err)
}
_ = absDev.Close()
err = absDev.LeftPress()
if err == nil {
t.Fatalf("Expected error due to closed device, but no error was returned.")
}
}
func TestTouchPadLeftReleaseFailsOnClosedDevice(t *testing.T) {
absDev, err := CreateTouchPad("/dev/uinput", []byte("Test TouchPad"), 0, 1024, 0, 768)
if err != nil {
t.Fatalf("Failed to create the virtual touch pad. Last error was: %s\n", err)
}
_ = absDev.Close()
err = absDev.LeftRelease()
if err == nil {
t.Fatalf("Expected error due to closed device, but no error was returned.")
}
}
func TestTouchPadRightClickFailsOnClosedDevice(t *testing.T) {
absDev, err := CreateTouchPad("/dev/uinput", []byte("Test TouchPad"), 0, 1024, 0, 768)
if err != nil {
t.Fatalf("Failed to create the virtual touch pad. Last error was: %s\n", err)
}
_ = absDev.Close()
err = absDev.RightClick()
if err == nil {
t.Fatalf("Expected error due to closed device, but no error was returned.")
}
}
func TestTouchPadRightPressFailsOnClosedDevice(t *testing.T) {
absDev, err := CreateTouchPad("/dev/uinput", []byte("Test TouchPad"), 0, 1024, 0, 768)
if err != nil {
t.Fatalf("Failed to create the virtual touch pad. Last error was: %s\n", err)
}
_ = absDev.Close()
err = absDev.RightPress()
if err == nil {
t.Fatalf("Expected error due to closed device, but no error was returned.")
}
}
func TestTouchPadRightReleaseFailsOnClosedDevice(t *testing.T) {
absDev, err := CreateTouchPad("/dev/uinput", []byte("Test TouchPad"), 0, 1024, 0, 768)
if err != nil {
t.Fatalf("Failed to create the virtual touch pad. Last error was: %s\n", err)
}
_ = absDev.Close()
err = absDev.RightRelease()
if err == nil {
t.Fatalf("Expected error due to closed device, but no error was returned.")
}
}
func TestMultipleTouchPadsWithDifferentSizes(t *testing.T) {
horizontal, err := CreateTouchPad("/dev/uinput", []byte("horizontal_pad"), 0, 200, 0, 100)
if err != nil {
t.Fatalf("Failed to create the virtual touch pad. Last error was: %s\n", err)
}
defer horizontal.Close()
vertical, err := CreateTouchPad("/dev/uinput", []byte("vertical_pad"), 0, 100, 0, 200)
if err != nil {
t.Fatalf("Failed to create the virtual touch pad. Last error was: %s\n", err)
}
defer vertical.Close()
err = horizontal.MoveTo(200, 100)
if err != nil {
t.Fatalf("Unable to move cursor on horizontal pad: %v", err)
}
err = vertical.MoveTo(100, 200)
if err != nil {
t.Fatalf("Unable to move cursor on horizontal pad: %v", err)
}
}
func TestPositioningInUpperLeftCorner(t *testing.T) {
dev, err := CreateTouchPad("/dev/uinput", []byte("touchpad"), 0, 200, 0, 100)
if err != nil {
t.Fatalf("Failed to create the virtual touch pad. Last error was: %s\n", err)
}
defer dev.Close()
err = dev.MoveTo(0, 0)
if err != nil {
t.Fatalf("Failed to move cursor to upper left corner: %v", err)
}
}
func TestSingleTouchEvent(t *testing.T) {
dev, err := CreateTouchPad("/dev/uinput", []byte("touchpad"), 0, 200, 0, 100)
if err != nil {
t.Fatalf("Failed to create the virtual touch pad. Last error was: %s\n", err)
}
defer dev.Close()
err = dev.TouchDown()
if err != nil {
t.Fatalf("Failed to issue touch down event: %v", err)
}
err = dev.TouchUp()
if err != nil {
t.Fatalf("Failed to issue touch up event: %v", err)
}
}
func TestTouchPadSyspath(t *testing.T) {
dev, err := CreateTouchPad("/dev/uinput", []byte("TouchPad"), 0, 1024, 0, 768)
if err != nil {
t.Fatalf("Failed to create the virtual touch pad. Last error was: %s\n", err)
}
sysPath, err := dev.FetchSyspath()
if err != nil {
t.Fatalf("Failed to fetch syspath. Last error was: %s\n", err)
}
if sysPath[:32] != "/sys/devices/virtual/input/input" {
t.Fatalf("Expected syspath to start with /sys/devices/virtual/input/input, but got %s", sysPath)
}
t.Logf("Syspath: %s", sysPath)
}