Skip to content

Commit f659766

Browse files
moritzsternemannregexident
authored andcommitted
Make image diffing strategies custom scale aware (pointfreeco#336)
* Add scale parameter to image diffing strategies * Use updated diffing in CALayer, UIView, UIViewController and SwiftUIView strategies * Pass scale through to CGPath and UIBezierPath strategies * Re-generate project file
1 parent c466812 commit f659766

File tree

7 files changed

+22
-13
lines changed

7 files changed

+22
-13
lines changed

Sources/SnapshotTesting/Snapshotting/CALayer.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ extension Snapshotting where Value == CALayer, Format == UIImage {
3737
/// - Parameter precision: The percentage of pixels that must match.
3838
public static func image(precision: Float = 1, traits: UITraitCollection = .init())
3939
-> Snapshotting {
40-
return SimplySnapshotting.image(precision: precision).pullback { layer in
40+
return SimplySnapshotting.image(precision: precision, scale: traits.displayScale).pullback { layer in
4141
renderer(bounds: layer.bounds, for: traits).image { ctx in
4242
layer.setNeedsLayout()
4343
layer.layoutIfNeeded()

Sources/SnapshotTesting/Snapshotting/CGPath.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ extension Snapshotting where Value == CGPath, Format == UIImage {
4040
///
4141
/// - Parameter precision: The percentage of pixels that must match.
4242
public static func image(precision: Float = 1, scale: CGFloat = 1, drawingMode: CGPathDrawingMode = .eoFill) -> Snapshotting {
43-
return SimplySnapshotting.image(precision: precision).pullback { path in
43+
return SimplySnapshotting.image(precision: precision, scale: scale).pullback { path in
4444
let bounds = path.boundingBoxOfPath
4545
let format: UIGraphicsImageRendererFormat
4646
if #available(iOS 11.0, tvOS 11.0, *) {

Sources/SnapshotTesting/Snapshotting/SwiftUIView.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ extension Snapshotting where Value: SwiftUI.View, Format == UIImage {
5151
config = .init(safeArea: .zero, size: size, traits: traits)
5252
}
5353

54-
return SimplySnapshotting.image(precision: precision).asyncPullback { view in
54+
return SimplySnapshotting.image(precision: precision, scale: traits.displayScale).asyncPullback { view in
5555
var config = config
5656

5757
let controller: UIViewController

Sources/SnapshotTesting/Snapshotting/UIBezierPath.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ extension Snapshotting where Value == UIBezierPath, Format == UIImage {
1111
///
1212
/// - Parameter precision: The percentage of pixels that must match.
1313
public static func image(precision: Float = 1, scale: CGFloat = 1) -> Snapshotting {
14-
return SimplySnapshotting.image(precision: precision).pullback { path in
14+
return SimplySnapshotting.image(precision: precision, scale: scale).pullback { path in
1515
let bounds = path.bounds
1616
let format: UIGraphicsImageRendererFormat
1717
if #available(iOS 11.0, tvOS 11.0, *) {

Sources/SnapshotTesting/Snapshotting/UIImage.swift

+15-6
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,24 @@ import XCTest
44

55
extension Diffing where Value == UIImage {
66
/// A pixel-diffing strategy for UIImage's which requires a 100% match.
7-
public static let image = Diffing.image(precision: 1)
7+
public static let image = Diffing.image(precision: 1, scale: nil)
88

99
/// A pixel-diffing strategy for UIImage that allows customizing how precise the matching must be.
1010
///
1111
/// - Parameter precision: A value between 0 and 1, where 1 means the images must match 100% of their pixels.
12+
/// - Parameter scale: Scale to use when loading the reference image from disk. If `nil` or the `UITraitCollection`s default value of `0.0`, the screens scale is used.
1213
/// - Returns: A new diffing strategy.
13-
public static func image(precision: Float) -> Diffing {
14+
public static func image(precision: Float, scale: CGFloat?) -> Diffing {
15+
let imageScale: CGFloat
16+
if let scale = scale, scale != 0.0 {
17+
imageScale = scale
18+
} else {
19+
imageScale = UIScreen.main.scale
20+
}
21+
1422
return Diffing(
1523
toData: { $0.pngData() ?? emptyImage().pngData()! },
16-
fromData: { UIImage(data: $0, scale: UIScreen.main.scale)! }
24+
fromData: { UIImage(data: $0, scale: imageScale)! }
1725
) { old, new in
1826
guard !compare(old, new, precision: precision) else { return nil }
1927
let difference = SnapshotTesting.diff(old, new)
@@ -48,16 +56,17 @@ extension Diffing where Value == UIImage {
4856
extension Snapshotting where Value == UIImage, Format == UIImage {
4957
/// A snapshot strategy for comparing images based on pixel equality.
5058
public static var image: Snapshotting {
51-
return .image(precision: 1)
59+
return .image(precision: 1, scale: nil)
5260
}
5361

5462
/// A snapshot strategy for comparing images based on pixel equality.
5563
///
5664
/// - Parameter precision: The percentage of pixels that must match.
57-
public static func image(precision: Float) -> Snapshotting {
65+
/// - Parameter scale: The scale of the reference image stored on disk.
66+
public static func image(precision: Float, scale: CGFloat?) -> Snapshotting {
5867
return .init(
5968
pathExtension: "png",
60-
diffing: .image(precision: precision)
69+
diffing: .image(precision: precision, scale: scale)
6170
)
6271
}
6372
}

Sources/SnapshotTesting/Snapshotting/UIView.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ extension Snapshotting where Value == UIView, Format == UIImage {
2222
)
2323
-> Snapshotting {
2424

25-
return SimplySnapshotting.image(precision: precision).asyncPullback { view in
25+
return SimplySnapshotting.image(precision: precision, scale: traits.displayScale).asyncPullback { view in
2626
snapshotView(
2727
config: .init(safeArea: .zero, size: size ?? view.frame.size, traits: .init()),
2828
drawHierarchyInKeyWindow: drawHierarchyInKeyWindow,

Sources/SnapshotTesting/Snapshotting/UIViewController.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ extension Snapshotting where Value == UIViewController, Format == UIImage {
2222
)
2323
-> Snapshotting {
2424

25-
return SimplySnapshotting.image(precision: precision).asyncPullback { viewController in
25+
return SimplySnapshotting.image(precision: precision, scale: traits.displayScale).asyncPullback { viewController in
2626
snapshotView(
2727
config: size.map { .init(safeArea: config.safeArea, size: $0, traits: config.traits) } ?? config,
2828
drawHierarchyInKeyWindow: false,
@@ -48,7 +48,7 @@ extension Snapshotting where Value == UIViewController, Format == UIImage {
4848
)
4949
-> Snapshotting {
5050

51-
return SimplySnapshotting.image(precision: precision).asyncPullback { viewController in
51+
return SimplySnapshotting.image(precision: precision, scale: traits.displayScale).asyncPullback { viewController in
5252
snapshotView(
5353
config: .init(safeArea: .zero, size: size, traits: traits),
5454
drawHierarchyInKeyWindow: drawHierarchyInKeyWindow,

0 commit comments

Comments
 (0)