示例工程 40-windows-scenes-macos

40-windows-scenes-macos/Tests/Ch40KitTests/WindowTests.swift

swift · 3.1 KB

import SwiftUI
import Testing
 
@testable import Ch40Kit
 
@Suite("窗口规格")
struct WindowSpecTests {
    @Test func idsAreUnique() {
        let ids = WindowCatalog.all.map(\.id)
        #expect(Set(ids).count == ids.count)
    }
 
    @Test func allSpecsAreValid() {
        for spec in WindowCatalog.all {
            #expect(spec.isValid)
        }
    }
 
    @Test func lookupByID() {
        #expect(WindowCatalog.spec(id: "stats")?.title == "统计")
        #expect(WindowCatalog.spec(id: "missing") == nil)
    }
 
    @Test func invalidSizeIsRejected() {
        let spec = WindowSpec(
            id: "bad",
            title: "坏",
            width: 100,
            height: 100,
            minWidth: 200,
            minHeight: 200
        )
        #expect(!spec.isValid)
    }
}
 
@Suite("共享模型")
struct WorkspaceTests {
    @Test func documentsAccumulate() {
        let workspace = Workspace()
        workspace.addDocument()
        workspace.addDocument()
        #expect(workspace.documentCount == 2)
    }
 
    @Test @MainActor func twoWindowsShareOneInstance() {
        let workspace = Workspace()
        let main = MainWindow(workspace: workspace)
        let stats = StatsWindow(workspace: workspace)
        workspace.addDocument()
        #expect(main.workspace === stats.workspace)
        #expect(main.workspace.documentCount == 1)
    }
}
 
@Suite("视图")
struct WindowViewTests {
    @Test @MainActor func boardRendersAtFixedSize() throws {
        let image = try #require(ImageRenderer(content: WindowBoard()).cgImage)
        #expect(image.width == 360)
        #expect(image.height > 150)
    }
}
 
@Suite("截图")
struct ScreenshotTests {
    @MainActor
    private func writeShot(
        _ view: some View,
        name: String,
        expectedWidth: Int,
        minHeight: Int
    ) throws {
        let renderer = ImageRenderer(content: view)
        renderer.scale = 2
        let image = try #require(renderer.cgImage)
        #expect(image.width == expectedWidth)
        #expect(image.height >= minHeight)
 
        let url = URL(fileURLWithPath: #filePath)
            .deletingLastPathComponent()
            .deletingLastPathComponent()
            .deletingLastPathComponent()
            .appendingPathComponent("shots/\(name)")
        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)
    }
 
    @Test @MainActor func boardShot() throws {
        try writeShot(WindowBoard(), name: "window-board.png", expectedWidth: 720, minHeight: 180)
    }
 
    @Test @MainActor func mainShot() throws {
        try writeShot(
            MainWindowBoard(), name: "window-main.png", expectedWidth: 720, minHeight: 120)
    }
 
    @Test @MainActor func settingsShot() throws {
        try writeShot(
            SettingsWindowBoard(), name: "window-settings.png", expectedWidth: 720, minHeight: 140)
    }
}