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
import ImageIO
import UIKit
import UniformTypeIdentifiers
import XCTest
@testable import OldFella
@MainActor
final class ImageUploadTests: XCTestCase {
func testAspectRatioMatchesEncodedPixelsForEachImageShape() throws {
for size in [
CGSize(width: 160, height: 90), CGSize(width: 90, height: 160),
CGSize(width: 64, height: 64), CGSize(width: 101, height: 67)
] {
let data = try XCTUnwrap(sourceImage(size: size).pngData())
let upload = try ATProtoBlueskyService.prepareImageForUpload(
ImageAttachment(data: data, mimeType: "image/png")
)
let encoded = try XCTUnwrap(UIImage(data: upload.data)?.cgImage)
XCTAssertEqual(upload.width, Int(size.width))
XCTAssertEqual(upload.height, Int(size.height))
XCTAssertEqual(upload.width, encoded.width)
XCTAssertEqual(upload.height, encoded.height)
XCTAssertEqual(upload.mimeType, "image/jpeg")
}
}
func testAspectRatioAccountsForEveryEXIFOrientation() throws {
let pixels = try XCTUnwrap(sourceImage(size: CGSize(width: 80, height: 40)).cgImage)
for rawValue in UInt32(1)...8 {
let orientation = try XCTUnwrap(CGImagePropertyOrientation(rawValue: rawValue))
let data = try orientedJPEG(pixels, orientation: orientation)
let upload = try ATProtoBlueskyService.prepareImageForUpload(ImageAttachment(data: data))
let encoded = try XCTUnwrap(UIImage(data: upload.data))
let encodedPixels = try XCTUnwrap(encoded.cgImage)
let swapsAxes = rawValue >= 5
XCTAssertEqual(upload.width, swapsAxes ? 40 : 80, "EXIF orientation: \(rawValue)")
XCTAssertEqual(upload.height, swapsAxes ? 80 : 40, "EXIF orientation: \(rawValue)")
XCTAssertEqual(upload.width, encodedPixels.width)
XCTAssertEqual(upload.height, encodedPixels.height)
XCTAssertEqual(encoded.imageOrientation, .up)
}
}
func testPixelLimitDoesNotMultiplyByScreenScale() throws {
let data = try XCTUnwrap(sourceImage(size: CGSize(width: 3072, height: 1536)).pngData())
let upload = try ATProtoBlueskyService.prepareImageForUpload(ImageAttachment(data: data))
let encoded = try XCTUnwrap(UIImage(data: upload.data)?.cgImage)
XCTAssertEqual(upload.width, 2048)
XCTAssertEqual(upload.height, 1024)
XCTAssertEqual(encoded.width, upload.width)
XCTAssertEqual(encoded.height, upload.height)
XCTAssertLessThanOrEqual(upload.data.count, 1_000_000)
}
func testInvalidImageFailsBeforeUploadMetadataIsCreated() {
XCTAssertThrowsError(try ATProtoBlueskyService.prepareImageForUpload(ImageAttachment(data: Data())))
}
private func sourceImage(size: CGSize) -> UIImage {
let format = UIGraphicsImageRendererFormat()
format.scale = 1
return UIGraphicsImageRenderer(size: size, format: format).image { context in
UIColor.systemBlue.setFill()
context.fill(CGRect(origin: .zero, size: size))
}
}
private func orientedJPEG(_ pixels: CGImage, orientation: CGImagePropertyOrientation) throws -> Data {
let data = NSMutableData()
let destination = try XCTUnwrap(
CGImageDestinationCreateWithData(
data, UTType.jpeg.identifier as CFString, 1, nil
))
CGImageDestinationAddImage(
destination, pixels, [kCGImagePropertyOrientation: orientation.rawValue] as CFDictionary
)
XCTAssertTrue(CGImageDestinationFinalize(destination))
return data as Data
}
}