2020-01-27 14:55:58 +08:00
|
|
|
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, cacheType in
|
2020-01-27 16:54:39 +08:00
|
|
|
#if os(iOS) || os(tvOS)
|
2020-01-27 14:55:58 +08:00
|
|
|
let displayImage = try? imageView.inspect().group().image(0).uiImage()
|
2020-01-27 16:54:39 +08:00
|
|
|
#else
|
|
|
|
let displayImage = try? imageView.inspect().group().image(0).nsImage()
|
|
|
|
#endif
|
2020-01-27 14:55:58 +08:00
|
|
|
XCTAssertNotNil(displayImage)
|
|
|
|
expectation.fulfill()
|
|
|
|
}.onFailure { error in
|
|
|
|
XCTFail(error.localizedDescription)
|
|
|
|
}
|
2020-02-04 11:04:55 +08:00
|
|
|
_ = try introspectView.inspect()
|
2020-01-27 14:55:58 +08:00
|
|
|
ViewHosting.host(view: introspectView)
|
|
|
|
self.waitForExpectations(timeout: 5, handler: nil)
|
2020-02-04 11:04:55 +08:00
|
|
|
ViewHosting.expel()
|
2020-01-27 14:55:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func testWebImageWithAnimatedURL() throws {
|
|
|
|
let expectation = self.expectation(description: "WebImage animated url initializer")
|
2020-01-27 17:34:53 +08:00
|
|
|
let imageUrl = URL(string: "https://apng.onevcat.com/assets/elephant.png")
|
2020-01-27 14:55:58 +08:00
|
|
|
let binding = Binding<Bool>(wrappedValue: true)
|
|
|
|
let imageView = WebImage(url: imageUrl, isAnimating: binding)
|
|
|
|
let introspectView = imageView.onSuccess { image, cacheType in
|
|
|
|
if let animatedImage = image as? SDAnimatedImage {
|
2020-01-27 17:34:53 +08:00
|
|
|
XCTAssertTrue(imageView.isAnimating)
|
|
|
|
#if os(iOS) || os(tvOS)
|
|
|
|
let displayImage = try? imageView.inspect().group().image(0).uiImage()
|
|
|
|
#else
|
|
|
|
let displayImage = try? imageView.inspect().group().image(0).nsImage()
|
|
|
|
#endif
|
|
|
|
XCTAssertNotNil(displayImage)
|
|
|
|
// Check display image should match the animated poster frame
|
|
|
|
let posterImage = animatedImage.animatedImageFrame(at: 0)
|
|
|
|
XCTAssertEqual(displayImage?.size, posterImage?.size)
|
|
|
|
expectation.fulfill()
|
2020-01-27 14:55:58 +08:00
|
|
|
} else {
|
|
|
|
XCTFail("WebImage animated image invalid")
|
|
|
|
}
|
|
|
|
}.onFailure { error in
|
|
|
|
XCTFail(error.localizedDescription)
|
|
|
|
}
|
2020-02-04 11:04:55 +08:00
|
|
|
_ = try introspectView.inspect()
|
2020-01-27 14:55:58 +08:00
|
|
|
ViewHosting.host(view: introspectView)
|
|
|
|
self.waitForExpectations(timeout: 5, handler: nil)
|
2020-02-04 11:04:55 +08:00
|
|
|
ViewHosting.expel()
|
2020-01-27 14:55:58 +08:00
|
|
|
}
|
|
|
|
|
2020-01-28 18:49:41 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
}
|
2020-04-05 14:22:32 +08:00
|
|
|
.placeholder(.init(platformImage: PlatformImage()))
|
2020-01-28 18:49:41 +08:00
|
|
|
.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)
|
2020-02-04 11:04:55 +08:00
|
|
|
_ = try introspectView.inspect()
|
2020-01-28 18:49:41 +08:00
|
|
|
ViewHosting.host(view: introspectView)
|
|
|
|
self.waitForExpectations(timeout: 5, handler: nil)
|
2020-02-04 11:04:55 +08:00
|
|
|
ViewHosting.expel()
|
2020-01-28 18:49:41 +08:00
|
|
|
}
|
|
|
|
|
2020-02-01 17:53:28 +08:00
|
|
|
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)
|
2020-02-01 18:18:52 +08:00
|
|
|
#if os(macOS)
|
|
|
|
let testImage = TestUtils.testImageBundle().image(forResource: "TestImage")
|
|
|
|
#else
|
2020-02-01 17:53:28 +08:00
|
|
|
let testImage = UIImage(named: "TestImage", in: TestUtils.testImageBundle(), compatibleWith: nil)
|
2020-02-01 18:18:52 +08:00
|
|
|
#endif
|
2020-02-01 17:53:28 +08:00
|
|
|
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()
|
|
|
|
}
|
2020-02-04 11:04:55 +08:00
|
|
|
_ = try introspectView.inspect()
|
2020-02-01 17:53:28 +08:00
|
|
|
ViewHosting.host(view: introspectView)
|
|
|
|
self.waitForExpectations(timeout: 5, handler: nil)
|
2020-02-04 11:04:55 +08:00
|
|
|
ViewHosting.expel()
|
2020-02-01 17:53:28 +08:00
|
|
|
}
|
|
|
|
|
2020-04-15 17:31:44 +08:00
|
|
|
func testWebImageEXIFImage() throws {
|
|
|
|
let expectation = self.expectation(description: "WebImage EXIF image url")
|
|
|
|
// EXIF 2, Up Mirrored
|
|
|
|
let imageUrl = URL(string: "https://raw.githubusercontent.com/recurser/exif-orientation-examples/master/Landscape_2.jpg")
|
|
|
|
let imageView = WebImage(url: imageUrl)
|
|
|
|
let introspectView = imageView.onSuccess { image, cacheType in
|
|
|
|
let displayImage = try? imageView.inspect().group().image(0).cgImage()
|
|
|
|
let orientation = try! imageView.inspect().group().image(0).orientation()
|
|
|
|
XCTAssertNotNil(displayImage)
|
|
|
|
XCTAssertEqual(orientation, .upMirrored)
|
|
|
|
expectation.fulfill()
|
|
|
|
}.onFailure { error in
|
|
|
|
XCTFail(error.localizedDescription)
|
|
|
|
}
|
|
|
|
_ = try introspectView.inspect()
|
|
|
|
ViewHosting.host(view: introspectView)
|
|
|
|
self.waitForExpectations(timeout: 5, handler: nil)
|
|
|
|
ViewHosting.expel()
|
|
|
|
}
|
2020-01-27 14:55:58 +08:00
|
|
|
}
|