DomainDigTests/ThemeTests.swift
167 lines · 7036 bytes
14 symbols in this file
ThemeTestscolorsetNamestestEveryTokenHasAColorsettestNoColorsetIsMissingATokentestTheDefaultThemeResolvesEveryTokenFromTheCatalogtestAnEmptyOverrideSetIsTheDefaulttestTheDefaultAccentOverridesNothingtestAnAccentOverridesOnlyTheAccentTokenstestEveryAccentHasATitletestAccentThemesArePrOPlusOnlytestEveryAccentsIconNameHasAnIconSettestTheDefaultAccentUsesThePrimaryIcontestStatusBadgesNameTokensRatherThanColorstestEveryToneMapsToATokenPair
1import SwiftUI
2import XCTest
3@testable import DomainDig
4
5/// The palette moved from 483 asset-catalog call sites into a `Theme` carried
6/// in the environment (I1's sibling, W13).
7///
8/// The one safety the move gave up is compile-time checking of the colour
9/// names: `ThemeToken.assetColor` looks them up by string, because the
10/// generated `ColorResource` symbols are MainActor-isolated and
11/// `ShapeStyle.resolve(in:)` is not. `testEveryTokenHasAColorset` and
12/// `testNoColorsetIsMissingAToken` are what replace it.
13final class ThemeTests: XCTestCase {
14
15 /// The colorsets on disk, which are the palette's definition.
16 private func colorsetNames() throws -> Set<String> {
17 // The bundle under test is the app's, and the catalog is compiled into
18 // it, so the names are read from the source tree instead.
19 let root = URL(fileURLWithPath: #filePath)
20 .deletingLastPathComponent()
21 .deletingLastPathComponent()
22 .appendingPathComponent("Shared/Colors.xcassets")
23 let entries = try FileManager.default.contentsOfDirectory(atPath: root.path)
24 return Set(
25 entries
26 .filter { $0.hasSuffix(".colorset") }
27 .map { String($0.dropLast(".colorset".count)) }
28 )
29 }
30
31 func testEveryTokenHasAColorset() throws {
32 let names = try colorsetNames()
33
34 for token in ThemeToken.allCases {
35 XCTAssertTrue(
36 names.contains(token.rawValue),
37 "\(token) names '\(token.rawValue)', which is not a colorset — it would render as a placeholder"
38 )
39 }
40 }
41
42 /// The other direction: a colour added to the catalog without a token is
43 /// unreachable through the theme, so it cannot be themed and nothing says
44 /// so.
45 func testNoColorsetIsMissingAToken() throws {
46 let names = try colorsetNames()
47 let tokens = Set(ThemeToken.allCases.map(\.rawValue))
48
49 XCTAssertEqual(
50 names.subtracting(tokens),
51 [],
52 "these colorsets have no ThemeToken, so the theme cannot reach them"
53 )
54 }
55
56 // MARK: Defaults
57
58 /// The default theme has to render exactly as the asset catalog did, or the
59 /// refactor changed the app's appearance rather than only its plumbing.
60 func testTheDefaultThemeResolvesEveryTokenFromTheCatalog() {
61 let theme = Theme.default
62
63 for token in ThemeToken.allCases {
64 XCTAssertEqual(theme[token], token.assetColor, "\(token) should come straight from the catalog")
65 }
66 }
67
68 func testAnEmptyOverrideSetIsTheDefault() {
69 XCTAssertEqual(Theme(overrides: [:]), Theme.default)
70 }
71
72 // MARK: Accents
73
74 func testTheDefaultAccentOverridesNothing() {
75 XCTAssertEqual(AccentTheme.default.theme, Theme.default)
76 }
77
78 /// An accent changes the accent and nothing else. Letting it reach the
79 /// status colours would undo the contrast work phases 1–5 of #21 did.
80 func testAnAccentOverridesOnlyTheAccentTokens() {
81 for accent in AccentTheme.allCases where accent != .default {
82 let theme = accent.theme
83
84 XCTAssertNotEqual(theme[.accent], ThemeToken.accent.assetColor, "\(accent) should change the accent")
85
86 for token in ThemeToken.allCases where token != .accent && token != .onAccent {
87 XCTAssertEqual(
88 theme[token],
89 token.assetColor,
90 "\(accent) must leave \(token) alone"
91 )
92 }
93 }
94 }
95
96 func testEveryAccentHasATitle() {
97 for accent in AccentTheme.allCases {
98 XCTAssertFalse(accent.title.isEmpty)
99 XCTAssertEqual(AccentTheme(rawValue: accent.rawValue), accent, "the stored value round-trips")
100 }
101 }
102
103 /// Accent themes are Pro+ per D1. Pro+ takes `allCases`, so the check that
104 /// matters is that the capability did not land in free or pro.
105 func testAccentThemesArePrOPlusOnly() {
106 XCTAssertFalse(FeatureAccessService.entitlements(for: .free).capabilities.contains(.accentTheme))
107 XCTAssertFalse(FeatureAccessService.entitlements(for: .pro).capabilities.contains(.accentTheme))
108 XCTAssertTrue(FeatureAccessService.entitlements(for: .proPlus).capabilities.contains(.accentTheme))
109 XCTAssertEqual(FeatureAccessService.upgradeMessage(for: .accentTheme), "Available in Pro+")
110 }
111
112 // MARK: App icons
113
114 /// The alternate icon names are strings matched against the asset catalog
115 /// at runtime, so a typo fails the same silent way a wrong colorset name
116 /// would — `setAlternateIconName` just reports an error and the icon stays
117 /// put. This is the parity check for them.
118 func testEveryAccentsIconNameHasAnIconSet() throws {
119 let catalog = URL(fileURLWithPath: #filePath)
120 .deletingLastPathComponent()
121 .deletingLastPathComponent()
122 .appendingPathComponent("DomainDig/Assets.xcassets")
123 let sets = Set(
124 try FileManager.default.contentsOfDirectory(atPath: catalog.path)
125 .filter { $0.hasSuffix(".appiconset") }
126 .map { String($0.dropLast(".appiconset".count)) }
127 )
128
129 for accent in AccentTheme.allCases {
130 guard let name = accent.alternateIconName else { continue }
131 XCTAssertTrue(sets.contains(name), "\(accent) names '\(name)', which is not an icon set")
132 }
133
134 // And the other direction: an icon set nothing selects is dead weight
135 // in the bundle.
136 let named = Set(AccentTheme.allCases.compactMap(\.alternateIconName)).union(["AppIcon"])
137 XCTAssertEqual(sets.subtracting(named), [], "these icon sets are unreachable")
138 }
139
140 /// The default accent uses the primary icon, which is what `nil` means to
141 /// `setAlternateIconName`.
142 func testTheDefaultAccentUsesThePrimaryIcon() {
143 XCTAssertNil(AccentTheme.default.alternateIconName)
144 for accent in AccentTheme.allCases where accent != .default {
145 XCTAssertNotNil(accent.alternateIconName, "\(accent) should have its own icon")
146 }
147 }
148
149 // MARK: Badge models carry tokens
150
151 /// The badges are built outside a view body, which is why their colours are
152 /// tokens rather than resolved values.
153 func testStatusBadgesNameTokensRatherThanColors() {
154 let registered = AppStatusFactory.availability(.registered)
155 XCTAssertFalse(registered.title.isEmpty)
156
157 // A token, so the theme resolves it at draw time.
158 XCTAssertEqual(Theme.default[registered.foreground], registered.foreground.assetColor)
159 XCTAssertEqual(Theme.default[registered.background], registered.background.assetColor)
160 }
161
162 func testEveryToneMapsToATokenPair() {
163 for tone in [AppStatusTone.positive, .warning, .critical, .info, .neutral] {
164 XCTAssertNotEqual(tone.foreground, tone.surface, "\(tone) should not use one colour for both")
165 }
166 }
167}