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

Improvements in test runner #5372

Merged
merged 3 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 69 additions & 35 deletions internal/driver/glfw/canvas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ func TestGlCanvas_ChildMinSizeChangeAffectsAncestorsUpToRoot(t *testing.T) {
rightObj2.SetMinSize(fyne.NewSize(100, 50))
rightCol := container.NewVBox(rightObj1, rightObj2)
content := container.NewHBox(leftCol, rightCol)
w.SetContent(content)

safeSetContent(w, content)
repaintWindow(w)

oldCanvasSize := fyne.NewSize(200+3*theme.Padding(), 100+3*theme.Padding())
Expand Down Expand Up @@ -59,10 +60,13 @@ func TestGlCanvas_ChildMinSizeChangeAffectsAncestorsUpToScroll(t *testing.T) {
rightCol := container.NewVBox(rightObj1, rightObj2)
rightColScroll := container.NewScroll(rightCol)
content := container.NewHBox(leftCol, rightColScroll)
w.SetContent(content)

runOnMain(func() {
w.SetContent(content)
})

oldCanvasSize := fyne.NewSize(200+3*theme.Padding(), 100+3*theme.Padding())
w.Resize(oldCanvasSize)
safeResize(w, oldCanvasSize)
repaintWindow(w)

// child size change affects ancestors up to scroll
Expand Down Expand Up @@ -95,13 +99,15 @@ func TestGlCanvas_ChildMinSizeChangesInDifferentScrollAffectAncestorsUpToScroll(
rightCol := container.NewVBox(rightObj1, rightObj2)
rightColScroll := container.NewScroll(rightCol)
content := container.NewHBox(leftColScroll, rightColScroll)
w.SetContent(content)
runOnMain(func() {
w.SetContent(content)
})

oldCanvasSize := fyne.NewSize(
2*leftColScroll.MinSize().Width+3*theme.Padding(),
leftColScroll.MinSize().Height+2*theme.Padding(),
)
w.Resize(oldCanvasSize)
safeResize(w, oldCanvasSize)
repaintWindow(w)

oldLeftColSize := leftCol.Size()
Expand All @@ -126,7 +132,9 @@ func TestGlCanvas_ChildMinSizeChangesInDifferentScrollAffectAncestorsUpToScroll(
func TestGlCanvas_Content(t *testing.T) {
content := &canvas.Circle{}
w := createWindow("Test")
w.SetContent(content)
runOnMain(func() {
w.SetContent(content)
})

assert.Equal(t, content, w.Content())
}
Expand All @@ -147,7 +155,9 @@ func TestGlCanvas_ContentChangeWithoutMinSizeChangeDoesNotLayout(t *testing.T) {
content := container.NewWithoutLayout(leftCol, rightCol)
layout := &recordingLayout{}
content.Layout = layout
w.SetContent(content)
runOnMain(func() {
w.SetContent(content)
})

repaintWindow(w)
// clear the recorded layouts
Expand All @@ -167,7 +177,7 @@ func TestGlCanvas_ContentChangeWithoutMinSizeChangeDoesNotLayout(t *testing.T) {

func TestGlCanvas_Focus(t *testing.T) {
w := createWindow("Test")
w.SetPadded(false)
safeUnpad(w)
c := w.Canvas().(*glCanvas)

ce := &focusable{id: "ce1"}
Expand All @@ -178,7 +188,9 @@ func TestGlCanvas_Focus(t *testing.T) {
overlay1 := container.NewVBox(o1e)
o2e := &focusable{id: "o2e1"}
overlay2 := container.NewVBox(o2e)
w.SetContent(content)
runOnMain(func() {
w.SetContent(content)
})
c.setMenuOverlay(menuOverlay)
c.Overlays().Add(overlay1)
c.Overlays().Add(overlay2)
Expand Down Expand Up @@ -206,17 +218,19 @@ func TestGlCanvas_Focus(t *testing.T) {

func TestGlCanvas_Focus_BeforeVisible(t *testing.T) {
w := createWindow("Test")
w.SetPadded(false)
safeUnpad(w)
e := widget.NewEntry()
c := w.Canvas().(*glCanvas)
c.Focus(e) // this crashed in the past
}

func TestGlCanvas_Focus_SetContent(t *testing.T) {
w := createWindow("Test")
w.SetPadded(false)
safeUnpad(w)
e := widget.NewEntry()
w.SetContent(container.NewHBox(e))
runOnMain(func() {
w.SetContent(container.NewHBox(e))
})
c := w.Canvas().(*glCanvas)
c.Focus(e)
assert.Equal(t, e, c.Focused())
Expand All @@ -227,7 +241,7 @@ func TestGlCanvas_Focus_SetContent(t *testing.T) {

func TestGlCanvas_FocusHandlingWhenAddingAndRemovingOverlays(t *testing.T) {
w := createWindow("Test")
w.SetPadded(false)
safeUnpad(w)
c := w.Canvas().(*glCanvas)

ce1 := &focusable{id: "ce1"}
Expand All @@ -239,7 +253,9 @@ func TestGlCanvas_FocusHandlingWhenAddingAndRemovingOverlays(t *testing.T) {
o2e1 := &focusable{id: "o2e1"}
o2e2 := &focusable{id: "o2e2"}
overlay2 := container.NewVBox(o2e1, o2e2)
w.SetContent(content)
runOnMain(func() {
w.SetContent(content)
})

assert.Nil(t, c.Focused())

Expand Down Expand Up @@ -300,7 +316,7 @@ func TestGlCanvas_InsufficientSizeDoesntTriggerResizeIfSizeIsAlreadyMaxedOut(t *
w := createWindow("Test").(*window)
c := w.Canvas().(*glCanvas)
canvasSize := fyne.NewSize(200, 100)
w.Resize(canvasSize)
safeResize(w, canvasSize)
ensureCanvasSize(t, w, canvasSize)
popUpContent := canvas.NewRectangle(color.Black)
popUpContent.SetMinSize(fyne.NewSize(1000, 10))
Expand Down Expand Up @@ -334,7 +350,9 @@ func TestGlCanvas_MinSizeShrinkTriggersLayout(t *testing.T) {
rightObj2.SetMinSize(fyne.NewSize(100, 50))
rightCol := container.NewVBox(rightObj1, rightObj2)
content := container.NewHBox(leftCol, rightCol)
w.SetContent(content)
runOnMain(func() {
w.SetContent(content)
})

oldCanvasSize := fyne.NewSize(200+3*theme.Padding(), 100+3*theme.Padding())
assert.Equal(t, oldCanvasSize, c.Size())
Expand Down Expand Up @@ -381,28 +399,32 @@ func TestGlCanvas_PixelCoordinateAtPosition(t *testing.T) {

func TestGlCanvas_Resize(t *testing.T) {
w := createWindow("Test")
w.SetPadded(false)
safeUnpad(w)

content := widget.NewLabel("Content")
w.SetContent(content)
runOnMain(func() {
w.SetContent(content)
})
ensureCanvasSize(t, w.(*window), fyne.NewSize(69, 36))

size := fyne.NewSize(200, 100)
assert.NotEqual(t, size, content.Size())

w.Resize(size)
safeResize(w, size)
ensureCanvasSize(t, w.(*window), size)
assert.Equal(t, size, content.Size())
}

// TODO: this can be removed when #707 is addressed
func TestGlCanvas_ResizeWithOtherOverlay(t *testing.T) {
w := createWindow("Test")
w.SetPadded(false)
safeUnpad(w)

content := widget.NewLabel("Content")
over := widget.NewLabel("Over")
w.SetContent(content)
runOnMain(func() {
w.SetContent(content)
})
w.Canvas().Overlays().Add(over)
ensureCanvasSize(t, w.(*window), fyne.NewSize(69, 36))
// TODO: address #707; overlays should always be canvas size
Expand All @@ -412,21 +434,23 @@ func TestGlCanvas_ResizeWithOtherOverlay(t *testing.T) {
assert.NotEqual(t, size, content.Size())
assert.NotEqual(t, size, over.Size())

w.Resize(size)
safeResize(w, size)
ensureCanvasSize(t, w.(*window), size)
assert.Equal(t, size, content.Size(), "canvas content is resized")
assert.Equal(t, size, over.Size(), "canvas overlay is resized")
}

func TestGlCanvas_ResizeWithOverlays(t *testing.T) {
w := createWindow("Test")
w.SetPadded(false)
safeUnpad(w)

content := widget.NewLabel("Content")
o1 := widget.NewLabel("o1")
o2 := widget.NewLabel("o2")
o3 := widget.NewLabel("o3")
w.SetContent(content)
runOnMain(func() {
w.SetContent(content)
})
w.Canvas().Overlays().Add(o1)
w.Canvas().Overlays().Add(o2)
w.Canvas().Overlays().Add(o3)
Expand All @@ -438,7 +462,7 @@ func TestGlCanvas_ResizeWithOverlays(t *testing.T) {
assert.NotEqual(t, size, o2.Size())
assert.NotEqual(t, size, o3.Size())

w.Resize(size)
safeResize(w, size)
ensureCanvasSize(t, w.(*window), size)
assert.Equal(t, size, content.Size(), "canvas content is resized")
assert.Equal(t, size, o1.Size(), "canvas overlay 1 is resized")
Expand All @@ -449,11 +473,13 @@ func TestGlCanvas_ResizeWithOverlays(t *testing.T) {
// TODO: this can be removed when #707 is addressed
func TestGlCanvas_ResizeWithPopUpOverlay(t *testing.T) {
w := createWindow("Test")
w.SetPadded(false)
safeUnpad(w)

content := widget.NewLabel("Content")
over := widget.NewPopUp(widget.NewLabel("Over"), w.Canvas())
w.SetContent(content)
runOnMain(func() {
w.SetContent(content)
})
over.Show()
ensureCanvasSize(t, w.(*window), fyne.NewSize(69, 36))

Expand All @@ -464,7 +490,7 @@ func TestGlCanvas_ResizeWithPopUpOverlay(t *testing.T) {
assert.NotEqual(t, size, over.Size())
assert.NotEqual(t, size, overContentSize)

w.Resize(size)
safeResize(w, size)
ensureCanvasSize(t, w.(*window), size)
assert.Equal(t, size, content.Size(), "canvas content is resized")
assert.Equal(t, size, over.Size(), "canvas overlay is resized")
Expand All @@ -473,10 +499,12 @@ func TestGlCanvas_ResizeWithPopUpOverlay(t *testing.T) {

func TestGlCanvas_ResizeWithModalPopUpOverlay(t *testing.T) {
w := createWindow("Test")
w.SetPadded(false)
safeUnpad(w)

content := widget.NewLabel("Content")
w.SetContent(content)
runOnMain(func() {
w.SetContent(content)
})

popup := widget.NewModalPopUp(widget.NewLabel("PopUp"), w.Canvas())
popupBgSize := fyne.NewSize(975, 575)
Expand All @@ -485,7 +513,7 @@ func TestGlCanvas_ResizeWithModalPopUpOverlay(t *testing.T) {
ensureCanvasSize(t, w.(*window), fyne.NewSize(69, 36))

winSize := fyne.NewSize(1000, 600)
w.Resize(winSize)
safeResize(w, winSize)
ensureCanvasSize(t, w.(*window), winSize)

// get popup content padding dynamically
Expand Down Expand Up @@ -526,15 +554,21 @@ func TestGlCanvas_SetContent(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := createWindow("Test").(*window)
w.SetPadded(tt.padding)
runOnMain(func() {
w.SetPadded(tt.padding)
})
if tt.menu {
w.SetMainMenu(fyne.NewMainMenu(fyne.NewMenu("Test", fyne.NewMenuItem("Test", func() {}))))
runOnMain(func() {
w.SetMainMenu(fyne.NewMainMenu(fyne.NewMenu("Test", fyne.NewMenuItem("Test", func() {}))))
})
}
ensureCanvasSize(t, w, fyne.NewSize(69, 37))
content := canvas.NewCircle(color.Black)
canvasSize := float32(200)
w.SetContent(content)
w.Resize(fyne.NewSize(canvasSize, canvasSize))
runOnMain(func() {
w.SetContent(content)
})
safeResize(w, fyne.NewSize(canvasSize, canvasSize))
ensureCanvasSize(t, w, fyne.NewSize(canvasSize, canvasSize))

newContent := canvas.NewCircle(color.White)
Expand Down
4 changes: 2 additions & 2 deletions internal/driver/glfw/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func Test_gLDriver_AbsolutePositionForObject(t *testing.T) {
c := w.canvas
movl := buildMenuOverlay(mm, w)
c.setMenuOverlay(movl)
w.SetContent(content)
w.Resize(fyne.NewSize(300, 200))
safeSetContent(w, content)
safeResize(w, fyne.NewSize(300, 200))
ensureCanvasSize(t, w, fyne.NewSize(300, 200))

ovli1 := widget.NewLabel("Overlay Item 1")
Expand Down
8 changes: 5 additions & 3 deletions internal/driver/glfw/glfw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ func ensureCanvasSize(t *testing.T, w *window, size fyne.Size) {
}

func repaintWindow(w *window) {
w.RunWithContext(func() {
d.repaintWindow(w)
runOnMain(func() {
w.RunWithContext(func() {
d.repaintWindow(w)
})
})

time.Sleep(time.Millisecond * 150) // wait for the frames to be rendered... o
Expand All @@ -51,7 +53,7 @@ func waitForCanvasSize(t *testing.T, w *window, size fyne.Size, resizeIfNecessar
}
if resizeIfNecessary && attempts%20 == 0 {
// sometimes the resize does not seem to reach the actual window at all
w.Resize(size)
safeResize(w, size)
}
time.Sleep(10 * time.Millisecond)
}
Expand Down
1 change: 0 additions & 1 deletion internal/driver/glfw/menu_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ func TestDarwinMenu(t *testing.T) {

var lastAction string
assertLastAction := func(wantAction string) {
w.WaitForEvents()
assert.Equal(t, wantAction, lastAction)
}

Expand Down
Loading
Loading