Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186import CoreGraphicsimport Foundationimport RealityDriverSupportimport Testing
@testable import ParcelRealityTests@testable import ParcelTestSupport
extension RealAppEndToEndTests { @Test( .enabled( if: ProcessInfo.processInfo.environment["PARCEL_E2E"] == "1" && ProcessInfo.processInfo.environment["PARCEL_E2E_CONFIGURATION"] != "release" && ProcessInfo.processInfo.environment["PARCEL_E2E_INSPECTABLE"] != "1"), arguments: [false, true]) func realityDriverRequiresOwnershipAndFailsUnconsumedChords(tilingOff: Bool) async throws { try requireRealityDriverScreenLock() try await withSession { session in let (fixture, ready) = try await session.addFixture(arguments: ["--windows", "1"]) let window = try #require(ready.windows.first) if tilingOff { try await session.startApp() try await session.command("toggle-automatic-tiling") } let driver = RealityDriver(appURL: session.appURL) do { try await driver.preflight() _ = try await fixture.control(FixtureControl(action: "focus", window: window.index)) await #expect(throws: RealityError.self) { _ = try await driver.send(.key(virtualKey: 80, flags: 0)) } _ = try await driver.send(.register(pid: ready.pid, windowID: window.number)) _ = try await driver.send(.focus(pid: ready.pid, windowID: window.number)) await #expect(throws: RealityError.self) { _ = try await driver.send( .heldKeys( first: 80, second: 80, flags: CGEventFlags.maskCommand.rawValue, holdMilliseconds: 500)) } try requireReleasedDriverInput(stage: "unconsumed-throw") _ = try await fixture.control(FixtureControl(action: "input-record-start")) _ = try await driver.send(.key(virtualKey: 80, flags: 0, expectation: .fixturePassThrough)) let received = try await requireFixtureInputReceipt(fixture, key: 80, windowID: window.number) #expect(received.allSatisfy { $0.flags & CGEventFlags.maskCommand.rawValue == 0 }) #expect(received.contains { $0.key == 80 && $0.down && $0.windowID == window.number }) #expect(received.contains { $0.key == 80 && !$0.down && $0.windowID == window.number }) try requireReleasedDriverInput(stage: "expected-pass-through") try await driver.stop() } catch { let failure = error do { try await driver.stop() } catch { throw RealityError("\(failure); cleanup: \(error)") } throw failure } } }
@Test( .enabled( if: ProcessInfo.processInfo.environment["PARCEL_E2E"] == "1" && ProcessInfo.processInfo.environment["PARCEL_E2E_CONFIGURATION"] != "release" && ProcessInfo.processInfo.environment["PARCEL_E2E_INSPECTABLE"] != "1")) func realityDriverReleasesOnlyItsSyntheticModifiersAfterKeysAndDrags() async throws { try requireRealityDriverScreenLock() try await withSession { session in let (fixture, ready) = try await session.addFixture(arguments: ["--windows", "1"]) let window = try #require(ready.windows.first) let driver = RealityDriver(appURL: session.appURL) do { try await driver.preflight() _ = try await driver.send(.register(pid: ready.pid, windowID: window.number)) _ = try await driver.send(.focus(pid: ready.pid, windowID: window.number)) _ = try await fixture.control(FixtureControl(action: "input-record-start")) _ = try await driver.send( .key( virtualKey: 0, flags: CGEventFlags.maskCommand.rawValue, expectation: .fixturePassThrough)) let received = try await requireFixtureInputReceipt(fixture, key: 0, windowID: window.number) #expect(received.contains { $0.key == 0 && $0.down && $0.windowID == window.number }) try requireReleasedDriverInput(stage: "modified-key") let frame = try #require(try session.frames(for: fixture)[window.number]) _ = try await driver.send( .drag( pid: ready.pid, windowID: window.number, fromX: frame.minX + 100, fromY: frame.minY + 12, toX: frame.minX + 200, toY: frame.minY + 62, flags: CGEventFlags.maskCommand.rawValue)) try requireReleasedDriverInput(stage: "modified-drag") try await driver.stop() } catch { let failure = error do { try await driver.stop() } catch { throw E2EError(description: "\(failure); driver cleanup failed: \(error)") } throw failure } } }
@Test( .enabled( if: ProcessInfo.processInfo.environment["PARCEL_E2E"] == "1" && ProcessInfo.processInfo.environment["PARCEL_E2E_CONFIGURATION"] != "release" && ProcessInfo.processInfo.environment["PARCEL_E2E_INSPECTABLE"] != "1")) func realityDriverEscapeDismissesAnOwnedMenuInWindowServerMetadata() async throws { try requireRealityDriverScreenLock() try await withSession { session in let (fixture, ready) = try await session.addFixture(arguments: ["--windows", "1"]) let window = try #require(ready.windows.first) let driver = RealityDriver(appURL: session.appURL) do { try await driver.preflight() _ = try await driver.send(.register(pid: ready.pid, windowID: window.number)) _ = try await driver.send(.focus(pid: ready.pid, windowID: window.number)) let before = try session.frames(for: fixture) _ = try await fixture.control(FixtureControl(action: "open-menu", window: window.index)) try await waitUntil("owned fixture menu appears in CG metadata") { try serverWindows().contains { $0.pid == ready.pid && $0.layer >= 101 } } let menus = try serverWindows().filter { $0.pid == ready.pid && $0.layer >= 101 } try #require(!menus.isEmpty) let ids = Set(menus.map(\.number)) try recordKeyboardSpike("menu-before-escape", windows: menus) _ = try await driver.send(.key(virtualKey: 53, flags: 0, expectation: .fixturePassThrough)) try await waitUntil("Escape removes the owned menu's CG IDs") { let current = try serverWindows() return !current.contains { ids.contains($0.number) } } // Native menu tracking consumes Escape before it reaches a window's event recorder. try requireReleasedDriverInput(stage: "menu-escape") let after = try serverWindows().filter { $0.pid == ready.pid } try recordKeyboardSpike("menu-after-escape", windows: after) #expect(try session.frames(for: fixture) == before) try await driver.stop() } catch { let failure = error do { try await driver.stop() } catch { throw E2EError(description: "\(failure); driver cleanup failed: \(error)") } throw failure } } }}
private func requireReleasedDriverInput(stage: String) throws { struct State: Encodable { let flags: UInt64 let heldModifiers: [UInt16] let leftMouseDown: Bool } let state = State( flags: CGEventSource.flagsState(.combinedSessionState).rawValue, heldModifiers: DriverModifierSequence.modifierKeys.filter { CGEventSource.keyState(.combinedSessionState, key: $0) }, leftMouseDown: CGEventSource.buttonState(.combinedSessionState, button: .left)) #expect(state.heldModifiers.isEmpty && !state.leftMouseDown) #expect(state.flags & DriverModifierSequence.controlledFlags == 0) if let directory = ProcessInfo.processInfo.environment["PARCEL_REALITY_SPIKE_ARTIFACTS"] { let url = URL(fileURLWithPath: directory).appendingPathComponent("\(stage)-input-restored.json") try JSONEncoder().encode(state).write(to: url, options: .atomic) }}
@MainActorfunc requireFixtureInputReceipt(_ fixture: FixtureProcess, key: UInt16, windowID: UInt32?) async throws -> [FixtureKeyboardEvent]{ var received: [FixtureKeyboardEvent] = [] try await waitUntil("FixtureApp native key \(key) down/up receipt") { let reply = try await fixture.snapshot() guard let events = reply.inputEvents else { throw E2EError(description: "FixtureApp omitted its keyboard recorder") } guard reply.overflow != true else { throw E2EError(description: "FixtureApp keyboard receipt overflowed") } received = events.filter { $0.key == key && (windowID == nil || $0.windowID == windowID) } return received.contains(where: \.down) && received.contains { !$0.down } } if let directory = ProcessInfo.processInfo.environment["PARCEL_REALITY_SPIKE_ARTIFACTS"] { let url = URL(fileURLWithPath: directory).appendingPathComponent("native-key-\(key)-\(UUID().uuidString).json") try JSONEncoder().encode(received).write(to: url, options: .atomic) } return received}
private func recordKeyboardSpike(_ name: String, windows: [ServerWindow]) throws { guard let directory = ProcessInfo.processInfo.environment["PARCEL_REALITY_SPIKE_ARTIFACTS"] else { return } let url = URL(fileURLWithPath: directory).appendingPathComponent("\(name).json") try JSONEncoder().encode(windows).write(to: url, options: .atomic)}