| 1 | import XCTest |
| 2 | import UIKit |
| 3 | final class NativeUITests: XCTestCase { |
| 4 | @MainActor func testInputRuleReversalAndMultilinePersistence() async throws { |
| 5 | continueAfterFailure = false |
| 6 | let app = XCUIApplication() |
| 7 | app.launch() |
| 8 | defer { app.terminate() } |
| 9 | let create = app.buttons["docs.create"] |
| 10 | XCTAssertTrue(create.waitForExistence(timeout: 15)) |
| 11 | create.tap() |
| 12 | let title = app.textFields["Title"] |
| 13 | XCTAssertTrue(title.waitForExistence(timeout: 5)) |
| 14 | title.tap(); title.typeText("Native input rule verification") |
| 15 | app.buttons["docs.create.submit"].tap() |
| 16 | let editor = app.textViews["docs.editor"] |
| 17 | XCTAssertTrue(editor.waitForExistence(timeout: 15)) |
| 18 | editor.tap(); editor.typeText("- ") |
| 19 | let transformed = editor.value as? String ?? "" |
| 20 | XCTAssertFalse(transformed.contains("-"), "The native input rule must remove its typed prefix") |
| 21 | editor.typeText(XCUIKeyboardKey.delete.rawValue) |
| 22 | let reverted = editor.value as? String ?? "" |
| 23 | print("Synthetic rule transformed: \(String(reflecting: transformed)); reverted: \(String(reflecting: reverted))") |
| 24 | let reversal = XCTAttachment(string: "Synthetic transformed value: \(String(reflecting: transformed))\nSynthetic reverted value: \(String(reflecting: reverted))") |
| 25 | reversal.name = "Native input rule reversal"; reversal.lifetime = .keepAlways; add(reversal) |
| 26 | XCTAssertTrue(reverted.contains("- "), "Backspace must restore the complete input-rule prefix") |
| 27 | editor.typeText("Alpha\nBeta") |
| 28 | let displayed = editor.value as? String ?? "" |
| 29 | XCTAssertTrue(displayed.contains("Alpha")) |
| 30 | XCTAssertTrue(displayed.contains("Beta")) |
| 31 | struct Config: Decodable { struct Credential: Decodable { let name: String; let value: String }; let origin: String; let credential: Credential } |
| 32 | let config = try JSONDecoder().decode(Config.self, from: Data(contentsOf: URL.documentsDirectory.appendingPathComponent("review.json"))) |
| 33 | func fetch(_ path: String) async throws -> Data { |
| 34 | var request = URLRequest(url: URL(string: config.origin + path)!) |
| 35 | request.setValue("\(config.credential.name)=\(config.credential.value)", forHTTPHeaderField: "Cookie") |
| 36 | request.setValue("application/json", forHTTPHeaderField: "Accept") |
| 37 | let (data, response) = try await URLSession.shared.data(for: request) |
| 38 | XCTAssertEqual((response as? HTTPURLResponse)?.statusCode, 200) |
| 39 | return data |
| 40 | } |
| 41 | struct Listed: Decodable { struct Item: Decodable { let sqid: String; let title: String }; let documents: [Item] } |
| 42 | let list = try JSONDecoder().decode(Listed.self, from: await fetch("/")) |
| 43 | let item = try XCTUnwrap(list.documents.first { $0.title == "Native input rule verification" }) |
| 44 | var saved = "" |
| 45 | for _ in 0..<50 { |
| 46 | saved = String(decoding: try await fetch("/d/\(item.sqid)/export.md"), as: UTF8.self) |
| 47 | if saved.contains("Alpha") && saved.contains("Beta") { break } |
| 48 | try await Task.sleep(for: .milliseconds(100)) |
| 49 | } |
| 50 | let attachment = XCTAttachment(string: "Synthetic Markdown: \(String(reflecting: saved))\nSynthetic editor value: \(String(reflecting: displayed))") |
| 51 | attachment.name = "Native multiline persistence"; attachment.lifetime = .keepAlways; add(attachment) |
| 52 | let prefix = "# Native input rule verification\n\n" |
| 53 | XCTAssertTrue(saved.hasPrefix(prefix)) |
| 54 | let body = String(saved.dropFirst(prefix.count)).replacingOccurrences(of: "\\-", with: "-") |
| 55 | print("Synthetic rule persisted Markdown: \(String(reflecting: saved)); displayed: \(String(reflecting: displayed))") |
| 56 | XCTAssertEqual(body.split(whereSeparator: { $0.isWhitespace }).joined(separator: " "), "- Alpha Beta", "Reversal and multiline typing must preserve every character in the real CRDT store") |
| 57 | let screenshot = XCTAttachment(screenshot: app.screenshot()) |
| 58 | screenshot.name = "Docs native input rule and multiline editor"; screenshot.lifetime = .keepAlways; add(screenshot) |
| 59 | } |
| 60 | |
| 61 | @MainActor func testKeyboardJumpSearchOpensDocument() throws { |
| 62 | // Apple's XCUIElement overview supports keyboard injection on iPadOS/macOS, |
| 63 | // while iPhone automation supports gestures. On iPhone this inserts literal K. |
| 64 | try XCTSkipUnless(UIDevice.current.userInterfaceIdiom == .pad, |
| 65 | "XCTest hardware keyboard injection requires iPadOS; use the jump-button case on iPhone") |
| 66 | try exerciseJump(keyboard: true) |
| 67 | } |
| 68 | @MainActor func testJumpButtonSearchOpensDocument() throws { try exerciseJump(keyboard: false) } |
| 69 | @MainActor func testJumpButtonAndHardwareReturnOpensDocument() throws { |
| 70 | try XCTSkipUnless(UIDevice.current.userInterfaceIdiom == .pad, |
| 71 | "XCTest hardware keyboard injection requires iPadOS") |
| 72 | try exerciseJump(keyboard: false, hardwareReturn: true) |
| 73 | } |
| 74 | @MainActor func testRegisteredCommandAndSoftwareSubmitOpenDocument() throws { |
| 75 | try exerciseJump(keyboard: false, responderProbe: true) |
| 76 | } |
| 77 | @MainActor private func exerciseJump(keyboard: Bool, hardwareReturn: Bool = false, responderProbe: Bool = false) throws { |
| 78 | continueAfterFailure = false |
| 79 | let app = XCUIApplication() |
| 80 | if responderProbe { app.launchEnvironment["DOCS_REVIEW_COMMAND_PROBE"] = "1" } |
| 81 | app.launch() |
| 82 | defer { app.terminate() } |
| 83 | let first = app.buttons["Native review document"].firstMatch |
| 84 | XCTAssertTrue(first.waitForExistence(timeout: 15)) |
| 85 | first.tap() |
| 86 | let editor = app.textViews["docs.editor"] |
| 87 | XCTAssertTrue(editor.waitForExistence(timeout: 15)) |
| 88 | editor.tap() |
| 89 | if responderProbe { /* Review target dispatches the registered action after focus. */ } |
| 90 | else if keyboard { editor.typeKey("k", modifierFlags: .command) } |
| 91 | else { app.buttons["docs.jump"].firstMatch.tap() } |
| 92 | print("Synthetic jump shortcut hierarchy: \(app.debugDescription)") |
| 93 | try app.screenshot().pngRepresentation.write(to: URL.documentsDirectory.appendingPathComponent("jump-shortcut.review.png"), options: .atomic) |
| 94 | let query = app.textFields["docs.jump.query"] |
| 95 | XCTAssertTrue(query.waitForExistence(timeout: 5), "Command-K must open native document search from the editor") |
| 96 | query.typeText("Second review") |
| 97 | let result = app.buttons.containing(.staticText, identifier: "Second review document").firstMatch |
| 98 | XCTAssertTrue(result.waitForExistence(timeout: 10), "Native search must show the existing server-ranked result") |
| 99 | if responderProbe { query.typeText("\n") } |
| 100 | else if keyboard || hardwareReturn { query.typeKey(XCUIKeyboardKey.return, modifierFlags: []) } |
| 101 | else { result.tap() } |
| 102 | print("Synthetic jump return hierarchy: \(app.debugDescription)") |
| 103 | try app.screenshot().pngRepresentation.write(to: URL.documentsDirectory.appendingPathComponent("jump-return.review.png"), options: .atomic) |
| 104 | let opened = app.navigationBars["Second review document"].waitForExistence(timeout: 10) |
| 105 | if !opened { |
| 106 | print("Synthetic jump result hierarchy: \(app.debugDescription)") |
| 107 | let diagnostic = XCTAttachment(screenshot: app.screenshot()) |
| 108 | diagnostic.name = "Docs native jump failure"; diagnostic.lifetime = .keepAlways; add(diagnostic) |
| 109 | } |
| 110 | XCTAssertTrue(opened, "Enter must open the selected native search result") |
| 111 | XCTAssertTrue(editor.waitForExistence(timeout: 10)) |
| 112 | let screenshot = XCTAttachment(screenshot: app.screenshot()) |
| 113 | screenshot.name = "Docs native keyboard jump result"; screenshot.lifetime = .keepAlways; add(screenshot) |
| 114 | } |
| 115 | |
| 116 | @MainActor func testExportPDFAndMarkdownDestinationsAndCancellation() throws { |
| 117 | continueAfterFailure = false |
| 118 | let app = XCUIApplication() |
| 119 | app.launch() |
| 120 | defer { app.terminate() } |
| 121 | let document = app.buttons["Native review document"].firstMatch |
| 122 | XCTAssertTrue(document.waitForExistence(timeout: 15)) |
| 123 | document.tap() |
| 124 | XCTAssertTrue(app.textViews["docs.editor"].waitForExistence(timeout: 15)) |
| 125 | let export = app.buttons["Export and print"] |
| 126 | for format in ["PDF", "Markdown"] { |
| 127 | XCTAssertTrue(export.waitForExistence(timeout: 5)) |
| 128 | export.tap() |
| 129 | app.buttons["Download as " + format].tap() |
| 130 | let cancel = app.buttons["Cancel"] |
| 131 | XCTAssertTrue(cancel.waitForExistence(timeout: 20), "The native destination picker must open for " + format) |
| 132 | let screenshot = XCTAttachment(screenshot: app.screenshot()) |
| 133 | screenshot.name = "Docs native " + format + " export destination"; screenshot.lifetime = .keepAlways; add(screenshot) |
| 134 | let hierarchy = XCTAttachment(string: app.debugDescription) |
| 135 | hierarchy.name = "Synthetic " + format + " export picker hierarchy"; hierarchy.lifetime = .keepAlways; add(hierarchy) |
| 136 | cancel.tap() |
| 137 | XCTAssertTrue(export.waitForExistence(timeout: 10)) |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | @MainActor func testCreateTypeFormatAndPersistDocument() async throws { |
| 142 | continueAfterFailure = false |
| 143 | let app = XCUIApplication() |
| 144 | app.launch() |
| 145 | defer { app.terminate() } |
| 146 | XCTAssertFalse(app.staticTexts["fixture.missing"].exists) |
| 147 | let create = app.buttons["docs.create"] |
| 148 | guard create.waitForExistence(timeout: 10) else { XCTFail("Fixture document list did not load"); throw NSError(domain: "NativeReview", code: 1) } |
| 149 | create.tap() |
| 150 | let title = app.textFields["Title"] |
| 151 | XCTAssertTrue(title.waitForExistence(timeout: 3)) |
| 152 | title.tap(); title.typeText("Native editing verification") |
| 153 | app.buttons["docs.create.submit"].tap() |
| 154 | let editor = app.textViews["docs.editor"] |
| 155 | guard editor.waitForExistence(timeout: 10) else { XCTFail("Native editor did not open"); throw NSError(domain: "NativeReview", code: 2) } |
| 156 | editor.tap(); editor.typeText("Native text") |
| 157 | XCTAssertTrue((editor.value as? String)?.contains("Native text") == true) |
| 158 | let bold = app.buttons["Bold"].firstMatch |
| 159 | XCTAssertTrue(bold.exists) |
| 160 | bold.tap() |
| 161 | // The toolbar retains the editor's first responder. Tapping the centre |
| 162 | // of a whole-page text view here would deliberately reposition the caret. |
| 163 | editor.typeText(" bold") |
| 164 | let displayed = editor.value as? String ?? "<missing editor value>" |
| 165 | if !displayed.contains("bold") { |
| 166 | let diagnostic = XCTAttachment(string: "Synthetic editor value: \(String(reflecting: displayed))\n\(app.debugDescription)") |
| 167 | diagnostic.name = "Synthetic native input failure"; diagnostic.lifetime = .keepAlways; add(diagnostic) |
| 168 | let failedScreenshot = XCTAttachment(screenshot: app.screenshot()) |
| 169 | failedScreenshot.name = "Docs native input failure"; failedScreenshot.lifetime = .keepAlways; add(failedScreenshot) |
| 170 | } |
| 171 | XCTAssertTrue(displayed.contains("bold"), "Formatted input must remain visible: \(String(reflecting: displayed))") |
| 172 | struct Config: Decodable { struct Credential: Decodable { let name: String; let value: String }; let origin: String; let credential: Credential } |
| 173 | let config = try JSONDecoder().decode(Config.self, from: Data(contentsOf: URL.documentsDirectory.appendingPathComponent("review.json"))) |
| 174 | func fetch(_ path: String) async throws -> Data { |
| 175 | var request = URLRequest(url: URL(string: config.origin + path)!) |
| 176 | request.setValue("\(config.credential.name)=\(config.credential.value)", forHTTPHeaderField: "Cookie") |
| 177 | request.setValue("application/json", forHTTPHeaderField: "Accept") |
| 178 | let (data, response) = try await URLSession.shared.data(for: request) |
| 179 | XCTAssertEqual((response as? HTTPURLResponse)?.statusCode, 200) |
| 180 | return data |
| 181 | } |
| 182 | struct Listed: Decodable { struct Item: Decodable { let sqid: String; let title: String }; let documents: [Item] } |
| 183 | let list = try JSONDecoder().decode(Listed.self, from: await fetch("/")) |
| 184 | let item = try XCTUnwrap(list.documents.first { $0.title == "Native editing verification" }) |
| 185 | var saved = "" |
| 186 | for _ in 0..<40 { |
| 187 | saved = String(decoding: try await fetch("/d/\(item.sqid)/export.md"), as: UTF8.self) |
| 188 | if saved.contains("Native text") && saved.contains("**") && saved.contains("bold") { break } |
| 189 | try await Task.sleep(for: .milliseconds(100)) |
| 190 | } |
| 191 | let persistedDiagnostic = XCTAttachment(string: "Synthetic persisted Markdown: \(String(reflecting: saved))\nSynthetic displayed value: \(String(reflecting: editor.value as? String ?? "<missing>"))") |
| 192 | persistedDiagnostic.name = "Synthetic native persistence"; persistedDiagnostic.lifetime = .keepAlways; add(persistedDiagnostic) |
| 193 | XCTAssertTrue(saved.contains("Native text"), "Native edits must reach the real CRDT store") |
| 194 | XCTAssertTrue(saved.contains("**"), "Bold must persist as Markdown formatting, not only visible text") |
| 195 | XCTAssertTrue(saved.contains("bold"), "Formatted typing must reach the real CRDT store") |
| 196 | // Canonical Markdown export includes the document title as its heading. |
| 197 | let titlePrefix = "# Native editing verification\n\n" |
| 198 | XCTAssertTrue(saved.hasPrefix(titlePrefix)) |
| 199 | let body = String(saved.dropFirst(titlePrefix.count)) |
| 200 | XCTAssertEqual(body.replacingOccurrences(of: "**", with: "").trimmingCharacters(in: .whitespacesAndNewlines), "Native text bold", "Formatting the insertion point must preserve every character and append at the retained caret") |
| 201 | let screenshot = XCTAttachment(screenshot: app.screenshot()) |
| 202 | screenshot.name = "Docs native editor"; screenshot.lifetime = .keepAlways; add(screenshot) |
| 203 | } |
| 204 | |
| 205 | // MARK: - S3: the iPhone document (spec 2026-11-22 §3, §11; plan S3 acceptance) |
| 206 | |
| 207 | /// The accessibility audit, saying which element each issue is on. |
| 208 | @MainActor private func audit(_ app: XCUIApplication) throws { |
| 209 | // Still first: mid-presentation a sheet is scaled, and the audit |
| 210 | // reads its text as clipped. Two equal readings of the tree a |
| 211 | // quarter second apart (at most 3 s). |
| 212 | var tree = app.debugDescription |
| 213 | for _ in 0..<12 { |
| 214 | usleep(250_000) |
| 215 | let next = app.debugDescription |
| 216 | if next == tree { break } |
| 217 | tree = next |
| 218 | } |
| 219 | let keyboard = app.keyboards.firstMatch |
| 220 | let keys = keyboard.exists ? keyboard.frame : nil |
| 221 | try app.performAccessibilityAudit { issue in |
| 222 | // The system keyboard is UIKit's, not ours: its prediction cells |
| 223 | // have no description, and its keys are text the audit cannot |
| 224 | // attribute to any element (it reports none). Both appear only |
| 225 | // while the keyboard is up, and not without it. |
| 226 | if let keys, issue.element.map({ keys.intersects($0.frame) }) ?? true { return true } |
| 227 | // The subtitle is a status line with no action (spec §6: one |
| 228 | // element, updating frequently, announced by its changes): the |
| 229 | // hit-area rule is for what a person taps. Its Dynamic Type |
| 230 | // check is unreliable on it: with the same build it passed one |
| 231 | // run and failed the next, and its element comes back with no |
| 232 | // identifier. It is the caption2 style on iOS, and the AX3 |
| 233 | // snapshots (docs-phone-ax3) show it scaled. |
| 234 | let shown = self.anything(app, "docs.document.subtitle") |
| 235 | let subtitle = issue.element.map { $0.identifier == "docs.document.subtitle" || $0.identifier.isEmpty && shown.exists && $0.label == shown.label } ?? false |
| 236 | if subtitle, issue.auditType == .hitRegion || issue.auditType == .dynamicType { return true } |
| 237 | // A disabled control is dimmed on purpose (Invite before an |
| 238 | // address is typed); WCAG's contrast rule exempts inactive |
| 239 | // components. |
| 240 | if issue.auditType == .contrast, issue.element?.isEnabled == false { return true } |
| 241 | // The navigation bar caps its type at the largest sizes, as |
| 242 | // UIKit's bars do, and its title is truncated (spec §3: "title |
| 243 | // truncated"); a long press shows it whole in the Large Content |
| 244 | // Viewer. |
| 245 | let bar = app.navigationBars.firstMatch |
| 246 | if issue.auditType == .dynamicType || issue.auditType == .textClipped, |
| 247 | let element = issue.element, bar.exists, bar.frame.contains(element.frame) { return true } |
| 248 | // A sheet at the medium detent is drawn inset and scaled (by |
| 249 | // about 0.96: a 52pt chip measures 49.9pt), and the audit then |
| 250 | // reads its text as clipped. Nothing in the Format sheet |
| 251 | // truncates (its chips wrap: DocsFlowLayout); the AX3 |
| 252 | // snapshots (docs-phone-ax3, phone-format-sheet) show it whole. |
| 253 | let format = self.anything(app, "docs.format.sheet") |
| 254 | if issue.auditType == .textClipped, let element = issue.element, format.exists, format.frame.contains(element.frame) { return true } |
| 255 | // With the sheet over it, the audit samples the sheet's pixels |
| 256 | // inside the text view's frame and reports them as the text |
| 257 | // view's contrast; the page's own text is black on white. |
| 258 | if issue.auditType == .contrast, issue.element?.identifier == "docs.editor", format.exists { return true } |
| 259 | // The page's text does not scale (spec §9): it is the |
| 260 | // document's own type, and System Zoom remains. |
| 261 | if issue.auditType == .dynamicType, issue.element?.identifier == "docs.editor" { return true } |
| 262 | print("AUDIT ISSUE: \(issue.auditType) \(issue.compactDescription) — \(issue.detailedDescription) — element: \(issue.element?.identifier ?? "?") \(issue.element?.label ?? "") \(String(describing: issue.element?.frame))") |
| 263 | return false |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | private func anything(_ app: XCUIApplication, _ identifier: String) -> XCUIElement { |
| 268 | app.descendants(matching: .any).matching(identifier: identifier).firstMatch |
| 269 | } |
| 270 | |
| 271 | @MainActor private func openPhoneDocument(_ app: XCUIApplication, _ title: String) throws -> XCUIElement { |
| 272 | try XCTSkipUnless(UIDevice.current.userInterfaceIdiom == .phone, "the iPhone layout; iPad keeps today's until S5") |
| 273 | let row = app.buttons[title].firstMatch |
| 274 | XCTAssertTrue(row.waitForExistence(timeout: 15), "\(title) is listed") |
| 275 | row.tap() |
| 276 | let editor = app.textViews["docs.editor"] |
| 277 | XCTAssertTrue(editor.waitForExistence(timeout: 15), "\(title) opened") |
| 278 | return editor |
| 279 | } |
| 280 | |
| 281 | /// A writer: the bar holds undo/redo, Aa and •••; ••• holds every item; |
| 282 | /// the keyboard brings the keyboard bar; the title folds into the bar on |
| 283 | /// typing, on scroll and with the Format sheet; Aa opens the sheet with |
| 284 | /// the text view still holding the keyboard focus (its selection |
| 285 | /// visible) and the keyboard gone; and the accessibility audit passes on |
| 286 | /// the document, the sheet and the Share sheet. |
| 287 | @MainActor func testPhoneDocumentForAWriter() throws { |
| 288 | continueAfterFailure = false |
| 289 | let app = XCUIApplication() |
| 290 | app.launch() |
| 291 | defer { app.terminate() } |
| 292 | let editor = try openPhoneDocument(app, "Native review document") |
| 293 | let inline = anything(app, "docs.document.inlineTitle") |
| 294 | XCTAssertTrue(app.staticTexts["docs.document.title"].waitForExistence(timeout: 5), "the large title shows") |
| 295 | XCTAssertFalse(inline.exists, "and the bar's title does not") |
| 296 | XCTAssertTrue(app.buttons["docs.format.toggle"].exists, "Aa") |
| 297 | XCTAssertTrue(app.buttons["docs.undo"].exists && app.buttons["docs.redo"].exists, "undo and redo") |
| 298 | XCTAssertFalse(app.buttons["docs.share"].exists, "no Share pill beside the title (ruling 4)") |
| 299 | try audit(app) |
| 300 | |
| 301 | app.buttons["docs.more"].tap() |
| 302 | // A SwiftUI menu's items reach UIKit's menu without their |
| 303 | // identifiers: they are found by their (English) titles. |
| 304 | for item in ["Share…", "Rename document", "Export and print", "Word count", "Open a document", "Paged", "Infinite", "Help", "Delete…"] { |
| 305 | XCTAssertTrue(app.buttons[item].waitForExistence(timeout: 3), "••• holds \(item) for the owner") |
| 306 | } |
| 307 | app.buttons["Share…"].tap() |
| 308 | XCTAssertTrue(anything(app, "docs.share.panel").waitForExistence(timeout: 10), "Share… opens the panel as a sheet") |
| 309 | try audit(app) |
| 310 | app.buttons["docs.share.close"].tap() |
| 311 | XCTAssertTrue(app.buttons["docs.more"].waitForExistence(timeout: 5)) |
| 312 | |
| 313 | // The keyboard and its bar, writers only; typing folds the title. |
| 314 | editor.tap() |
| 315 | XCTAssertTrue(app.keyboards.firstMatch.waitForExistence(timeout: 5), "the keyboard") |
| 316 | XCTAssertTrue(app.buttons["docs.keyboard.bold"].waitForExistence(timeout: 5), "the keyboard bar comes with it") |
| 317 | try audit(app) |
| 318 | editor.typeText("x") |
| 319 | XCTAssertTrue(inline.waitForExistence(timeout: 3), "typing folds the title into the bar") |
| 320 | // Folded, undo and redo leave the bar for the top of •••. |
| 321 | XCTAssertTrue(app.buttons["docs.undo"].waitForNonExistence(timeout: 3), "no undo/redo pill while the title is folded") |
| 322 | app.buttons["docs.more"].tap() |
| 323 | XCTAssertTrue(app.buttons["Undo"].waitForExistence(timeout: 3), "••• begins with Undo") |
| 324 | XCTAssertTrue(app.buttons["Redo"].exists, "and Redo") |
| 325 | app.buttons["Undo"].tap() |
| 326 | XCTAssertTrue(app.buttons["Undo"].waitForNonExistence(timeout: 3), "Undo runs and the menu goes") |
| 327 | editor.typeText("x") |
| 328 | let typed = XCTAttachment(screenshot: app.screenshot()); typed.name = "S3 keyboard bar"; typed.lifetime = .keepAlways; add(typed) |
| 329 | |
| 330 | // Aa: the sheet, the keyboard gone, the text view still focused. |
| 331 | editor.doubleTap() |
| 332 | app.buttons["docs.keyboard.format"].tap() |
| 333 | XCTAssertTrue(anything(app, "docs.format.sheet").waitForExistence(timeout: 5), "Aa opens the Format sheet") |
| 334 | XCTAssertTrue(app.keyboards.firstMatch.waitForNonExistence(timeout: 5), "the keyboard goes") |
| 335 | XCTAssertEqual(editor.value(forKey: "hasKeyboardFocus") as? Bool, true, "the text view keeps first responder, so its selection stays visible") |
| 336 | XCTAssertFalse(app.buttons["docs.keyboard.bold"].exists, "no keyboard bar under the sheet") |
| 337 | let sheet = XCTAttachment(screenshot: app.screenshot()); sheet.name = "S3 Format sheet over a selection"; sheet.lifetime = .keepAlways; add(sheet) |
| 338 | try audit(app) |
| 339 | // ••• with the sheet up: the sheet goes, then what was asked for |
| 340 | // comes (two sheets cannot be up at once). |
| 341 | app.buttons["docs.more"].tap() |
| 342 | app.buttons["Word count"].tap() |
| 343 | XCTAssertTrue(anything(app, "docs.format.sheet").waitForNonExistence(timeout: 5), "••• closes the Format sheet") |
| 344 | XCTAssertTrue(app.navigationBars["Word count"].waitForExistence(timeout: 5), "then shows the word count") |
| 345 | app.navigationBars["Word count"].buttons["Close"].tap() |
| 346 | XCTAssertTrue(app.navigationBars["Word count"].waitForNonExistence(timeout: 5)) |
| 347 | XCTAssertTrue(app.keyboards.firstMatch.waitForExistence(timeout: 5), "the keyboard comes back with the sheet gone: it was up") |
| 348 | |
| 349 | // Keyboard down, back at the top: the title unfolds; a scroll folds it. |
| 350 | app.buttons["docs.keyboard.hide"].tap() |
| 351 | XCTAssertTrue(app.keyboards.firstMatch.waitForNonExistence(timeout: 5)) |
| 352 | editor.swipeDown(); editor.swipeDown() |
| 353 | XCTAssertTrue(inline.waitForNonExistence(timeout: 5), "at the top with the keyboard down, the title unfolds") |
| 354 | XCTAssertTrue(app.buttons["docs.undo"].waitForExistence(timeout: 3), "and the undo/redo pill is back in the bar") |
| 355 | editor.swipeUp() |
| 356 | XCTAssertTrue(inline.waitForExistence(timeout: 5), "scrolling past the title folds it") |
| 357 | |
| 358 | // A refused rename (a title over the server's 1,024 bytes) brings |
| 359 | // the alert back with the title as typed and why. |
| 360 | app.buttons["docs.more"].tap() |
| 361 | app.buttons["Rename document"].tap() |
| 362 | // An alert's controls reach UIKit without their identifiers. |
| 363 | let field = app.alerts.textFields.firstMatch |
| 364 | XCTAssertTrue(field.waitForExistence(timeout: 5), "Rename opens its alert") |
| 365 | field.typeText(String(repeating: "📄", count: 257)) |
| 366 | app.alerts.buttons["Rename document"].firstMatch.tap() |
| 367 | XCTAssertTrue(app.alerts.staticTexts["That title is too long."].waitForExistence(timeout: 5), "the refusal is shown") |
| 368 | XCTAssertTrue((field.value as? String)?.hasSuffix("📄📄📄") == true, "with the title as typed") |
| 369 | app.alerts.buttons["Cancel"].tap() |
| 370 | XCTAssertTrue(app.alerts.firstMatch.waitForNonExistence(timeout: 5)) |
| 371 | XCTAssertTrue(app.staticTexts["docs.document.title"].exists || inline.exists, "the document is still there") |
| 372 | // Refused again, and Back before the alert comes back: it does not |
| 373 | // come back over the library. |
| 374 | app.buttons["docs.more"].tap() |
| 375 | app.buttons["Rename document"].tap() |
| 376 | XCTAssertTrue(field.waitForExistence(timeout: 5)) |
| 377 | field.typeText(String(repeating: "📄", count: 257)) |
| 378 | app.alerts.buttons["Rename document"].firstMatch.tap() |
| 379 | app.buttons["BackButton"].tap() |
| 380 | XCTAssertTrue(app.buttons["Native review document"].firstMatch.waitForExistence(timeout: 5), "the library") |
| 381 | XCTAssertFalse(app.alerts.firstMatch.waitForExistence(timeout: 2), "and no rename alert over it") |
| 382 | _ = try openPhoneDocument(app, "Native review document") |
| 383 | |
| 384 | // Back with the Format sheet up: nothing of it is left behind. |
| 385 | app.buttons["docs.format.toggle"].tap() |
| 386 | XCTAssertTrue(anything(app, "docs.format.sheet").waitForExistence(timeout: 5)) |
| 387 | app.buttons["BackButton"].tap() |
| 388 | XCTAssertTrue(anything(app, "docs.format.sheet").waitForNonExistence(timeout: 5), "Back takes the sheet with it") |
| 389 | let again = try openPhoneDocument(app, "Native review document") |
| 390 | XCTAssertFalse(anything(app, "docs.format.sheet").waitForExistence(timeout: 2), "and it is not there when the document opens again") |
| 391 | again.tap() |
| 392 | XCTAssertTrue(app.keyboards.firstMatch.waitForExistence(timeout: 5), "the keyboard comes, not an empty input view") |
| 393 | } |
| 394 | |
| 395 | /// F5 (Fable's delta review on #180), a measurement, not a gate: does |
| 396 | /// UIKit keep a contentOffset change made during a drag and during a |
| 397 | /// fling? The review app's drag probe makes one in each; this drives |
| 398 | /// the drag and the fling and prints what the probe saw. |
| 399 | @MainActor func testDragProbe() throws { |
| 400 | try XCTSkipUnless(UIDevice.current.userInterfaceIdiom == .phone, "the iPhone editor") |
| 401 | let app = XCUIApplication() |
| 402 | app.launchEnvironment["DOCS_REVIEW_DRAG_PROBE"] = "1" |
| 403 | app.launch() |
| 404 | defer { app.terminate() } |
| 405 | let editor = try openPhoneDocument(app, "Second review document") |
| 406 | let probe = app.staticTexts["review.drag.probe"] |
| 407 | XCTAssertTrue(probe.waitForExistence(timeout: 10)) |
| 408 | let ready = NSPredicate(format: "label == %@", "Drag probe ready") |
| 409 | XCTAssertEqual(XCTWaiter().wait(for: [expectation(for: ready, evaluatedWith: probe)], timeout: 30), .completed, probe.label) |
| 410 | // A slow drag up, held at the end so it does not fling. |
| 411 | let from = editor.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.85)) |
| 412 | let to = editor.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.25)) |
| 413 | from.press(forDuration: 0.05, thenDragTo: to, withVelocity: .slow, thenHoldForDuration: 0.5) |
| 414 | // Then a fling up. |
| 415 | editor.swipeUp(velocity: XCUIGestureVelocity(1500)) |
| 416 | let finished = NSPredicate(format: "label BEGINSWITH %@", "Drag probe: ") |
| 417 | _ = XCTWaiter().wait(for: [expectation(for: finished, evaluatedWith: probe)], timeout: 15) |
| 418 | print("PROBE F5 " + probe.label) |
| 419 | } |
| 420 | |
| 421 | /// A reader: back and ••• only, ••• without Share, Rename or Delete, |
| 422 | /// and no keyboard bar (the text cannot take typing). |
| 423 | @MainActor func testPhoneDocumentForAReader() throws { |
| 424 | continueAfterFailure = false |
| 425 | let app = XCUIApplication() |
| 426 | app.launch() |
| 427 | defer { app.terminate() } |
| 428 | let editor = try openPhoneDocument(app, "Read-only review document") |
| 429 | XCTAssertTrue(app.buttons["docs.more"].waitForExistence(timeout: 5), "••• for a reader") |
| 430 | XCTAssertFalse(app.buttons["docs.format.toggle"].exists, "no Aa for a reader") |
| 431 | XCTAssertFalse(app.buttons["docs.undo"].exists, "no undo for a reader") |
| 432 | app.buttons["docs.more"].tap() |
| 433 | XCTAssertTrue(app.buttons["Word count"].waitForExistence(timeout: 3), "••• holds Word count for a reader") |
| 434 | for item in ["Share…", "Rename document", "Delete…"] { |
| 435 | XCTAssertFalse(app.buttons[item].exists, "\(item) is absent for a reader") |
| 436 | } |
| 437 | XCTAssertTrue(app.buttons["Export and print"].exists, "Export is a reader's too") |
| 438 | // A tap outside the menu closes it. |
| 439 | app.coordinate(withNormalizedOffset: CGVector(dx: 0.1, dy: 0.95)).tap() |
| 440 | XCTAssertTrue(app.buttons["Word count"].waitForNonExistence(timeout: 3)) |
| 441 | editor.tap() |
| 442 | XCTAssertFalse(app.buttons["docs.keyboard.bold"].waitForExistence(timeout: 2), "no keyboard bar for a reader") |
| 443 | try audit(app) |
| 444 | } |
| 445 | |
| 446 | /// Plan S3 acceptance: iPad keeps today's layout until S5 (spec §5, the |
| 447 | /// gate): the split view, the formatting strip and the toolbar's items, |
| 448 | /// and none of the iPhone's ••• or large title. |
| 449 | @MainActor func testIPadKeepsTodaysLayout() throws { |
| 450 | try XCTSkipUnless(UIDevice.current.userInterfaceIdiom == .pad, "the iPad gate") |
| 451 | continueAfterFailure = false |
| 452 | let app = XCUIApplication() |
| 453 | app.launch() |
| 454 | defer { app.terminate() } |
| 455 | let row = app.buttons["Native review document"].firstMatch |
| 456 | XCTAssertTrue(row.waitForExistence(timeout: 15)) |
| 457 | row.tap() |
| 458 | XCTAssertTrue(app.textViews["docs.editor"].waitForExistence(timeout: 15)) |
| 459 | XCTAssertTrue(anything(app, "docs.formatting-toolbar").waitForExistence(timeout: 5), "today's formatting strip") |
| 460 | XCTAssertTrue(app.buttons["Export and print"].exists, "today's toolbar items") |
| 461 | XCTAssertFalse(app.buttons["docs.more"].exists, "no iPhone •••") |
| 462 | XCTAssertFalse(anything(app, "docs.document.header").exists, "no iPhone large title") |
| 463 | let shot = XCTAttachment(screenshot: app.screenshot()); shot.name = "S3 iPad keeps today's layout"; shot.lifetime = .keepAlways; add(shot) |
| 464 | } |
| 465 | } |
| 466 | |