Skip to content
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

Possible Strategy to use SwiftUI snapshots on a mac with ImageRenderer #888

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions Sources/SnapshotTesting/Snapshotting/NSView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion Tests/SnapshotTestingTests/AssertSnapshotSwiftTests.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#if canImport(Testing)
import Testing
import Foundation
import InlineSnapshotTesting
@_spi(Experimental) import SnapshotTesting

@Suite(
Expand Down
41 changes: 41 additions & 0 deletions Tests/SnapshotTestingTests/SwiftUITests.swift
Original file line number Diff line number Diff line change
@@ -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
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.