-
Notifications
You must be signed in to change notification settings - Fork 524
/
UIImageTests.swift
390 lines (309 loc) · 18.6 KB
/
UIImageTests.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
//
// UIImageTests.swift
//
// Copyright (c) 2015 Alamofire Software Foundation (http://alamofire.org/)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#if !os(macOS)
import AlamofireImage
import Foundation
import UIKit
import XCTest
class UIImageTestCase: BaseTestCase {
// MARK: - Properties
var appleImage: UIImage { return image(forResource: "apple", withExtension: "jpg") }
var pirateImage: UIImage { return image(forResource: "pirate", withExtension: "jpg") }
var rainbowImage: UIImage { return image(forResource: "rainbow", withExtension: "jpg") }
var unicornImage: UIImage { return image(forResource: "unicorn", withExtension: "png") }
let scale = Int(UIScreen.main.scale.rounded())
let squareSize = CGSize(width: 50, height: 50)
let horizontalRectangularSize = CGSize(width: 60, height: 30)
let verticalRectangularSize = CGSize(width: 30, height: 60)
// MARK: - Initialization Tests
func testThatHundredsOfLargeImagesCanBeInitializedAcrossMultipleThreads() {
// Given
let url = self.url(forResource: "huge_map", withExtension: "jpg")
let data = try! Data(contentsOf: url)
let lock = NSLock()
var images: [UIImage?] = []
let totalIterations = 200
// When
for _ in 0..<totalIterations {
let expectation = self.expectation(description: "image should be created successfully")
DispatchQueue.global(qos: .utility).async {
let image = UIImage(data: data)
let imageWithScale = UIImage(data: data, scale: CGFloat(self.scale))
lock.lock()
images.append(image)
images.append(imageWithScale)
lock.unlock()
expectation.fulfill()
}
}
waitForExpectations(timeout: timeout, handler: nil)
// Then
images.forEach { XCTAssertNotNil($0, "image should not be nil") }
}
func testThatHundredsOfLargeImagesCanBeInitializedAcrossMultipleThreadsWithThreadSafeInitializers() {
// Given
let url = self.url(forResource: "huge_map", withExtension: "jpg")
let data = try! Data(contentsOf: url)
let lock = NSLock()
var images: [UIImage?] = []
let totalIterations = 200
// When
for _ in 0..<totalIterations {
let expectation = self.expectation(description: "image should be created successfully")
DispatchQueue.global(qos: .utility).async {
let image = UIImage.af.threadSafeImage(with: data)
let imageWithScale = UIImage.af.threadSafeImage(with: data, scale: CGFloat(self.scale))
lock.lock()
images.append(image)
images.append(imageWithScale)
lock.unlock()
expectation.fulfill()
}
}
waitForExpectations(timeout: timeout, handler: nil)
// Then
images.forEach { XCTAssertNotNil($0, "image should not be nil") }
}
// MARK: - Inflation Tests
func testThatImageCanBeInflated() {
// Given
let rainbowImage = image(forResource: "rainbow", withExtension: "jpg")
let unicornImage = image(forResource: "unicorn", withExtension: "png")
// When, Then
rainbowImage.af.inflate()
unicornImage.af.inflate()
}
func testThatImageThatHasAlreadyBeenInflatedIsNotInflatedAgain() {
// Given
let unicornImage = image(forResource: "unicorn", withExtension: "png")
unicornImage.af.inflate()
// When, Then
unicornImage.af.inflate()
}
// MARK: - Alpha Tests
func testThatImageAlphaComponentPropertiesReturnExpectedValues() {
// Given, When, Then
XCTAssertTrue(appleImage.af.isOpaque)
XCTAssertTrue(pirateImage.af.isOpaque)
XCTAssertTrue(rainbowImage.af.isOpaque)
XCTAssertFalse(unicornImage.af.isOpaque)
XCTAssertFalse(appleImage.af.containsAlphaComponent)
XCTAssertFalse(pirateImage.af.containsAlphaComponent)
XCTAssertFalse(rainbowImage.af.containsAlphaComponent)
XCTAssertTrue(unicornImage.af.containsAlphaComponent)
}
// MARK: - Scaling Tests
#if !os(tvOS)
func testThatImageIsScaledToSquareSize() {
executeImageScaledToSizeTest(squareSize)
}
func testThatImageIsScaledToHorizontalRectangularSize() {
executeImageScaledToSizeTest(horizontalRectangularSize)
}
func testThatImageIsScaledToVerticalRectangularSize() {
executeImageScaledToSizeTest(verticalRectangularSize)
}
#endif
private func executeImageScaledToSizeTest(_ size: CGSize) {
// Given
let w = Int(size.width.rounded())
let h = Int(size.height.rounded())
// When
let scaledAppleImage = appleImage.af.imageScaled(to: size)
let scaledPirateImage = pirateImage.af.imageScaled(to: size)
let scaledRainbowImage = rainbowImage.af.imageScaled(to: size)
let scaledUnicornImage = unicornImage.af.imageScaled(to: size)
// Then
let expectedAppleImage = image(forResource: "apple-scaled-\(w)x\(h)-@\(scale)x", withExtension: "png")
let expectedPirateImage = image(forResource: "pirate-scaled-\(w)x\(h)-@\(scale)x", withExtension: "png")
let expectedRainbowImage = image(forResource: "rainbow-scaled-\(w)x\(h)-@\(scale)x", withExtension: "png")
let expectedUnicornImage = image(forResource: "unicorn-scaled-\(w)x\(h)-@\(scale)x", withExtension: "png")
XCTAssertTrue(scaledAppleImage.af.isEqualToImage(expectedAppleImage), "scaled apple image pixels do not match")
XCTAssertTrue(scaledPirateImage.af.isEqualToImage(expectedPirateImage), "scaled pirate image pixels do not match")
XCTAssertTrue(scaledRainbowImage.af.isEqualToImage(expectedRainbowImage), "scaled rainbow image pixels do not match")
XCTAssertTrue(scaledUnicornImage.af.isEqualToImage(expectedUnicornImage), "scaled unicorn image pixels do not match")
XCTAssertEqual(scaledAppleImage.scale, CGFloat(scale), "image scale should be equal to screen scale")
XCTAssertEqual(scaledPirateImage.scale, CGFloat(scale), "image scale should be equal to screen scale")
XCTAssertEqual(scaledRainbowImage.scale, CGFloat(scale), "image scale should be equal to screen scale")
XCTAssertEqual(scaledUnicornImage.scale, CGFloat(scale), "image scale should be equal to screen scale")
}
func testThatImageIsAspectScaledToFitSquareSize() {
executeImageAspectScaledToFitSizeTest(squareSize)
}
func testThatImageIsAspectScaledToFitHorizontalRectangularSize() {
executeImageAspectScaledToFitSizeTest(horizontalRectangularSize)
}
func testThatImageIsAspectScaledToFitVerticalRectangularSize() {
executeImageAspectScaledToFitSizeTest(verticalRectangularSize)
}
private func executeImageAspectScaledToFitSizeTest(_ size: CGSize) {
// Given
let w = Int(size.width.rounded())
let h = Int(size.height.rounded())
// When
let scaledAppleImage = appleImage.af.imageAspectScaled(toFit: size)
let scaledPirateImage = pirateImage.af.imageAspectScaled(toFit: size)
let scaledRainbowImage = rainbowImage.af.imageAspectScaled(toFit: size)
let scaledUnicornImage = unicornImage.af.imageAspectScaled(toFit: size)
// Then
let expectedAppleImage = image(forResource: "apple-aspect.scaled.to.fit-\(w)x\(h)-@\(scale)x", withExtension: "png")
let expectedPirateImage = image(forResource: "pirate-aspect.scaled.to.fit-\(w)x\(h)-@\(scale)x", withExtension: "png")
let expectedRainbowImage = image(forResource: "rainbow-aspect.scaled.to.fit-\(w)x\(h)-@\(scale)x", withExtension: "png")
let expectedUnicornImage = image(forResource: "unicorn-aspect.scaled.to.fit-\(w)x\(h)-@\(scale)x", withExtension: "png")
XCTAssertTrue(scaledAppleImage.af.isEqualToImage(expectedAppleImage, withinTolerance: 53), "scaled apple image pixels do not match")
XCTAssertTrue(scaledPirateImage.af.isEqualToImage(expectedPirateImage), "scaled pirate image pixels do not match")
XCTAssertTrue(scaledRainbowImage.af.isEqualToImage(expectedRainbowImage, withinTolerance: 46), "scaled rainbow image pixels do not match")
XCTAssertTrue(scaledUnicornImage.af.isEqualToImage(expectedUnicornImage, withinTolerance: 26), "scaled unicorn image pixels do not match")
XCTAssertEqual(scaledAppleImage.scale, CGFloat(scale), "image scale should be equal to screen scale")
XCTAssertEqual(scaledPirateImage.scale, CGFloat(scale), "image scale should be equal to screen scale")
XCTAssertEqual(scaledRainbowImage.scale, CGFloat(scale), "image scale should be equal to screen scale")
XCTAssertEqual(scaledUnicornImage.scale, CGFloat(scale), "image scale should be equal to screen scale")
}
func testThatImageIsAspectScaledToFillSquareSize() {
executeImageAspectScaledToFillSizeTest(squareSize)
}
func testThatImageIsAspectScaledToFillHorizontalRectangularSize() {
executeImageAspectScaledToFillSizeTest(horizontalRectangularSize)
}
func testThatImageIsAspectScaledToFillVerticalRectangularSize() {
executeImageAspectScaledToFillSizeTest(verticalRectangularSize)
}
private func executeImageAspectScaledToFillSizeTest(_ size: CGSize) {
// Given
let w = Int(size.width.rounded())
let h = Int(size.height.rounded())
// When
let scaledAppleImage = appleImage.af.imageAspectScaled(toFill: size)
let scaledPirateImage = pirateImage.af.imageAspectScaled(toFill: size)
let scaledRainbowImage = rainbowImage.af.imageAspectScaled(toFill: size)
let scaledUnicornImage = unicornImage.af.imageAspectScaled(toFill: size)
// Then
let expectedAppleImage = image(forResource: "apple-aspect.scaled.to.fill-\(w)x\(h)-@\(scale)x", withExtension: "png")
let expectedPirateImage = image(forResource: "pirate-aspect.scaled.to.fill-\(w)x\(h)-@\(scale)x", withExtension: "png")
let expectedRainbowImage = image(forResource: "rainbow-aspect.scaled.to.fill-\(w)x\(h)-@\(scale)x", withExtension: "png")
let expectedUnicornImage = image(forResource: "unicorn-aspect.scaled.to.fill-\(w)x\(h)-@\(scale)x", withExtension: "png")
XCTAssertTrue(scaledAppleImage.af.isEqualToImage(expectedAppleImage), "scaled apple image pixels do not match")
XCTAssertTrue(scaledPirateImage.af.isEqualToImage(expectedPirateImage), "scaled pirate image pixels do not match")
XCTAssertTrue(scaledRainbowImage.af.isEqualToImage(expectedRainbowImage), "scaled rainbow image pixels do not match")
XCTAssertTrue(scaledUnicornImage.af.isEqualToImage(expectedUnicornImage), "scaled unicorn image pixels do not match")
XCTAssertEqual(scaledAppleImage.scale, CGFloat(scale), "image scale should be equal to screen scale")
XCTAssertEqual(scaledPirateImage.scale, CGFloat(scale), "image scale should be equal to screen scale")
XCTAssertEqual(scaledRainbowImage.scale, CGFloat(scale), "image scale should be equal to screen scale")
XCTAssertEqual(scaledUnicornImage.scale, CGFloat(scale), "image scale should be equal to screen scale")
}
// MARK: - Rounded Corners
// TODO: Needs updates for latest rendering results.
func _testThatImageCornersAreRoundedToRadius() {
// Given
let radius: CGFloat = 20
let r = Int(radius.rounded())
// When
let roundedAppleImage = appleImage.af.imageRounded(withCornerRadius: radius, divideRadiusByImageScale: true)
let roundedPirateImage = pirateImage.af.imageRounded(withCornerRadius: radius, divideRadiusByImageScale: true)
let roundedRainbowImage = rainbowImage.af.imageRounded(withCornerRadius: radius, divideRadiusByImageScale: true)
let roundedUnicornImage = unicornImage.af.imageRounded(withCornerRadius: radius, divideRadiusByImageScale: true)
// Then
let expectedAppleImage = image(forResource: "apple-radius-\(r)", withExtension: "png")
let expectedPirateImage = image(forResource: "pirate-radius-\(r)", withExtension: "png")
let expectedRainbowImage = image(forResource: "rainbow-radius-\(r)", withExtension: "png")
let expectedUnicornImage = image(forResource: "unicorn-radius-\(r)", withExtension: "png")
XCTAssertTrue(roundedAppleImage.af.isEqualToImage(expectedAppleImage), "rounded apple image pixels do not match")
XCTAssertTrue(roundedPirateImage.af.isEqualToImage(expectedPirateImage), "rounded pirate image pixels do not match")
XCTAssertTrue(roundedRainbowImage.af.isEqualToImage(expectedRainbowImage), "rounded rainbow image pixels do not match")
XCTAssertTrue(roundedUnicornImage.af.isEqualToImage(expectedUnicornImage), "rounded unicorn image pixels do not match")
XCTAssertEqual(roundedAppleImage.scale, CGFloat(scale), "image scale should be equal to screen scale")
XCTAssertEqual(roundedPirateImage.scale, CGFloat(scale), "image scale should be equal to screen scale")
XCTAssertEqual(roundedRainbowImage.scale, CGFloat(scale), "image scale should be equal to screen scale")
XCTAssertEqual(roundedUnicornImage.scale, CGFloat(scale), "image scale should be equal to screen scale")
}
func testThatImageIsRoundedIntoCircle() {
// Given, When
let circularAppleImage = appleImage.af.imageRoundedIntoCircle()
let circularPirateImage = pirateImage.af.imageRoundedIntoCircle()
let circularRainbowImage = rainbowImage.af.imageRoundedIntoCircle()
let circularUnicornImage = unicornImage.af.imageRoundedIntoCircle()
// Then
let expectedAppleImage = image(forResource: "apple-circle", withExtension: "png")
let expectedPirateImage = image(forResource: "pirate-circle", withExtension: "png")
let expectedRainbowImage = image(forResource: "rainbow-circle", withExtension: "png")
let expectedUnicornImage = image(forResource: "unicorn-circle", withExtension: "png")
XCTAssertTrue(circularAppleImage.af.isEqualToImage(expectedAppleImage), "rounded apple image pixels do not match")
XCTAssertTrue(circularPirateImage.af.isEqualToImage(expectedPirateImage), "rounded pirate image pixels do not match")
XCTAssertTrue(circularRainbowImage.af.isEqualToImage(expectedRainbowImage), "rounded rainbow image pixels do not match")
XCTAssertTrue(circularUnicornImage.af.isEqualToImage(expectedUnicornImage), "rounded unicorn image pixels do not match")
XCTAssertEqual(circularAppleImage.scale, CGFloat(scale), "image scale should be equal to screen scale")
XCTAssertEqual(circularPirateImage.scale, CGFloat(scale), "image scale should be equal to screen scale")
XCTAssertEqual(circularRainbowImage.scale, CGFloat(scale), "image scale should be equal to screen scale")
XCTAssertEqual(circularUnicornImage.scale, CGFloat(scale), "image scale should be equal to screen scale")
let expectedAppleSize = expectedImageSizeForCircularImage(circularAppleImage)
let expectedPirateSize = expectedImageSizeForCircularImage(circularPirateImage)
let expectedRainbowSize = expectedImageSizeForCircularImage(circularRainbowImage)
let expectedUnicornSize = expectedImageSizeForCircularImage(circularUnicornImage)
XCTAssertEqual(circularAppleImage.size, expectedAppleSize, "image scale should be equal to screen scale")
XCTAssertEqual(circularPirateImage.size, expectedPirateSize, "image scale should be equal to screen scale")
XCTAssertEqual(circularRainbowImage.size, expectedRainbowSize, "image scale should be equal to screen scale")
XCTAssertEqual(circularUnicornImage.size, expectedUnicornSize, "image scale should be equal to screen scale")
}
private func expectedImageSizeForCircularImage(_ image: UIImage) -> CGSize {
let dimension = min(image.size.width, image.size.height)
return CGSize(width: dimension, height: dimension)
}
// MARK: - Core Image Filters
func testThatImageWithAppliedGaussianBlurFilterReturnsBlurredImage() {
// Given
let parameters: [String: Any] = ["inputRadius": 8]
// When
let blurredImage = unicornImage.af.imageFiltered(withCoreImageFilter: "CIGaussianBlur", parameters: parameters)
// Then
if let blurredImage = blurredImage {
var expectedResource = "unicorn-blurred-8"
if #available(iOS 13.0, macOS 10.15, tvOS 13.0, *) { expectedResource.append("-ios-13") }
let expectedBlurredImage = image(forResource: expectedResource, withExtension: "png")
let pixelsMatch = blurredImage.af.isEqualToImage(expectedBlurredImage)
XCTAssertTrue(pixelsMatch, "pixels match should be true")
} else {
XCTFail("blurred image should not be nil")
}
}
func testThatImageWithAppliedSepiaToneFilterReturnsSepiaImage() {
// Given, When
let sepiaImage = unicornImage.af.imageFiltered(withCoreImageFilter: "CISepiaTone")
// Then
if let sepiaImage = sepiaImage {
let expectedSepiaImage = image(forResource: "unicorn-sepia.tone", withExtension: "png")
XCTAssertTrue(sepiaImage.af.isEqualToImage(expectedSepiaImage), "sepia image pixels do not match")
} else {
XCTFail("sepia image should not be nil")
}
}
func testThatInvalidCoreImageFilterReturnsNil() {
// Given
let filterName = "SomeFilterThatDoesNotExist"
// When
let filteredImage = unicornImage.af.imageFiltered(withCoreImageFilter: filterName)
// Then
XCTAssertNil(filteredImage, "filtered image should be nil")
}
}
#endif