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 } }