forked from SDWebImage/SDWebImageSwiftUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebImageTests.swift
181 lines (170 loc) · 7.55 KB
/
WebImageTests.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
import XCTest
import SwiftUI
import ViewInspector
@testable import SDWebImageSwiftUI
extension WebImage : Inspectable {}
class WebImageTests: XCTestCase {
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 testWebImageWithStaticURL() throws {
let expectation = self.expectation(description: "WebImage static url initializer")
let imageUrl = URL(string: "https://nr-platform.s3.amazonaws.com/uploads/platform/published_extension/branding_icon/275/AmazonS3.png")
let imageView = WebImage(url: imageUrl)
let introspectView = imageView.onSuccess { image, data, cacheType in
#if os(macOS)
let displayImage = try? imageView.inspect().zStack().image(1).actualImage().nsImage()
#else
let displayImage = try? imageView.inspect().zStack().image(1).actualImage().cgImage()
#endif
XCTAssertNotNil(displayImage)
expectation.fulfill()
}.onFailure { error in
XCTFail(error.localizedDescription)
}
_ = try introspectView.inspect()
ViewHosting.host(view: introspectView)
self.waitForExpectations(timeout: 5, handler: nil)
ViewHosting.expel()
}
func testWebImageWithAnimatedURL() throws {
let expectation = self.expectation(description: "WebImage animated url initializer")
let imageUrl = URL(string: "https://apng.onevcat.com/assets/elephant.png")
let binding = Binding<Bool>(wrappedValue: true)
let imageView = WebImage(url: imageUrl, isAnimating: binding)
let introspectView = imageView.onSuccess { image, data, cacheType in
if let animatedImage = image as? SDAnimatedImage {
XCTAssertTrue(imageView.isAnimating)
#if os(macOS)
let displayImage = try? imageView.inspect().zStack().image(1).actualImage().nsImage()
let size = displayImage?.size
#else
let displayImage = try? imageView.inspect().zStack().image(1).actualImage().cgImage()
let size = CGSize(width: displayImage?.width ?? 0, height: displayImage?.height ?? 0)
#endif
XCTAssertNotNil(displayImage)
// Check display image should match the animated poster frame
let posterImage = animatedImage.animatedImageFrame(at: 0)
XCTAssertEqual(size, posterImage?.size)
expectation.fulfill()
} else {
XCTFail("WebImage animated image invalid")
}
}.onFailure { error in
XCTFail(error.localizedDescription)
}
_ = try introspectView.inspect()
ViewHosting.host(view: introspectView)
self.waitForExpectations(timeout: 5, handler: nil)
ViewHosting.expel()
}
func testWebImageModifier() throws {
let expectation = self.expectation(description: "WebImage modifier")
let imageUrl = URL(string: "https://raw.githubusercontent.com/ibireme/YYImage/master/Demo/YYImageDemo/mew_baseline.jpg")
let imageView = WebImage(url: imageUrl, options: [.progressiveLoad], context: [.imageScaleFactor: 1])
let introspectView = imageView
.onSuccess { _, _, _ in
expectation.fulfill()
}
.onFailure { _ in
XCTFail()
}
.onProgress { _, _ in
}
.placeholder(.init(platformImage: PlatformImage()))
.placeholder {
Circle()
}
// Image
.resizable()
.renderingMode(.original)
.interpolation(.high)
.antialiased(true)
// Animation
.runLoopMode(.common)
.customLoopCount(1)
.maxBufferSize(0)
.pausable(true)
.purgeable(true)
.playbackRate(1)
// WebImage
.retryOnAppear(true)
.cancelOnDisappear(true)
.indicator(.activity)
.transition(.fade)
.animation(.easeInOut)
_ = try introspectView.inspect()
ViewHosting.host(view: introspectView)
self.waitForExpectations(timeout: 5, handler: nil)
ViewHosting.expel()
}
func testWebImageOnSuccessWhenMemoryCacheHit() throws {
let expectation = self.expectation(description: "WebImage onSuccess when memory cache hit")
let imageUrl = URL(string: "https://foo.bar/buzz.png")
let cacheKey = SDWebImageManager.shared.cacheKey(for: imageUrl)
#if os(macOS)
let testImage = TestUtils.testImageBundle().image(forResource: "TestImage")
#else
let testImage = UIImage(named: "TestImage", in: TestUtils.testImageBundle(), compatibleWith: nil)
#endif
SDImageCache.shared.storeImage(toMemory: testImage, forKey: cacheKey)
let imageView = WebImage(url: imageUrl)
let introspectView = imageView.onSuccess { image, data, cacheType in
XCTAssert(cacheType == .memory)
XCTAssertNotNil(image)
XCTAssertEqual(image, testImage)
expectation.fulfill()
}
_ = try introspectView.inspect()
ViewHosting.host(view: introspectView)
self.waitForExpectations(timeout: 5, handler: nil)
ViewHosting.expel()
}
func testWebImageOnSuccessWhenCacheMiss() throws {
let expectation = self.expectation(description: "WebImage onSuccess when cache miss")
let imageUrl = URL(string: "http://via.placeholder.com/100x100.png")
let cacheKey = SDWebImageManager.shared.cacheKey(for: imageUrl)
SDImageCache.shared.removeImageFromMemory(forKey: cacheKey)
SDImageCache.shared.removeImageFromDisk(forKey: cacheKey)
let imageView = WebImage(url: imageUrl)
let introspectView = imageView.onSuccess { image, data, cacheType in
XCTAssert(cacheType == .none)
XCTAssertNotNil(image)
XCTAssertNotNil(data)
expectation.fulfill()
}
_ = try introspectView.inspect()
ViewHosting.host(view: introspectView)
self.waitForExpectations(timeout: 5, handler: nil)
ViewHosting.expel()
}
func testWebImageEXIFImage() throws {
let expectation = self.expectation(description: "WebImage EXIF image url")
// EXIF 5, Left Mirrored
let imageUrl = URL(string: "https://raw.githubusercontent.com/recurser/exif-orientation-examples/master/Landscape_5.jpg")
let imageView = WebImage(url: imageUrl)
let introspectView = imageView.onSuccess { image, data, cacheType in
#if os(macOS)
let displayImage = try? imageView.inspect().zStack().image(1).actualImage().nsImage()
XCTAssertNotNil(displayImage)
#else
let displayImage = try? imageView.inspect().zStack().image(1).actualImage().cgImage()
let orientation = try? imageView.inspect().zStack().image(1).actualImage().orientation()
XCTAssertNotNil(displayImage)
XCTAssertEqual(orientation, .leftMirrored)
#endif
expectation.fulfill()
}.onFailure { error in
XCTFail(error.localizedDescription)
}
_ = try introspectView.inspect()
ViewHosting.host(view: introspectView)
self.waitForExpectations(timeout: 5, handler: nil)
ViewHosting.expel()
}
}