test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. package test
  2. import (
  3. "fmt"
  4. "image"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "testing"
  9. "time"
  10. "fyne.io/fyne/v2"
  11. "fyne.io/fyne/v2/driver/desktop"
  12. "fyne.io/fyne/v2/internal/cache"
  13. "fyne.io/fyne/v2/internal/driver"
  14. "fyne.io/fyne/v2/internal/painter/software"
  15. "fyne.io/fyne/v2/internal/test"
  16. "github.com/stretchr/testify/assert"
  17. "github.com/stretchr/testify/require"
  18. )
  19. // AssertCanvasTappableAt asserts that the canvas is tappable at the given position.
  20. func AssertCanvasTappableAt(t *testing.T, c fyne.Canvas, pos fyne.Position) bool {
  21. if o, _ := findTappable(c, pos); o == nil {
  22. t.Errorf("No tappable found at %#v", pos)
  23. return false
  24. }
  25. return true
  26. }
  27. // AssertObjectRendersToImage asserts that the given `CanvasObject` renders the same image as the one stored in the master file.
  28. // The theme used is the standard test theme which may look different to how it shows on your device.
  29. // The master filename is relative to the `testdata` directory which is relative to the test.
  30. // The test `t` fails if the given image is not equal to the loaded master image.
  31. // In this case the given image is written into a file in `testdata/failed/<masterFilename>` (relative to the test).
  32. // This path is also reported, thus the file can be used as new master.
  33. //
  34. // Since 2.3
  35. func AssertObjectRendersToImage(t *testing.T, masterFilename string, o fyne.CanvasObject, msgAndArgs ...interface{}) bool {
  36. c := NewCanvasWithPainter(software.NewPainter())
  37. c.SetPadded(false)
  38. size := o.MinSize().Max(o.Size())
  39. c.SetContent(o)
  40. c.Resize(size) // ensure we are large enough for current size
  41. return AssertRendersToImage(t, masterFilename, c, msgAndArgs...)
  42. }
  43. // AssertObjectRendersToMarkup asserts that the given `CanvasObject` renders the same markup as the one stored in the master file.
  44. // The master filename is relative to the `testdata` directory which is relative to the test.
  45. // The test `t` fails if the rendered markup is not equal to the loaded master markup.
  46. // In this case the rendered markup is written into a file in `testdata/failed/<masterFilename>` (relative to the test).
  47. // This path is also reported, thus the file can be used as new master.
  48. //
  49. // Be aware, that the indentation has to use tab characters ('\t') instead of spaces.
  50. // Every element starts on a new line indented one more than its parent.
  51. // Closing elements stand on their own line, too, using the same indentation as the opening element.
  52. // The only exception to this are text elements which do not contain line breaks unless the text includes them.
  53. //
  54. // Since 2.3
  55. func AssertObjectRendersToMarkup(t *testing.T, masterFilename string, o fyne.CanvasObject, msgAndArgs ...interface{}) bool {
  56. c := NewCanvas()
  57. c.SetPadded(false)
  58. size := o.MinSize().Max(o.Size())
  59. c.SetContent(o)
  60. c.Resize(size) // ensure we are large enough for current size
  61. return AssertRendersToMarkup(t, masterFilename, c, msgAndArgs...)
  62. }
  63. // AssertImageMatches asserts that the given image is the same as the one stored in the master file.
  64. // The master filename is relative to the `testdata` directory which is relative to the test.
  65. // The test `t` fails if the given image is not equal to the loaded master image.
  66. // In this case the given image is written into a file in `testdata/failed/<masterFilename>` (relative to the test).
  67. // This path is also reported, thus the file can be used as new master.
  68. func AssertImageMatches(t *testing.T, masterFilename string, img image.Image, msgAndArgs ...interface{}) bool {
  69. return test.AssertImageMatches(t, masterFilename, img, msgAndArgs...)
  70. }
  71. // AssertRendersToImage asserts that the given canvas renders the same image as the one stored in the master file.
  72. // The master filename is relative to the `testdata` directory which is relative to the test.
  73. // The test `t` fails if the given image is not equal to the loaded master image.
  74. // In this case the given image is written into a file in `testdata/failed/<masterFilename>` (relative to the test).
  75. // This path is also reported, thus the file can be used as new master.
  76. //
  77. // Since 2.3
  78. func AssertRendersToImage(t *testing.T, masterFilename string, c fyne.Canvas, msgAndArgs ...interface{}) bool {
  79. return test.AssertImageMatches(t, masterFilename, c.Capture(), msgAndArgs...)
  80. }
  81. // AssertRendersToMarkup asserts that the given canvas renders the same markup as the one stored in the master file.
  82. // The master filename is relative to the `testdata` directory which is relative to the test.
  83. // The test `t` fails if the rendered markup is not equal to the loaded master markup.
  84. // In this case the rendered markup is written into a file in `testdata/failed/<masterFilename>` (relative to the test).
  85. // This path is also reported, thus the file can be used as new master.
  86. //
  87. // Be aware, that the indentation has to use tab characters ('\t') instead of spaces.
  88. // Every element starts on a new line indented one more than its parent.
  89. // Closing elements stand on their own line, too, using the same indentation as the opening element.
  90. // The only exception to this are text elements which do not contain line breaks unless the text includes them.
  91. //
  92. // Since: 2.0
  93. func AssertRendersToMarkup(t *testing.T, masterFilename string, c fyne.Canvas, msgAndArgs ...interface{}) bool {
  94. wd, err := os.Getwd()
  95. require.NoError(t, err)
  96. got := snapshot(c)
  97. masterPath := filepath.Join(wd, "testdata", masterFilename)
  98. failedPath := filepath.Join(wd, "testdata/failed", masterFilename)
  99. _, err = os.Stat(masterPath)
  100. if os.IsNotExist(err) {
  101. require.NoError(t, writeMarkup(failedPath, got))
  102. t.Errorf("Master not found at %s. Markup written to %s might be used as master.", masterPath, failedPath)
  103. return false
  104. }
  105. raw, err := os.ReadFile(masterPath)
  106. require.NoError(t, err)
  107. master := strings.ReplaceAll(string(raw), "\r", "")
  108. var msg string
  109. if len(msgAndArgs) > 0 {
  110. msg = fmt.Sprintf(msgAndArgs[0].(string)+"\n", msgAndArgs[1:]...)
  111. }
  112. if !assert.Equal(t, master, got, "%sMarkup did not match master. Actual markup written to file://%s.", msg, failedPath) {
  113. require.NoError(t, writeMarkup(failedPath, got))
  114. return false
  115. }
  116. return true
  117. }
  118. // Drag drags at an absolute position on the canvas.
  119. // deltaX/Y is the dragging distance: <0 for dragging up/left, >0 for dragging down/right.
  120. func Drag(c fyne.Canvas, pos fyne.Position, deltaX, deltaY float32) {
  121. matches := func(object fyne.CanvasObject) bool {
  122. if _, ok := object.(fyne.Draggable); ok {
  123. return true
  124. }
  125. return false
  126. }
  127. o, p, _ := driver.FindObjectAtPositionMatching(pos, matches, c.Overlays().Top(), c.Content())
  128. if o == nil {
  129. return
  130. }
  131. e := &fyne.DragEvent{
  132. PointEvent: fyne.PointEvent{Position: p},
  133. Dragged: fyne.Delta{DX: deltaX, DY: deltaY},
  134. }
  135. o.(fyne.Draggable).Dragged(e)
  136. o.(fyne.Draggable).DragEnd()
  137. }
  138. // FocusNext focuses the next focusable on the canvas.
  139. func FocusNext(c fyne.Canvas) {
  140. if tc, ok := c.(*testCanvas); ok {
  141. tc.focusManager().FocusNext()
  142. } else {
  143. fyne.LogError("FocusNext can only be called with a test canvas", nil)
  144. }
  145. }
  146. // FocusPrevious focuses the previous focusable on the canvas.
  147. func FocusPrevious(c fyne.Canvas) {
  148. if tc, ok := c.(*testCanvas); ok {
  149. tc.focusManager().FocusPrevious()
  150. } else {
  151. fyne.LogError("FocusPrevious can only be called with a test canvas", nil)
  152. }
  153. }
  154. // LaidOutObjects returns all fyne.CanvasObject starting at the given fyne.CanvasObject which is laid out previously.
  155. func LaidOutObjects(o fyne.CanvasObject) (objects []fyne.CanvasObject) {
  156. if o != nil {
  157. objects = layoutAndCollect(objects, o, o.MinSize().Max(o.Size()))
  158. }
  159. return objects
  160. }
  161. // MoveMouse simulates a mouse movement to the given position.
  162. func MoveMouse(c fyne.Canvas, pos fyne.Position) {
  163. if fyne.CurrentDevice().IsMobile() {
  164. return
  165. }
  166. tc, _ := c.(*testCanvas)
  167. var oldHovered, hovered desktop.Hoverable
  168. if tc != nil {
  169. oldHovered = tc.hovered
  170. }
  171. matches := func(object fyne.CanvasObject) bool {
  172. if _, ok := object.(desktop.Hoverable); ok {
  173. return true
  174. }
  175. return false
  176. }
  177. o, p, _ := driver.FindObjectAtPositionMatching(pos, matches, c.Overlays().Top(), c.Content())
  178. if o != nil {
  179. hovered = o.(desktop.Hoverable)
  180. me := &desktop.MouseEvent{
  181. PointEvent: fyne.PointEvent{
  182. AbsolutePosition: pos,
  183. Position: p,
  184. },
  185. }
  186. if hovered == oldHovered {
  187. hovered.MouseMoved(me)
  188. } else {
  189. if oldHovered != nil {
  190. oldHovered.MouseOut()
  191. }
  192. hovered.MouseIn(me)
  193. }
  194. } else if oldHovered != nil {
  195. oldHovered.MouseOut()
  196. }
  197. if tc != nil {
  198. tc.hovered = hovered
  199. }
  200. }
  201. // Scroll scrolls at an absolute position on the canvas.
  202. // deltaX/Y is the scrolling distance: <0 for scrolling up/left, >0 for scrolling down/right.
  203. func Scroll(c fyne.Canvas, pos fyne.Position, deltaX, deltaY float32) {
  204. matches := func(object fyne.CanvasObject) bool {
  205. if _, ok := object.(fyne.Scrollable); ok {
  206. return true
  207. }
  208. return false
  209. }
  210. o, _, _ := driver.FindObjectAtPositionMatching(pos, matches, c.Overlays().Top(), c.Content())
  211. if o == nil {
  212. return
  213. }
  214. e := &fyne.ScrollEvent{Scrolled: fyne.Delta{DX: deltaX, DY: deltaY}}
  215. o.(fyne.Scrollable).Scrolled(e)
  216. }
  217. // DoubleTap simulates a double left mouse click on the specified object.
  218. func DoubleTap(obj fyne.DoubleTappable) {
  219. ev, c := prepareTap(obj, fyne.NewPos(1, 1))
  220. handleFocusOnTap(c, obj)
  221. obj.DoubleTapped(ev)
  222. }
  223. // Tap simulates a left mouse click on the specified object.
  224. func Tap(obj fyne.Tappable) {
  225. TapAt(obj, fyne.NewPos(1, 1))
  226. }
  227. // TapAt simulates a left mouse click on the passed object at a specified place within it.
  228. func TapAt(obj fyne.Tappable, pos fyne.Position) {
  229. ev, c := prepareTap(obj, pos)
  230. tap(c, obj, ev)
  231. }
  232. // TapCanvas taps at an absolute position on the canvas.
  233. func TapCanvas(c fyne.Canvas, pos fyne.Position) {
  234. if o, p := findTappable(c, pos); o != nil {
  235. tap(c, o.(fyne.Tappable), &fyne.PointEvent{AbsolutePosition: pos, Position: p})
  236. }
  237. }
  238. // TapSecondary simulates a right mouse click on the specified object.
  239. func TapSecondary(obj fyne.SecondaryTappable) {
  240. TapSecondaryAt(obj, fyne.NewPos(1, 1))
  241. }
  242. // TapSecondaryAt simulates a right mouse click on the passed object at a specified place within it.
  243. func TapSecondaryAt(obj fyne.SecondaryTappable, pos fyne.Position) {
  244. ev, c := prepareTap(obj, pos)
  245. handleFocusOnTap(c, obj)
  246. obj.TappedSecondary(ev)
  247. }
  248. // Type performs a series of key events to simulate typing of a value into the specified object.
  249. // The focusable object will be focused before typing begins.
  250. // The chars parameter will be input one rune at a time to the focused object.
  251. func Type(obj fyne.Focusable, chars string) {
  252. obj.FocusGained()
  253. typeChars([]rune(chars), obj.TypedRune)
  254. }
  255. // TypeOnCanvas is like the Type function but it passes the key events to the canvas object
  256. // rather than a focusable widget.
  257. func TypeOnCanvas(c fyne.Canvas, chars string) {
  258. typeChars([]rune(chars), c.OnTypedRune())
  259. }
  260. // ApplyTheme sets the given theme and waits for it to be applied to the current app.
  261. func ApplyTheme(t *testing.T, theme fyne.Theme) {
  262. require.IsType(t, &testApp{}, fyne.CurrentApp())
  263. a := fyne.CurrentApp().(*testApp)
  264. a.Settings().SetTheme(theme)
  265. for a.lastAppliedTheme() != theme {
  266. time.Sleep(1 * time.Millisecond)
  267. }
  268. }
  269. // WidgetRenderer allows test scripts to gain access to the current renderer for a widget.
  270. // This can be used for verifying correctness of rendered components for a widget in unit tests.
  271. func WidgetRenderer(wid fyne.Widget) fyne.WidgetRenderer {
  272. return cache.Renderer(wid)
  273. }
  274. // WithTestTheme runs a function with the testTheme temporarily set.
  275. func WithTestTheme(t *testing.T, f func()) {
  276. settings := fyne.CurrentApp().Settings()
  277. current := settings.Theme()
  278. ApplyTheme(t, NewTheme())
  279. defer ApplyTheme(t, current)
  280. f()
  281. }
  282. func findTappable(c fyne.Canvas, pos fyne.Position) (o fyne.CanvasObject, p fyne.Position) {
  283. matches := func(object fyne.CanvasObject) bool {
  284. _, ok := object.(fyne.Tappable)
  285. return ok
  286. }
  287. o, p, _ = driver.FindObjectAtPositionMatching(pos, matches, c.Overlays().Top(), c.Content())
  288. return
  289. }
  290. func prepareTap(obj interface{}, pos fyne.Position) (*fyne.PointEvent, fyne.Canvas) {
  291. d := fyne.CurrentApp().Driver()
  292. ev := &fyne.PointEvent{Position: pos}
  293. var c fyne.Canvas
  294. if co, ok := obj.(fyne.CanvasObject); ok {
  295. c = d.CanvasForObject(co)
  296. ev.AbsolutePosition = d.AbsolutePositionForObject(co).Add(pos)
  297. }
  298. return ev, c
  299. }
  300. func tap(c fyne.Canvas, obj fyne.Tappable, ev *fyne.PointEvent) {
  301. handleFocusOnTap(c, obj)
  302. obj.Tapped(ev)
  303. }
  304. func handleFocusOnTap(c fyne.Canvas, obj interface{}) {
  305. if c == nil {
  306. return
  307. }
  308. unfocus := true
  309. if focus, ok := obj.(fyne.Focusable); ok {
  310. if dis, ok := obj.(fyne.Disableable); !ok || !dis.Disabled() {
  311. unfocus = false
  312. if focus != c.Focused() {
  313. unfocus = true
  314. }
  315. }
  316. }
  317. if unfocus {
  318. c.Unfocus()
  319. }
  320. }
  321. func typeChars(chars []rune, keyDown func(rune)) {
  322. for _, char := range chars {
  323. keyDown(char)
  324. }
  325. }
  326. func writeMarkup(path string, markup string) error {
  327. if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
  328. return err
  329. }
  330. return os.WriteFile(path, []byte(markup), 0644)
  331. }