// What every Notes UI test does: launch Notes Dev on the invented notes and // the crowd (notes-dev-seed.json, then notes-crowd.json: sixty-four invented // notes over thirty-six invented tags, and one note wearing seven), find a // row by its words, a sheet by what it holds, and drag from a given part of // an element as a finger would. Nothing here is anyone's real note. import XCTest enum NotesUI { /// Notes Dev on the invented notes and the crowd, with any extra launch arguments (a look to open, a text size). @MainActor static func launch(_ extra: [String] = []) -> XCUIApplication { let app = XCUIApplication() app.launchArguments += ["-notes-crowd", "YES"] + extra app.launch() return app } /// The largest accessibility text size, set at launch. static let largestText = ["-UIPreferredContentSizeCategoryName", "UICTContentSizeCategoryAccessibilityXXXL"] /// The crowd's note that wears seven tags. static let many = "Invented note with many tags" /// A row of the list (each is one button, its title, preview and date read together). @MainActor static func rows(_ app: XCUIApplication) -> XCUIElementQuery { app.buttons.matching(identifier: "notes-row") } /// A row by the words its label starts with (its title, or its preview for a note with none). @MainActor static func row(_ app: XCUIApplication, _ words: String) -> XCUIElement { rows(app).matching(NSPredicate(format: "label BEGINSWITH %@", words)).firstMatch } /// The list has come in with the crowd: one of its notes stands. @MainActor @discardableResult static func list(_ app: XCUIApplication) -> XCUIElement { let first = rows(app).matching(NSPredicate(format: "label CONTAINS %@", "Invented note")).firstMatch XCTAssertTrue(first.waitForExistence(timeout: 90), "the crowd came in") return first } /// What stands on screen, read in one snapshot (rows asked one by one for /// their frames take long): each element's type, identifier, label and frame. struct Seen { let type: XCUIElement.ElementType let id: String let label: String let frame: CGRect } @MainActor static func seen(_ app: XCUIApplication) -> [Seen] { guard let root = try? app.snapshot() else { return [] } var out: [Seen] = [] func walk(_ s: XCUIElementSnapshot) { if [.button, .textField, .textView, .staticText, .other].contains(s.elementType) { out.append(Seen(type: s.elementType, id: s.identifier, label: s.label, frame: s.frame)) } s.children.forEach(walk) } walk(root) return out } /// Where the list stands (under the header and search, or the shell's card if one stands above Notes). @MainActor static func listFrame(_ app: XCUIApplication) -> CGRect { let list = app.collectionViews.matching(identifier: "notes-list").firstMatch return list.exists ? list.frame : app.windows.firstMatch.frame } /// Every row standing whole in the list's view, top to bottom (a row half under the header is not one). @MainActor static func rowsOnScreen(_ app: XCUIApplication, _ all: [Seen]? = nil) -> [Seen] { let list = listFrame(app) return (all ?? seen(app)).filter { $0.type == .button && $0.id == "notes-row" && $0.frame.height > 0 && $0.frame.minY >= list.minY - 1 && $0.frame.maxY <= list.maxY + 1 } .sorted { $0.frame.minY < $1.frame.minY } } /// A row of the crowd standing whole in the middle of the list. @MainActor static func middleRow(_ app: XCUIApplication) -> Seen? { let list = listFrame(app) return rowsOnScreen(app).first { $0.label.contains("Invented note") && $0.frame.minY > list.minY + list.height * 0.15 && $0.frame.maxY < list.minY + list.height * 0.8 } } /// Where the row labelled `label` stands now (nil when it is not in the view at all). @MainActor static func whereIs(_ app: XCUIApplication, _ label: String) -> CGFloat? { seen(app).first { $0.id == "notes-row" && $0.label == label && $0.frame.height > 0 }?.frame.minY } /// A point of a row a snapshot saw (0…1 across and down it), to touch. @MainActor static func point(_ app: XCUIApplication, _ row: Seen, _ at: CGVector) -> XCUICoordinate { app.windows.firstMatch.coordinate(withNormalizedOffset: .zero) .withOffset(CGVector(dx: row.frame.minX + row.frame.width * at.dx, dy: row.frame.minY + row.frame.height * at.dy)) } /// A finger put down on a part of a row a snapshot saw and moved `by` quickly. @MainActor static func drag(_ app: XCUIApplication, _ row: Seen, at: CGVector, by: CGVector, hold: TimeInterval = 0.05, velocity: XCUIGestureVelocity = .fast) { let start = point(app, row, at) start.press(forDuration: hold, thenDragTo: start.withOffset(by), withVelocity: velocity, thenHoldForDuration: 0.05) } /// A finger put down at `at` (0…1 across and down `element`) and moved `by` points quickly. @MainActor static func drag(_ element: XCUIElement, at: CGVector, by: CGVector, hold: TimeInterval = 0.05) { let start = element.coordinate(withNormalizedOffset: at) start.press(forDuration: hold, thenDragTo: start.withOffset(by), withVelocity: .fast, thenHoldForDuration: 0.05) } /// A drag from a point of the window (0…1 of its width and height). @MainActor static func drag(_ app: XCUIApplication, from: CGVector, by: CGVector, hold: TimeInterval = 0.05) { drag(app.windows.firstMatch, at: from, by: by, hold: hold) } /// Flicks the list up until its last row stops moving; that row, or nil. /// The last row seen at all, whole or not: at the largest text under the /// shell's card the list's view holds less than two rows, and often none /// of them whole, which read as the end. @MainActor static func scrollToTheEnd(_ app: XCUIApplication, tries: Int = 90) -> Seen? { var last = "" for _ in 0.. 0 && $0.frame.intersects(list) } .max { $0.frame.minY < $1.frame.minY }.map { "\($0.label)@\(Int($0.frame.minY))" } ?? "" if now == last, !now.isEmpty { break } last = now } return rowsOnScreen(app).last } /// A row standing whole in the list's view, moving the list a little until one does. @MainActor static func wholeRow(_ app: XCUIApplication) -> Seen? { for _ in 0..<6 { if let row = rowsOnScreen(app).first { return row } let start = app.windows.firstMatch.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.75)) start.press(forDuration: 0.05, thenDragTo: start.withOffset(CGVector(dx: 0, dy: -90)), withVelocity: .slow, thenHoldForDuration: 0.3) } return nil } /// A button by its exact label. @MainActor static func button(_ app: XCUIApplication, _ label: String) -> XCUIElement { app.buttons.matching(NSPredicate(format: "label == %@", label)).firstMatch } /// Any element by its identifier. @MainActor static func id(_ app: XCUIApplication, _ id: String) -> XCUIElement { app.descendants(matching: .any).matching(identifier: id).firstMatch } /// Whether a frame stands wholly inside the window's safe part: under /// the clock and above the home indicator, clear of the sides. @MainActor static func inside(_ frame: CGRect, _ app: XCUIApplication, top: CGFloat = 44, bottom: CGFloat = 20) -> Bool { let window = app.windows.firstMatch.frame return frame.minY >= window.minY + top && frame.maxY <= window.maxY - bottom && frame.minX >= window.minX && frame.maxX <= window.maxX } @MainActor static func inside(_ element: XCUIElement, _ app: XCUIApplication, top: CGFloat = 44, bottom: CGFloat = 20) -> Bool { inside(element.frame, app, top: top, bottom: bottom) } /// The software keyboard, once it has risen. @MainActor static func keyboard(_ app: XCUIApplication) -> XCUIElement { let keys = app.keyboards.firstMatch XCTAssertTrue(keys.waitForExistence(timeout: 5), "the keyboard rose") // Let it finish rising. _ = app.buttons["__none__"].waitForExistence(timeout: 0.6) return keys } /// The note's writing surface, once a note has opened. @MainActor static func writer(_ app: XCUIApplication) -> XCUIElement { id(app, "note-writer") } } /// Every Notes UI test: upright to start, upright again after, and the /// screen kept with a failure so it can be looked at. class NotesUITestCase: XCTestCase { override func setUp() { continueAfterFailure = false XCUIDevice.shared.orientation = .portrait } override func tearDown() { if let run = testRun, run.failureCount > 0 { let shot = XCTAttachment(screenshot: XCUIScreen.main.screenshot()) shot.name = name shot.lifetime = .keepAlways add(shot) } XCUIDevice.shared.orientation = .portrait } /// Another device asking to be let in stands the shell's card at the top /// of the screen (the dev server's own traffic, not this test's). The /// card pushes Words down (a note covers it), so this skips only when it /// truly stands over one of Words' own top controls (`controls`, else the /// list's place and More, and a note's Back, Info and More): on top, /// where a finger would land on it. The card is never tapped. @MainActor func skipUnderTheShellsCard(_ app: XCUIApplication, wait: TimeInterval = 2, _ controls: [XCUIElement] = []) throws { let tops = controls.isEmpty ? ["notes-place", "notes-more", "note-back", "note-info", "note-more"].map { NotesUI.id(app, $0) } : controls if ShellCard.covers(app, tops, wait: wait) { throw XCTSkip("another device's unlock request stands over Words' own top") } } /// Waits until `element` can be touched (a sheet's slide has finished). @MainActor func waitHittable(_ element: XCUIElement, _ timeout: TimeInterval = 3) { let deadline = Date().addingTimeInterval(timeout) while Date() < deadline, !(element.exists && element.isHittable) { RunLoop.current.run(until: Date().addingTimeInterval(0.1)) } } /// A moment for a slide or a scroll to settle. @MainActor func settle(_ seconds: TimeInterval = 0.5) { RunLoop.current.run(until: Date().addingTimeInterval(seconds)) } } /// The shell's card of another device asking to be let in. `seed` stands an /// invented one (Walk Mac, `-shell-card-seed YES`, a development build only): /// it reaches no server, and its Refuse only takes it down and says so (`ShellToastUI`). enum ShellCard { static let seed = ["-shell-card-seed", "YES"] /// The card's Refuse: standing on top while it can be touched. Its id is /// `approval-refuse`, or the card's own `approval-banner` where the card /// (an app's look) hands its id to everything in it. @MainActor static func refuse(_ app: XCUIApplication) -> XCUIElement { app.buttons.matching(NSPredicate(format: "identifier BEGINSWITH %@", "approval-")).firstMatch } /// The band the shell's cards stand in, scrolling inside when they are taller than it. @MainActor static func band(_ app: XCUIApplication) -> XCUIElement { app.scrollViews.containing(NSPredicate(format: "identifier BEGINSWITH %@", "approval-")).firstMatch } /// Everything of the card drawn on screen (inside its band), or nil while none of it is drawn. @MainActor static func frame(_ app: XCUIApplication) -> CGRect? { let parts = app.descendants(matching: .any).matching(NSPredicate(format: "identifier BEGINSWITH %@", "approval-")) guard parts.firstMatch.exists else { return nil } var frame = parts.allElementsBoundByIndex.reduce(CGRect.null) { $0.union($1.frame) } let band = band(app) if band.exists { frame = frame.intersection(band.frame) } return frame.isNull ? nil : frame } /// Whether the card stands on top (its Refuse can be touched) over any of `controls`. @MainActor static func covers(_ app: XCUIApplication, _ controls: [XCUIElement], wait: TimeInterval) -> Bool { let refuse = refuse(app) guard refuse.waitForExistence(timeout: wait), refuse.isHittable, let card = frame(app) else { return false } return controls.contains { $0.exists && $0.frame.intersects(card) } } /// `control` stands, can be touched, and lies wholly outside the card. @MainActor static func clear(_ app: XCUIApplication, _ control: XCUIElement, _ name: String, file: StaticString = #filePath, line: UInt = #line) { XCTAssertTrue(control.waitForExistence(timeout: 10), "\(name) stands", file: file, line: line) let card = frame(app) ?? .null XCTAssertFalse(card.isNull, "the card stands while \(name) is looked at", file: file, line: line) XCTAssertTrue(control.isHittable, "\(name) can be touched", file: file, line: line) // A page drawn over the whole window (a note) covers the card: it stands under that page, over nothing. if refuse(app).isHittable { XCTAssertFalse(control.frame.intersects(card), "\(name) \(control.frame) stands clear of the card \(card)", file: file, line: line) } let window = app.windows.firstMatch.frame XCTAssertTrue(window.contains(control.frame), "\(name) \(control.frame) is on screen", file: file, line: line) } /// The card fits: no taller than half the screen, and its Refuse whole on screen above the home bar. @MainActor static func fits(_ app: XCUIApplication, file: StaticString = #filePath, line: UInt = #line) { let window = app.windows.firstMatch.frame let refuse = refuse(app) XCTAssertTrue(refuse.waitForExistence(timeout: 10), "the card stands", file: file, line: line) let card = frame(app) ?? .null XCTAssertLessThanOrEqual(card.height, window.height * 0.5, "the card \(card) takes at most half the screen", file: file, line: line) // Taller than its band, the card scrolls: Refuse is reached by a swipe up inside it, never tapped. let band = band(app) for _ in 0..<4 where band.exists && !(refuse.isHittable && refuse.frame.maxY <= band.frame.maxY + 1) { let start = band.coordinate(withNormalizedOffset: CGVector(dx: 0.9, dy: 0.8)) start.press(forDuration: 0.01, thenDragTo: start.withOffset(CGVector(dx: 0, dy: -band.frame.height * 0.5))) } if band.exists { XCTAssertLessThanOrEqual(refuse.frame.maxY, band.frame.maxY + 1, "Refuse \(refuse.frame) stands whole in the card's band \(band.frame)", file: file, line: line) } XCTAssertTrue(refuse.isHittable, "Refuse can be touched", file: file, line: line) XCTAssertLessThanOrEqual(refuse.frame.maxY, window.maxY - 34, "Refuse \(refuse.frame) stands above the home bar", file: file, line: line) } }