#if os(iOS) import Foundation import RastrilloApple import RastrilloTestSupport import SwiftUI import UIKit import XCTest @testable import DocsKit /// The iPhone document (S3, spec 2026-11-22 §3, §11), in the real /// `DocsWorkspace` on an in-memory server, in a window. @MainActor final class PhoneDocumentTests: XCTestCase { private var window: UIWindow? override func tearDown() async throws { window?.rootViewController = nil; window?.isHidden = true; window = nil } private func open() async throws -> (DocsTextView, DocumentSurface, LoopServer) { DocsFonts.register() let server = LoopServer() try await server.seed(LoopFixture.everything) let connectivity = NativeConnectivity(source: ScriptedPathSource(.satisfied)) let workspace = DocsWorkspace(client: NativeHTTP(origin: server.origin), transport: { server }, signOut: {}, documentID: "doc", team: "Tito", connectivity: connectivity) let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 402, height: 874)) if let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene { window.windowScene = scene } window.rootViewController = UIHostingController(rootView: workspace) window.makeKeyAndVisible() self.window = window func find(_ view: UIView) -> DocsTextView? { (view as? DocsTextView) ?? view.subviews.lazy.compactMap(find).first } try await until("the document is shown") { find(window).map { $0.textStorage.string.contains(LoopFixture.middle) } ?? false } let text = try XCTUnwrap(find(window)) let surface = try XCTUnwrap(text.delegate as? DocumentSurface) try await until("open settled") { surface.newest.map { $0.revision == surface.appliedRevision } ?? false } try await Task.sleep(for: .milliseconds(300)) return (text, surface, server) } private func until(_ what: String, _ condition: () -> Bool) async throws { let deadline = Date().addingTimeInterval(10) while !condition() { guard Date() < deadline else { throw Failure(what: what) } try await Task.sleep(for: .milliseconds(10)) } } private struct Failure: Error, CustomStringConvertible { let what: String; var description: String { "timed out: \(what)" } } /// Spec §11 (Low 18): with the caret mid-paragraph, a peer's edit to /// another block moves neither the caret nor the scroll, so the /// VoiceOver cursor, which follows them, stays where it was. func testAPeerEditElsewhereMovesNeitherTheCaretNorTheScroll() async throws { let (text, surface, server) = try await open() let content = text.textStorage.string as NSString let middle = content.range(of: LoopFixture.middle) let caret = NSRange(location: middle.location + 10, length: 0) text.selectedRange = caret _ = text.becomeFirstResponder() try await Task.sleep(for: .milliseconds(200)) let offset = text.contentOffset // The peer types into the last block, after the caret's. let after = (text.textStorage.string as NSString).range(of: LoopFixture.afterRule).location try await server.peerInsert("A peer’s note. ", at: after) try await until("the peer's note is drawn") { surface.content.string.contains("A peer’s note.") } try await Task.sleep(for: .milliseconds(200)) XCTAssertEqual(text.selectedRange, caret, "the caret did not move") XCTAssertEqual(text.contentOffset.y, offset.y, accuracy: 0.5, "nor did the scroll") } /// The hosted chrome lays itself out without the window's safe area /// (Fable on #180): the large title scrolls under the bar inside the /// text view, and the keyboard bar sits on the bottom edge over the home /// indicator when a hardware keyboard is connected. Neither is to be /// padded by insets that belong to the screen. func testTheHostedChromeIgnoresTheSafeArea() async throws { let (text, _, _) = try await open() let header = try XCTUnwrap(text.header) XCTAssertEqual(header.controller.safeAreaRegions, [], "the large title") let bar = try XCTUnwrap(text.inputAccessoryView) let host = try XCTUnwrap(DocsPhoneEditorChrome.host(of: bar)) XCTAssertEqual(host.safeAreaRegions, [], "the keyboard bar") } /// A text view the surface attaches while the Format sheet is open (the /// editor built again: a new `editorIdentity`) gets the sheet's state: /// no keyboard, and the text clear of the sheet (Fable on #180). func testANewTextViewUnderTheSheetKeepsItsState() { let surface = DocumentSurface() surface.isEditable = true let first = DocsTextView(frame: CGRect(x: 0, y: 0, width: 402, height: 874)) surface.attach(first) surface.suppressKeyboard(true) surface.sheetCovers(300) let second = DocsTextView(frame: CGRect(x: 0, y: 0, width: 402, height: 874)) surface.attach(second) XCTAssertTrue(second.keyboardSuppressed, "the keyboard stays away under the sheet") XCTAssertEqual(second.bottomObstruction, 300, "and the text clear of it") surface.suppressKeyboard(false) surface.sheetCovers(0) let third = DocsTextView(frame: CGRect(x: 0, y: 0, width: 402, height: 874)) surface.attach(third) XCTAssertFalse(third.keyboardSuppressed) XCTAssertEqual(third.bottomObstruction, 0) } /// Spec §3, "Header and scrolling": the title folds into the bar on the /// keyboard and with the Format sheet, and unfolds only at the top with /// the keyboard down and the sheet closed. func testTheTitleFoldsOnTheKeyboardAndTheSheet() async throws { let (text, _, _) = try await open() let state = try XCTUnwrap(text.header?.state) XCTAssertFalse(state.collapsed, "at the top, the large title") XCTAssertTrue(text.becomeFirstResponder()) try await until("the keyboard folds the title") { state.collapsed } XCTAssertTrue(text.resignFirstResponder()) try await until("the keyboard down at the top unfolds it") { !state.collapsed } state.sheetOpen = true XCTAssertTrue(state.collapsed, "the sheet folds it") state.sheetOpen = false XCTAssertFalse(state.collapsed) } /// The keyboard bar is a writer's, rides on the text view as its input /// accessory (so a hardware keyboard shows it too, at the bottom), and /// goes while the Format sheet is open, when the keyboard goes but the /// text view keeps first responder and its selection. func testTheKeyboardBarAndTheFormatSheetsKeyboard() async throws { let (text, surface, _) = try await open() XCTAssertNotNil(text.inputAccessoryView, "a writer's text view carries the keyboard bar") text.selectedRange = NSRange(location: 3, length: 5) XCTAssertTrue(text.becomeFirstResponder()) surface.suppressKeyboard(true) XCTAssertTrue(text.isFirstResponder, "the Format sheet keeps first responder") XCTAssertEqual(text.selectedRange, NSRange(location: 3, length: 5), "and the selection") XCTAssertNotNil(text.inputView, "the keyboard is an empty input view meanwhile") XCTAssertNil(text.inputAccessoryView, "and no keyboard bar under the sheet") surface.suppressKeyboard(false) XCTAssertNil(text.inputView, "the keyboard comes back as it was") XCTAssertNotNil(text.inputAccessoryView) surface.isEditable = false XCTAssertNil(text.inputAccessoryView, "a reader's text view has none") } } #endif