-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* use transparent background for diff * add premultiply alpha before comparing * premultiply alpha for diff, clear background for host * use transparent background * test compare of same data should have 0 difference * use hierarchy and sRGB * use scale 1.0 * allow appearance transition when adding and removing child controller * move settingAlphaOne to package * use extended range and P3 gamut * set flatness, test no rendering in unit tests * fix: unbalance calls to appearance for delaying for 0.01s * use standard range to fix image comparison for sample view * fix: favorite view renders with 5% accuracy on GitHub runner
- Loading branch information
Showing
15 changed files
with
261 additions
and
45 deletions.
There are no files selected for viewing
This file contains 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
105 changes: 105 additions & 0 deletions
105
.swiftpm/xcode/xcshareddata/xcschemes/SwiftUI-snapshot-testing.xcscheme
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Scheme | ||
LastUpgradeVersion = "1340" | ||
version = "1.3"> | ||
<BuildAction | ||
parallelizeBuildables = "YES" | ||
buildImplicitDependencies = "YES"> | ||
<BuildActionEntries> | ||
<BuildActionEntry | ||
buildForTesting = "YES" | ||
buildForRunning = "YES" | ||
buildForProfiling = "YES" | ||
buildForArchiving = "YES" | ||
buildForAnalyzing = "YES"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "SwiftUI_SnapshotTesting" | ||
BuildableName = "SwiftUI_SnapshotTesting" | ||
BlueprintName = "SwiftUI_SnapshotTesting" | ||
ReferencedContainer = "container:"> | ||
</BuildableReference> | ||
</BuildActionEntry> | ||
<BuildActionEntry | ||
buildForTesting = "YES" | ||
buildForRunning = "YES" | ||
buildForProfiling = "NO" | ||
buildForArchiving = "NO" | ||
buildForAnalyzing = "YES"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "UnitTests" | ||
BuildableName = "UnitTests" | ||
BlueprintName = "UnitTests" | ||
ReferencedContainer = "container:"> | ||
</BuildableReference> | ||
</BuildActionEntry> | ||
<BuildActionEntry | ||
buildForTesting = "YES" | ||
buildForRunning = "YES" | ||
buildForProfiling = "YES" | ||
buildForArchiving = "YES" | ||
buildForAnalyzing = "YES"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "ViewSnapshotTesting" | ||
BuildableName = "ViewSnapshotTesting" | ||
BlueprintName = "ViewSnapshotTesting" | ||
ReferencedContainer = "container:"> | ||
</BuildableReference> | ||
</BuildActionEntry> | ||
</BuildActionEntries> | ||
</BuildAction> | ||
<TestAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
shouldUseLaunchSchemeArgsEnv = "YES"> | ||
<Testables> | ||
<TestableReference | ||
skipped = "NO"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "UnitTests" | ||
BuildableName = "UnitTests" | ||
BlueprintName = "UnitTests" | ||
ReferencedContainer = "container:"> | ||
</BuildableReference> | ||
</TestableReference> | ||
</Testables> | ||
</TestAction> | ||
<LaunchAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
launchStyle = "0" | ||
useCustomWorkingDirectory = "NO" | ||
ignoresPersistentStateOnLaunch = "NO" | ||
debugDocumentVersioning = "YES" | ||
debugServiceExtension = "internal" | ||
allowLocationSimulation = "YES"> | ||
</LaunchAction> | ||
<ProfileAction | ||
buildConfiguration = "Release" | ||
shouldUseLaunchSchemeArgsEnv = "YES" | ||
savedToolIdentifier = "" | ||
useCustomWorkingDirectory = "NO" | ||
debugDocumentVersioning = "YES"> | ||
<MacroExpansion> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "SwiftUI_SnapshotTesting" | ||
BuildableName = "SwiftUI_SnapshotTesting" | ||
BlueprintName = "SwiftUI_SnapshotTesting" | ||
ReferencedContainer = "container:"> | ||
</BuildableReference> | ||
</MacroExpansion> | ||
</ProfileAction> | ||
<AnalyzeAction | ||
buildConfiguration = "Debug"> | ||
</AnalyzeAction> | ||
<ArchiveAction | ||
buildConfiguration = "Release" | ||
revealArchiveInOrganizer = "YES"> | ||
</ArchiveAction> | ||
</Scheme> |
This file contains 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,67 @@ | ||
import UIKit | ||
import XCTest | ||
|
||
extension UIView { | ||
func renderFormat() -> UIGraphicsImageRendererFormat { | ||
let format = UIGraphicsImageRendererFormat(for: .current) | ||
format.opaque = true | ||
extension UITraitCollection { | ||
static let snapshots = UITraitCollection(traitsFrom: [ | ||
UITraitCollection(displayGamut: .P3), | ||
UITraitCollection(displayScale: 1.0), | ||
UITraitCollection(activeAppearance: .active), | ||
UITraitCollection(userInterfaceLevel: .base), | ||
UITraitCollection(legibilityWeight: .regular), | ||
UITraitCollection(userInterfaceStyle: .light), | ||
UITraitCollection(preferredContentSizeCategory: .medium), | ||
]) | ||
} | ||
|
||
extension UIGraphicsImageRendererFormat { | ||
static let snapshots: UIGraphicsImageRendererFormat = { | ||
let format = UIGraphicsImageRendererFormat(for: .snapshots) | ||
format.opaque = false | ||
format.preferredRange = .standard | ||
return format | ||
} | ||
}() | ||
} | ||
|
||
extension UIView { | ||
func renderer() -> UIGraphicsImageRenderer { | ||
UIGraphicsImageRenderer(bounds: bounds, format: renderFormat()) | ||
UIGraphicsImageRenderer(bounds: bounds, format: .snapshots) | ||
} | ||
|
||
func renderLayerAsBitmap() -> UIImage { | ||
renderer().image { | ||
layer.render(in: $0.cgContext) | ||
} | ||
renderer().image(actions: renderLayerActions(_:)) | ||
} | ||
|
||
func renderLayerAsPNG() -> Data { | ||
renderer().pngData(actions: renderLayerActions(_:)) | ||
} | ||
|
||
func renderLayerActions(_ context: UIGraphicsImageRendererContext) { | ||
configureContext(context) | ||
layer.render(in: context.cgContext) | ||
} | ||
|
||
func configureContext(_ context: UIGraphicsImageRendererContext) { | ||
context.cgContext.setFlatness(0.01) | ||
context.cgContext.setShouldAntialias(false) | ||
context.cgContext.setAllowsAntialiasing(false) | ||
context.cgContext.setAllowsFontSubpixelPositioning(false) | ||
context.cgContext.setShouldSubpixelPositionFonts(false) | ||
context.cgContext.setShouldSmoothFonts(false) | ||
context.cgContext.setAllowsFontSubpixelQuantization(false) | ||
context.cgContext.setShouldSubpixelQuantizeFonts(false) | ||
} | ||
|
||
func renderHierarchyAsPNG() -> Data { | ||
renderer().pngData(actions: drawHierarchyActions(_:)) | ||
} | ||
|
||
func drawHierarchyActions(_ context: UIGraphicsImageRendererContext) { | ||
configureContext(context) | ||
XCTAssertTrue(drawHierarchy(in: bounds, afterScreenUpdates: true), | ||
"unable to take snapshot of the view") | ||
} | ||
|
||
func renderHierarchyOnScreen() -> UIImage { | ||
renderer().image { _ in | ||
drawHierarchy(in: bounds, afterScreenUpdates: true) | ||
} | ||
renderer().image(actions: drawHierarchyActions(_:)) | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
@testable import ViewSnapshotTesting | ||
import XCTest | ||
|
||
class CompareTest: XCTestCase { | ||
func testSameDataShouldHave0Difference() throws { | ||
let expectedData = try Data( | ||
contentsOf: folderUrl().appendingPathComponent("SampleView.png") | ||
) | ||
let result = compare(expectedData, expectedData) | ||
XCTAssertEqual(result.maxColorDifference(), 0) | ||
} | ||
} |
Oops, something went wrong.