-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointer_test.go
More file actions
387 lines (357 loc) · 9.91 KB
/
pointer_test.go
File metadata and controls
387 lines (357 loc) · 9.91 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// Copyright 2026 The gogpu Authors
// SPDX-License-Identifier: MIT
package gpucontext
import (
"testing"
"time"
)
func TestPointerEventType_String(t *testing.T) {
tests := []struct {
eventType PointerEventType
want string
}{
{PointerDown, "PointerDown"},
{PointerUp, "PointerUp"},
{PointerMove, "PointerMove"},
{PointerEnter, "PointerEnter"},
{PointerLeave, "PointerLeave"},
{PointerCancel, "PointerCancel"},
{PointerEventType(99), "Unknown"},
}
for _, tt := range tests {
t.Run(tt.want, func(t *testing.T) {
if got := tt.eventType.String(); got != tt.want {
t.Errorf("PointerEventType(%d).String() = %q, want %q", tt.eventType, got, tt.want)
}
})
}
}
func TestPointerType_String(t *testing.T) {
tests := []struct {
pointerType PointerType
want string
}{
{PointerTypeMouse, "Mouse"},
{PointerTypeTouch, "Touch"},
{PointerTypePen, "Pen"},
{PointerType(99), "Unknown"},
}
for _, tt := range tests {
t.Run(tt.want, func(t *testing.T) {
if got := tt.pointerType.String(); got != tt.want {
t.Errorf("PointerType(%d).String() = %q, want %q", tt.pointerType, got, tt.want)
}
})
}
}
func TestButton_String(t *testing.T) {
tests := []struct {
button Button
want string
}{
{ButtonNone, "None"},
{ButtonLeft, "Left"},
{ButtonMiddle, "Middle"},
{ButtonRight, "Right"},
{ButtonX1, "X1"},
{ButtonX2, "X2"},
{ButtonEraser, "Eraser"},
{Button(99), "Unknown"},
}
for _, tt := range tests {
t.Run(tt.want, func(t *testing.T) {
if got := tt.button.String(); got != tt.want {
t.Errorf("Button(%d).String() = %q, want %q", tt.button, got, tt.want)
}
})
}
}
func TestButtonConstants(t *testing.T) {
// Verify W3C-compliant button values
if ButtonNone != -1 {
t.Errorf("ButtonNone = %d, want -1", ButtonNone)
}
if ButtonLeft != 0 {
t.Errorf("ButtonLeft = %d, want 0", ButtonLeft)
}
if ButtonMiddle != 1 {
t.Errorf("ButtonMiddle = %d, want 1", ButtonMiddle)
}
if ButtonRight != 2 {
t.Errorf("ButtonRight = %d, want 2", ButtonRight)
}
if ButtonX1 != 3 {
t.Errorf("ButtonX1 = %d, want 3", ButtonX1)
}
if ButtonX2 != 4 {
t.Errorf("ButtonX2 = %d, want 4", ButtonX2)
}
}
func TestButtons_HasMethods(t *testing.T) {
tests := []struct {
name string
buttons Buttons
left bool
right bool
middle bool
x1 bool
x2 bool
eraser bool
}{
{"none", ButtonsNone, false, false, false, false, false, false},
{"left only", ButtonsLeft, true, false, false, false, false, false},
{"right only", ButtonsRight, false, true, false, false, false, false},
{"middle only", ButtonsMiddle, false, false, true, false, false, false},
{"x1 only", ButtonsX1, false, false, false, true, false, false},
{"x2 only", ButtonsX2, false, false, false, false, true, false},
{"eraser only", ButtonsEraser, false, false, false, false, false, true},
{"left and right", ButtonsLeft | ButtonsRight, true, true, false, false, false, false},
{"all buttons", ButtonsLeft | ButtonsRight | ButtonsMiddle | ButtonsX1 | ButtonsX2 | ButtonsEraser,
true, true, true, true, true, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.buttons.HasLeft(); got != tt.left {
t.Errorf("Buttons.HasLeft() = %v, want %v", got, tt.left)
}
if got := tt.buttons.HasRight(); got != tt.right {
t.Errorf("Buttons.HasRight() = %v, want %v", got, tt.right)
}
if got := tt.buttons.HasMiddle(); got != tt.middle {
t.Errorf("Buttons.HasMiddle() = %v, want %v", got, tt.middle)
}
if got := tt.buttons.HasX1(); got != tt.x1 {
t.Errorf("Buttons.HasX1() = %v, want %v", got, tt.x1)
}
if got := tt.buttons.HasX2(); got != tt.x2 {
t.Errorf("Buttons.HasX2() = %v, want %v", got, tt.x2)
}
if got := tt.buttons.HasEraser(); got != tt.eraser {
t.Errorf("Buttons.HasEraser() = %v, want %v", got, tt.eraser)
}
})
}
}
func TestButtons_Count(t *testing.T) {
tests := []struct {
buttons Buttons
count int
}{
{ButtonsNone, 0},
{ButtonsLeft, 1},
{ButtonsLeft | ButtonsRight, 2},
{ButtonsLeft | ButtonsRight | ButtonsMiddle, 3},
{ButtonsLeft | ButtonsRight | ButtonsMiddle | ButtonsX1 | ButtonsX2 | ButtonsEraser, 6},
}
for _, tt := range tests {
if got := tt.buttons.Count(); got != tt.count {
t.Errorf("Buttons(%d).Count() = %d, want %d", tt.buttons, got, tt.count)
}
}
}
func TestButtonsConstants(t *testing.T) {
// Verify button bitmask values
if ButtonsNone != 0 {
t.Errorf("ButtonsNone = %d, want 0", ButtonsNone)
}
if ButtonsLeft != 1 {
t.Errorf("ButtonsLeft = %d, want 1", ButtonsLeft)
}
if ButtonsRight != 2 {
t.Errorf("ButtonsRight = %d, want 2", ButtonsRight)
}
if ButtonsMiddle != 4 {
t.Errorf("ButtonsMiddle = %d, want 4", ButtonsMiddle)
}
if ButtonsX1 != 8 {
t.Errorf("ButtonsX1 = %d, want 8", ButtonsX1)
}
if ButtonsX2 != 16 {
t.Errorf("ButtonsX2 = %d, want 16", ButtonsX2)
}
if ButtonsEraser != 32 {
t.Errorf("ButtonsEraser = %d, want 32", ButtonsEraser)
}
}
func TestPointerEvent_ZeroValue(t *testing.T) {
var ev PointerEvent
if ev.Type != PointerDown {
t.Errorf("Zero value Type = %v, want PointerDown", ev.Type)
}
if ev.PointerID != 0 {
t.Errorf("Zero value PointerID = %d, want 0", ev.PointerID)
}
if ev.X != 0 {
t.Errorf("Zero value X = %f, want 0", ev.X)
}
if ev.Y != 0 {
t.Errorf("Zero value Y = %f, want 0", ev.Y)
}
if ev.Pressure != 0 {
t.Errorf("Zero value Pressure = %f, want 0", ev.Pressure)
}
if ev.PointerType != PointerTypeMouse {
t.Errorf("Zero value PointerType = %v, want PointerTypeMouse", ev.PointerType)
}
if ev.IsPrimary {
t.Error("Zero value IsPrimary should be false")
}
}
func TestPointerEvent_FullConstruction(t *testing.T) {
ev := PointerEvent{
Type: PointerMove,
PointerID: 42,
X: 100.5,
Y: 200.5,
Pressure: 0.75,
TiltX: 15.0,
TiltY: -10.0,
Twist: 45.0,
Width: 20.0,
Height: 25.0,
PointerType: PointerTypePen,
IsPrimary: true,
Button: ButtonNone,
Buttons: ButtonsLeft | ButtonsMiddle,
Modifiers: ModShift | ModControl,
Timestamp: time.Millisecond * 12345,
}
if ev.Type != PointerMove {
t.Errorf("Type = %v, want PointerMove", ev.Type)
}
if ev.PointerID != 42 {
t.Errorf("PointerID = %d, want 42", ev.PointerID)
}
if ev.X != 100.5 {
t.Errorf("X = %f, want 100.5", ev.X)
}
if ev.Y != 200.5 {
t.Errorf("Y = %f, want 200.5", ev.Y)
}
if ev.Pressure != 0.75 {
t.Errorf("Pressure = %f, want 0.75", ev.Pressure)
}
if ev.TiltX != 15.0 {
t.Errorf("TiltX = %f, want 15.0", ev.TiltX)
}
if ev.TiltY != -10.0 {
t.Errorf("TiltY = %f, want -10.0", ev.TiltY)
}
if ev.Twist != 45.0 {
t.Errorf("Twist = %f, want 45.0", ev.Twist)
}
if ev.Width != 20.0 {
t.Errorf("Width = %f, want 20.0", ev.Width)
}
if ev.Height != 25.0 {
t.Errorf("Height = %f, want 25.0", ev.Height)
}
if ev.PointerType != PointerTypePen {
t.Errorf("PointerType = %v, want PointerTypePen", ev.PointerType)
}
if !ev.IsPrimary {
t.Error("IsPrimary should be true")
}
if ev.Button != ButtonNone {
t.Errorf("Button = %v, want ButtonNone", ev.Button)
}
if !ev.Buttons.HasLeft() {
t.Error("Buttons should have left")
}
if !ev.Buttons.HasMiddle() {
t.Error("Buttons should have middle")
}
if !ev.Modifiers.HasShift() {
t.Error("Modifiers should have shift")
}
if !ev.Modifiers.HasControl() {
t.Error("Modifiers should have control")
}
if ev.Timestamp != time.Millisecond*12345 {
t.Errorf("Timestamp = %v, want %v", ev.Timestamp, time.Millisecond*12345)
}
}
func TestPointerEventType_Values(t *testing.T) {
// Verify event type constants are sequential
if PointerDown != 0 {
t.Errorf("PointerDown = %d, want 0", PointerDown)
}
if PointerUp != 1 {
t.Errorf("PointerUp = %d, want 1", PointerUp)
}
if PointerMove != 2 {
t.Errorf("PointerMove = %d, want 2", PointerMove)
}
if PointerEnter != 3 {
t.Errorf("PointerEnter = %d, want 3", PointerEnter)
}
if PointerLeave != 4 {
t.Errorf("PointerLeave = %d, want 4", PointerLeave)
}
if PointerCancel != 5 {
t.Errorf("PointerCancel = %d, want 5", PointerCancel)
}
}
func TestPointerType_Values(t *testing.T) {
// Verify pointer type constants are sequential
if PointerTypeMouse != 0 {
t.Errorf("PointerTypeMouse = %d, want 0", PointerTypeMouse)
}
if PointerTypeTouch != 1 {
t.Errorf("PointerTypeTouch = %d, want 1", PointerTypeTouch)
}
if PointerTypePen != 2 {
t.Errorf("PointerTypePen = %d, want 2", PointerTypePen)
}
}
func TestNullPointerEventSource(t *testing.T) {
// NullPointerEventSource should implement PointerEventSource
var pes PointerEventSource = NullPointerEventSource{}
// Method should be callable without panic
pes.OnPointer(func(PointerEvent) {})
}
// mockPointerEventSource is used to verify PointerEventSource interface.
type mockPointerEventSource struct {
handler func(PointerEvent)
}
func (m *mockPointerEventSource) OnPointer(fn func(PointerEvent)) {
m.handler = fn
}
// Ensure mockPointerEventSource implements PointerEventSource.
var _ PointerEventSource = &mockPointerEventSource{}
func TestPointerEventSource_Interface(t *testing.T) {
// Verify PointerEventSource can be used through the interface
mock := &mockPointerEventSource{}
var source PointerEventSource = mock
var received *PointerEvent
source.OnPointer(func(ev PointerEvent) {
received = &ev
})
// Simulate event dispatch
testEvent := PointerEvent{
Type: PointerDown,
PointerID: 1,
X: 100,
Y: 200,
PointerType: PointerTypeMouse,
IsPrimary: true,
Button: ButtonLeft,
Buttons: ButtonsLeft,
}
mock.handler(testEvent)
if received == nil {
t.Fatal("Handler was not called")
}
if received.Type != PointerDown {
t.Errorf("received.Type = %v, want PointerDown", received.Type)
}
if received.PointerID != 1 {
t.Errorf("received.PointerID = %d, want 1", received.PointerID)
}
if received.X != 100 {
t.Errorf("received.X = %f, want 100", received.X)
}
if received.Y != 200 {
t.Errorf("received.Y = %f, want 200", received.Y)
}
}