Repository Public License
1import CoreGraphics2import Foundation3import NavigatorCore4import Testing5@testable import OrbitEngine67private let gigabyte: UInt64 = 1_073_741_82489@Suite struct SplitEngineTests {10 let tabs = (0..<10).map { _ in TabID() }11 let size = CGSize(width: 1_200, height: 800)1213 private func layout(_ count: Int, axis: SplitLayout.Axis = .horizontal) -> SplitLayout {14 SplitLayout(axis: axis, panes: Array(tabs.prefix(count)))15 }1617 @Test func startingASplitPutsTheNewTabSecondAndFocusesIt() throws {18 let split = try SplitEngine.adding(tabs[1], to: nil, selected: tabs[0])19 #expect(split.panes == [tabs[0], tabs[1]] && split.fractions == [0.5, 0.5] && split.focusedPane == 1)20 #expect(throws: SplitError.tabAlreadyInSplit) { try SplitEngine.adding(tabs[0], to: nil, selected: tabs[0]) }21 #expect(throws: SplitError.paneLimitReached(limit: 1)) { try SplitEngine.adding(tabs[1], to: nil, selected: tabs[0], limit: 1) }22 }2324 @Test func addingSplitsTheFocusedPaneInTwo() throws {25 var split = SplitLayout(panes: [tabs[0], tabs[1]], fractions: [0.6, 0.4], focusedPane: 0)26 split = try SplitEngine.adding(tabs[2], to: split, selected: tabs[0])27 #expect(split.panes == [tabs[0], tabs[2], tabs[1]])28 #expect(zip(split.fractions, [0.3, 0.3, 0.4]).allSatisfy { abs($0 - $1) < 1e-12 })29 #expect(split.focusedPane == 1)30 #expect(throws: SplitError.tabAlreadyInSplit) { try SplitEngine.adding(tabs[1], to: split, selected: tabs[0]) }31 }3233 @Test func theLimitIsEnforced() throws {34 var split: SplitLayout? = nil35 for index in 1..<8 { split = try SplitEngine.adding(tabs[index], to: split, selected: tabs[0]) }36 #expect(split?.panes.count == 8)37 #expect(throws: SplitError.paneLimitReached(limit: 8)) { try SplitEngine.adding(tabs[8], to: split, selected: tabs[0]) }38 #expect(throws: SplitError.paneLimitReached(limit: 8)) { try SplitEngine.adding(tabs[8], to: split, selected: tabs[0], limit: 20) }39 let three = layout(3)40 #expect(throws: SplitError.paneLimitReached(limit: 3)) { try SplitEngine.adding(tabs[8], to: three, selected: tabs[0], limit: 3) }41 #expect(abs((split?.fractions.reduce(0, +) ?? 0) - 1) < 1e-12)42 }4344 @Test func removingRedistributesAndEndsTheSplitAtOnePane() {45 let split = SplitLayout(panes: Array(tabs.prefix(3)), fractions: [0.5, 0.25, 0.25], focusedPane: 2)46 let two = SplitEngine.removing(tabs[0], from: split)47 #expect(two?.panes == [tabs[1], tabs[2]] && two?.fractions == [0.5, 0.5] && two?.focusedPane == 1)48 #expect(SplitEngine.removing(tabs[1], from: two!) == nil)49 #expect(SplitEngine.removing(tabs[9], from: split) == split)50 let focusedGone = SplitEngine.removing(tabs[2], from: split)51 #expect(focusedGone?.focusedPane == 1)52 }5354 @Test func replacingSwapsWhenTheTabIsAlreadyShown() throws {55 let split = layout(3)56 #expect(try SplitEngine.replacing(paneAt: 0, with: tabs[5], in: split).panes == [tabs[5], tabs[1], tabs[2]])57 #expect(try SplitEngine.replacing(paneAt: 0, with: tabs[2], in: split).panes == [tabs[2], tabs[1], tabs[0]])58 #expect(throws: SplitError.paneOutOfRange) { try SplitEngine.replacing(paneAt: 3, with: tabs[5], in: split) }59 let moved = try SplitEngine.moving(paneAt: 0, to: 2, in: SplitLayout(panes: Array(tabs.prefix(3)), fractions: [0.5, 0.3, 0.2]))60 #expect(moved.panes == [tabs[1], tabs[2], tabs[0]] && moved.focusedPane == 2)61 #expect(abs(moved.fractions[2] - 0.5) < 1e-12)62 }6364 @Test func focusCyclesThroughPanes() throws {65 var split = layout(3)66 split = SplitEngine.focusingPrevious(split)67 #expect(split.focusedPane == 2)68 split = SplitEngine.focusingNext(split)69 #expect(split.focusedPane == 0 && SplitEngine.focusedTab(of: split) == tabs[0])70 #expect(try SplitEngine.focusing(paneAt: 1, in: split).focusedPane == 1)71 #expect(throws: SplitError.paneOutOfRange) { try SplitEngine.focusing(paneAt: -1, in: split) }72 }7374 @Test func horizontalAndVerticalRects() {75 let split = SplitLayout(axis: .horizontal, panes: Array(tabs.prefix(3)), fractions: [0.5, 0.25, 0.25])76 let rects = SplitEngine.paneRects(for: split, in: size, spacing: 0)77 #expect(rects == [CGRect(x: 0, y: 0, width: 600, height: 800), CGRect(x: 600, y: 0, width: 300, height: 800),78 CGRect(x: 900, y: 0, width: 300, height: 800)])79 let spaced = SplitEngine.paneRects(for: split, in: size, spacing: 10)80 #expect(abs(spaced[0].width - 590) < 1e-9 && abs(spaced[1].minX - 600) < 1e-9 && abs(spaced[2].maxX - 1_200) < 1e-9)81 let vertical = SplitEngine.paneRects(for: SplitEngine.changingAxis(of: split, to: .vertical), in: size, spacing: 0)82 #expect(vertical[1] == CGRect(x: 0, y: 400, width: 1_200, height: 200))83 }8485 @Test(arguments: 2...8)86 func gridRectsTileTheContainer(count: Int) {87 let split = layout(count, axis: .grid)88 for container in [size, CGSize(width: 600, height: 1_000)] {89 let rects = SplitEngine.paneRects(for: split, in: container, spacing: 0)90 #expect(rects.count == count)91 let area = rects.reduce(0.0) { $0 + $1.width * $1.height }92 #expect(abs(area - container.width * container.height) < 1e-6)93 for (index, a) in rects.enumerated() {94 #expect(a.minX >= -1e-9 && a.minY >= -1e-9 && a.maxX <= container.width + 1e-9 && a.maxY <= container.height + 1e-9)95 for b in rects[(index + 1)...] {96 let overlap = a.intersection(b)97 #expect(overlap.isNull || overlap.width * overlap.height < 1e-6)98 }99 }100 }101 }102103 @Test func gridArrangements() {104 #expect((2...8).map(SplitEngine.gridRows(paneCount:)) == [[2], [2, 1], [2, 2], [3, 2], [3, 3], [4, 3], [4, 4]])105 let five = SplitEngine.paneRects(for: layout(5, axis: .grid), in: size, spacing: 0)106 #expect(abs(five[0].width - 400) < 1e-9 && five[0].height == 400 && five[0].origin == .zero)107 #expect(abs(five[2].maxX - 1_200) < 1e-9)108 #expect(five[3] == CGRect(x: 0, y: 400, width: 600, height: 400))109 // Portrait: lines become columns.110 let portrait = SplitEngine.paneRects(for: layout(3, axis: .grid), in: CGSize(width: 600, height: 1_000), spacing: 0)111 #expect(portrait[0] == CGRect(x: 0, y: 0, width: 300, height: 500))112 #expect(portrait[2] == CGRect(x: 300, y: 0, width: 300, height: 1_000))113 }114115 @Test func dividersSitBetweenNeighbours() {116 #expect(SplitEngine.dividerIndices(for: layout(4)) == [0, 1, 2])117 #expect(SplitEngine.dividerIndices(for: layout(5, axis: .grid)) == [0, 1, 3])118 #expect(SplitEngine.dividerIndices(for: layout(3, axis: .grid)) == [0])119 let dividers = SplitEngine.dividers(for: layout(2), in: size, spacing: 2, hitThickness: 8)120 #expect(dividers.count == 1 && dividers[0].isVertical)121 #expect(abs(dividers[0].rect.midX - 600) < 1e-9 && dividers[0].rect.width == 8)122 let stacked = SplitEngine.dividers(for: layout(2, axis: .vertical), in: size, spacing: 2)123 #expect(!stacked[0].isVertical && abs(stacked[0].rect.midY - 400) < 1e-9)124 }125126 @Test func resizingMovesOnlyTheTwoNeighbours() throws {127 let split = layout(3)128 let resized = try SplitEngine.resizing(split, dividerAfter: 0, by: 100, in: size, minimumLength: 200, spacing: 0)129 let rects = SplitEngine.paneRects(for: resized, in: size, spacing: 0)130 #expect(abs(rects[0].width - 500) < 1e-9 && abs(rects[1].width - 300) < 1e-9 && abs(rects[2].width - 400) < 1e-9)131 #expect(abs(resized.fractions.reduce(0, +) - 1) < 1e-12)132 }133134 @Test func resizingRespectsMinimumSizes() throws {135 let split = layout(3)136 let far = try SplitEngine.resizing(split, dividerAfter: 0, by: 10_000, in: size, minimumLength: 200, spacing: 0)137 #expect(abs(SplitEngine.paneRects(for: far, in: size, spacing: 0)[1].width - 200) < 1e-9)138 let back = try SplitEngine.resizing(split, dividerAfter: 0, by: -10_000, in: size, minimumLength: 200, spacing: 0)139 #expect(abs(SplitEngine.paneRects(for: back, in: size, spacing: 0)[0].width - 200) < 1e-9)140 // Container too small for the minimum: the pair splits evenly instead of collapsing a pane.141 let small = CGSize(width: 300, height: 300)142 let cramped = try SplitEngine.resizing(layout(2), dividerAfter: 0, by: 500, in: small, minimumLength: 240, spacing: 0)143 #expect(abs(SplitEngine.paneRects(for: cramped, in: small, spacing: 0)[0].width - 150) < 1e-9)144 #expect(throws: SplitError.dividerOutOfRange) { try SplitEngine.resizing(split, dividerAfter: 2, by: 1, in: size) }145 #expect(try SplitEngine.resizing(split, dividerAfter: 0, by: .nan, in: size) == split)146 }147148 @Test func resizingInAGridStaysInsideTheRow() throws {149 let split = layout(5, axis: .grid)150 #expect(throws: SplitError.dividerOutOfRange) { try SplitEngine.resizing(split, dividerAfter: 2, by: 10, in: size) }151 let resized = try SplitEngine.resizing(split, dividerAfter: 3, by: 100, in: size, minimumLength: 100, spacing: 0)152 let rects = SplitEngine.paneRects(for: resized, in: size, spacing: 0)153 #expect(abs(rects[3].width - 700) < 1e-9 && abs(rects[4].width - 500) < 1e-9)154 #expect(abs(rects[0].width - 400) < 1e-9) // the other row is untouched155 }156157 @Test func equalizeAndHitTest() {158 let split = SplitLayout(panes: Array(tabs.prefix(2)), fractions: [0.8, 0.2], focusedPane: 1)159 #expect(SplitEngine.equalized(split).fractions == [0.5, 0.5])160 #expect(SplitEngine.equalized(split).focusedPane == 1)161 #expect(SplitEngine.pane(at: CGPoint(x: 1_000, y: 10), layout: split, in: size) == 1)162 #expect(SplitEngine.pane(at: CGPoint(x: -5, y: 10), layout: split, in: size) == nil)163 }164165 @Test func restoreDropsMissingTabsAndRespectsTheDeviceLimit() {166 let stored = SplitLayout(axis: .grid, panes: Array(tabs.prefix(4)), fractions: [0.4, 0.3, 0.2, 0.1], focusedPane: 2)167 let all = SplitEngine.restored(stored, existingTabs: Set(tabs))168 #expect(all == stored)169 let missing = SplitEngine.restored(stored, existingTabs: [tabs[0], tabs[2], tabs[3]])170 #expect(missing?.panes == [tabs[0], tabs[2], tabs[3]] && missing?.focusedPane == 1)171 #expect(abs((missing?.fractions[0] ?? 0) - 0.4 / 0.7) < 1e-12)172 #expect(SplitEngine.restored(stored, existingTabs: [tabs[0]]) == nil)173 #expect(SplitEngine.restored(stored, existingTabs: Set(tabs), limit: 2)?.panes == [tabs[0], tabs[1]])174 #expect(SplitEngine.restored(stored, existingTabs: Set(tabs), limit: 1) == nil)175 #expect(SplitEngine.restored(nil, existingTabs: Set(tabs)) == nil)176 // Corrupt stored values: duplicates and a broken fraction.177 var corrupt = stored178 corrupt.panes = [tabs[0], tabs[0], tabs[1]]179 corrupt.fractions = [0.5, .nan, -1]180 let repaired = SplitEngine.restored(corrupt, existingTabs: Set(tabs))181 #expect(repaired?.panes == [tabs[0], tabs[1]] && repaired?.fractions == [0.5, 0.5])182 }183184 @Test(arguments: [185 (SplitPlatform.phone, UInt64(8), 1), (.phone, 16, 1),186 (.pad(isRegularWidth: false), 16, 2), (.pad(isRegularWidth: true), 4, 2), (.pad(isRegularWidth: true), 8, 3),187 (.mac, 8, 4), (.mac, 16, 6), (.mac, 18, 6), (.mac, 24, 8), (.mac, 96, 8),188 ])189 func paneLimitsByDevice(platform: SplitPlatform, gigabytes: UInt64, expected: Int) {190 #expect(SplitCapacity.maximumPanes(platform: platform, physicalMemoryBytes: gigabytes * gigabyte) == expected)191 }192193 @Test func thisDeviceHasAUsableLimit() {194 #expect((1...8).contains(SplitCapacity.maximumPanesOnThisDevice()))195 #if os(macOS)196 #expect(SplitCapacity.currentPlatform == .mac)197 #endif198 }199}200