-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinput_event_test.go
More file actions
161 lines (145 loc) · 4.08 KB
/
Copy pathinput_event_test.go
File metadata and controls
161 lines (145 loc) · 4.08 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
// Copyright 2026 The gogpu Authors
// SPDX-License-Identifier: MIT
package gpucontext
import "testing"
func TestInputEventInterface(t *testing.T) {
events := []InputEvent{
KeyEvent{Key: KeyA, Mods: ModShift, Pressed: true},
KeyEvent{Key: KeyEscape, Pressed: false},
CharEvent{Char: 'A'},
FocusEvent{Focused: true},
FocusEvent{Focused: false},
ResizeEvent{Width: 800, Height: 600},
ScaleChangedEvent{ScaleFactor: 2.0, Width: 800, Height: 600},
PointerEvent{Type: PointerDown, X: 100, Y: 200},
PointerEvent{Type: PointerMove, X: 150, Y: 250},
ScrollEvent{DeltaY: -3.0, DeltaMode: ScrollDeltaLine},
}
if len(events) != 10 {
t.Fatalf("expected 10 events, got %d", len(events))
}
for i, ev := range events {
if ev == nil {
t.Errorf("event %d is nil", i)
}
}
}
func TestInputEventTypeSwitch(t *testing.T) {
events := []InputEvent{
KeyEvent{Key: KeyA, Pressed: true},
PointerEvent{Type: PointerDown, X: 10, Y: 20},
ScrollEvent{DeltaY: 1.0},
CharEvent{Char: 'x'},
FocusEvent{Focused: true},
ResizeEvent{Width: 1024, Height: 768},
ScaleChangedEvent{ScaleFactor: 2.0, Width: 800, Height: 600},
}
counts := map[string]int{}
for _, ev := range events {
switch ev.(type) {
case KeyEvent:
counts["key"]++
case PointerEvent:
counts["pointer"]++
case ScrollEvent:
counts["scroll"]++
case CharEvent:
counts["char"]++
case FocusEvent:
counts["focus"]++
case ResizeEvent:
counts["resize"]++
case ScaleChangedEvent:
counts["scale"]++
default:
t.Errorf("unexpected event type: %T", ev)
}
}
for _, name := range []string{"key", "pointer", "scroll", "char", "focus", "resize", "scale"} {
if counts[name] != 1 {
t.Errorf("expected 1 %s event, got %d", name, counts[name])
}
}
}
func TestKeyEventFields(t *testing.T) {
ev := KeyEvent{Key: KeyA, Mods: ModShift | ModControl, Pressed: true}
if ev.Key != KeyA {
t.Errorf("Key = %v, want KeyA", ev.Key)
}
if !ev.Mods.HasShift() {
t.Error("expected HasShift")
}
if !ev.Mods.HasControl() {
t.Error("expected HasControl")
}
if !ev.Pressed {
t.Error("expected Pressed = true")
}
}
func TestCharEventFields(t *testing.T) {
ev := CharEvent{Char: '日'}
if ev.Char != '日' {
t.Errorf("Char = %q, want '日'", ev.Char)
}
}
func TestFocusEventFields(t *testing.T) {
gained := FocusEvent{Focused: true}
lost := FocusEvent{Focused: false}
if !gained.Focused {
t.Error("expected Focused = true")
}
if lost.Focused {
t.Error("expected Focused = false")
}
}
func TestResizeEventFields(t *testing.T) {
ev := ResizeEvent{Width: 1920, Height: 1080}
if ev.Width != 1920 || ev.Height != 1080 {
t.Errorf("got %dx%d, want 1920x1080", ev.Width, ev.Height)
}
}
func TestScaleChangedEventFields(t *testing.T) {
ev := ScaleChangedEvent{ScaleFactor: 2.0, Width: 800, Height: 600}
if ev.ScaleFactor != 2.0 {
t.Errorf("ScaleFactor = %v, want 2.0", ev.ScaleFactor)
}
if ev.Width != 800 || ev.Height != 600 {
t.Errorf("got %dx%d, want 800x600", ev.Width, ev.Height)
}
}
func TestScaleChangedEventImplementsInputEvent(t *testing.T) {
var ev InputEvent = ScaleChangedEvent{ScaleFactor: 1.5, Width: 1024, Height: 768}
se, ok := ev.(ScaleChangedEvent)
if !ok {
t.Fatal("ScaleChangedEvent does not implement InputEvent")
}
if se.ScaleFactor != 1.5 {
t.Errorf("ScaleFactor = %v, want 1.5", se.ScaleFactor)
}
}
func TestPointerEventImplementsInputEvent(t *testing.T) {
var ev InputEvent = PointerEvent{Type: PointerDown, X: 50, Y: 100}
pe, ok := ev.(PointerEvent)
if !ok {
t.Fatal("PointerEvent does not implement InputEvent")
}
if pe.Type != PointerDown {
t.Errorf("Type = %v, want PointerDown", pe.Type)
}
if pe.X != 50 || pe.Y != 100 {
t.Errorf("coordinates = (%v, %v), want (50, 100)", pe.X, pe.Y)
}
}
func TestScrollEventImplementsInputEvent(t *testing.T) {
var ev InputEvent = ScrollEvent{DeltaY: -1.0, Phase: ScrollPhaseBegan}
se, ok := ev.(ScrollEvent)
if !ok {
t.Fatal("ScrollEvent does not implement InputEvent")
}
if se.DeltaY != -1.0 {
t.Errorf("DeltaY = %v, want -1.0", se.DeltaY)
}
if se.Phase != ScrollPhaseBegan {
t.Errorf("Phase = %v, want ScrollPhaseBegan", se.Phase)
}
}