Skip to content

Commit e3ba3ab

Browse files
committed
Update the Test Case to ensure the thumbnail decoding works
1 parent 50136be commit e3ba3ab

File tree

2 files changed

+55
-15
lines changed

2 files changed

+55
-15
lines changed

SDWebImageWebPCoderTests/Podfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ project '../SDWebImageWebPCoder'
55
workspace '../SDWebImageWebPCoder'
66

77
target 'SDWebImageWebPCoderTests' do
8+
pod 'Expecta'
89
pod 'SDWebImageWebPCoder', :path => '../'
910
end

SDWebImageWebPCoderTests/SDWebImageWebPCoderTests.m

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
@import XCTest;
1111
#import <SDWebImage/SDWebImage.h>
1212
#import <SDWebImageWebPCoder/SDWebImageWebPCoder.h>
13+
#import <Expecta/Expecta.h>
1314
#import <objc/runtime.h>
1415

1516
const int64_t kAsyncTestTimeout = 5;
@@ -193,45 +194,83 @@ - (void)verifyCoder:(id<SDImageCoder>)coder
193194
withLocalImageURL:(NSURL *)imageUrl
194195
supportsEncoding:(BOOL)supportsEncoding
195196
isAnimatedImage:(BOOL)isAnimated {
197+
SDImageFormat encodingFormat = SDImageFormatWebP;
198+
196199
NSData *inputImageData = [NSData dataWithContentsOfURL:imageUrl];
197-
XCTAssertNotNil(inputImageData, @"Input image data should not be nil");
200+
expect(inputImageData).toNot.beNil();
198201
SDImageFormat inputImageFormat = [NSData sd_imageFormatForImageData:inputImageData];
199-
XCTAssert(inputImageFormat != SDImageFormatUndefined, @"Input image format should not be undefined");
202+
expect(inputImageFormat).toNot.equal(SDImageFormatUndefined);
200203

201204
// 1 - check if we can decode - should be true
202-
XCTAssertTrue([coder canDecodeFromData:inputImageData]);
205+
expect([coder canDecodeFromData:inputImageData]).to.beTruthy();
203206

204207
// 2 - decode from NSData to UIImage and check it
205208
UIImage *inputImage = [coder decodedImageWithData:inputImageData options:nil];
206-
XCTAssertNotNil(inputImage, @"The decoded image from input data should not be nil");
209+
expect(inputImage).toNot.beNil();
207210

208211
if (isAnimated) {
209212
// 2a - check images count > 0 (only for animated images)
210-
XCTAssertTrue(inputImage.sd_isAnimated, @"The decoded image should be animated");
213+
expect(inputImage.sd_isAnimated).to.beTruthy();
211214

212215
// 2b - check image size and scale for each frameImage (only for animated images)
213216
#if SD_UIKIT
214217
CGSize imageSize = inputImage.size;
215218
CGFloat imageScale = inputImage.scale;
216219
[inputImage.images enumerateObjectsUsingBlock:^(UIImage * frameImage, NSUInteger idx, BOOL * stop) {
217-
XCTAssertTrue(CGSizeEqualToSize(imageSize, frameImage.size), @"Each frame size should match the image size");
218-
XCTAssertEqual(imageScale, frameImage.scale, @"Each frame scale should match the image scale");
220+
expect(imageSize).to.equal(frameImage.size);
221+
expect(imageScale).to.equal(frameImage.scale);
219222
}];
220223
#endif
221224
}
222225

226+
// 3 - check thumbnail decoding
227+
CGFloat pixelWidth = inputImage.size.width;
228+
CGFloat pixelHeight = inputImage.size.height;
229+
expect(pixelWidth).beGreaterThan(0);
230+
expect(pixelHeight).beGreaterThan(0);
231+
// check thumnail with scratch
232+
CGFloat thumbnailWidth = 50;
233+
CGFloat thumbnailHeight = 50;
234+
UIImage *thumbImage = [coder decodedImageWithData:inputImageData options:@{
235+
SDImageCoderDecodeThumbnailPixelSize : @(CGSizeMake(thumbnailWidth, thumbnailHeight)),
236+
SDImageCoderDecodePreserveAspectRatio : @(NO)
237+
}];
238+
expect(thumbImage).toNot.beNil();
239+
expect(thumbImage.size).equal(CGSizeMake(thumbnailWidth, thumbnailHeight));
240+
// check thumnail with aspect ratio limit
241+
thumbImage = [coder decodedImageWithData:inputImageData options:@{
242+
SDImageCoderDecodeThumbnailPixelSize : @(CGSizeMake(thumbnailWidth, thumbnailHeight)),
243+
SDImageCoderDecodePreserveAspectRatio : @(YES)
244+
}];
245+
expect(thumbImage).toNot.beNil();
246+
CGFloat ratio = pixelWidth / pixelHeight;
247+
CGFloat thumbnailRatio = thumbnailWidth / thumbnailHeight;
248+
CGSize thumbnailPixelSize;
249+
if (ratio > thumbnailRatio) {
250+
thumbnailPixelSize = CGSizeMake(thumbnailWidth, round(thumbnailWidth / ratio));
251+
} else {
252+
thumbnailPixelSize = CGSizeMake(round(thumbnailHeight * ratio), thumbnailHeight);
253+
}
254+
// Image/IO's thumbnail API does not always use round to preserve precision, we check ABS <= 1
255+
expect(ABS(thumbImage.size.width - thumbnailPixelSize.width) <= 1);
256+
expect(ABS(thumbImage.size.height - thumbnailPixelSize.height) <= 1);
257+
258+
223259
if (supportsEncoding) {
224-
// 3 - check if we can encode to the original format
225-
XCTAssertTrue([coder canEncodeToFormat:inputImageFormat], @"Coder should be able to encode");
260+
// 4 - check if we can encode to the original format
261+
if (encodingFormat == SDImageFormatUndefined) {
262+
encodingFormat = inputImageFormat;
263+
}
264+
expect([coder canEncodeToFormat:encodingFormat]).to.beTruthy();
226265

227-
// 4 - encode from UIImage to NSData using the inputImageFormat and check it
228-
NSData *outputImageData = [coder encodedDataWithImage:inputImage format:inputImageFormat options:nil];
229-
XCTAssertNotNil(outputImageData, @"The encoded image data should not be nil");
266+
// 5 - encode from UIImage to NSData using the inputImageFormat and check it
267+
NSData *outputImageData = [coder encodedDataWithImage:inputImage format:encodingFormat options:nil];
268+
expect(outputImageData).toNot.beNil();
230269
UIImage *outputImage = [coder decodedImageWithData:outputImageData options:nil];
231-
XCTAssertTrue(CGSizeEqualToSize(outputImage.size, inputImage.size), @"Output and input image size should match");
232-
XCTAssertEqual(outputImage.scale, inputImage.scale, @"Output and input image scale should match");
270+
expect(outputImage.size).to.equal(inputImage.size);
271+
expect(outputImage.scale).to.equal(inputImage.scale);
233272
#if SD_UIKIT
234-
XCTAssertEqual(outputImage.images.count, inputImage.images.count, @"Output and input image frame count should match");
273+
expect(outputImage.images.count).to.equal(inputImage.images.count);
235274
#endif
236275
}
237276
}

0 commit comments

Comments
 (0)