This commit is contained in:
David Roman 2023-06-03 14:53:45 +01:00
parent f327069e9c
commit 5c7fbfa262
No known key found for this signature in database
GPG Key ID: 7058646EEFCB70A7
3 changed files with 55 additions and 11 deletions

View File

@ -186,12 +186,18 @@ struct NavigationShowcase: View {
.introspect(.navigationView(style: .columns), on: .tvOS(.v13, .v14, .v15, .v16)) { navigationController in
navigationController.navigationBar.backgroundColor = .cyan
}
.introspect(.searchField, on: .iOS(.v15, .v16), .tvOS(.v15, .v16)) { searchField in
searchField.backgroundColor = .red
#endif
#if os(iOS)
.introspect(.searchField, on: .iOS(.v15, .v16)) { searchController in
searchController.searchBar.backgroundColor = .red
#if os(iOS)
searchField.searchTextField.backgroundColor = .purple
searchController.searchBar.searchTextField.backgroundColor = .purple
#endif
}
#elseif os(tvOS)
.introspect(.searchField, on: .tvOS(.v15, .v16)) { searchField in
searchField.backgroundColor = .red
}
#endif
}
}

View File

@ -9,13 +9,20 @@ extension IntrospectableViewType where Self == SearchFieldType {
}
#if canImport(UIKit)
extension iOSViewVersion<SearchFieldType, UISearchBar> {
@available(tvOS, unavailable)
extension iOSViewVersion<SearchFieldType, UISearchController> {
@available(*, unavailable, message: ".searchable isn't available on iOS 13")
public static let v13 = Self.unavailable()
@available(*, unavailable, message: ".searchable isn't available on iOS 14")
public static let v14 = Self.unavailable()
public static let v15 = Self(for: .v15)
public static let v16 = Self(for: .v16)
public static let v15 = Self(for: .v15, selector: selector)
public static let v16 = Self(for: .v16, selector: selector)
private static var selector: IntrospectionSelector<UISearchController> {
.from(UINavigationController.self) {
$0.navigationBar.topItem?.searchController
}
}
}
extension tvOSViewVersion<SearchFieldType, UISearchBar> {
@ -23,7 +30,13 @@ extension tvOSViewVersion<SearchFieldType, UISearchBar> {
public static let v13 = Self.unavailable()
@available(*, unavailable, message: ".searchable isn't available on tvOS 14")
public static let v14 = Self.unavailable()
public static let v15 = Self(for: .v15)
public static let v16 = Self(for: .v16)
public static let v15 = Self(for: .v15, selector: selector)
public static let v16 = Self(for: .v16, selector: selector)
private static var selector: IntrospectionSelector<UISearchBar> {
.from(UINavigationController.self) {
$0.view.allDescendants.compactMap { $0 as? UISearchBar }.first
}
}
}
#endif

View File

@ -5,7 +5,9 @@ import XCTest
@available(iOS 15, tvOS 15, *)
final class SearchFieldTests: XCTestCase {
#if canImport(UIKit)
#if canImport(UIKit) && os(iOS)
typealias PlatformSearchField = UISearchController
#elseif canImport(UIKit) && os(tvOS)
typealias PlatformSearchField = UISearchBar
#endif
@ -22,10 +24,33 @@ final class SearchFieldTests: XCTestCase {
.searchable(text: .constant(""))
}
.navigationViewStyle(.stack)
#if os(iOS) || os(tvOS)
.introspect(.searchField, on: .iOS(.v15, .v16), .tvOS(.v15, .v16), customize: spy)
#if os(iOS)
.introspect(.searchField, on: .iOS(.v15, .v16), customize: spy)
#elseif os(tvOS)
.introspect(.searchField, on: .tvOS(.v15, .v16), customize: spy)
#endif
}
}
func testSearchFieldAsAncestor() throws {
guard #available(iOS 15, tvOS 15, *) else {
throw XCTSkip()
}
XCTAssertViewIntrospection(of: PlatformSearchField.self) { spies in
let spy = spies[0]
NavigationView {
Text("Customized")
.searchable(text: .constant(""))
#if os(iOS)
.introspect(.searchField, on: .iOS(.v15, .v16), scope: .ancestor, customize: spy)
#elseif os(tvOS)
.introspect(.searchField, on: .tvOS(.v15, .v16), scope: .ancestor, customize: spy)
#endif
}
.navigationViewStyle(.stack)
}
}
}
#endif