Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce test.NewTempWindow() to avoid memory leak in test windows #4849

Merged
merged 8 commits into from
Jun 18, 2024
2 changes: 1 addition & 1 deletion container/innerwindow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func TestInnerWindow_Close(t *testing.T) {
w := NewInnerWindow("Thing", widget.NewLabel("Content"))

outer := test.NewWindow(w)
outer := test.NewTempWindow(t, w)
outer.SetPadded(false)
outer.Resize(w.MinSize())
assert.True(t, w.Visible())
Expand Down
2 changes: 1 addition & 1 deletion container/tabs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestTab_ThemeChange(t *testing.T) {
tabs := NewAppTabs(
NewTabItem("a", widget.NewLabel("a")),
NewTabItem("b", widget.NewLabel("b")))
w := test.NewWindow(tabs)
w := test.NewTempWindow(t, tabs)
w.Resize(fyne.NewSize(180, 120))

initial := w.Canvas().Capture()
Expand Down
4 changes: 1 addition & 3 deletions dialog/color_button_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ func Test_colorButton_Layout(t *testing.T) {
color.MouseIn(nil)
}

window := test.NewWindow(container.NewCenter(color))
window := test.NewTempWindow(t, container.NewCenter(color))
window.Resize(color.MinSize().Max(fyne.NewSize(50, 50)))

test.AssertRendersToImage(t, "color/button_layout_"+name+".png", window.Canvas())

window.Close()
})
}
}
4 changes: 1 addition & 3 deletions dialog/color_channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,9 @@ func Test_colorChannel_Layout(t *testing.T) {
color := newColorChannel(tt.name, min, max, tt.value, nil)
color.Resize(size)

window := test.NewWindow(color)
window := test.NewTempWindow(t, color)

test.AssertRendersToImage(t, "color/channel_layout_"+name+".png", window.Canvas())

window.Close()
})
}
}
16 changes: 4 additions & 12 deletions dialog/color_picker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,21 @@ func Test_colorGreyscalePicker_Layout(t *testing.T) {

color := newColorGreyscalePicker(nil)

window := test.NewWindow(container.NewCenter(color))
window := test.NewTempWindow(t, container.NewCenter(color))
window.Resize(color.MinSize().Max(fyne.NewSize(360, 60)))

test.AssertRendersToImage(t, "color/picker_layout_greyscale.png", window.Canvas())

window.Close()
}

func Test_colorBasicPicker_Layout(t *testing.T) {
test.NewTempApp(t)

color := newColorBasicPicker(nil)

window := test.NewWindow(container.NewCenter(color))
window := test.NewTempWindow(t, container.NewCenter(color))
window.Resize(color.MinSize().Max(fyne.NewSize(360, 60)))

test.AssertRendersToImage(t, "color/picker_layout_basic.png", window.Canvas())

window.Close()
}

func Test_colorRecentPicker_Layout(t *testing.T) {
Expand All @@ -43,12 +39,10 @@ func Test_colorRecentPicker_Layout(t *testing.T) {

color := newColorRecentPicker(nil)

window := test.NewWindow(container.NewCenter(color))
window := test.NewTempWindow(t, container.NewCenter(color))
window.Resize(color.MinSize().Max(fyne.NewSize(360, 60)))

test.AssertRendersToImage(t, "color/picker_layout_recent.png", window.Canvas())

window.Close()
}

func Test_colorAdvancedPicker_Layout(t *testing.T) {
Expand All @@ -58,10 +52,8 @@ func Test_colorAdvancedPicker_Layout(t *testing.T) {

color.Refresh()

window := test.NewWindow(container.NewCenter(color))
window := test.NewTempWindow(t, container.NewCenter(color))
window.Resize(color.MinSize().Max(fyne.NewSize(200, 200)))

test.AssertRendersToImage(t, "color/picker_layout_advanced.png", window.Canvas())

window.Close()
}
4 changes: 1 addition & 3 deletions dialog/color_preview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ func Test_colorPreview_Color(t *testing.T) {

preview := newColorPreview(color.RGBA{53, 113, 233, 255})
preview.SetColor(color.RGBA{90, 206, 80, 180})
window := test.NewWindow(preview)
window := test.NewTempWindow(t, preview)
padding := theme.Padding() * 2
window.Resize(fyne.NewSize(128+padding, 64+padding))

test.AssertRendersToImage(t, "color/preview_color.png", window.Canvas())

window.Close()
}
19 changes: 5 additions & 14 deletions dialog/color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func TestColorDialog_Theme(t *testing.T) {
test.NewTempApp(t)

w := test.NewWindow(canvas.NewRectangle(color.Transparent))
w := test.NewTempWindow(t, canvas.NewRectangle(color.Transparent))
w.Resize(fyne.NewSize(1000, 800))

d := NewColorPicker("Color Picker", "Pick a Color", nil, w)
Expand All @@ -35,16 +35,14 @@ func TestColorDialog_Theme(t *testing.T) {

test.ApplyTheme(t, test.NewTheme())
test.AssertRendersToImage(t, "color/dialog_expanded_theme_ugly.png", w.Canvas())

w.Close()
}

func TestColorDialog_Recents(t *testing.T) {
a := test.NewTempApp(t)
// Inject recent preferences
a.Preferences().SetString("color_recents", "#2196f3,#4caf50,#f44336")

w := test.NewWindow(canvas.NewRectangle(color.Transparent))
w := test.NewTempWindow(t, canvas.NewRectangle(color.Transparent))
w.Resize(fyne.NewSize(800, 600))

d := NewColorPicker("Color Picker", "Pick a Color", nil, w)
Expand All @@ -56,13 +54,11 @@ func TestColorDialog_Recents(t *testing.T) {

test.ApplyTheme(t, test.NewTheme())
test.AssertRendersToImage(t, "color/dialog_recents_theme_ugly.png", w.Canvas())

w.Close()
}

func TestColorDialog_SetColor(t *testing.T) {

w := test.NewWindow(canvas.NewRectangle(color.Transparent))
w := test.NewTempWindow(t, canvas.NewRectangle(color.Transparent))
w.Resize(fyne.NewSize(800, 600))

col := color.RGBA{70, 210, 200, 255}
Expand Down Expand Up @@ -95,13 +91,12 @@ func TestColorDialog_SetColor(t *testing.T) {
assert.Equal(t, 244, d.picker.Alpha)

d.Show()
w.Close()
}

func TestColorDialogSimple_Theme(t *testing.T) {
test.NewTempApp(t)

w := test.NewWindow(canvas.NewRectangle(color.Transparent))
w := test.NewTempWindow(t, canvas.NewRectangle(color.Transparent))
w.Resize(fyne.NewSize(600, 400))

d := NewColorPicker("Color Picker", "Pick a Color", nil, w)
Expand All @@ -111,8 +106,6 @@ func TestColorDialogSimple_Theme(t *testing.T) {

test.ApplyTheme(t, test.NewTheme())
test.AssertRendersToImage(t, "color/dialog_simple_theme_ugly.png", w.Canvas())

w.Close()
}

func TestColorDialogSimple_Recents(t *testing.T) {
Expand All @@ -121,7 +114,7 @@ func TestColorDialogSimple_Recents(t *testing.T) {
// Inject recent preferences
a.Preferences().SetString("color_recents", "#2196f3,#4caf50,#f44336")

w := test.NewWindow(canvas.NewRectangle(color.Transparent))
w := test.NewTempWindow(t, canvas.NewRectangle(color.Transparent))
w.Resize(fyne.NewSize(600, 400))

d := NewColorPicker("Color Picker", "Pick a Color", nil, w)
Expand All @@ -131,8 +124,6 @@ func TestColorDialogSimple_Recents(t *testing.T) {

test.ApplyTheme(t, test.NewTheme())
test.AssertRendersToImage(t, "color/dialog_simple_recents_theme_ugly.png", w.Canvas())

w.Close()
}

func Test_recent_color(t *testing.T) {
Expand Down
4 changes: 1 addition & 3 deletions dialog/color_wheel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ func Test_colorWheel_Layout(t *testing.T) {

wheel := newColorWheel(nil)
wheel.SetHSLA(180, 100, 50, 255)
window := test.NewWindow(wheel)
window := test.NewTempWindow(t, wheel)
window.Resize(wheel.MinSize().Max(fyne.NewSize(100, 100)))

test.AssertRendersToImage(t, "color/wheel_layout.png", window.Canvas())
test.AssertRendersToMarkup(t, "color/wheel_layout.xml", window.Canvas())

window.Close()
}
10 changes: 5 additions & 5 deletions dialog/confirm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestDialog_ConfirmDoubleCallback(t *testing.T) {
ch := make(chan int)
cnf := NewConfirm("Test", "Test", func(_ bool) {
ch <- 42
}, test.NewWindow(nil))
}, test.NewTempWindow(t, nil))
cnf.SetDismissText("No")
cnf.SetConfirmText("Yes")
cnf.SetOnClosed(func() {
Expand All @@ -34,7 +34,7 @@ func TestDialog_ConfirmDoubleCallback(t *testing.T) {

func TestDialog_ConfirmCallbackOnlyOnClosed(t *testing.T) {
ch := make(chan int)
cnf := NewConfirm("Test", "Test", nil, test.NewWindow(nil))
cnf := NewConfirm("Test", "Test", nil, test.NewTempWindow(t, nil))
cnf.SetDismissText("No")
cnf.SetConfirmText("Yes")
cnf.SetOnClosed(func() {
Expand All @@ -52,7 +52,7 @@ func TestDialog_ConfirmCallbackOnlyOnConfirm(t *testing.T) {
ch := make(chan int)
cnf := NewConfirm("Test", "Test", func(_ bool) {
ch <- 42
}, test.NewWindow(nil))
}, test.NewTempWindow(t, nil))
cnf.SetDismissText("No")
cnf.SetConfirmText("Yes")
cnf.Show()
Expand All @@ -65,8 +65,8 @@ func TestDialog_ConfirmCallbackOnlyOnConfirm(t *testing.T) {

func TestConfirmDialog_Resize(t *testing.T) {
window := test.NewWindow(nil)
window.Resize(fyne.NewSize(600, 400))
defer window.Close()
window.Resize(fyne.NewSize(600, 400))
d := NewConfirm("Test", "Test", nil, window)

theDialog := d.dialog
Expand Down Expand Up @@ -108,7 +108,7 @@ func TestConfirmDialog_Resize(t *testing.T) {

func TestConfirm_Importance(t *testing.T) {
test.NewTempApp(t)
w := test.NewWindow(canvas.NewRectangle(color.Transparent))
w := test.NewTempWindow(t, canvas.NewRectangle(color.Transparent))
size := fyne.NewSize(200, 300)
w.Resize(size)

Expand Down
14 changes: 7 additions & 7 deletions dialog/custom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
func TestShowCustom_ApplyTheme(t *testing.T) {
test.NewTempApp(t)

w := test.NewWindow(canvas.NewRectangle(color.Transparent))
w := test.NewTempWindow(t, canvas.NewRectangle(color.Transparent))

label := widget.NewLabel("Content")
label.Alignment = fyne.TextAlignCenter
Expand All @@ -35,7 +35,7 @@ func TestShowCustom_ApplyTheme(t *testing.T) {
}

func TestShowCustom_Resize(t *testing.T) {
w := test.NewWindow(canvas.NewRectangle(color.Transparent))
w := test.NewTempWindow(t, canvas.NewRectangle(color.Transparent))
w.Resize(fyne.NewSize(300, 300))

label := widget.NewLabel("Content")
Expand All @@ -50,7 +50,7 @@ func TestShowCustom_Resize(t *testing.T) {

func TestCustom_ApplyThemeOnShow(t *testing.T) {
test.NewTempApp(t)
w := test.NewWindow(canvas.NewRectangle(color.Transparent))
w := test.NewTempWindow(t, canvas.NewRectangle(color.Transparent))
w.Resize(fyne.NewSize(200, 300))

label := widget.NewLabel("Content")
Expand All @@ -75,7 +75,7 @@ func TestCustom_ApplyThemeOnShow(t *testing.T) {

func TestCustom_ResizeOnShow(t *testing.T) {
test.NewTempApp(t)
w := test.NewWindow(canvas.NewRectangle(color.Transparent))
w := test.NewTempWindow(t, canvas.NewRectangle(color.Transparent))
size := fyne.NewSize(200, 300)
w.Resize(size)

Expand All @@ -96,7 +96,7 @@ func TestCustom_ResizeOnShow(t *testing.T) {

func TestConfirm_SetButtons(t *testing.T) {
test.NewTempApp(t)
w := test.NewWindow(canvas.NewRectangle(color.Transparent))
w := test.NewTempWindow(t, canvas.NewRectangle(color.Transparent))
size := fyne.NewSize(200, 300)
w.Resize(size)

Expand All @@ -118,7 +118,7 @@ func TestConfirm_SetButtons(t *testing.T) {

func TestConfirmWithoutButtons(t *testing.T) {
test.NewTempApp(t)
w := test.NewWindow(canvas.NewRectangle(color.Transparent))
w := test.NewTempWindow(t, canvas.NewRectangle(color.Transparent))
size := fyne.NewSize(200, 300)
w.Resize(size)

Expand All @@ -130,7 +130,7 @@ func TestConfirmWithoutButtons(t *testing.T) {

func TestCustomConfirm_Importance(t *testing.T) {
test.NewTempApp(t)
w := test.NewWindow(canvas.NewRectangle(color.Transparent))
w := test.NewTempWindow(t, canvas.NewRectangle(color.Transparent))
size := fyne.NewSize(200, 300)
w.Resize(size)

Expand Down
4 changes: 2 additions & 2 deletions dialog/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestEntryDialog_Confirm(t *testing.T) {
value := ""
ed := NewEntryDialog("Test", "message", func(v string) {
value = v
}, test.NewWindow(nil))
}, test.NewTempWindow(t, nil))
ed.Show()
test.Type(ed.entry, "123")
test.Tap(ed.confirm)
Expand All @@ -23,7 +23,7 @@ func TestEntryDialog_Dismiss(t *testing.T) {
value := "123"
ed := NewEntryDialog("Test", "message", func(v string) {
value = v
}, test.NewWindow(nil))
}, test.NewTempWindow(t, nil))
ed.Show()
test.Type(ed.entry, "XYZ")
test.Tap(ed.cancel)
Expand Down
Loading