-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathSnapshotTests.swift
166 lines (135 loc) · 7.96 KB
/
SnapshotTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//
// SnapshotTests.swift
// LockdownTests
//
// Created by Oleg Dreyman on 27.05.2020.
// Copyright © 2020 Confirmed Inc. All rights reserved.
//
import XCTest
@testable import SnapshotTesting
@testable import Lockdown
// SIMULATOR: iPhone SE, 1st Generation
// OS: iOS 13.3
class SnapshotTests: XCTestCase {
func testEmailSignupVC() {
let emailSignUpVC = make(EmailSignUpViewController.self, storyboardIdentifier: "emailSignUpViewController")
lockdownSnapshotTest(emailSignUpVC)
}
func testEmailSignInVC() {
let emailSignInVC = make(EmailSignInViewController.self, storyboardIdentifier: "emailSignInViewController")
lockdownSnapshotTest(emailSignInVC)
}
func testWhatIsVpnVC() {
// We are not using `lockdownSnapshotTest` here because WhatIsVpnViewController
// has changing logic based on raw screen size, and by defaults that's not something
// that snapshot testing library is designed to test. So we're "emulating" that
// two different modes (iPhone SE and not iPhone SE) by toggling `is4InchIphone`
// on and off.
do {
let whatIsVpnVC = make(WhatIsVpnViewController.self, storyboardIdentifier: "whatIsVpnViewController")
whatIsVpnVC.is4InchIphone = true
assertSnapshot(matching: whatIsVpnVC, as: .image(on: .iPhoneSe, userInterfaceStyle: .light))
}
do {
let whatIsVpnVC = make(WhatIsVpnViewController.self, storyboardIdentifier: "whatIsVpnViewController")
whatIsVpnVC.is4InchIphone = false
assertSnapshot(matching: whatIsVpnVC, as: .image(on: .iPhone8, userInterfaceStyle: .light))
}
do {
let whatIsVpnVC = make(WhatIsVpnViewController.self, storyboardIdentifier: "whatIsVpnViewController")
whatIsVpnVC.is4InchIphone = false
assertSnapshot(matching: whatIsVpnVC, as: .image(on: .iPhoneXsMax, userInterfaceStyle: .light))
}
do {
let whatIsVpnVC = make(WhatIsVpnViewController.self, storyboardIdentifier: "whatIsVpnViewController")
whatIsVpnVC.is4InchIphone = true
assertSnapshot(matching: whatIsVpnVC, as: .image(on: .iPhoneSe, userInterfaceStyle: .dark))
}
do {
let whatIsVpnVC = make(WhatIsVpnViewController.self, storyboardIdentifier: "whatIsVpnViewController")
whatIsVpnVC.is4InchIphone = false
assertSnapshot(matching: whatIsVpnVC, as: .image(on: .iPhone8, userInterfaceStyle: .dark))
}
do {
let whatIsVpnVC = make(WhatIsVpnViewController.self, storyboardIdentifier: "whatIsVpnViewController")
whatIsVpnVC.is4InchIphone = false
assertSnapshot(matching: whatIsVpnVC, as: .image(on: .iPhoneXsMax, userInterfaceStyle: .dark))
}
}
func testHomeVC() {
let homeVC = make(HomeViewController.self, storyboardIdentifier: "homeViewController")
lockdownHighQualitySnapshotTest(homeVC)
}
func testFirewallPrivacyPolicyVC() {
let privacyPolicyVC = make(PrivacyPolicyViewController.self, storyboardIdentifier: "firewallPrivacyPolicyViewController")
privacyPolicyVC.parentVC = nil
privacyPolicyVC.privacyPolicyKey = kHasAgreedToFirewallPrivacyPolicy
lockdownSnapshotTest(privacyPolicyVC)
}
func testTitleVC() {
let titleVC = make(TitleViewController.self, storyboardIdentifier: "titleViewController")
titleVC.isAnimatingOnAppear = false
lockdownSnapshotTest(titleVC)
}
func testLogVC() {
BlockDayLog.shared.clear()
let date = Calendar.current.date(bySettingHour: 9, minute: 41, second: 10, of: Date())!
BlockDayLog.shared.append(host: "snapshot-test.com", date: date)
BlockDayLog.shared.append(host: "lockdown-test.com", date: date)
let logVC = make(BlockLogViewController.self, storyboardIdentifier: "blockLogViewController")
lockdownSnapshotTest(logVC)
BlockDayLog.shared.clear()
}
}
extension SnapshotTests {
private func make<ViewController: UIViewController>(_ vc: ViewController.Type, storyboardIdentifier: String) -> ViewController {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: storyboardIdentifier) as! ViewController
return viewController
}
private func lockdownHighQualitySnapshotTest(
_ viewController: UIViewController,
record: Bool = false,
file: StaticString = #file,
testName: String = #function,
line: UInt = #line
) {
guard UIDevice.is4InchIphone else {
XCTFail("These snapshot tests are designed to run on iPhone SE 1st Gen, iOS 13.3 simulator", file: file, line: line)
return
}
assertSnapshot(matching: viewController, as: .keyWindowImage(on: .iPhoneSe, userInterfaceStyle: .light), record: record, file: file, testName: testName, line: line)
assertSnapshot(matching: viewController, as: .keyWindowImage(on: .iPhone8, userInterfaceStyle: .light), record: record, file: file, testName: testName, line: line)
assertSnapshot(matching: viewController, as: .keyWindowImage(on: .iPhoneXsMax, userInterfaceStyle: .light), record: record, file: file, testName: testName, line: line)
assertSnapshot(matching: viewController, as: .keyWindowImage(on: .iPhoneSe, userInterfaceStyle: .dark), record: record, file: file, testName: testName, line: line)
assertSnapshot(matching: viewController, as: .keyWindowImage(on: .iPhone8, userInterfaceStyle: .dark), record: record, file: file, testName: testName, line: line)
assertSnapshot(matching: viewController, as: .keyWindowImage(on: .iPhoneXsMax, userInterfaceStyle: .dark), record: record, file: file, testName: testName, line: line)
}
private func lockdownSnapshotTest(
_ viewController: UIViewController,
record: Bool = false,
file: StaticString = #file,
testName: String = #function,
line: UInt = #line
) {
guard UIDevice.is4InchIphone else {
XCTFail("These snapshot tests are designed to run on iPhone SE 1st Gen, iOS 13.3 simulator", file: file, line: line)
return
}
assertSnapshot(matching: viewController, as: .image(on: .iPhoneSe, userInterfaceStyle: .light), record: record, file: file, testName: testName, line: line)
assertSnapshot(matching: viewController, as: .image(on: .iPhone8, userInterfaceStyle: .light), record: record, file: file, testName: testName, line: line)
assertSnapshot(matching: viewController, as: .image(on: .iPhoneXsMax, userInterfaceStyle: .light), record: record, file: file, testName: testName, line: line)
assertSnapshot(matching: viewController, as: .image(on: .iPhoneSe, userInterfaceStyle: .dark), record: record, file: file, testName: testName, line: line)
assertSnapshot(matching: viewController, as: .image(on: .iPhone8, userInterfaceStyle: .dark), record: record, file: file, testName: testName, line: line)
assertSnapshot(matching: viewController, as: .image(on: .iPhoneXsMax, userInterfaceStyle: .dark), record: record, file: file, testName: testName, line: line)
}
}
extension Snapshotting where Value == UIViewController, Format == UIImage {
static func image(on device: ViewImageConfig, userInterfaceStyle: UIUserInterfaceStyle) -> Snapshotting {
return image(on: device, traits: .init(userInterfaceStyle: userInterfaceStyle))
}
static func keyWindowImage(on device: ViewImageConfig, userInterfaceStyle: UIUserInterfaceStyle) -> Snapshotting {
let traits = UITraitCollection(traitsFrom: [device.traits, .init(userInterfaceStyle: userInterfaceStyle)])
return image(drawHierarchyInKeyWindow: true, precision: 0.995, size: device.size, traits: traits)
}
}