forked from aykevl/tinygl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
screen_test.go
195 lines (172 loc) · 4.8 KB
/
screen_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
package tinygl_test
import (
"flag"
"image"
"image/color"
"image/png"
"math/rand"
"os"
"path/filepath"
"testing"
"github.com/aykevl/tinygl"
"github.com/aykevl/tinygl/font/roboto"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/pixel"
)
var flagUpdate = flag.Bool("update", false, "update test outputs")
type testCase struct {
name string
test func(t *testing.T, name string, img ImageDisplay)
}
func TestScreen(t *testing.T) {
for _, tc := range []testCase{
{"hello-banner", testHelloBanner},
{"flex-grow", testGrowable},
} {
t.Run(tc.name, func(t *testing.T) {
img := NewImageDisplay(160, 128) // same size as ST7735
// Run the test.
tc.test(t, tc.name, img)
})
}
}
func makeScreen(img ImageDisplay) *tinygl.Screen[pixel.RGB888] {
buf := pixel.NewImage[pixel.RGB888](160, 8)
screen := tinygl.NewScreen[pixel.RGB888](img, buf, 120) // 120PPI
return screen
}
func testImage(t *testing.T, name string, img ImageDisplay) {
path := filepath.Join("testdata", name+".png")
golden := loadImage(t, path)
if x, y, equal := isSameImage(img.image, golden); !equal {
if *flagUpdate {
// -update flag was passed, so update the PNG file.
f, err := os.Create(path)
if err != nil {
t.Fatal("could not open test output:", err)
}
defer f.Close()
png.Encode(f, img.image)
} else {
t.Errorf("image %s is not the same (mismatch at x=%d, y=%d)", path, x, y)
}
}
}
func loadImage(t *testing.T, path string) image.Image {
f, err := os.Open(path)
if err != nil {
if *flagUpdate {
return nil
}
t.Fatal("could not open test output:", err)
}
defer f.Close()
golden, err := png.Decode(f)
if err != nil {
t.Fatal("could not decode test output:", err)
}
return golden
}
func testHelloBanner(t *testing.T, name string, img ImageDisplay) {
screen := makeScreen(img)
font := roboto.Regular24
foreground := pixel.NewRGB888(0xff, 0xff, 0xff)
background := pixel.NewRGB888(64, 64, 64)
topbar := tinygl.NewText(font, foreground, pixel.NewRGB888(255, 0, 0), "Hello world!")
timelabel := tinygl.NewText(font, foreground, pixel.NewRGB888(0, 0, 0), "00:00")
all := tinygl.NewVBox[pixel.RGB888](background, topbar, timelabel)
screen.SetChild(all)
screen.Update()
testImage(t, name+"-0", img)
timelabel.SetText("00:00:00")
screen.Update()
testImage(t, name+"-1", img)
}
func testGrowable(t *testing.T, name string, img ImageDisplay) {
screen := makeScreen(img)
font := roboto.Regular24
foreground := pixel.NewRGB888(0xff, 0xff, 0xff)
background := pixel.NewRGB888(64, 64, 64)
topbar := tinygl.NewText(font, foreground, pixel.NewRGB888(255, 0, 0), "Grow objects")
line1 := tinygl.NewText(font, foreground, pixel.NewRGB888(0, 0, 0), "line 1")
line2 := tinygl.NewText(font, foreground, pixel.NewRGB888(0, 0, 255), "line 2")
all := tinygl.NewVBox[pixel.RGB888](background, topbar, line1, line2)
screen.SetChild(all)
// Test with a single element that is growable (it takes up all remaining
// space).
line1.SetGrowable(0, 1)
screen.Update()
testImage(t, name+"-0", img)
// Add a second element that's growable. This time, it has a factor of 2, so
// it will get two thirds of the slack pixels.
line2.SetGrowable(0, 2)
screen.Update()
testImage(t, name+"-1", img)
}
type ImageDisplay struct {
image *image.NRGBA
}
func NewImageDisplay(width, height int) ImageDisplay {
// Create a new image, initialized with random data (to make non-drawn areas
// very obvious).
img := ImageDisplay{
image: image.NewNRGBA(image.Rect(0, 0, width, height)),
}
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
r := rand.Uint32()
c := color.RGBA{
R: uint8(r >> 0),
G: uint8(r >> 8),
B: uint8(r >> 16),
}
img.image.Set(x, y, c)
}
}
return img
}
func (img ImageDisplay) Size() (int16, int16) {
rect := img.image.Rect.Size()
return int16(rect.X), int16(rect.Y)
}
func (img ImageDisplay) DrawBitmap(startX, startY int16, image pixel.Image[pixel.RGB888]) error {
width, height := image.Size()
for y := 0; y < int(height); y++ {
for x := 0; x < int(width); x++ {
inCol := image.Get(x, y)
c := color.NRGBA{
R: inCol.R,
G: inCol.G,
B: inCol.B,
A: 0xff,
}
img.image.SetNRGBA(x+int(startX), y+int(startY), c)
}
}
return nil
}
func (img ImageDisplay) Rotation() drivers.Rotation {
return drivers.Rotation0
}
func (img ImageDisplay) Display() error {
return nil // no-op
}
func isSameImage(image1, image2 image.Image) (x, y int, equal bool) {
if image1 == nil || image2 == nil {
return 0, 0, false
}
if image1.Bounds() != image2.Bounds() {
return 0, 0, false
}
rect := image1.Bounds().Size()
for y := 0; y < rect.Y; y++ {
for x := 0; x < rect.X; x++ {
r1, g1, b1, a1 := image1.At(x, y).RGBA()
r2, g2, b2, a2 := image2.At(x, y).RGBA()
if r1 != r2 || g1 != g2 || b1 != b2 || a1 != a2 {
return x, y, false
}
}
}
return 0, 0, true
}