Skip to content

Commit 0496e9b

Browse files
committed
Updated tests for SnapshotTestingTests
1 parent ebd1dbc commit 0496e9b

30 files changed

+272
-1049
lines changed
Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
import SnapshotTesting
2+
@testable import XCTSnapshot
3+
#if canImport(Testing)
4+
import Testing
5+
6+
#if os(iOS) || os(tvOS) || os(visionOS)
7+
import UIKit
8+
9+
typealias SDKView = UIView
10+
typealias SDKLabel = UILabel
11+
typealias SDKScrollView = UIScrollView
12+
#elseif os(macOS)
13+
import AppKit
14+
15+
typealias SDKView = NSView
16+
typealias SDKLabel = NSText
17+
typealias SDKScrollView = NSScrollView
18+
#endif
19+
20+
#if os(iOS) || os(tvOS) || os(visionOS) || os(macOS)
21+
@MainActor
22+
struct UITests {
23+
24+
// MARK: - Base Configuration
25+
private func setupViewWithSafeArea() -> SDKView {
26+
let view = SDKView()
27+
let contentView = SDKView()
28+
contentView.translatesAutoresizingMaskIntoConstraints = false
29+
view.addSubview(contentView)
30+
31+
NSLayoutConstraint.activate([
32+
contentView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
33+
contentView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
34+
contentView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
35+
contentView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor)
36+
])
37+
38+
return view
39+
}
40+
41+
#if !os(macOS)
42+
@Test
43+
func basicElements() async throws {
44+
try await withThrowingTaskGroup(of: Void.self) { group in
45+
group.addTask {
46+
let label: SDKLabel = await MainActor.run {
47+
let label = SDKLabel()
48+
#if os(macOS)
49+
label.string = "Hello World"
50+
#else
51+
label.text = "Hello World"
52+
#endif
53+
label.backgroundColor = .white
54+
label.textColor = .black
55+
return label
56+
}
57+
58+
try await assert(of: label, as: .image)
59+
}
60+
61+
group.addTask {
62+
let scrollView: SDKScrollView = await MainActor.run {
63+
let scrollView = SDKScrollView()
64+
let contentView = SDKView()
65+
let rectangleView = SDKView()
66+
67+
rectangleView.backgroundColor = .red
68+
69+
contentView.translatesAutoresizingMaskIntoConstraints = false
70+
rectangleView.translatesAutoresizingMaskIntoConstraints = false
71+
72+
scrollView.addSubview(contentView)
73+
contentView.addSubview(rectangleView)
74+
75+
NSLayoutConstraint.activate([
76+
contentView.topAnchor.constraint(equalTo: scrollView.contentLayoutGuide.topAnchor),
77+
contentView.bottomAnchor.constraint(equalTo: scrollView.contentLayoutGuide.bottomAnchor),
78+
contentView.leadingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.leadingAnchor),
79+
contentView.trailingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.trailingAnchor),
80+
contentView.widthAnchor.constraint(equalTo: scrollView.frameLayoutGuide.widthAnchor)
81+
])
82+
83+
NSLayoutConstraint.activate([
84+
rectangleView.topAnchor.constraint(equalTo: contentView.topAnchor),
85+
rectangleView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
86+
rectangleView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
87+
rectangleView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
88+
rectangleView.heightAnchor.constraint(equalToConstant: 200)
89+
])
90+
91+
return scrollView
92+
}
93+
94+
try await assert(of: scrollView, as: .image(layout: .fixed(width: 400, height: 400)))
95+
}
96+
97+
for try await _ in group {}
98+
}
99+
}
100+
#endif
101+
102+
@Test
103+
func dynamicUpdates() async throws {
104+
let view = SDKView()
105+
106+
let constraint = view.widthAnchor.constraint(equalToConstant: 600)
107+
108+
NSLayoutConstraint.activate([
109+
view.heightAnchor.constraint(equalToConstant: 300),
110+
constraint
111+
])
112+
113+
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
114+
constraint.constant *= 1.5
115+
}
116+
117+
DispatchQueue.main.asyncAfter(deadline: .now() + 4) {
118+
constraint.constant /= 2
119+
}
120+
121+
try await assert(of: view, as: .image(delay: 6))
122+
}
123+
124+
#if !os(macOS)
125+
@Test
126+
func uiTraits() async throws {
127+
try await withTestingEnvironment {
128+
$0.disableInconsistentTraitsChecker = true
129+
} operation: {
130+
let view = SDKView()
131+
#if !os(tvOS)
132+
view.backgroundColor = .systemBackground
133+
#else
134+
view.backgroundColor = .systemGray
135+
#endif
136+
137+
let testCases = [
138+
(.light, "lightMode"),
139+
(.dark, "darkMode")
140+
]
141+
142+
for (style, name) in testCases {
143+
try await assert(
144+
of: view,
145+
as: .image(traits: .init(userInterfaceStyle: style)),
146+
named: name
147+
)
148+
}
149+
}
150+
}
151+
152+
@Test
153+
func accessibilityAndContentSize() async throws {
154+
let label = SDKLabel()
155+
label.text = "Hello World"
156+
label.font = UIFont.preferredFont(forTextStyle: .body)
157+
label.adjustsFontForContentSizeCategory = true
158+
159+
let categories: [UITraitCollection.ContentSizeCategory] = [
160+
.extraSmall,
161+
.large,
162+
.accessibilityExtraExtraExtraLarge
163+
]
164+
165+
for category in categories {
166+
try await assert(
167+
of: label,
168+
as: .image(traits: .init(preferredContentSizeCategory: category)),
169+
named: category.rawValue
170+
)
171+
}
172+
}
173+
174+
@Test
175+
func deviceSpecificLayouts() async throws {
176+
let view = setupViewWithSafeArea()
177+
view.backgroundColor = .red
178+
179+
try await assert(
180+
of: view,
181+
as: .image(layout: .device(.iPhone16Pro)),
182+
named: "iPhone16Pro"
183+
)
184+
}
185+
#endif
186+
187+
@Test
188+
func complexLayouts() async throws {
189+
let bigView = SDKView()
190+
#if os(macOS)
191+
bigView.wantsLayer = true
192+
bigView.layer?.backgroundColor = NSColor.cyan.cgColor
193+
#else
194+
bigView.backgroundColor = .cyan
195+
#endif
196+
NSLayoutConstraint.activate([
197+
bigView.heightAnchor.constraint(equalToConstant: 3000),
198+
bigView.widthAnchor.constraint(equalToConstant: 2000)
199+
])
200+
201+
try await assert(of: bigView, as: .image)
202+
}
203+
204+
#if !os(macOS)
205+
@Test
206+
func concurrentUpdates() async throws {
207+
let view = setupViewWithSafeArea()
208+
209+
try await withThrowingTaskGroup(of: Void.self) { group in
210+
group.addTask {
211+
try await assert(
212+
of: view,
213+
as: .image(layout: .device(.iPhone16Pro), delay: 1),
214+
named: "1"
215+
)
216+
}
217+
group.addTask {
218+
try await assert(
219+
of: view,
220+
as: .image(layout: .device(.iPhone16Pro), delay: 2),
221+
named: "2"
222+
)
223+
}
224+
225+
for try await _ in group {}
226+
}
227+
}
228+
#endif
229+
}
230+
#endif
231+
#endif

0 commit comments

Comments
 (0)