Skip to content

Commit

Permalink
Remove call of callback with nil argument
Browse files Browse the repository at this point in the history
  • Loading branch information
ole108 committed Nov 10, 2024
1 parent 6661a81 commit fce2b2f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 25 deletions.
5 changes: 1 addition & 4 deletions dialog/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,7 @@ func (p *ColorPickerDialog) updateUI() {
w.Hide()
}
p.dialog.dismiss = &widget.Button{Text: lang.L("Cancel"), Icon: theme.CancelIcon(),
OnTapped: func() {
p.callback(nil)
p.dialog.Hide()
},
OnTapped: p.dialog.Hide,
}
if p.Advanced {
p.picker = newColorAdvancedPicker(p.color, func(c color.Color) {
Expand Down
63 changes: 42 additions & 21 deletions dialog/color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ func TestColorDialog_SetColor(t *testing.T) {

func TestColorDialog_Buttons(t *testing.T) {
for name, tt := range map[string]struct {
action func(d *ColorPickerDialog, w fyne.Window)
advanced bool
expectedCalled bool
expectedColored bool
action func(d *ColorPickerDialog, w fyne.Window)
advanced bool
expectedCalled bool
expectedClosed bool
}{
"confirmAdvanced": {
action: func(d *ColorPickerDialog, w fyne.Window) {
Expand All @@ -107,28 +107,28 @@ func TestColorDialog_Buttons(t *testing.T) {
test.FocusNext(w.Canvas()) // confirm button
w.Canvas().Focused().TypedKey(&fyne.KeyEvent{Name: fyne.KeySpace})
},
advanced: true,
expectedCalled: true,
expectedColored: true,
advanced: true,
expectedCalled: true,
expectedClosed: true,
},
"dismissAdvanced": {
action: func(d *ColorPickerDialog, w fyne.Window) {
test.FocusNext(w.Canvas()) // advanced accordion
test.FocusNext(w.Canvas()) // dismiss button
w.Canvas().Focused().TypedKey(&fyne.KeyEvent{Name: fyne.KeySpace})
},
advanced: true,
expectedCalled: true,
expectedColored: false,
advanced: true,
expectedCalled: false,
expectedClosed: true,
},
"dismissSimple": {
action: func(d *ColorPickerDialog, w fyne.Window) {
test.FocusNext(w.Canvas()) // dismiss button
w.Canvas().Focused().TypedKey(&fyne.KeyEvent{Name: fyne.KeySpace})
},
advanced: false,
expectedCalled: true,
expectedColored: false,
advanced: false,
expectedCalled: false,
expectedClosed: true,
},
} {
t.Run(name, func(t *testing.T) {
Expand All @@ -137,24 +137,26 @@ func TestColorDialog_Buttons(t *testing.T) {
w.Resize(fyne.NewSize(800, 600))

called := false
colored := false
d := NewColorPicker("Color Picker", "Pick a Color", func(c color.Color) {
closed := false
d := NewColorPicker("Color Picker", "Pick a Color", func(_ color.Color) {
called = true
colored = c != nil
}, w)
d.Advanced = tt.advanced
d.SetOnClosed(func() {
closed = true
})
d.Refresh()
d.Show()
tt.action(d, w)

assert.Equal(t, tt.expectedCalled, called, "called isn't equal")
assert.Equal(t, tt.expectedColored, colored, "colored isn't equal")
assert.Equal(t, tt.expectedClosed, closed, "closed isn't equal")
})
}
}

func TestColorDialog_DoubleCallback(t *testing.T) {
ch := make(chan int)
ch := make(chan int, 2)
d := NewColorPicker("Color Picker", "Pick a Color", func(_ color.Color) {
ch <- 42
}, test.NewTempWindow(t, nil))
Expand All @@ -163,14 +165,33 @@ func TestColorDialog_DoubleCallback(t *testing.T) {
})
d.Refresh()
d.Show()

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

ch = make(chan int, 2)
w := test.NewTempWindow(t, canvas.NewRectangle(color.Transparent))
d = NewColorPicker("Color Picker", "Pick a Color", func(_ color.Color) {
ch <- 52
}, w)
d.Advanced = true
d.SetOnClosed(func() {
ch <- 53
})
d.Refresh()
d.Show()
assert.False(t, d.win.Hidden)
test.FocusNext(w.Canvas()) // advanced accordion
test.FocusNext(w.Canvas()) // dismiss button
test.FocusNext(w.Canvas()) // confirm button
w.Canvas().Focused().TypedKey(&fyne.KeyEvent{Name: fyne.KeySpace})
assert.EqualValues(t, <-ch, 52)
assert.EqualValues(t, <-ch, 53)
//assert.True(t, d.win.Hidden, "hidden status of window") shouldn't this work???
close(ch)
}

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

0 comments on commit fce2b2f

Please sign in to comment.