Skip to content

Commit

Permalink
Add a snapshot test
Browse files Browse the repository at this point in the history
  • Loading branch information
pinarol committed Dec 12, 2024
1 parent 8ca0f8a commit d7e468f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
39 changes: 39 additions & 0 deletions Tests/GravatarUITests/CropFrameOverlayViewTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
@testable import GravatarUI
import SnapshotTesting
import XCTest

final class CropFrameOverlayViewTests: XCTestCase {
enum Constants {
static let imageSize: CGSize = .init(width: 320, height: 480)
}

override func invokeTest() {
withSnapshotTesting(record: .failed) {
super.invokeTest()
}
}

@MainActor
func testCropFrameOverlay() throws {
let image = UIImage.create(size: Constants.imageSize)
let imageView = UIImageView(image: image)
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.widthAnchor.constraint(equalToConstant: Constants.imageSize.width).isActive = true
imageView.heightAnchor.constraint(equalToConstant: Constants.imageSize.height).isActive = true
imageView.contentMode = .scaleAspectFill
let containerView = CropFrameOverlayView()
// center the cropping layer
containerView.scrollViewFrame = .init(
origin: .init(x: 0, y: (Constants.imageSize.height - Constants.imageSize.width) / 2),
size: .init(width: Constants.imageSize.width, height: Constants.imageSize.width)
)
containerView.translatesAutoresizingMaskIntoConstraints = false
containerView.widthAnchor.constraint(equalToConstant: Constants.imageSize.width).isActive = true
containerView.heightAnchor.constraint(equalToConstant: Constants.imageSize.height).isActive = true
imageView.addSubview(containerView)
containerView.centerXAnchor.constraint(equalTo: imageView.centerXAnchor).isActive = true
containerView.centerYAnchor.constraint(equalTo: imageView.centerYAnchor).isActive = true
containerView.backgroundColor = .clear
assertSnapshot(of: imageView, as: .image)
}
}
12 changes: 7 additions & 5 deletions Tests/GravatarUITests/UIImageAdditionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import XCTest
final class UIImageAdditionsTests: XCTestCase {
@MainActor
func testSquareUnequalEdgesSmallerThanMax() throws {
let image = try XCTUnwrap(createImage(size: .init(width: 96.1, height: 96.0)))
let image = try XCTUnwrap(UIImage.create(size: .init(width: 96.1, height: 96.0)))
let squareImage = try XCTUnwrap(image.square(maxLength: 1024))
let targetLength = min(image.size.width * image.scale, image.size.height * image.scale)
XCTAssertEqual(squareImage.size.width, squareImage.size.height)
Expand All @@ -15,7 +15,7 @@ final class UIImageAdditionsTests: XCTestCase {
@MainActor
func testSquareUnequalEdgesBiggerThanMax() throws {
let maxLength: CGFloat = 50.0
let image = try XCTUnwrap(createImage(size: .init(width: 96.1, height: 96.0)))
let image = try XCTUnwrap(UIImage.create(size: .init(width: 96.1, height: 96.0)))
let squareImage = try XCTUnwrap(image.square(maxLength: maxLength))
XCTAssertEqual(squareImage.size.width, squareImage.size.height)
XCTAssertEqual(squareImage.size.width * squareImage.scale, maxLength)
Expand All @@ -25,7 +25,7 @@ final class UIImageAdditionsTests: XCTestCase {
@MainActor
func testSquareEqualEdgesBiggerThanMax() throws {
let maxLength: CGFloat = 50.0
let image = try XCTUnwrap(createImage(size: .init(width: 96.0, height: 96.0)))
let image = try XCTUnwrap(UIImage.create(size: .init(width: 96.0, height: 96.0)))
let squareImage = try XCTUnwrap(image.square(maxLength: maxLength))
XCTAssertEqual(squareImage.size.width, squareImage.size.height)
XCTAssertEqual(squareImage.size.width * squareImage.scale, maxLength)
Expand All @@ -34,15 +34,17 @@ final class UIImageAdditionsTests: XCTestCase {

@MainActor
func testSquareEqualEdgesSmallerThanMax() throws {
let image = try XCTUnwrap(createImage(size: .init(width: 96.1, height: 96.1)))
let image = try XCTUnwrap(UIImage.create(size: .init(width: 96.1, height: 96.1)))
let squareImage = try XCTUnwrap(image.square(maxLength: 1024))
let targetLength = min(image.size.width * image.scale, image.size.height * image.scale)
XCTAssertEqual(squareImage.size.width, squareImage.size.height)
XCTAssertEqual(squareImage.size.width * squareImage.scale, targetLength)
XCTAssertEqual(squareImage.size.height * squareImage.scale, targetLength)
}
}

private func createImage(color: UIColor = .blue, size: CGSize) -> UIImage? {
extension UIImage {
static func create(color: UIColor = .blue, size: CGSize) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(CGRectMake(0, 0, size.width, size.height))
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d7e468f

Please sign in to comment.