Repository Public License
1// ORIX Clean — UITests/ORIXCleanUITests.swift2// Description: End-to-end UI tests on a disposable sample home folder stored on a test disk image (Scripts/build.sh uitest).3// Cleanup tests really move files to the Trash of that disk image; nothing outside it is touched.4// Author: Simon-Pierre Boucher — contact@spboucher.ai5import XCTest67final class ORIXCleanUITests: XCTestCase {8 static let home = "/tmp/orix-uitest/home"9 static let exports = "/tmp/orix-uitest-exports-" + UUID().uuidString.prefix(8)10 var app: XCUIApplication!1112 override func setUpWithError() throws {13 continueAfterFailure = false14 try XCTSkipUnless(FileManager.default.fileExists(atPath: Self.home + "/Movies"), "Sample home missing: run Scripts/build.sh uitest")15 }1617 override func tearDown() { app?.terminate() }1819 @discardableResult20 func launch(section: String = "overview", scan: Bool = true, analyze: Bool = false, extra: [String] = []) -> XCUIApplication {21 app = XCUIApplication()22 app.launchArguments = ["-ORIXHome", Self.home, "-ORIXStore", "memory", "-ORIXSection", section, "-ORIXExportDir", Self.exports,23 "-ApplePersistenceIgnoreState", "YES", "-AppleLanguages", "(en)", "-AppleLocale", "en_CA"]24 if !extra.contains("-ORIXOnboarding") { app.launchArguments += ["-ORIXSkipOnboarding", "YES"] }25 if scan { app.launchArguments += ["-ORIXScanRoot", Self.home] }26 if analyze { app.launchArguments += ["-ORIXAutoAnalyze", "YES"] }27 app.launchArguments += extra.filter { $0 != "-ORIXOnboarding" }28 app.launch()29 app.activate()30 XCTAssertTrue(app.windows.firstMatch.waitForExistence(timeout: 15))31 return app32 }3334 func element(_ id: String) -> XCUIElement { app.descendants(matching: .any).matching(identifier: id).firstMatch }35 func text(containing s: String) -> XCUIElement {36 app.descendants(matching: .any).matching(NSPredicate(format: "label CONTAINS %@ OR value CONTAINS %@ OR title CONTAINS %@", s, s, s)).firstMatch37 }3839 /// Shows another section by relaunching on it (independent of sidebar scroll position).40 func go(_ section: String, scan: Bool = true, analyze: Bool = false) {41 app.terminate()42 launch(section: section, scan: scan, analyze: analyze)43 }44 /// Clicks with the app brought back to the front first; other automation on this Mac can take focus mid-test.45 func tap(_ e: XCUIElement, file: StaticString = #filePath, line: UInt = #line) {46 for attempt in 0 ..< 4 {47 app.activate()48 if e.waitForExistence(timeout: 5), e.isHittable { e.click(); return }49 Thread.sleep(forTimeInterval: 0.5 + Double(attempt) * 0.5)50 }51 app.activate()52 e.click() // last attempt: let XCTest report its own diagnosis if this fails53 }5455 func type(_ string: String, into e: XCUIElement) {56 for _ in 0 ..< 4 {57 tap(e)58 if (e.value(forKey: "hasKeyboardFocus") as? Bool) == true { e.typeText(string); return }59 Thread.sleep(forTimeInterval: 0.7)60 }61 e.typeText(string)62 }6364 func attach(_ name: String) {65 let a = XCTAttachment(screenshot: app.windows.firstMatch.screenshot())66 a.name = name; a.lifetime = .keepAlways67 add(a)68 }6970 func testFirstLaunchShowsOnboardingAndNothingIsScanned() {71 launch(scan: false, extra: ["-ORIXOnboarding", "-onboarded", "NO"])72 let start = element("onboarding.start")73 XCTAssertTrue(start.waitForExistence(timeout: 10))74 XCTAssertTrue(text(containing: "Nothing is removed without you").exists)75 attach("onboarding")76 tap(start)77 XCTAssertTrue(element("overview.scanHome").waitForExistence(timeout: 5))78 XCTAssertFalse(element("selection.review").exists)79 }8081 func testScanStorageMapSearchAndSidebarNavigation() {82 launch(section: "map")83 XCTAssertTrue(element("map.canvas").waitForExistence(timeout: 20))84 XCTAssertTrue(element("map.list").waitForExistence(timeout: 5))85 XCTAssertTrue(text(containing: "Library").waitForExistence(timeout: 5))86 attach("storage-map")87 let search = element("map.search")88 type("Conference", into: search)89 XCTAssertTrue(text(containing: "Conference talk").waitForExistence(timeout: 5))90 XCTAssertTrue(text(containing: "matches").exists)91 go("largeFiles")92 XCTAssertTrue(element("large.list").waitForExistence(timeout: 10))93 XCTAssertTrue(text(containing: "Lisbon trip 4K.mov").waitForExistence(timeout: 5))94 attach("large-files")95 }9697 func testDuplicatesAndSimilarImages() {98 launch(section: "duplicates", analyze: true)99 XCTAssertTrue(element("duplicates.list").waitForExistence(timeout: 40))100 XCTAssertTrue(text(containing: "3 copies").waitForExistence(timeout: 5))101 XCTAssertTrue(text(containing: "shares storage").exists)102 attach("duplicates")103 go("similar", analyze: true)104 XCTAssertTrue(element("similar.list").waitForExistence(timeout: 40))105 XCTAssertTrue(text(containing: "similar images").exists)106 attach("similar-images")107 }108109 func testCachesDeveloperDownloadsAndTrashLists() {110 launch(section: "caches", scan: false)111 XCTAssertTrue(element("caches.list").waitForExistence(timeout: 20))112 XCTAssertTrue(text(containing: "Google Chrome").waitForExistence(timeout: 5))113 XCTAssertFalse(element("selection.review").exists, "nothing may be preselected")114 go("developer", scan: false)115 XCTAssertTrue(element("developer.list").waitForExistence(timeout: 20))116 XCTAssertTrue(text(containing: "Atlas-bqxmfldzjgheuz").waitForExistence(timeout: 5))117 go("downloads", scan: false)118 XCTAssertTrue(element("downloads.list").waitForExistence(timeout: 20))119 XCTAssertTrue(text(containing: "Sketcher-4.2.dmg").waitForExistence(timeout: 5))120 go("trash", scan: false)121 XCTAssertTrue(element("trash.list").waitForExistence(timeout: 20))122 XCTAssertTrue(text(containing: "Old presentation.key").waitForExistence(timeout: 5))123 }124125 func testUninstallPreviewCancelThenRealCleanupAndPutBack() {126 let appPath = Self.home + "/Applications/Notably.app"127 XCTAssertTrue(FileManager.default.fileExists(atPath: appPath))128 launch(section: "applications", scan: false)129 XCTAssertTrue(element("apps.list").waitForExistence(timeout: 20))130 tap(text(containing: "Notably"))131 let select = element("apps.selectUninstall")132 XCTAssertTrue(select.waitForExistence(timeout: 10))133 let enabled = NSPredicate(format: "isEnabled == true")134 wait(for: [expectation(for: enabled, evaluatedWith: select)], timeout: 15)135 tap(select)136 if !element("selection.review").waitForExistence(timeout: 5) { print("HIERARCHY\n" + app.debugDescription) }137 XCTAssertTrue(element("selection.review").exists)138 XCTAssertTrue(FileManager.default.fileExists(atPath: appPath), "selecting must not remove anything")139 tap(element("selection.review"))140 XCTAssertTrue(element("preview.list").waitForExistence(timeout: 5))141 XCTAssertTrue(text(containing: "Applications/Notably.app").exists)142 attach("cleanup-preview")143 tap(element("preview.cancel"))144 XCTAssertTrue(FileManager.default.fileExists(atPath: appPath), "cancelling the preview must not remove anything")145 tap(element("selection.review"))146 XCTAssertTrue(element("preview.confirm").waitForExistence(timeout: 5))147 tap(element("preview.confirm"))148 XCTAssertTrue(element("results.list").waitForExistence(timeout: 20))149 XCTAssertTrue(text(containing: "Moved to Trash").exists)150 XCTAssertFalse(FileManager.default.fileExists(atPath: appPath), "the confirmed item must be gone from its location")151 attach("cleanup-results")152 tap(app.buttons["Put Back"].firstMatch)153 XCTAssertTrue(text(containing: "Put back").waitForExistence(timeout: 5))154 tap(element("results.done"))155 XCTAssertFalse(element("selection.review").exists)156 }157158 func testReportExportWritesAFile() throws {159 launch(section: "reports")160 XCTAssertTrue(element("reports.preview").waitForExistence(timeout: 20))161 tap(element("reports.export"))162 let found = NSPredicate { _, _ in ((try? FileManager.default.contentsOfDirectory(atPath: Self.exports)) ?? []).contains { $0.hasSuffix(".pdf") } }163 wait(for: [expectation(for: found, evaluatedWith: nil)], timeout: 10)164 let file = try XCTUnwrap(try FileManager.default.contentsOfDirectory(atPath: Self.exports).first { $0.hasSuffix(".pdf") })165 let data = try Data(contentsOf: URL(fileURLWithPath: Self.exports + "/" + file))166 XCTAssertEqual(String(decoding: data.prefix(5), as: UTF8.self), "%PDF-")167 attach("reports")168 }169170 func testActionStopsWhenTheReviewIsDeclined() {171 let cache = Self.home + "/Library/Caches/io.renderly.app"172 launch(section: "actions", scan: false)173 let run = element("actions.run.builtin.caches")174 XCTAssertTrue(run.waitForExistence(timeout: 10))175 tap(run)176 XCTAssertTrue(element("preview.list").waitForExistence(timeout: 30))177 tap(element("preview.cancel"))178 XCTAssertTrue(text(containing: "Nothing was removed").waitForExistence(timeout: 10))179 XCTAssertTrue(FileManager.default.fileExists(atPath: cache))180 }181182 func testSettingsLightModeHistoryAndWindowResize() {183 launch(section: "history", extra: ["-ORIXAppearance", "light"])184 XCTAssertTrue(text(containing: "Cleanup history").waitForExistence(timeout: 10))185 attach("history-light")186 app.typeKey(",", modifierFlags: .command)187 XCTAssertTrue(text(containing: "Storage map colours").waitForExistence(timeout: 5))188 app.typeKey("w", modifierFlags: .command)189 // Layout at the minimum size and at a large size (the window is really resized by the app at launch).190 for (size, minWidth) in [("980x620", 970.0), ("1800x1100", 1500.0)] {191 app.terminate()192 launch(section: "map", extra: ["-ORIXWindowSize", size])193 XCTAssertTrue(element("map.canvas").waitForExistence(timeout: 20))194 let frame = app.windows.firstMatch.frame195 XCTAssertGreaterThanOrEqual(Double(frame.width), minWidth)196 if size == "980x620" { XCTAssertLessThanOrEqual(Double(frame.width), 1000) }197 XCTAssertTrue(element("map.search").exists)198 XCTAssertTrue(element("toolbar.scanFolder").exists)199 attach("map-" + size)200 }201 }202}203