Skip to content

Commit

Permalink
Add Dismiss (and Confirm where appropriate) to Dialogs to ease testing
Browse files Browse the repository at this point in the history
Fixes #2771
  • Loading branch information
andydotxyz committed Feb 27, 2025
1 parent 92a2cb7 commit e471a5a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 6 deletions.
13 changes: 12 additions & 1 deletion dialog/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,15 @@ type Dialog interface {
Refresh()
Resize(size fyne.Size)

// MinSize returns the size that this dialog should not shrink below.
//
// Since: 2.1
MinSize() fyne.Size

// Dismiss instructs the dialog to close without any affirmative action.
//
// Since: 2.6
Dismiss()
}

// Declare conformity to Dialog interface
Expand All @@ -49,11 +56,15 @@ type dialog struct {
beforeShowHook func()
}

func (d *dialog) Dismiss() {
d.Hide()
}

func (d *dialog) Hide() {
d.hideWithResponse(false)
}

// MinSize returns the size that this dialog should not shrink below
// MinSize returns the size that this dialog should not shrink below.
//
// Since: 2.1
func (d *dialog) MinSize() fyne.Size {
Expand Down
7 changes: 7 additions & 0 deletions dialog/confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ type ConfirmDialog struct {
confirm *widget.Button
}

// Confirm instructs the dialog to close agreeing with whatever content was displayed.
//
// Since: 2.6
func (d *ConfirmDialog) Confirm() {
d.hideWithResponse(true)
}

// SetConfirmText allows custom text to be set in the confirmation button
func (d *ConfirmDialog) SetConfirmText(label string) {
d.confirm.SetText(label)
Expand Down
18 changes: 14 additions & 4 deletions dialog/confirm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestDialog_ConfirmDoubleCallback(t *testing.T) {
cnf.Show()

assert.False(t, cnf.win.Hidden)
go test.Tap(cnf.dismiss)
go cnf.Dismiss()
assert.EqualValues(t, 43, <-ch)
assert.EqualValues(t, 42, <-ch)
assert.True(t, cnf.win.Hidden)
Expand All @@ -43,22 +43,32 @@ func TestDialog_ConfirmCallbackOnlyOnClosed(t *testing.T) {
cnf.Show()

assert.False(t, cnf.win.Hidden)
go test.Tap(cnf.dismiss)
go cnf.Dismiss()
assert.EqualValues(t, 43, <-ch)
assert.True(t, cnf.win.Hidden)
}

func TestDialog_ConfirmCallbackOnlyOnConfirm(t *testing.T) {
ch := make(chan int)
cnf := NewConfirm("Test", "Test", func(_ bool) {
cnf := NewConfirm("Test", "Test", func(ok bool) {
if !ok {
ch <- 0
return
}
ch <- 42
}, test.NewTempWindow(t, nil))
cnf.SetDismissText("No")
cnf.SetConfirmText("Yes")
cnf.Show()

assert.False(t, cnf.win.Hidden)
go test.Tap(cnf.dismiss)
go cnf.Dismiss()
assert.EqualValues(t, 0, <-ch)
assert.True(t, cnf.win.Hidden)

cnf.Show()
assert.False(t, cnf.win.Hidden)
go cnf.Confirm()
assert.EqualValues(t, 42, <-ch)
assert.True(t, cnf.win.Hidden)
}
Expand Down
7 changes: 7 additions & 0 deletions dialog/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,13 @@ func showFile(file *FileDialog) *fileDialog {
return d
}

// Dismiss instructs the dialog to close without any affirmative action.
//
// Since: 2.6
func (f *FileDialog) Dismiss() {
f.dialog.dismiss.OnTapped()
}

// MinSize returns the size that this dialog should not shrink below
//
// Since: 2.1
Expand Down
2 changes: 1 addition & 1 deletion dialog/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ func TestFileFavorites(t *testing.T) {
assert.True(t, ok)
}

test.Tap(dlg.dialog.dismiss)
dlg.Dismiss()
}

func TestSetFileNameBeforeShow(t *testing.T) {
Expand Down

0 comments on commit e471a5a

Please sign in to comment.