Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315import CoreGraphicsimport Foundationimport Testing
@testable import ParcelPlatform
struct WindowFrameReadbackTests { let frame = CGRect(x: -900, y: 58, width: 890, height: 846) var write: AppThreadFrameWrite { AppThreadFrameWrite(windowID: 42, target: frame, generation: 1, phase: .final) }
@Test(arguments: [false, true]) func onlyTheOwnedCGFrameIsReadIncludingHiddenStackMembers(onscreen: Bool) throws { var calls = 0 let actual = try AppThreadWindowFrameReadback.read(windowID: 42, pid: 123) { options, id in #expect(options == .optionIncludingWindow && id == 42) calls += 1 return [.init(number: 42, pid: 123, bounds: frame, layer: 0, isOnscreen: onscreen)] } #expect(actual == frame && calls == 1) }
@Test func anAlreadyCommittedBatchNeedsOnlyOnePublicationWake() async throws { var reads = 0 var pauses = 0 let actual = try await AppThreadWindowFrameReadback.settled(writes: [write], pid: 123, pause: { pauses += 1 }) { options, id in #expect(options == .optionIncludingWindow && id == 42) reads += 1 return [.init(number: 42, pid: 123, bounds: frame, layer: 0, isOnscreen: true)] } #expect(actual.frames == [42: frame] && actual.failures.isEmpty && reads == 1 && pauses == 1) }
@Test(arguments: [ CGRect(x: 160, y: 57, width: 1280, height: 797), CGRect(x: 180, y: 200, width: 1332, height: 274), CGRect(x: -900, y: 57, width: 890, height: 846), ]) func finalReadbackWaitsForPublicationRatherThanLearningAnIntermediateFrame(old: CGRect) async throws { let samples = [old, old, old, frame] var reads = 0 var pauses = 0 let actual = try await AppThreadWindowFrameReadback.settled(writes: [write], pid: 123, pause: { pauses += 1 }) { _, _ in let current = samples[min(reads, samples.count - 1)] reads += 1 return [.init(number: 42, pid: 123, bounds: current, layer: 0, isOnscreen: true)] } #expect(actual.frames == [42: frame] && actual.failures.isEmpty && reads == 4 && pauses == 4) }
@Test func aStableRefusalReturnsRealityOnlyAfterTheBoundedPublicationWindow() async throws { var pauses = 0 let expected = AppThreadFrameWrite(windowID: 42, target: .zero, generation: 1, phase: .final) let actual = try await AppThreadWindowFrameReadback.settled( writes: [expected], pid: 123, pause: { pauses += 1 } ) { _, _ in [.init(number: 42, pid: 123, bounds: frame, layer: 0, isOnscreen: true)] } #expect(actual.frames == [42: frame] && actual.failures.isEmpty && pauses == 6) }
@Test func continuingMotionFailsInsteadOfPretendingAnIntermediateFrameIsFinal() async throws { var reads = 0 let result = try await AppThreadWindowFrameReadback.settled(writes: [write], pid: 123, pause: {}) { _, _ in reads += 1 return [ .init( number: 42, pid: 123, bounds: CGRect(x: reads, y: 0, width: 100, height: 100), layer: 0, isOnscreen: true) ] } #expect(result.frames.isEmpty && reads == 6) #expect(result.failures[42] as? AppThreadWindowFrameReadbackError == .unsettled(42)) }
@Test func emptyAndDuplicateBatchesNeverStartVerification() async throws { let empty = try await AppThreadWindowFrameReadback.settled( writes: [], pid: 123, pause: { Issue.record("Unexpected delay") } ) { _, _ in Issue.record("Unexpected CG read") return [] } #expect(empty.frames.isEmpty && empty.failures.isEmpty) await #expect(throws: AppThreadFrameWriteError.duplicateWindow(42)) { try await AppThreadWindowFrameReadback.settled( writes: [write, write], pid: 123, pause: { Issue.record("Unexpected delay") } ) { _, _ in [] } } }
@Test func unavailableCGReadDoesNotFallBackToAccessibility() async throws { #expect(throws: WindowServer.ReadError.windowListUnavailable) { try AppThreadWindowFrameReadback.read(windowID: 42, pid: 123) { _, _ in throw WindowServer.ReadError.windowListUnavailable } } #expect(throws: AppThreadFailure.windowNotFound(42)) { try AppThreadWindowFrameReadback.read(windowID: 42, pid: 123) { _, _ in [] } } let result = try await AppThreadWindowFrameReadback.settled(writes: [write], pid: 123, pause: {}) { _, _ in throw WindowServer.ReadError.windowListUnavailable } #expect(result.frames.isEmpty) #expect(result.failures[42] as? WindowServer.ReadError == .windowListUnavailable) }
@Test func cancellationDoesNotReadOrPublishFramesAfterTheWakeFails() async { await #expect(throws: CancellationError.self) { try await AppThreadWindowFrameReadback.settled( writes: [write], pid: 123, pause: { throw CancellationError() } ) { _, _ in Issue.record("Read after cancellation") return [] } } }
@Test func aCancelledQueryCannotBecomeAWindowFailure() async { await #expect(throws: CancellationError.self) { try await AppThreadWindowFrameReadback.settled(writes: [write], pid: 123, pause: {}) { _, _ in throw CancellationError() } } }
@Test func onlyPendingBatchIdentitiesAreQueriedIncludingHiddenMembers() async throws { let second = CGRect(x: 2, y: 3, width: 4, height: 5) let other = AppThreadFrameWrite(windowID: 43, target: second, generation: 1, phase: .final) var queried: [CGWindowID] = [] let result = try await AppThreadWindowFrameReadback.settled(writes: [write, other], pid: 123, pause: {}) { options, id in #expect(options == .optionIncludingWindow) queried.append(id) return [.init(number: id, pid: 123, bounds: id == 42 ? frame : second, layer: 0, isOnscreen: id == 42)] } #expect(result.frames == [42: frame, 43: second] && result.failures.isEmpty) #expect(queried == [42, 43]) }
@Test(arguments: [false, true]) func aVanishedSiblingDoesNotDiscardSuccessfulReadback(vanishesAfterPublication: Bool) async throws { let other = AppThreadFrameWrite(windowID: 43, target: frame, generation: 1, phase: .final) var reads: [CGWindowID: Int] = [:] let result = try await AppThreadWindowFrameReadback.settled(writes: [write, other], pid: 123, pause: {}) { _, id in reads[id, default: 0] += 1 if id == 43 { if !vanishesAfterPublication || reads[id, default: 0] > 1 { return [] } return [.init(number: id, pid: 123, bounds: frame.offsetBy(dx: 4, dy: 0), layer: 0, isOnscreen: true)] } return [.init(number: id, pid: 123, bounds: frame, layer: 0, isOnscreen: true)] } #expect(result.frames == [42: frame]) #expect(result.failures[43] as? AppThreadFailure == .windowNotFound(43)) #expect(reads[42] == 1 && reads[43] == (vanishesAfterPublication ? 2 : 1)) }
@Test func movingAndFailedWindowsRetainSiblingFramesAcrossRealPublicationDelays() async throws { let moving = AppThreadFrameWrite(windowID: 43, target: frame, generation: 1, phase: .final) let failed = AppThreadFrameWrite(windowID: 44, target: frame, generation: 1, phase: .final) var reads: [CGWindowID: Int] = [:] let started = ContinuousClock.now let result = try await AppThreadWindowFrameReadback.settled(writes: [write, moving, failed], pid: 123) { _, id in reads[id, default: 0] += 1 if id == 44 { throw WindowServer.ReadError.windowListUnavailable } let current = id == 42 ? frame : frame.offsetBy(dx: CGFloat(reads[id, default: 0]), dy: 0) return [.init(number: id, pid: 123, bounds: current, layer: 0, isOnscreen: true)] } #expect(started.duration(to: .now) >= .milliseconds(120)) #expect(result.frames == [42: frame]) #expect(result.failures[43] as? AppThreadWindowFrameReadbackError == .unsettled(43)) #expect(result.failures[44] as? WindowServer.ReadError == .windowListUnavailable) #expect(reads == [42: 1, 43: 6, 44: 1]) }
@Test func settledRefusalIsNotDiscardedWhenASiblingKeepsMoving() async throws { let refusal = AppThreadFrameWrite( windowID: 42, target: frame.offsetBy(dx: -5, dy: 0), generation: 1, phase: .final) let moving = AppThreadFrameWrite(windowID: 43, target: frame, generation: 1, phase: .final) var reads = 0 let result = try await AppThreadWindowFrameReadback.settled(writes: [refusal, moving], pid: 123, pause: {}) { _, id in if id == 43 { reads += 1 } return [ .init( number: id, pid: 123, bounds: id == 42 ? frame : frame.offsetBy(dx: CGFloat(reads), dy: 0), layer: 0, isOnscreen: true) ] } #expect(result.frames == [42: frame]) #expect(result.failures[43] as? AppThreadWindowFrameReadbackError == .unsettled(43)) }
@Test func failedFinalsAndUnchangedReceiptsNeverQueryTheCompositor() async throws { let failed = AppThreadFrameWrite(windowID: 43, target: frame, generation: 1, phase: .final) let stale = AppThreadFrameWrite(windowID: 44, target: frame, generation: 1, phase: .final) let result = try await AppThreadWindowFrameReadback.verified( writes: [write, failed, stale], receipts: [ .init(write, before: frame, actual: frame, error: nil), .init(failed, before: frame.offsetBy(dx: 1, dy: 0), actual: frame, error: AXPlatformError.notPermitted), .init(stale, before: nil, actual: nil, error: AppThreadFrameWriteError.staleGeneration(44, 1)), ], pid: 123, pause: { Issue.record("Unexpected publication wake") } ) { _, _ in Issue.record("Unexpected compositor query") return [] } #expect(result.frames == [42: frame] && result.failures.isEmpty) }
@Test func anAXReplyMatchingTheTargetStillNeedsPublicationWhenTheBeforeFrameDiffered() async throws { var queried: [CGWindowID] = [] let result = try await AppThreadWindowFrameReadback.verified( writes: [write], receipts: [.init(write, before: frame.offsetBy(dx: 1, dy: 0), actual: frame, error: nil)], pid: 123, pause: {} ) { _, id in queried.append(id) return [.init(number: id, pid: 123, bounds: frame, layer: 0, isOnscreen: true)] } #expect(result.frames == [42: frame] && queried == [42]) }
@MainActor @Test func compositorQueryLeavesTheMainActorAndRequestsOnlyOneIdentity() async throws { let rows = try await WindowServer().frameReadbackWindows(windowID: 42) { options, id in #expect(!Thread.isMainThread) #expect(options == .optionIncludingWindow && id == 42) return [] } #expect(rows.isEmpty) }
@Test(arguments: [1, 2, 3, 4, 5, 6]) func ownershipIsCheckedAgainAfterEveryPublicationWake(recycledAt: Int) async throws { var pauses = 0 var reads = 0 let result = try await AppThreadWindowFrameReadback.settled(writes: [write], pid: 123, pause: { pauses += 1 }) { _, _ in reads += 1 return [ .init( number: 42, pid: pauses >= recycledAt ? 124 : 123, bounds: frame.offsetBy(dx: 0, dy: -1), layer: 0, isOnscreen: false) ] } #expect(result.frames.isEmpty) guard let failure = result.failures[42] as? AppThreadFailure, case .windowIdentification = failure else { Issue.record("Recycled window ownership must retain its typed failure") return } #expect(pauses == recycledAt && reads == recycledAt) }
@Test func positionCorrectionUsesMeasuredCoordinatesWithoutRepeatingTheSizeWrite() throws { let desired = CGRect(x: 20, y: 58, width: 890, height: 846) let accepted = CGRect(x: 160, y: 57, width: 890, height: 846) let accessibility = CGRect(x: 160, y: 58, width: 891, height: 846) #expect( try AppThreadWindowFrameReadback.positionOnlyTarget( desired: desired, accepted: accepted, accessibility: accessibility) == CGRect(x: 20, y: 59, width: 891, height: 846)) #expect( try AppThreadWindowFrameReadback.positionOnlyTarget( desired: desired, accepted: desired, accessibility: accessibility) == nil) }
@Test(arguments: [CGPoint(x: -1, y: 0), CGPoint(x: 0, y: -1), CGPoint(x: 1, y: 1)]) func onePointPositionDriftStillGetsAnAXCoordinateCorrection(offset: CGPoint) throws { let accepted = frame.offsetBy(dx: offset.x, dy: offset.y) let accessibility = CGRect(origin: accepted.origin, size: CGSize(width: 891, height: 846)) #expect( try AppThreadWindowFrameReadback.positionOnlyTarget( desired: frame, accepted: accepted, accessibility: accessibility) == CGRect(origin: frame.origin, size: accessibility.size)) }
@Test func positionCorrectionDoesNotHideSizeRefusalsAndRejectsInvalidGeometry() throws { let oversized = CGRect(x: -920, y: 58, width: 891, height: 846) #expect( try AppThreadWindowFrameReadback.positionOnlyTarget( desired: frame, accepted: oversized, accessibility: oversized) == nil) #expect(throws: AppThreadFailure.self) { try AppThreadWindowFrameReadback.positionOnlyTarget(desired: .zero, accepted: frame, accessibility: frame) } }
@Test(arguments: [41, 42]) func recycledIdentityOrUnexpectedWindowCannotSupplyAFrame(id: Int) { #expect(throws: AppThreadFailure.self) { try AppThreadWindowFrameReadback.read(windowID: 42, pid: 123) { _, _ in [.init(number: CGWindowID(id), pid: 124, bounds: frame, layer: 0, isOnscreen: true)] } } }
@Test func duplicateCGRecordsAreRejected() { let window = WindowServer.Window(number: 42, pid: 123, bounds: frame, layer: 0, isOnscreen: true) #expect(throws: AppThreadFailure.self) { try AppThreadWindowFrameReadback.read(windowID: 42, pid: 123) { _, _ in [window, window] } } }
@Test(arguments: [CGRect.zero, CGRect(x: CGFloat.infinity, y: 0, width: 100, height: 100)]) func unusableFramesRemainVisibleFailures(frame: CGRect) { #expect(throws: AppThreadFailure.self) { try AppThreadWindowFrameReadback.read(windowID: 42, pid: 123) { _, _ in [.init(number: 42, pid: 123, bounds: frame, layer: 0, isOnscreen: true)] } } }}