From 4f789af90ea93feb572d0c68a3753bace2cfcbc3 Mon Sep 17 00:00:00 2001 From: Hemanth Prasad Date: Fri, 4 Aug 2023 13:26:59 -0700 Subject: [PATCH] Preserve UIView margins for snapshots --- Sources/SnapshotTesting/Common/View.swift | 3 ++ .../SnapshotTestingTests.swift | 29 ++++++++++++++++++ ...llerPreserveDirectionalLayoutMargins.1.png | Bin 0 -> 25335 bytes 3 files changed, 32 insertions(+) create mode 100644 Tests/SnapshotTestingTests/__Snapshots__/SnapshotTestingTests/testUIViewControllerPreserveDirectionalLayoutMargins.1.png diff --git a/Sources/SnapshotTesting/Common/View.swift b/Sources/SnapshotTesting/Common/View.swift index fb6261c94..cc402109b 100644 --- a/Sources/SnapshotTesting/Common/View.swift +++ b/Sources/SnapshotTesting/Common/View.swift @@ -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 diff --git a/Tests/SnapshotTestingTests/SnapshotTestingTests.swift b/Tests/SnapshotTestingTests/SnapshotTestingTests.swift index 1de4623a6..72dd7a090 100644 --- a/Tests/SnapshotTestingTests/SnapshotTestingTests.swift +++ b/Tests/SnapshotTestingTests/SnapshotTestingTests.swift @@ -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 { diff --git a/Tests/SnapshotTestingTests/__Snapshots__/SnapshotTestingTests/testUIViewControllerPreserveDirectionalLayoutMargins.1.png b/Tests/SnapshotTestingTests/__Snapshots__/SnapshotTestingTests/testUIViewControllerPreserveDirectionalLayoutMargins.1.png new file mode 100644 index 0000000000000000000000000000000000000000..89c5feea21d0feb7cb235bbe44de827c485e7cc1 GIT binary patch literal 25335 zcmeI5drVtZ9LMkFj=~Cr#S!a(glQ&%ngJtgHD=?HPH19*F#gavUb;;P1V-vu4X;** z2Iq8(#u14b#JDBu3Yn;k*N|=EgiT%IN*Iq0aEoC)#$!-csOMgKSm^O;36OHRBlH6fleT?_t) z{Gs3fs^)-J%RNi3y}5OKAm=G2A;(TWX!ZJKK8zEj`yL$kK`yt>ktT_p#n*9ih@V+G z3$?r>HMv*oK~sAwYXy4kF(G~|>YX==$Z7q-#Mg8j_xRJ|!D~{tOp%+xx$5LJawZvy zFa9uj3tKsh*HO=S8=lj0T;z;K9hau(@7|0n*sFO=9XDXVo|YSa>0E4=aNWD-+>`e3 zk>@kswp*+0HtWpHNX4Lmmpzf?{AN6B)GKY)eHZWhH@x03c(WwBgA;rs*S^`JQ|wog zXOE65s$70Y8=pPXtuPdSa-*h1=PHwk?+5GcBg@CG{_eA|Yfk3;{Ze+eLW{Y3^kjvh zAv|B+;xb9FkXhW7Y43`u*gqcfwauP7v7^4Vzez} z3!7}}M`Nj~$3H+>sde$2dD(5*^gv|HhQ1f~n5%!Pk6K<{T4IdT5hKiQ%b@ zTVISxFtJ4utLv^`>L^wea<30GMf&Ay{8+2ysw$lKOd@=BdYs*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