|
| 1 | +import SwiftUI |
| 2 | +import SwiftUIIntrospect |
| 3 | +import XCTest |
| 4 | + |
| 5 | +final class SecureFieldTests: XCTestCase { |
| 6 | + #if canImport(UIKit) |
| 7 | + typealias PlatformSecureField = UITextField |
| 8 | + #elseif canImport(AppKit) |
| 9 | + typealias PlatformSecureField = NSTextField |
| 10 | + #endif |
| 11 | + |
| 12 | + func testSecureField() { |
| 13 | + XCTAssertViewIntrospection(of: PlatformSecureField.self) { spies in |
| 14 | + let spy0 = spies[0] |
| 15 | + let spy1 = spies[1] |
| 16 | + let spy2 = spies[2] |
| 17 | + |
| 18 | + VStack { |
| 19 | + SecureField("", text: .constant("Secure Field 0")) |
| 20 | + #if os(iOS) || os(tvOS) |
| 21 | + .introspect(.secureField, on: .iOS(.v13, .v14, .v15, .v16, .v17), .tvOS(.v13, .v14, .v15, .v16, .v17), customize: spy0) |
| 22 | + #elseif os(macOS) |
| 23 | + .introspect(.secureField, on: .macOS(.v10_15, .v11, .v12, .v13, .v14), customize: spy0) |
| 24 | + #endif |
| 25 | + .cornerRadius(8) |
| 26 | + |
| 27 | + SecureField("", text: .constant("Secure Field 1")) |
| 28 | + #if os(iOS) || os(tvOS) |
| 29 | + .introspect(.secureField, on: .iOS(.v13, .v14, .v15, .v16, .v17), .tvOS(.v13, .v14, .v15, .v16, .v17), customize: spy1) |
| 30 | + #elseif os(macOS) |
| 31 | + .introspect(.secureField, on: .macOS(.v10_15, .v11, .v12, .v13, .v14), customize: spy1) |
| 32 | + #endif |
| 33 | + .cornerRadius(8) |
| 34 | + |
| 35 | + SecureField("", text: .constant("Secure Field 2")) |
| 36 | + #if os(iOS) || os(tvOS) |
| 37 | + .introspect(.secureField, on: .iOS(.v13, .v14, .v15, .v16, .v17), .tvOS(.v13, .v14, .v15, .v16, .v17), customize: spy2) |
| 38 | + #elseif os(macOS) |
| 39 | + .introspect(.secureField, on: .macOS(.v10_15, .v11, .v12, .v13, .v14), customize: spy2) |
| 40 | + #endif |
| 41 | + } |
| 42 | + } extraAssertions: { |
| 43 | + #if canImport(UIKit) |
| 44 | + XCTAssertEqual($0[safe: 0]?.text, "Secure Field 0") |
| 45 | + XCTAssertEqual($0[safe: 1]?.text, "Secure Field 1") |
| 46 | + XCTAssertEqual($0[safe: 2]?.text, "Secure Field 2") |
| 47 | + #elseif canImport(AppKit) |
| 48 | + XCTAssertEqual($0[safe: 0]?.stringValue, "Secure Field 0") |
| 49 | + XCTAssertEqual($0[safe: 1]?.stringValue, "Secure Field 1") |
| 50 | + XCTAssertEqual($0[safe: 2]?.stringValue, "Secure Field 2") |
| 51 | + #endif |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + func testSecureFieldsEmbeddedInList() { |
| 56 | + XCTAssertViewIntrospection(of: PlatformSecureField.self) { spies in |
| 57 | + let spy0 = spies[0] |
| 58 | + let spy1 = spies[1] |
| 59 | + let spy2 = spies[2] |
| 60 | + |
| 61 | + List { |
| 62 | + SecureField("", text: .constant("Secure Field 0")) |
| 63 | + #if os(iOS) || os(tvOS) |
| 64 | + .introspect(.secureField, on: .iOS(.v13, .v14, .v15, .v16, .v17), .tvOS(.v13, .v14, .v15, .v16, .v17), customize: spy0) |
| 65 | + #elseif os(macOS) |
| 66 | + .introspect(.secureField, on: .macOS(.v10_15, .v11, .v12, .v13, .v14), customize: spy0) |
| 67 | + #endif |
| 68 | + |
| 69 | + SecureField("", text: .constant("Secure Field 1")) |
| 70 | + #if os(iOS) || os(tvOS) |
| 71 | + .introspect(.secureField, on: .iOS(.v13, .v14, .v15, .v16, .v17), .tvOS(.v13, .v14, .v15, .v16, .v17), customize: spy1) |
| 72 | + #elseif os(macOS) |
| 73 | + .introspect(.secureField, on: .macOS(.v10_15, .v11, .v12, .v13, .v14), customize: spy1) |
| 74 | + #endif |
| 75 | + |
| 76 | + SecureField("", text: .constant("Secure Field 2")) |
| 77 | + #if os(iOS) || os(tvOS) |
| 78 | + .introspect(.secureField, on: .iOS(.v13, .v14, .v15, .v16, .v17), .tvOS(.v13, .v14, .v15, .v16, .v17), customize: spy2) |
| 79 | + #elseif os(macOS) |
| 80 | + .introspect(.secureField, on: .macOS(.v10_15, .v11, .v12, .v13, .v14), customize: spy2) |
| 81 | + #endif |
| 82 | + } |
| 83 | + } extraAssertions: { |
| 84 | + #if canImport(UIKit) |
| 85 | + XCTAssertEqual($0[safe: 0]?.text, "Secure Field 0") |
| 86 | + XCTAssertEqual($0[safe: 1]?.text, "Secure Field 1") |
| 87 | + XCTAssertEqual($0[safe: 2]?.text, "Secure Field 2") |
| 88 | + #elseif canImport(AppKit) |
| 89 | + XCTAssertEqual($0[safe: 0]?.stringValue, "Secure Field 0") |
| 90 | + XCTAssertEqual($0[safe: 1]?.stringValue, "Secure Field 1") |
| 91 | + XCTAssertEqual($0[safe: 2]?.stringValue, "Secure Field 2") |
| 92 | + #endif |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments