Repository Public
1import CoreGraphics2import Foundation3import Testing4import WorkspaceKit5@testable import WindowManager67/// Simulated monitors: a laptop panel and an external display to its right, plus variants.8enum Rig {9 static let laptop = ScreenDescriptor(id: 1, name: "Built-in Retina Display", frame: CGRect(x: 0, y: 0, width: 1512, height: 982),10 visibleFrame: CGRect(x: 0, y: 70, width: 1512, height: 874), isMain: true)11 static let external = ScreenDescriptor(id: 2, name: "Studio Display", frame: CGRect(x: 1512, y: -200, width: 2560, height: 1440),12 visibleFrame: CGRect(x: 1512, y: -200, width: 2560, height: 1415), isMain: false)13 static let left = ScreenDescriptor(id: 3, name: "LG UltraFine", frame: CGRect(x: -1920, y: 0, width: 1920, height: 1080), isMain: false)14 static let dual = [laptop, external]15 static let triple = [laptop, external, left]16}1718@Suite("Window placement on simulated monitors")19struct PlacementTests {20 @Test func savedFrameOnSameScreenIsKept() {21 let frame = CGRect(x: 1700, y: 100, width: 1200, height: 800)22 let placement = PlacementEngine.resolve(savedFrame: frame, savedScreen: Rig.external.reference, in: Rig.dual)!23 #expect(placement.frame == frame)24 #expect(placement.screenID == 2)25 #expect(placement.reason == .sameScreen)26 }2728 @Test func removedMonitorBringsWindowBackOnScreen() {29 let frame = CGRect(x: 2600, y: 300, width: 1400, height: 900)30 let placement = PlacementEngine.resolve(savedFrame: frame, savedScreen: Rig.external.reference, in: [Rig.laptop])!31 #expect(placement.reason == .screenMissing)32 #expect(placement.screenID == 1)33 #expect(Rig.laptop.visibleFrame.contains(placement.frame))34 #expect(PlacementEngine.isReachable(placement.frame, in: [Rig.laptop]))35 // Relative position is preserved: the window was in the right half and stays there.36 #expect(placement.frame.midX > Rig.laptop.visibleFrame.midX - 200)37 }3839 @Test func runtimeRescueMovesOnlyUnreachableWindows() {40 let onLaptop = WindowID(), onExternal = WindowID(), straddling = WindowID()41 let windows: [WindowID: CGRect] = [42 onLaptop: CGRect(x: 100, y: 200, width: 900, height: 600),43 onExternal: CGRect(x: 2000, y: 0, width: 1600, height: 1000),44 straddling: CGRect(x: 1300, y: 300, width: 700, height: 500),45 ]46 let rescued = PlacementEngine.rescue(windows: windows, previousScreens: Rig.dual, currentScreens: [Rig.laptop])47 #expect(rescued[onLaptop] == nil)48 #expect(rescued[onExternal]?.reason == .screenMissing)49 // The straddling window keeps a reachable title bar on the laptop, so it is not moved.50 #expect(rescued[straddling] == nil)51 var final = windows52 for (id, placement) in rescued { final[id] = placement.frame }53 for (_, frame) in final { #expect(PlacementEngine.isReachable(frame, in: [Rig.laptop])) }54 #expect(Rig.laptop.visibleFrame.contains(rescued[onExternal]!.frame))55 }5657 @Test func noWindowIsEverLostWhateverTheConfigurationChange() {58 var generator = SystemRandomNumberGenerator()59 let configurations: [[ScreenDescriptor]] = [[Rig.laptop], Rig.dual, Rig.triple, [Rig.external], [Rig.left, Rig.laptop],60 [ScreenDescriptor(id: 9, name: "Projector", frame: CGRect(x: 0, y: 0, width: 800, height: 600), isMain: true)]]61 for _ in 0..<500 {62 let before = configurations.randomElement(using: &generator)!63 let after = configurations.randomElement(using: &generator)!64 let source = before.randomElement(using: &generator)!65 let width = CGFloat.random(in: 300...3000, using: &generator), height = CGFloat.random(in: 200...1600, using: &generator)66 let frame = CGRect(x: source.frame.minX + CGFloat.random(in: -400...source.frame.width, using: &generator),67 y: source.frame.minY + CGFloat.random(in: -400...source.frame.height, using: &generator),68 width: width, height: height)69 // Restore path.70 let restored = PlacementEngine.resolve(savedFrame: frame, savedScreen: source.reference, in: after)!71 #expect(PlacementEngine.isReachable(restored.frame, in: after))72 let target = after.first { $0.id == restored.screenID }!73 #expect(target.visibleFrame.contains(restored.frame))74 // Live path.75 let id = WindowID()76 let rescued = PlacementEngine.rescue(windows: [id: frame], previousScreens: before, currentScreens: after)77 let live = rescued[id]?.frame ?? frame78 if PlacementEngine.isReachable(frame, in: before) || rescued[id] != nil {79 #expect(PlacementEngine.isReachable(live, in: after))80 }81 }82 }8384 @Test func noScreensYieldsNoPlacement() {85 #expect(PlacementEngine.resolve(savedFrame: nil, savedScreen: nil, in: []) == nil)86 #expect(PlacementEngine.rescue(windows: [WindowID(): .zero], previousScreens: Rig.dual, currentScreens: []).isEmpty)87 }8889 @Test func resolutionChangeRescalesProportionally() {90 let saved = Rig.external.reference91 let smaller = ScreenDescriptor(id: 2, name: "Studio Display", frame: CGRect(x: 1512, y: 0, width: 1280, height: 720), isMain: false)92 let frame = CGRect(x: 1512 + 1280, y: -200 + 720, width: 1280, height: 720) // top-right quarter93 let placement = PlacementEngine.resolve(savedFrame: frame, savedScreen: saved, in: [Rig.laptop, smaller])!94 #expect(placement.reason == .screenGeometryChanged)95 #expect(smaller.visibleFrame.contains(placement.frame))96 #expect(abs(placement.frame.width - 640) <= 1)97 #expect(abs(placement.frame.maxX - smaller.frame.maxX) <= 1)98 }99100 @Test func displayIDChangeIsMatchedByNameAndSize() {101 let replugged = ScreenDescriptor(id: 77, name: "Studio Display", frame: CGRect(x: -2560, y: 0, width: 2560, height: 1440), isMain: false)102 let frame = CGRect(x: 1512 + 100, y: 0, width: 1000, height: 700)103 let placement = PlacementEngine.resolve(savedFrame: frame, savedScreen: Rig.external.reference, in: [Rig.laptop, replugged])!104 #expect(placement.reason == .matchedByName)105 #expect(placement.screenID == 77)106 #expect(replugged.visibleFrame.contains(placement.frame))107 }108109 @Test func newWindowsCascadeInsideTheVisibleFrame() {110 var seen = Set<CGPoint.Key>()111 for index in 0..<60 {112 let frame = PlacementEngine.frameForNewWindow(on: Rig.laptop, cascadeIndex: index)113 #expect(Rig.laptop.visibleFrame.contains(frame))114 seen.insert(.init(frame.origin))115 }116 #expect(seen.count >= 3)117 // A display smaller than the default size still gets a window that fits.118 let tiny = ScreenDescriptor(id: 5, name: "Tiny", frame: CGRect(x: 0, y: 0, width: 640, height: 400), isMain: true)119 #expect(tiny.visibleFrame.contains(PlacementEngine.frameForNewWindow(on: tiny)))120 }121122 @Test func slotsMapToDisplaysAndWrapOnSingleScreen() {123 #expect(PlacementEngine.screen(forSlot: 0, in: Rig.triple)?.id == 1)124 #expect(PlacementEngine.screen(forSlot: 1, in: Rig.triple)?.id == 3) // leftmost first125 #expect(PlacementEngine.screen(forSlot: 2, in: Rig.triple)?.id == 2)126 #expect(PlacementEngine.screen(forSlot: 1, in: [Rig.laptop])?.id == 1)127 #expect(PlacementEngine.screen(forSlot: 5, in: Rig.dual)?.id == 2)128 }129130 @Test func dualMonitorPresetArrangement() {131 let twoScreens = PresetBuilder.build(.dualMonitor, options: PresetOptions(screenCount: 2))132 let placed = PlacementEngine.arrange(twoScreens.windows, in: Rig.dual)133 #expect(placed[twoScreens.windows[0].id]?.screenID == 1)134 #expect(placed[twoScreens.windows[1].id]?.screenID == 2)135 // Same preset on one display: both windows visible, tiled, not stacked.136 let oneScreen = PresetBuilder.build(.dualMonitor, options: PresetOptions(screenCount: 1))137 let single = PlacementEngine.arrange(oneScreen.windows, in: [Rig.laptop])138 let frames = oneScreen.windows.compactMap { single[$0.id]?.frame }139 #expect(frames.count == 2)140 #expect(frames.allSatisfy { Rig.laptop.visibleFrame.contains($0) })141 let overlap = frames[0].intersection(frames[1])142 #expect(overlap.isNull || overlap.width * overlap.height == 0)143 }144145 @Test func tilingCoversWithoutOverlap() {146 let visible = Rig.external.visibleFrame147 for count in 1...9 {148 let frames = PlacementEngine.tile(count: count, in: visible)149 #expect(frames.count == count)150 #expect(frames.allSatisfy { visible.insetBy(dx: -1, dy: -1).contains($0) })151 }152 #expect(PlacementEngine.tile(count: 0, in: visible).isEmpty)153 }154155 @Test func screenChangeDiff() {156 let change = ScreenChange.between(Rig.dual, [Rig.laptop, Rig.left])157 #expect(change.added == [3])158 #expect(change.removed == [2])159 #expect(change.changed.isEmpty)160 var resized = Rig.laptop161 resized.frame.size.width = 1728162 #expect(ScreenChange.between([Rig.laptop], [resized]).changed == [1])163 #expect(ScreenChange.between(Rig.dual, Rig.dual).isEmpty)164 }165166 @Test func clampEnforcesMinimumAndBounds() {167 let visible = Rig.laptop.visibleFrame168 let clamped = PlacementEngine.clamp(CGRect(x: -5000, y: 9000, width: 10, height: 10), to: visible)169 #expect(visible.contains(clamped))170 #expect(clamped.width >= PlacementEngine.minimumWindowSize.width)171 let huge = PlacementEngine.clamp(CGRect(x: 0, y: 0, width: 9000, height: 9000), to: visible)172 #expect(huge == visible.integral)173 }174}175176extension CGPoint {177 struct Key: Hashable { let x: Int, y: Int; init(_ p: CGPoint) { x = Int(p.x); y = Int(p.y) } }178}179