// Days opens on today and stays there: the list does not run off into the // past on its own, the button back to today stands only while today's date is off // screen (however long today is), and a long quiet stretch of the past is // one fold, not a fold for every sixty-two days (Android bd81b91d54). // Invented days only. import XCTest final class DaysOpeningTests: DaysUITestCase { @MainActor private func todaysDate(_ app: XCUIApplication) -> XCUIElement { app.buttons.matching(NSPredicate(format: "label BEGINSWITH %@", "Today, ")).firstMatch } /// The folds of quiet days standing above today's date: the past. @MainActor private func pastFolds(_ app: XCUIApplication) -> [String] { let top = todaysDate(app).frame.minY return app.staticTexts.matching(NSPredicate(format: "label CONTAINS %@ OR label ENDSWITH %@", "quiet days", "a quiet day")) .allElementsBoundByIndex.filter { $0.frame.maxY <= top }.map(\.label) } /// The round button back to today at the bottom left. @MainActor private func pill(_ app: XCUIApplication) -> XCUIElement { DaysUI.button(app, "Back to today") } // RULE: Days opens with today's date at the top, and it is still there a few seconds later. @MainActor func testDaysOpensOnTodayAndStaysThere() { let app = DaysUI.launch() DaysUI.days(app) let today = todaysDate(app) XCTAssertTrue(today.waitForExistence(timeout: 5), "today's date stands") settle(4) XCTAssertTrue(today.isHittable, "today's date is still on screen") XCTAssertLessThan(today.frame.minY, app.windows.firstMatch.frame.height * 0.5, "today's date stands near the top") XCTAssertLessThanOrEqual(pastFolds(app).count, 1, "the quiet past before yesterday is one fold: \(pastFolds(app))") } // RULE: with today's date on screen the button back to today does not stand; scrolled away from it, it does. @MainActor func testTheTodayPillStandsOnlyWhileTodaysDateIsAway() { let app = DaysUI.launch() DaysUI.days(app) let today = todaysDate(app) XCTAssertTrue(today.waitForExistence(timeout: 5)) settle(1.5) XCTAssertTrue(today.isHittable, "today's date is on screen") let x = today.frame.minX XCTAssertFalse(pill(app).exists, "no button back to today while today's date is on screen") for _ in 0..<3 { DaysUI.drag(app, from: CGVector(dx: 0.5, dy: 0.75), by: CGVector(dx: 0, dy: -400)) } settle() XCTAssertFalse(today.isHittable, "today's date scrolled away") XCTAssertTrue(pill(app).waitForExistence(timeout: 3), "the button back to today stands while today's date is away") XCTAssertEqual(today.frame.minX, x, accuracy: 1, "the list moved up and down only") } // RULE: scrolled up into the past, the quiet months before yesterday stand as one fold at most, and the list stops at what it has read. @MainActor func testThePastIsOneFoldAndTheListStaysPut() { let app = DaysUI.launch() DaysUI.days(app) let today = todaysDate(app) XCTAssertTrue(today.waitForExistence(timeout: 5)) for _ in 0..<4 { DaysUI.drag(app, from: CGVector(dx: 0.5, dy: 0.35), by: CGVector(dx: 0, dy: 400)) } settle(3) // One fold at most: the list may begin at its first item (CONTRACT ยง2, The beginning). XCTAssertLessThanOrEqual(pastFolds(app).count, 1, "the quiet past is one fold at most: \(pastFolds(app))") XCTAssertTrue(today.exists && today.frame.minY < app.windows.firstMatch.frame.height, "today's date is still near, the list did not run off into the past") } // RULE: far down a long today, a read (the app coming back, another device syncing) leaves the list where it stood, never back at today's date. @MainActor func testAReadKeepsThePlaceInALongToday() { let app = DaysUI.launch() DaysUI.days(app) for _ in 0..<3 { DaysUI.drag(app, from: CGVector(dx: 0.5, dy: 0.75), by: CGVector(dx: 0, dy: -400)) } settle(2) guard let row = DaysUI.middleRows(app, "Invented chore").first else { return XCTFail("a chore stands mid-screen") } let label = row.label let y = row.frame.minY // Away and back: Days reads again on coming back. XCUIDevice.shared.press(.home) settle(1.5) app.activate() settle(3) let now = DaysUI.whereIs(app, label, near: y) XCTAssertNotNil(now, "\(label) still stands after the read") XCTAssertEqual(now ?? -1000, y, accuracy: 4, "the list kept its place") XCTAssertFalse(todaysDate(app).isHittable, "the list did not go back to today's date") } }