-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTests.swift
208 lines (171 loc) · 6.32 KB
/
Tests.swift
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#if canImport(AppKit)
import AppKit
#elseif canImport(UIKit)
import UIKit
#endif
import XCTest
import SwiftyImageIO
#if canImport(MobileCoreServices)
import MobileCoreServices
#endif
import Foundation
internal let bundle = Bundle(for: Tests.self)
class Tests: XCTestCase {
func testSourceSupportedIdentifiers() {
let identifiers = ImageSource.supportedUTIs()
XCTAssert(identifiers.contains(UTI(kUTTypePNG)), "")
for identifier in identifiers {
XCTAssertTrue(ImageSource.supportsUTI(identifier))
}
}
func testDestinationSupportedIdentifiers() {
let identifiers = ImageDestination.supportedUTIs()
XCTAssert(identifiers.contains(UTI(kUTTypePNG)), "")
for identifier in identifiers {
XCTAssertTrue(ImageDestination.supportsUTI(identifier))
}
}
func testGifImageSource() {
let imageSource = ImageSource(url: gifImageURL, options: nil)!
XCTAssert(imageSource.imageCount == 120)
XCTAssert(imageSource.status == .statusComplete)
for imageIndex in (0..<imageSource.imageCount) {
XCTAssertNotNil(imageSource.createImage(atIndex: imageIndex, options: nil))
}
XCTAssert(imageSource.UTI! == "com.compuserve.gif")
}
func testPngImageSource() {
var options = ImageSource.Options()
options.typeIdentifierHint = kUTTypePNG
let imageSource = ImageSource(url: pngImageURL, options: options)!
XCTAssert(imageSource.imageCount == 1)
XCTAssert(imageSource.status == .statusComplete)
XCTAssertNotNil(imageSource.createImage())
XCTAssert(imageSource.UTI! == kUTTypePNG)
}
func testImageDestination() {
let data = NSMutableData()
guard let destination = ImageDestination(data: data, UTI: kUTTypePNG, imageCount: 1) else {
XCTFail();
return
}
let cgimage: CGImage = cgImage(at: pngImageURL)
destination.addImage(cgimage)
XCTAssertTrue(destination.finalize())
#if os(OSX)
let out_image = NSImage(data: data as Data)
#else
let out_image = UIImage(data: data as Data)
#endif
XCTAssertNotNil(out_image)
}
func testEquatable() {
let UTIPure = UTI("a")
let UTIPure2 = UTI("a")
let UTIConvertible = "a"
let UTIConvertible2 = "a"
XCTAssert(UTIPure == UTIConvertible)
XCTAssert(UTIConvertible == UTIPure)
XCTAssert(UTIConvertible == UTIConvertible2)
XCTAssert(UTIPure == UTIPure2)
}
func testEXIF() {
let data = NSMutableData()
guard let destination = ImageDestination(data: data, UTI: kUTTypeJPEG, imageCount: 1) else {
XCTFail();
return
}
let cgimage: CGImage = cgImage(at: pngImageURL)
let exposureTime = TimeInterval(10)
var exifProperties = EXIFImageProperties()
exifProperties.exposureTime = exposureTime
var properties = ImageDestination.Properties()
properties.imageProperties = [exifProperties]
destination.addImage(cgimage, properties: properties)
XCTAssertTrue(destination.finalize())
var url = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
url.appendPathComponent("tmp.jpeg")
try! data.write(to: url, options: NSData.WritingOptions.atomic)
let source = ImageSource(url: url, options: nil)
guard let extractedProperties = source?.propertiesForImage() else {
XCTFail("We created image without properties.");
return
}
guard let extractedImageEXIFImageProperties = extractedProperties.get(EXIFImageProperties.self) else {
XCTFail("EXIF not available")
return
}
XCTAssertEqual(extractedImageEXIFImageProperties.exposureTime, exposureTime)
}
func testGPSProperties() {
let source = ImageSource(url: jpgWithExifImageURL, options: nil)
guard var properties = source?.propertiesForImage() else {
XCTFail("We created image without properties.");
return
}
guard let gpsProperties = properties.get(GPSImageProperties.self) else {
XCTFail("GPS Not available")
return
}
XCTAssertEqual(gpsProperties.longitudeRef, GPSImageProperties.LongitudeRef.east)
XCTAssertEqual(gpsProperties.latitudeRef, GPSImageProperties.LatitudeRef.north)
properties.mutate { (gpsProperties: inout GPSImageProperties?) in
gpsProperties?.latitudeRef = .south
}
XCTAssert(properties.get(GPSImageProperties.self)?.latitudeRef == .south)
}
#if canImport(UIKit)
func testMakeGIF() {
let savePath = makeSavePath(fileExt: "gif")
do {
let gifMaker = GIF()
try gifMaker.makeGIF(fromAnimatedImage: sampleAnimatedImage,
writeTo: savePath,
properties: GIF.Properties(loopCount: 1),
frameProperties: GIF.FrameProperties(delayTime: 0.1))
XCTAssert(FileManager.default.fileExists(atPath: savePath))
let gifSource = ImageSource(data: try! Data(contentsOf: URL(fileURLWithPath: savePath)), options: nil)
XCTAssert(gifSource!.UTI! == kUTTypeGIF)
}
catch {
print(error)
}
}
#endif
}
private extension Tests {
func cgImage(at url: URL) -> CGImage {
#if os(OSX)
let image = NSImage(contentsOfFile: pngImageURL.path)!
var imageRect:CGRect = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
return image.cgImage(forProposedRect: &imageRect, context: nil, hints: nil)!
#else
return UIImage(contentsOfFile: pngImageURL.path)!.cgImage!
#endif
}
var pngImageURL: URL {
return bundle.url(forResource: "sample", withExtension: "png")!
}
var gifImageURL: URL {
return bundle.url(forResource: "gifSample", withExtension: "gif")!
}
var jpgImageURL: URL {
return bundle.url(forResource: "cameraSample", withExtension: "jpg")!
}
var jpgWithExifImageURL: URL {
return bundle.url(forResource: "image_with_gps_data", withExtension: "jpg")!
}
#if canImport(UIKit)
var sampleAnimatedImage: UIImage {
var images = Array<UIImage>()
for i in 0...10 {
images.append(UIImage(named: "giphy\(i)", in: Bundle(for: Tests.self), compatibleWith: nil)!)
}
return UIImage.animatedImage(with: images, duration: 1)!
}
#endif
func makeSavePath(fileExt: String) -> String {
return (FileManager.default.urls(for: .cachesDirectory,
in: .userDomainMask).first!.path as NSString).appendingPathComponent("\(UUID().uuidString).\(fileExt)")
}
}