diff --git a/Sources/ScrollkeeperKit/Store/LibraryEnvironment.swift b/Sources/ScrollkeeperKit/Store/LibraryEnvironment.swift index fb9a26e..d6c48b5 100644 --- a/Sources/ScrollkeeperKit/Store/LibraryEnvironment.swift +++ b/Sources/ScrollkeeperKit/Store/LibraryEnvironment.swift @@ -92,15 +92,31 @@ extension LibraryEnvironment { /// The launch argument that starts the app in demo mode. public static let demoArgument = "-ScrollkeeperDemo" + /// The launch argument followed by how many seconds each demo download takes. The UI tests ask for long ones, + /// so that a download is still running when a slow simulator gets around to looking. + public static let demoDownloadSecondsArgument = "-demoDownloadSeconds" + + public static let defaultDemoDownloadDuration: TimeInterval = 8 + /// The environment the app should run in: demo mode when launched with `demoArgument`. public static func forLaunch(arguments: [String] = ProcessInfo.processInfo.arguments) -> LibraryEnvironment { - arguments.contains(demoArgument) ? demo() : live() + guard arguments.contains(demoArgument) else { return live() } + return demo(downloadDuration: demoDownloadDuration(in: arguments)) + } + + /// The seconds given after `demoDownloadSecondsArgument`, or the usual eight without a usable number. + static func demoDownloadDuration(in arguments: [String]) -> TimeInterval { + guard let name = arguments.firstIndex(of: demoDownloadSecondsArgument), + arguments.indices.contains(name + 1), + let seconds = TimeInterval(arguments[name + 1]), seconds > 0 + else { return defaultDemoDownloadDuration } + return seconds } /// An invented library served from inside the app, already signed in, kept in a temporary folder that starts /// empty on every launch. Only the free titles' downloads reach the network, and nothing the Keychain. On the Mac /// it shares through the App Group when no real library is there (library-manager 13.7). - public static func demo(downloadDuration: TimeInterval = 8, + public static func demo(downloadDuration: TimeInterval = defaultDemoDownloadDuration, outside: HTTPClient = URLSessionHTTPClient()) -> LibraryEnvironment { let run = UUID().uuidString var environment = LibraryEnvironment( diff --git a/Tests/ScrollkeeperKitTests/DemoTests.swift b/Tests/ScrollkeeperKitTests/DemoTests.swift index e41693b..a5d2124 100644 --- a/Tests/ScrollkeeperKitTests/DemoTests.swift +++ b/Tests/ScrollkeeperKitTests/DemoTests.swift @@ -71,6 +71,16 @@ import Testing #expect(live.dataDirectory.lastPathComponent == "Scrollkeeper") } + @Test func demoDownloadsTakeAsLongAsTheLaunchArgumentSays() { + let name = LibraryEnvironment.demoDownloadSecondsArgument + + #expect(LibraryEnvironment.demoDownloadDuration(in: ["Scrollkeeper", name, "120"]) == 120) + #expect(LibraryEnvironment.demoDownloadDuration(in: ["Scrollkeeper"]) == 8) + #expect(LibraryEnvironment.demoDownloadDuration(in: ["Scrollkeeper", name]) == 8) + #expect(LibraryEnvironment.demoDownloadDuration(in: ["Scrollkeeper", name, "soon"]) == 8) + #expect(LibraryEnvironment.demoDownloadDuration(in: ["Scrollkeeper", name, "0"]) == 8) + } + @Test func demoServerAnswersUnknownAddressesWithNotFound() async throws { let client = DemoHTTPClient() let unknown = try #require(URL(string: "https://store.paizo.com/nowhere")) diff --git a/ios/UITests/DownloadsListUITests.swift b/ios/UITests/DownloadsListUITests.swift index 1629cdf..e114e6e 100644 --- a/ios/UITests/DownloadsListUITests.swift +++ b/ios/UITests/DownloadsListUITests.swift @@ -8,10 +8,14 @@ final class DownloadsListUITests: XCTestCase { continueAfterFailure = false } + /// Long enough that a download is still running whenever a slow simulator gets around to looking. + private let unhurried = 300 + + /// Launches the demo library, whose downloads take eight seconds unless told otherwise. @MainActor - private func launchDemo() -> XCUIApplication { + private func launchDemo(downloadSeconds: Int = 8) -> XCUIApplication { let app = XCUIApplication() - app.launchArguments = ["-ScrollkeeperDemo", "-viewMode", "list"] + app.launchArguments = ["-ScrollkeeperDemo", "-viewMode", "list", "-demoDownloadSeconds", "\(downloadSeconds)"] app.launch() XCTAssertTrue(app.titleRow("Sample Creature Rulebook").exists, "library did not load") return app @@ -29,7 +33,7 @@ final class DownloadsListUITests: XCTestCase { @MainActor func testDownloadsListOpensWhileADownloadIsRunning() { - let app = launchDemo() + let app = launchDemo(downloadSeconds: unhurried) startDownload(of: "Sample Creature Rulebook", in: app) let downloads = app.buttons["downloads-button"].firstMatch @@ -56,7 +60,7 @@ final class DownloadsListUITests: XCTestCase { @MainActor func testRunningDownloadCanBeCanceledFromTheList() { - let app = launchDemo() + let app = launchDemo(downloadSeconds: unhurried) startDownload(of: "Sample Creature Rulebook", in: app) app.buttons["downloads-button"].firstMatch.tap() XCTAssertTrue(app.buttons["Cancel All"].waitForExistence(timeout: 5)) @@ -78,13 +82,22 @@ final class DownloadsListUITests: XCTestCase { extension XCUIApplication { /// The row of a title in the library list, scrolled into view: the list builds only the rows on screen, and - /// the demo library has more titles than fit on an iPad. Gives up after `timeout` seconds. - func titleRow(_ title: String, timeout: TimeInterval = 30) -> XCUIElement { + /// the demo library has more titles than fit on an iPad. Gives up after `timeout` seconds, which also have to + /// cover the library loading on a slow simulator. + func titleRow(_ title: String, timeout: TimeInterval = 90) -> XCUIElement { let row = staticTexts[title] let deadline = Date().addingTimeInterval(timeout) while !row.waitForExistence(timeout: 1), Date() < deadline { - swipeUp() + scrollDownWithoutCoasting() } return row } + + /// Moves the list on by a third of the screen and holds it there. A swipe lets the list coast, and a list + /// that coasts past the row has dropped it again by the time the row is looked for. + private func scrollDownWithoutCoasting() { + let lower = coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.7)) + let upper = coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.4)) + lower.press(forDuration: 0.1, thenDragTo: upper, withVelocity: .default, thenHoldForDuration: 0.3) + } }