~ knotless Search/
↑↓ move↵ open⇧↵ all results

geesawra.industries/old-fella

Old people's social media app

0
1
main
90 lines · 4.5 KB Raw History
import ImageIO
import UIKit
import UniformTypeIdentifiers
import XCTest

@testable import OldFella

@MainActor
final class ImageResolutionTests: XCTestCase {
    func testInlineImagesAreReducedAndViewerImagesKeepSourcePixels() async throws {
        for size in [CGSize(width: 2400, height: 1200), CGSize(width: 1200, height: 2400)] {
            let data = try XCTUnwrap(sourceImage(size: size).pngData())
            let store = InlineImageStore(loader: { _ in data })
            let inline = await store.load(URL(string: "https://example.test/photo.png")!, decodeGIF: false)
            let thumbnail = try XCTUnwrap(inline?.image?.cgImage)
            XCTAssertEqual(max(thumbnail.width, thumbnail.height), 720)
            let fullSize = await ImageDecodeQueue.shared.decode(data, resolution: .fullSize)
            let fullPixels = try XCTUnwrap(fullSize?.cgImage)
            XCTAssertEqual(fullPixels.width, Int(size.width))
            XCTAssertEqual(fullPixels.height, Int(size.height))
        }
    }

    func testViewerPreservesAllImageOrientationsWithoutReducingPixels() throws {
        let pixels = try XCTUnwrap(sourceImage(size: CGSize(width: 1600, height: 800)).cgImage)
        for orientation in 1...8 {
            let data = NSMutableData()
            let destination = try XCTUnwrap(
                CGImageDestinationCreateWithData(data, UTType.jpeg.identifier as CFString, 1, nil))
            CGImageDestinationAddImage(destination, pixels, [kCGImagePropertyOrientation: orientation] as CFDictionary)
            XCTAssertTrue(CGImageDestinationFinalize(destination))
            let image = try XCTUnwrap(RemoteImageDecoder.decode(data as Data, resolution: .fullSize))
            XCTAssertEqual(image.cgImage?.width, orientation >= 5 ? 800 : 1600)
            XCTAssertEqual(image.cgImage?.height, orientation >= 5 ? 1600 : 800)
            XCTAssertEqual(image.imageOrientation, .up)
        }
    }

    func testViewerGIFKeepsSourceResolutionAndAnimation() throws {
        let data = NSMutableData()
        let destination = try XCTUnwrap(
            CGImageDestinationCreateWithData(data, UTType.gif.identifier as CFString, 2, nil))
        for color in [UIColor.blue, .red] {
            let pixels = try XCTUnwrap(sourceImage(size: CGSize(width: 1200, height: 600), color: color).cgImage)
            CGImageDestinationAddImage(
                destination, pixels,
                [kCGImagePropertyGIFDictionary: [kCGImagePropertyGIFDelayTime: 0.1]] as CFDictionary)
        }
        XCTAssertTrue(CGImageDestinationFinalize(destination))
        let inline = try XCTUnwrap(RemoteImageDecoder.decode(data as Data)?.images)
        let viewer = try XCTUnwrap(RemoteImageDecoder.decode(data as Data, resolution: .fullSize)?.images)
        XCTAssertEqual(inline.count, 2)
        XCTAssertEqual(viewer.count, 2)
        XCTAssertEqual(inline.first?.cgImage?.width, 720)
        XCTAssertEqual(viewer.first?.cgImage?.width, 1200)
        XCTAssertEqual(viewer.first?.cgImage?.height, 600)
    }

    func testSmallImagesAreNotEnlargedAndInvalidImagesFail() throws {
        let data = try XCTUnwrap(sourceImage(size: CGSize(width: 32, height: 16)).pngData())
        XCTAssertEqual(RemoteImageDecoder.decode(data)?.cgImage?.width, 32)
        XCTAssertEqual(RemoteImageDecoder.decode(data, resolution: .fullSize)?.cgImage?.width, 32)
        XCTAssertNil(RemoteImageDecoder.decode(Data(), resolution: .fullSize))
    }

    func testImageVariantsFallBackToTheAvailableURL() {
        let fullSize = URL(string: "https://example.test/full.jpg")!
        let thumbnail = URL(string: "https://example.test/thumb.jpg")!
        let images: [[String: Any]] = [
            ["fullsize": fullSize.absoluteString],
            ["thumb": thumbnail.absoluteString],
            ["fullSize": fullSize.absoluteString, "thumbnail": thumbnail.absoluteString]
        ]
        XCTAssertEqual(images.compactMap(ATProtoBlueskyService.imageURL), [fullSize, thumbnail, fullSize])
        for (index, image) in images.enumerated() {
            let result = ATProtoBlueskyService.imageThumbnailURLs(from: ["images": [image]])
            let source = index == 1 ? thumbnail : fullSize
            XCTAssertEqual(result, index == 0 ? [:] : [source: thumbnail])
        }
    }

    private func sourceImage(size: CGSize, color: UIColor = .blue) -> UIImage {
        let format = UIGraphicsImageRendererFormat()
        format.scale = 1
        return UIGraphicsImageRenderer(size: size, format: format).image { context in
            color.setFill()
            context.fill(CGRect(origin: .zero, size: size))
        }
    }
}