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
/
multitouch_test.go
225 lines (183 loc) · 6.56 KB
/
multitouch_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
package uinput
import (
"fmt"
"io/ioutil"
"os"
"testing"
"time"
)
func TestBasicMultiTouchMoves(t *testing.T) {
absDev, err := CreateMultiTouch("/dev/uinput", []byte("Test MultiTouch"), 0, 1024, 0, 768, 3)
if err != nil {
t.Fatalf("Failed to create the virtual multi touch device. Last error was: %s\n", err)
}
defer func(absDev MultiTouch) {
err := absDev.Close()
if err != nil {
t.Fatalf("Failed to close device. Last error was: %s\n", err)
}
}(absDev)
contacts := absDev.GetContacts()
if len(contacts) != 3 {
t.Fatalf("Failed to create contacts, expected 3, got %s", fmt.Sprint(len(contacts)))
}
err = contacts[0].TouchDownAt(0, 0)
if err != nil {
t.Fatalf("Failed to move contact 0 to initial position. Last error was: %s\n", err)
}
err = contacts[0].TouchDownAt(100, 200)
if err != nil {
t.Fatalf("Failed to move contact 0 to position x:100, y:200. Last error was: %s\n", err)
}
}
func TestBasicMultiTouchGesture(t *testing.T) {
absDev, err := CreateMultiTouch("/dev/uinput", []byte("Test MultiTouch"), 0, 1024, 0, 768, 3)
if err != nil {
t.Fatalf("Failed to create the virtual multi touch device. Last error was: %s\n", err)
}
defer func(absDev MultiTouch) {
err := absDev.Close()
if err != nil {
t.Fatalf("Failed to close device. Last error was: %s\n", err)
}
}(absDev)
contacts := absDev.GetContacts()
if len(contacts) != 3 {
t.Fatalf("Failed to create contacts, expected 3, got %s", fmt.Sprint(len(contacts)))
}
for i := int32(0); i < 200; i++ {
time.Sleep(3 * time.Millisecond)
for n := int32(0); n < 3; n++ {
y := 255 - i
err := contacts[n].TouchDownAt(0, y)
if err != nil {
t.Fatalf("Failed to move contact %s at [0, %s]", fmt.Sprint(i), fmt.Sprint(y))
}
}
}
}
func TestMultiTouchCreationFailsOnEmptyPath(t *testing.T) {
expected := "device path must not be empty"
_, err := CreateMultiTouch("", []byte("TouchDevice"), 0, 1024, 0, 768, 3)
if err.Error() != expected {
t.Fatalf("Expected: %s\nActual: %s", expected, err)
}
}
func TestMultiTouchCreationFailsOnNonExistentPathName(t *testing.T) {
path := "/some/bogus/path"
_, err := CreateMultiTouch(path, []byte("TouchDevice"), 0, 1024, 0, 768, 3)
if !os.IsNotExist(err) {
t.Fatalf("Expected: os.IsNotExist error\nActual: %s", err)
}
}
func TestMultiTouchCreationFailsOnWrongPathName(t *testing.T) {
file, err := ioutil.TempFile(os.TempDir(), "uinput-MultiTouch-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 = CreateMultiTouch(file.Name(), []byte("TouchDevice"), 0, 1024, 0, 768, 3)
if err == nil || !(expected == err.Error()) {
t.Fatalf("Expected: %s\nActual: %s", expected, err)
}
}
func TestMultiTouchCreationFailsIfNameIsTooLong(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 := CreateMultiTouch("/dev/uinput", []byte(name), 0, 1024, 0, 768, 3)
if err.Error() != expected {
t.Fatalf("Expected: %s\nActual: %s", expected, err)
}
}
func TestMultiTouchMoveToFailsOnClosedDevice(t *testing.T) {
absDev, err := CreateMultiTouch("/dev/uinput", []byte("Test MultiTouch"), 0, 1024, 0, 768, 3)
if err != nil {
t.Fatalf("Failed to create the virtual touch pad. Last error was: %s\n", err)
}
_ = absDev.Close()
contacts := absDev.GetContacts()
if len(contacts) != 3 {
t.Fatalf("Failed to create contacts, expected 3, got %s", fmt.Sprint(len(contacts)))
}
err = contacts[0].TouchDownAt(1, 1)
if err == nil {
t.Fatalf("Expected error due to closed device, but no error was returned.")
}
}
func TestMultipleMultiTouchsWithDifferentSizes(t *testing.T) {
horizontal, err := CreateMultiTouch("/dev/uinput", []byte("horizontal_pad"), 0, 200, 0, 100, 3)
if err != nil {
t.Fatalf("Failed to create the virtual touch pad. Last error was: %s\n", err)
}
defer horizontal.Close()
vertical, err := CreateMultiTouch("/dev/uinput", []byte("vertical_pad"), 0, 100, 0, 200, 3)
if err != nil {
t.Fatalf("Failed to create the virtual touch pad. Last error was: %s\n", err)
}
defer vertical.Close()
contactsHorizontal := horizontal.GetContacts()
if len(contactsHorizontal) != 3 {
t.Fatalf("Failed to create contacts, expected 3, got %s", fmt.Sprint(len(contactsHorizontal)))
}
err = contactsHorizontal[0].TouchDownAt(200, 100)
if err != nil {
t.Fatalf("Unable to move cursor on horizontal pad: %v", err)
}
contactsVertical := vertical.GetContacts()
if len(contactsVertical) != 3 {
t.Fatalf("Failed to create contacts, expected 3, got %s", fmt.Sprint(len(contactsVertical)))
}
err = contactsHorizontal[0].TouchDownAt(100, 200)
if err != nil {
t.Fatalf("Unable to move cursor on horizontal pad: %v", err)
}
}
func TestMultiTouchPositioningInUpperLeftCorner(t *testing.T) {
dev, err := CreateMultiTouch("/dev/uinput", []byte("MultiTouch"), 0, 200, 0, 100, 3)
if err != nil {
t.Fatalf("Failed to create the virtual touch pad. Last error was: %s\n", err)
}
defer dev.Close()
contacts := dev.GetContacts()
if len(contacts) != 3 {
t.Fatalf("Failed to create contacts, expected 3, got %s", fmt.Sprint(len(contacts)))
}
err = contacts[0].TouchDownAt(0, 0)
if err != nil {
t.Fatalf("Failed to move cursor to upper left corner: %v", err)
}
}
func TestMultiTouchSingleTouchEvent(t *testing.T) {
dev, err := CreateMultiTouch("/dev/uinput", []byte("MultiTouch"), 0, 200, 0, 100, 3)
if err != nil {
t.Fatalf("Failed to create the virtual touch pad. Last error was: %s\n", err)
}
defer dev.Close()
contacts := dev.GetContacts()
if len(contacts) != 3 {
t.Fatalf("Failed to create contacts, expected 3, got %s", fmt.Sprint(len(contacts)))
}
err = contacts[0].TouchDownAt(0, 0)
if err != nil {
t.Fatalf("Failed to issue touch down event at [0,0]: %v", err)
}
err = contacts[0].TouchUp()
if err != nil {
t.Fatalf("Failed to issue touch up event [0,0]: %v", err)
}
}
func TestMultiTouchSyspath(t *testing.T) {
dev, err := CreateMultiTouch("/dev/uinput", []byte("MultiTouch"), 0, 1024, 0, 768, 3)
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)
}