import CoreGraphics import Foundation import ParcelCore import Synchronization import Testing @testable import ParcelEngine @testable import ParcelPlatform @Suite struct NativeObservationInventoryTests { private func candidate( pid: Int32 = 100, bundle: String = "test.document", policy: AppActivationPolicy? = .regular, bundleURL: URL? = nil, launch: Date? = nil ) -> NativeObservationCandidate { NativeObservationCandidate( info: NativeApplicationInfo(id: AppID(pid: pid, bundleID: bundle), launchIdentity: launch), activationPolicy: policy, bundleURL: bundleURL) } @Test(arguments: [AppActivationPolicy.regular, .accessory]) func documentApplicationsNeedRealWindows(policy: AppActivationPolicy) { let app = candidate(policy: policy) var inventory = NativeObservationInventory() #expect(inventory.update(candidates: [app], windowOwners: []).isEmpty) #expect(inventory.applications.isEmpty) #expect(inventory.update(candidates: [app], windowOwners: [100]).map(\.id) == [app.info.id]) } @Test(arguments: [AppActivationPolicy.regular, .accessory]) func freshLaunchIsObservedBeforeItsFirstBackgroundWindow(policy: AppActivationPolicy) { let app = candidate(policy: policy) var inventory = NativeObservationInventory() #expect(inventory.update(candidates: [app], windowOwners: []).isEmpty) #expect(inventory.admitLaunch(app)?.id == app.info.id) #expect(inventory.update(candidates: [app], windowOwners: []).isEmpty) #expect(inventory.applications[app.info.id] != nil) #expect(inventory.update(candidates: [app], windowOwners: [100]).isEmpty) #expect(inventory.update(candidates: [], windowOwners: []).isEmpty) #expect(inventory.applications.isEmpty) } @Test(arguments: ["prohibited", "unknown", "system-agent", "web-service", "xpc"]) func launchNotificationDoesNotAdmitIneligibleApplications(_ kind: String) { let app: NativeObservationCandidate = switch kind { case "prohibited": candidate(policy: .prohibited) case "unknown": candidate(policy: nil) case "system-agent": candidate(bundle: "com.apple.dock", policy: .accessory) case "web-service": candidate(bundle: "com.apple.WebKit.WebContent") default: candidate(bundleURL: URL(fileURLWithPath: "/Applications/Host.app/Contents/Service.xpc")) } var inventory = NativeObservationInventory() #expect(inventory.admitLaunch(app) == nil) #expect(inventory.applications.isEmpty) } @Test(arguments: [AppActivationPolicy.prohibited, nil]) func prohibitedAndUnknownPoliciesAreNotObserved(policy: AppActivationPolicy?) { let app = candidate(policy: policy) var inventory = NativeObservationInventory() #expect(inventory.update(candidates: [app], windowOwners: [100]).isEmpty) #expect(inventory.applications.isEmpty) } @Test(arguments: [ "com.apple.universalcontrol", "com.apple.dock", "com.apple.WindowManager", "com.apple.OSDUIHelper", "com.apple.controlcenter", "com.apple.systemuiserver", ]) func systemAgentsAreNotDocumentOwners(bundle: String) { let app = candidate(bundle: bundle, policy: .accessory) #expect(!app.canObserve) var inventory = NativeObservationInventory() #expect(inventory.admitLaunch(app) == nil) #expect(inventory.update(candidates: [app], windowOwners: [100]).isEmpty) } @Test(arguments: [ "com.apple.WebKit.GPU", "com.apple.WebKit.Networking", "com.apple.WebKit.WebContent", "com.apple.WebKit.WebContent.Development", "test.host.WebKit.GPU", ]) func webServicesAreNotObserved(bundle: String) { #expect(!candidate(bundle: bundle).canObserve) } @Test(arguments: [ "com.apple.finder", "test.document", "test.webkit.gpumonitor", "test.webkit.networkingtool", "test.webcontent", "test.helper", ]) func unrelatedBundlesRemainEligible(bundle: String) { #expect(candidate(bundle: bundle).canObserve) } @Test(arguments: [ "/Applications/Document.app/Contents/XPCServices/Service.xpc", "/Applications/Document.app/Contents/XPCServices/Service.xpc/Contents/Helper.app", ]) func xpcServiceBundlesAreNotObserved(path: String) { #expect(!candidate(bundleURL: URL(fileURLWithPath: path)).canObserve) } @Test func ordinaryEmbeddedApplicationsRemainEligible() { #expect( candidate(bundleURL: URL(fileURLWithPath: "/Applications/Document.app/Contents/Helpers/Tool.app")) .canObserve) } @Test func deferredApplicationArrivesExactlyOnceAndKeepsItsObserverAfterClosing() { let app = candidate() var inventory = NativeObservationInventory() #expect(inventory.update(candidates: [app], windowOwners: []).isEmpty) #expect(inventory.update(candidates: [app], windowOwners: [100]).count == 1) #expect(inventory.update(candidates: [app], windowOwners: [100]).isEmpty) #expect(inventory.update(candidates: [app], windowOwners: []).isEmpty) #expect(inventory.applications.keys.contains(app.info.id)) #expect(inventory.update(candidates: [app], windowOwners: [100]).isEmpty) #expect(inventory.update(candidates: [], windowOwners: []).isEmpty) #expect(inventory.applications.isEmpty) } @Test func launchIdentityAndBundleChangesCannotRetainAnOldObserver() { let original = candidate(launch: Date(timeIntervalSince1970: 1)) var inventory = NativeObservationInventory() #expect(inventory.update(candidates: [original], windowOwners: [100]).count == 1) let replacement = candidate(launch: Date(timeIntervalSince1970: 2)) #expect(inventory.update(candidates: [replacement], windowOwners: []).isEmpty) #expect(inventory.applications.isEmpty) #expect(inventory.update(candidates: [replacement], windowOwners: [100]).count == 1) let other = candidate(bundle: "test.other") #expect(inventory.update(candidates: [other], windowOwners: []).isEmpty) #expect(inventory.applications.isEmpty) } @Test func resetAllowsFreshAdmission() { let app = candidate() var inventory = NativeObservationInventory() #expect(inventory.update(candidates: [app], windowOwners: [100]).count == 1) inventory.reset() #expect(inventory.applications.isEmpty) #expect(inventory.update(candidates: [app], windowOwners: [100]).count == 1) } @Test(arguments: [CGRect.zero, CGRect(x: 0, y: 0, width: 0, height: 40), CGRect(x: 0, y: 0, width: 40, height: 0)]) func zeroAreaPlaceholdersDoNotAuthorizeAX(bounds: CGRect) { let window = WindowServer.Window(number: 1, pid: 100, bounds: bounds, layer: 0, isOnscreen: false) #expect(WindowServer.windowOwners([window]).isEmpty) } @Test func ownerGeometryIncludesOffscreenDocumentsButNotOtherLayers() { let bounds = CGRect(x: -800, y: -600, width: 400, height: 300) let windows = [ WindowServer.Window(number: 1, pid: 100, bounds: bounds, layer: 0, isOnscreen: false), WindowServer.Window(number: 2, pid: 200, bounds: bounds, layer: 0, isOnscreen: true), WindowServer.Window(number: 3, pid: 300, bounds: bounds, layer: 25, isOnscreen: true), ] #expect(WindowServer.windowOwners(windows) == [100, 200]) } @MainActor @Test func backendDefersAXThenRechecksTheNativeCatalogOnActivity() async throws { let app = candidate(policy: .accessory) let windows = Mutex<[WindowServer.Window]>([]) let backend = SystemNativeEngineBackend( observationCandidates: { [app] }, readWindows: { windows.withLock { $0 } }) _ = try await backend.visibleWindows() #expect(backend.runningApplications().isEmpty) #expect(throws: EnginePlatformFailure.appUnavailable(app.info.id)) { try backend.application(app.info.id) } windows.withLock { $0 = [ WindowServer.Window( number: 1, pid: 100, bounds: CGRect(x: 0, y: 0, width: 400, height: 300), layer: 0, isOnscreen: false) ] } _ = try await backend.visibleWindows() #expect(backend.runningApplications().map(\.id) == [app.info.id]) backend.stop() #expect(backend.runningApplications().isEmpty) } @MainActor @Test func backendLaunchAdmissionRequiresTheCurrentEligibleProcessIdentity() async throws { let launch = Date(timeIntervalSince1970: 2) let app = candidate(launch: launch) let candidates = Mutex<[NativeObservationCandidate]>([app]) let backend = SystemNativeEngineBackend( observationCandidates: { candidates.withLock { $0 } }, readWindows: { [] }) defer { backend.stop() } _ = try await backend.visibleWindows() #expect(backend.runningApplications().isEmpty) #expect(backend.admitLaunchedApplication(app.info.id, launchIdentity: Date(timeIntervalSince1970: 1)) == nil) #expect( backend.admitLaunchedApplication(AppID(pid: 101, bundleID: app.info.id.bundleID), launchIdentity: launch) == nil) #expect(backend.admitLaunchedApplication(app.info.id, launchIdentity: launch)?.id == app.info.id) _ = try await backend.visibleWindows() #expect(backend.runningApplications().map(\.id) == [app.info.id]) candidates.withLock { $0 = [] } #expect(backend.runningApplications().isEmpty) } @MainActor @Test func runningInventoryRevalidatesTerminationAndPIDReuseBeforeAX() async throws { let original = candidate(launch: Date(timeIntervalSince1970: 1)) let candidates = Mutex<[NativeObservationCandidate]>([original]) let bounds = CGRect(x: 0, y: 0, width: 400, height: 300) let backend = SystemNativeEngineBackend( observationCandidates: { candidates.withLock { $0 } }, readWindows: { [WindowServer.Window(number: 1, pid: 100, bounds: bounds, layer: 0, isOnscreen: true)] }) _ = try await backend.visibleWindows() #expect(backend.runningApplications().count == 1) candidates.withLock { $0 = [] } #expect(backend.runningApplications().isEmpty) #expect(throws: EnginePlatformFailure.appUnavailable(original.info.id)) { try backend.application(original.info.id) } let replacement = candidate(launch: Date(timeIntervalSince1970: 2)) candidates.withLock { $0 = [replacement] } #expect(backend.runningApplications().isEmpty) _ = try await backend.visibleWindows() #expect(backend.runningApplications().first?.launchIdentity == replacement.info.launchIdentity) } @MainActor @Test func windowServerFailureIsNotAnEmptyInventoryFallback() async { let backend = SystemNativeEngineBackend( observationCandidates: { [] }, readWindows: { throw WindowServer.ReadError.windowListUnavailable }) await #expect(throws: WindowServer.ReadError.windowListUnavailable) { try await backend.visibleWindows() } } }