Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341import ApplicationServicesimport Foundationimport Testing
@testable import ParcelPlatform
@Test(arguments: [ (AXError.apiDisabled, AXPlatformError.notPermitted), (.cannotComplete, .messagingFailure), (.attributeUnsupported, .attributeUnsupported), (.notImplemented, .other(-25208)), (.invalidUIElement, .elementInvalid), (.noValue, .noValue), (.failure, .other(AXError.failure.rawValue)),])func mapsAccessibilityErrors(error: AXError, expected: AXPlatformError) { #expect(AXPlatformError(error) == expected)}
@Test func distinguishesSlowCallsFromOtherMessagingFailures() { #expect(throws: AXPlatformError.timedOut) { try AXElement.checkCall(.cannotComplete, elapsed: 0.35, timeout: 0.35) } #expect(throws: AXPlatformError.messagingFailure) { try AXElement.checkCall(.cannotComplete, elapsed: 0.01, timeout: 0.35) } #expect(throws: Never.self) { try AXElement.checkCall(.success, elapsed: 1, timeout: 0.35) }}
private func batchedAXError(_ error: AXError) throws -> AXValue { var error = error return try #require(AXValueCreate(.axError, &error))}
@Suite struct AXAttributeBatchTests { @Test(arguments: [kAXChildrenAttribute, kAXTabsAttribute]) func oversizedRemoteArraysAreRejectedBeforeTheAppSerializesTheirValues(name: String) { var calls: [String] = [] #expect(throws: NativeTabScanError.limitExceeded) { try AXAttributeArrayReader.read( name: name, limit: 32, count: { calls.append("count"); return 10_000 }, values: { calls.append("values"); return [] as CFArray }) } #expect(calls == ["count"]) }
@Test func boundedArrayReadChecksTheCountAgainWhenTheTreeGrowsDuringTheRead() { var calls: [String] = [] #expect(throws: NativeTabScanError.limitExceeded) { try AXAttributeArrayReader.read( name: kAXChildrenAttribute, limit: 2, count: { calls.append("count"); return 2 }, values: { calls.append("values"); return ["one", "two", "three"] as CFArray }) } #expect(calls == ["count", "values"]) }
@Test func boundedArrayAtTheLimitIsReadAfterTheCountGuard() throws { var calls: [String] = [] let values = try AXAttributeArrayReader.read( name: kAXTabsAttribute, limit: 2, count: { calls.append("count"); return 2 }, values: { calls.append("values"); return ["one", "two"] as CFArray }) #expect(CFArrayGetCount(values) == 2) #expect(calls == ["count", "values"]) }
@Test(arguments: [AXPlatformError.timedOut, .notPermitted, .elementInvalid]) func failedArrayCountDoesNotReadValuesOrPretendTheTreeIsEmpty(error: AXPlatformError) { var reads = 0 #expect(throws: error) { try AXAttributeArrayReader.read( name: kAXChildrenAttribute, limit: 32, count: { throw error }, values: { reads += 1; return [] as CFArray }) } #expect(reads == 0) }
@Test func alignsValuesByAttributeNameAndLeavesUnrequestedAttributesUncached() throws { let title = "Document" as CFString let role = "AXWindow" as CFString let batch = try AXAttributeValues( names: ["AXTitle", "AXRole"], values: [title, role] as CFArray) let matchesTitle = try batch.value("AXTitle").map { CFEqual($0, title) } let matchesRole = try batch.value("AXRole").map { CFEqual($0, role) } #expect(matchesTitle == true) #expect(matchesRole == true) #expect(batch.contains("AXTitle")) #expect(!batch.contains("AXChildren")) #expect(try batch.value("AXChildren") == nil) #expect(batch.callFailure == nil) }
@Test func missingAttributeDoesNotDiscardLaterSuccessfulValues() throws { let unsupported = try batchedAXError(.attributeUnsupported) let title = "Document" as CFString let batch = try AXAttributeValues( names: ["AXCloseButton", "AXTitle"], values: [unsupported, title] as [CFTypeRef] as CFArray) #expect(throws: AXPlatformError.attributeUnsupported) { try batch.value("AXCloseButton") } let matchesTitle = try batch.value("AXTitle").map { CFEqual($0, title) } #expect(matchesTitle == true) #expect(batch.callFailure == nil) }
@Test func nullAndExplicitNoValueBothRemainUnknown() throws { let null = try #require(kCFNull) let noValue = try batchedAXError(.noValue) let batch = try AXAttributeValues( names: ["null", "empty"], values: [null, noValue] as [CFTypeRef] as CFArray) for name in ["null", "empty"] { #expect(throws: AXPlatformError.noValue) { try batch.value(name) } } #expect(batch.callFailure == nil) }
@Test(arguments: [ (AXError.apiDisabled, AXPlatformError.notPermitted), (.invalidUIElement, .elementInvalid), (.cannotComplete, .messagingFailure), (.failure, .other(AXError.failure.rawValue)), ]) func embeddedRealErrorsPropagateAndMarkTheNativeBatchCall(error: AXError, expected: AXPlatformError) throws { let value = try batchedAXError(error) let batch = try AXAttributeValues(names: ["AXRole"], values: [value] as CFArray) #expect(throws: expected) { try batch.value("AXRole") } #expect(batch.callFailure == expected) let activity = AXCallActivity() activity.record(nil) activity.recordBatchFailure(try #require(batch.callFailure)) #expect(activity.results == [expected]) }
@Test func embeddedMessagingFailureUsesTheBatchDeadlineForTimeoutClassification() throws { let error = try batchedAXError(.cannotComplete) let batch = try AXAttributeValues( names: ["AXRole"], values: [error] as CFArray, elapsed: 0.35, timeout: 0.35) #expect(throws: AXPlatformError.timedOut) { try batch.value("AXRole") } #expect(batch.callFailure == .timedOut) }
@Test func errorWrappersAreNotConfusedWithGeometryValues() throws { var size = CGSize(width: 640, height: 480) let value = try #require(AXValueCreate(.cgSize, &size)) let batch = try AXAttributeValues(names: ["AXSize"], values: [value] as CFArray) let matchesSize = try batch.value("AXSize").map { CFEqual($0, value) } #expect(matchesSize == true) #expect(batch.callFailure == nil) }
@Test func malformedBatchShapeDoesNotSilentlyMisalignAttributes() { let values = ["Document" as CFString] as CFArray #expect(throws: AXPlatformError.unexpectedValue("AX attribute batch count")) { try AXAttributeValues(names: ["AXTitle", "AXRole"], values: values) } #expect(throws: AXPlatformError.unexpectedValue("AX attribute batch count")) { try AXAttributeValues(names: ["AXTitle", "AXTitle"], values: ["A", "B"] as CFArray) } }
@Test func successfulErrorWrapperIsRejectedRatherThanUsedAsAnAttribute() throws { let value = try batchedAXError(.success) #expect(throws: AXPlatformError.unexpectedValue("AXRole")) { try AXAttributeValues(names: ["AXRole"], values: [value] as CFArray) } }}
@Test func unchangedFrameHasNoWrites() { let frame = CGRect(x: 10, y: 20, width: 300, height: 200) #expect(FrameWritePlan(current: frame, target: frame).steps.isEmpty)}
@Test func changedFramePlansSizePositionSize() { let old = CGRect(x: 10, y: 20, width: 300, height: 200) let new = CGRect(x: 30, y: 40, width: 500, height: 400) #expect( FrameWritePlan(current: old, target: new).steps == [ .size(new.size), .position(new.origin), .size(new.size), ])}
@Test func movedFrameStillChecksSizeAfterPosition() { let old = CGRect(x: 10, y: 20, width: 300, height: 200) let new = CGRect(x: 30, y: 40, width: 300, height: 200) #expect( FrameWritePlan(current: old, target: new).steps == [ .size(new.size), .position(new.origin), .size(new.size), ])}
@Test func resizedFrameChecksSizeTwice() { let old = CGRect(x: 10, y: 20, width: 300, height: 200) let new = CGRect(x: 10, y: 20, width: 400, height: 400) #expect( FrameWritePlan(current: old, target: new).steps == [ .size(new.size), .position(new.origin), .size(new.size), ])}
private final class FrameWriteProbe: FrameWriteAccess { var rectangle = CGRect(x: 10, y: 20, width: 300, height: 200) var enabled = true var actions: [String] = [] var failDisable = false var failPosition = false var failRestore = false var positionResetsSize = false var sizeChangesPosition = false var disableAppliesBeforeFailure = false
var frame: CGRect { get throws { rectangle } } var size: CGSize { get throws { rectangle.size } } var position: CGPoint { get throws { rectangle.origin } } var enhancedUI: Bool { get throws { enabled } }
func setSize(_ value: CGSize) throws { actions.append("size") rectangle.size = value if sizeChangesPosition { rectangle.origin = CGPoint(x: 99, y: 99) } } func setPosition(_ value: CGPoint) throws { actions.append("position") if failPosition { throw AXPlatformError.notPermitted } rectangle.origin = value if positionResetsSize { rectangle.size = CGSize(width: 300, height: 200) } } func setEnhancedUI(_ value: Bool) throws { actions.append(value ? "enable" : "disable") if !value && failDisable { if disableAppliesBeforeFailure { enabled = false } throw AXPlatformError.timedOut } if value && failRestore { throw AXPlatformError.elementInvalid } enabled = value }}
@Test func frameWriteRestoresEnhancedUIAndRechecksSize() throws { let probe = FrameWriteProbe() probe.positionResetsSize = true let target = CGRect(x: 30, y: 40, width: 500, height: 400) let actual = try executeFrameWrite( target, window: probe, application: probe, assistiveTechnology: AssistiveTechnology(voiceOver: false, switchControl: false) ) #expect(actual == target) #expect(probe.actions == ["disable", "size", "position", "size", "enable"])}
@Test(arguments: [ AssistiveTechnology(voiceOver: true, switchControl: false), AssistiveTechnology(voiceOver: false, switchControl: true),])func frameWriteKeepsEnhancedUIForAssistiveTechnology(assistive: AssistiveTechnology) throws { let probe = FrameWriteProbe() let target = CGRect(x: 30, y: 20, width: 300, height: 200) _ = try executeFrameWrite(target, window: probe, application: probe, assistiveTechnology: assistive) #expect(probe.actions == ["position"]) #expect(probe.enabled)}
@Test func frameWriteRestoresAfterUncertainDisable() { let probe = FrameWriteProbe() probe.failDisable = true probe.disableAppliesBeforeFailure = true #expect(throws: AXPlatformError.timedOut) { try executeFrameWrite( CGRect(x: 30, y: 20, width: 300, height: 200), window: probe, application: probe, assistiveTechnology: AssistiveTechnology(voiceOver: false, switchControl: false) ) } #expect(probe.enabled) #expect(probe.actions == ["disable", "enable"])}
@Test func frameWriteReportsDisableAndRestoreFailures() { let probe = FrameWriteProbe() probe.failDisable = true probe.failRestore = true do { _ = try executeFrameWrite( CGRect(x: 30, y: 20, width: 300, height: 200), window: probe, application: probe, assistiveTechnology: AssistiveTechnology(voiceOver: false, switchControl: false) ) Issue.record("Expected disable and restore failure") } catch let failure as FrameWriteFailure { #expect(failure.writeError as? AXPlatformError == .timedOut) #expect(failure.restoreError as? AXPlatformError == .elementInvalid) } catch { Issue.record("Unexpected failure: \(error)") }}
@Test func frameWriteCorrectsPositionChangedByResize() throws { let probe = FrameWriteProbe() probe.sizeChangesPosition = true let target = CGRect(x: 10, y: 20, width: 500, height: 400) let actual = try executeFrameWrite( target, window: probe, application: probe, assistiveTechnology: AssistiveTechnology(voiceOver: false, switchControl: false) ) #expect(actual == target) #expect(probe.actions == ["disable", "size", "position", "enable"])}
@Test func frameWriteReportsBothWriteAndRestoreFailures() { let probe = FrameWriteProbe() probe.failPosition = true probe.failRestore = true do { _ = try executeFrameWrite( CGRect(x: 30, y: 20, width: 300, height: 200), window: probe, application: probe, assistiveTechnology: AssistiveTechnology(voiceOver: false, switchControl: false) ) Issue.record("Expected frame write failure") } catch let failure as FrameWriteFailure { #expect(failure.writeError as? AXPlatformError == .notPermitted) #expect(failure.restoreError as? AXPlatformError == .elementInvalid) } catch { Issue.record("Unexpected failure: \(error)") } #expect(probe.actions == ["disable", "position", "enable"])}