Skip to content

Commit dc43928

Browse files
committed
Add tests for fallback image size
1 parent e4c4993 commit dc43928

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

WooCommerce/WooCommerceTests/Mocks/MockKingfisherImageDownloader.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ final class MockKingfisherImageDownloader: Kingfisher.ImageDownloader {
55
// Mocks in-memory cache.
66
private let imagesByKey: [String: UIImage]
77

8+
private(set) var capturedProcessor: ImageProcessor?
9+
810
init(imagesByKey: [String: UIImage]) {
911
self.imagesByKey = imagesByKey
1012
super.init(name: "Mock!")
@@ -14,6 +16,14 @@ final class MockKingfisherImageDownloader: Kingfisher.ImageDownloader {
1416
options: KingfisherOptionsInfo? = nil,
1517
progressBlock: DownloadProgressBlock?,
1618
completionHandler: ((Result<ImageLoadingResult, KingfisherError>) -> Void)? = nil) -> DownloadTask? {
19+
if let options = options {
20+
for option in options {
21+
if case .processor(let processor) = option {
22+
capturedProcessor = processor
23+
break
24+
}
25+
}
26+
}
1727
if let image = imagesByKey[url.absoluteString] {
1828
completionHandler?(.success(.init(image: image, url: url, originalData: Data())))
1929
} else {
@@ -25,6 +35,7 @@ final class MockKingfisherImageDownloader: Kingfisher.ImageDownloader {
2535
override func downloadImage(with url: URL,
2636
options: KingfisherParsedOptionsInfo,
2737
completionHandler: ((Result<ImageLoadingResult, KingfisherError>) -> Void)? = nil) -> DownloadTask? {
38+
capturedProcessor = options.processor
2839
if let image = imagesByKey[url.absoluteString] {
2940
completionHandler?(.success(.init(image: image, url: url, originalData: Data())))
3041
} else {

WooCommerce/WooCommerceTests/Tools/DefaultImageServiceTests.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,4 +265,33 @@ final class DefaultImageServiceTests: XCTestCase {
265265

266266
waitForExpectations(timeout: Constants.expectationTimeout, handler: nil)
267267
}
268+
269+
func testDownloadAndCacheImageForImageView_withEmptyBounds_usesDefaultThumbnailSize() {
270+
// Given
271+
let featureFlagService = MockFeatureFlagService()
272+
featureFlagService.isProductImageOptimizedHandlingEnabled = true
273+
ServiceLocator.setFeatureFlagService(featureFlagService)
274+
275+
let mockImageView = UIImageView(frame: .zero)
276+
let mockCache = MockImageCache(name: "Testing")
277+
let mockDownloader = MockKingfisherImageDownloader(imagesByKey: [url.absoluteString: testImage])
278+
imageService = DefaultImageService(imageCache: mockCache, imageDownloader: mockDownloader)
279+
280+
// When
281+
imageService.downloadAndCacheImageForImageView(
282+
mockImageView,
283+
with: url.absoluteString,
284+
placeholder: nil,
285+
progressBlock: nil,
286+
completion: nil
287+
)
288+
289+
// Then
290+
guard let downsamplingProcessor = mockDownloader.capturedProcessor as? DownsamplingImageProcessor else {
291+
XCTFail("DownsamplingImageProcessor not found or not the correct type")
292+
return
293+
}
294+
295+
XCTAssertEqual(downsamplingProcessor.size, CGSize(width: 800, height: 800))
296+
}
268297
}

0 commit comments

Comments
 (0)