Skip to content

Commit 8c1140f

Browse files
authored
Add page control introspection (#297)
1 parent b867870 commit 8c1140f

File tree

7 files changed

+111
-8
lines changed

7 files changed

+111
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ Changelog
33

44
## master
55

6+
- Added: page control introspection (#297)
7+
68
## [0.8.0]
79

810
- Added: `Map` introspection (#288)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ Introspection
109109
- [`NavigationStack`](https://swiftpackageindex.com/siteline/swiftui-introspect/master/documentation/swiftuiintrospect/navigationstacktype)
110110
- [`NavigationView` with `.columns` style](https://swiftpackageindex.com/siteline/swiftui-introspect/master/documentation/swiftuiintrospect/NavigationViewWithColumnsStyleType)
111111
- [`NavigationView` with `.stack` style](https://swiftpackageindex.com/siteline/swiftui-introspect/master/documentation/swiftuiintrospect/NavigationViewWithStackStyleType)
112+
- [`PageControl`](https://swiftpackageindex.com/siteline/swiftui-introspect/master/documentation/swiftuiintrospect/pagecontroltype)
112113
- [`Picker` with `.menu` style](https://swiftpackageindex.com/siteline/swiftui-introspect/master/documentation/swiftuiintrospect/pickerwithmenustyletype)
113114
- [`Picker` with `.segmented` style](https://swiftpackageindex.com/siteline/swiftui-introspect/master/documentation/swiftuiintrospect/pickerwithsegmentedstyletype)
114115
- [`Picker` with `.wheel` style](https://swiftpackageindex.com/siteline/swiftui-introspect/master/documentation/swiftuiintrospect/pickerwithwheelstyletype)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import SwiftUI
2+
3+
/// An abstract representation of the page control type in SwiftUI.
4+
///
5+
/// ### iOS
6+
///
7+
/// ```swift
8+
/// struct ContentView: View {
9+
/// var body: some View {
10+
/// TabView {
11+
/// Text("Page 1").frame(maxWidth: .infinity, maxHeight: .infinity).background(Color.red)
12+
/// Text("Page 2").frame(maxWidth: .infinity, maxHeight: .infinity).background(Color.blue)
13+
/// }
14+
/// .tabViewStyle(.page(indexDisplayMode: .always))
15+
/// .introspect(.pageControl, on: .iOS(.v14, .v15, .v16, .v17)) {
16+
/// print(type(of: $0)) // UIPageControl
17+
/// }
18+
/// }
19+
/// }
20+
/// ```
21+
///
22+
/// ### tvOS
23+
///
24+
/// ```swift
25+
/// struct ContentView: View {
26+
/// var body: some View {
27+
/// TabView {
28+
/// Text("Page 1").frame(maxWidth: .infinity, maxHeight: .infinity).background(Color.red)
29+
/// Text("Page 2").frame(maxWidth: .infinity, maxHeight: .infinity).background(Color.blue)
30+
/// }
31+
/// .tabViewStyle(.page(indexDisplayMode: .always))
32+
/// .introspect(.pageControl, on: .tvOS(.v14, .v15, .v16, .v17)) {
33+
/// print(type(of: $0)) // UIPageControl
34+
/// }
35+
/// }
36+
/// }
37+
/// ```
38+
///
39+
/// ### macOS
40+
///
41+
/// Not available.
42+
///
43+
public struct PageControlType: IntrospectableViewType {}
44+
45+
extension IntrospectableViewType where Self == PageControlType {
46+
public static var pageControl: Self { .init() }
47+
}
48+
49+
#if canImport(UIKit)
50+
extension iOSViewVersion<PageControlType, UIPageControl> {
51+
@available(*, unavailable, message: ".tabViewStyle(.page) isn't available on iOS 13")
52+
public static let v13 = Self(for: .v13)
53+
public static let v14 = Self(for: .v14)
54+
public static let v15 = Self(for: .v15)
55+
public static let v16 = Self(for: .v16)
56+
public static let v17 = Self(for: .v17)
57+
}
58+
59+
extension tvOSViewVersion<PageControlType, UIPageControl> {
60+
@available(*, unavailable, message: ".tabViewStyle(.page) isn't available on tvOS 13")
61+
public static let v13 = Self(for: .v13)
62+
public static let v14 = Self(for: .v14)
63+
public static let v15 = Self(for: .v15)
64+
public static let v16 = Self(for: .v16)
65+
public static let v17 = Self(for: .v17)
66+
}
67+
#endif

Sources/ViewTypes/TabViewWithPageStyle.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ extension IntrospectableViewType where Self == TabViewWithPageStyleType {
5353

5454
#if canImport(UIKit)
5555
extension iOSViewVersion<TabViewWithPageStyleType, UICollectionView> {
56-
@available(*, unavailable, message: "TabView {}.tabViewStyle(.page) isn't available on iOS 13")
56+
@available(*, unavailable, message: ".tabViewStyle(.page) isn't available on iOS 13")
5757
public static let v13 = Self.unavailable()
5858
public static let v14 = Self(for: .v14)
5959
public static let v15 = Self(for: .v15)
@@ -62,7 +62,7 @@ extension iOSViewVersion<TabViewWithPageStyleType, UICollectionView> {
6262
}
6363

6464
extension tvOSViewVersion<TabViewWithPageStyleType, UICollectionView> {
65-
@available(*, unavailable, message: "TabView {}.tabViewStyle(.page) isn't available on tvOS 13")
65+
@available(*, unavailable, message: ".tabViewStyle(.page) isn't available on tvOS 13")
6666
public static let v13 = Self.unavailable()
6767
public static let v14 = Self(for: .v14)
6868
public static let v15 = Self(for: .v15)

Tests/Tests.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
D5B67B842A0D318F007D5D9B /* TextFieldTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5B67B832A0D318F007D5D9B /* TextFieldTests.swift */; };
112112
D5F0BE4D29C0DBE800AD95AB /* TestsHostApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5F0BE4C29C0DBE800AD95AB /* TestsHostApp.swift */; };
113113
D5F0BE6A29C0DC4900AD95AB /* PlatformVersionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5F0BE6729C0DC4900AD95AB /* PlatformVersionTests.swift */; };
114+
D5F26E022A561130001209E6 /* PageControlTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5F26E012A561130001209E6 /* PageControlTests.swift */; };
114115
D5F8D5ED2A1E7B490054E9AB /* NavigationViewWithStackStyleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5F8D5EC2A1E7B490054E9AB /* NavigationViewWithStackStyleTests.swift */; };
115116
D5F8D5EF2A1E87950054E9AB /* NavigationViewWithColumnsStyleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5F8D5EE2A1E87950054E9AB /* NavigationViewWithColumnsStyleTests.swift */; };
116117
/* End PBXBuildFile section */
@@ -196,6 +197,7 @@
196197
D5F0BE4C29C0DBE800AD95AB /* TestsHostApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestsHostApp.swift; sourceTree = "<group>"; };
197198
D5F0BE5D29C0DC0000AD95AB /* Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = Tests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
198199
D5F0BE6729C0DC4900AD95AB /* PlatformVersionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PlatformVersionTests.swift; sourceTree = "<group>"; };
200+
D5F26E012A561130001209E6 /* PageControlTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PageControlTests.swift; sourceTree = "<group>"; };
199201
D5F8D5EC2A1E7B490054E9AB /* NavigationViewWithStackStyleTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NavigationViewWithStackStyleTests.swift; sourceTree = "<group>"; };
200202
D5F8D5EE2A1E87950054E9AB /* NavigationViewWithColumnsStyleTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NavigationViewWithColumnsStyleTests.swift; sourceTree = "<group>"; };
201203
/* End PBXFileReference section */
@@ -269,6 +271,7 @@
269271
D58547F72A1CDD740068ADF4 /* NavigationStackTests.swift */,
270272
D5F8D5EE2A1E87950054E9AB /* NavigationViewWithColumnsStyleTests.swift */,
271273
D5F8D5EC2A1E7B490054E9AB /* NavigationViewWithStackStyleTests.swift */,
274+
D5F26E012A561130001209E6 /* PageControlTests.swift */,
272275
D57506792A27BF6C00A628E4 /* PickerWithMenuStyleTests.swift */,
273276
D57506772A27BBBD00A628E4 /* PickerWithSegmentedStyleTests.swift */,
274277
D58119D52A23AED70081F853 /* PickerWithWheelStyleTests.swift */,
@@ -604,6 +607,7 @@
604607
D58119D02A23A62C0081F853 /* SliderTests.swift in Sources */,
605608
D5ADFAD02A4A3E54009494FD /* PopoverTests.swift in Sources */,
606609
D58119D82A23B3B00081F853 /* ButtonTests.swift in Sources */,
610+
D5F26E022A561130001209E6 /* PageControlTests.swift in Sources */,
607611
D55F448D2A1FF209003381E4 /* ListTests.swift in Sources */,
608612
D5AAF56F2A502EF000CAFFB6 /* MapTests.swift in Sources */,
609613
D58547FA2A1D12270068ADF4 /* NavigationSplitViewTests.swift in Sources */,
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#if !os(macOS) && !LEGACY_MACOS_SDK
2+
import SwiftUI
3+
import SwiftUIIntrospect
4+
import XCTest
5+
6+
@available(iOS 14, tvOS 14, *)
7+
final class PageControlTests: XCTestCase {
8+
#if canImport(UIKit)
9+
typealias PlatformPageControl = UIPageControl
10+
#endif
11+
12+
func testPageControl() throws {
13+
guard #available(iOS 14, tvOS 14, *) else {
14+
throw XCTSkip()
15+
}
16+
17+
XCTAssertViewIntrospection(of: PlatformPageControl.self) { spies in
18+
let spy = spies[0]
19+
20+
TabView {
21+
Text("Page 1").frame(maxWidth: .infinity, maxHeight: .infinity).background(Color.red)
22+
Text("Page 2").frame(maxWidth: .infinity, maxHeight: .infinity).background(Color.blue)
23+
}
24+
.tabViewStyle(.page(indexDisplayMode: .always))
25+
#if os(iOS) || os(tvOS)
26+
.introspect(.pageControl, on: .iOS(.v14, .v15, .v16, .v17), .tvOS(.v14, .v15, .v16, .v17), customize: spy)
27+
#endif
28+
}
29+
}
30+
}
31+
#endif

Tests/Tests/ViewTypes/TabViewWithPageStyleTests.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ final class TabViewWithPageStyleTests: XCTestCase {
1818
let spy = spies[0]
1919

2020
TabView {
21-
ZStack {
22-
Color.red
23-
Text("Something")
24-
}
21+
Text("Page 1").frame(maxWidth: .infinity, maxHeight: .infinity).background(Color.red)
22+
Text("Page 2").frame(maxWidth: .infinity, maxHeight: .infinity).background(Color.blue)
2523
}
2624
.tabViewStyle(.page)
2725
#if os(iOS) || os(tvOS)
@@ -39,11 +37,11 @@ final class TabViewWithPageStyleTests: XCTestCase {
3937
let spy = spies[0]
4038

4139
TabView {
42-
ZStack { Color.red; Text("1") }
40+
Text("Page 1").frame(maxWidth: .infinity, maxHeight: .infinity).background(Color.red)
4341
#if os(iOS) || os(tvOS)
4442
.introspect(.tabView(style: .page), on: .iOS(.v14, .v15, .v16, .v17), .tvOS(.v14, .v15, .v16, .v17), scope: .ancestor, customize: spy)
4543
#endif
46-
ZStack { Color.green; Text("2") }
44+
Text("Page 2").frame(maxWidth: .infinity, maxHeight: .infinity).background(Color.blue)
4745
}
4846
.tabViewStyle(.page)
4947
}

0 commit comments

Comments
 (0)