// The keyboard is dismissed once, for the whole app. // // The decision half is a pure function; the sweep half holds that no screen // writes its own dismissal, so the reported "sometimes it does not dismiss" // cannot come back one screen at a time. Every fixture is synthetic. import XCTest @testable import CottonUI final class KeyboardDismissalTests: XCTestCase { private let bar = CGRect(x: 0, y: 700, width: 400, height: 80) func testATapOutsideEverythingDismisses() { XCTAssertTrue(KeyboardDismissal.shouldDismiss(at: CGPoint(x: 10, y: 300), hitIsTextInput: false, persistent: [bar])) } func testATapIntoAFieldIsNotATapOutOfOne() { XCTAssertFalse(KeyboardDismissal.shouldDismiss(at: CGPoint(x: 10, y: 300), hitIsTextInput: true, persistent: [])) } func testATapOnTheComposerBarKeepsTheKeyboard() { XCTAssertFalse(KeyboardDismissal.shouldDismiss(at: CGPoint(x: 380, y: 740), hitIsTextInput: false, persistent: [bar])) // The edge is inside; a point just above it is outside. XCTAssertFalse(KeyboardDismissal.shouldDismiss(at: CGPoint(x: 0, y: 700), hitIsTextInput: false, persistent: [bar])) XCTAssertTrue(KeyboardDismissal.shouldDismiss(at: CGPoint(x: 0, y: 699), hitIsTextInput: false, persistent: [bar])) } func testAButtonUnderTheFingerIsNotProtectedByItself() { // A button outside any persistent frame still dismisses — the gesture // is simultaneous, so the button fires too; nothing is stolen. XCTAssertTrue(KeyboardDismissal.shouldDismiss(at: CGPoint(x: 200, y: 100), hitIsTextInput: false, persistent: [bar])) } /// RULE: Dragging a list DOWN puts the keyboard away, on every screen (architect 2045 sev 3: a downward drag left it up, only a tap outside folded it); a sideways or upward drag, or a nudge, does not — reading on while typing keeps it. func testADownwardDragDismissesAndNothingElseDoes() { XCTAssertTrue(KeyboardDismissal.dragDismisses(CGSize(width: 0, height: 60))) XCTAssertTrue(KeyboardDismissal.dragDismisses(CGSize(width: 20, height: 60))) XCTAssertFalse(KeyboardDismissal.dragDismisses(CGSize(width: 0, height: -120)), "scrolling on to read") XCTAssertFalse(KeyboardDismissal.dragDismisses(CGSize(width: 0, height: 20)), "a nudge") XCTAssertFalse(KeyboardDismissal.dragDismisses(CGSize(width: 90, height: 60)), "a sideways swipe") } } /// No screen dismisses the keyboard for itself: the frame does it once. final class KeyboardDismissalSweepTests: XCTestCase { private static let theRule = "CottonUI/Chrome/KeyboardDismissal.swift" private static let theFrame = "CottonUI/Chrome/ScreenFrame.swift" func testNoScreenResignsTheKeyboardForItself() throws { var offenders: [String] = [] // The frame wears the rule; every other file is a screen. A screen's // own `.interactively` is redundant now and swept too, so the next // per-screen keyboard rule cannot start as a harmless duplicate. // `.never` (the chips, the city picker) and `.immediately` (Home, // Find) are deliberate choices a screen may still make for itself. let patterns = [ "resignFirstResponder", "endEditing(", "dismissesKeyboard()", ".scrollDismissesKeyboard(.interactively)", ] for (name, text) in try AppSources.all() where name != Self.theRule { // A text storage's `endEditing()` closes its own batch of edits // and takes no argument; a view's `endEditing(_:)` resigns the // keyboard and always takes one, so only that one is swept. let code = SourceSweep.withoutComments(text).replacingOccurrences(of: "endEditing()", with: "") for pattern in patterns where code.contains(pattern) { if name == Self.theFrame, pattern == "dismissesKeyboard()" { continue } offenders.append("\(name): \(pattern)") } } XCTAssertEqual(offenders, [], "a screen dismissing the keyboard for itself: " + offenders.joined(separator: ", ") + " — the frame does it once (Chrome/KeyboardDismissal.swift)") } }