Skip to content

Commit

Permalink
Merge pull request #4871 from Jacalz/test-package-cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz authored May 23, 2024
2 parents f84c1dc + 93c720f commit cb5e0a4
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 55 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
23 changes: 12 additions & 11 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 @@ -219,11 +219,11 @@ func (c *testCanvas) SetContent(content fyne.CanvasObject) {
return
}

padding := fyne.NewSize(0, 0)
minSize := content.MinSize()
if c.padded {
padding = fyne.NewSize(theme.Padding()*2, theme.Padding()*2)
minSize = minSize.Add(fyne.NewSquareSize(theme.Padding() * 2))
}
c.Resize(content.MinSize().Add(padding))
c.Resize(minSize)
}

func (c *testCanvas) SetOnTypedKey(handler func(*fyne.KeyEvent)) {
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 @@ -46,10 +46,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 @@ -84,7 +81,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 @@ -93,10 +89,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 @@ -131,7 +124,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

0 comments on commit cb5e0a4

Please sign in to comment.