Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dweymouth committed Jan 6, 2024
1 parent e283aca commit 673f261
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 27 deletions.
26 changes: 17 additions & 9 deletions widget/radio_group_extended_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func newextendedRadioGroup(opts []string, f func(string)) *extendedRadioGroup {
ret.Options = opts
ret.OnChanged = f
ret.ExtendBaseWidget(ret)
ret.update() // Not needed for extending Radio but for the tests to be able to access items without creating a renderer first.
//ret.update() // Not needed for extending Radio but for the tests to be able to access items without creating a renderer first.

return ret
}
Expand All @@ -32,7 +32,7 @@ func TestRadioGroup_Extended_Selected(t *testing.T) {
radio := newextendedRadioGroup([]string{"Hi"}, func(sel string) {
selected = sel
})
radio.items[0].Tapped(&fyne.PointEvent{Position: fyne.NewPos(theme.Padding(), theme.Padding())})
extendedRadioGroupTestTapItem(t, radio, 0)

assert.Equal(t, "Hi", selected)
}
Expand All @@ -43,7 +43,8 @@ func TestRadioGroup_Extended_Unselected(t *testing.T) {
selected = sel
})
radio.Selected = selected
radio.items[0].Tapped(&fyne.PointEvent{Position: fyne.NewPos(theme.Padding(), theme.Padding())})
radio.Refresh() // sets up selectedIndex
extendedRadioGroupTestTapItem(t, radio, 0)

assert.Equal(t, "", selected)
}
Expand Down Expand Up @@ -89,7 +90,7 @@ func TestRadioGroup_Extended_Disable(t *testing.T) {
})

radio.Disable()
radio.items[0].Tapped(&fyne.PointEvent{Position: fyne.NewPos(theme.Padding(), theme.Padding())})
extendedRadioGroupTestTapItem(t, radio, 0)

assert.Equal(t, "", selected, "RadioGroup should have been disabled.")
}
Expand All @@ -101,11 +102,11 @@ func TestRadioGroup_Extended_Enable(t *testing.T) {
})

radio.Disable()
radio.items[0].Tapped(&fyne.PointEvent{Position: fyne.NewPos(theme.Padding(), theme.Padding())})
extendedRadioGroupTestTapItem(t, radio, 0)
assert.Equal(t, "", selected, "Radio should have been disabled.")

radio.Enable()
radio.items[0].Tapped(&fyne.PointEvent{Position: fyne.NewPos(theme.Padding(), theme.Padding())})
extendedRadioGroupTestTapItem(t, radio, 0)
assert.Equal(t, "Hi", selected, "Radio should have been re-enabled.")
}

Expand All @@ -131,9 +132,9 @@ func TestRadioGroup_Extended_Hovered(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
radio := newextendedRadioGroup(tt.options, nil)
radio.Horizontal = tt.isHorizontal
item1 := radio.items[0]
item1 := test.WidgetRenderer(radio).Objects()[0].(*radioItem)
render1 := cache.Renderer(item1).(*radioItemRenderer)
render2 := cache.Renderer(radio.items[1]).(*radioItemRenderer)
render2 := cache.Renderer(test.WidgetRenderer(radio).Objects()[1].(*radioItem)).(*radioItemRenderer)

assert.False(t, item1.hovered)
assert.Equal(t, color.Transparent, render1.focusIndicator.FillColor)
Expand Down Expand Up @@ -166,7 +167,7 @@ func TestRadioGroup_Extended_Hovered(t *testing.T) {

func TestRadioGroupRenderer_Extended_ApplyTheme(t *testing.T) {
radio := newextendedRadioGroup([]string{"Test"}, func(string) {})
render := cache.Renderer(radio.items[0]).(*radioItemRenderer)
render := cache.Renderer(test.WidgetRenderer(radio).Objects()[0].(*radioItem)).(*radioItemRenderer)

textSize := render.label.TextSize
customTextSize := textSize
Expand All @@ -177,3 +178,10 @@ func TestRadioGroupRenderer_Extended_ApplyTheme(t *testing.T) {

assert.NotEqual(t, textSize, customTextSize)
}

func extendedRadioGroupTestTapItem(t *testing.T, radio *extendedRadioGroup, item int) {
t.Helper()
renderer := test.WidgetRenderer(radio)
radioItem := renderer.Objects()[item].(*radioItem)
radioItem.Tapped(&fyne.PointEvent{Position: fyne.NewPos(theme.Padding(), theme.Padding())})
}
57 changes: 39 additions & 18 deletions widget/radio_group_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestRadioGroup_Selected(t *testing.T) {
radio := NewRadioGroup([]string{"Hi"}, func(sel string) {
selected = sel
})
radio.items[0].Tapped(&fyne.PointEvent{Position: fyne.NewPos(theme.Padding(), theme.Padding())})
radioGroupTestTapItem(t, radio, 0)

assert.Equal(t, "Hi", selected)
}
Expand All @@ -57,15 +57,16 @@ func TestRadioGroup_Unselected(t *testing.T) {
selected = sel
})
radio.Selected = selected
radio.items[0].Tapped(&fyne.PointEvent{Position: fyne.NewPos(theme.Padding(), theme.Padding())})
radio.Refresh() // sets up selectedIndex
radioGroupTestTapItem(t, radio, 0)

assert.Equal(t, "", selected)
}

func TestRadioGroup_DisableWhenSelected(t *testing.T) {
radio := NewRadioGroup([]string{"Hi"}, nil)
radio.SetSelected("Hi")
render := test.WidgetRenderer(radio.items[0]).(*radioItemRenderer)
render := radioGroupTestItemRenderer(t, radio, 0)
icon := fyne.CurrentApp().Settings().Theme().Icon("iconNameRadioButtonFill")
assert.Equal(t, "primary_"+icon.Name(), render.icon.Resource.Name())

Expand All @@ -75,7 +76,7 @@ func TestRadioGroup_DisableWhenSelected(t *testing.T) {

func TestRadioGroup_DisableWhenNotSelected(t *testing.T) {
radio := NewRadioGroup([]string{"Hi"}, nil)
render := test.WidgetRenderer(radio.items[0]).(*radioItemRenderer)
render := radioGroupTestItemRenderer(t, radio, 0)

radio.Disable()
resName := render.over.Resource.Name()
Expand All @@ -87,7 +88,7 @@ func TestRadioGroup_SelectedOther(t *testing.T) {
radio := NewRadioGroup([]string{"Hi", "Hi2"}, func(sel string) {
selected = sel
})
radio.items[1].Tapped(&fyne.PointEvent{Position: fyne.NewPos(theme.Padding(), radio.MinSize().Height-theme.Padding())})
radioGroupTestTapItem(t, radio, 1)

assert.Equal(t, "Hi2", selected)
}
Expand Down Expand Up @@ -156,17 +157,25 @@ func TestRadioGroup_SetSelectedEmpty(t *testing.T) {
func TestRadioGroup_DuplicatedOptions(t *testing.T) {
radio := NewRadioGroup([]string{"Hi", "Hi", "Hi", "Another", "Another"}, nil)

assert.Equal(t, 2, len(radio.Options))
assert.Equal(t, 2, len(test.WidgetRenderer(radio).(*radioGroupRenderer).items))
assert.Equal(t, 5, len(radio.Options))
assert.Equal(t, 5, len(test.WidgetRenderer(radio).(*radioGroupRenderer).items))

radioGroupTestTapItem(t, radio, 1)
assert.Equal(t, "Hi", radio.Selected)
assert.Equal(t, 1, radio.selectedIndex())
item0 := test.WidgetRenderer(radio).Objects()[0].(*radioItem)
assert.Equal(t, false, item0.focused)
item1 := test.WidgetRenderer(radio).Objects()[0].(*radioItem)
assert.Equal(t, false, item1.focused)
}

func TestRadioGroup_AppendDuplicate(t *testing.T) {
radio := NewRadioGroup([]string{"Hi"}, nil)

radio.Append("Hi")

assert.Equal(t, 1, len(radio.Options))
assert.Equal(t, 1, len(test.WidgetRenderer(radio).(*radioGroupRenderer).items))
assert.Equal(t, 2, len(radio.Options))
assert.Equal(t, 2, len(test.WidgetRenderer(radio).(*radioGroupRenderer).items))
}

func TestRadioGroup_Disable(t *testing.T) {
Expand All @@ -176,7 +185,7 @@ func TestRadioGroup_Disable(t *testing.T) {
})

radio.Disable()
radio.items[0].Tapped(&fyne.PointEvent{Position: fyne.NewPos(theme.Padding(), theme.Padding())})
radioGroupTestTapItem(t, radio, 0)

assert.Equal(t, "", selected, "RadioGroup should have been disabled.")
}
Expand All @@ -188,11 +197,11 @@ func TestRadioGroup_Enable(t *testing.T) {
})

radio.Disable()
radio.items[0].Tapped(&fyne.PointEvent{Position: fyne.NewPos(theme.Padding(), theme.Padding())})
radioGroupTestTapItem(t, radio, 0)
assert.Equal(t, "", selected, "Radio should have been disabled.")

radio.Enable()
radio.items[0].Tapped(&fyne.PointEvent{Position: fyne.NewPos(theme.Padding(), theme.Padding())})
radioGroupTestTapItem(t, radio, 0)
assert.Equal(t, "Hi", selected, "Radio should have been re-enabled.")
}

Expand Down Expand Up @@ -228,9 +237,9 @@ func TestRadioGroup_Hovered(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
radio := NewRadioGroup(tt.options, nil)
radio.Horizontal = tt.isHorizontal
item1 := radio.items[0]
render1 := cache.Renderer(item1).(*radioItemRenderer)
render2 := cache.Renderer(radio.items[1]).(*radioItemRenderer)
item1 := test.WidgetRenderer(radio).Objects()[0].(*radioItem)
render1 := radioGroupTestItemRenderer(t, radio, 0)
render2 := radioGroupTestItemRenderer(t, radio, 1)

assert.False(t, item1.hovered)
assert.Equal(t, color.Transparent, render1.focusIndicator.FillColor)
Expand Down Expand Up @@ -282,15 +291,16 @@ func TestRadioGroup_Required(t *testing.T) {
radio.Resize(radio.MinSize())
radio.SetSelected("Hi")
require.Equal(t, "Hi", radio.Selected)
radio.items[0].Tapped(&fyne.PointEvent{Position: fyne.NewPos(theme.Padding(), theme.Padding())})
radioGroupTestTapItem(t, radio, 0)
assert.Equal(t, "Hi", radio.Selected, "tapping selected option of required radio does nothing")
radio.items[1].Tapped(&fyne.PointEvent{Position: fyne.NewPos(theme.Padding(), radio.Size().Height-theme.Padding())})
radioGroupTestTapItem(t, radio, 1)
//radio.items[1].Tapped(&fyne.PointEvent{Position: fyne.NewPos(theme.Padding(), radio.Size().Height-theme.Padding())})
assert.Equal(t, "There", radio.Selected)
}

func TestRadioGroupRenderer_ApplyTheme(t *testing.T) {
radio := NewRadioGroup([]string{"Test"}, func(string) {})
render := cache.Renderer(radio.items[0]).(*radioItemRenderer)
render := radioGroupTestItemRenderer(t, radio, 0)

textSize := render.label.TextSize
customTextSize := textSize
Expand All @@ -301,3 +311,14 @@ func TestRadioGroupRenderer_ApplyTheme(t *testing.T) {

assert.NotEqual(t, textSize, customTextSize)
}

func radioGroupTestTapItem(t *testing.T, radio *RadioGroup, item int) {
t.Helper()
renderer := test.WidgetRenderer(radio)
radioItem := renderer.Objects()[item].(*radioItem)
radioItem.Tapped(&fyne.PointEvent{Position: fyne.NewPos(theme.Padding(), theme.Padding())})
}

func radioGroupTestItemRenderer(t *testing.T, radio *RadioGroup, item int) *radioItemRenderer {
return cache.Renderer(test.WidgetRenderer(radio).Objects()[item].(fyne.Widget)).(*radioItemRenderer)
}

0 comments on commit 673f261

Please sign in to comment.