Files
deskview/internal/ui/theme_test.go
T
2026-10-28 08:47:09 +08:00

118 lines
3.7 KiB
Go

package ui
import (
"image/color"
"testing"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/test"
"fyne.io/fyne/v2/theme"
)
// isGray 判断颜色是否是无彩度的灰度色。
func isGray(c color.Color) bool {
n := color.NRGBAModel.Convert(c).(color.NRGBA)
return n.R == n.G && n.G == n.B
}
func TestThemeLabelRoundTrip(t *testing.T) {
for _, mode := range []string{ThemeSystem, ThemeContrast, ThemeLight} {
label := themeLabel(mode)
if got := themeFromLabel(label); got != mode {
t.Fatalf("主题 %q 的中文名 %q 反查应为 %q,实际 %q", mode, label, mode, got)
}
}
if got := themeFromLabel("不存在的名字"); got != ThemeSystem {
t.Fatalf("未知中文名应回落到 %q,实际 %q", ThemeSystem, got)
}
if len(themeOptions()) != 3 {
t.Fatalf("主题下拉应有 3 个选项,实际 %v", themeOptions())
}
}
func TestDarkPaletteIsPureBlackOnWhite(t *testing.T) {
p := darkPalette()
black := color.NRGBA{R: 0, G: 0, B: 0, A: 0xFF}
white := color.NRGBA{R: 0xFF, G: 0xFF, B: 0xFF, A: 0xFF}
if got := p[theme.ColorNameBackground]; got != black {
t.Fatalf("黑白主题背景应为纯黑,实际 %v", got)
}
if got := p[theme.ColorNameForeground]; got != white {
t.Fatalf("黑白主题文字应为纯白,实际 %v", got)
}
for name, c := range p {
if !isGray(c) {
t.Fatalf("黑白主题不应出现彩色:%s = %v", name, c)
}
}
}
func TestLightPaletteIsPureWhiteOnBlack(t *testing.T) {
p := lightPalette()
black := color.NRGBA{R: 0, G: 0, B: 0, A: 0xFF}
white := color.NRGBA{R: 0xFF, G: 0xFF, B: 0xFF, A: 0xFF}
if got := p[theme.ColorNameBackground]; got != white {
t.Fatalf("浅色主题背景应为纯白,实际 %v", got)
}
if got := p[theme.ColorNameForeground]; got != black {
t.Fatalf("浅色主题文字应为纯黑,实际 %v", got)
}
for name, c := range p {
if !isGray(c) {
t.Fatalf("浅色高对比主题不应出现彩色:%s = %v", name, c)
}
}
}
func TestMonoThemeFallsBackToDefault(t *testing.T) {
th := newMonoTheme(darkPalette())
black := color.NRGBA{R: 0, G: 0, B: 0, A: 0xFF}
// 调色板里有的走调色板。
if got := th.Color(theme.ColorNameBackground, theme.VariantDark); got != black {
t.Fatalf("背景色应取自调色板,实际 %v", got)
}
// 调色板里没有的沿用默认主题。
if got := th.Color(theme.ColorNameOverlayBackground, theme.VariantDark); got == nil {
t.Fatal("未覆盖的颜色名应回落到默认主题")
}
if th.Font(fyne.TextStyle{}) == nil || th.Size(theme.SizeNamePadding) <= 0 {
t.Fatal("字体与尺寸应沿用默认主题")
}
}
func TestApplyThemeModeSwitchesTheme(t *testing.T) {
app := test.NewApp()
black := color.NRGBA{R: 0, G: 0, B: 0, A: 0xFF}
white := color.NRGBA{R: 0xFF, G: 0xFF, B: 0xFF, A: 0xFF}
applyThemeMode(ThemeContrast)
mt, ok := app.Settings().Theme().(*monoTheme)
if !ok {
t.Fatalf("黑白模式应切到 monoTheme,实际 %T", app.Settings().Theme())
}
if got := mt.Color(theme.ColorNameBackground, app.Settings().ThemeVariant()); got != black {
t.Fatalf("黑白模式背景应为纯黑,实际 %v", got)
}
applyThemeMode(ThemeLight)
mt, ok = app.Settings().Theme().(*monoTheme)
if !ok {
t.Fatalf("浅色模式应切到 monoTheme,实际 %T", app.Settings().Theme())
}
if got := mt.Color(theme.ColorNameBackground, app.Settings().ThemeVariant()); got != white {
t.Fatalf("浅色模式背景应为纯白,实际 %v", got)
}
applyThemeMode(ThemeSystem)
if _, ok := app.Settings().Theme().(*monoTheme); ok {
t.Fatal("跟随系统应还原默认主题")
}
if got := app.Settings().Theme().Color(theme.ColorNameBackground, app.Settings().ThemeVariant()); got == nil {
t.Fatal("默认主题应能取到背景色")
}
}