1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import UIKit
import XCTest
@testable import OldFella
final class AvatarCropTests: XCTestCase {
func testPanKeepsTheSquareCoveredAfterRotationAndFlip() {
let landscape = CGSize(width: 300, height: 100)
var crop = AvatarCrop()
XCTAssertEqual(crop.scaledImageSize(for: landscape), CGSize(width: 3, height: 1))
crop.pan(by: CGSize(width: 2, height: 2), imageSize: landscape)
XCTAssertEqual(crop.offset, CGSize(width: 1, height: 0))
crop.rotateClockwise()
XCTAssertEqual(crop.quarterTurns, 1)
XCTAssertEqual(crop.offset, CGSize(width: 0, height: 1))
crop.pan(by: CGSize(width: 2, height: -3), imageSize: landscape)
XCTAssertEqual(crop.offset, CGSize(width: 0, height: -1))
crop.flipHorizontally()
crop.rotateClockwise()
XCTAssertTrue(crop.isFlipped)
XCTAssertEqual(crop.quarterTurns, 0)
XCTAssertEqual(crop.offset, CGSize(width: 1, height: 0))
}
func testZoomPreservesThePinchAnchorUntilItReachesAnEdge() {
let square = CGSize(width: 200, height: 200)
let anchor = CGPoint(x: 0.25, y: -0.25)
var crop = AvatarCrop()
crop.magnify(by: 2, anchor: anchor, imageSize: square)
XCTAssertEqual(crop.zoom, 2)
XCTAssertEqual(crop.offset, CGSize(width: -0.25, height: 0.25))
crop.magnify(by: 100, anchor: anchor, imageSize: square)
XCTAssertEqual(crop.zoom, AvatarCrop.maximumZoom)
XCTAssertEqual(crop.offset, CGSize(width: -0.75, height: 0.75))
crop.pan(by: CGSize(width: 5, height: -5), imageSize: square)
XCTAssertEqual(crop.offset, CGSize(width: 1.5, height: -1.5))
crop.magnify(by: 0.1, anchor: anchor, imageSize: square)
XCTAssertEqual(crop.zoom, 1)
XCTAssertEqual(crop.offset, .zero)
}
func testJPEGMatchesTheCropAfterFlippingAndRotating() throws {
let format = UIGraphicsImageRendererFormat()
format.scale = 1
let source = UIGraphicsImageRenderer(size: CGSize(width: 200, height: 200), format: format).image { renderer in
UIColor.red.setFill()
renderer.fill(CGRect(x: 0, y: 0, width: 100, height: 100))
UIColor.green.setFill()
renderer.fill(CGRect(x: 100, y: 0, width: 100, height: 100))
UIColor.blue.setFill()
renderer.fill(CGRect(x: 0, y: 100, width: 100, height: 100))
UIColor.yellow.setFill()
renderer.fill(CGRect(x: 100, y: 100, width: 100, height: 100))
}
var crop = AvatarCrop()
crop.flipHorizontally()
crop.rotateClockwise()
let data = try crop.jpegData(from: source)
let result = try XCTUnwrap(UIImage(data: data)?.cgImage)
XCTAssertEqual(result.width, 512)
XCTAssertEqual(result.height, 512)
XCTAssertLessThanOrEqual(data.count, 1_000_000)
try assertColor(in: result, x: 128, y: 128, red: 255, green: 255, blue: 0)
try assertColor(in: result, x: 384, y: 128, red: 0, green: 255, blue: 0)
try assertColor(in: result, x: 128, y: 384, red: 0, green: 0, blue: 255)
try assertColor(in: result, x: 384, y: 384, red: 255, green: 0, blue: 0)
}
private func assertColor(in image: CGImage, x: Int, y: Int, red: Int, green: Int, blue: Int) throws {
let sample = try XCTUnwrap(image.cropping(to: CGRect(x: x, y: y, width: 1, height: 1)))
let bitmap = try XCTUnwrap(CGContext(
data: nil, width: 1, height: 1, bitsPerComponent: 8, bytesPerRow: 4,
space: CGColorSpace(name: CGColorSpace.sRGB)!,
bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue | CGBitmapInfo.byteOrder32Big.rawValue
))
bitmap.draw(sample, in: CGRect(x: 0, y: 0, width: 1, height: 1))
let pixel = try XCTUnwrap(bitmap.data).assumingMemoryBound(to: UInt8.self)
XCTAssertEqual(Int(pixel[0]), red, accuracy: 12)
XCTAssertEqual(Int(pixel[1]), green, accuracy: 12)
XCTAssertEqual(Int(pixel[2]), blue, accuracy: 12)
}
}