Skip to content

Commit

Permalink
Merge pull request #4921 from toaster/refactoring/test
Browse files Browse the repository at this point in the history
Improve test theme tests
  • Loading branch information
toaster committed Jun 10, 2024
2 parents 44fa9f6 + 40aaa22 commit 832fe62
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 20 deletions.
4 changes: 4 additions & 0 deletions test/markup_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,17 +406,21 @@ func knownColor(c color.Color) string {
nrgbaColor(theme.ForegroundColor()): "foreground",
nrgbaColor(theme.Color(theme.ColorNameHeaderBackground)): "headerBackground",
nrgbaColor(theme.HoverColor()): "hover",
nrgbaColor(theme.Color(theme.ColorNameHyperlink)): "hyperlink",
nrgbaColor(theme.InputBackgroundColor()): "inputBackground",
nrgbaColor(theme.InputBorderColor()): "inputBorder",
nrgbaColor(theme.MenuBackgroundColor()): "menuBackground",
nrgbaColor(theme.Color(theme.ColorNameOnPrimary)): "onPrimary",
nrgbaColor(theme.OverlayBackgroundColor()): "overlayBackground",
nrgbaColor(theme.PlaceHolderColor()): "placeholder",
nrgbaColor(theme.Color(theme.ColorNamePressed)): "pressed",
nrgbaColor(theme.PrimaryColor()): "primary",
nrgbaColor(theme.ScrollBarColor()): "scrollbar",
nrgbaColor(theme.SelectionColor()): "selection",
nrgbaColor(theme.Color(theme.ColorNameSeparator)): "separator",
nrgbaColor(theme.Color(theme.ColorNameSuccess)): "success",
nrgbaColor(theme.ShadowColor()): "shadow",
nrgbaColor(theme.Color(theme.ColorNameWarning)): "warning",
}[nrgbaColor(c)]
}

Expand Down
6 changes: 6 additions & 0 deletions test/markup_renderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import (
"fyne.io/fyne/v2/widget"
)

func Test_knownColor(t *testing.T) {
for _, name := range knownColorNames {
assert.NotEmpty(t, knownColor(theme.Color(name)), "color name %s is not known", name)
}
}

func Test_snapshot(t *testing.T) {
// content elements
for name, tt := range map[string]struct {
Expand Down
13 changes: 9 additions & 4 deletions test/theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ func NewTheme() fyne.Theme {
theme.ColorNameError: blue(255),
theme.ColorNameFocus: red(66),
theme.ColorNameForeground: gray(255),
theme.ColorNameHover: green(200),
theme.ColorNameHeaderBackground: red(22),
theme.ColorNameHover: green(200),
theme.ColorNameHyperlink: blue(240),
theme.ColorNameInputBackground: red(30),
theme.ColorNameInputBorder: gray(10),
theme.ColorNameMenuBackground: red(50),
Expand All @@ -45,9 +46,11 @@ func NewTheme() fyne.Theme {
theme.ColorNamePressed: blue(250),
theme.ColorNamePrimary: green(255),
theme.ColorNameScrollBar: blue(220),
theme.ColorNameSeparator: gray(30),
theme.ColorNameSelection: red(55),
theme.ColorNameSeparator: gray(30),
theme.ColorNameShadow: blue(150),
theme.ColorNameSuccess: green(150),
theme.ColorNameWarning: red(100),
},
fonts: map[fyne.TextStyle]fyne.Resource{
{}: theme.DefaultTextBoldFont(),
Expand Down Expand Up @@ -89,8 +92,8 @@ func Theme() fyne.Theme {
theme.ColorNameError: color.NRGBA{R: 0xf4, G: 0x43, B: 0x36, A: 0xff},
theme.ColorNameFocus: color.NRGBA{R: 0x78, G: 0x3a, B: 0x3a, A: 0xff},
theme.ColorNameForeground: color.NRGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff},
theme.ColorNameHover: color.NRGBA{R: 0x88, G: 0xff, B: 0xff, A: 0x22},
theme.ColorNameHeaderBackground: color.NRGBA{R: 0x25, G: 0x25, B: 0x25, A: 0xff},
theme.ColorNameHover: color.NRGBA{R: 0x88, G: 0xff, B: 0xff, A: 0x22},
theme.ColorNameHyperlink: color.NRGBA{R: 0xff, G: 0xcc, B: 0x80, A: 0xff},
theme.ColorNameInputBackground: color.NRGBA{R: 0x66, G: 0x66, B: 0x66, A: 0xff},
theme.ColorNameInputBorder: color.NRGBA{R: 0x86, G: 0x86, B: 0x86, A: 0xff},
Expand All @@ -101,9 +104,11 @@ func Theme() fyne.Theme {
theme.ColorNamePressed: color.NRGBA{R: 0x00, G: 0x00, B: 0x00, A: 0x33},
theme.ColorNamePrimary: color.NRGBA{R: 0xff, G: 0xc0, B: 0x80, A: 0xff},
theme.ColorNameScrollBar: color.NRGBA{R: 0x00, G: 0x00, B: 0x00, A: 0xaa},
theme.ColorNameSeparator: color.NRGBA{R: 0x90, G: 0x90, B: 0x90, A: 0xff},
theme.ColorNameSelection: color.NRGBA{R: 0x78, G: 0x3a, B: 0x3a, A: 0x99},
theme.ColorNameSeparator: color.NRGBA{R: 0x90, G: 0x90, B: 0x90, A: 0xff},
theme.ColorNameShadow: color.NRGBA{R: 0x00, G: 0x00, B: 0x00, A: 0x88},
theme.ColorNameSuccess: color.NRGBA{R: 0x00, G: 0x99, B: 0x00, A: 0xff},
theme.ColorNameWarning: color.NRGBA{R: 0xee, G: 0xee, B: 0x00, A: 0xff},
},
fonts: map[fyne.TextStyle]fyne.Resource{
{}: theme.DefaultTextFont(),
Expand Down
96 changes: 80 additions & 16 deletions test/theme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,92 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/theme"
)

func Test_NewTheme_checkForUniqueColors(t *testing.T) {
th := NewTheme().(*configurableTheme)
seen := map[string]fyne.ThemeColorName{}
for cn, c := range th.colors {
r, g, b, a := c.RGBA()
key := fmt.Sprintf("%d %d %d %d", r, g, b, a)
assert.True(t, seen[key] == "", "color value %#v for color %s already used for color %s in theme %s", c, cn, seen[key], th.name)
seen[key] = cn
// Try to keep these in sync with the existing color names at theme/color.go.
var knownColorNames = []fyne.ThemeColorName{
theme.ColorNameBackground,
theme.ColorNameButton,
theme.ColorNameDisabledButton,
theme.ColorNameDisabled,
theme.ColorNameError,
theme.ColorNameFocus,
theme.ColorNameForeground,
theme.ColorNameHeaderBackground,
theme.ColorNameHover,
theme.ColorNameHyperlink,
theme.ColorNameInputBackground,
theme.ColorNameInputBorder,
theme.ColorNameMenuBackground,
theme.ColorNameOnPrimary,
theme.ColorNameOverlayBackground,
theme.ColorNamePlaceHolder,
theme.ColorNamePressed,
theme.ColorNamePrimary,
theme.ColorNameScrollBar,
theme.ColorNameSelection,
theme.ColorNameSeparator,
theme.ColorNameShadow,
theme.ColorNameSuccess,
theme.ColorNameWarning,
}

// Try to keep this in sync with the existing variants at theme/theme.go
var knownVariants = []fyne.ThemeVariant{
theme.VariantDark,
theme.VariantLight,
}

func Test_NewTheme(t *testing.T) {
suite.Run(t, &configurableThemeTestSuite{
constructor: NewTheme,
name: "Ugly Test Theme",
})
}

func Test_Theme(t *testing.T) {
suite.Run(t, &configurableThemeTestSuite{
constructor: Theme,
name: "Default Test Theme",
})
}

type configurableThemeTestSuite struct {
suite.Suite
constructor func() fyne.Theme
name string
}

func (s *configurableThemeTestSuite) TestAllColorsDefined() {
t := s.T()
th := s.constructor()
for _, variant := range knownVariants {
for _, cn := range knownColorNames {
assert.NotNil(t, th.Color(cn, variant), "undefined color %s variant %d in theme %s", cn, variant, s.name)
}
}
}

func Test_Theme_checkForUniqueColors(t *testing.T) {
th := Theme().(*configurableTheme)
seen := map[string]fyne.ThemeColorName{}
for cn, c := range th.colors {
r, g, b, a := c.RGBA()
key := fmt.Sprintf("%d %d %d %d", r, g, b, a)
assert.True(t, seen[key] == "", "color value %#v for color %s already used for color %s in theme %s", c, cn, seen[key], th.name)
seen[key] = cn
func (s *configurableThemeTestSuite) TestUniqueColorValues() {
t := s.T()
th := s.constructor()
seenByVariant := map[fyne.ThemeVariant]map[string]fyne.ThemeColorName{}
for _, variant := range knownVariants {
seen := seenByVariant[variant]
if seen == nil {
seen = map[string]fyne.ThemeColorName{}
seenByVariant[variant] = seen
}
for _, cn := range knownColorNames {
c := th.Color(cn, theme.VariantDark)
r, g, b, a := c.RGBA()
key := fmt.Sprintf("%d %d %d %d", r, g, b, a)
assert.True(t, seen[key] == "", "color value %#v for color %s variant %d already used for color %s in theme %s", c, cn, variant, seen[key], s.name)
seen[key] = cn
}
}
}
1 change: 1 addition & 0 deletions theme/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fyne.io/fyne/v2"
)

// Keep in mind to add new constants to the tests at test/theme_test.go.
const (
// ColorRed is the red primary color name.
//
Expand Down
1 change: 1 addition & 0 deletions theme/theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fyne.io/fyne/v2/internal/cache"
)

// Keep in mind to add new constants to the tests at test/theme_test.go.
const (
// VariantDark is the version of a theme that satisfies a user preference for a dark look.
//
Expand Down

0 comments on commit 832fe62

Please sign in to comment.