Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221import CoreGraphicsimport Foundationimport Testing
@testable import ParcelPlatform
private func windowFields() -> [String: Any] { [ kCGWindowNumber as String: 123, kCGWindowOwnerPID as String: 456, kCGWindowBounds as String: ["X": -20.5, "Y": 30.0, "Width": 800.0, "Height": 600.0], kCGWindowLayer as String: -1, kCGWindowIsOnscreen as String: true, ]}
@Test func probesCapabilitiesIndependently() { let available: Set<String> = [ "SLSMainConnectionID", "SLSCopyManagedDisplaySpaces", "CGSSetWindowListBrightness", "_AXUIElementGetWindow", ] var probed: [String] = [] let capabilities = WindowServer.Capabilities { symbol in probed.append(symbol) return available.contains(symbol) } #expect( probed == [ "SLSMainConnectionID", "SLSCopyManagedDisplaySpaces", "SLSManagedDisplayGetCurrentSpace", "CGSSetWindowListBrightness", "_AXUIElementGetWindow", ]) #expect(capabilities.mainConnection) #expect(capabilities.axWindowID) #expect(capabilities.managedDisplaySpaces) #expect(!capabilities.currentSpace) #expect(capabilities.experimentalBrightness) #expect(capabilities.canReadDisplaySpaces) #expect(!capabilities.canReadCurrentSpace)
let missingConnection = WindowServer.Capabilities { $0 != "SLSMainConnectionID" && $0 != "CGSMainConnectionID" } #expect(!missingConnection.canReadDisplaySpaces) #expect(!missingConnection.canReadCurrentSpace) #expect(!missingConnection.experimentalBrightness) let legacyBrightness = WindowServer.Capabilities { ["CGSMainConnectionID", "CGSSetWindowListBrightness"].contains($0) } #expect(legacyBrightness.experimentalBrightness) #expect(!legacyBrightness.canReadDisplaySpaces) let missingBrightness = WindowServer.Capabilities { $0 != "CGSSetWindowListBrightness" } #expect(!missingBrightness.experimentalBrightness) let noSymbols = WindowServer.Capabilities { _ in false } #expect(!noSymbols.experimentalBrightness) #expect(!noSymbols.axWindowID) #expect(!noSymbols.canReadDisplaySpaces) #expect(!noSymbols.canReadCurrentSpace)}
@Test(arguments: [false, true])func singleWindowVisibilityReadsOnlyTheRequestedID(isOnScreen: Bool) throws { var reads = 0 let visible = try WindowServer.isOnScreen(windowID: 42) { options, id in reads += 1 #expect(options == .optionIncludingWindow && id == 42) return [.init(number: id, pid: 456, bounds: .zero, layer: 0, isOnscreen: isOnScreen)] } #expect(visible == isOnScreen && reads == 1)}
@Test func missingWindowServerEntryIsNotOnScreen() throws { #expect(try WindowServer.isOnScreen(windowID: 42) { _, _ in [] } == false)}
@Test func visibilityReadFailureDoesNotAssumeTheWindowIsVisible() { #expect(throws: WindowServer.ReadError.windowListUnavailable) { try WindowServer.isOnScreen(windowID: 42) { _, _ in throw WindowServer.ReadError.windowListUnavailable } }}
@Test func unrelatedWindowServerEntryCannotProveVisibility() { #expect(throws: WindowServer.ReadError.malformedWindowList) { try WindowServer.isOnScreen(windowID: 42) { _, _ in [.init(number: 99, pid: 456, bounds: .zero, layer: 0, isOnscreen: true)] } }}
@Test func decodesWindowRecords() throws { var offscreen = windowFields() offscreen.removeValue(forKey: kCGWindowIsOnscreen as String) let windows = try WindowServer.decodeWindows(([windowFields(), offscreen] as NSArray).map { $0 }) #expect(windows.count == 2) #expect(windows[0].number == 123) #expect(windows[0].pid == 456) #expect(windows[0].bounds == CGRect(x: -20.5, y: 30, width: 800, height: 600)) #expect(windows[0].layer == -1) #expect(windows[0].isOnscreen) #expect(!windows[1].isOnscreen) #expect(try WindowServer.decodeWindows([]).isEmpty)}
@Test func rejectsMissingAndMalformedWindowFields() { let number = kCGWindowNumber as String let pid = kCGWindowOwnerPID as String let bounds = kCGWindowBounds as String let layer = kCGWindowLayer as String let onscreen = kCGWindowIsOnscreen as String
#expect(throws: WindowServer.ReadError.invalidWindow(index: 0, field: "record", problem: .malformed)) { try WindowServer.decodeWindows(["not a dictionary"]) } var fields = windowFields() fields.removeValue(forKey: number) #expect(throws: WindowServer.ReadError.invalidWindow(index: 0, field: number, problem: .missing)) { try WindowServer.decodeWindows([fields]) } for invalid: Any in [true, -1, 0, 4.5, UInt64.max, "123"] { fields = windowFields() fields[number] = invalid #expect(throws: WindowServer.ReadError.invalidWindow(index: 0, field: number, problem: .malformed)) { try WindowServer.decodeWindows([fields]) } } fields = windowFields() fields[pid] = Int64(Int32.max) + 1 #expect(throws: WindowServer.ReadError.invalidWindow(index: 0, field: pid, problem: .malformed)) { try WindowServer.decodeWindows([fields]) } fields = windowFields() fields[layer] = 0.5 #expect(throws: WindowServer.ReadError.invalidWindow(index: 0, field: layer, problem: .malformed)) { try WindowServer.decodeWindows([fields]) } fields = windowFields() fields[onscreen] = 1 #expect(throws: WindowServer.ReadError.invalidWindow(index: 0, field: onscreen, problem: .malformed)) { try WindowServer.decodeWindows([fields]) } fields = windowFields() fields[bounds] = ["X": 1, "Y": 2, "Width": 3] #expect(throws: WindowServer.ReadError.invalidWindow(index: 0, field: "\(bounds).Height", problem: .missing)) { try WindowServer.decodeWindows([fields]) } for invalid: Any in [Double.infinity, Double.nan, true, "2"] { fields = windowFields() fields[bounds] = ["X": invalid, "Y": 2, "Width": 3, "Height": 4] #expect(throws: WindowServer.ReadError.invalidWindow(index: 0, field: "\(bounds).X", problem: .malformed)) { try WindowServer.decodeWindows([fields]) } } fields = windowFields() fields[bounds] = ["X": 0, "Y": 0, "Width": -1, "Height": 2] #expect(throws: WindowServer.ReadError.invalidWindow(index: 1, field: bounds, problem: .malformed)) { try WindowServer.decodeWindows([windowFields(), fields]) }}
@Test func decodesDisplaySpaces() throws { let entries: [Any] = [ ["Display Identifier": "display-a", "Spaces": [["ManagedSpaceID": 3], ["ManagedSpaceID": UInt64.max]]], ["Display Identifier": "display-b", "Spaces": []], ] let result = try WindowServer.decodeDisplaySpaces((entries as NSArray).map { $0 }) #expect(result.count == 2) #expect(result[0].displayID == "display-a") #expect(result[0].spaces == [3, UInt64.max]) #expect(result[1].displayID == "display-b") #expect(result[1].spaces.isEmpty) #expect(try WindowServer.decodeDisplaySpaces([]).isEmpty)}
@Test func rejectsMalformedDisplaySpaces() { #expect(throws: WindowServer.ReadError.invalidDisplaySpaces(index: 0, field: "record", problem: .malformed)) { try WindowServer.decodeDisplaySpaces([false]) } #expect( throws: WindowServer.ReadError.invalidDisplaySpaces(index: 0, field: "Display Identifier", problem: .missing) ) { try WindowServer.decodeDisplaySpaces([[:]]) } #expect( throws: WindowServer.ReadError.invalidDisplaySpaces(index: 0, field: "Display Identifier", problem: .malformed) ) { try WindowServer.decodeDisplaySpaces([["Display Identifier": ""]]) } #expect(throws: WindowServer.ReadError.invalidDisplaySpaces(index: 0, field: "Spaces", problem: .missing)) { try WindowServer.decodeDisplaySpaces([["Display Identifier": "display"]]) } #expect(throws: WindowServer.ReadError.invalidDisplaySpaces(index: 0, field: "Spaces", problem: .malformed)) { try WindowServer.decodeDisplaySpaces([["Display Identifier": "display", "Spaces": "not an array"]]) } #expect(throws: WindowServer.ReadError.invalidDisplaySpaces(index: 0, field: "Spaces[0]", problem: .malformed)) { try WindowServer.decodeDisplaySpaces([["Display Identifier": "display", "Spaces": [5]]]) } #expect( throws: WindowServer.ReadError.invalidDisplaySpaces( index: 0, field: "Spaces[0].ManagedSpaceID", problem: .missing) ) { try WindowServer.decodeDisplaySpaces([["Display Identifier": "display", "Spaces": [[:]]]]) } for invalid: Any in [true, -1, 0, 1.5, "3"] { #expect( throws: WindowServer.ReadError.invalidDisplaySpaces( index: 0, field: "Spaces[0].ManagedSpaceID", problem: .malformed) ) { try WindowServer.decodeDisplaySpaces([ ["Display Identifier": "display", "Spaces": [["ManagedSpaceID": invalid]]] ]) } }}
@Test func readsCurrentWindowList() throws { let windows = try WindowServer().windows() #expect(windows.allSatisfy { $0.number != 0 && $0.bounds.width >= 0 && $0.bounds.height >= 0 })}
@Test func rejectsEmptyDisplayID() { #expect(throws: WindowServer.ReadError.currentSpaceUnavailable(displayID: "")) { try WindowServer().currentSpace(displayID: "") }}