Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159import Foundationimport RealityDriverSupportimport Testing
struct DriverInputSafetyTests { @Test(arguments: [nil, 1, 2, 3, DriverWire.protocolVersion - 1, DriverWire.protocolVersion + 1] as [Int?]) func staleDriversCannotInjectBeforeTheSafetyHandshake(_ version: Int?) { #expect(throws: DriverError.self) { try DriverWire.requireProtocolVersion(version) } }
@Test func missingVersionIsNotUpgradedDuringDecoding() throws { let data = Data( #"{"axTrusted":true,"postEventAccess":true,"screenRecording":false,"processID":11,"parentProcessID":7}"# .utf8) let status = try JSONDecoder().decode(DriverPreflight.self, from: data) #expect(status.protocolVersion == nil) #expect(throws: DriverError.self) { try DriverWire.requireProtocolVersion(status.protocolVersion) } }
@Test func currentSafetyProtocolIsRequired() throws { try DriverWire.requireProtocolVersion(DriverWire.protocolVersion) }
@Test func unownedForegroundRefusesKeysAndDrags() { let target = DriverInputTarget(pid: 42, windowID: 7) #expect(throws: DriverError.self) { try target.requireOwned(registered: []) } #expect(throws: DriverError.self) { try target.requireOwned(registered: [8], dragWindow: 7) } }
@Test func ownedWindowMustBeTheDragSource() throws { let target = DriverInputTarget(pid: 42, windowID: 7) try target.requireOwned(registered: [7]) try target.requireOwned(registered: [7], dragWindow: 7) #expect(throws: DriverError.self) { try target.requireOwned(registered: [7], dragWindow: 8) } }
@Test func panelAcceptsKeysButNotDocumentDrags() throws { let target = DriverInputTarget(pid: 42, windowID: 7, panel: true) try target.requireOwned(registered: []) #expect(throws: DriverError.self) { try target.requireOwned(registered: [], dragWindow: 7) } }
@Test(arguments: [false, true], [false, true]) func fenceFailureNeverCountsAsConsumption(barrier: Bool, disabled: Bool) throws { let target = DriverInputTarget(pid: 42, windowID: 7) if barrier && !disabled { #expect( try DriverInputVerification.outcome( target: target, expectation: .shortcut, reachedApplication: false, barrierObserved: barrier, disabled: disabled ).contains("effect must be checked")) } else { #expect(throws: DriverError.self) { try DriverInputVerification.outcome( target: target, expectation: .shortcut, reachedApplication: false, barrierObserved: barrier, disabled: disabled) } } }
@Test(arguments: [false, true]) func escapedShortcutsFailEvenWithAnOwnedFixture(fixture: Bool) { #expect(throws: DriverError.self) { try DriverInputVerification.outcome( target: DriverInputTarget(pid: 42, windowID: 7, fixture: fixture), expectation: .shortcut, reachedApplication: true, barrierObserved: true, disabled: false) } }
@Test func onlyFixturePassThroughAndParcelPanelsMayReceiveEscapedKeys() throws { let document = DriverInputTarget(pid: 42, windowID: 7) let fixture = DriverInputTarget(pid: 42, windowID: 7, fixture: true) let panel = DriverInputTarget(pid: 42, windowID: 7, panel: true) #expect(!DriverInputVerification.allowsDelivery(target: document, eventPID: 42)) #expect(!DriverInputVerification.allowsDelivery(target: fixture, eventPID: 43)) #expect(DriverInputVerification.allowsDelivery(target: fixture, eventPID: 42)) #expect(DriverInputVerification.allowsDelivery(target: panel, eventPID: 42)) #expect(throws: DriverError.self) { try DriverInputVerification.requireExpectation(.fixturePassThrough, target: document) } #expect(throws: DriverError.self) { try DriverInputVerification.outcome( target: fixture, expectation: .fixturePassThrough, reachedApplication: false, barrierObserved: true, disabled: false) } #expect( try DriverInputVerification.outcome( target: fixture, expectation: .fixturePassThrough, reachedApplication: true, barrierObserved: true, disabled: false ).contains("receipt must be checked")) #expect( try DriverInputVerification.outcome( target: panel, expectation: .shortcut, reachedApplication: true, barrierObserved: true, disabled: false ).contains("effect must be checked")) }
@Test(arguments: DriverModifierSequence.modifierKeys + [57]) func ordinaryKeysCannotPressModifiers(_ key: UInt16) { #expect(throws: DriverError.self) { try DriverCommand.key(virtualKey: key, flags: 0).validate() } #expect(throws: DriverError.self) { try DriverCommand.heldKeys(first: 48, second: key, flags: 0, holdMilliseconds: 500).validate() } }
@Test(arguments: [UInt16(35), 96]) func globalPrintAndVoiceOverToggleAreForbidden(_ key: UInt16) { #expect(throws: DriverError.self) { try DriverCommand.key(virtualKey: key, flags: 0x0010_0000).validate() } }
@Test(arguments: [false, true]) func modifiersReleaseAndVerificationRunsEvenWhenOperationThrows(_ throwsError: Bool) throws { let sequence = try DriverModifierSequence(flags: 0x001C_0000, initialFlags: 0, heldKeys: []) var events: [DriverModifierEvent] = [] var verified = false var operated = false do { try sequence.perform( post: { events.append($0) }, verifyRestored: { #expect(events == sequence.pressed + sequence.released) verified = true }, operation: { flags in operated = true #expect(flags == sequence.activeFlags) if throwsError { throw DriverError("operation failed") } }) #expect(!throwsError) } catch { #expect(throwsError) #expect(String(describing: error) == "operation failed") } #expect(operated && verified) #expect(events.last?.flags == 0) }
@Test func restorationFailureIsNotHiddenByOperationFailure() throws { let sequence = try DriverModifierSequence(flags: 0x0010_0000, initialFlags: 0, heldKeys: []) var events: [DriverModifierEvent] = [] #expect(throws: DriverError.self) { try sequence.perform( post: { events.append($0) }, verifyRestored: { throw DriverError("still held") }, operation: { _ in throw DriverError("operation failed") }) } #expect(events == sequence.pressed + sequence.released) }}