Skip to content

Add view controller introspection #298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Changelog

## master

- Added: view controller introspection (#298)
- Added: page control introspection (#297)

## [0.8.0]
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ Introspection
- [`Toggle` with `switch` style](https://swiftpackageindex.com/siteline/swiftui-introspect/master/documentation/swiftuiintrospect/togglewithswitchstyletype)
- [`VideoPlayer`](https://swiftpackageindex.com/siteline/swiftui-introspect/master/documentation/swiftuiintrospect/videoplayertype)
- [`View`](https://swiftpackageindex.com/siteline/swiftui-introspect/master/documentation/swiftuiintrospect/viewtype)
- [`ViewController`](https://swiftpackageindex.com/siteline/swiftui-introspect/master/documentation/swiftuiintrospect/viewcontrollertype)
- [`Window`](https://swiftpackageindex.com/siteline/swiftui-introspect/master/documentation/swiftuiintrospect/windowtype)

**Missing an element?** Please [create an issue](https://github.com/timbersoftware/SwiftUI-Introspect/issues). As a temporary solution, you can [implement your own introspectable view type](#implement-your-own-view-type).
Expand Down
72 changes: 72 additions & 0 deletions Sources/ViewTypes/ViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import SwiftUI

/// An abstract representation of the receiving SwiftUI view's view controller,
/// or the closest ancestor view controller if missing.
///
/// ### iOS
///
/// ```swift
/// struct ContentView: View {
/// var body: some View {
/// NavigationView {
/// Text("Root").frame(maxWidth: .infinity, maxHeight: .infinity).background(Color.red)
/// .introspect(.viewController, on: .iOS(.v13, .v14, .v15, .v16, .v17)) {
/// print(type(of: $0)) // some subclass of UIHostingController
/// }
/// }
/// .navigationViewStyle(.stack)
/// .introspect(.viewController, on: .iOS(.v13, .v14, .v15, .v16, .v17)) {
/// print(type(of: $0)) // UINavigationController
/// }
/// }
/// }
/// ```
///
/// ### tvOS
///
/// ```swift
/// struct ContentView: View {
/// var body: some View {
/// NavigationView {
/// Text("Root").frame(maxWidth: .infinity, maxHeight: .infinity).background(Color.red)
/// .introspect(.viewController, on: .tvOS(.v13, .v14, .v15, .v16, .v17)) {
/// print(type(of: $0)) // some subclass of UIHostingController
/// }
/// }
/// .navigationViewStyle(.stack)
/// .introspect(.viewController, on: .tvOS(.v13, .v14, .v15, .v16, .v17)) {
/// print(type(of: $0)) // UINavigationController
/// }
/// }
/// }
/// ```
///
/// ### macOS
///
/// Not available.
///
public struct ViewControllerType: IntrospectableViewType {
public var scope: IntrospectionScope { [.receiver, .ancestor] }
}

extension IntrospectableViewType where Self == ViewControllerType {
public static var viewController: Self { .init() }
}

#if canImport(UIKit)
extension iOSViewVersion<ViewControllerType, UIViewController> {
public static let v13 = Self(for: .v13)
public static let v14 = Self(for: .v14)
public static let v15 = Self(for: .v15)
public static let v16 = Self(for: .v16)
public static let v17 = Self(for: .v17)
}

extension tvOSViewVersion<ViewControllerType, UIViewController> {
public static let v13 = Self(for: .v13)
public static let v14 = Self(for: .v14)
public static let v15 = Self(for: .v15)
public static let v16 = Self(for: .v16)
public static let v17 = Self(for: .v17)
}
#endif
4 changes: 4 additions & 0 deletions Tests/Tests.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
D5F0BE4D29C0DBE800AD95AB /* TestsHostApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5F0BE4C29C0DBE800AD95AB /* TestsHostApp.swift */; };
D5F0BE6A29C0DC4900AD95AB /* PlatformVersionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5F0BE6729C0DC4900AD95AB /* PlatformVersionTests.swift */; };
D5F26E022A561130001209E6 /* PageControlTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5F26E012A561130001209E6 /* PageControlTests.swift */; };
D5F26E042A56E74B001209E6 /* ViewControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5F26E032A56E74B001209E6 /* ViewControllerTests.swift */; };
D5F8D5ED2A1E7B490054E9AB /* NavigationViewWithStackStyleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5F8D5EC2A1E7B490054E9AB /* NavigationViewWithStackStyleTests.swift */; };
D5F8D5EF2A1E87950054E9AB /* NavigationViewWithColumnsStyleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5F8D5EE2A1E87950054E9AB /* NavigationViewWithColumnsStyleTests.swift */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -198,6 +199,7 @@
D5F0BE5D29C0DC0000AD95AB /* Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = Tests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
D5F0BE6729C0DC4900AD95AB /* PlatformVersionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PlatformVersionTests.swift; sourceTree = "<group>"; };
D5F26E012A561130001209E6 /* PageControlTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PageControlTests.swift; sourceTree = "<group>"; };
D5F26E032A56E74B001209E6 /* ViewControllerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewControllerTests.swift; sourceTree = "<group>"; };
D5F8D5EC2A1E7B490054E9AB /* NavigationViewWithStackStyleTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NavigationViewWithStackStyleTests.swift; sourceTree = "<group>"; };
D5F8D5EE2A1E87950054E9AB /* NavigationViewWithColumnsStyleTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NavigationViewWithColumnsStyleTests.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -297,6 +299,7 @@
D575068B2A27D40500A628E4 /* ToggleWithSwitchStyleTests.swift */,
D503B2AB2A49BFE300027F5F /* VideoPlayerTests.swift */,
D58119C52A227E930081F853 /* ViewTests.swift */,
D5F26E032A56E74B001209E6 /* ViewControllerTests.swift */,
D534D4DB2A4A596200218BFB /* WindowTests.swift */,
);
path = ViewTypes;
Expand Down Expand Up @@ -630,6 +633,7 @@
D575068C2A27D40500A628E4 /* ToggleWithSwitchStyleTests.swift in Sources */,
D58119C42A211B8A0081F853 /* ListCellTests.swift in Sources */,
D57506A22A281B9C00A628E4 /* SearchFieldTests.swift in Sources */,
D5F26E042A56E74B001209E6 /* ViewControllerTests.swift in Sources */,
D58119C62A227E930081F853 /* ViewTests.swift in Sources */,
D575067E2A27C43400A628E4 /* ListWithGroupedStyleTests.swift in Sources */,
D575069C2A27F68700A628E4 /* ProgressViewWithCircularStyleTests.swift in Sources */,
Expand Down
46 changes: 46 additions & 0 deletions Tests/Tests/ViewTypes/ViewControllerTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#if os(iOS) || os(tvOS)
import SwiftUI
import SwiftUIIntrospect
import XCTest

final class ViewControllerTests: XCTestCase {
func testViewController() {
XCTAssertViewIntrospection(of: PlatformViewController.self) { spies in
let spy0 = spies[0]
let spy1 = spies[1]
let spy2 = spies[2]

TabView {
NavigationView {
Text("Root").frame(maxWidth: .infinity, maxHeight: .infinity).background(Color.red)
.introspect(
.viewController,
on: .iOS(.v13, .v14, .v15, .v16, .v17), .tvOS(.v13, .v14, .v15, .v16, .v17),
customize: spy2
)
}
.navigationViewStyle(.stack)
.tabItem {
Image(systemName: "1.circle")
Text("Tab 1")
}
.introspect(
.viewController,
on: .iOS(.v13, .v14, .v15, .v16, .v17), .tvOS(.v13, .v14, .v15, .v16, .v17),
customize: spy1
)
}
.introspect(
.viewController,
on: .iOS(.v13, .v14, .v15, .v16, .v17), .tvOS(.v13, .v14, .v15, .v16, .v17),
customize: spy0
)
} extraAssertions: {
XCTAssert($0[safe: 0] is UITabBarController)
XCTAssert($0[safe: 1] is UINavigationController)
XCTAssert(String(describing: $0[safe: 2]).contains("UIHostingController"))
XCTAssert($0[safe: 1] === $0[safe: 2]?.parent)
}
}
}
#endif