forked from onevcat/Kingfisher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageProcessorTests.swift
217 lines (178 loc) · 9.34 KB
/
ImageProcessorTests.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
//
// ImageProcessorTests.swift
// Kingfisher
//
// Created by Wei Wang on 2016/08/30.
//
// Copyright (c) 2017 Wei Wang <[email protected]>
//
// 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.
import XCTest
import Kingfisher
class ImageProcessorTests: XCTestCase {
let imageNames = ["kingfisher.jpg", "onevcat.jpg", "unicorn.png"]
var nonPNGIamgeNames: [String] {
return imageNames.filter { !$0.contains(".png") }
}
func imageData(noAlpha: Bool = false) -> [Data] {
return noAlpha ? nonPNGIamgeNames.map { Data(fileName: $0) } : imageNames.map { Data(fileName: $0) }
}
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testRenderEqual() {
let image1 = Image(data: testImageData! as Data)!
let image2 = Image(data: testImagePNGData)!
XCTAssertTrue(image1.renderEqual(to: image2))
}
func testRoundCornerProcessor() {
let p = RoundCornerImageProcessor(cornerRadius: 40)
XCTAssertEqual(p.identifier, "com.onevcat.Kingfisher.RoundCornerImageProcessor(40.0)")
checkProcessor(p, with: "round-corner-40")
}
func testRoundCornerWithResizingProcessor() {
let p = RoundCornerImageProcessor(cornerRadius: 60, targetSize: CGSize(width: 100, height: 100))
XCTAssertEqual(p.identifier, "com.onevcat.Kingfisher.RoundCornerImageProcessor(60.0_(100.0, 100.0))")
checkProcessor(p, with: "round-corner-60-resize-100")
}
func testRoundCornerWithRectCornerProcessor() {
let p1 = RoundCornerImageProcessor(cornerRadius: 40, roundingCorners: [.topLeft, .topRight])
XCTAssertEqual(p1.identifier, "com.onevcat.Kingfisher.RoundCornerImageProcessor(40.0_corner(3))")
checkProcessor(p1, with: "round-corner-40-corner-3")
let p2 = RoundCornerImageProcessor(cornerRadius: 40, roundingCorners: [.bottomLeft, .bottomRight])
XCTAssertEqual(p2.identifier, "com.onevcat.Kingfisher.RoundCornerImageProcessor(40.0_corner(12))")
checkProcessor(p2, with: "round-corner-40-corner-12")
let p3 = RoundCornerImageProcessor(cornerRadius: 40, roundingCorners: .all)
XCTAssertEqual(p3.identifier, "com.onevcat.Kingfisher.RoundCornerImageProcessor(40.0)")
checkProcessor(p3, with: "round-corner-40")
}
func testResizingProcessor() {
let p = ResizingImageProcessor(referenceSize: CGSize(width: 120, height: 120))
XCTAssertEqual(p.identifier, "com.onevcat.Kingfisher.ResizingImageProcessor((120.0, 120.0))")
checkProcessor(p, with: "resize-120")
}
func testResizingProcessorWithContentMode() {
let p1 = ResizingImageProcessor(referenceSize: CGSize(width: 240, height: 60), mode: .aspectFill)
XCTAssertEqual(p1.identifier, "com.onevcat.Kingfisher.ResizingImageProcessor((240.0, 60.0), aspectFill)")
checkProcessor(p1, with: "resize-240-60-aspectFill")
let p2 = ResizingImageProcessor(referenceSize: CGSize(width: 240, height: 60), mode: .aspectFit)
XCTAssertEqual(p2.identifier, "com.onevcat.Kingfisher.ResizingImageProcessor((240.0, 60.0), aspectFit)")
checkProcessor(p2, with: "resize-240-60-aspectFit")
}
func testBlurProcessor() {
let p = BlurImageProcessor(blurRadius: 10)
XCTAssertEqual(p.identifier, "com.onevcat.Kingfisher.BlurImageProcessor(10.0)")
// Alpha convolving would vary due to context. So we do not test blur for PNGs.
// See results in Resource folder.
checkProcessor(p, with: "blur-10", noAlpha: true)
}
func testOverlayProcessor() {
let p1 = OverlayImageProcessor(overlay: .red)
XCTAssertEqual(p1.identifier, "com.onevcat.Kingfisher.OverlayImageProcessor(#ff0000ff_0.5)")
checkProcessor(p1, with: "overlay-red")
let p2 = OverlayImageProcessor(overlay: .red, fraction: 0.7)
XCTAssertEqual(p2.identifier, "com.onevcat.Kingfisher.OverlayImageProcessor(#ff0000ff_0.7)")
checkProcessor(p2, with: "overlay-red-07")
}
func testTintProcessor() {
let color = Color.yellow.withAlphaComponent(0.2)
let p = TintImageProcessor(tint: color)
XCTAssertEqual(p.identifier, "com.onevcat.Kingfisher.TintImageProcessor(#ffff0033)")
checkProcessor(p, with: "tint-yellow-02")
}
func testColorControlProcessor() {
let p = ColorControlsProcessor(brightness: 0, contrast: 1.1, saturation: 1.2, inputEV: 0.7)
XCTAssertEqual(p.identifier, "com.onevcat.Kingfisher.ColorControlsProcessor(0.0_1.1_1.2_0.7)")
checkProcessor(p, with: "color-control-b00-c11-s12-ev07")
}
func testBlackWhiteProcessor() {
let p = BlackWhiteProcessor()
XCTAssertEqual(p.identifier, "com.onevcat.Kingfisher.BlackWhiteProcessor")
checkProcessor(p, with: "b&w")
}
func testCompositionProcessor() {
let p = BlurImageProcessor(blurRadius: 4) >> RoundCornerImageProcessor(cornerRadius: 60)
XCTAssertEqual(p.identifier, "com.onevcat.Kingfisher.BlurImageProcessor(4.0)|>com.onevcat.Kingfisher.RoundCornerImageProcessor(60.0)")
// Alpha convolving would vary due to context. So we do not test blur for PNGs.
// See results in Resource folder.
checkProcessor(p, with: "blur-4-round-corner-60", noAlpha: true)
}
func testCIImageProcessor() {
let p = TestCIImageProcessor(filter: .tint(Color.yellow.withAlphaComponent(0.2)))
checkProcessor(p, with: "tint-yellow-02")
}
func testCroppingImageProcessor() {
let p = CroppingImageProcessor(size: CGSize(width: 50, height: 50), anchor: CGPoint(x: 0.5, y: 0.5))
XCTAssertEqual(p.identifier, "com.onevcat.Kingfisher.CroppingImageProcessor((50.0, 50.0)_(0.5, 0.5))")
checkProcessor(p, with: "cropping-50-50-anchor-center")
}
}
struct TestCIImageProcessor: CIImageProcessor {
let identifier = "com.onevcat.kingfishertest.tint"
let filter: Filter
}
extension ImageProcessorTests {
func checkProcessor(_ p: ImageProcessor, with suffix: String, noAlpha: Bool = false) {
let specifiedSuffix = getSuffix(with: suffix)
let filteredImageNames = noAlpha ? nonPNGIamgeNames : imageNames
let targetImages = filteredImageNames
.map { $0.replacingOccurrences(of: ".", with: "-\(specifiedSuffix).") }
.flatMap { Image(fileName: $0) }
let resultImages = imageData(noAlpha: noAlpha).flatMap { p.process(item: .data($0), options: []) }
checkImagesEqual(targetImages: targetImages, resultImages: resultImages, for: specifiedSuffix)
}
func checkImagesEqual(targetImages: [Image], resultImages: [Image], for suffix: String) {
XCTAssertEqual(targetImages.count, resultImages.count)
for (i, (resultImage, targetImage)) in zip(resultImages, targetImages).enumerated() {
guard resultImage.renderEqual(to: targetImage) else {
let originalName = imageNames[i]
let excutingName = originalName.replacingOccurrences(of: ".", with: "-\(suffix).")
XCTFail("Result image is not the same to target. Failed at: \(excutingName)) for \(originalName)")
let t = targetImage.write("target-\(excutingName)")
let r = resultImage.write("result-\(excutingName)")
print("Expected: \(t)")
print("But Got: \(r)")
continue
}
}
}
func getSuffix(with ori: String) -> String {
#if os(macOS)
return "\(ori)-mac"
#else
return ori
#endif
}
}
extension ImageProcessorTests {
//Helper Writer
func _testWrite() {
let p = BlurImageProcessor(blurRadius: 4) >> RoundCornerImageProcessor(cornerRadius: 60)
let suffix = "blur-4-round-corner-60-mac"
let resultImages = imageData().flatMap { p.process(item: .data($0), options: []) }
for i in 0..<resultImages.count {
resultImages[i].write(imageNames[i].replacingOccurrences(of: ".", with: "-\(suffix)."))
}
}
}