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