diff --git a/Examples/Showcase/Showcase/ContentView.swift b/Examples/Showcase/Showcase/ContentView.swift index 5527381..70032fc 100644 --- a/Examples/Showcase/Showcase/ContentView.swift +++ b/Examples/Showcase/Showcase/ContentView.swift @@ -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 } } diff --git a/Sources/ViewTypes/SearchField.swift b/Sources/ViewTypes/SearchField.swift index 37b509a..8e97cc2 100644 --- a/Sources/ViewTypes/SearchField.swift +++ b/Sources/ViewTypes/SearchField.swift @@ -9,8 +9,7 @@ extension IntrospectableViewType where Self == SearchFieldType { } #if canImport(UIKit) -@available(tvOS, unavailable) -extension iOSViewVersion { +extension iOSViewVersion { @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 { public static let v15 = Self(for: .v15, selector: selector) public static let v16 = Self(for: .v16, selector: selector) - private static var selector: IntrospectionSelector { + private static var selector: IntrospectionSelector { .from(UINavigationController.self) { - $0.navigationBar.topItem?.searchController + $0.view.allDescendants.lazy.compactMap { $0 as? UISearchBar }.first } } } diff --git a/Tests/Tests/ViewTypes/SearchFieldTests.swift b/Tests/Tests/ViewTypes/SearchFieldTests.swift index 9ff67c1..4d5c1f3 100644 --- a/Tests/Tests/ViewTypes/SearchFieldTests.swift +++ b/Tests/Tests/ViewTypes/SearchFieldTests.swift @@ -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