Skip to content

Commit

Permalink
Add support for renderscale (#7)
Browse files Browse the repository at this point in the history
* Add support for renderscale

* Fix
  • Loading branch information
materik authored May 8, 2024
1 parent 681af21 commit 786078a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
19 changes: 19 additions & 0 deletions Sources/SnapshotTestCase/Extensions/UIImageExtensions+Resize.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import UIKit

extension UIImage {
func resized(toScale scale: CGFloat) -> UIImage? {
guard scale > 0, scale != 1 else {
return self
}
return resized(toWidth: scale * size.width)
}

func resized(toWidth width: CGFloat, isOpaque: Bool = true) -> UIImage? {
let canvas = CGSize(width: width, height: CGFloat(ceil(width / size.width * size.height)))
let format = imageRendererFormat
format.opaque = isOpaque
return UIGraphicsImageRenderer(size: canvas, format: format).image { _ in
draw(in: CGRect(origin: .zero, size: canvas))
}
}
}
8 changes: 8 additions & 0 deletions Sources/SnapshotTestCase/Models/LaunchEnvironment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ enum LaunchEnvironment {
static let failurePath: String = "snapshotFailures"
static let tolerance: String = "snapshotTolerance"
static let renderOffsetY: String = "snapshotRenderOffsetY"
static let renderScale: String = "snapshotRenderScale"
static let recordMode: String = "-RecordingSnapshot"
}

Expand Down Expand Up @@ -34,4 +35,11 @@ enum LaunchEnvironment {
}
return Double(renderOffsetY) ?? 0
}

static var renderScale: CGFloat {
guard let renderScale = ProcessInfo.processInfo.environment[Key.renderScale] else {
return 1
}
return Double(renderScale) ?? 1
}
}
17 changes: 14 additions & 3 deletions Sources/SnapshotTestCase/Snapshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import UIKit

public class Snapshot {
static var renderOffsetY: CGFloat = LaunchEnvironment.renderOffsetY
static var renderScale: CGFloat = LaunchEnvironment.renderScale

enum Constants {
static let imageExt: String = "png"
Expand Down Expand Up @@ -189,9 +190,11 @@ private extension Snapshot.TestCase {
let window = UIWindow(frame: CGRect(origin: .zero, size: size))
window.rootViewController = viewController
window.makeKeyAndVisible()
let snapshot = try await renderSnapshot(view: view, in: size)
window.removeFromSuperview()
return try await crop(snapshot, to: size)
defer { window.removeFromSuperview() }
var snapshot = try await renderSnapshot(view: view, in: size)
snapshot = try await crop(snapshot, to: size)
snapshot = try await resize(snapshot, to: Snapshot.renderScale)
return snapshot
}

@MainActor
Expand Down Expand Up @@ -223,6 +226,14 @@ private extension Snapshot.TestCase {
return UIImage(cgImage: cgImage)
}

@MainActor
private func resize(_ image: UIImage, to scale: CGFloat) async throws -> UIImage {
guard let image = image.resized(toScale: scale) else {
throw SnapshotError.resizeSnapshot
}
return image
}

@MainActor
private func create(
with config: SnapshotConfig.Config,
Expand Down
1 change: 1 addition & 0 deletions Sources/SnapshotTestCase/SnapshotError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public enum SnapshotError: Error {
case comparison(Error)
case referenceImageNotEqual(Double)
case cropSnapshot
case resizeSnapshot
case timeout(String)
case unknown(Error)
}
Expand Down

0 comments on commit 786078a

Please sign in to comment.