Skip to content

Commit

Permalink
Refactor the test canvas/window usage to be more real
Browse files Browse the repository at this point in the history
You can choose a re-usable window if you want,
or create an individual one for a single test - which you should close
  • Loading branch information
andydotxyz committed Jul 23, 2018
1 parent 7354e46 commit 8b3121e
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 25 deletions.
3 changes: 1 addition & 2 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ func (app *fyneApp) applyTheme(Settings) {
switch themed := content.(type) {
case ThemedObject:
themed.ApplyTheme()
window.Canvas().Refresh(content)
}

window.Canvas().Refresh(content)
}
}

Expand Down
12 changes: 6 additions & 6 deletions test/testcanvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ import "github.com/fyne-io/fyne"

var dummyCanvas fyne.Canvas

// Canvas returns an un-rendered canvas that is used for behavioural testing
func Canvas() fyne.Canvas {
return dummyCanvas
}

type testCanvas struct {
content fyne.CanvasObject
focused fyne.FocusableObject
Expand Down Expand Up @@ -51,10 +46,15 @@ func (c *testCanvas) SetScale(float32) {
func (c *testCanvas) SetOnKeyDown(func(*fyne.KeyEvent)) {
}

// NewTestCanvas returns a single use in-memory canvas used for testing
func NewTestCanvas() fyne.Canvas {
return &testCanvas{}
}

// GetTestCanvas returns a reusable in-memory canvas used for testing
func GetTestCanvas() fyne.Canvas {
if dummyCanvas == nil {
dummyCanvas = &testCanvas{}
dummyCanvas = NewTestCanvas()
}

return dummyCanvas
Expand Down
4 changes: 2 additions & 2 deletions test/testdriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type testDriver struct {
}

func (d *testDriver) CreateWindow(string) fyne.Window {
return NewTestWindow()
return NewTestWindow(nil)
}

func (d *testDriver) AllWindows() []fyne.Window {
Expand All @@ -25,7 +25,7 @@ func (d *testDriver) Quit() {
func NewTestDriver() fyne.Driver {
driver := new(testDriver)
// make a single dummy window for rendering tests
NewTestWindow()
NewTestWindow(nil)

return driver
}
24 changes: 19 additions & 5 deletions test/testwindow.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import "github.com/fyne-io/fyne"
type testWindow struct {
title string
fullscreen bool

canvas fyne.Canvas
}

var windows = make([]fyne.Window, 0)
Expand All @@ -29,7 +31,17 @@ func (w *testWindow) Show() {}

func (w *testWindow) Hide() {}

func (w *testWindow) Close() {}
func (w *testWindow) Close() {
i := 0
for _, window := range windows {
if window == w {
break
}
i++
}

windows = append(windows[:i], windows[i+1:]...)
}

func (w *testWindow) Content() fyne.CanvasObject {
return w.Canvas().Content()
Expand All @@ -40,13 +52,15 @@ func (w *testWindow) SetContent(obj fyne.CanvasObject) {
}

func (w *testWindow) Canvas() fyne.Canvas {
return GetTestCanvas()
return w.canvas
}

// NewTestWindow creates and registers a new window for test purposes
func NewTestWindow() fyne.Window {
window := &testWindow{}
windows = append(windows, window)
func NewTestWindow(content fyne.CanvasObject) fyne.Window {
canvas := NewTestCanvas()
canvas.SetContent(content)
window := &testWindow{canvas: canvas}

windows = append(windows, window)
return window
}
2 changes: 1 addition & 1 deletion widget/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestEntryFocus(t *testing.T) {

func TestEntryWindowFocus(t *testing.T) {
entry := NewEntry()
canvas := test.Canvas()
canvas := test.GetTestCanvas()

canvas.Focus(entry)
assert.True(t, entry.Focused())
Expand Down
36 changes: 27 additions & 9 deletions widget/widget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,48 @@ import "github.com/stretchr/testify/assert"
import "github.com/fyne-io/fyne"
import "github.com/fyne-io/fyne/test"

var applied = make(chan bool)

type myWidget struct {
baseWidget

applied chan bool
}

func (m *myWidget) ApplyTheme() {
applied <- true
m.applied <- true
close(m.applied)
}

func TestApplyThemeCalled(t *testing.T) {
widget := &myWidget{}

app := test.NewApp()
window := app.NewWindow("Testing")
window.SetContent(widget)
widget := &myWidget{applied: make(chan bool)}

window := test.NewTestWindow(widget)
fyne.GetSettings().SetTheme("light")

func() {
select {
case <-applied:
case <-widget.applied:
case <-time.After(1 * time.Second):
assert.Fail(t, "Timed out waiting for theme apply")
}
}()

window.Close()
}

func TestAppplyThemeCalledChild(t *testing.T) {
child := &myWidget{applied: make(chan bool)}
parent := NewList(child)

window := test.NewTestWindow(parent)
fyne.GetSettings().SetTheme("light")

func() {
select {
case <-child.applied:
case <-time.After(1 * time.Second):
assert.Fail(t, "Timed out waiting for child theme apply")
}
}()

window.Close()
}

0 comments on commit 8b3121e

Please sign in to comment.