SPB Git

Repository Public License

ThemeTests.swift 7.6 KB 162 lines swift

1import SwiftUI2import Testing3@testable import LunarDesign45@Suite struct LunarThemeTests {6    static let renditions: [LunarTheme] = LunarThemes.all.flatMap { [$0.dark, $0.light] }7    /// Includes the Increase Contrast rendition of every theme.8    static let allRenditions: [LunarTheme] = renditions + renditions.map(\.increasedContrast)910    @Test func thereAreExactlyTwelveThemes() {11        #expect(LunarThemes.all.count == 12)12        #expect(LunarThemes.all.map(\.name) == [13            "Lunar", "Obsidian", "Aurora", "Eclipse", "Silver", "Nebula", "Ocean", "Solar", "Forest", "Rose",14            "Monochrome", "High Contrast",15        ])16        #expect(LunarThemes.default.id == "lunar")17    }1819    /// Persisted values: these strings must never change once shipped.20    @Test func themeIdentifiersAreUniqueAndStable() {21        let ids = LunarThemes.all.map(\.id)22        #expect(ids == [23            "lunar", "obsidian", "aurora", "eclipse", "silver", "nebula", "ocean", "solar", "forest", "rose",24            "monochrome", "high-contrast",25        ])26        #expect(Set(ids).count == ids.count)27        #expect(Set(Self.renditions.map(\.id)).count == 24)28        for family in LunarThemes.all {29            #expect(family.dark.id == "\(family.id).dark" && family.light.id == "\(family.id).light")30            #expect(family.dark.familyID == family.id && family.light.familyID == family.id)31            #expect(family.dark.variant == .dark && family.light.variant == .light)32            #expect(LunarThemes.family(id: family.id) == family)33        }34        #expect(LunarThemes.family(id: "no-such-theme") == LunarThemes.lunar)35    }3637    @Test func lunarDarkUsesTheCorePalette() {38        let lunar = LunarThemes.lunar.dark39        #expect(lunar.windowBackground == LunarPalette.obsidian)40        #expect(lunar.chromeSurface == LunarPalette.midnight)41        #expect(lunar.elevatedSurface == LunarPalette.lunarSlate)42        #expect(lunar.textPrimary == LunarPalette.silver)43        #expect(lunar.accent == LunarPalette.lunarBlue)44        #expect(lunar.accentSecondary == LunarPalette.spectralViolet)45    }4647    @Test func variantsAreReallyDarkAndLight() {48        for family in LunarThemes.all {49            #expect(family.dark.windowBackground.relativeLuminance < 0.02, "\(family.id) dark field")50            #expect(family.light.windowBackground.relativeLuminance > 0.85, "\(family.id) light field")51            // A designed light mode: its accent is not the dark accent.52            if family.id != "monochrome" { #expect(family.light.accent != family.dark.accent, "\(family.id)") }53            #expect(family.theme(for: .dark) == family.dark && family.theme(for: .light) == family.light)54        }55    }5657    @Test(arguments: allRenditions)58    func primaryTextContrast(theme: LunarTheme) {59        let minimum = theme.familyID == "high-contrast" ? 7.0 : 4.560        for surface in theme.textSurfaces {61            let ratio = theme.textPrimary.contrastRatio(against: surface)62            #expect(ratio >= minimum, "\(theme.id): primary text \(ratio) on \(surface.hexString)")63        }64    }6566    @Test(arguments: allRenditions)67    func secondaryTextContrast(theme: LunarTheme) {68        let minimum = theme.familyID == "high-contrast" ? 7.0 : 4.569        for surface in theme.textSurfaces {70            let ratio = theme.textSecondary.contrastRatio(against: surface)71            #expect(ratio >= minimum, "\(theme.id): secondary text \(ratio) on \(surface.hexString)")72        }73    }7475    /// Tertiary text is for hints and placeholders: it still meets the 3:1 floor of large or incidental text.76    @Test(arguments: allRenditions)77    func tertiaryTextContrast(theme: LunarTheme) {78        for surface in theme.textSurfaces {79            let ratio = theme.textTertiary.contrastRatio(against: surface)80            #expect(ratio >= 3, "\(theme.id): tertiary text \(ratio) on \(surface.hexString)")81        }82    }8384    @Test(arguments: allRenditions)85    func accentAndStatusContrast(theme: LunarTheme) {86        let colours: [(String, LunarRGBA)] = [87            ("accent", theme.accent), ("accentSecondary", theme.accentSecondary), ("success", theme.success),88            ("warning", theme.warning), ("danger", theme.danger), ("focusRing", theme.focusRing),89            ("orbitCore", theme.orbitCore),90        ]91        for (name, colour) in colours {92            for surface in theme.textSurfaces {93                let ratio = colour.contrastRatio(against: surface)94                #expect(ratio >= 3, "\(theme.id): \(name) \(ratio) on \(surface.hexString)")95            }96        }97        // Accent is also used as text (links, selected chips): it must reach AA on the window and on chrome.98        for surface in [theme.windowBackground, theme.chromeSurface, theme.elevatedSurface] {99            #expect(theme.accent.contrastRatio(against: surface) >= 4.5, "\(theme.id): accent as text")100        }101        #expect(theme.onAccent.contrastRatio(against: theme.accent) >= 4.5, "\(theme.id): text on accent")102    }103104    /// Selected rows and chips: text stays legible on the selection fill laid over any surface.105    @Test(arguments: allRenditions)106    func selectionKeepsTextLegible(theme: LunarTheme) {107        for surface in theme.textSurfaces {108            let selected = theme.selection.composited(over: surface)109            #expect(theme.textPrimary.contrastRatio(against: selected) >= 4.5, "\(theme.id): text on selection")110            #expect(theme.accent.contrastRatio(against: selected) >= 3, "\(theme.id): accent on selection")111        }112    }113114    @Test(arguments: allRenditions)115    func tintPaletteContrast(theme: LunarTheme) {116        for tint in LunarTint.allCases {117            for surface in theme.textSurfaces {118                let ratio = theme.color(for: tint).contrastRatio(against: surface)119                #expect(ratio >= 3, "\(theme.id): tint \(tint.rawValue) \(ratio) on \(surface.hexString)")120            }121        }122    }123124    @Test(arguments: renditions)125    func structuralRolesAreCoherent(theme: LunarTheme) {126        #expect(theme.orbitRings.count == 3)127        #expect(theme.hairline.alpha > 0 && theme.hairline.alpha < 1)128        #expect(theme.hairlineStrong.alpha >= theme.hairline.alpha)129        #expect(theme.selection.alpha < 0.5)130        #expect(theme.shadow.alpha <= 0.5, "\(theme.id): shadows stay light")131        // Surfaces are layered: elevated is never darker than the window in dark mode nor darker than chrome in light.132        if theme.isDark {133            #expect(theme.elevatedSurface.relativeLuminance >= theme.chromeSurface.relativeLuminance)134            #expect(theme.chromeSurface.relativeLuminance >= theme.windowBackground.relativeLuminance)135        } else {136            #expect(theme.elevatedSurface.relativeLuminance >= theme.chromeSurface.relativeLuminance)137        }138        let stronger = theme.increasedContrast139        for surface in theme.textSurfaces {140            #expect(stronger.textSecondary.contrastRatio(against: surface)141                        >= theme.textSecondary.contrastRatio(against: surface))142        }143        #expect(stronger.hairline.alpha > theme.hairline.alpha || theme.familyID == "high-contrast")144    }145146    @Test func themesAreCodable() throws {147        for theme in Self.renditions {148            let data = try JSONEncoder().encode(theme)149            #expect(try JSONDecoder().decode(LunarTheme.self, from: data) == theme)150        }151    }152153    @MainActor @Test func environmentDefaultsToLunarDark() {154        #expect(EnvironmentValues().lunarTheme == LunarThemes.lunar.dark)155        #expect(EnvironmentValues().lunarRenderMode == .live)156    }157}158159extension LunarTheme: CustomTestStringConvertible {160    public var testDescription: String { id }161}162
Files main
Menu

Theme