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

Support SwiftUI snapshots on macOS with NSImage #477

wants to merge 4 commits into from

Conversation

MaxDesiatov
Copy link
Contributor

Currently, snapshots for SwiftUI views aren't support on macOS, since there's no extension on Snapshotting where Value: SwiftUI.View, Format == NSImage. Such conformance is added, which reuses some of the code for NSView snapshotting.

@stephencelis
Copy link
Member

Would be nice to merge this but I'm not sure how to provide a consistent environment for that test to pass. On CI it seems to be outputting the wrong size, and locally things are failing for me, as well (visually things are identical, so might be something subtle like a color profile issue).

@MaxDesiatov
Copy link
Contributor Author

Yeah, I'm pretty sure that color profile and display scale (1x on CI and 2x Retina locally) are the culprits. I wonder if there's a way to enforce those in the tests, in the same way we can enforce dark/light mode appearance?

tillhainbach added a commit to tillhainbach/MacSnapshottesting that referenced this pull request Nov 2, 2021
@tillhainbach
Copy link

tillhainbach commented Nov 2, 2021

I wonder if there's a way to enforce those in the tests, in the same way we can enforce dark/light mode appearance?

I did a little bit of experimenting, initially trying to use CoreImage to resample the snapshot to generic color scheme and size with no success (colors will match but the antialiasing is different). So my guess was, not matter what, one has to manipulate the local Quartz Display Services. I found this gist and this which reveal private apis of CoreGraphics for setting the display mode. I set my Macbook to a low-res mode, et voilà, the snapshot test passed on CI. 🎉 🥳
Playgroundrepo with passing test here

EDIT: Turns out a working solution was actually already proposed in #412. I'll try if I can clean that PR up (fix conflicts with main and get test passing). => see #533

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants