-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscroll_test.go
More file actions
334 lines (300 loc) · 8.11 KB
/
Copy pathscroll_test.go
File metadata and controls
334 lines (300 loc) · 8.11 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
// Copyright 2026 The gogpu Authors
// SPDX-License-Identifier: MIT
package gpucontext
import (
"testing"
"time"
)
func TestScrollPhase_Values(t *testing.T) {
// Verify scroll phase constants are sequential starting from 0
if ScrollPhaseNone != 0 {
t.Errorf("ScrollPhaseNone = %d, want 0", ScrollPhaseNone)
}
if ScrollPhaseBegan != 1 {
t.Errorf("ScrollPhaseBegan = %d, want 1", ScrollPhaseBegan)
}
if ScrollPhaseChanged != 2 {
t.Errorf("ScrollPhaseChanged = %d, want 2", ScrollPhaseChanged)
}
if ScrollPhaseEnded != 3 {
t.Errorf("ScrollPhaseEnded = %d, want 3", ScrollPhaseEnded)
}
if ScrollPhaseCanceled != 4 {
t.Errorf("ScrollPhaseCanceled = %d, want 4", ScrollPhaseCanceled)
}
}
func TestScrollPhase_String(t *testing.T) {
tests := []struct {
phase ScrollPhase
want string
}{
{ScrollPhaseNone, "None"},
{ScrollPhaseBegan, "Began"},
{ScrollPhaseChanged, "Changed"},
{ScrollPhaseEnded, "Ended"},
{ScrollPhaseCanceled, "Canceled"},
{ScrollPhase(99), "Unknown"},
}
for _, tt := range tests {
t.Run(tt.want, func(t *testing.T) {
if got := tt.phase.String(); got != tt.want {
t.Errorf("ScrollPhase(%d).String() = %q, want %q", tt.phase, got, tt.want)
}
})
}
}
func TestScrollDeltaMode_String(t *testing.T) {
tests := []struct {
mode ScrollDeltaMode
want string
}{
{ScrollDeltaPixel, "Pixel"},
{ScrollDeltaLine, "Line"},
{ScrollDeltaPage, "Page"},
{ScrollDeltaMode(99), "Unknown"},
}
for _, tt := range tests {
t.Run(tt.want, func(t *testing.T) {
if got := tt.mode.String(); got != tt.want {
t.Errorf("ScrollDeltaMode(%d).String() = %q, want %q", tt.mode, got, tt.want)
}
})
}
}
func TestScrollDeltaMode_Values(t *testing.T) {
// Verify delta mode constants are sequential
if ScrollDeltaPixel != 0 {
t.Errorf("ScrollDeltaPixel = %d, want 0", ScrollDeltaPixel)
}
if ScrollDeltaLine != 1 {
t.Errorf("ScrollDeltaLine = %d, want 1", ScrollDeltaLine)
}
if ScrollDeltaPage != 2 {
t.Errorf("ScrollDeltaPage = %d, want 2", ScrollDeltaPage)
}
}
func TestScrollEvent_ZeroValue(t *testing.T) {
var ev ScrollEvent
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.DeltaX != 0 {
t.Errorf("Zero value DeltaX = %f, want 0", ev.DeltaX)
}
if ev.DeltaY != 0 {
t.Errorf("Zero value DeltaY = %f, want 0", ev.DeltaY)
}
if ev.DeltaMode != ScrollDeltaPixel {
t.Errorf("Zero value DeltaMode = %v, want ScrollDeltaPixel", ev.DeltaMode)
}
if ev.Modifiers != 0 {
t.Errorf("Zero value Modifiers = %d, want 0", ev.Modifiers)
}
if ev.Timestamp != 0 {
t.Errorf("Zero value Timestamp = %v, want 0", ev.Timestamp)
}
if ev.Phase != ScrollPhaseNone {
t.Errorf("Zero value Phase = %v, want ScrollPhaseNone", ev.Phase)
}
if ev.IsMomentum {
t.Error("Zero value IsMomentum = true, want false")
}
}
func TestScrollEvent_FullConstruction(t *testing.T) {
ev := ScrollEvent{
X: 100.5,
Y: 200.5,
DeltaX: 10.0,
DeltaY: -20.0,
DeltaMode: ScrollDeltaLine,
Modifiers: ModControl,
Timestamp: time.Millisecond * 5000,
}
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.DeltaX != 10.0 {
t.Errorf("DeltaX = %f, want 10.0", ev.DeltaX)
}
if ev.DeltaY != -20.0 {
t.Errorf("DeltaY = %f, want -20.0", ev.DeltaY)
}
if ev.DeltaMode != ScrollDeltaLine {
t.Errorf("DeltaMode = %v, want ScrollDeltaLine", ev.DeltaMode)
}
if !ev.Modifiers.HasControl() {
t.Error("Modifiers should have control")
}
if ev.Timestamp != time.Millisecond*5000 {
t.Errorf("Timestamp = %v, want %v", ev.Timestamp, time.Millisecond*5000)
}
}
func TestScrollEvent_VerticalScroll(t *testing.T) {
// Typical vertical scroll event from mouse wheel
ev := ScrollEvent{
X: 400,
Y: 300,
DeltaX: 0,
DeltaY: 3, // Scroll down 3 lines
DeltaMode: ScrollDeltaLine,
}
if ev.DeltaX != 0 {
t.Errorf("DeltaX = %f, want 0", ev.DeltaX)
}
if ev.DeltaY != 3 {
t.Errorf("DeltaY = %f, want 3", ev.DeltaY)
}
if ev.DeltaMode != ScrollDeltaLine {
t.Errorf("DeltaMode = %v, want ScrollDeltaLine", ev.DeltaMode)
}
}
func TestScrollEvent_HorizontalScroll(t *testing.T) {
// Horizontal scroll from trackpad
ev := ScrollEvent{
X: 400,
Y: 300,
DeltaX: 50, // Scroll right 50 pixels
DeltaY: 0,
DeltaMode: ScrollDeltaPixel,
}
if ev.DeltaX != 50 {
t.Errorf("DeltaX = %f, want 50", ev.DeltaX)
}
if ev.DeltaY != 0 {
t.Errorf("DeltaY = %f, want 0", ev.DeltaY)
}
if ev.DeltaMode != ScrollDeltaPixel {
t.Errorf("DeltaMode = %v, want ScrollDeltaPixel", ev.DeltaMode)
}
}
func TestScrollEvent_PageScroll(t *testing.T) {
// Page scroll (rare, but possible)
ev := ScrollEvent{
X: 400,
Y: 300,
DeltaX: 0,
DeltaY: 1, // Scroll down 1 page
DeltaMode: ScrollDeltaPage,
}
if ev.DeltaY != 1 {
t.Errorf("DeltaY = %f, want 1", ev.DeltaY)
}
if ev.DeltaMode != ScrollDeltaPage {
t.Errorf("DeltaMode = %v, want ScrollDeltaPage", ev.DeltaMode)
}
}
func TestScrollEvent_CtrlScroll(t *testing.T) {
// Ctrl+scroll typically means zoom
ev := ScrollEvent{
X: 400,
Y: 300,
DeltaX: 0,
DeltaY: -1,
DeltaMode: ScrollDeltaLine,
Modifiers: ModControl,
}
if !ev.Modifiers.HasControl() {
t.Error("Should detect Ctrl modifier for zoom detection")
}
}
func TestScrollEvent_WithPhaseAndMomentum(t *testing.T) {
// macOS trackpad momentum event
ev := ScrollEvent{
X: 400,
Y: 300,
DeltaX: 0,
DeltaY: -5.5,
DeltaMode: ScrollDeltaPixel,
Phase: ScrollPhaseChanged,
IsMomentum: true,
}
if ev.Phase != ScrollPhaseChanged {
t.Errorf("Phase = %v, want ScrollPhaseChanged", ev.Phase)
}
if !ev.IsMomentum {
t.Error("IsMomentum = false, want true")
}
if ev.DeltaY != -5.5 {
t.Errorf("DeltaY = %f, want -5.5", ev.DeltaY)
}
}
func TestScrollEvent_GestureLifecycle(t *testing.T) {
// Simulate a complete macOS trackpad gesture lifecycle
phases := []struct {
phase ScrollPhase
isMomentum bool
desc string
}{
{ScrollPhaseBegan, false, "finger touch"},
{ScrollPhaseChanged, false, "finger drag"},
{ScrollPhaseChanged, false, "finger drag"},
{ScrollPhaseEnded, false, "finger lift"},
{ScrollPhaseBegan, true, "momentum start"},
{ScrollPhaseChanged, true, "momentum coast"},
{ScrollPhaseChanged, true, "momentum coast"},
{ScrollPhaseEnded, true, "momentum stop"},
}
for i, tt := range phases {
ev := ScrollEvent{
Phase: tt.phase,
IsMomentum: tt.isMomentum,
}
if ev.Phase != tt.phase {
t.Errorf("step %d (%s): Phase = %v, want %v", i, tt.desc, ev.Phase, tt.phase)
}
if ev.IsMomentum != tt.isMomentum {
t.Errorf("step %d (%s): IsMomentum = %v, want %v", i, tt.desc, ev.IsMomentum, tt.isMomentum)
}
}
}
func TestNullScrollEventSource(t *testing.T) {
// NullScrollEventSource should implement ScrollEventSource
var ses ScrollEventSource = NullScrollEventSource{}
// Method should be callable without panic
ses.OnScrollEvent(func(ScrollEvent) {})
}
// mockScrollEventSource is used to verify ScrollEventSource interface.
type mockScrollEventSource struct {
handler func(ScrollEvent)
}
func (m *mockScrollEventSource) OnScrollEvent(fn func(ScrollEvent)) {
m.handler = fn
}
// Ensure mockScrollEventSource implements ScrollEventSource.
var _ ScrollEventSource = &mockScrollEventSource{}
func TestScrollEventSource_Interface(t *testing.T) {
// Verify ScrollEventSource can be used through the interface
mock := &mockScrollEventSource{}
var source ScrollEventSource = mock
var received *ScrollEvent
source.OnScrollEvent(func(ev ScrollEvent) {
received = &ev
})
// Simulate event dispatch
testEvent := ScrollEvent{
X: 100,
Y: 200,
DeltaX: 0,
DeltaY: -120, // Scroll up 120 pixels
DeltaMode: ScrollDeltaPixel,
}
mock.handler(testEvent)
if received == nil {
t.Fatal("Handler was not called")
}
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)
}
if received.DeltaY != -120 {
t.Errorf("received.DeltaY = %f, want -120", received.DeltaY)
}
}