-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathebitest.go
More file actions
351 lines (296 loc) · 7.93 KB
/
Copy pathebitest.go
File metadata and controls
351 lines (296 loc) · 7.93 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
package ebitest
import (
"context"
"fmt"
"image"
"image/color"
"image/draw"
"image/png"
"log"
"os"
"path/filepath"
"testing"
"github.com/google/uuid"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/text/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
baseDumpFoler = "_ebitest_dump/"
findAllSelectors = true
)
var (
emptyRec image.Rectangle
)
type Ebitest struct {
game *Game
PingPong *PingPong
ctxCancelFn context.CancelFunc
endGameChan chan struct{}
options options
}
type options struct {
face text.Face
color color.Color
dumpErrorImages bool
}
type optionsFn func(*options)
// WithFace set's the default Face for when checking for texts
func WithFace(f text.Face) optionsFn {
return func(o *options) {
o.face = f
}
}
// WithColor set's the default Color for when checking for texts
func WithColor(c color.Color) optionsFn {
return func(o *options) {
o.color = c
}
}
// WithDumpErrorImages enables the option to output a custom image when a test fails
// that has the screen and the image that was tried to match in order to debug it
func WithDumpErrorImages() optionsFn {
return func(o *options) {
o.dumpErrorImages = true
}
}
func Run(game ebiten.Game, opts ...optionsFn) *Ebitest {
ctx, cfn := context.WithCancel(context.TODO())
pingPong := NewPingPong()
g := newGame(ctx, game, pingPong)
endGameChan := make(chan struct{})
go func() {
ebiten.RunGame(g)
endGameChan <- struct{}{}
}()
et := &Ebitest{
game: g,
ctxCancelFn: cfn,
PingPong: pingPong,
endGameChan: endGameChan,
}
op := options{}
for _, ofn := range opts {
ofn(&op)
}
et.options = op
et.PingPong.Ping()
if et.options.dumpErrorImages {
os.RemoveAll(baseDumpFoler)
os.MkdirAll(baseDumpFoler, 0777)
}
return et
}
// Close stops the underlying game
func (e *Ebitest) Close() {
e.ctxCancelFn()
<-e.endGameChan
close(e.endGameChan)
}
// Should checks if selector(s) is present in the game and returns it
// s can be a: 'string', 'image.Image', '*ebiten.Image' and '*ebitest.Selector'
func (e *Ebitest) Should(t *testing.T, s interface{}) (*Selector, bool) {
t.Helper()
e.PingPong.Ping()
sc := e.game.GetScreen()
sel, ok := e.findSelector(sc, s)
if !ok {
msg := "selector not found"
if e.options.dumpErrorImages {
p := dumpErrorImages(sc, sel)
msg += "\nimage at: " + p
}
assert.Fail(t, msg)
return nil, false
}
return sel, true
}
// ShouldNot checks if selector(s) is not present in the game
// s can be a: 'string', 'image.Image', '*ebiten.Image' and '*ebitest.Selector'
func (e *Ebitest) ShouldNot(t *testing.T, s interface{}) bool {
t.Helper()
e.PingPong.Ping()
sc := e.game.GetScreen()
sel, ok := e.findSelector(sc, s)
if !ok {
return true
}
msg := "selector found"
if e.options.dumpErrorImages {
p := dumpErrorImages(sc, sel)
msg += "\nimage at: " + p
}
assert.Fail(t, msg)
return false
}
// Must checks if selector(s) is present in the game and returns it.
// If it's not present it'll fail the test
// s can be a: 'string', 'image.Image', '*ebiten.Image' and '*ebitest.Selector'
func (e *Ebitest) Must(t *testing.T, s interface{}) *Selector {
t.Helper()
e.PingPong.Ping()
sc := e.game.GetScreen()
sel, ok := e.findSelector(sc, s)
if !ok {
msg := "selector not found"
if e.options.dumpErrorImages {
p := dumpErrorImages(sc, sel)
msg += "\nimage at: " + p
}
require.Fail(t, msg)
return nil
}
return sel
}
// MustNot checks if selector(s) is not present in the game.
// If it's present it'll fail the test
// s can be a: 'string', 'image.Image', '*ebiten.Image' and '*ebitest.Selector'
func (e *Ebitest) MustNot(t *testing.T, s interface{}) {
t.Helper()
e.PingPong.Ping()
sc := e.game.GetScreen()
sel, ok := e.findSelector(sc, s)
if !ok {
return
}
msg := "selector found"
if e.options.dumpErrorImages {
p := dumpErrorImages(sc, sel)
msg += "\nimage at: " + p
}
require.Fail(t, msg)
}
// GetAll returns all the repeated instances of s or none if nothing is found
func (e *Ebitest) GetAll(s interface{}) []*Selector {
sc := e.game.GetScreen()
sels, _ := e.findSelectors(sc, s, findAllSelectors)
return sels
}
// KeyTap taps all the keys at once
func (e *Ebitest) KeyTap(keys ...ebiten.Key) {
if len(keys) == 0 {
return
}
e.PingPong.KeyTapPing(Ball{KeyTap: BallKeyTap{Keys: keys}})
}
// getSelector converts s to the right Selector initialization
func (e *Ebitest) getSelector(s interface{}) *Selector {
switch v := s.(type) {
case string:
return NewFromText(v, e.options.face, e.options.color)
case *ebiten.Image:
return NewFromImage(ebitenImageToImage(v))
case image.Image:
return NewFromImage(v)
case *Selector:
return NewFromImage(v.Image())
default:
panic(fmt.Sprintf("Invalid Selector of type %T, the supported ones are: 'string', 'image.Image', '*ebiten.Image' and '*ebitest.Selector'", s))
}
}
// findSelector returns a Selector from ss if found. `all` will basically mean it'll return all of them
func (e *Ebitest) findSelector(sc image.Image, ss interface{}) (*Selector, bool) {
sels, sel := e.findSelectors(sc, ss, !findAllSelectors)
if len(sels) == 0 {
return sel, false
}
return sels[0], true
}
// findSelector returns a Selector from ss if found. `all` will basically mean it'll return all of them
func (e *Ebitest) findSelectors(sc image.Image, ss interface{}, all bool) ([]*Selector, *Selector) {
selectors := make([]*Selector, 0)
bsel := e.getSelector(ss)
sx := sc.Bounds().Dx()
sy := sc.Bounds().Dy()
for x := range sx {
for y := range sy {
if hasImageAt(sc, bsel.Image(), x, y) {
sel := NewFromImage(bsel.Image())
selx := sel.Image().Bounds().Dx()
sely := sel.Image().Bounds().Dy()
sel.rect = image.Rect(x, y, x+selx, y+sely)
sel.PingPong = e.PingPong
selectors = append(selectors, sel)
if !all {
return selectors, bsel
}
}
}
}
return selectors, bsel
}
// hasImageAt checks if the image sub is image i at the ix, iy
func hasImageAt(i, sub image.Image, ix, iy int) bool {
sx, sy := sub.Bounds().Dx(), sub.Bounds().Dy()
for x := range sx {
for y := range sy {
sc := sub.At(x, y)
ic := i.At(ix+x, iy+y)
nsc := sc.(color.NRGBA)
// If the source it's transparent we ignore it
// we want to only compare colors so we consider
// it as good
if nsc.A != 255 {
continue
}
if !equalColors(sc, ic) {
return false
}
}
}
return true
}
// equalColors checks if c1 and c2 have the same RGB
func equalColors(c1, c2 color.Color) bool {
r1, g1, b1, _ := c1.RGBA()
r2, g2, b2, _ := c2.RGBA()
return r1 == r2 && g1 == g2 && b1 == b2
}
// dumpErrorImages dumps a composition of the 2 images into 1 so it displays
// what was checked
func dumpErrorImages(s image.Image, sel *Selector) string {
i := sel.Image()
sb := s.Bounds()
ib := i.Bounds()
x := sb.Dx() + ib.Dx()
y := sb.Dy()
img := image.NewRGBA(image.Rect(0, 0, x, y))
draw.Draw(img, sb, s, image.Point{}, draw.Over)
draw.Draw(img, image.Rect(sb.Dx(), 0, x, ib.Dy()), i, image.Point{}, draw.Over)
if sel.Rec() != emptyRec {
drawRectangle(img, sel.Rec(), 2)
}
u, _ := uuid.NewV7()
ip := filepath.Join(baseDumpFoler, u.String()+".png")
writeImage(ip, img)
wd, _ := os.Getwd()
return filepath.Join(wd, ip)
}
// drawRectangle will draw in the image(img) the rectangel(rec) with thiknes
func drawRectangle(img *image.RGBA, rec image.Rectangle, thickness int) {
col := color.RGBA{255, 0, 0, 255}
for t := 0; t < thickness; t++ {
// draw horizontal lines
for x := rec.Min.X; x <= rec.Max.X; x++ {
img.Set(x, rec.Min.Y+t, col)
img.Set(x, rec.Max.Y-t, col)
}
// draw vertical lines
for y := rec.Min.Y; y <= rec.Max.Y; y++ {
img.Set(rec.Min.X+t, y, col)
img.Set(rec.Max.X-t, y, col)
}
}
}
// writeImage writes on the path the image i
func writeImage(path string, i image.Image) {
f, err := os.Create(path)
if err != nil {
log.Fatal(err)
}
defer f.Close()
if err := png.Encode(f, i); err != nil {
log.Fatal(err)
}
}