Skip to content

Support SwiftUI snapshots on macOS with NSImage #477

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion Sources/SnapshotTesting/Snapshotting/SwiftUIView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extension Snapshotting where Value: SwiftUI.View, Format == UIImage {
/// - Parameters:
/// - drawHierarchyInKeyWindow: Utilize the simulator's key window in order to render `UIAppearance` and `UIVisualEffect`s. This option requires a host application for your tests and will _not_ work for framework test targets.
/// - precision: The percentage of pixels that must match.
/// - size: A view size override.
/// - layout: A view size override.
/// - traits: A trait collection override.
public static func image(
drawHierarchyInKeyWindow: Bool = false,
Expand Down Expand Up @@ -80,4 +80,59 @@ extension Snapshotting where Value: SwiftUI.View, Format == UIImage {
}
}
#endif

#if os(macOS)
@available(macOS 11.0, *)
extension Snapshotting where Value: SwiftUI.View, Format == NSImage {

/// A snapshot strategy for comparing SwiftUI Views based on pixel equality.
public static var image: Snapshotting {
return .image()
}

/// A snapshot strategy for comparing SwiftUI Views based on pixel equality.
///
/// - Parameters:
/// - precision: The percentage of pixels that must match.
/// - layout: A view size override.
/// - appearance: A light/dark mode override.
public static func image(
precision: Float = 1,
layout: SwiftUISnapshotLayout = .sizeThatFits,
appearance: NSAppearance? = NSAppearance(named: .aqua)
) -> Snapshotting {
let size: CGSize?

switch layout {
case .sizeThatFits:
size = nil
case let .fixed(width: width, height: height):
size = .init(width: width, height: height)
}
return SimplySnapshotting.image(precision: precision).asyncPullback { swiftUIView in
let controller = NSHostingController(rootView: swiftUIView)
let view = controller.view
view.appearance = appearance

let initialSize = view.frame.size
view.frame.size = size ?? controller.sizeThatFits(in: .zero)
guard view.frame.width > 0, view.frame.height > 0 else {
fatalError("View not renderable to image at size \(view.frame.size)")
}

return view.snapshot ?? Async { callback in
addImagesForRenderedViews(view).sequence().run { views in
let bitmapRep = view.bitmapImageRepForCachingDisplay(in: view.bounds)!
view.cacheDisplay(in: view.bounds, to: bitmapRep)
let image = NSImage(size: view.bounds.size)
image.addRepresentation(bitmapRep)
callback(image)
views.forEach { $0.removeFromSuperview() }
view.frame.size = initialSize
}
}
}
}
}
#endif
#endif
21 changes: 21 additions & 0 deletions Tests/SnapshotTestingTests/SnapshotTestingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,27 @@ final class SnapshotTestingTests: XCTestCase {
#endif
}

@available(macOS 11.0, *)
func testSwiftUIView_macOS() {
#if os(macOS)
struct MyView: SwiftUI.View {
var body: some SwiftUI.View {
HStack {
Image(systemName: "checkmark.circle.fill")
Text("Checked").fixedSize()
}
.padding(5)
.background(RoundedRectangle(cornerRadius: 5.0).fill(Color.blue))
.padding(10)
}
}

let view = MyView().background(Color.yellow)

assertSnapshot(matching: view, as: .image)
#endif
}

@available(*, deprecated)
func testIsRecordingProxy() {
SnapshotTesting.record = true
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.