Skip to content

Commit e471a5a

Browse files
committed
Add Dismiss (and Confirm where appropriate) to Dialogs to ease testing
Fixes #2771
1 parent 92a2cb7 commit e471a5a

File tree

5 files changed

+41
-6
lines changed

5 files changed

+41
-6
lines changed

dialog/base.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,15 @@ type Dialog interface {
2727
Refresh()
2828
Resize(size fyne.Size)
2929

30+
// MinSize returns the size that this dialog should not shrink below.
31+
//
3032
// Since: 2.1
3133
MinSize() fyne.Size
34+
35+
// Dismiss instructs the dialog to close without any affirmative action.
36+
//
37+
// Since: 2.6
38+
Dismiss()
3239
}
3340

3441
// Declare conformity to Dialog interface
@@ -49,11 +56,15 @@ type dialog struct {
4956
beforeShowHook func()
5057
}
5158

59+
func (d *dialog) Dismiss() {
60+
d.Hide()
61+
}
62+
5263
func (d *dialog) Hide() {
5364
d.hideWithResponse(false)
5465
}
5566

56-
// MinSize returns the size that this dialog should not shrink below
67+
// MinSize returns the size that this dialog should not shrink below.
5768
//
5869
// Since: 2.1
5970
func (d *dialog) MinSize() fyne.Size {

dialog/confirm.go

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ type ConfirmDialog struct {
1515
confirm *widget.Button
1616
}
1717

18+
// Confirm instructs the dialog to close agreeing with whatever content was displayed.
19+
//
20+
// Since: 2.6
21+
func (d *ConfirmDialog) Confirm() {
22+
d.hideWithResponse(true)
23+
}
24+
1825
// SetConfirmText allows custom text to be set in the confirmation button
1926
func (d *ConfirmDialog) SetConfirmText(label string) {
2027
d.confirm.SetText(label)

dialog/confirm_test.go

+14-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestDialog_ConfirmDoubleCallback(t *testing.T) {
2626
cnf.Show()
2727

2828
assert.False(t, cnf.win.Hidden)
29-
go test.Tap(cnf.dismiss)
29+
go cnf.Dismiss()
3030
assert.EqualValues(t, 43, <-ch)
3131
assert.EqualValues(t, 42, <-ch)
3232
assert.True(t, cnf.win.Hidden)
@@ -43,22 +43,32 @@ func TestDialog_ConfirmCallbackOnlyOnClosed(t *testing.T) {
4343
cnf.Show()
4444

4545
assert.False(t, cnf.win.Hidden)
46-
go test.Tap(cnf.dismiss)
46+
go cnf.Dismiss()
4747
assert.EqualValues(t, 43, <-ch)
4848
assert.True(t, cnf.win.Hidden)
4949
}
5050

5151
func TestDialog_ConfirmCallbackOnlyOnConfirm(t *testing.T) {
5252
ch := make(chan int)
53-
cnf := NewConfirm("Test", "Test", func(_ bool) {
53+
cnf := NewConfirm("Test", "Test", func(ok bool) {
54+
if !ok {
55+
ch <- 0
56+
return
57+
}
5458
ch <- 42
5559
}, test.NewTempWindow(t, nil))
5660
cnf.SetDismissText("No")
5761
cnf.SetConfirmText("Yes")
5862
cnf.Show()
5963

6064
assert.False(t, cnf.win.Hidden)
61-
go test.Tap(cnf.dismiss)
65+
go cnf.Dismiss()
66+
assert.EqualValues(t, 0, <-ch)
67+
assert.True(t, cnf.win.Hidden)
68+
69+
cnf.Show()
70+
assert.False(t, cnf.win.Hidden)
71+
go cnf.Confirm()
6272
assert.EqualValues(t, 42, <-ch)
6373
assert.True(t, cnf.win.Hidden)
6474
}

dialog/file.go

+7
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,13 @@ func showFile(file *FileDialog) *fileDialog {
698698
return d
699699
}
700700

701+
// Dismiss instructs the dialog to close without any affirmative action.
702+
//
703+
// Since: 2.6
704+
func (f *FileDialog) Dismiss() {
705+
f.dialog.dismiss.OnTapped()
706+
}
707+
701708
// MinSize returns the size that this dialog should not shrink below
702709
//
703710
// Since: 2.1

dialog/file_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ func TestFileFavorites(t *testing.T) {
743743
assert.True(t, ok)
744744
}
745745

746-
test.Tap(dlg.dialog.dismiss)
746+
dlg.Dismiss()
747747
}
748748

749749
func TestSetFileNameBeforeShow(t *testing.T) {

0 commit comments

Comments
 (0)