Last active
August 8, 2022 23:33
-
-
Save gohanlon/d40e8205117d68e66732b8e6e85bd6cb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if canImport(UIKit) | |
import SnapshotTesting | |
import SwiftUI | |
public func assertCurrentDeviceSnapshot<SnapshotContent: View>( | |
for view: SnapshotContent, | |
precision: Float = 1, | |
record: Bool = false, | |
file: StaticString = #file, | |
testName: String = #function, | |
line: UInt = #line | |
) { | |
var transaction = Transaction(animation: nil) | |
transaction.disablesAnimations = true | |
withTransaction(transaction) { | |
let safeArea: UIEdgeInsets | |
switch UIDevice.current.name { | |
case "iPhone 8": | |
safeArea = .init(top: 20, left: 0, bottom: 0, right: 0) | |
case "iPhone 13 mini": | |
safeArea = .init(top: 50, left: 0, bottom: 34, right: 0) | |
case "iPhone 13", "iPhone 13 Pro", "iPhone 13 Pro Max": | |
safeArea = .init(top: 47, left: 0, bottom: 34, right: 0) | |
case "iPad mini (6th generation)": | |
safeArea = .init(top: 24, left: 0, bottom: 20, right: 0) | |
case "iPad Pro (12.9-inch) (5th generation)": | |
safeArea = .init(top: 24, left: 0, bottom: 20, right: 0) | |
case "iPhone SE (3rd generation)": | |
safeArea = .init(top: 20, left: 0, bottom: 0, right: 0) | |
case "iPod touch (7th generation)": | |
safeArea = .init(top: 20, left: 0, bottom: 0, right: 0) | |
default: | |
fatalError("Don't know safe area for device: \(UIDevice.current.name)") | |
} | |
assertSnapshot( | |
matching: view, | |
as: .image( | |
precision: precision, | |
layout: .device(config: .init(safeArea: safeArea, size: UIScreen.main.bounds.size)) | |
// traits: UITraitCollection.init(userInterfaceStyle: .light) | |
), | |
named: "\(UIDevice.current.name).\(UIDevice.current.systemVersion)", | |
record: record, | |
file: file, | |
testName: testName, | |
line: line | |
) | |
} | |
} | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Copyright (c) 2022 Galen O'Hanlon | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
time xcodebuild test -scheme "SnapshotTests" \ | |
-destination "platform=iOS Simulator,OS=15.4,name=iPhone 8" \ | |
-destination "platform=iOS Simulator,OS=15.4,name=iPhone 13 mini" \ | |
-destination "platform=iOS Simulator,OS=15.4,name=iPhone 13" \ | |
-destination "platform=iOS Simulator,OS=15.4,name=iPhone 13 Pro" \ | |
-destination "platform=iOS Simulator,OS=15.4,name=iPhone 13 Pro Max" \ | |
-destination "platform=iOS Simulator,OS=15.4,name=iPad mini (6th generation)" \ | |
-destination "platform=iOS Simulator,OS=15.4,name=iPad Pro (12.9-inch) (5th generation)" \ | |
-destination "platform=iOS Simulator,OS=15.4,name=iPhone SE (3rd generation)" \ | |
-destination "platform=iOS Simulator,OS=15.4,name=iPod touch (7th generation)" \ | |
-maximum-concurrent-test-simulator-destinations 3 | |
# Note: I run 9 simulators at once instead of 3 using `-maximum-concurrent-test-simulator-destinations 9` | |
# on my 64 GB machine. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Run this app on your target device to print that devices safe area insets. | |
// Sample output: | |
// | |
// case "iPad Pro (12.9-inch) (5th generation)": | |
// safeArea = UIEdgeInsets(top: 24.0, left: 0.0, bottom: 20.0, right: 0.0) | |
import SwiftUI | |
@main | |
struct TestApp: App { | |
var body: some Scene { | |
WindowGroup { | |
SafeAreaPrinter() | |
} | |
} | |
} | |
struct SafeAreaPrinter: View { | |
var body: some View { | |
GeometryReader { geo in | |
ZStack {} | |
.onAppear() { | |
print("case \"\(UIDevice.current.name)\":") | |
print( | |
" safeArea =", | |
UIEdgeInsets( | |
top: geo.safeAreaInsets.top, | |
left: geo.safeAreaInsets.leading, | |
bottom: geo.safeAreaInsets.bottom, | |
right: geo.safeAreaInsets.trailing | |
) | |
) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment