-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathchild_test.go
387 lines (344 loc) · 10.1 KB
/
child_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
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
package furex
import (
"image"
"testing"
"time"
"github.com/hajimehoshi/ebiten/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestHandlers(t *testing.T) {
for scenario, fn := range map[string]func(
t *testing.T,
flex *View,
h *mockHandler,
frame image.Rectangle,
){
// "button touch": testButtonTouch,
// "mouse click": testMouchClick,
// "mouse move": testMouseMove,
"swipe": testSwipe,
} {
t.Run(scenario, func(t *testing.T) {
flex := &View{
Width: 300,
Height: 500,
Left: 100,
Top: 50,
Position: PositionAbsolute,
Direction: Column,
Justify: JustifyCenter,
AlignItems: AlignItemCenter,
}
flex2 := &View{
Width: 100,
Height: 200,
Direction: Column,
Justify: JustifyEnd,
AlignItems: AlignItemEnd,
}
flex.AddChild(flex2)
h := &mockHandler{}
flex2.AddChild(&View{
Width: 10,
Height: 20,
Handler: h,
})
// (0,0)
// ┌───────────────────────────────────┐
// │ view │
// │ (100,50) │
// │ ┌────────────────────────────┤
// │ │flex(300x500) │
// │ │ │
// │ │ │
// │ │ (100,150) │
// │ │ ┌─────────────────┐ │
// │ │ │flex2(100x200) │ │
// │ │ │ │ │
// │ │ │ ┌──────-──────┤ │
// │ │ │ │button(10x20)│ │
// │ │ │ │ │ │
// │ │ │ │ │ │
// │ │ │ │ │ │
// │ │ │ │ │ │
// │ │ │ │ │ │
// │ │ └───┴──────────-──┘ │
// │ │ (300,400) │
// │ │ │
// │ │ │
// └──────┴────────────────────────────┘
// (400,550)
// expected button frame:
// x = 300-10 = 290 to 300
// y = 400-20 = 380 to 400
flex.Update()
flex.Draw(nil)
frame := image.Rect(290, 380, 300, 400)
require.Equal(t, frame, h.Frame)
fn(t, flex, h, frame)
})
}
}
func testButtonTouch(t *testing.T, flex *View, h *mockHandler, frame image.Rectangle) {
type result struct {
IsPressed bool
IsReleased bool
IsCanceled bool
}
var tests = []struct {
Scenario string
Start image.Point
End image.Point
Want result
}{
{
Scenario: "press inside and release inside",
Start: frame.Min,
End: frame.Min,
Want: result{true, true, false},
},
{
Scenario: "press inside and release outside",
Start: frame.Min,
End: image.Pt(frame.Min.X, frame.Min.Y-1),
Want: result{true, true, true},
},
{
Scenario: "press inside and release inside (right-bottom)",
Start: frame.Max,
End: frame.Max,
Want: result{true, true, false},
},
{
Scenario: "press inside and release outside (right-bottom)",
Start: frame.Max,
End: image.Pt(frame.Max.X+1, frame.Max.Y),
Want: result{true, true, true},
},
{
Scenario: "press outside and release inside",
Start: image.Pt(frame.Min.X-1, frame.Min.Y),
End: image.Pt(frame.Min.X+frame.Dx()/2, frame.Min.Y+frame.Dy()/2),
Want: result{false, false, false},
},
}
for _, tt := range tests {
t.Run(tt.Scenario, func(t *testing.T) {
h.Init()
flex.HandleJustPressedTouchID(0, tt.Start.X, tt.Start.Y)
flex.HandleJustReleasedTouchID(0, tt.End.X, tt.End.Y)
assert.Equal(t, tt.Want, result{h.IsPressed, h.IsReleased, h.IsCancel})
})
}
}
func testMouchClick(t *testing.T, flex *View, h *mockHandler, frame image.Rectangle) {
type result struct {
IsPressed bool
IsReleased bool
IsCancel bool
}
var tests = []struct {
Scenario string
Start image.Point
End image.Point
Want result
}{
{
Scenario: "press inside and release inside",
Start: frame.Min,
End: frame.Min,
Want: result{true, true, false},
},
{
Scenario: "press inside left-top edge, release outside",
Start: frame.Min,
End: image.Pt(frame.Min.X, frame.Min.Y-1),
Want: result{true, true, true},
},
{
Scenario: "press inside righ-bottom edge, release inside",
Start: frame.Max,
End: frame.Max,
Want: result{true, true, false},
},
{
Scenario: "press inside righ-bottom edge, release outside",
Start: frame.Max,
End: image.Pt(frame.Max.X+1, frame.Max.Y),
Want: result{true, true, true},
},
{
Scenario: "press outside, release inside",
Start: image.Pt(frame.Min.X-1, frame.Min.Y),
End: image.Pt(frame.Min.X+frame.Dx()/2, frame.Min.Y+frame.Dy()/2),
Want: result{false, false, false},
},
}
for _, tt := range tests {
t.Run(tt.Scenario, func(t *testing.T) {
h.Init()
flex.handleMouseButtonLeftPressed(tt.Start.X, tt.Start.Y)
flex.handleMouseButtonLeftReleased(tt.End.X, tt.End.Y)
assert.Equal(t, tt.Want, result{h.IsPressed, h.IsReleased, h.IsCancel})
})
}
}
func testMouseMove(t *testing.T, flex *View, h *mockHandler, frame image.Rectangle) {
type result struct {
IsMouseMoved bool
MousePoint image.Point
}
var tests = []struct {
Scenario string
Point image.Point
Want result
}{
{
Scenario: "move mouse left-top inside",
Point: image.Point{frame.Min.X, frame.Min.Y},
Want: result{IsMouseMoved: true, MousePoint: image.Point{frame.Min.X, frame.Min.Y}},
},
{
Scenario: "move mouse right-bottom inside",
Point: image.Point{frame.Max.X, frame.Max.Y},
Want: result{IsMouseMoved: true, MousePoint: image.Point{frame.Max.X, frame.Max.Y}},
},
{
Scenario: "move mouse left outside",
Point: image.Point{frame.Min.X - 1, frame.Min.Y},
Want: result{IsMouseMoved: false, MousePoint: image.Point{-1, -1}},
},
{
Scenario: "move mouse right outside",
Point: image.Point{frame.Max.X + 1, frame.Min.Y},
Want: result{IsMouseMoved: false, MousePoint: image.Point{-1, -1}},
},
{
Scenario: "move mouse top outside",
Point: image.Point{frame.Min.X, frame.Min.Y - 1},
Want: result{IsMouseMoved: false, MousePoint: image.Point{-1, -1}},
},
{
Scenario: "move mouse bottom outside",
Point: image.Point{frame.Min.X, frame.Max.Y + 1},
Want: result{IsMouseMoved: false, MousePoint: image.Point{-1, -1}},
},
}
for _, tt := range tests {
t.Run(tt.Scenario, func(t *testing.T) {
h.Init()
flex.handleMouse(tt.Point.X, tt.Point.Y)
assert.Equal(t, tt.Want, result{h.IsMouseMoved, h.MousePoint})
})
}
}
func testSwipe(t *testing.T, flex *View, h *mockHandler, frame image.Rectangle) {
type result struct {
IsSwiped bool
SwipeDir SwipeDirection
}
var tests = []struct {
Scenario string
From image.Point
To image.Point
Time time.Duration
Want result
}{
{
Scenario: "swipe left",
From: image.Point{frame.Min.X, frame.Min.Y},
To: image.Point{frame.Min.X - 50, frame.Min.Y},
Time: time.Duration(0),
Want: result{IsSwiped: true, SwipeDir: SwipeDirectionLeft},
},
{
Scenario: "swipe right",
From: image.Point{frame.Min.X, frame.Min.Y},
To: image.Point{frame.Min.X + 50, frame.Min.Y},
Time: time.Millisecond * 50,
Want: result{IsSwiped: true, SwipeDir: SwipeDirectionRight},
},
{
Scenario: "swipe down",
From: image.Point{frame.Min.X, frame.Min.Y},
To: image.Point{frame.Min.X, frame.Min.Y + 50},
Time: time.Millisecond * 50,
Want: result{IsSwiped: true, SwipeDir: SwipeDirectionDown},
},
{
Scenario: "swipe slow",
From: image.Point{frame.Min.X, frame.Min.Y},
To: image.Point{frame.Min.X, frame.Min.Y + 50},
Time: time.Millisecond * 301,
Want: result{IsSwiped: false},
},
{
Scenario: "swipe short",
From: image.Point{frame.Min.X, frame.Min.Y},
To: image.Point{frame.Min.X, frame.Min.Y + 49},
Time: time.Millisecond * 50,
Want: result{IsSwiped: false},
},
}
for _, tt := range tests {
t.Run(tt.Scenario, func(t *testing.T) {
h.Init()
flex.HandleJustPressedTouchID(0, tt.From.X, tt.From.Y)
<-time.After(tt.Time)
flex.HandleJustReleasedTouchID(0, tt.To.X, tt.To.Y)
if tt.Want.IsSwiped {
assert.Equal(t, tt.Want, result{h.IsSwiped, h.SwipeDir})
} else {
assert.False(t, h.IsSwiped)
}
})
}
}
type mockHandler struct {
mockFlags
Frame image.Rectangle
MousePoint image.Point
SwipeDir SwipeDirection
}
type mockFlags struct {
IsPressed bool
IsReleased bool
IsCancel bool
IsUpdated bool
IsDrawn bool
IsMouseMoved bool
IsSwiped bool
}
var _ DrawHandler = (*mockHandler)(nil)
var _ UpdateHandler = (*mockHandler)(nil)
var _ ButtonHandler = (*mockHandler)(nil)
var _ MouseHandler = (*mockHandler)(nil)
var _ SwipeHandler = (*mockHandler)(nil)
func (h *mockHandler) Init() {
h.mockFlags = mockFlags{}
h.MousePoint = image.Pt(-1, -1)
}
func (h *mockHandler) HandleUpdate() {
h.IsUpdated = true
}
func (h *mockHandler) HandleDraw(screen *ebiten.Image, frame image.Rectangle) {
h.Frame = frame
h.IsDrawn = true
}
func (h *mockHandler) HandlePress(x, y int, t ebiten.TouchID) {
h.IsPressed = true
}
func (h *mockHandler) HandleRelease(x, y int, isCancel bool) {
h.IsReleased = true
h.IsCancel = isCancel
}
func (h *mockHandler) HandleMouse(x, y int) bool {
h.IsMouseMoved = true
h.MousePoint = image.Pt(x, y)
return true
}
func (h *mockHandler) HandleSwipe(dir SwipeDirection) {
h.IsSwiped = true
h.SwipeDir = dir
}