diff --git a/Sources/SnapshotTesting/Snapshotting/NSView.swift b/Sources/SnapshotTesting/Snapshotting/NSView.swift index b2e7edfb0..7ccf76378 100644 --- a/Sources/SnapshotTesting/Snapshotting/NSView.swift +++ b/Sources/SnapshotTesting/Snapshotting/NSView.swift @@ -34,8 +34,13 @@ return view.snapshot ?? Async { callback in addImagesForRenderedViews(view).sequence().run { views in + let scaleFactor = NSScreen.main?.backingScaleFactor ?? 1.0 let bitmapRep = view.bitmapImageRepForCachingDisplay(in: view.bounds)! + let scaledSize = CGSize(width: initialSize.width * scaleFactor, height: initialSize.height * scaleFactor) + bitmapRep.size = scaledSize + view.cacheDisplay(in: view.bounds, to: bitmapRep) + let image = NSImage(size: view.bounds.size) image.addRepresentation(bitmapRep) callback(image) diff --git a/Tests/SnapshotTestingTests/AssertSnapshotSwiftTests.swift b/Tests/SnapshotTestingTests/AssertSnapshotSwiftTests.swift index 12528b843..385b0f2b8 100644 --- a/Tests/SnapshotTestingTests/AssertSnapshotSwiftTests.swift +++ b/Tests/SnapshotTestingTests/AssertSnapshotSwiftTests.swift @@ -1,7 +1,6 @@ #if canImport(Testing) import Testing import Foundation - import InlineSnapshotTesting @_spi(Experimental) import SnapshotTesting @Suite( diff --git a/Tests/SnapshotTestingTests/SwiftUITests.swift b/Tests/SnapshotTestingTests/SwiftUITests.swift new file mode 100644 index 000000000..08e5ef095 --- /dev/null +++ b/Tests/SnapshotTestingTests/SwiftUITests.swift @@ -0,0 +1,41 @@ +#if compiler(>=6) && canImport(Testing) && canImport(SwiftUI) + import Testing + import SnapshotTesting + import SwiftUI + +/// you can use `NSScreen.main?.backingScaleFactor` or `UIScreen.main.scale` but for more consistent results a fixed value is choosen +let scaleFactor: CGFloat = 4 + +private struct TestView: View { + var body: some View { + ZStack { + Color.white + Text("Hello world") + } + .frame(width: 120) + } +} + +#if canImport(AppKit) +import AppKit + +@MainActor @Test func simpleSwiftUIViewAllPlatforms() async throws { + let renderer = ImageRenderer(content: TestView()) + renderer.scale = scaleFactor + + // Note there is an issue with blurred images https://github.com/pointfreeco/swift-snapshot-testing/issues/428 + // but here the scaleFactor is chosen large so it is nearly not visible. + let nsImage = try #require(renderer.nsImage) + assertSnapshot(of: nsImage, as: .image, named: "appkit") +} +#elseif canImport(UIKit) +@available(iOS 16.0, tvOS 16.0, *) +@MainActor @Test func simpleSwiftUIViewAllPlatforms() async throws { + let renderer = ImageRenderer(content: TestView()) + renderer.scale = scaleFactor + + let nsImage = try #require(renderer.uiImage) + assertSnapshot(of: nsImage, as: .image, named: "uikit") +} +#endif +#endif diff --git a/Tests/SnapshotTestingTests/__Snapshots__/SwiftUITests/simpleSwiftUIViewAllPlatforms.appkit.png b/Tests/SnapshotTestingTests/__Snapshots__/SwiftUITests/simpleSwiftUIViewAllPlatforms.appkit.png new file mode 100644 index 000000000..cd195c909 Binary files /dev/null and b/Tests/SnapshotTestingTests/__Snapshots__/SwiftUITests/simpleSwiftUIViewAllPlatforms.appkit.png differ diff --git a/Tests/SnapshotTestingTests/__Snapshots__/SwiftUITests/simpleSwiftUIViewAllPlatforms.uikit.png b/Tests/SnapshotTestingTests/__Snapshots__/SwiftUITests/simpleSwiftUIViewAllPlatforms.uikit.png new file mode 100644 index 000000000..38f45c699 Binary files /dev/null and b/Tests/SnapshotTestingTests/__Snapshots__/SwiftUITests/simpleSwiftUIViewAllPlatforms.uikit.png differ