Repository Public
1// SP Drive iOS2// File: UITests/SystemUITests.swift3// Description: The parts of SP Drive that live in other processes, driven on the simulator as a user would: a widget added to the home4// screen (to look at its real rendering), and the share extension opened from the Photos app.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import XCTest910final class SystemUITests: DriveUITestCase {11 private func dump(_ app: XCUIApplication, _ name: String) { let d = XCTAttachment(string: app.debugDescription); d.name = name; d.lifetime = .keepAlways; add(d) }12 private func firstExisting(_ candidates: [XCUIElement], timeout: TimeInterval = 4) -> XCUIElement? {13 let deadline = Date().addingTimeInterval(timeout)14 repeat { if let e = candidates.first(where: { $0.exists }) { return e }; Thread.sleep(forTimeInterval: 0.3) } while Date() < deadline15 return nil16 }1718 /// Adds the « Fichiers récents » widget to the home screen and captures it with real data from the app's snapshot.19 func testWidgetOnTheHomeScreen() throws {20 let folderID = try seedFolder("Widgets \(UUID().uuidString.prefix(5))")21 _ = try seedFile("Rapport pour le widget.txt", "texte", in: folderID)22 _ = try seedFile("Budget du widget.csv", "a,b\n", in: folderID)23 let app = launch(signedIn: true)24 XCTAssertTrue(app.descendants(matching: .any).matching(NSPredicate(format: "identifier BEGINSWITH %@", "item.Widgets")).firstMatch.waitForExistence(timeout: 30))25 Thread.sleep(forTimeInterval: 4) // the snapshot for the widgets is written a moment after the data changes26 XCUIDevice.shared.press(.home)27 let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")28 Thread.sleep(forTimeInterval: 1.5)29 // jiggle mode: long press on an empty spot of the home screen30 springboard.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.72)).press(forDuration: 1.6)31 guard let edit = firstExisting([springboard.buttons["Modifier"], springboard.buttons["Edit"], springboard.buttons["Ajouter un widget"], springboard.buttons["Add Widget"]], timeout: 6) else {32 dump(springboard, "springboard-jiggle"); shot("140x-ecran-accueil"); throw XCTSkip("the home screen did not enter edit mode on this simulator")33 }34 edit.tap()35 if let add = firstExisting([springboard.buttons["Ajouter un widget"], springboard.buttons["Add Widget"], springboard.menuItems["Ajouter un widget"], springboard.menuItems["Add Widget"]], timeout: 4), add.isHittable { add.tap() }36 guard let search = firstExisting([springboard.searchFields.firstMatch], timeout: 8) else { dump(springboard, "springboard-gallery"); shot("140x-galerie"); throw XCTSkip("the widget gallery did not open") }37 search.tap(); search.typeText("SP Drive")38 let entry = springboard.descendants(matching: .any).matching(NSPredicate(format: "label BEGINSWITH %@", "SP Drive")).allElementsBoundByIndex.first { $0.isHittable && $0.elementType != .searchField }39 guard let entry else { dump(springboard, "springboard-search"); shot("140x-recherche-widget"); throw XCTSkip("SP Drive is not listed in the widget gallery") }40 entry.tap()41 shot("140-galerie-widgets")42 guard let confirm = firstExisting([springboard.buttons["Ajouter le widget"], springboard.buttons["Add Widget"], springboard.buttons[" Ajouter le widget"]], timeout: 8) else { dump(springboard, "springboard-add"); throw XCTSkip("no « add widget » button") }43 confirm.tap()44 Thread.sleep(forTimeInterval: 1)45 if let done = firstExisting([springboard.buttons["Terminé"], springboard.buttons["Done"], springboard.buttons["OK"]], timeout: 4) { done.tap() } else { XCUIDevice.shared.press(.home) }46 Thread.sleep(forTimeInterval: 3)47 shot("141-widget-ecran-accueil")48 let widget = springboard.descendants(matching: .any).matching(NSPredicate(format: "label CONTAINS %@ OR label CONTAINS %@", "Rapport pour le widget", "Récents")).firstMatch49 XCTAssertTrue(widget.waitForExistence(timeout: 10), "the widget shows the app's snapshot on the home screen")50 }5152 /// Shares a photo from the Photos app to SP Drive through the share extension, and checks that it reached the Drive.53 func testShareExtensionFromPhotos() throws {54 let app = launch(signedIn: true)55 XCTAssertTrue(app.tabBars.firstMatch.waitForExistence(timeout: 30) || app.descendants(matching: .any)["sidebar.drive"].firstMatch.waitForExistence(timeout: 5))56 Thread.sleep(forTimeInterval: 3)57 let before = ((try api("GET", "/api/v1/folders/root"))["files"] as? [[String: Any]] ?? []).count58 let photos = XCUIApplication(bundleIdentifier: "com.apple.mobileslideshow")59 photos.launchArguments = ["-AppleLanguages", "(fr)", "-AppleLocale", "fr_CA"]60 photos.launch()61 for label in ["Continuer", "Continue", "Plus tard", "Not Now"] { let b = photos.buttons[label].firstMatch; if b.waitForExistence(timeout: 2) { b.tap() } }62 guard photos.staticTexts.matching(NSPredicate(format: "label CONTAINS[c] %@", "photo")).firstMatch.waitForExistence(timeout: 10) else { dump(photos, "photos"); shot("150x-photos"); throw XCTSkip("no photo in the simulator's library") }63 // a long press on the first photo of the grid opens its menu, which offers « Partager »64 photos.windows.firstMatch.coordinate(withNormalizedOffset: CGVector(dx: 0.17, dy: 0.24)).press(forDuration: 1.3)65 guard let share = firstExisting([photos.buttons["Partager"], photos.buttons["Share"], photos.menuItems["Partager"], photos.staticTexts["Partager"]], timeout: 8) else { dump(photos, "photos-menu"); shot("150x-photo"); throw XCTSkip("no share entry in the photo's menu") }66 share.tap()67 var target = photos.descendants(matching: .any).matching(NSPredicate(format: "label == %@", "SP Drive")).firstMatch68 if !target.waitForExistence(timeout: 6) {69 // the app row of the share sheet ends with « Plus »: SP Drive may have to be picked there70 let row = photos.collectionViews.firstMatch71 row.swipeLeft(); row.swipeLeft()72 if let more = firstExisting([photos.buttons["Plus"], photos.buttons["More"], photos.cells["Plus"], photos.cells["More"]], timeout: 3) { more.tap() }73 target = photos.descendants(matching: .any).matching(NSPredicate(format: "label == %@", "SP Drive")).firstMatch74 }75 guard target.waitForExistence(timeout: 6) else { dump(photos, "share-sheet"); shot("150x-feuille-partage"); throw XCTSkip("SP Drive is not offered in the share sheet") }76 target.tap()77 let send = photos.buttons["Envoyer"].firstMatch78 XCTAssertTrue(send.waitForExistence(timeout: 20), "the extension's own screen is shown")79 shot("150-extension-partage")80 send.tap()81 XCTAssertTrue(photos.staticTexts["Envoyé et vérifié"].firstMatch.waitForExistence(timeout: 40) || photos.buttons["OK"].firstMatch.waitForExistence(timeout: 5))82 shot("151-extension-envoye")83 let deadline = Date().addingTimeInterval(20)84 var after = before85 while Date() < deadline, after <= before { after = ((try? apiSync("/api/v1/folders/root"))?["files"] as? [[String: Any]] ?? []).count; Thread.sleep(forTimeInterval: 0.5) }86 XCTAssertGreaterThan(after, before, "the photo is on the Drive")87 if photos.buttons["OK"].firstMatch.exists { photos.buttons["OK"].firstMatch.tap() }88 }89}90