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

Preserve View margins for snapshots #757

Open
wants to merge 1 commit 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
3 changes: 3 additions & 0 deletions Sources/SnapshotTesting/Common/View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -960,8 +960,11 @@ func snapshotView(
view: view,
viewController: viewController
)

// NB: Avoid safe area influence.
let directionalLayoutMargins = view.directionalLayoutMargins
if config.safeArea == .zero { view.frame.origin = .init(x: offscreen, y: offscreen) }
view.directionalLayoutMargins = directionalLayoutMargins

return (view.snapshot ?? Async { callback in
addImagesForRenderedViews(view).sequence().run { views in
Expand Down
29 changes: 29 additions & 0 deletions Tests/SnapshotTestingTests/SnapshotTestingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,35 @@ final class SnapshotTestingTests: XCTestCase {
#endif
}

func testUIViewControllerPreserveDirectionalLayoutMargins() {
#if os(iOS)
class ViewController: UIViewController {

let blueView: UIView = {
let view = UIView()
view.backgroundColor = .blue
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()

override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
view.addSubview(blueView)
NSLayoutConstraint.activate([
blueView.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor),
blueView.leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor),
blueView.trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor),
blueView.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor)
])
}
}
let device: ViewImageConfig = .iPhone8(.landscape)
let vc = ViewController()
assertSnapshot(matching: vc, as: .image(on: device))
#endif
}

func testUIViewControllerLifeCycle() {
#if os(iOS)
class ViewController: UIViewController {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.