diff --git a/Tests/GravatarUITests/CropFrameOverlayViewTests.swift b/Tests/GravatarUITests/CropFrameOverlayViewTests.swift new file mode 100644 index 00000000..51023210 --- /dev/null +++ b/Tests/GravatarUITests/CropFrameOverlayViewTests.swift @@ -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) + } +} diff --git a/Tests/GravatarUITests/UIImageAdditionsTests.swift b/Tests/GravatarUITests/UIImageAdditionsTests.swift index 99be4d94..c88210ee 100644 --- a/Tests/GravatarUITests/UIImageAdditionsTests.swift +++ b/Tests/GravatarUITests/UIImageAdditionsTests.swift @@ -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) @@ -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) @@ -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) @@ -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)) diff --git a/Tests/GravatarUITests/__Snapshots__/CropFrameOverlayViewTests/testCropFrameOverlay.1.png b/Tests/GravatarUITests/__Snapshots__/CropFrameOverlayViewTests/testCropFrameOverlay.1.png new file mode 100644 index 00000000..ff0256b2 Binary files /dev/null and b/Tests/GravatarUITests/__Snapshots__/CropFrameOverlayViewTests/testCropFrameOverlay.1.png differ