-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
util_test.go
90 lines (76 loc) · 2.9 KB
/
util_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
package test_test
import (
"fmt"
"image"
"image/color"
"image/draw"
"os"
"testing"
"fyne.io/fyne/v2/internal/painter"
"github.com/goki/freetype"
"github.com/goki/freetype/truetype"
"github.com/stretchr/testify/require"
"fyne.io/fyne/v2/internal/test"
"fyne.io/fyne/v2/theme"
"github.com/stretchr/testify/assert"
)
func TestAssertImageMatches(t *testing.T) {
bounds := image.Rect(0, 0, 100, 50)
img := image.NewNRGBA(bounds)
draw.Draw(img, bounds, image.NewUniform(color.White), image.Point{}, draw.Src)
txtImg := image.NewNRGBA(bounds)
opts := truetype.Options{Size: 20, DPI: 96}
f, _ := truetype.Parse(theme.TextFont().Content())
face := truetype.NewFace(f, &opts)
d := painter.FontDrawer{}
d.Dst = txtImg
d.Src = image.NewUniform(color.Black)
d.Face = face
d.Dot = freetype.Pt(0, 50-face.Metrics().Descent.Ceil())
d.DrawString("Hello!", 4)
draw.Draw(img, bounds, txtImg, image.Point{}, draw.Over)
tt := &testing.T{}
assert.False(t, test.AssertImageMatches(tt, "non_existing_master.png", img), "non existing master is not equal a given image")
assert.True(t, tt.Failed(), "test failed")
assert.Equal(t, img, readImage(t, "testdata/failed/non_existing_master.png"), "image was written to disk")
tt = &testing.T{}
assert.True(t, test.AssertImageMatches(tt, "master.png", img), "existing master is equal a given image")
assert.False(t, tt.Failed(), "test did not fail")
tt = &testing.T{}
assert.False(t, test.AssertImageMatches(tt, "diffing_master.png", img), "existing master is not equal a given image")
assert.True(t, tt.Failed(), "test did not fail")
assert.Equal(t, img, readImage(t, "testdata/failed/diffing_master.png"), "image was written to disk")
if !t.Failed() {
os.RemoveAll("testdata/failed")
}
}
func TestNewCheckedImage(t *testing.T) {
img := test.NewCheckedImage(10, 10, 5, 2)
expectedColorValues := [][]uint8{
{0xff, 0xff, 0x0, 0x0, 0xff, 0xff, 0x0, 0x0, 0xff, 0xff},
{0xff, 0xff, 0x0, 0x0, 0xff, 0xff, 0x0, 0x0, 0xff, 0xff},
{0xff, 0xff, 0x0, 0x0, 0xff, 0xff, 0x0, 0x0, 0xff, 0xff},
{0xff, 0xff, 0x0, 0x0, 0xff, 0xff, 0x0, 0x0, 0xff, 0xff},
{0xff, 0xff, 0x0, 0x0, 0xff, 0xff, 0x0, 0x0, 0xff, 0xff},
{0x0, 0x0, 0xff, 0xff, 0x0, 0x0, 0xff, 0xff, 0x0, 0x0},
{0x0, 0x0, 0xff, 0xff, 0x0, 0x0, 0xff, 0xff, 0x0, 0x0},
{0x0, 0x0, 0xff, 0xff, 0x0, 0x0, 0xff, 0xff, 0x0, 0x0},
{0x0, 0x0, 0xff, 0xff, 0x0, 0x0, 0xff, 0xff, 0x0, 0x0},
{0x0, 0x0, 0xff, 0xff, 0x0, 0x0, 0xff, 0xff, 0x0, 0x0},
}
for y, xv := range expectedColorValues {
for x, v := range xv {
assert.Equal(t, color.NRGBA{R: v, G: v, B: v, A: 0xff}, img.At(x, y), fmt.Sprintf("color value at %d,%d", x, y))
}
}
}
func readImage(t *testing.T, path string) image.Image {
file, err := os.Open(path)
require.NoError(t, err)
defer file.Close()
raw, _, err := image.Decode(file)
require.NoError(t, err)
img := image.NewNRGBA(raw.Bounds())
draw.Draw(img, img.Bounds(), raw, image.Pt(0, 0), draw.Src)
return img
}