Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 86 additions & 17 deletions HiddenTests/AppStoreMigrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,29 +184,98 @@ final class AppStoreMigrationTests: XCTestCase {
// MARK: move into /Applications

func testApplicationsFolderDetection() {
let home = URL(fileURLWithPath: "/Users/x")
XCTAssertTrue(MoveToApplications.isInApplicationsFolder(URL(fileURLWithPath: "/Applications/Hidden Bar.app"), home: home))
XCTAssertTrue(MoveToApplications.isInApplicationsFolder(URL(fileURLWithPath: "/Applications/Utilities/Hidden Bar.app"), home: home))
XCTAssertTrue(MoveToApplications.isInApplicationsFolder(URL(fileURLWithPath: "/Users/x/Applications/Hidden Bar.app"), home: home))
XCTAssertFalse(MoveToApplications.isInApplicationsFolder(URL(fileURLWithPath: "/Users/x/Downloads/Hidden Bar.app"), home: home))
XCTAssertFalse(MoveToApplications.isInApplicationsFolder(URL(fileURLWithPath: "/ApplicationsOld/Hidden Bar.app"), home: home))
XCTAssertTrue(MoveToApplications.isInApplicationsFolder(URL(fileURLWithPath: "/Applications/Hidden Bar.app")))
XCTAssertTrue(MoveToApplications.isInApplicationsFolder(URL(fileURLWithPath: "/Applications/Utilities/Hidden Bar.app")))
XCTAssertFalse(MoveToApplications.isInApplicationsFolder(URL(fileURLWithPath: "/Users/x/Applications/Hidden Bar.app")),
"macOS 27 hides the arrow of a copy in ~/Applications")
XCTAssertFalse(MoveToApplications.isInApplicationsFolder(URL(fileURLWithPath: "/Users/x/Downloads/Hidden Bar.app")))
XCTAssertFalse(MoveToApplications.isInApplicationsFolder(URL(fileURLWithPath: "/ApplicationsOld/Hidden Bar.app")))
XCTAssertTrue(MoveToApplications.isTranslocated(URL(fileURLWithPath: "/private/var/folders/ab/T/AppTranslocation/1234/d/Hidden Bar.app")))
XCTAssertEqual(MoveToApplications.destination(for: URL(fileURLWithPath: "/Users/x/Downloads/Hidden Bar.app")).path,
"/Applications/Hidden Bar.app")
}

func testMoveOfferRules() {
let home = URL(fileURLWithPath: "/Users/x")
let downloads = URL(fileURLWithPath: "/Users/x/Downloads/Hidden Bar.app")
func offer(_ url: URL = downloads, answered: Bool = false, masAtDestination: Bool = false, writable: Bool = true) -> Bool {
MoveToApplications.shouldOffer(runningBundle: url, home: home, alreadyAnswered: answered,
destinationIsAppStoreCopy: masAtDestination, applicationsWritable: writable)
// Fake answers: the plan reads no disk, so writability and the App Store copy are parameters.
private func plan(_ running: String = "/Users/x/Downloads/Hidden Bar.app", answered: Bool = false,
writable: Bool = true, masInApplications: Bool = false) -> MoveToApplications.Plan {
MoveToApplications.plan(runningBundle: URL(fileURLWithPath: running), alreadyAnswered: answered,
applicationsWritable: writable, applicationsHoldsAppStoreCopy: masInApplications)
}

func testMoveGoesToApplicationsWhenItCan() {
XCTAssertEqual(plan(), .moveToApplications(URL(fileURLWithPath: "/Applications/Hidden Bar.app")))
XCTAssertEqual(plan("/Users/x/Applications/Hidden Bar.app"), .moveToApplications(URL(fileURLWithPath: "/Applications/Hidden Bar.app")))
XCTAssertEqual(plan("/Applications/Hidden Bar.app"), MoveToApplications.Plan.none)
XCTAssertEqual(plan(answered: true), MoveToApplications.Plan.none, "offered once")
}

/// Dead end: "Not now" to the Trash left the App Store copy in /Applications
/// and the move offer never showed, nor anything else.
func testKeptAppStoreCopyInApplicationsExplainsWhatToDo() {
XCTAssertEqual(plan(masInApplications: true), .explainAppStoreCopyKept)
XCTAssertEqual(plan(answered: true, masInApplications: true), MoveToApplications.Plan.none)
}

/// Dead end: a standard account cannot write /Applications.
func testUnwritableApplicationsExplainsWhatToDo() {
XCTAssertEqual(plan(writable: false), .explainNotWritable)
XCTAssertEqual(plan(writable: false, masInApplications: true), .explainNotWritable)
}

/// The move check reads the real folder: a fake /Applications on disk.
func testMovePlanReadsTheFolderAndTheKeptAppStoreCopy() throws {
let downloads = try makeBundle("Downloads/Hidden Bar.app", bundleID: bundleID, appStore: false)
let system = root.appendingPathComponent("SystemApplications")
try FileManager.default.createDirectory(at: system, withIntermediateDirectories: true)
func migration() -> AppStoreMigration {
AppStoreMigration(environment: .init(home: root, bundleURL: downloads, bundleIdentifier: bundleID,
defaults: defaults, registeredDefaults: registered,
systemApplications: system))
}
XCTAssertTrue(offer())
XCTAssertFalse(offer(URL(fileURLWithPath: "/Applications/Hidden Bar.app")))
XCTAssertFalse(offer(answered: true), "offered once")
XCTAssertFalse(offer(masAtDestination: true), "the Trash offer handles the App Store copy first")
XCTAssertFalse(offer(writable: false))
XCTAssertEqual(migration().movePlan, .moveToApplications(system.appendingPathComponent("Hidden Bar.app")))

_ = try makeBundle("SystemApplications/Hidden Bar.app", bundleID: bundleID, appStore: true)
XCTAssertEqual(migration().movePlan, .explainAppStoreCopyKept)

// The Trash answer does not matter.
MigrationOfferLedger(defaults: defaults).recordAnswer(MigrationOfferLedger.trashKey)
XCTAssertEqual(migration().movePlan, .explainAppStoreCopyKept)
}

// MARK: another copy already running

private let downloadsApp = URL(fileURLWithPath: "/Users/x/Downloads/Hidden Bar.app")

private func instance(_ pid: pid_t, _ path: String, appStore: Bool) -> OtherRunningCopy.Instance {
.init(pid: pid, bundleURL: URL(fileURLWithPath: path), isAppStoreBuild: appStore)
}

/// Dead end: the App Store copy runs at login and the direct build quit silently.
func testRunningAppStoreCopyIsOfferedAQuit() {
let others = [instance(41, "/Applications/Hidden Bar.app", appStore: true)]
XCTAssertEqual(OtherRunningCopy.decide(others: others, runningBundle: downloadsApp), .offerToReplaceAppStoreCopy(41))
}

func testRunningDirectCopyStillWinsSilently() {
XCTAssertEqual(OtherRunningCopy.decide(others: [instance(42, "/Applications/Hidden Bar.app", appStore: false)],
runningBundle: downloadsApp), .quitInFavorOf(42))
XCTAssertEqual(OtherRunningCopy.decide(others: [instance(41, "/Applications/Hidden Bar.app", appStore: true),
instance(42, "/Users/x/Applications/Hidden Bar.app", appStore: false)],
runningBundle: downloadsApp), .quitInFavorOf(42),
"a direct copy is already running; nothing to switch")
XCTAssertEqual(OtherRunningCopy.decide(others: [], runningBundle: downloadsApp), .launch)
}

func testDebugRulesOnlyCountTheSamePathUnlessForced() {
let installed = instance(42, "/Applications/Hidden Bar.app", appStore: false)
let appStore = instance(41, "/Applications/Hidden Bar.app", appStore: true)
let relaunch = instance(43, downloadsApp.path, appStore: false)
XCTAssertEqual(OtherRunningCopy.decide(others: [installed, appStore], runningBundle: downloadsApp,
countsOtherPaths: false, countsAppStoreCopies: false), .launch)
XCTAssertEqual(OtherRunningCopy.decide(others: [installed, appStore], runningBundle: downloadsApp,
countsOtherPaths: false, countsAppStoreCopies: true), .offerToReplaceAppStoreCopy(41))
XCTAssertEqual(OtherRunningCopy.decide(others: [appStore, relaunch], runningBundle: downloadsApp,
countsOtherPaths: false, countsAppStoreCopies: true), .quitInFavorOf(43))
}
}

Expand Down
5 changes: 5 additions & 0 deletions hidden/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ class AppDelegate: NSObject, NSApplicationDelegate{
/// If another instance with the same bundle ID is already running, activate it
/// and terminate this one instead.
private func ensureSingleInstance() -> Bool {
#if HIDDENBAR_NATIVE_VISIBILITY
// The direct build asks before quitting an App Store copy that is running.
return AppStoreMigration.shared.resolveOtherRunningCopy()
#else
let current = ProcessInfo.processInfo.processIdentifier
let existing = NSWorkspace.shared.runningApplications.first {
guard $0.bundleIdentifier == Bundle.main.bundleIdentifier,
Expand All @@ -79,6 +83,7 @@ class AppDelegate: NSObject, NSApplicationDelegate{
existing.activate(options: [.activateAllWindows])
NSApp.terminate(nil)
return false
#endif
}

func openPreferencesIfNeeded() {
Expand Down
Loading