Skip to content

Commit b94f3a5

Browse files
committed
Added Indicator and Transition test case
1 parent b24d465 commit b94f3a5

File tree

4 files changed

+197
-0
lines changed

4 files changed

+197
-0
lines changed

SDWebImageSwiftUI.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848
32B933E623659A1900BB7CAD /* Transition.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32B933E423659A1900BB7CAD /* Transition.swift */; };
4949
32B933E723659A1900BB7CAD /* Transition.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32B933E423659A1900BB7CAD /* Transition.swift */; };
5050
32B933E823659A1900BB7CAD /* Transition.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32B933E423659A1900BB7CAD /* Transition.swift */; };
51+
32BD9C4723E03B08008D5F6A /* IndicatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32BD9C4623E03B08008D5F6A /* IndicatorTests.swift */; };
52+
32BD9C4823E03B08008D5F6A /* IndicatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32BD9C4623E03B08008D5F6A /* IndicatorTests.swift */; };
53+
32BD9C4923E03B08008D5F6A /* IndicatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32BD9C4623E03B08008D5F6A /* IndicatorTests.swift */; };
5154
32C43DE622FD54CD00BE87F5 /* SDWebImageSwiftUI.h in Headers */ = {isa = PBXBuildFile; fileRef = 32C43DE422FD54CD00BE87F5 /* SDWebImageSwiftUI.h */; settings = {ATTRIBUTES = (Public, ); }; };
5255
32C43DEA22FD577300BE87F5 /* SDWebImage.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 32C43DE922FD577300BE87F5 /* SDWebImage.framework */; };
5356
32C43DEB22FD577300BE87F5 /* SDWebImage.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 32C43DE922FD577300BE87F5 /* SDWebImage.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
@@ -162,6 +165,7 @@
162165
326B848B236335400011BDFB /* ProgressIndicator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ProgressIndicator.swift; sourceTree = "<group>"; };
163166
326E480923431C0F00C633E9 /* ImageViewWrapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageViewWrapper.swift; sourceTree = "<group>"; };
164167
32B933E423659A1900BB7CAD /* Transition.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Transition.swift; sourceTree = "<group>"; };
168+
32BD9C4623E03B08008D5F6A /* IndicatorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IndicatorTests.swift; sourceTree = "<group>"; };
165169
32C43DCC22FD540D00BE87F5 /* SDWebImageSwiftUI.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SDWebImageSwiftUI.framework; sourceTree = BUILT_PRODUCTS_DIR; };
166170
32C43DDC22FD54C600BE87F5 /* ImageManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageManager.swift; sourceTree = "<group>"; };
167171
32C43DDE22FD54C600BE87F5 /* WebImage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WebImage.swift; sourceTree = "<group>"; };
@@ -254,6 +258,7 @@
254258
3211F84823DE984D00FC757F /* Info.plist */,
255259
3211F84623DE984D00FC757F /* AnimatedImageTests.swift */,
256260
3211F84F23DE98E300FC757F /* WebImageTests.swift */,
261+
32BD9C4623E03B08008D5F6A /* IndicatorTests.swift */,
257262
);
258263
path = Tests;
259264
sourceTree = "<group>";
@@ -704,6 +709,7 @@
704709
buildActionMask = 2147483647;
705710
files = (
706711
3211F85023DE98E300FC757F /* WebImageTests.swift in Sources */,
712+
32BD9C4723E03B08008D5F6A /* IndicatorTests.swift in Sources */,
707713
3211F84723DE984D00FC757F /* AnimatedImageTests.swift in Sources */,
708714
);
709715
runOnlyForDeploymentPostprocessing = 0;
@@ -713,6 +719,7 @@
713719
buildActionMask = 2147483647;
714720
files = (
715721
321C1D6B23DEDB98009CF62A /* WebImageTests.swift in Sources */,
722+
32BD9C4823E03B08008D5F6A /* IndicatorTests.swift in Sources */,
716723
321C1D6A23DEDB98009CF62A /* AnimatedImageTests.swift in Sources */,
717724
);
718725
runOnlyForDeploymentPostprocessing = 0;
@@ -722,6 +729,7 @@
722729
buildActionMask = 2147483647;
723730
files = (
724731
321C1D6D23DEDB98009CF62A /* WebImageTests.swift in Sources */,
732+
32BD9C4923E03B08008D5F6A /* IndicatorTests.swift in Sources */,
725733
321C1D6C23DEDB98009CF62A /* AnimatedImageTests.swift in Sources */,
726734
);
727735
runOnlyForDeploymentPostprocessing = 0;

Tests/AnimatedImageTests.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,41 @@ class AnimatedImageTests: XCTestCase {
124124
self.waitForExpectations(timeout: 5, handler: nil)
125125
}
126126

127+
func testAnimatedImageModifier() throws {
128+
let expectation = self.expectation(description: "WebImage modifier")
129+
let imageUrl = URL(string: "https://assets.sbnation.com/assets/2512203/dogflops.gif")
130+
let imageView = AnimatedImage(url: imageUrl, options: [.progressiveLoad], context: [.imageScaleFactor: 1])
131+
let introspectView = imageView
132+
.onSuccess { _, _ in
133+
expectation.fulfill()
134+
}
135+
.onFailure { _ in
136+
XCTFail()
137+
}
138+
.onProgress { _, _ in
139+
140+
}
141+
.placeholder(WebImage.emptyImage)
142+
.indicator(SDWebImageActivityIndicator.medium)
143+
// Image
144+
.resizable()
145+
.renderingMode(.original)
146+
.interpolation(.high)
147+
.antialiased(true)
148+
// Animation
149+
.runLoopMode(.common)
150+
.customLoopCount(1)
151+
.maxBufferSize(0)
152+
.pausable(true)
153+
.purgeable(true)
154+
.playbackRate(1)
155+
.transition(.fade)
156+
.animation(.easeInOut)
157+
_ = try introspectView.inspect(AnimatedImage.self)
158+
ViewHosting.host(view: introspectView)
159+
self.waitForExpectations(timeout: 5, handler: nil)
160+
}
161+
127162
// MARK: Helper
128163
func testBundle() -> Bundle {
129164
Bundle(for: type(of: self))

Tests/IndicatorTests.swift

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import XCTest
2+
import SwiftUI
3+
import ViewInspector
4+
import Introspect
5+
@testable import SDWebImageSwiftUI
6+
7+
extension ActivityIndicator : Inspectable {}
8+
extension ProgressIndicator : Inspectable {}
9+
10+
#if os(iOS) || os(tvOS)
11+
typealias ActivityIndicatorViewType = UIActivityIndicatorView
12+
typealias ProgressIndicatorViewType = UIProgressView
13+
#else
14+
typealias ActivityIndicatorViewType = NSProgressIndicator
15+
typealias ProgressIndicatorViewType = NSProgressIndicator
16+
#endif
17+
18+
extension View {
19+
func introspectActivityIndicator(customize: @escaping (ActivityIndicatorViewType) -> ()) -> some View {
20+
return inject(IntrospectionView(
21+
selector: { introspectionView in
22+
guard let viewHost = Introspect.findViewHost(from: introspectionView) else {
23+
return nil
24+
}
25+
return Introspect.previousSibling(containing: ActivityIndicatorViewType.self, from: viewHost)
26+
},
27+
customize: customize
28+
))
29+
}
30+
31+
func introspectProgressIndicator(customize: @escaping (ProgressIndicatorViewType) -> ()) -> some View {
32+
return inject(IntrospectionView(
33+
selector: { introspectionView in
34+
guard let viewHost = Introspect.findViewHost(from: introspectionView) else {
35+
return nil
36+
}
37+
return Introspect.previousSibling(containing: ProgressIndicatorViewType.self, from: viewHost)
38+
},
39+
customize: customize
40+
))
41+
}
42+
}
43+
44+
class IndicatorTests: XCTestCase {
45+
46+
override func setUp() {
47+
super.setUp()
48+
// Put setup code here. This method is called before the invocation of each test method in the class.
49+
}
50+
51+
override func tearDown() {
52+
// Put teardown code here. This method is called after the invocation of each test method in the class.
53+
super.tearDown()
54+
SDImageCache.shared.clear(with: .all)
55+
}
56+
57+
func testActivityIndicator() throws {
58+
let expectation = self.expectation(description: "Activity indicator")
59+
let binding = Binding<Bool>(wrappedValue: true)
60+
let indicator = ActivityIndicator(binding, style: .medium)
61+
let introspectView = indicator.introspectActivityIndicator { indicatorView in
62+
#if os(iOS) || os(tvOS)
63+
XCTAssertTrue(indicatorView.isAnimating)
64+
#endif
65+
binding.wrappedValue = false
66+
XCTAssertFalse(binding.wrappedValue)
67+
XCTAssertFalse(indicator.isAnimating)
68+
#if os(iOS) || os(tvOS)
69+
indicatorView.stopAnimating()
70+
#else
71+
indicatorView.stopAnimation(nil)
72+
#endif
73+
#if os(iOS) || os(tvOS)
74+
XCTAssertFalse(indicatorView.isAnimating)
75+
#endif
76+
expectation.fulfill()
77+
}
78+
_ = try introspectView.inspect(ActivityIndicator.self)
79+
ViewHosting.host(view: introspectView)
80+
self.waitForExpectations(timeout: 5, handler: nil)
81+
}
82+
83+
func testProgressIndicator() throws {
84+
let expectation = self.expectation(description: "Progress indicator")
85+
let binding = Binding<Bool>(wrappedValue: true)
86+
let progress = Binding<CGFloat>(wrappedValue: 0)
87+
let indicator = ProgressIndicator(binding, progress: progress)
88+
let introspectView = indicator.introspectProgressIndicator { indicatorView in
89+
#if os(iOS) || os(tvOS)
90+
XCTAssertEqual(indicatorView.progress, 0.0)
91+
#else
92+
XCTAssertEqual(indicatorView.doubleValue, 0.0)
93+
#endif
94+
progress.wrappedValue = 1.0
95+
XCTAssertEqual(indicator.progress, 1.0)
96+
#if os(iOS) || os(tvOS)
97+
indicatorView.setProgress(1.0, animated: true)
98+
#else
99+
indicatorView.increment(by: 1.0)
100+
#endif
101+
#if os(iOS) || os(tvOS)
102+
XCTAssertEqual(indicatorView.progress, 1.0)
103+
#else
104+
XCTAssertEqual(indicatorView.doubleValue, 1.0)
105+
#endif
106+
expectation.fulfill()
107+
}
108+
_ = try introspectView.inspect(ProgressIndicator.self)
109+
ViewHosting.host(view: introspectView)
110+
self.waitForExpectations(timeout: 5, handler: nil)
111+
}
112+
113+
}

Tests/WebImageTests.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class WebImageTests: XCTestCase {
1515
override func tearDown() {
1616
// Put teardown code here. This method is called after the invocation of each test method in the class.
1717
super.tearDown()
18+
SDImageCache.shared.clear(with: .all)
1819
}
1920

2021
func testWebImageWithStaticURL() throws {
@@ -66,4 +67,44 @@ class WebImageTests: XCTestCase {
6667
self.waitForExpectations(timeout: 5, handler: nil)
6768
}
6869

70+
func testWebImageModifier() throws {
71+
let expectation = self.expectation(description: "WebImage modifier")
72+
let imageUrl = URL(string: "https://raw.githubusercontent.com/ibireme/YYImage/master/Demo/YYImageDemo/mew_baseline.jpg")
73+
let imageView = WebImage(url: imageUrl, options: [.progressiveLoad], context: [.imageScaleFactor: 1])
74+
let introspectView = imageView
75+
.onSuccess { _, _ in
76+
expectation.fulfill()
77+
}
78+
.onFailure { _ in
79+
XCTFail()
80+
}
81+
.onProgress { _, _ in
82+
83+
}
84+
.placeholder {
85+
Circle()
86+
}
87+
// Image
88+
.resizable()
89+
.renderingMode(.original)
90+
.interpolation(.high)
91+
.antialiased(true)
92+
// Animation
93+
.runLoopMode(.common)
94+
.customLoopCount(1)
95+
.maxBufferSize(0)
96+
.pausable(true)
97+
.purgeable(true)
98+
.playbackRate(1)
99+
// WebImage
100+
.retryOnAppear(true)
101+
.cancelOnDisappear(true)
102+
.indicator(.activity)
103+
.transition(.fade)
104+
.animation(.easeInOut)
105+
_ = try introspectView.inspect(WebImage.self)
106+
ViewHosting.host(view: introspectView)
107+
self.waitForExpectations(timeout: 5, handler: nil)
108+
}
109+
69110
}

0 commit comments

Comments
 (0)