Commit ae49cdd2d8
Unsigned
Layout: unified · split
Docs/ACCESSIBILITY.txt +42 −4
| @@ -33,6 +33,21 @@ worst of those three is shown: | ||
| 33 | 33 | Each status foreground has a matching `…Surface` colour for the fill behind it, |
| 34 | 34 | paired through `AppStatusTone`. |
| 35 | 35 | |
| 36 | **That table is enforced, not just recorded.** `PaletteContrastTests` reads the | |
| 37 | colorsets, computes WCAG relative luminance, and asserts every status | |
| 38 | foreground clears 4.5:1 on the page, on a card, and on its own badge fill, in | |
| 39 | light and dark — the three surfaces `AppStatusBadgeView` can draw it on. It | |
| 40 | covers `AppTextSecondary` on all three surfaces too, and `AppOnAccent` on | |
| 41 | `StatusInfo`, which is the selected Dashboard chip. Until it existed, the | |
| 42 | harness called this palette "the actual guard against contrast regressions" | |
| 43 | while nothing checked it. | |
| 44 | ||
| 45 | One pairing is deliberately not asserted. `AppOnAccent` over `AccentFill` | |
| 46 | measures **2.78:1** in dark — black on `#0040DD` — but nothing draws it: | |
| 47 | `accentFill` goes to `.tint`, where iOS picks its own label colour, and the | |
| 48 | accent themes override `.onAccent` to white against their own fill. The number | |
| 49 | is noted in the test so anything that starts drawing that pair has it to hand. | |
| 50 | ||
| 36 | 51 | ### Contrast alone is not a palette |
| 37 | 52 | |
| 38 | 53 | The first version of this palette maximised contrast and produced mud. Requiring |
| @@ -362,8 +377,8 @@ the Settings table above overstate what is left: | ||
| 362 | 377 | | --- | --- | --- | |
| 363 | 378 | | `dynamicType` | 21 | Not actionable — position-dependent, per the above | |
| 364 | 379 | | `textClipped` | 15 | 7 fixed by `AdaptivePicker`; 4 are long navigation titles; **4 remain** | |
| 365 | | `contrast` | 6 | Report-only by standing policy | | |
| 366 | | `elementDetection` | 1 | Not investigated | | |
| 380 | | `contrast` | 6 | Report-only; the palette is tested instead | | |
| 381 | | `elementDetection` | 1 | Unattributed — see below | | |
| 367 | 382 | |
| 368 | 383 | The actionable remainder was four clipped picker values — "Compact", "Default" |
| 369 | 384 | and "System" on Display, and "Merge" on Import & Export — the same construct |
| @@ -383,8 +398,8 @@ That leaves, across the ten Settings destinations: | ||
| 383 | 398 | | --- | --- | --- | |
| 384 | 399 | | 4 clipped navigation titles | History & Network, Scheduled Reports, Import & Export, Data Management | Only a shorter screen name fixes it | |
| 385 | 400 | | 21 `dynamicType` | all | Position-dependent, per the above | |
| 386 | | 6 `contrast` | all | Report-only by standing policy | | |
| 387 | | 1 `elementDetection` | Local API | Not investigated | | |
| 401 | | 6 `contrast` | all | Report-only; the palette is tested instead | | |
| 402 | | 1 `elementDetection` | Local API | Unattributed — see below | | |
| 388 | 403 | |
| 389 | 404 | `AdaptivePicker` also reached the integration editor's three pickers — Type, |
| 390 | 405 | Minimum Severity and SMTP Security. Its clipped findings go from four to one: |
| @@ -421,6 +436,29 @@ artifact and stay reported — those screens do not gate, so there is nothing to | ||
| 421 | 436 | unblock, and a suppression wide enough to cover arbitrary `Form` rows would |
| 422 | 437 | hide real regressions. |
| 423 | 438 | |
| 439 | ## `contrast` and `elementDetection`, the last two categories | |
| 440 | ||
| 441 | Neither is a to-do list either, for different reasons. | |
| 442 | ||
| 443 | **Every `elementDetection` finding is unattributed.** Across both runtimes they | |
| 444 | report against "unknown element" — on `seeded-dashboard`, | |
| 445 | `seeded-tracked-domains` and `monitoring-log`, and nowhere else. All three are | |
| 446 | report-only screens, so none of them gates anything, and a finding that names no | |
| 447 | element cannot be acted on. No carve-out was added: there is nothing to unblock, | |
| 448 | and a rule wide enough to cover them would weaken an enforced category | |
| 449 | everywhere else. | |
| 450 | ||
| 451 | **The `contrast` attribution differs so much between runtimes that the names | |
| 452 | cannot be worked through.** On `seeded-tracked-domains` the floor names 25 | |
| 453 | elements and the current runtime names 5, largely a different set; on | |
| 454 | `seeded-dashboard` the floor reports 4 unattributed findings and the current | |
| 455 | runtime names 17. Same build, same fixtures. That is the flip the `.contrast` | |
| 456 | carve-out was written for, at a larger scale. | |
| 457 | ||
| 458 | What was done instead is the thing the carve-out always pointed at: the palette | |
| 459 | is tested, so a colour that stops clearing AA fails the unit suite in seconds | |
| 460 | rather than appearing as one more unstable name in an audit list. | |
| 461 | ||
| 424 | 462 | ## Long navigation titles clip |
| 425 | 463 | |
| 426 | 464 | `textClipped` is reported against the navigation title of exactly the screens |
DomainDigTests/PaletteContrastTests.swift added +170
| @@ -0,0 +1,170 @@ | ||
| 1 | import XCTest | |
| 2 | @testable import DomainDig | |
| 3 | ||
| 4 | /// The measured ratios in `Docs/ACCESSIBILITY.txt` were prose, while | |
| 5 | /// `AccessibilityAuditHarness` names the centralised palette as "the actual | |
| 6 | /// guard against contrast regressions" — the reason `.contrast` is allowed to | |
| 7 | /// stay report-only in the audit. Nothing checked the palette, so the guard was | |
| 8 | /// a claim. This is the check. | |
| 9 | /// | |
| 10 | /// Read from the colorsets rather than resolved `UIColor`s, for the reason | |
| 11 | /// `ThemeTests` gives: the catalog is compiled into the app bundle, and the | |
| 12 | /// source tree is the palette's definition. | |
| 13 | final class PaletteContrastTests: XCTestCase { | |
| 14 | ||
| 15 | /// WCAG AA for body text. | |
| 16 | private let minimumRatio = 4.5 | |
| 17 | ||
| 18 | /// Each status foreground and the surfaces it is drawn on: the page, a | |
| 19 | /// card, and its own badge fill — `AppStatusBadgeView` draws | |
| 20 | /// `theme[foreground]` over `theme[background]`, paired by `AppStatusTone`. | |
| 21 | private let pairings: [(foreground: String, badge: String)] = [ | |
| 22 | ("StatusInfo", "StatusInfoSurface"), | |
| 23 | ("StatusPositive", "StatusPositiveSurface"), | |
| 24 | ("StatusWarning", "StatusWarningSurface"), | |
| 25 | ("StatusCritical", "StatusCriticalSurface"), | |
| 26 | ("StatusNeutral", "StatusNeutralSurface") | |
| 27 | ] | |
| 28 | ||
| 29 | func testEveryStatusForegroundClearsAAOnEverySurfaceItIsDrawnOn() throws { | |
| 30 | for appearance in Appearance.allCases { | |
| 31 | let background = try components("AppBackground", appearance) | |
| 32 | let surface = try components("AppSurface", appearance) | |
| 33 | ||
| 34 | for pairing in pairings { | |
| 35 | let foreground = try components(pairing.foreground, appearance) | |
| 36 | let badge = try components(pairing.badge, appearance) | |
| 37 | ||
| 38 | for (name, against) in [("page", background), ("card", surface), ("badge", badge)] { | |
| 39 | let ratio = contrastRatio(foreground, against) | |
| 40 | XCTAssertGreaterThanOrEqual( | |
| 41 | ratio, | |
| 42 | minimumRatio, | |
| 43 | "\(pairing.foreground) on its \(name) in \(appearance) is \(rounded(ratio)):1" | |
| 44 | ) | |
| 45 | } | |
| 46 | } | |
| 47 | } | |
| 48 | } | |
| 49 | ||
| 50 | /// `AppTextSecondary` exists because iOS's own `secondaryLabel` is 3.29:1 | |
| 51 | /// on a light card. A regression here would reintroduce that across the | |
| 52 | /// body text it replaced. | |
| 53 | func testSecondaryTextClearsAAOnThePageAndOnACard() throws { | |
| 54 | for appearance in Appearance.allCases { | |
| 55 | let secondary = try components("AppTextSecondary", appearance) | |
| 56 | ||
| 57 | for name in ["AppBackground", "AppSurface", "AppSurfaceElevated"] { | |
| 58 | let ratio = try contrastRatio(secondary, components(name, appearance)) | |
| 59 | XCTAssertGreaterThanOrEqual( | |
| 60 | ratio, | |
| 61 | minimumRatio, | |
| 62 | "AppTextSecondary on \(name) in \(appearance) is \(rounded(ratio)):1" | |
| 63 | ) | |
| 64 | } | |
| 65 | } | |
| 66 | } | |
| 67 | ||
| 68 | /// `AppOnAccent` is drawn over `StatusInfo`, by the selected Dashboard | |
| 69 | /// filter chip — the only site that uses it. It is deliberately white in | |
| 70 | /// light and black in dark, because `StatusInfo` goes from `#0000FF` to | |
| 71 | /// `#4DA3FF` and the readable foreground flips with it. | |
| 72 | /// | |
| 73 | /// Not asserted against `AccentFill`, which nothing pairs it with: | |
| 74 | /// `accentFill` is passed to `.tint`, where iOS picks its own label colour. | |
| 75 | /// Were anything to start drawing that pair, note that black on the dark | |
| 76 | /// `AccentFill` (`#0040DD`) measures 2.78:1 and would need white instead. | |
| 77 | func testTextOnTheSelectedChipClearsAA() throws { | |
| 78 | for appearance in Appearance.allCases { | |
| 79 | let ratio = try contrastRatio( | |
| 80 | components("AppOnAccent", appearance), | |
| 81 | components("StatusInfo", appearance) | |
| 82 | ) | |
| 83 | XCTAssertGreaterThanOrEqual( | |
| 84 | ratio, | |
| 85 | minimumRatio, | |
| 86 | "AppOnAccent on StatusInfo in \(appearance) is \(rounded(ratio)):1" | |
| 87 | ) | |
| 88 | } | |
| 89 | } | |
| 90 | ||
| 91 | // MARK: Colorsets | |
| 92 | ||
| 93 | private enum Appearance: String, CaseIterable { | |
| 94 | case light | |
| 95 | case dark | |
| 96 | } | |
| 97 | ||
| 98 | private struct RGB { | |
| 99 | let red: Double | |
| 100 | let green: Double | |
| 101 | let blue: Double | |
| 102 | } | |
| 103 | ||
| 104 | /// The sRGB components a colorset defines for one appearance. | |
| 105 | /// | |
| 106 | /// The light entry is the one with no `appearances` key; the dark entry | |
| 107 | /// carries `luminosity: dark`. High-contrast variants are skipped: they | |
| 108 | /// only ever push further in the same direction, so the base values are the | |
| 109 | /// worst case. | |
| 110 | private func components(_ name: String, _ appearance: Appearance) throws -> RGB { | |
| 111 | let url = URL(fileURLWithPath: #filePath) | |
| 112 | .deletingLastPathComponent() | |
| 113 | .deletingLastPathComponent() | |
| 114 | .appendingPathComponent("Shared/Colors.xcassets/\(name).colorset/Contents.json") | |
| 115 | let data = try Data(contentsOf: url) | |
| 116 | let parsed = try JSONSerialization.jsonObject(with: data) as? [String: Any] | |
| 117 | let colors = parsed?["colors"] as? [[String: Any]] ?? [] | |
| 118 | ||
| 119 | let entry = colors.first { entry in | |
| 120 | let appearances = entry["appearances"] as? [[String: Any]] ?? [] | |
| 121 | if appearances.contains(where: { ($0["appearance"] as? String) == "contrast" }) { | |
| 122 | return false | |
| 123 | } | |
| 124 | let isDark = appearances.contains { $0["appearance"] as? String == "luminosity" && $0["value"] as? String == "dark" } | |
| 125 | return appearance == .dark ? isDark : appearances.isEmpty | |
| 126 | } | |
| 127 | ||
| 128 | let raw = try XCTUnwrap( | |
| 129 | (entry?["color"] as? [String: Any])?["components"] as? [String: String], | |
| 130 | "\(name) has no \(appearance) components" | |
| 131 | ) | |
| 132 | return RGB( | |
| 133 | red: try channel(raw["red"], in: name), | |
| 134 | green: try channel(raw["green"], in: name), | |
| 135 | blue: try channel(raw["blue"], in: name) | |
| 136 | ) | |
| 137 | } | |
| 138 | ||
| 139 | /// Xcode writes these as `0xAD`, but a catalog edited by hand can hold a | |
| 140 | /// decimal or a float, so all three are accepted and anything else fails | |
| 141 | /// loudly rather than silently measuring black. | |
| 142 | private func channel(_ raw: String?, in name: String) throws -> Double { | |
| 143 | let value = try XCTUnwrap(raw, "\(name) is missing a channel") | |
| 144 | if value.hasPrefix("0x") { | |
| 145 | return Double(try XCTUnwrap(UInt8(value.dropFirst(2), radix: 16), "\(name): bad hex '\(value)'")) / 255 | |
| 146 | } | |
| 147 | let number = try XCTUnwrap(Double(value), "\(name): bad component '\(value)'") | |
| 148 | return number > 1 ? number / 255 : number | |
| 149 | } | |
| 150 | ||
| 151 | // MARK: WCAG | |
| 152 | ||
| 153 | private func contrastRatio(_ lhs: RGB, _ rhs: RGB) -> Double { | |
| 154 | let a = relativeLuminance(lhs) | |
| 155 | let b = relativeLuminance(rhs) | |
| 156 | return (max(a, b) + 0.05) / (min(a, b) + 0.05) | |
| 157 | } | |
| 158 | ||
| 159 | /// WCAG 2.1 relative luminance. | |
| 160 | private func relativeLuminance(_ rgb: RGB) -> Double { | |
| 161 | func linear(_ channel: Double) -> Double { | |
| 162 | channel <= 0.03928 ? channel / 12.92 : pow((channel + 0.055) / 1.055, 2.4) | |
| 163 | } | |
| 164 | return 0.2126 * linear(rgb.red) + 0.7152 * linear(rgb.green) + 0.0722 * linear(rgb.blue) | |
| 165 | } | |
| 166 | ||
| 167 | private func rounded(_ value: Double) -> String { | |
| 168 | String(format: "%.2f", value) | |
| 169 | } | |
| 170 | } | |