示例工程 54-ios-app-anatomy

54-ios-app-anatomy/Tests/Ch54KitTests/AnatomyTests.swift

swift · 3.5 KB

import SwiftUI
import Testing
 
@testable import Ch54Kit
 
@Suite("平台差异")
struct PlatformTests {
    @Test func macOSHasMenuBarAndMultipleWindows() {
        #expect(PlatformProfile.macOS.menuBar)
        #expect(PlatformProfile.macOS.multipleWindows)
    }
 
    @Test func iOSIsSingleWindowWithoutMenuBar() {
        #expect(!PlatformProfile.iOS.menuBar)
        #expect(!PlatformProfile.iOS.multipleWindows)
        #expect(PlatformProfile.iOS.safeAreaInsets)
    }
 
    @Test func minimumSystemsAreSet() {
        #expect(PlatformProfile.iOS.minimumSystem == "26.0")
        #expect(PlatformProfile.macOS.minimumSystem == "27.0")
    }
}
 
@Suite("安全区")
struct SafeAreaTests {
    @Test func notchReducesContentHeight() {
        let bounds = CGRect(x: 0, y: 0, width: 390, height: 844)
        let rect = SafeArea.phoneWithNotch.contentRect(in: bounds)
        #expect(rect.height == CGFloat(844 - 59 - 34))
        #expect(rect.width == 390)
        #expect(rect.minY == 59)
    }
 
    @Test func zeroInsetsKeepBounds() {
        let bounds = CGRect(x: 10, y: 20, width: 100, height: 200)
        let rect = SafeArea(top: 0, bottom: 0).contentRect(in: bounds)
        #expect(rect == bounds)
    }
 
    @Test func oversizedInsetsClampToZero() {
        let bounds = CGRect(x: 0, y: 0, width: 100, height: 50)
        let rect = SafeArea(top: 40, bottom: 40).contentRect(in: bounds)
        #expect(rect.height == 0)
    }
}
 
@Suite("生命周期")
struct LifecycleTests {
    @Test func launchThenActivate() {
        var state = AppState.notRunning
        state = Lifecycle.reduce(state, .launch)
        state = Lifecycle.reduce(state, .activate)
        #expect(state == .active)
    }
 
    @Test func backgroundAndForeground() {
        var state = AppState.active
        state = Lifecycle.reduce(state, .deactivate)
        state = Lifecycle.reduce(state, .background)
        #expect(state == .background)
        state = Lifecycle.reduce(state, .foreground)
        #expect(state == .inactive)
    }
 
    @Test func terminateIsFinal() {
        let state = Lifecycle.reduce(.background, .terminate)
        #expect(state == .terminated)
        #expect(!Lifecycle.canAnimate(state))
    }
 
    @Test func unknownTransitionsAreIgnored() {
        #expect(Lifecycle.reduce(.notRunning, .background) == .notRunning)
    }
}
 
@Suite("视图")
struct AnatomyViewTests {
    @Test @MainActor func boardRendersAtFixedSize() throws {
        let image = try #require(ImageRenderer(content: AnatomyBoard()).cgImage)
        #expect(image.width == 340)
        #expect(image.height > 120)
    }
}
 
@Suite("截图")
struct ScreenshotTests {
    @Test @MainActor func shotMatchesExpectedWidth() throws {
        let renderer = ImageRenderer(content: AnatomyBoard())
        renderer.scale = 2
        let image = try #require(renderer.cgImage)
        #expect(image.width == 680)
 
        let url = URL(fileURLWithPath: #filePath)
            .deletingLastPathComponent()
            .deletingLastPathComponent()
            .deletingLastPathComponent()
            .appendingPathComponent("shots/ios-anatomy-board.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)
    }
}