forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.go
320 lines (285 loc) · 9.89 KB
/
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
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
package test
import (
"fmt"
"image"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/qmsu/fyne/v2"
"github.com/qmsu/fyne/v2/driver/desktop"
"github.com/qmsu/fyne/v2/internal/cache"
"github.com/qmsu/fyne/v2/internal/driver"
"github.com/qmsu/fyne/v2/internal/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// AssertCanvasTappableAt asserts that the canvas is tappable at the given position.
func AssertCanvasTappableAt(t *testing.T, c fyne.Canvas, pos fyne.Position) bool {
if o, _ := findTappable(c, pos); o == nil {
t.Errorf("No tappable found at %#v", pos)
return false
}
return true
}
// AssertImageMatches asserts that the given image is the same as the one stored in the master file.
// The master filename is relative to the `testdata` directory which is relative to the test.
// The test `t` fails if the given image is not equal to the loaded master image.
// In this case the given image is written into a file in `testdata/failed/<masterFilename>` (relative to the test).
// This path is also reported, thus the file can be used as new master.
func AssertImageMatches(t *testing.T, masterFilename string, img image.Image, msgAndArgs ...interface{}) bool {
return test.AssertImageMatches(t, masterFilename, img, msgAndArgs...)
}
// AssertRendersToMarkup asserts that the given canvas renders the same markup as the one stored in the master file.
// The master filename is relative to the `testdata` directory which is relative to the test.
// The test `t` fails if the rendered markup is not equal to the loaded master markup.
// In this case the rendered markup is written into a file in `testdata/failed/<masterFilename>` (relative to the test).
// This path is also reported, thus the file can be used as new master.
//
// Be aware, that the indentation has to use tab characters ('\t') instead of spaces.
// Every element starts on a new line indented one more than its parent.
// Closing elements stand on their own line, too, using the same indentation as the opening element.
// The only exception to this are text elements which do not contain line breaks unless the text includes them.
//
// Since: 2.0
func AssertRendersToMarkup(t *testing.T, masterFilename string, c fyne.Canvas, msgAndArgs ...interface{}) bool {
wd, err := os.Getwd()
require.NoError(t, err)
got := snapshot(c)
masterPath := filepath.Join(wd, "testdata", masterFilename)
failedPath := filepath.Join(wd, "testdata/failed", masterFilename)
_, err = os.Stat(masterPath)
if os.IsNotExist(err) {
require.NoError(t, writeMarkup(failedPath, got))
t.Errorf("Master not found at %s. Markup written to %s might be used as master.", masterPath, failedPath)
return false
}
raw, err := ioutil.ReadFile(masterPath)
require.NoError(t, err)
master := strings.ReplaceAll(string(raw), "\r", "")
var msg string
if len(msgAndArgs) > 0 {
msg = fmt.Sprintf(msgAndArgs[0].(string)+"\n", msgAndArgs[1:]...)
}
if !assert.Equal(t, master, got, "%sMarkup did not match master. Actual markup written to file://%s.", msg, failedPath) {
require.NoError(t, writeMarkup(failedPath, got))
return false
}
return true
}
// Drag drags at an absolute position on the canvas.
// deltaX/Y is the dragging distance: <0 for dragging up/left, >0 for dragging down/right.
func Drag(c fyne.Canvas, pos fyne.Position, deltaX, deltaY float32) {
matches := func(object fyne.CanvasObject) bool {
if _, ok := object.(fyne.Draggable); ok {
return true
}
return false
}
o, p, _ := driver.FindObjectAtPositionMatching(pos, matches, c.Overlays().Top(), c.Content())
if o == nil {
return
}
e := &fyne.DragEvent{
PointEvent: fyne.PointEvent{Position: p},
Dragged: fyne.Delta{DX: deltaX, DY: deltaY},
}
o.(fyne.Draggable).Dragged(e)
o.(fyne.Draggable).DragEnd()
}
// FocusNext focuses the next focusable on the canvas.
func FocusNext(c fyne.Canvas) {
if tc, ok := c.(*testCanvas); ok {
tc.focusManager().FocusNext()
} else {
fyne.LogError("FocusNext can only be called with a test canvas", nil)
}
}
// FocusPrevious focuses the previous focusable on the canvas.
func FocusPrevious(c fyne.Canvas) {
if tc, ok := c.(*testCanvas); ok {
tc.focusManager().FocusPrevious()
} else {
fyne.LogError("FocusPrevious can only be called with a test canvas", nil)
}
}
// LaidOutObjects returns all fyne.CanvasObject starting at the given fyne.CanvasObject which is laid out previously.
func LaidOutObjects(o fyne.CanvasObject) (objects []fyne.CanvasObject) {
if o != nil {
objects = layoutAndCollect(objects, o, o.MinSize().Max(o.Size()))
}
return objects
}
// MoveMouse simulates a mouse movement to the given position.
func MoveMouse(c fyne.Canvas, pos fyne.Position) {
if fyne.CurrentDevice().IsMobile() {
return
}
tc, _ := c.(*testCanvas)
var oldHovered, hovered desktop.Hoverable
if tc != nil {
oldHovered = tc.hovered
}
matches := func(object fyne.CanvasObject) bool {
if _, ok := object.(desktop.Hoverable); ok {
return true
}
return false
}
o, p, _ := driver.FindObjectAtPositionMatching(pos, matches, c.Overlays().Top(), c.Content())
if o != nil {
hovered = o.(desktop.Hoverable)
me := &desktop.MouseEvent{
PointEvent: fyne.PointEvent{
AbsolutePosition: pos,
Position: p,
},
}
if hovered == oldHovered {
hovered.MouseMoved(me)
} else {
if oldHovered != nil {
oldHovered.MouseOut()
}
hovered.MouseIn(me)
}
} else if oldHovered != nil {
oldHovered.MouseOut()
}
if tc != nil {
tc.hovered = hovered
}
}
// Scroll scrolls at an absolute position on the canvas.
// deltaX/Y is the scrolling distance: <0 for scrolling up/left, >0 for scrolling down/right.
func Scroll(c fyne.Canvas, pos fyne.Position, deltaX, deltaY float32) {
matches := func(object fyne.CanvasObject) bool {
if _, ok := object.(fyne.Scrollable); ok {
return true
}
return false
}
o, _, _ := driver.FindObjectAtPositionMatching(pos, matches, c.Overlays().Top(), c.Content())
if o == nil {
return
}
e := &fyne.ScrollEvent{Scrolled: fyne.Delta{DX: deltaX, DY: deltaY}}
o.(fyne.Scrollable).Scrolled(e)
}
// DoubleTap simulates a double left mouse click on the specified object.
func DoubleTap(obj fyne.DoubleTappable) {
ev, c := prepareTap(obj, fyne.NewPos(1, 1))
handleFocusOnTap(c, obj)
obj.DoubleTapped(ev)
}
// Tap simulates a left mouse click on the specified object.
func Tap(obj fyne.Tappable) {
TapAt(obj, fyne.NewPos(1, 1))
}
// TapAt simulates a left mouse click on the passed object at a specified place within it.
func TapAt(obj fyne.Tappable, pos fyne.Position) {
ev, c := prepareTap(obj, pos)
tap(c, obj, ev)
}
// TapCanvas taps at an absolute position on the canvas.
func TapCanvas(c fyne.Canvas, pos fyne.Position) {
if o, p := findTappable(c, pos); o != nil {
tap(c, o.(fyne.Tappable), &fyne.PointEvent{AbsolutePosition: pos, Position: p})
}
}
// TapSecondary simulates a right mouse click on the specified object.
func TapSecondary(obj fyne.SecondaryTappable) {
TapSecondaryAt(obj, fyne.NewPos(1, 1))
}
// TapSecondaryAt simulates a right mouse click on the passed object at a specified place within it.
func TapSecondaryAt(obj fyne.SecondaryTappable, pos fyne.Position) {
ev, c := prepareTap(obj, pos)
handleFocusOnTap(c, obj)
obj.TappedSecondary(ev)
}
// Type performs a series of key events to simulate typing of a value into the specified object.
// The focusable object will be focused before typing begins.
// The chars parameter will be input one rune at a time to the focused object.
func Type(obj fyne.Focusable, chars string) {
obj.FocusGained()
typeChars([]rune(chars), obj.TypedRune)
}
// TypeOnCanvas is like the Type function but it passes the key events to the canvas object
// rather than a focusable widget.
func TypeOnCanvas(c fyne.Canvas, chars string) {
typeChars([]rune(chars), c.OnTypedRune())
}
// ApplyTheme sets the given theme and waits for it to be applied to the current app.
func ApplyTheme(t *testing.T, theme fyne.Theme) {
require.IsType(t, &testApp{}, fyne.CurrentApp())
a := fyne.CurrentApp().(*testApp)
a.Settings().SetTheme(theme)
for a.lastAppliedTheme() != theme {
time.Sleep(1 * time.Millisecond)
}
}
// WidgetRenderer allows test scripts to gain access to the current renderer for a widget.
// This can be used for verifying correctness of rendered components for a widget in unit tests.
func WidgetRenderer(wid fyne.Widget) fyne.WidgetRenderer {
return cache.Renderer(wid)
}
// WithTestTheme runs a function with the testTheme temporarily set.
func WithTestTheme(t *testing.T, f func()) {
settings := fyne.CurrentApp().Settings()
current := settings.Theme()
ApplyTheme(t, NewTheme())
defer ApplyTheme(t, current)
f()
}
func findTappable(c fyne.Canvas, pos fyne.Position) (o fyne.CanvasObject, p fyne.Position) {
matches := func(object fyne.CanvasObject) bool {
_, ok := object.(fyne.Tappable)
return ok
}
o, p, _ = driver.FindObjectAtPositionMatching(pos, matches, c.Overlays().Top(), c.Content())
return
}
func prepareTap(obj interface{}, pos fyne.Position) (*fyne.PointEvent, fyne.Canvas) {
d := fyne.CurrentApp().Driver()
ev := &fyne.PointEvent{Position: pos}
var c fyne.Canvas
if co, ok := obj.(fyne.CanvasObject); ok {
c = d.CanvasForObject(co)
ev.AbsolutePosition = d.AbsolutePositionForObject(co).Add(pos)
}
return ev, c
}
func tap(c fyne.Canvas, obj fyne.Tappable, ev *fyne.PointEvent) {
handleFocusOnTap(c, obj)
obj.Tapped(ev)
}
func handleFocusOnTap(c fyne.Canvas, obj interface{}) {
if c == nil {
return
}
unfocus := true
if focus, ok := obj.(fyne.Focusable); ok {
if dis, ok := obj.(fyne.Disableable); !ok || !dis.Disabled() {
unfocus = false
if focus != c.Focused() {
unfocus = true
}
}
}
if unfocus {
c.Unfocus()
}
}
func typeChars(chars []rune, keyDown func(rune)) {
for _, char := range chars {
keyDown(char)
}
}
func writeMarkup(path string, markup string) error {
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
return err
}
return ioutil.WriteFile(path, []byte(markup), 0644)
}