zashi-ios-wallet-private/secantTests/SnapshotTests/View+UIImage.swift

60 lines
1.8 KiB
Swift
Raw Normal View History

[337] Set up Snapshot Testing (#350) - snapshot package added - welcome snapshot taken [337] Set up Snapshot Testing - iPhone 8 Plus welcome screen references [337] Set up Snapshot Testing - snapshot references removed - snapshots only recorded, no comparison [337] Set up Snapshot Testing - isRecording removed [337] Set up Snapshot Testing - iPhone 8 Plus, iOS 15.4 screenshots [337] Set up Snapshot Testing - lowering the precision to 0.999 [337] Set up Snapshot Testing - attempts to get it pass the test [337] Set up Snapshot Testing - frame set up [337] Set up Snapshot Testing - resized image [337] Set up Snapshot Testing - many different snapshot tests [337] Set up Snapshot Testing - low precision test [337] Set up Snapshot Testing - updated pngs [337] Set up Snapshot Testing - another set of pngs [337] Set up Snapshot Testing - precision 0.99 [337] Set up Snapshot Testing - XCTAttachments [337] Set up Snapshot Testing - typo fixed [337] Set up Snapshot Testing - reference images [337] Set up Snapshot Testing - no precision set [337] Set up Snapshot Testing - screenshot names added - precision so low it passes all the time [337] Set up Snapshot Testing (350) - cleanup [337] Set up Snapshot Testing (350) - removed snapshot library completely [337] Set up Snapshot Testing (350) - code simplification and cleanup [337] Set up Snapshot Testing (350) - adding attachments refactored to simplify the code even more [337] Set up Snapshot Testing (350) - last simplification to wrap both light and dark under the hood [337] Set up Snapshot Testing (350) - addAttachments method to follow the same XCTestCase terminology [337] Set up Snapshot Testing (350) - attachments name fix
2022-06-10 06:46:35 -07:00
//
// View+UIImage.swift
// secantTests
//
// Created by Lukáš Korba on 10.06.2022.
//
import XCTest
import SwiftUI
extension XCTestCase {
func addAttachments<Content: View>(name: String = #function, _ view: Content) {
view.attachments(name).forEach { add($0) }
}
}
extension View {
func attachments(_ name: String) -> [XCTAttachment] {
2023-03-06 06:14:18 -08:00
[attachment(name)]
[337] Set up Snapshot Testing (#350) - snapshot package added - welcome snapshot taken [337] Set up Snapshot Testing - iPhone 8 Plus welcome screen references [337] Set up Snapshot Testing - snapshot references removed - snapshots only recorded, no comparison [337] Set up Snapshot Testing - isRecording removed [337] Set up Snapshot Testing - iPhone 8 Plus, iOS 15.4 screenshots [337] Set up Snapshot Testing - lowering the precision to 0.999 [337] Set up Snapshot Testing - attempts to get it pass the test [337] Set up Snapshot Testing - frame set up [337] Set up Snapshot Testing - resized image [337] Set up Snapshot Testing - many different snapshot tests [337] Set up Snapshot Testing - low precision test [337] Set up Snapshot Testing - updated pngs [337] Set up Snapshot Testing - another set of pngs [337] Set up Snapshot Testing - precision 0.99 [337] Set up Snapshot Testing - XCTAttachments [337] Set up Snapshot Testing - typo fixed [337] Set up Snapshot Testing - reference images [337] Set up Snapshot Testing - no precision set [337] Set up Snapshot Testing - screenshot names added - precision so low it passes all the time [337] Set up Snapshot Testing (350) - cleanup [337] Set up Snapshot Testing (350) - removed snapshot library completely [337] Set up Snapshot Testing (350) - code simplification and cleanup [337] Set up Snapshot Testing (350) - adding attachments refactored to simplify the code even more [337] Set up Snapshot Testing (350) - last simplification to wrap both light and dark under the hood [337] Set up Snapshot Testing (350) - addAttachments method to follow the same XCTestCase terminology [337] Set up Snapshot Testing (350) - attachments name fix
2022-06-10 06:46:35 -07:00
}
func attachment(_ fileName: String, _ colorScheme: ColorScheme = .light) -> XCTAttachment {
let colorSchemePostfix = colorScheme == .light ? "\(fileName)_light" : "\(fileName)_dark"
let trimTest = colorSchemePostfix.replacingOccurrences(of: "test", with: "")
let imageName = trimTest.replacingOccurrences(of: "()", with: "")
let rect = UIScreen.main.bounds
let viewHelper = self
.environment(\.colorScheme, colorScheme)
.frame(width: rect.width, height: rect.height, alignment: .center)
guard let image = viewHelper.asUiImage() else {
return XCTAttachment(string: "\(imageName)_SnapshotFailed")
}
let attachment = XCTAttachment(image: image)
attachment.name = imageName
attachment.lifetime = .keepAlways
return attachment
}
func asUiImage() -> UIImage? {
let controller = UIHostingController(rootView: self)
if let view = controller.view {
let contentSize = view.intrinsicContentSize
view.bounds = CGRect(origin: .zero, size: contentSize)
view.backgroundColor = .clear
let renderer = UIGraphicsImageRenderer(size: contentSize)
let uiImage = renderer.image { _ in
view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
}
return uiImage
}
return nil
}
}