Skip to content

Commit

Permalink
Various test cleanups
Browse files Browse the repository at this point in the history
A bit less code, faster in some situations but mostly just better readability.

PS: Tests on develop failed even before these changes so the updated images are included.
  • Loading branch information
Jacalz committed May 23, 2024
1 parent fb1b46b commit 009cf17
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 53 deletions.
33 changes: 12 additions & 21 deletions test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,8 @@ func AssertRendersToMarkup(t *testing.T, masterFilename string, c fyne.Canvas, m
// 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
_, ok := object.(fyne.Draggable)
return ok
}
o, p, _ := driver.FindObjectAtPositionMatching(pos, matches, c.Overlays().Top(), c.Content())
if o == nil {
Expand Down Expand Up @@ -189,10 +187,8 @@ func MoveMouse(c fyne.Canvas, pos fyne.Position) {
oldHovered = tc.hovered
}
matches := func(object fyne.CanvasObject) bool {
if _, ok := object.(desktop.Hoverable); ok {
return true
}
return false
_, ok := object.(desktop.Hoverable)
return ok
}
o, p, _ := driver.FindObjectAtPositionMatching(pos, matches, c.Overlays().Top(), c.Content())
if o != nil {
Expand Down Expand Up @@ -223,10 +219,8 @@ func MoveMouse(c fyne.Canvas, pos fyne.Position) {
// 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
_, ok := object.(fyne.Scrollable)
return ok
}
o, _, _ := driver.FindObjectAtPositionMatching(pos, matches, c.Overlays().Top(), c.Content())
if o == nil {
Expand Down Expand Up @@ -343,18 +337,15 @@ func handleFocusOnTap(c fyne.Canvas, obj any) {
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
}
dis, ok := obj.(fyne.Disableable)
if (!ok || !dis.Disabled()) && focus == c.Focused() {
return
}
}
if unfocus {
c.Unfocus()
}

c.Unfocus()
}

func typeChars(chars []rune, keyDown func(rune)) {
Expand Down
7 changes: 3 additions & 4 deletions test/testapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type testApp struct {
prefs fyne.Preferences
propertyLock sync.RWMutex
storage fyne.Storage
lifecycle fyne.Lifecycle
lifecycle app.Lifecycle
cloud fyne.CloudProvider

// user action variables
Expand Down Expand Up @@ -99,7 +99,7 @@ func (a *testApp) Storage() fyne.Storage {
}

func (a *testApp) Lifecycle() fyne.Lifecycle {
return a.lifecycle
return &a.lifecycle
}

func (a *testApp) Metadata() fyne.AppMetadata {
Expand Down Expand Up @@ -152,8 +152,7 @@ func NewApp() fyne.App {
settings := &testSettings{scale: 1.0, theme: Theme()}
prefs := internal.NewInMemoryPreferences()
store := &testStorage{}
test := &testApp{settings: settings, prefs: prefs, storage: store, driver: NewDriver().(*testDriver),
lifecycle: &app.Lifecycle{}}
test := &testApp{settings: settings, prefs: prefs, storage: store, driver: NewDriver().(*testDriver)}
root, _ := store.docRootURI()
store.Docs = &internal.Docs{RootDocURI: root}
painter.ClearFontCache()
Expand Down
19 changes: 10 additions & 9 deletions test/testcanvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ import (
"fyne.io/fyne/v2/theme"
)

var (
dummyCanvas fyne.Canvas
)
var dummyCanvas WindowlessCanvas

// WindowlessCanvas provides functionality for a canvas to operate without a window
type WindowlessCanvas interface {
Expand Down Expand Up @@ -91,7 +89,8 @@ func NewTransparentCanvasWithPainter(painter SoftwarePainter) WindowlessCanvas {

func (c *testCanvas) Capture() image.Image {
cache.Clean(true)
bounds := image.Rect(0, 0, scale.ToScreenCoordinate(c, c.Size().Width), scale.ToScreenCoordinate(c, c.Size().Height))
size := c.Size()
bounds := image.Rect(0, 0, scale.ToScreenCoordinate(c, size.Width), scale.ToScreenCoordinate(c, size.Height))
img := image.NewNRGBA(bounds)
if !c.transparent {
draw.Draw(img, bounds, image.NewUniform(theme.BackgroundColor()), image.Point{}, draw.Src)
Expand Down Expand Up @@ -194,8 +193,9 @@ func (c *testCanvas) Resize(size fyne.Size) {
}

if padded {
content.Resize(size.Subtract(fyne.NewSize(theme.Padding()*2, theme.Padding()*2)))
content.Move(fyne.NewPos(theme.Padding(), theme.Padding()))
padding := theme.Padding()
content.Resize(size.Subtract(fyne.NewSquareSize(padding * 2)))
content.Move(fyne.NewSquareOffsetPos(padding))
} else {
content.Resize(size)
content.Move(fyne.NewPos(0, 0))
Expand All @@ -221,7 +221,7 @@ func (c *testCanvas) SetContent(content fyne.CanvasObject) {

padding := fyne.NewSize(0, 0)
if c.padded {
padding = fyne.NewSize(theme.Padding()*2, theme.Padding()*2)
padding = fyne.NewSquareSize(theme.Padding() * 2)
}
c.Resize(content.MinSize().Add(padding))
}
Expand Down Expand Up @@ -276,11 +276,12 @@ func (c *testCanvas) focusManager() *app.FocusManager {
}

func (c *testCanvas) objectTrees() []fyne.CanvasObject {
trees := make([]fyne.CanvasObject, 0, len(c.Overlays().List())+1)
overlays := c.Overlays().List()
trees := make([]fyne.CanvasObject, 0, len(overlays)+1)
if c.content != nil {
trees = append(trees, c.content)
}
trees = append(trees, c.Overlays().List()...)
trees = append(trees, overlays...)
return trees
}

Expand Down
18 changes: 7 additions & 11 deletions test/testdriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type SoftwarePainter interface {
}

type testDriver struct {
device *device
device device
painter SoftwarePainter
windows []fyne.Window
windowsMutex sync.RWMutex
Expand All @@ -42,10 +42,7 @@ func NewDriver() fyne.Driver {
// NewDriverWithPainter creates a new dummy driver that will pass the given
// painter to all canvases created
func NewDriverWithPainter(painter SoftwarePainter) fyne.Driver {
return &testDriver{
painter: painter,
windowsMutex: sync.RWMutex{},
}
return &testDriver{painter: painter}
}

func (d *testDriver) AbsolutePositionForObject(co fyne.CanvasObject) fyne.Position {
Expand Down Expand Up @@ -80,7 +77,6 @@ func (d *testDriver) CreateWindow(string) fyne.Window {
}

window := &testWindow{canvas: canvas, driver: d}
window.clipboard = &testClipboard{}

d.windowsMutex.Lock()
d.windows = append(d.windows, window)
Expand All @@ -89,10 +85,7 @@ func (d *testDriver) CreateWindow(string) fyne.Window {
}

func (d *testDriver) Device() fyne.Device {
if d.device == nil {
d.device = &device{}
}
return d.device
return &d.device
}

// RenderedTextSize looks up how bit a string would be if drawn on screen
Expand Down Expand Up @@ -127,7 +120,10 @@ func (d *testDriver) removeWindow(w *testWindow) {
i++
}

d.windows = append(d.windows[:i], d.windows[i+1:]...)
copy(d.windows[i:], d.windows[i+1:])
d.windows[len(d.windows)-1] = nil // Allow the garbage collector to reclaim the memory.
d.windows = d.windows[:len(d.windows)-1]

d.windowsMutex.Unlock()
}

Expand Down
15 changes: 9 additions & 6 deletions test/testfile.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package test

import (
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -44,14 +45,16 @@ func (f *file) URI() fyne.URI {

func openFile(uri fyne.URI, create bool) (*file, error) {
if uri.Scheme() != "file" {
return nil, fmt.Errorf("unsupported URL protocol")
return nil, errors.New("unsupported URL protocol")
}

path := uri.String()[7:]
f, err := os.Open(path)
if err != nil && create {
f, err = os.Create(path)
path := uri.Path()
if create {
f, err := os.Create(path)
return &file{File: f, path: path}, err
}

f, err := os.Open(path)
return &file{File: f, path: path}, err
}

Expand All @@ -65,7 +68,7 @@ func (d *testDriver) FileWriterForURI(uri fyne.URI) (fyne.URIWriteCloser, error)

func (d *testDriver) ListerForURI(uri fyne.URI) (fyne.ListableURI, error) {
if uri.Scheme() != "file" {
return nil, fmt.Errorf("unsupported URL protocol")
return nil, errors.New("unsupported URL protocol")
}

path := uri.String()[len(uri.Scheme())+3 : len(uri.String())]
Expand Down
4 changes: 2 additions & 2 deletions test/testwindow.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type testWindow struct {
onCloseIntercepted func()

canvas *testCanvas
clipboard fyne.Clipboard
clipboard testClipboard
driver *testDriver
menu *fyne.MainMenu
}
Expand All @@ -34,7 +34,7 @@ func (w *testWindow) CenterOnScreen() {
}

func (w *testWindow) Clipboard() fyne.Clipboard {
return w.clipboard
return &w.clipboard
}

func (w *testWindow) Close() {
Expand Down
Binary file modified widget/testdata/menu/desktop/layout_shortcuts_other.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 009cf17

Please sign in to comment.