/
testlib.go
339 lines (275 loc) · 8.03 KB
/
testlib.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
package testlib
import (
"bytes"
"fmt"
"image"
"image/color"
"image/draw"
"image/png"
"os"
"path/filepath"
"runtime"
"time"
"git.tideland.biz/goas/loop"
"github.com/remogatto/mandala"
"github.com/remogatto/mathgl"
gl "github.com/remogatto/opengles2"
"github.com/remogatto/prettytest"
"github.com/remogatto/shaders"
"github.com/remogatto/shapes"
)
const (
// We don't need high framerate for testing
FramesPerSecond = 15
expectedImgPath = "drawable"
)
type world struct {
width, height int
projMatrix mathgl.Mat4f
viewMatrix mathgl.Mat4f
}
type TestSuite struct {
prettytest.Suite
rlControl *renderLoopControl
timeout <-chan time.Time
testDraw chan image.Image
renderState *renderState
outputPath string
}
type renderLoopControl struct {
window chan mandala.Window
drawFunc chan func()
}
type renderState struct {
window mandala.Window
boxProgram, segmentProgram shaders.Program
}
func (renderState *renderState) init(window mandala.Window) {
window.MakeContextCurrent()
renderState.window = window
width, height := window.GetSize()
// Set the viewport
gl.Viewport(0, 0, gl.Sizei(width), gl.Sizei(height))
gl.ClearColor(0.0, 0.0, 0.0, 1.0)
renderState.boxProgram = shaders.NewProgram(shapes.DefaultBoxFS, shapes.DefaultBoxVS)
renderState.segmentProgram = shaders.NewProgram(shapes.DefaultSegmentFS, shapes.DefaultSegmentVS)
}
func newRenderLoopControl() *renderLoopControl {
return &renderLoopControl{
drawFunc: make(chan func()),
window: make(chan mandala.Window),
}
}
// Timeout timeouts the tests after the given duration.
func (t *TestSuite) Timeout(timeout time.Duration) {
t.timeout = time.After(timeout)
}
// Run runs renderLoop. The loop renders a frame and swaps the buffer
// at each tick received.
func (t *TestSuite) renderLoopFunc(control *renderLoopControl) loop.LoopFunc {
return func(loop loop.Loop) error {
// renderState stores rendering state variables such
// as the EGL state
t.renderState = new(renderState)
// Lock/unlock the loop to the current OS thread. This is
// necessary because OpenGL functions should be called from
// the same thread.
runtime.LockOSThread()
defer runtime.UnlockOSThread()
window := <-control.window
t.renderState.init(window)
for {
select {
case drawFunc := <-control.drawFunc:
drawFunc()
}
}
}
}
// eventLoopFunc is listening for events originating from the
// framwork.
func (t *TestSuite) eventLoopFunc(renderLoopControl *renderLoopControl) loop.LoopFunc {
return func(loop loop.Loop) error {
for {
select {
// Receive events from the framework.
case untypedEvent := <-mandala.Events():
switch event := untypedEvent.(type) {
case mandala.CreateEvent:
case mandala.StartEvent:
case mandala.NativeWindowCreatedEvent:
renderLoopControl.window <- event.Window
case mandala.ActionUpDownEvent:
case mandala.ActionMoveEvent:
case mandala.NativeWindowDestroyedEvent:
case mandala.DestroyEvent:
case mandala.NativeWindowRedrawNeededEvent:
case mandala.PauseEvent:
case mandala.ResumeEvent:
}
}
}
}
}
func (t *TestSuite) timeoutLoopFunc() loop.LoopFunc {
return func(loop loop.Loop) error {
time := <-t.timeout
err := fmt.Errorf("Tests timed out after %v", time)
mandala.Logf("%s %s", err.Error(), mandala.Stacktrace())
t.Error(err)
return nil
}
}
func (t *TestSuite) BeforeAll() {
// Create rendering loop control channels
t.rlControl = newRenderLoopControl()
// Start the rendering loop
loop.GoRecoverable(
t.renderLoopFunc(t.rlControl),
func(rs loop.Recoverings) (loop.Recoverings, error) {
for _, r := range rs {
mandala.Logf("%s", r.Reason)
mandala.Logf("%s", mandala.Stacktrace())
}
return rs, fmt.Errorf("Unrecoverable loop\n")
},
)
// Start the event loop
loop.GoRecoverable(
t.eventLoopFunc(t.rlControl),
func(rs loop.Recoverings) (loop.Recoverings, error) {
for _, r := range rs {
mandala.Logf("%s", r.Reason)
mandala.Logf("%s", mandala.Stacktrace())
}
return rs, fmt.Errorf("Unrecoverable loop\n")
},
)
if t.timeout != nil {
// Start the timeout loop
loop.GoRecoverable(
t.timeoutLoopFunc(),
func(rs loop.Recoverings) (loop.Recoverings, error) {
for _, r := range rs {
mandala.Logf("%s", r.Reason)
mandala.Logf("%s", mandala.Stacktrace())
}
return rs, fmt.Errorf("Unrecoverable loop\n")
},
)
}
}
func newWorld(width, height int) *world {
return &world{
width: width,
height: height,
projMatrix: mathgl.Ortho2D(0, float32(width), -float32(height/2), float32(height/2)),
viewMatrix: mathgl.Ident4f(),
}
}
func (w *world) Projection() mathgl.Mat4f {
return w.projMatrix
}
func (w *world) View() mathgl.Mat4f {
return w.viewMatrix
}
func (w *world) addImageAsTexture(filename string) uint32 {
var texBuffer uint32
texImg, err := loadImageResource(filename)
if err != nil {
panic(err)
}
b := texImg.Bounds()
rgbaImage := image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
draw.Draw(rgbaImage, rgbaImage.Bounds(), texImg, b.Min, draw.Src)
width, height := gl.Sizei(b.Dx()), gl.Sizei(b.Dy())
gl.GenTextures(1, &texBuffer)
gl.BindTexture(gl.TEXTURE_2D, texBuffer)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Void(&rgbaImage.Pix[0]))
return texBuffer
}
// loadImageResource loads an image with the given filename from the
// resource folder.
func loadImageResource(filename string) (image.Image, error) {
request := mandala.LoadResourceRequest{
Filename: filepath.Join(expectedImgPath, filename),
Response: make(chan mandala.LoadResourceResponse),
}
mandala.ResourceManager() <- request
response := <-request.Response
buffer := response.Buffer
if response.Error != nil {
return nil, response.Error
}
img, err := png.Decode(bytes.NewReader(buffer))
if err != nil {
return nil, err
}
return img, nil
}
// Create an image containing both expected and actual images, side by
// side.
func saveExpAct(outputPath string, filename string, exp image.Image, act image.Image) {
// Build the destination rectangle
expRect := exp.Bounds()
actRect := act.Bounds()
unionRect := expRect.Union(actRect)
dstRect := image.Rectangle{
image.ZP,
image.Point{unionRect.Max.X * 3, unionRect.Max.Y},
}
// Create the empty destination image
dstImage := image.NewRGBA(dstRect)
// Copy the expected image
dp := image.Point{
(unionRect.Max.X-unionRect.Min.X)/2 - (expRect.Max.X-expRect.Min.X)/2,
(unionRect.Max.Y-unionRect.Min.Y)/2 - (expRect.Max.Y-expRect.Min.Y)/2,
}
r := image.Rectangle{dp, dp.Add(expRect.Size())}
draw.Draw(dstImage, r, exp, image.ZP, draw.Src)
// Copy the actual image
dp = image.Point{
(unionRect.Max.X-unionRect.Min.X)/2 - (actRect.Max.X-expRect.Min.X)/2,
(unionRect.Max.Y-unionRect.Min.Y)/2 - (actRect.Max.Y-expRect.Min.Y)/2,
}
dp = dp.Add(image.Point{unionRect.Max.X, 0})
r = image.Rectangle{dp, dp.Add(actRect.Size())}
draw.Draw(dstImage, r, act, image.ZP, draw.Src)
// Re-copy the actual image
dp = dp.Add(image.Point{unionRect.Max.X, 0})
r = image.Rectangle{dp, dp.Add(actRect.Size())}
draw.Draw(dstImage, r, act, image.ZP, draw.Src)
// Composite expected over actual
dp = image.Point{dp.X, unionRect.Min.Y}
dstRect = image.Rectangle{dp, dp.Add(expRect.Size())}
draw.DrawMask(dstImage, dstRect, exp, image.ZP, &image.Uniform{color.RGBA{A: 64}}, image.ZP, draw.Over)
_, err := os.Stat(outputPath)
if os.IsNotExist(err) {
// Create the output dir
err := os.Mkdir(outputPath, 0777)
if err != nil {
panic(err)
}
} else if err != nil {
panic(err)
}
// Save the output file
file, err := os.Create(filepath.Join(outputPath, filename))
if err != nil {
panic(err)
}
defer file.Close()
err = png.Encode(file, dstImage)
if err != nil {
panic(err)
}
}
func NewTestSuite(outputPath string) *TestSuite {
return &TestSuite{
rlControl: newRenderLoopControl(),
testDraw: make(chan image.Image),
outputPath: outputPath,
}
}