import Foundation import Testing @testable import ParcelCore @Test(arguments: Layout.allCases.filter { $0 != .accordion }, [10.0, 3.5]) func arrivalWithGapsKeepsOlderTilesStill(layout: Layout, gap: Double) throws { var state = State( displays: [Display(id: primaryDisplay, frame: testScreen)], activeSpaces: [primaryDisplay: primarySpace], workspaces: [primaryKey: workspace(0, layout: layout, gaps: Gaps(inner: gap))]) for number in 1...8 { let previous = state let newest = state.workspaces[primaryKey]?.tree?.stacks.last?.visible let result = reduce(state, .windowAppeared(managed(number), workspace: primaryKey, frame: Rect())) for window in previous.visibleWindows where window != newest { #expect(result.0.frames[window] == previous.frames[window]) #expect(!frameWrites(result.1).contains { $0.window == window }) } #expect( result.0.workspaces[primaryKey]?.tree?.tileCount == min(number, layout.capabilities(in: testScreen).maximumWindowCount)) #expect(result.0.cannotFit.isEmpty) state = result.0 } try roundTrip(state) } @Test( arguments: Layout.allCases.filter { $0 != .accordion }, [ (MainRatio.half, testScreen), (.threeFifths, testScreen), (.twoThirds, testScreen), (.half, Rect(width: 800, height: 1200)), (.threeFifths, Rect(width: 800, height: 1200)), (.twoThirds, Rect(width: 800, height: 1200)), ] ) func secondTileUsesTheConfiguredMainSize(layout: Layout, settings: (MainRatio, Rect)) throws { let (ratio, screen) = settings var space = workspace(0, layout: layout) space.configuredRatio = ratio space.changeRatio(ratio) space.adapt(to: screen) for number in 1...2 { space.insert(windowID(number), floating: false, in: screen, windows: [:]) } #expect(space.mainSizeRatio == ratio.value) let frames = LayoutEngine.arrange(space, in: screen).frames let first = try #require(frames[windowID(1)]) let second = try #require(frames[windowID(2)]) let firstArea = first.size.width * first.size.height let secondArea = second.size.width * second.size.height #expect(abs(firstArea / (firstArea + secondArea) - ratio.value) < 0.002) } @Test(arguments: [Layout.columns, .rows]) func thirdArrivalSplitsNewestGappedAreaOrStacksAtCap(layout: Layout) { var workspace = workspace(0, layout: layout, gaps: Gaps(inner: 10)) workspace.adapt(to: testScreen) for number in 1...3 { workspace.insert(windowID(number), floating: false, in: testScreen, windows: [:]) } let expected = layout == .columns ? [ windowID(1): Rect(x: 0, y: 0, width: 595, height: 800), windowID(2): Rect(x: 605, y: 0, width: 293, height: 800), windowID(3): Rect(x: 908, y: 0, width: 292, height: 800), ] : [ windowID(1): Rect(x: 0, y: 0, width: 1200, height: 395), windowID(2): Rect(x: 0, y: 405, width: 1200, height: 395), windowID(3): Rect(x: 0, y: 405, width: 1200, height: 395), ] #expect(LayoutEngine.arrange(workspace, in: testScreen).frames == expected) } @Test(arguments: [Point(), Point(x: 0.4, y: -0.4), Point(x: -300.7, y: 100.3)]) func fractionalGridInsertionUsesUnroundedLongestAxis(origin: Point) { let screen = Rect(origin: origin, size: Size(width: 100.3, height: 100.4)) var workspace = workspace(0) workspace.adapt(to: screen) for number in 1...2 { workspace.insert(windowID(number), floating: false, in: screen, windows: [:]) } #expect( LayoutEngine.arrange(workspace, in: screen).frames == [ windowID(1): Rect(origin: origin, size: Size(width: 100.3, height: 50.2)).roundedToWholePoints(), windowID(2): Rect(x: origin.xCoordinate, y: origin.yCoordinate + 50.2, width: 100.3, height: 50.2) .roundedToWholePoints(), ]) } @Test(arguments: [MainRatio.half, .threeFifths, .twoThirds], [false, true]) func removingSideMainPromotesNextTileAndKeepsRatio(ratio: MainRatio, floating: Bool) throws { var state = focused(1, in: populated(3, layout: .side)) state.workspaces[primaryKey]?.gaps = Gaps(inner: 10) state = command(.setRatio(ratio), in: state).0 let previousMain = try #require(state.frames[windowID(1)]) state = floating ? command(.toggleFloat, in: state).0 : reduce(state, .windowDisappeared(windowID(1))).0 #expect(state.frames[windowID(2)] == previousMain) let secondary = Rect(x: previousMain.maxX + 10, y: 0, width: 1200 - previousMain.maxX - 10, height: 800) #expect(state.frames[windowID(3)] == secondary) #expect(state.workspaces[primaryKey]?.layout == .side) #expect(state.workspaces[primaryKey]?.ratio == ratio) state = reduce(state, .windowAppeared(managed(4), workspace: primaryKey, frame: Rect())).0 #expect(state.frames[windowID(2)] == previousMain) #expect(state.frames[windowID(3)] == Rect(x: secondary.minX, y: 0, width: secondary.size.width, height: 395)) #expect(state.frames[windowID(4)] == Rect(x: secondary.minX, y: 405, width: secondary.size.width, height: 395)) } @Test func sideMainMetadataChangeAndTransferAlsoPromote() throws { for transfer in [false, true] { var state = multipleDisplays() state = command(.setLayout(.side), in: state).0 state = command(.setRatio(.threeFifths), in: state).0 let main = try #require(state.frames[windowID(1)]) state = transfer ? command(.moveToDisplay(.right), in: state).0 : reduce(state, .windowAppeared(managed(1, floating: true), workspace: primaryKey, frame: main)).0 #expect(state.frames[windowID(2)] == testScreen) state = reduce(state, .windowAppeared(managed(5), workspace: primaryKey, frame: Rect())).0 #expect(state.frames[windowID(2)] == main) #expect(state.frames[windowID(5)] == Rect(x: 720, y: 0, width: 480, height: 800)) } } @Test func removingSideSecondaryKeepsMainAndSurvivingSplits() throws { var state = focused(4, in: populated(4, layout: .side)) state.workspaces[primaryKey]?.gaps = Gaps(inner: 10) state = command(.retile, in: state).0 let main = try #require(state.frames[windowID(1)]) state = reduce(state, .windowDisappeared(windowID(3))).0 #expect(state.frames[windowID(1)] == main) #expect(state.workspaces[primaryKey]?.tree?.stacks.map(\.windows) == [[windowID(1)], [windowID(2)], [windowID(4)]]) #expect(state.focusedWindow == windowID(4)) } @Test func removingOneMainStackMemberDoesNotPromoteSecondary() throws { var state = focused(2, in: populated(3, layout: .side, cap: 2)) state = command(.stack(.left), in: state).0 let main = try #require(state.frames[windowID(1)]) state = reduce(state, .windowDisappeared(windowID(1))).0 #expect(state.frames[windowID(2)] == main) #expect(state.frames[windowID(3)] == Rect(x: 600, y: 0, width: 600, height: 800)) } @Test(arguments: [-0.1, 1.1], [false, true]) func decodedOutOfBoundsSplitFractionIsRejected(fraction: Double, partition: Bool) throws { var workspace = workspace(2) let first = TileTree.tile(WindowStack(windowID(1))) let second = TileTree.tile(WindowStack(windowID(2))) workspace.tree = partition ? .partition(axis: .horizontal, fraction: fraction, first: first, second: second) : .split(axis: .horizontal, fraction: fraction, first: first, second: second) let data = try JSONEncoder().encode(workspace) #expect(throws: DecodingError.self) { try JSONDecoder().decode(Workspace.self, from: data) } } @Test(arguments: [false, true]) func decodedDuplicateLeavesAreRejected(partition: Bool) throws { var workspace = workspace(2) let tile = TileTree.tile(WindowStack(windowID(1))) workspace.tree = partition ? .partition(axis: .horizontal, fraction: 0.5, first: tile, second: tile) : .split(axis: .horizontal, fraction: 0.5, first: tile, second: tile) let data = try JSONEncoder().encode(workspace) #expect(throws: DecodingError.self) { try JSONDecoder().decode(Workspace.self, from: data) } } @Test(arguments: Layout.allCases, [10.0, 400.0]) func insertedTreesWithImpossibleMinimumsStayInsideTheScreen(layout: Layout, gap: Double) throws { var workspace = workspace(0, layout: layout, gaps: Gaps(inner: gap)) workspace.adapt(to: testScreen) let windows = Dictionary( uniqueKeysWithValues: (1...8).map { number in (windowID(number), managed(number, minimum: Size(width: 300, height: 300))) }) for number in 1...8 { workspace.insert(windowID(number), floating: false, in: testScreen, windows: windows) let result = LayoutEngine.arrange(workspace, in: testScreen, windows: windows) #expect(result.frames.count == number) for frame in result.frames.values { #expect(frame.minX >= 0 && frame.minY >= 0) #expect(frame.maxX <= 1200 && frame.maxY <= 800) #expect(frame.size.width >= 0 && frame.size.height >= 0) } #expect( result.cannotFit == result.frames.keys.sorted().filter { guard let frame = result.frames[$0] else { return false } return frame.size.width < 300 || frame.size.height < 300 }) } try roundTrip(workspace) } @Test(arguments: [false, true]) func insertionPartitionsRemainInsideAnExistingBalancedOrPartitionedArea(balanced: Bool) throws { var workspace = workspace(3, gaps: Gaps(inner: 10)) let first = TileTree.tile(WindowStack(windowID(1))) let second = TileTree.partition( axis: .horizontal, fraction: 0.5, first: .tile(WindowStack(windowID(2))), second: .tile(WindowStack(windowID(3)))) workspace.tree = balanced ? .split(axis: .horizontal, fraction: 0.5, first: first, second: second) : .partition(axis: .horizontal, fraction: 0.5, first: first, second: second) workspace.adapt(to: testScreen) #expect( LayoutEngine.arrange(workspace, in: testScreen).frames == [ windowID(1): Rect(x: 0, y: 0, width: 595, height: 800), windowID(2): Rect(x: 605, y: 0, width: 293, height: 800), windowID(3): Rect(x: 908, y: 0, width: 292, height: 800), ]) try roundTrip(workspace) } @Test func decodedTreeBeyondCapIsRejected() throws { var workspace = workspace(2, cap: 1) workspace.tree = TileTree.balanced([WindowStack(windowID(1)), WindowStack(windowID(2))], layout: .columns) let data = try JSONEncoder().encode(workspace) #expect(throws: DecodingError.self) { try JSONDecoder().decode(Workspace.self, from: data) } } @Test func decodedTileOutsideWorkspaceOrderIsRejected() throws { var workspace = workspace(2) workspace.tree = .tile(WindowStack(windowID(9))) let data = try JSONEncoder().encode(workspace) #expect(throws: DecodingError.self) { try JSONDecoder().decode(Workspace.self, from: data) } }