From 0338f2ca0504de01c7acfa4d33dea54360ccaa8e Mon Sep 17 00:00:00 2001 From: Alex Odawa Date: Thu, 6 Feb 2025 15:43:38 +0100 Subject: [PATCH] adding tests --- .../Tests/AccessibililtyTraitsTests.swift | 35 +++++++++++++++++++ .../Tests/Extensions/UIView+Testing.swift | 32 +++++++++++++++++ .../{ => Extensions}/XCTestCase+AppHost.swift | 0 3 files changed, 67 insertions(+) create mode 100644 BlueprintUI/Tests/AccessibililtyTraitsTests.swift create mode 100644 BlueprintUI/Tests/Extensions/UIView+Testing.swift rename BlueprintUI/Tests/{ => Extensions}/XCTestCase+AppHost.swift (100%) diff --git a/BlueprintUI/Tests/AccessibililtyTraitsTests.swift b/BlueprintUI/Tests/AccessibililtyTraitsTests.swift new file mode 100644 index 000000000..1d1c93b19 --- /dev/null +++ b/BlueprintUI/Tests/AccessibililtyTraitsTests.swift @@ -0,0 +1,35 @@ +import BlueprintUI +import UIKit +import XCTest + + +class UIAccessibilityTraits_Tests: XCTestCase { + + func test_backButton() throws { + + let root = UIViewController() + let second = UIViewController() + let navigationController = UINavigationController() + navigationController.viewControllers = [root, second] + + try show(vc: navigationController) { _ in + let button = try XCTUnwrap( + navigationController.navigationBar.findSubview(ofType: UIControl.self) + ) + + /// The `.backButton` trait is private, so this test is here to verify it does not change. + + XCTAssertEqual(button.accessibilityTraits, .backButton) + } + } + + func test_toggleSwitch() { + if #available(iOS 17.0, *) { + /// The `.toggleButton` trait is exposed iOS 17, so this test is here to verify it matches ours. + XCTAssertEqual(UIAccessibilityTraits._toggleButton, UIAccessibilityTraits.toggleButton) + } + let uiSwitch = UISwitch() + /// The `.toggleButton` trait is private prior to iOS 17, so this test is here to verify it exists on prior versions. + XCTAssertTrue(uiSwitch.accessibilityTraits.contains(._toggleButton)) + } +} diff --git a/BlueprintUI/Tests/Extensions/UIView+Testing.swift b/BlueprintUI/Tests/Extensions/UIView+Testing.swift new file mode 100644 index 000000000..75eee7ce1 --- /dev/null +++ b/BlueprintUI/Tests/Extensions/UIView+Testing.swift @@ -0,0 +1,32 @@ +import UIKit +import XCTest + +extension UIView { + /// Does a breadth-first search for a subview of the given type, optionally matching a + /// predicate. + public func findSubview( + ofType viewType: View.Type, + where predicate: (View) -> Bool = { _ in true } + ) -> View? { + for subview in subviews { + if let match = subview as? View, predicate(match) { + return match + } + } + + for view in subviews { + if let match = view.findSubview(ofType: viewType, where: predicate) { + return match + } + } + + return nil + } + + public func expectedChild(ofType viewType: T.Type) throws -> T { + try XCTUnwrap( + findSubview(ofType: viewType), + "Expected to find child of type \(T.self) but no child was found" + ) + } +} diff --git a/BlueprintUI/Tests/XCTestCase+AppHost.swift b/BlueprintUI/Tests/Extensions/XCTestCase+AppHost.swift similarity index 100% rename from BlueprintUI/Tests/XCTestCase+AppHost.swift rename to BlueprintUI/Tests/Extensions/XCTestCase+AppHost.swift