forked from fyne-io/fyne
/
util_test.go
150 lines (124 loc) · 4.04 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
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
package test_test
import (
"image"
"image/color"
"image/draw"
"os"
"testing"
"github.com/goki/freetype"
"github.com/goki/freetype/truetype"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/image/font"
"fyne.io/fyne"
"fyne.io/fyne/test"
"fyne.io/fyne/theme"
"fyne.io/fyne/widget"
)
func TestAssertCanvasTappableAt(t *testing.T) {
c := test.NewCanvas()
b := widget.NewButton("foo", nil)
c.SetContent(b)
c.Resize(fyne.NewSize(300, 300))
b.Resize(fyne.NewSize(100, 100))
b.Move(fyne.NewPos(100, 100))
tt := &testing.T{}
assert.True(t, test.AssertCanvasTappableAt(tt, c, fyne.NewPos(101, 101)), "tappable found")
assert.False(t, tt.Failed(), "test did not fail")
tt = &testing.T{}
assert.False(t, test.AssertCanvasTappableAt(tt, c, fyne.NewPos(99, 99)), "tappable not found")
assert.True(t, tt.Failed(), "test failed")
}
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 := font.Drawer{
Dst: txtImg,
Src: image.NewUniform(color.Black),
Face: face,
Dot: freetype.Pt(0, 50-face.Metrics().Descent.Ceil()),
}
d.DrawString("Hello!")
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 TestDrag(t *testing.T) {
c := test.NewCanvas()
c.SetPadded(false)
d := &draggable{}
c.SetContent(fyne.NewContainer(d))
c.Resize(fyne.NewSize(30, 30))
d.Resize(fyne.NewSize(20, 20))
d.Move(fyne.NewPos(10, 10))
test.Drag(c, fyne.NewPos(5, 5), 10, 10)
assert.Nil(t, d.event, "nothing happens if no draggable was found at position")
assert.False(t, d.wasDragged)
test.Drag(c, fyne.NewPos(15, 15), 17, 42)
assert.Equal(t, &fyne.DragEvent{
PointEvent: fyne.PointEvent{Position: fyne.Position{X: 5, Y: 5}},
DraggedX: 17,
DraggedY: 42,
}, d.event)
assert.True(t, d.wasDragged)
}
func TestScroll(t *testing.T) {
c := test.NewCanvas()
c.SetPadded(false)
s := &scrollable{}
c.SetContent(fyne.NewContainer(s))
c.Resize(fyne.NewSize(30, 30))
s.Resize(fyne.NewSize(20, 20))
s.Move(fyne.NewPos(10, 10))
test.Scroll(c, fyne.NewPos(5, 5), 10, 10)
assert.Nil(t, s.event, "nothing happens if no scrollable was found at position")
test.Scroll(c, fyne.NewPos(15, 15), 17, 42)
assert.Equal(t, &fyne.ScrollEvent{DeltaX: 17, DeltaY: 42}, s.event)
}
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
}
type draggable struct {
widget.BaseWidget
event *fyne.DragEvent
wasDragged bool
}
var _ fyne.Draggable = (*draggable)(nil)
func (d *draggable) Dragged(event *fyne.DragEvent) {
d.event = event
}
func (d *draggable) DragEnd() {
d.wasDragged = true
}
type scrollable struct {
widget.BaseWidget
event *fyne.ScrollEvent
}
var _ fyne.Scrollable = (*scrollable)(nil)
func (s *scrollable) Scrolled(event *fyne.ScrollEvent) {
s.event = event
}