This commit is contained in:
David Roman 2023-06-03 16:36:19 +01:00
parent 4af686c887
commit cfb5414557
No known key found for this signature in database
GPG Key ID: 7058646EEFCB70A7
3 changed files with 55 additions and 21 deletions

View File

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

View File

@ -9,8 +9,7 @@ extension IntrospectableViewType where Self == SearchFieldType {
}
#if canImport(UIKit)
@available(tvOS, unavailable)
extension iOSViewVersion<SearchFieldType, UISearchController> {
extension iOSViewVersion<SearchFieldType, UISearchBar> {
@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")
@ -18,9 +17,9 @@ extension iOSViewVersion<SearchFieldType, UISearchController> {
public static let v15 = Self(for: .v15, selector: selector)
public static let v16 = Self(for: .v16, selector: selector)
private static var selector: IntrospectionSelector<UISearchController> {
private static var selector: IntrospectionSelector<UISearchBar> {
.from(UINavigationController.self) {
$0.navigationBar.topItem?.searchController
$0.view.allDescendants.lazy.compactMap { $0 as? UISearchBar }.first
}
}
}

View File

@ -3,16 +3,15 @@ import SwiftUI
import SwiftUIIntrospect
import XCTest
@available(iOS 15, tvOS 15, *)
// FIXME: crashes on tvOS 15, tests only... perhaps has to do with TestUtils?
@available(iOS 15, tvOS 16, *)
final class SearchFieldTests: XCTestCase {
#if canImport(UIKit) && os(iOS)
typealias PlatformSearchField = UISearchController
#elseif canImport(UIKit) && os(tvOS)
#if canImport(UIKit)
typealias PlatformSearchField = UISearchBar
#endif
func testSearchField() throws {
guard #available(iOS 15, tvOS 15, *) else {
func testSearchFieldInNavigationStack() throws {
guard #available(iOS 15, tvOS 16, *) else {
throw XCTSkip()
}
@ -32,8 +31,8 @@ final class SearchFieldTests: XCTestCase {
}
}
func testSearchFieldAsAncestor() throws {
guard #available(iOS 15, tvOS 15, *) else {
func testSearchFieldInNavigationStackAsAncestor() throws {
guard #available(iOS 15, tvOS 16, *) else {
throw XCTSkip()
}
@ -52,5 +51,47 @@ final class SearchFieldTests: XCTestCase {
.navigationViewStyle(.stack)
}
}
func testSearchFieldInNavigationSplitView() throws {
guard #available(iOS 15, tvOS 16, *) else {
throw XCTSkip()
}
XCTAssertViewIntrospection(of: PlatformSearchField.self) { spies in
let spy = spies[0]
NavigationView {
Text("Customized")
.searchable(text: .constant(""))
}
.navigationViewStyle(DoubleColumnNavigationViewStyle())
#if os(iOS)
.introspect(.searchField, on: .iOS(.v15, .v16), customize: spy)
#elseif os(tvOS)
.introspect(.searchField, on: .tvOS(.v15, .v16), customize: spy)
#endif
}
}
func testSearchFieldInNavigationSplitViewAsAncestor() 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(DoubleColumnNavigationViewStyle())
}
}
}
#endif