Download this file
| 1 | #if os(macOS) |
| 2 | import AppKit |
| 3 | import SwiftUI |
| 4 | import Observation |
| 5 | import Testing |
| 6 | @testable import EquipoMac |
| 7 | |
| 8 | @MainActor @Observable private final class GrowingContent { var expanded = false } |
| 9 | @MainActor @Suite(.serialized) struct ResizingHostTests { |
| 10 | @Test func lateContentRemeasuresItsHost() async throws { |
| 11 | let state = GrowingContent() |
| 12 | var heights: [CGFloat] = [] |
| 13 | let host = ResizingHostingView(content: GrowingView(state: state), width: 300) { _, height in heights.append(height) } |
| 14 | let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 400, height: 400), styleMask: [.borderless], backing: .buffered, defer: false) |
| 15 | window.contentView = host |
| 16 | defer { window.contentView = nil } |
| 17 | window.orderFront(nil) |
| 18 | func waitForHeight(_ height: CGFloat) async throws { |
| 19 | for _ in 0..<250 { |
| 20 | host.layoutSubtreeIfNeeded() |
| 21 | if heights.last == height { return } |
| 22 | try await Task.sleep(for: .milliseconds(20)) |
| 23 | } |
| 24 | #expect(heights.last == height) |
| 25 | } |
| 26 | try await waitForHeight(30) |
| 27 | state.expanded = true |
| 28 | try await waitForHeight(160) |
| 29 | state.expanded = false |
| 30 | try await waitForHeight(30) |
| 31 | window.orderOut(nil) |
| 32 | } |
| 33 | } |
| 34 | private struct GrowingView: View { |
| 35 | var state: GrowingContent |
| 36 | var body: some View { Color.clear.frame(height: state.expanded ? 160 : 30) } |
| 37 | } |
| 38 | #endif |
| 39 | |