Skip to content

Commit d77b63a

Browse files
authored
Fix generic view introspection tests and document behavior (#470)
1 parent b7577c4 commit d77b63a

File tree

2 files changed

+56
-38
lines changed

2 files changed

+56
-38
lines changed

Sources/ViewTypes/View.swift

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
#if !os(watchOS)
22
/// An abstract representation of a generic SwiftUI view type.
33
///
4+
/// Note: prior to iOS 26, primitive views like `Text`, `Image`, `Button`, and layout
5+
/// stacks were drawn inside a subclass of `UIView` called `_UIGraphicsView` which was
6+
/// introspectable via `.introspect(.view)`, however starting iOS 26 this is no longer the
7+
/// case and all SwiftUI primitives seem to somehow be drawn without an underlying
8+
/// `UIView` vessel.
9+
///
410
/// ### iOS
511
///
612
/// ```swift
713
/// struct ContentView: View {
814
/// var body: some View {
9-
/// HStack {
10-
/// Image(systemName: "scribble")
11-
/// Text("Some text")
12-
/// }
13-
/// .introspect(.view, on: .iOS(.v13, .v14, .v15, .v16, .v17, .v18, .v26)) {
14-
/// print(type(of: $0)) // some subclass of UIView
15-
/// }
15+
/// ExampleUIViewRepresentable()
16+
/// .introspect(.view, on: .iOS(.v13, .v14, .v15, .v16, .v17, .v18, .v26)) {
17+
/// print(type(of: $0)) // some subclass of UIView
18+
/// }
1619
/// }
1720
/// }
1821
/// ```
@@ -22,13 +25,10 @@
2225
/// ```swift
2326
/// struct ContentView: View {
2427
/// var body: some View {
25-
/// HStack {
26-
/// Image(systemName: "scribble")
27-
/// Text("Some text")
28-
/// }
29-
/// .introspect(.view, on: .tvOS(.v13, .v14, .v15, .v16, .v17, .v18, .v26)) {
30-
/// print(type(of: $0)) // some subclass of UIView
31-
/// }
28+
/// ExampleUIViewRepresentable()
29+
/// .introspect(.view, on: .tvOS(.v13, .v14, .v15, .v16, .v17, .v18, .v26)) {
30+
/// print(type(of: $0)) // some subclass of UIView
31+
/// }
3232
/// }
3333
/// }
3434
/// ```
@@ -38,13 +38,10 @@
3838
/// ```swift
3939
/// struct ContentView: View {
4040
/// var body: some View {
41-
/// HStack {
42-
/// Image(systemName: "scribble")
43-
/// Text("Some text")
44-
/// }
45-
/// .introspect(.view, on: .macOS(.v10_15, .v11, .v12, .v13, .v14, .v15, .v26)) {
46-
/// print(type(of: $0)) // some subclass of NSView
47-
/// }
41+
/// ExampleUIViewRepresentable()
42+
/// .introspect(.view, on: .macOS(.v10_15, .v11, .v12, .v13, .v14, .v15, .v26)) {
43+
/// print(type(of: $0)) // some subclass of NSView
44+
/// }
4845
/// }
4946
/// }
5047
/// ```
@@ -54,13 +51,10 @@
5451
/// ```swift
5552
/// struct ContentView: View {
5653
/// var body: some View {
57-
/// HStack {
58-
/// Image(systemName: "scribble")
59-
/// Text("Some text")
60-
/// }
61-
/// .introspect(.view, on: .visionOS(.v1, .v2, .v26)) {
62-
/// print(type(of: $0)) // some subclass of UIView
63-
/// }
54+
/// ExampleUIViewRepresentable()
55+
/// .introspect(.view, on: .visionOS(.v1, .v2, .v26)) {
56+
/// print(type(of: $0)) // some subclass of UIView
57+
/// }
6458
/// }
6559
/// }
6660
/// ```
Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import SwiftUI
2-
import SwiftUIIntrospect
2+
@testable import SwiftUIIntrospect
33
import XCTest
44

55
@MainActor
@@ -8,33 +8,57 @@ final class ViewTests: XCTestCase {
88
XCTAssertViewIntrospection(of: PlatformView.self) { spies in
99
let spy0 = spies[0]
1010
let spy1 = spies[1]
11-
let spy2 = spies[2]
1211

1312
VStack(spacing: 10) {
14-
Image(systemName: "scribble").resizable().frame(height: 30)
13+
SUTView().frame(height: 30)
1514
#if os(iOS) || os(tvOS) || os(visionOS)
1615
.introspect(.view, on: .iOS(.v13, .v14, .v15, .v16, .v17, .v18, .v26), .tvOS(.v13, .v14, .v15, .v16, .v17, .v18, .v26), .visionOS(.v1, .v2, .v26), customize: spy0)
1716
#elseif os(macOS)
1817
.introspect(.view, on: .macOS(.v10_15, .v11, .v12, .v13, .v14, .v15, .v26), customize: spy0)
1918
#endif
2019

21-
Text("Text").frame(height: 40)
20+
SUTView().frame(height: 40)
2221
#if os(iOS) || os(tvOS) || os(visionOS)
2322
.introspect(.view, on: .iOS(.v13, .v14, .v15, .v16, .v17, .v18, .v26), .tvOS(.v13, .v14, .v15, .v16, .v17, .v18, .v26), .visionOS(.v1, .v2, .v26), customize: spy1)
2423
#elseif os(macOS)
2524
.introspect(.view, on: .macOS(.v10_15, .v11, .v12, .v13, .v14, .v15, .v26), customize: spy1)
2625
#endif
2726
}
2827
.padding(10)
29-
#if os(iOS) || os(tvOS) || os(visionOS)
30-
.introspect(.view, on: .iOS(.v13, .v14, .v15, .v16, .v17, .v18, .v26), .tvOS(.v13, .v14, .v15, .v16, .v17, .v18, .v26), .visionOS(.v1, .v2, .v26), customize: spy2)
31-
#elseif os(macOS)
32-
.introspect(.view, on: .macOS(.v10_15, .v11, .v12, .v13, .v14, .v15, .v26), customize: spy2)
33-
#endif
3428
} extraAssertions: {
3529
XCTAssertEqual($0[safe: 0]?.frame.height, 30)
3630
XCTAssertEqual($0[safe: 1]?.frame.height, 40)
37-
XCTAssertEqual($0[safe: 2]?.frame.height, 100)
3831
}
3932
}
4033
}
34+
35+
struct SUTView: PlatformViewControllerRepresentable {
36+
#if canImport(UIKit)
37+
typealias UIViewControllerType = PlatformViewController
38+
#elseif canImport(AppKit)
39+
typealias NSViewControllerType = PlatformViewController
40+
#endif
41+
42+
func makePlatformViewController(context: Context) -> PlatformViewController {
43+
let controller = PlatformViewController(nibName: nil, bundle: nil)
44+
controller.view.translatesAutoresizingMaskIntoConstraints = false
45+
46+
let widthConstraint = controller.view.widthAnchor.constraint(greaterThanOrEqualToConstant: .greatestFiniteMagnitude)
47+
widthConstraint.priority = .defaultLow
48+
49+
let heightConstraint = controller.view.heightAnchor.constraint(greaterThanOrEqualToConstant: .greatestFiniteMagnitude)
50+
heightConstraint.priority = .defaultLow
51+
52+
NSLayoutConstraint.activate([widthConstraint, heightConstraint])
53+
54+
return controller
55+
}
56+
57+
func updatePlatformViewController(_ controller: PlatformViewController, context: Context) {
58+
// NO-OP
59+
}
60+
61+
static func dismantlePlatformViewController(_ controller: PlatformViewController, coordinator: Coordinator) {
62+
// NO-OP
63+
}
64+
}

0 commit comments

Comments
 (0)