Skip to content

Commit

Permalink
Introduce test.TempWidgetRenderer to destroy the renderer once the te…
Browse files Browse the repository at this point in the history
…st completes

This allows us to fix many cases where the tests created and looked up widget renderers that would stay alive until a new window was created and the old was destroyed.
  • Loading branch information
Jacalz committed May 23, 2024
1 parent fb1b46b commit 089f20b
Show file tree
Hide file tree
Showing 37 changed files with 163 additions and 155 deletions.
16 changes: 8 additions & 8 deletions container/apptabs_extend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,31 @@ func TestAppTabs_Extended_Tapped(t *testing.T) {
NewTabItem("Test2", widget.NewLabel("Test2")),
)
tabs.Resize(fyne.NewSize(150, 150)) // Ensure AppTabs is big enough to show both tab buttons
r := test.WidgetRenderer(tabs).(*appTabsRenderer)
r := test.TempWidgetRenderer(t, tabs).(*appTabsRenderer)

tab1 := r.bar.Objects[0].(*fyne.Container).Objects[0].(*tabButton)
tab2 := r.bar.Objects[0].(*fyne.Container).Objects[1].(*tabButton)
require.Equal(t, 0, tabs.SelectedIndex())
require.Equal(t, theme.PrimaryColor(), test.WidgetRenderer(tab1).(*tabButtonRenderer).label.Color)
require.Equal(t, theme.PrimaryColor(), test.TempWidgetRenderer(t, tab1).(*tabButtonRenderer).label.Color)

tab2.Tapped(&fyne.PointEvent{})
assert.Equal(t, 1, tabs.SelectedIndex())
require.Equal(t, theme.ForegroundColor(), test.WidgetRenderer(tab1).(*tabButtonRenderer).label.Color)
require.Equal(t, theme.PrimaryColor(), test.WidgetRenderer(tab2).(*tabButtonRenderer).label.Color)
require.Equal(t, theme.ForegroundColor(), test.TempWidgetRenderer(t, tab1).(*tabButtonRenderer).label.Color)
require.Equal(t, theme.PrimaryColor(), test.TempWidgetRenderer(t, tab2).(*tabButtonRenderer).label.Color)
assert.False(t, tabs.Items[0].Content.Visible())
assert.True(t, tabs.Items[1].Content.Visible())

tab2.Tapped(&fyne.PointEvent{})
assert.Equal(t, 1, tabs.SelectedIndex())
require.Equal(t, theme.ForegroundColor(), test.WidgetRenderer(tab1).(*tabButtonRenderer).label.Color)
require.Equal(t, theme.PrimaryColor(), test.WidgetRenderer(tab2).(*tabButtonRenderer).label.Color)
require.Equal(t, theme.ForegroundColor(), test.TempWidgetRenderer(t, tab1).(*tabButtonRenderer).label.Color)
require.Equal(t, theme.PrimaryColor(), test.TempWidgetRenderer(t, tab2).(*tabButtonRenderer).label.Color)
assert.False(t, tabs.Items[0].Content.Visible())
assert.True(t, tabs.Items[1].Content.Visible())

tab1.Tapped(&fyne.PointEvent{})
assert.Equal(t, 0, tabs.SelectedIndex())
require.Equal(t, theme.PrimaryColor(), test.WidgetRenderer(tab1).(*tabButtonRenderer).label.Color)
require.Equal(t, theme.ForegroundColor(), test.WidgetRenderer(tab2).(*tabButtonRenderer).label.Color)
require.Equal(t, theme.PrimaryColor(), test.TempWidgetRenderer(t, tab1).(*tabButtonRenderer).label.Color)
require.Equal(t, theme.ForegroundColor(), test.TempWidgetRenderer(t, tab2).(*tabButtonRenderer).label.Color)
assert.True(t, tabs.Items[0].Content.Visible())
assert.False(t, tabs.Items[1].Content.Visible())
}
4 changes: 2 additions & 2 deletions container/apptabs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestAppTabs_Empty(t *testing.T) {
tabs = &AppTabs{}
assert.Equal(t, 0, len(tabs.Items))
assert.Nil(t, tabs.Selected())
assert.NotNil(t, test.WidgetRenderer(tabs)) // doesn't crash
assert.NotNil(t, test.TempWidgetRenderer(t, tabs)) // doesn't crash
}

func TestAppTabs_Hidden_AsChild(t *testing.T) {
Expand Down Expand Up @@ -183,7 +183,7 @@ func TestAppTabs_DisableIndex(t *testing.T) {

func TestAppTabs_ShowAfterAdd(t *testing.T) {
tabs := NewAppTabs()
renderer := test.WidgetRenderer(tabs).(*appTabsRenderer)
renderer := test.TempWidgetRenderer(t, tabs).(*appTabsRenderer)

assert.True(t, renderer.indicator.Hidden)

Expand Down
16 changes: 8 additions & 8 deletions container/doctabs_extend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,32 @@ func TestDocTabs_Extended_Tapped(t *testing.T) {
NewTabItem("Test2", widget.NewLabel("Test2")),
)
tabs.Resize(fyne.NewSize(150, 150)) // Ensure DocTabs is big enough to show both tab buttons
r := test.WidgetRenderer(tabs).(*docTabsRenderer)
r := test.TempWidgetRenderer(t, tabs).(*docTabsRenderer)

buttons := r.bar.Objects[0].(*Scroll).Content.(*fyne.Container).Objects
tab1 := buttons[0].(*tabButton)
tab2 := buttons[1].(*tabButton)
require.Equal(t, 0, tabs.SelectedIndex())
require.Equal(t, theme.PrimaryColor(), test.WidgetRenderer(tab1).(*tabButtonRenderer).label.Color)
require.Equal(t, theme.PrimaryColor(), test.TempWidgetRenderer(t, tab1).(*tabButtonRenderer).label.Color)

tab2.Tapped(&fyne.PointEvent{})
assert.Equal(t, 1, tabs.SelectedIndex())
require.Equal(t, theme.ForegroundColor(), test.WidgetRenderer(tab1).(*tabButtonRenderer).label.Color)
require.Equal(t, theme.PrimaryColor(), test.WidgetRenderer(tab2).(*tabButtonRenderer).label.Color)
require.Equal(t, theme.ForegroundColor(), test.TempWidgetRenderer(t, tab1).(*tabButtonRenderer).label.Color)
require.Equal(t, theme.PrimaryColor(), test.TempWidgetRenderer(t, tab2).(*tabButtonRenderer).label.Color)
assert.False(t, tabs.Items[0].Content.Visible())
assert.True(t, tabs.Items[1].Content.Visible())

tab2.Tapped(&fyne.PointEvent{})
assert.Equal(t, 1, tabs.SelectedIndex())
require.Equal(t, theme.ForegroundColor(), test.WidgetRenderer(tab1).(*tabButtonRenderer).label.Color)
require.Equal(t, theme.PrimaryColor(), test.WidgetRenderer(tab2).(*tabButtonRenderer).label.Color)
require.Equal(t, theme.ForegroundColor(), test.TempWidgetRenderer(t, tab1).(*tabButtonRenderer).label.Color)
require.Equal(t, theme.PrimaryColor(), test.TempWidgetRenderer(t, tab2).(*tabButtonRenderer).label.Color)
assert.False(t, tabs.Items[0].Content.Visible())
assert.True(t, tabs.Items[1].Content.Visible())

tab1.Tapped(&fyne.PointEvent{})
assert.Equal(t, 0, tabs.SelectedIndex())
require.Equal(t, theme.PrimaryColor(), test.WidgetRenderer(tab1).(*tabButtonRenderer).label.Color)
require.Equal(t, theme.ForegroundColor(), test.WidgetRenderer(tab2).(*tabButtonRenderer).label.Color)
require.Equal(t, theme.PrimaryColor(), test.TempWidgetRenderer(t, tab1).(*tabButtonRenderer).label.Color)
require.Equal(t, theme.ForegroundColor(), test.TempWidgetRenderer(t, tab2).(*tabButtonRenderer).label.Color)
assert.True(t, tabs.Items[0].Content.Visible())
assert.False(t, tabs.Items[1].Content.Visible())
}
2 changes: 1 addition & 1 deletion container/doctabs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestDocTabs_Empty(t *testing.T) {
tabs = &container.DocTabs{}
assert.Equal(t, 0, len(tabs.Items))
assert.Nil(t, tabs.Selected())
assert.NotNil(t, test.WidgetRenderer(tabs)) // doesn't crash
assert.NotNil(t, test.TempWidgetRenderer(t, tabs)) // doesn't crash
}

func TestDocTabs_Hidden_AsChild(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion container/multiplewindows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestMultipleWindows_Add(t *testing.T) {
func TestMultipleWindows_Drag(t *testing.T) {
w := NewInnerWindow("1", widget.NewLabel("Inside"))
m := NewMultipleWindows(w)
_ = test.WidgetRenderer(m) // initialise display
_ = test.TempWidgetRenderer(t, m) // initialise display
assert.Equal(t, 1, len(m.Windows))

assert.True(t, w.Position().IsZero())
Expand Down
8 changes: 4 additions & 4 deletions container/split_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func TestSplitContainer_divider_drag(t *testing.T) {
t.Run("Horizontal", func(t *testing.T) {
split := NewHSplit(objA, objB)
split.Resize(fyne.NewSize(108, 108))
divider := test.WidgetRenderer(split).(*splitContainerRenderer).divider
divider := test.TempWidgetRenderer(t, split).(*splitContainerRenderer).divider
assert.Equal(t, 0.5, split.Offset)

divider.Dragged(&fyne.DragEvent{
Expand Down Expand Up @@ -324,7 +324,7 @@ func TestSplitContainer_divider_drag(t *testing.T) {
t.Run("Vertical", func(t *testing.T) {
split := NewVSplit(objA, objB)
split.Resize(fyne.NewSize(108, 108))
divider := test.WidgetRenderer(split).(*splitContainerRenderer).divider
divider := test.TempWidgetRenderer(t, split).(*splitContainerRenderer).divider
assert.Equal(t, 0.5, split.Offset)

divider.Dragged(&fyne.DragEvent{
Expand Down Expand Up @@ -366,7 +366,7 @@ func TestSplitContainer_divider_drag_StartOffsetLessThanMinSize(t *testing.T) {
t.Run("Horizontal", func(t *testing.T) {
split := NewHSplit(objA, objB)
split.Resize(fyne.NewSize(108, 108))
divider := test.WidgetRenderer(split).(*splitContainerRenderer).divider
divider := test.TempWidgetRenderer(t, split).(*splitContainerRenderer).divider
t.Run("Leading", func(t *testing.T) {
split.SetOffset(0.1)

Expand All @@ -391,7 +391,7 @@ func TestSplitContainer_divider_drag_StartOffsetLessThanMinSize(t *testing.T) {
t.Run("Vertical", func(t *testing.T) {
split := NewVSplit(objA, objB)
split.Resize(fyne.NewSize(108, 108))
divider := test.WidgetRenderer(split).(*splitContainerRenderer).divider
divider := test.TempWidgetRenderer(t, split).(*splitContainerRenderer).divider
t.Run("Leading", func(t *testing.T) {
split.SetOffset(0.1)

Expand Down
18 changes: 9 additions & 9 deletions dialog/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,17 +214,17 @@ func TestShowFileOpen(t *testing.T) {
}

files := ui.Objects[0].(*container.Split).Trailing.(*fyne.Container).Objects[1].(*container.Scroll).Content.(*fyne.Container).Objects[0].(*widget.GridWrap)
objects := test.WidgetRenderer(files).Objects()[0].(*container.Scroll).Content.(*fyne.Container).Objects
objects := test.TempWidgetRenderer(t, files).Objects()[0].(*container.Scroll).Content.(*fyne.Container).Objects
assert.Greater(t, len(objects), 0)

fileName := test.WidgetRenderer(objects[0].(fyne.Widget)).Objects()[1].(*fileDialogItem).name
fileName := test.TempWidgetRenderer(t, objects[0].(fyne.Widget)).Objects()[1].(*fileDialogItem).name
assert.Equal(t, "(Parent)", fileName)
assert.True(t, open.Disabled())

var target *fileDialogItem
id := 0
for i, icon := range objects {
item := test.WidgetRenderer(icon.(fyne.Widget)).Objects()[1].(*fileDialogItem)
item := test.TempWidgetRenderer(t, icon.(fyne.Widget)).Objects()[1].(*fileDialogItem)
if item.dir == false {
target = item
id = i
Expand Down Expand Up @@ -283,12 +283,12 @@ func TestHiddenFiles(t *testing.T) {
assert.Equal(t, theme.SettingsIcon().Name(), optionsButton.Icon.Name())

files := ui.Objects[0].(*container.Split).Trailing.(*fyne.Container).Objects[1].(*container.Scroll).Content.(*fyne.Container).Objects[0].(*widget.GridWrap)
objects := test.WidgetRenderer(files).Objects()[0].(*container.Scroll).Content.(*fyne.Container).Objects
objects := test.TempWidgetRenderer(t, files).Objects()[0].(*container.Scroll).Content.(*fyne.Container).Objects
assert.Greater(t, len(objects), 0)

var target *fileDialogItem
for _, icon := range objects {
item := test.WidgetRenderer(icon.(fyne.Widget)).Objects()[1].(*fileDialogItem)
item := test.TempWidgetRenderer(t, icon.(fyne.Widget)).Objects()[1].(*fileDialogItem)
if item.name == ".hidden" {
target = item
}
Expand All @@ -299,7 +299,7 @@ func TestHiddenFiles(t *testing.T) {
d.dialog.refreshDir(d.dialog.dir)

for _, icon := range objects {
item := test.WidgetRenderer(icon.(fyne.Widget)).Objects()[1].(*fileDialogItem)
item := test.TempWidgetRenderer(t, icon.(fyne.Widget)).Objects()[1].(*fileDialogItem)
if item.name == ".hidden" {
target = item
}
Expand Down Expand Up @@ -330,17 +330,17 @@ func TestShowFileSave(t *testing.T) {
save := buttons.Objects[1].(*widget.Button)

files := ui.Objects[0].(*container.Split).Trailing.(*fyne.Container).Objects[1].(*container.Scroll).Content.(*fyne.Container).Objects[0].(*widget.GridWrap)
objects := test.WidgetRenderer(files).Objects()[0].(*container.Scroll).Content.(*fyne.Container).Objects
objects := test.TempWidgetRenderer(t, files).Objects()[0].(*container.Scroll).Content.(*fyne.Container).Objects
assert.Greater(t, len(objects), 0)

item := test.WidgetRenderer(objects[0].(fyne.Widget)).Objects()[1].(*fileDialogItem)
item := test.TempWidgetRenderer(t, objects[0].(fyne.Widget)).Objects()[1].(*fileDialogItem)
assert.Equal(t, "(Parent)", item.name)
assert.True(t, save.Disabled())

var target *fileDialogItem
id := -1
for i, icon := range objects {
item := test.WidgetRenderer(icon.(fyne.Widget)).Objects()[1].(*fileDialogItem)
item := test.TempWidgetRenderer(t, icon.(fyne.Widget)).Objects()[1].(*fileDialogItem)
if item.dir == false {
target = item
id = i
Expand Down
8 changes: 4 additions & 4 deletions dialog/fileitem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ func TestFileItem_Wrap(t *testing.T) {
_ = f.makeUI()
item := f.newFileItem(storage.NewFileURI("/path/to/filename.txt"), false, false)
item.Resize(item.MinSize())
label := test.WidgetRenderer(item).(*fileItemRenderer).text
label := test.TempWidgetRenderer(t, item).(*fileItemRenderer).text
assert.Equal(t, "filename", label.Text)
texts := test.WidgetRenderer(label).Objects()
texts := test.TempWidgetRenderer(t, label).Objects()
assert.Equal(t, 1, len(texts))

item.setLocation(storage.NewFileURI("/path/to/averylongfilename.svg"), false, false)
rich := test.WidgetRenderer(label).Objects()[0].(*widget.RichText)
texts = test.WidgetRenderer(rich).Objects()
rich := test.TempWidgetRenderer(t, label).Objects()[0].(*widget.RichText)
texts = test.TempWidgetRenderer(t, rich).Objects()
assert.Equal(t, 2, len(texts))
assert.Equal(t, "averylon", texts[0].(*canvas.Text).Text)
}
4 changes: 2 additions & 2 deletions dialog/folder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func TestShowFolderOpen(t *testing.T) {
files := ui.Objects[0].(*container.Split).Trailing.(*fyne.Container).Objects[1].(*container.Scroll).Content.(*fyne.Container).Objects[0].(*widget.GridWrap)
assert.Greater(t, len(d.dialog.data), 0)

item := test.WidgetRenderer(files).Objects()[0].(*container.Scroll).Content.(*fyne.Container).Objects[0]
fileName := test.WidgetRenderer(item.(fyne.Widget)).Objects()[1].(*fileDialogItem).name
item := test.TempWidgetRenderer(t, files).Objects()[0].(*container.Scroll).Content.(*fyne.Container).Objects[0]
fileName := test.TempWidgetRenderer(t, item.(fyne.Widget)).Objects()[1].(*fileDialogItem).name
assert.Equal(t, "(Parent)", fileName)
assert.False(t, open.Disabled())

Expand Down
8 changes: 8 additions & 0 deletions test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,14 @@ func ApplyTheme(t *testing.T, theme fyne.Theme) {
}
}

// TempWidgetRenderer 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.
// The widget renderer is automatically destroyed when the test ends.
func TempWidgetRenderer(t *testing.T, wid fyne.Widget) fyne.WidgetRenderer {
t.Cleanup(func() { cache.DestroyRenderer(wid) })
return cache.Renderer(wid)
}

// 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 {
Expand Down
20 changes: 10 additions & 10 deletions widget/accordion_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func TestAccordion_Toggle(t *testing.T) {
ai := NewAccordionItem("foo", NewLabel("foobar"))
ac := NewAccordion(ai)
ar := test.WidgetRenderer(ac).(*accordionRenderer)
ar := test.TempWidgetRenderer(t, ac).(*accordionRenderer)
aih := ar.headers[0]
assert.False(t, ai.Open)

Expand All @@ -35,7 +35,7 @@ func TestAccordionRenderer_Layout(t *testing.T) {
ac.Append(ai1)
ac.Append(ai2)

ar := test.WidgetRenderer(ac).(*accordionRenderer)
ar := test.TempWidgetRenderer(t, ac).(*accordionRenderer)
aih0 := ar.headers[0]
aih1 := ar.headers[1]
aih2 := ar.headers[2]
Expand Down Expand Up @@ -143,7 +143,7 @@ func TestAccordionRenderer_Layout(t *testing.T) {
func TestAccordionRenderer_MinSize(t *testing.T) {
t.Run("Empty", func(t *testing.T) {
ac := NewAccordion()
ar := test.WidgetRenderer(ac).(*accordionRenderer)
ar := test.TempWidgetRenderer(t, ac).(*accordionRenderer)
min := ar.MinSize()
assert.Equal(t, float32(0), min.Width)
assert.Equal(t, float32(0), min.Height)
Expand All @@ -154,7 +154,7 @@ func TestAccordionRenderer_MinSize(t *testing.T) {
ac := NewAccordion()
ac.Append(ai)
ac.Open(0)
ar := test.WidgetRenderer(ac).(*accordionRenderer)
ar := test.TempWidgetRenderer(t, ac).(*accordionRenderer)
min := ar.MinSize()
aih := ar.headers[0].MinSize()
aid := ai.Detail.MinSize()
Expand All @@ -165,7 +165,7 @@ func TestAccordionRenderer_MinSize(t *testing.T) {
ac := NewAccordion()
ac.Append(ai)
ac.Close(0)
ar := test.WidgetRenderer(ac).(*accordionRenderer)
ar := test.TempWidgetRenderer(t, ac).(*accordionRenderer)
min := ar.MinSize()
aih := ar.headers[0].MinSize()
assert.Equal(t, aih.Width, min.Width)
Expand All @@ -184,7 +184,7 @@ func TestAccordionRenderer_MinSize(t *testing.T) {
ac.Open(0)
ac.Close(1)
ac.Close(2)
ar := test.WidgetRenderer(ac).(*accordionRenderer)
ar := test.TempWidgetRenderer(t, ac).(*accordionRenderer)
min := ar.MinSize()
aih0 := ar.headers[0].MinSize()
aih1 := ar.headers[1].MinSize()
Expand All @@ -209,7 +209,7 @@ func TestAccordionRenderer_MinSize(t *testing.T) {
ac.Append(ai1)
ac.Append(ai2)
ac.OpenAll()
ar := test.WidgetRenderer(ac).(*accordionRenderer)
ar := test.TempWidgetRenderer(t, ac).(*accordionRenderer)
min := ar.MinSize()
aih0 := ar.headers[0].MinSize()
aih1 := ar.headers[1].MinSize()
Expand Down Expand Up @@ -242,7 +242,7 @@ func TestAccordionRenderer_MinSize(t *testing.T) {
ac.Open(0)
ac.Open(1)
ac.Close(2)
ar := test.WidgetRenderer(ac).(*accordionRenderer)
ar := test.TempWidgetRenderer(t, ac).(*accordionRenderer)
min := ar.MinSize()
aih0 := ar.headers[0].MinSize()
aih1 := ar.headers[1].MinSize()
Expand All @@ -268,7 +268,7 @@ func TestAccordionRenderer_MinSize(t *testing.T) {
ac.Append(ai1)
ac.Append(ai2)
ac.CloseAll()
ar := test.WidgetRenderer(ac).(*accordionRenderer)
ar := test.TempWidgetRenderer(t, ac).(*accordionRenderer)
min := ar.MinSize()
aih0 := ar.headers[0].MinSize()
aih1 := ar.headers[1].MinSize()
Expand All @@ -287,7 +287,7 @@ func TestAccordionRenderer_MinSize(t *testing.T) {

func TestAccordionRenderer_AddRemove(t *testing.T) {
ac := NewAccordion()
ar := test.WidgetRenderer(ac).(*accordionRenderer)
ar := test.TempWidgetRenderer(t, ac).(*accordionRenderer)
ac.Append(NewAccordionItem("foo0", NewLabel("foobar0")))
ac.Append(NewAccordionItem("foo1", NewLabel("foobar1")))
ac.Append(NewAccordionItem("foo2", NewLabel("foobar2")))
Expand Down
2 changes: 1 addition & 1 deletion widget/activity_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestActivity_Animation(t *testing.T) {
defer w.Close()
w.Resize(a.MinSize())

render := test.WidgetRenderer(a).(*activityRenderer)
render := test.TempWidgetRenderer(t, a).(*activityRenderer)
render.anim.Tick(0)
test.AssertImageMatches(t, "activity/animate_0.0.png", w.Canvas().Capture())

Expand Down
Loading

0 comments on commit 089f20b

Please sign in to comment.