@@ -13,6 +13,19 @@ import SwiftUI
13
13
import XCTest
14
14
import AccessibilitySnapshot
15
15
16
+ extension SnapshotTestBase {
17
+ public typealias DeviceConfiguration = ( name: String , device: ViewImageConfig )
18
+ }
19
+
20
+ extension SnapshotTestBase {
21
+ /// Configuration of Accessibility snapshots.
22
+ public enum A11ySnapshotConfiguration {
23
+ case disabled
24
+ case enabled
25
+ case enabledWithDevices( [ DeviceConfiguration ] )
26
+ }
27
+ }
28
+
16
29
open class SnapshotTestBase : XCTestCase {
17
30
public var allowAnimations : Bool = false
18
31
@@ -21,7 +34,7 @@ open class SnapshotTestBase: XCTestCase {
21
34
UIView . setAnimationsEnabled ( allowAnimations)
22
35
}
23
36
24
- open var defaultDevices : [ ( name : String , device : ViewImageConfig ) ] {
37
+ open var defaultDevices : [ DeviceConfiguration ] {
25
38
[
26
39
( " iPhone8 " , . iPhone8) ,
27
40
( " iPhone13proMax " , . iPhone13ProMax) ,
@@ -30,16 +43,27 @@ open class SnapshotTestBase: XCTestCase {
30
43
]
31
44
}
32
45
33
- open var accessibilityDevices : [ ( name : String , device : ViewImageConfig ) ] {
46
+ open var a11yDefaultDevices : [ DeviceConfiguration ] {
34
47
[
35
48
( " iPhone13pro " , . iPhone13)
36
49
]
37
50
}
38
-
51
+
52
+ /// Asserts snapshots on the given devices (or default) respecting it's parameters.
53
+ /// - Parameters:
54
+ /// - view: The view to be snapshotted
55
+ /// - devices: Specified devices, if not given it'll take default devices
56
+ /// - a11ySnapshotConfiguration: Accessibility snapshot configuration, if enabled it adds a accessibility snapshot
57
+ /// - style: `UIUserInterfaceStyle` to be applied
58
+ /// - imageDiffPrecision: Precision of the compared images, 1 means it matches 100%, range is between 0 and 1.
59
+ /// - file: The file this was executed from, it will be taken as the snapshot's file name.
60
+ /// - testName: The test name taken as a part of the snapshot's file name.
61
+ /// - line: The line number on which failure occurred. Defaults to the line number on which this
62
+ /// function was called.
39
63
open func assertSnapshotDevices< V: View > (
40
64
_ view: V ,
41
- devices: [ ( name : String , device : ViewImageConfig ) ] ? = nil ,
42
- accessibilityDevices : [ ( name : String , device : ViewImageConfig ) ] ? = nil ,
65
+ devices: [ DeviceConfiguration ] ? = nil ,
66
+ a11ySnapshotConfiguration : A11ySnapshotConfiguration = . disabled ,
43
67
style: [ UIUserInterfaceStyle ] = [ . unspecified] ,
44
68
imageDiffPrecision: Float = 1.0 ,
45
69
file: StaticString = #file,
@@ -74,7 +98,19 @@ open class SnapshotTestBase: XCTestCase {
74
98
}
75
99
}
76
100
77
- ( accessibilityDevices ?? self . accessibilityDevices) . forEach { config in
101
+ let a11ySnapshotDevices : [ DeviceConfiguration ] ? = {
102
+ switch a11ySnapshotConfiguration {
103
+ case . disabled:
104
+ return nil
105
+ case . enabled:
106
+ return a11yDefaultDevices
107
+ case . enabledWithDevices( let specifiedDevices) :
108
+ return specifiedDevices
109
+ }
110
+ } ( )
111
+
112
+ guard let a11ySnapshotDevices else { return }
113
+ a11ySnapshotDevices. forEach { config in
78
114
let vc = UIHostingController ( rootView: view)
79
115
assertSnapshot (
80
116
of: vc,
0 commit comments