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

Provide a Snapshotting strategy using UIView.systemLayoutSizeFitting #727

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
37 changes: 37 additions & 0 deletions Sources/SnapshotTesting/Snapshotting/UIView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,43 @@ extension Snapshotting where Value == UIView, Format == UIImage {
)
}
}

/// A snapshot strategy for comparing views based on pixel equality.
///
/// - Parameters:
/// - config: A set of device configuration settings.
/// - precision: The percentage of pixels that must match.
/// - perceptualPrecision: The percentage a pixel must match the source pixel to be considered a match. [98-99% mimics the precision of the human eye.](http://zschuessler.github.io/DeltaE/learn/#toc-defining-delta-e)
/// - targetSize: The size that you prefer for the view. To obtain a view that is as small as possible, specify the constant [layoutFittingCompressedSize](https://developer.apple.com/documentation/uikit/uiview/1622568-layoutfittingcompressedsize). To obtain a view that is as large as possible, specify the constant [layoutFittingExpandedSize](https://developer.apple.com/documentation/uikit/uiview/1622532-layoutfittingexpandedsize).
/// - horizontalFittingPriority: The priority for horizontal constraints. Specify [fittingSizeLevel](https://developer.apple.com/documentation/uikit/uilayoutpriority/1622248-fittingsizelevel) to get a width that is as close as possible to the width value of `targetSize`.
/// - verticalFittingPriority: The priority for vertical constraints. Specify [fittingSizeLevel](https://developer.apple.com/documentation/uikit/uilayoutpriority/1622248-fittingsizelevel) to get a height that is as close as possible to the height value of `targetSize`.
/// - traits: A trait collection override.
public static func image(
drawHierarchyInKeyWindow: Bool = false,
precision: Float = 1,
perceptualPrecision: Float = 1,
targetSize: CGSize,
horizontalFittingPriority: UILayoutPriority,
verticalFittingPriority: UILayoutPriority,
traits: UITraitCollection = .init()
)
-> Snapshotting {

return SimplySnapshotting.image(precision: precision, perceptualPrecision: perceptualPrecision, scale: traits.displayScale).asyncPullback { view in
let size = view.systemLayoutSizeFitting(
targetSize,
withHorizontalFittingPriority: horizontalFittingPriority,
verticalFittingPriority: verticalFittingPriority
)
return snapshotView(
config: .init(safeArea: .zero, size: size, traits: .init()),
drawHierarchyInKeyWindow: drawHierarchyInKeyWindow,
traits: traits,
view: view,
viewController: .init()
)
}
}
}

extension Snapshotting where Value == UIView, Format == String {
Expand Down
17 changes: 17 additions & 0 deletions Tests/SnapshotTestingTests/SnapshotTestingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,23 @@ final class SnapshotTestingTests: XCTestCase {
assertSnapshot(matching: view, as: .recursiveDescription)
#endif
}

func testSystemLayoutFittingWithView() {
#if os(iOS)
let label = UILabel()
label.text = "What's the point?"
label.numberOfLines = .zero

assertSnapshot(
matching: label,
as: .image(
targetSize: CGSize(width: 50, height: UIView.layoutFittingExpandedSize.height),
horizontalFittingPriority: .required,
verticalFittingPriority: .fittingSizeLevel
)
)
#endif
}

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