From 3363162b036bd9c8f7f7a55163111c0f6be5ea80 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Sat, 1 Feb 2020 17:53:28 +0800 Subject: [PATCH] Added the test case `testWebImageOnSuccessWhenMemoryCacheHit` --- SDWebImageSwiftUI.xcodeproj/project.pbxproj | 8 +++++++ Tests/AnimatedImageTests.swift | 24 +++------------------ Tests/TestUtils.swift | 18 ++++++++++++++++ Tests/WebImageTests.swift | 19 +++++++++++++++- 4 files changed, 47 insertions(+), 22 deletions(-) create mode 100644 Tests/TestUtils.swift diff --git a/SDWebImageSwiftUI.xcodeproj/project.pbxproj b/SDWebImageSwiftUI.xcodeproj/project.pbxproj index 48e3759..0ec7543 100644 --- a/SDWebImageSwiftUI.xcodeproj/project.pbxproj +++ b/SDWebImageSwiftUI.xcodeproj/project.pbxproj @@ -28,6 +28,9 @@ 321C1D6B23DEDB98009CF62A /* WebImageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3211F84F23DE98E300FC757F /* WebImageTests.swift */; }; 321C1D6C23DEDB98009CF62A /* AnimatedImageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3211F84623DE984D00FC757F /* AnimatedImageTests.swift */; }; 321C1D6D23DEDB98009CF62A /* WebImageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3211F84F23DE98E300FC757F /* WebImageTests.swift */; }; + 322E0F4823E57F09006836DC /* TestUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 322E0F4723E57F09006836DC /* TestUtils.swift */; }; + 322E0F4923E57F09006836DC /* TestUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 322E0F4723E57F09006836DC /* TestUtils.swift */; }; + 322E0F4A23E57F09006836DC /* TestUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 322E0F4723E57F09006836DC /* TestUtils.swift */; }; 326B84822363350C0011BDFB /* Indicator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 326B84812363350C0011BDFB /* Indicator.swift */; }; 326B84832363350C0011BDFB /* Indicator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 326B84812363350C0011BDFB /* Indicator.swift */; }; 326B84842363350C0011BDFB /* Indicator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 326B84812363350C0011BDFB /* Indicator.swift */; }; @@ -160,6 +163,7 @@ 3211F85423DE9D2700FC757F /* Images.bundle */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.plug-in"; path = Images.bundle; sourceTree = ""; }; 321C1D3B23DEC17D009CF62A /* SDWebImageSwiftUITests macOS.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "SDWebImageSwiftUITests macOS.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; 321C1D4A23DEC185009CF62A /* SDWebImageSwiftUITests tvOS.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "SDWebImageSwiftUITests tvOS.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; + 322E0F4723E57F09006836DC /* TestUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestUtils.swift; sourceTree = ""; }; 326B84812363350C0011BDFB /* Indicator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Indicator.swift; sourceTree = ""; }; 326B8486236335110011BDFB /* ActivityIndicator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ActivityIndicator.swift; sourceTree = ""; }; 326B848B236335400011BDFB /* ProgressIndicator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ProgressIndicator.swift; sourceTree = ""; }; @@ -259,6 +263,7 @@ 3211F84623DE984D00FC757F /* AnimatedImageTests.swift */, 3211F84F23DE98E300FC757F /* WebImageTests.swift */, 32BD9C4623E03B08008D5F6A /* IndicatorTests.swift */, + 322E0F4723E57F09006836DC /* TestUtils.swift */, ); path = Tests; sourceTree = ""; @@ -711,6 +716,7 @@ 3211F85023DE98E300FC757F /* WebImageTests.swift in Sources */, 32BD9C4723E03B08008D5F6A /* IndicatorTests.swift in Sources */, 3211F84723DE984D00FC757F /* AnimatedImageTests.swift in Sources */, + 322E0F4823E57F09006836DC /* TestUtils.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -721,6 +727,7 @@ 321C1D6B23DEDB98009CF62A /* WebImageTests.swift in Sources */, 32BD9C4823E03B08008D5F6A /* IndicatorTests.swift in Sources */, 321C1D6A23DEDB98009CF62A /* AnimatedImageTests.swift in Sources */, + 322E0F4923E57F09006836DC /* TestUtils.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -731,6 +738,7 @@ 321C1D6D23DEDB98009CF62A /* WebImageTests.swift in Sources */, 32BD9C4923E03B08008D5F6A /* IndicatorTests.swift in Sources */, 321C1D6C23DEDB98009CF62A /* AnimatedImageTests.swift in Sources */, + 322E0F4A23E57F09006836DC /* TestUtils.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Tests/AnimatedImageTests.swift b/Tests/AnimatedImageTests.swift index 1c50388..59ea71a 100644 --- a/Tests/AnimatedImageTests.swift +++ b/Tests/AnimatedImageTests.swift @@ -34,7 +34,7 @@ class AnimatedImageTests: XCTestCase { func testAnimatedImageWithName() throws { let expectation = self.expectation(description: "AnimatedImage name initializer") - let imageView = AnimatedImage(name: "TestImage.gif", bundle: testImageBundle()) + let imageView = AnimatedImage(name: "TestImage.gif", bundle: TestUtils.testImageBundle()) let introspectView = imageView.introspectAnimatedImage { animatedImageView in if let animatedImage = animatedImageView.image as? SDAnimatedImage { XCTAssertEqual(animatedImage.animatedImageLoopCount, 0) @@ -51,7 +51,7 @@ class AnimatedImageTests: XCTestCase { func testAnimatedImageWithData() throws { let expectation = self.expectation(description: "AnimatedImage data initializer") - let imageData = try XCTUnwrap(testImageData(name: "TestImageAnimated.apng")) + let imageData = try XCTUnwrap(TestUtils.testImageData(name: "TestImageAnimated.apng")) let imageView = AnimatedImage(data: imageData) let introspectView = imageView.introspectAnimatedImage { animatedImageView in if let animatedImage = animatedImageView.image as? SDAnimatedImage { @@ -91,7 +91,7 @@ class AnimatedImageTests: XCTestCase { let expectation = self.expectation(description: "AnimatedImage binding control") let binding = Binding(wrappedValue: true) var context: AnimatedImage.Context? - let imageView = AnimatedImage(name: "TestLoopCount.gif", bundle: testImageBundle(), isAnimating: binding) + let imageView = AnimatedImage(name: "TestLoopCount.gif", bundle: TestUtils.testImageBundle(), isAnimating: binding) .onViewCreate { _, c in context = c } @@ -171,22 +171,4 @@ class AnimatedImageTests: XCTestCase { self.waitForExpectations(timeout: 5, handler: nil) AnimatedImage.onViewDestroy() } - - // MARK: Helper - func testBundle() -> Bundle { - Bundle(for: type(of: self)) - } - - func testImageBundle() -> Bundle { - let imagePath = (testBundle().resourcePath! as NSString).appendingPathComponent("Images.bundle") - return Bundle(path: imagePath)! - } - - func testImageData(name: String) -> Data? { - guard let url = testImageBundle().url(forResource: name, withExtension: nil) else { - return nil - } - return try? Data(contentsOf: url) - } - } diff --git a/Tests/TestUtils.swift b/Tests/TestUtils.swift new file mode 100644 index 0000000..c8aa257 --- /dev/null +++ b/Tests/TestUtils.swift @@ -0,0 +1,18 @@ +import XCTest +import SwiftUI + +class TestUtils { + static var testBundle = Bundle(for: TestUtils.self) + + class func testImageBundle() -> Bundle { + let imagePath = (testBundle.resourcePath! as NSString).appendingPathComponent("Images.bundle") + return Bundle(path: imagePath)! + } + + class func testImageData(name: String) -> Data? { + guard let url = testImageBundle().url(forResource: name, withExtension: nil) else { + return nil + } + return try? Data(contentsOf: url) + } +} diff --git a/Tests/WebImageTests.swift b/Tests/WebImageTests.swift index 8f497b6..4eef0ed 100644 --- a/Tests/WebImageTests.swift +++ b/Tests/WebImageTests.swift @@ -15,7 +15,6 @@ class WebImageTests: XCTestCase { override func tearDown() { // Put teardown code here. This method is called after the invocation of each test method in the class. super.tearDown() - SDImageCache.shared.clear(with: .all) } func testWebImageWithStaticURL() throws { @@ -107,4 +106,22 @@ class WebImageTests: XCTestCase { self.waitForExpectations(timeout: 5, handler: nil) } + func testWebImageOnSuccessWhenMemoryCacheHit() throws { + let expectation = self.expectation(description: "WebImage onSuccess when memory hit") + let imageUrl = URL(string: "https://foo.bar/buzz.png") + let cacheKey = SDWebImageManager.shared.cacheKey(for: imageUrl) + let testImage = UIImage(named: "TestImage", in: TestUtils.testImageBundle(), compatibleWith: nil) + SDImageCache.shared.storeImage(toMemory: testImage, forKey: cacheKey) + let imageView = WebImage(url: imageUrl) + let introspectView = imageView.onSuccess { image, cacheType in + XCTAssert(cacheType == .memory) + XCTAssertNotNil(image) + XCTAssertEqual(image, testImage) + expectation.fulfill() + } + _ = try introspectView.inspect(WebImage.self) + ViewHosting.host(view: introspectView) + self.waitForExpectations(timeout: 5, handler: nil) + } + }