SPB Git

Repository Public MIT

GlassBannerTests.swift 4.7 KB 99 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP GlassKit7// File             : Tests/SPGlassComponentsTests/GlassBannerTests.swift8// Module           : Tests9// Repository       : https://git.spboucher.ai/sp-glasskit10// Project Website  : https://glass.spboucher.ai11// Created          : 2026-12-0212// Last Modified    : 2026-12-0213//14// Author           : Simon-Pierre Boucher15// Position         : Professor of Finance16// Institution      : Université du Québec en Outaouais (UQO)17// Organization     : SP Boucher18// Website          : https://spboucher.ai19// Git              : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development      : dev@spboucher.ai23// Support          : support@spboucher.ai24// Security/Admin   : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License          : MIT33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import SwiftUI42import Testing43import SPGlassCore44import SPGlassTokens45import SPGlassThemes46@testable import SPGlassComponents4748@Suite("GlassBanner")49struct GlassBannerTests {50    @Test func everySeverityHasItsOwnSymbolAndWord() {51        #expect(Set(GlassBannerSeverity.allCases.map(\.systemImage)).count == GlassBannerSeverity.allCases.count)52        #expect(Set(GlassBannerSeverity.allCases.map(\.labelKey)).count == GlassBannerSeverity.allCases.count)53        for severity in GlassBannerSeverity.allCases {54            #expect(!severity.label.isEmpty)55        }56    }5758    @Test func severitiesUseTheMatchingThemeRoles() {59        #expect(GlassBannerSeverity.information.colorToken == .accent)60        #expect(GlassBannerSeverity.success.colorToken == .success)61        #expect(GlassBannerSeverity.warning.colorToken == .warning)62        #expect(GlassBannerSeverity.error.colorToken == .destructive)63    }6465    @Test("Banner text reaches AA contrast in every built-in theme", arguments: GlassTheme.builtIn, [ColorScheme.light, .dark])66    func contrast(theme: GlassTheme, scheme: ColorScheme) {67        let resolved = GlassThemeResolver.resolve(theme, colorScheme: scheme)68        for severity in GlassBannerSeverity.allCases {69            guard let tint = resolved.rgba(severity.colorToken), let surface = resolved.rgba(.surface), let text = resolved.rgba(.textPrimary) else { continue }70            let colors = GlassTintedColors.resolve(tint: tint, surface: surface, text: text)71            #expect(colors.foreground.contrastRatio(against: colors.background) >= 4.5, "\(theme.id) \(severity)")72            #expect(colors.accent.contrastRatio(against: colors.background) >= 3, "\(theme.id) \(severity)")73        }74    }7576    @Test func illegibleTextIsReplacedByBlackOrWhite() {77        let grey = GlassRGBA(rgb: 0x808080)78        let onLight = GlassTintedColors.resolve(tint: GlassRGBA(rgb: 0xFFD60A), surface: .white, text: GlassRGBA(rgb: 0xEEEEEE))79        #expect(onLight.foreground == .black)80        let onDark = GlassTintedColors.resolve(tint: GlassRGBA(rgb: 0xFF453A), surface: GlassRGBA(rgb: 0x101014), text: GlassRGBA(rgb: 0x222222))81        #expect(onDark.foreground == .white)82        #expect(GlassTintedColors.legible(preferred: grey, on: grey, minimum: 4.5).contrastRatio(against: grey) >= 4.5)83    }8485    @Test func aWeakTintIsNotUsedForTheIcon() {86        let colors = GlassTintedColors.resolve(tint: GlassRGBA(rgb: 0xFFF3B0), surface: .white, text: .black)87        #expect(colors.accent == colors.foreground)88        let strong = GlassTintedColors.resolve(tint: GlassRGBA(rgb: 0xB00020), surface: .white, text: .black)89        #expect(strong.accent == GlassRGBA(rgb: 0xB00020))90    }9192    @Test("Any background has a legible black or white", arguments: stride(from: 0, through: 255, by: 5).map { UInt32($0) })93    func fallbackAlwaysReachesAA(level: UInt32) {94        let background = GlassRGBA(rgb: level << 16 | level << 8 | level)95        let chosen = GlassTintedColors.legible(preferred: background, on: background, minimum: 4.5)96        #expect(chosen.contrastRatio(against: background) >= 4.5)97    }98}99
Files main
Menu

Theme