示例工程 33-layout-advanced
33-layout-advanced/Tests/Ch33KitTests/AdvancedLayoutTests.swift
import AppKit
import SwiftUI
import Testing
@testable import Ch33Kit
@Suite("流式布局")
struct FlowEngineTests {
private let engine = FlowEngine(spacing: 8)
@Test func wrapsIntoThreeRows() {
let result = engine.layout(
widths: Array(repeating: 40, count: 5),
heights: Array(repeating: 20, count: 5),
maxWidth: 100
)
#expect(result.rows == 3)
#expect(result.size == CGSize(width: 88, height: 76))
}
@Test func secondRowStartsAtOrigin() {
let result = engine.layout(
widths: Array(repeating: 40, count: 5),
heights: Array(repeating: 20, count: 5),
maxWidth: 100
)
#expect(result.placements[2] == FlowEngine.Placement(x: 0, y: 28))
#expect(result.placements[3] == FlowEngine.Placement(x: 48, y: 28))
}
@Test func singleRowKeepsHeight() {
let result = engine.layout(widths: [30, 30], heights: [10, 14], maxWidth: 200)
#expect(result.rows == 1)
#expect(result.size == CGSize(width: 68, height: 14))
}
@Test @MainActor func layoutProtocolMatchesEngine() throws {
let view = FlowItems().frame(width: 100, alignment: .leading)
let image = try #require(ImageRenderer(content: view).cgImage)
#expect(image.width == 100)
#expect(image.height == 76)
}
}
@Suite("进阶布局")
struct AdvancedLayoutTests {
@Test @MainActor func viewThatFitsPicksTheFirstFittingBranch() throws {
let narrow = try #require(
ImageRenderer(content: FitChoice().frame(width: 200, alignment: .leading)).cgImage)
let narrowColor = try #require(
NSBitmapImageRep(cgImage: narrow).colorAt(x: 4, y: 4)?.usingColorSpace(.sRGB)
)
#expect(narrowColor.blueComponent > narrowColor.redComponent)
let wide = try #require(
ImageRenderer(content: FitChoice().frame(width: 400, alignment: .leading)).cgImage)
let wideColor = try #require(
NSBitmapImageRep(cgImage: wide).colorAt(x: 4, y: 4)?.usingColorSpace(.sRGB)
)
#expect(wideColor.redComponent > wideColor.blueComponent)
}
@Test @MainActor func anyLayoutChangesOrientationSize() throws {
let row = try #require(ImageRenderer(content: LayoutSwitch(horizontal: true)).cgImage)
let column = try #require(ImageRenderer(content: LayoutSwitch(horizontal: false)).cgImage)
#expect(row.width == 48 && row.height == 20)
#expect(column.width == 20 && column.height == 48)
}
@Test @MainActor func gridAddsColumnWidths() throws {
let image = try #require(ImageRenderer(content: SimpleGrid()).cgImage)
#expect(image.width == 80)
#expect(image.height == 20)
}
@Test func flowLayoutIsSmallValue() {
#expect(MemoryLayout<FlowLayout>.size <= 16)
}
}
@Suite("截图")
struct ScreenshotTests {
@Test @MainActor func shotMatchesExpectedSize() throws {
let renderer = ImageRenderer(content: AdvancedBoard())
renderer.scale = 2
let image = try #require(renderer.cgImage)
#expect(image.width > 300)
#expect(image.height > 300)
let url = URL(fileURLWithPath: #filePath)
.deletingLastPathComponent()
.deletingLastPathComponent()
.deletingLastPathComponent()
.appendingPathComponent("shots/advanced-layouts.png")
guard !FileManager.default.fileExists(atPath: url.path) else { return }
let data = try #require(
NSBitmapImageRep(cgImage: image).representation(using: .png, properties: [:])
)
try FileManager.default.createDirectory(
at: url.deletingLastPathComponent(),
withIntermediateDirectories: true
)
try data.write(to: url)
}
}