import XCTest /// Opens every model's step list and every step of every model by the labels VoiceOver sees and /// runs Xcode's accessibility audit on each screen, at the default text size and at the largest /// accessibility size. Also checks that two-finger gestures on the paper change the view without /// advancing the fold or touching the diagram. XCUITest has no two-finger drag, so a pinch and a /// rotation stand in for it. final class AccessibilityAuditTests: XCTestCase { static let largestText = "UICTContentSizeCategoryAccessibilityXXXL" /// Elements that exist only under `-UITesting`. static let testHooks: Set = ["fold.path", "paper.camera", "step.kind"] /// Every model in the list, by title, with its number of steps. static let models = [("Cup", 6), ("Box", 12), ("Sampan", 6), ("Crane", 16)] private var sizeName = "" override func setUp() { continueAfterFailure = true } func testEveryScreenPassesTheAccessibilityAuditAtDefaultTextSize() throws { try everyScreen(textSize: nil) } func testEveryScreenPassesTheAuditAtTheLargestTextSize() throws { try everyScreen(textSize: Self.largestText) } func testPaperViewDescribesTheCurrentStepInWords() { let app = XCUIApplication(); app.launch() app.buttons["Cup"].tap() XCTAssertTrue(app.otherElements["fold.paper"].label.hasPrefix("Valley fold")) } func testTwoFingerGesturesChangeTheViewWithoutAdvancingTheFold() { let app = XCUIApplication(); app.launchArguments = ["-UITesting"]; app.launch() app.buttons["Cup"].tap() let paper = app.otherElements["fold.paper"] let camera = app.descendants(matching: .any)["paper.camera"] XCTAssertTrue(camera.waitForExistence(timeout: 5)) let before = paper.value as? String let cameraBefore = camera.value as? String twoFingerGestures(paper) XCTAssertEqual(app.staticTexts["step.index"].label, "1") XCTAssertEqual(paper.value as? String, before) XCTAssertNotEqual(camera.value as? String, cameraBefore) } func testDiagramDoesNotChangeWhenTheModelViewChanges() { let app = XCUIApplication(); app.launchArguments = ["-UITesting"]; app.launch() app.buttons["Cup"].tap() let diagram = app.descendants(matching: .any)["diagram"] let camera = app.descendants(matching: .any)["paper.camera"] XCTAssertTrue(diagram.waitForExistence(timeout: 5)) XCTAssertTrue(camera.waitForExistence(timeout: 5)) let before = diagram.screenshot().pngRepresentation let cameraBefore = camera.value as? String twoFingerGestures(app.otherElements["fold.paper"]) XCTAssertNotEqual(camera.value as? String, cameraBefore) XCTAssertEqual(diagram.screenshot().pngRepresentation, before) } // MARK: - Helpers /// The model list, then for every model its step list and every step, each advanced by the /// user's own drag: a reorient drag on a turn-over or rotate step, the fold drag otherwise. private func everyScreen(textSize: String?) throws { sizeName = textSize == nil ? "default" : "largest" let app = XCUIApplication() app.launchArguments = ["-UITesting"] if let textSize { app.launchArguments += ["-UIPreferredContentSizeCategoryName", textSize] } app.launch() try audit(app, "models") for (title, steps) in Self.models { app.buttons[title].tap() let index = app.staticTexts["step.index"] XCTAssertTrue(index.waitForExistence(timeout: 5)) app.buttons["steps.button"].tap() XCTAssertTrue(app.buttons["step.1"].waitForExistence(timeout: 5)) try audit(app, "\(title) step list") app.buttons["step.1"].tap() XCTAssertTrue(app.buttons["step.1"].waitForNonExistence(timeout: 5)) while true { let step = index.label try audit(app, "\(title) step \(step)") if ["turnOver", "rotate"].contains(stepKind(app)) { dragAcross(app.otherElements["fold.paper"]) } else { completeCurrentFold(app) } if index.label == step { break } } // Stopping before the last step means an advance failed. XCTAssertEqual(index.label, "\(steps)", title) try audit(app, "\(title) finished") app.navigationBars.buttons.firstMatch.tap() XCTAssertTrue(app.buttons[title].waitForExistence(timeout: 5)) } } private func audit(_ app: XCUIApplication, _ name: String) throws { sleep(1) let shot = XCTAttachment(screenshot: app.screenshot()) shot.name = "\(sizeName) \(name)" shot.lifetime = .keepAlways add(shot) let screen = app.windows.firstMatch.frame let navigationBar = app.navigationBars.firstMatch let barFrame = navigationBar.exists ? navigationBar.frame : .null let scrollFrames = app.scrollViews.allElementsBoundByIndex.map(\.frame) let cutRows = rowsCutByTheStepList(app) var clipped: [String] = [] try app.performAccessibilityAudit { issue in if self.isExpected(issue, screen: screen, barFrame: barFrame, scrollFrames: scrollFrames) { return true } if self.isCutStepRow(issue, cutRows) { return true } // Clipping is re-checked below: it is reported now and then for text that is not clipped. if issue.auditType == .textClipped { clipped.append(issue.element?.label ?? "") return true } self.report(issue, name) return false } guard !clipped.isEmpty else { return } sleep(1) try app.performAccessibilityAudit(for: .textClipped) { issue in guard clipped.contains(issue.element?.label ?? ""), !self.isCutStepRow(issue, cutRows) else { return true } self.report(issue, name) return false } } private func isExpected(_ issue: XCUIAccessibilityAuditIssue, screen: CGRect, barFrame: CGRect, scrollFrames: [CGRect]) -> Bool { if let element = issue.element, Self.testHooks.contains(element.identifier) { return true } // Navigation bar buttons are drawn by the system. if let element = issue.element, barFrame.intersects(element.frame) { return true } // Text growth is checked by the largest-text run, where clipping shows. if sizeName == "default", issue.auditType == .dynamicType { return true } // Contrast is measured from pixels, so text partly off screen measures wrong. if issue.auditType == .contrast, let element = issue.element, !screen.contains(element.frame) { return true } // The same for the caption, which at the largest size runs past the bottom of its scroll view. if issue.auditType == .contrast, let element = issue.element, element.identifier == "diagram.caption", scrollFrames.contains(where: { $0.intersects(element.frame) && !$0.contains(element.frame) }) { return true } return false } /// The frames of the step list's rows (`step.N`) partly scrolled out of the list, whose text the /// list's edge clips. private func rowsCutByTheStepList(_ app: XCUIApplication) -> [CGRect] { let list = app.collectionViews.firstMatch guard list.exists else { return [] } let edge = list.frame let rows = app.buttons.matching(NSPredicate(format: "identifier BEGINSWITH 'step.'")).allElementsBoundByIndex return rows.map(\.frame).filter { $0.intersects(edge) && !edge.contains($0) } } /// Whether `issue` is text clipped in a step-list row that the list's edge cuts. The audit /// reports such text without an element at times, which matches only while a row is cut. private func isCutStepRow(_ issue: XCUIAccessibilityAuditIssue, _ cutRows: [CGRect]) -> Bool { guard issue.auditType == .textClipped, !cutRows.isEmpty else { return false } guard let element = issue.element else { return true } return cutRows.contains { $0.intersects(element.frame) } } private func report(_ issue: XCUIAccessibilityAuditIssue, _ name: String) { print("AUDIT [\(sizeName)] [\(name)] \(issue.auditType.rawValue) \(issue.compactDescription) :: \(issue.element?.debugDescription.prefix(200) ?? "-")") } } /// The two-finger gestures XCUITest can make on the element: a pinch out, a pinch in and a /// rotation. func twoFingerGestures(_ element: XCUIElement) { element.pinch(withScale: 1.6, velocity: 1) element.pinch(withScale: 0.8, velocity: -1) element.rotate(0.6, withVelocity: 1) }