Refactored the `SDWebImageDecoderTests` a bit. Per DRY, created `verifyCoder:withLocalImageURL:isAnimatedImage:` method that will take a imageURL and a coder and will check decoding and encoding back and forth, sizes and so. Reusing this method for the static WebP image, for the animated WebP image and for the GIF.

This commit is contained in:
Bogdan Poplauschi 2017-10-20 18:40:09 +03:00
parent f57c21d52a
commit 90c90a06bc
2 changed files with 49 additions and 50 deletions

View File

@ -47,7 +47,7 @@ NSString *kImageTestKey = @"TestImageKey.jpg";
[self.sharedImageCache diskImageExistsWithKey:kImageTestKey completion:^(BOOL isInCache) {
if (!isInCache) {
[self.sharedImageCache calculateSizeWithCompletionBlock:^(NSUInteger fileCount, NSUInteger totalSize) {
expect(fileCount).to.beLessThan(100);
expect(fileCount).to.equal(0);
[expectation fulfill];
}];
} else {

View File

@ -12,6 +12,7 @@
#import <SDWebImage/SDWebImageWebPCoder.h>
#import <SDWebImage/UIImage+ForceDecode.h>
#import <SDWebImage/SDWebImageGIFCoder.h>
#import <SDWebImage/NSData+ImageContentType.h>
@interface SDWebImageDecoderTests : SDTestCase
@ -97,65 +98,63 @@
- (void)test09ThatStaticWebPCoderWorks {
NSURL *staticWebPURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"TestImageStatic" withExtension:@"webp"];
NSData *staticWebPData = [NSData dataWithContentsOfURL:staticWebPURL];
expect([[SDWebImageWebPCoder sharedCoder] canDecodeFromData:staticWebPData]).to.beTruthy();
expect([[SDWebImageImageIOCoder sharedCoder] canDecodeFromData:staticWebPData]).to.beFalsy();
UIImage *staticWebPImage = [[SDWebImageWebPCoder sharedCoder] decodedImageWithData:staticWebPData];
expect(staticWebPImage).toNot.beNil();
expect([[SDWebImageWebPCoder sharedCoder] canEncodeToFormat:SDImageFormatWebP]).to.beTruthy();
expect([[SDWebImageImageIOCoder sharedCoder] canEncodeToFormat:SDImageFormatWebP]).to.beFalsy();
NSData *outputData = [[SDWebImageWebPCoder sharedCoder] encodedDataWithImage:staticWebPImage format:SDImageFormatWebP];
expect(outputData).toNot.beNil();
[self verifyCoder:[SDWebImageWebPCoder sharedCoder]
withLocalImageURL:staticWebPURL
isAnimatedImage:NO];
}
- (void)test10ThatAnimatedWebPCoderWorks {
NSURL *animatedWebPURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"TestImageAnimated" withExtension:@"webp"];
NSData *animatedWebPData = [NSData dataWithContentsOfURL:animatedWebPURL];
expect([[SDWebImageWebPCoder sharedCoder] canDecodeFromData:animatedWebPData]).to.beTruthy();
UIImage *animatedWebPImage = [[SDWebImageWebPCoder sharedCoder] decodedImageWithData:animatedWebPData];
expect(animatedWebPImage).toNot.beNil();
expect(animatedWebPImage.images.count).to.beGreaterThan(0);
CGSize imageSize = animatedWebPImage.size;
CGFloat imageScale = animatedWebPImage.scale;
[animatedWebPImage.images enumerateObjectsUsingBlock:^(UIImage * _Nonnull image, NSUInteger idx, BOOL * _Nonnull stop) {
CGSize size = image.size;
CGFloat scale = image.scale;
expect(imageSize.width).to.equal(size.width);
expect(imageSize.height).to.equal(size.height);
expect(imageScale).to.equal(scale);
}];
expect([[SDWebImageWebPCoder sharedCoder] canEncodeToFormat:SDImageFormatWebP]).to.beTruthy();
NSData *outputData = [[SDWebImageWebPCoder sharedCoder] encodedDataWithImage:animatedWebPImage format:SDImageFormatWebP];
expect(outputData).toNot.beNil();
[self verifyCoder:[SDWebImageWebPCoder sharedCoder]
withLocalImageURL:animatedWebPURL
isAnimatedImage:YES];
}
- (void)test20ThatOurGIFCoderWorksNotFLAnimatedImage {
NSURL *gifURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"TestImage" withExtension:@"gif"];
NSData *gifData = [NSData dataWithContentsOfURL:gifURL];
expect([[SDWebImageGIFCoder sharedCoder] canDecodeFromData:gifData]).to.beTruthy();
// the IOCoder can also decode GIF
expect([[SDWebImageImageIOCoder sharedCoder] canDecodeFromData:gifData]).to.beTruthy();
UIImage *gifImage = [[SDWebImageGIFCoder sharedCoder] decodedImageWithData:gifData];
expect(gifImage).toNot.beNil();
expect(gifImage.images.count).to.beGreaterThan(0);
CGSize imageSize = gifImage.size;
CGFloat imageScale = gifImage.scale;
[gifImage.images enumerateObjectsUsingBlock:^(UIImage * _Nonnull image, NSUInteger idx, BOOL * _Nonnull stop) {
CGSize size = image.size;
CGFloat scale = image.scale;
expect(imageSize.width).to.equal(size.width);
expect(imageSize.height).to.equal(size.height);
expect(imageScale).to.equal(scale);
}];
[self verifyCoder:[SDWebImageGIFCoder sharedCoder]
withLocalImageURL:gifURL
isAnimatedImage:YES];
}
- (void)verifyCoder:(id<SDWebImageCoder>)coder
withLocalImageURL:(NSURL *)imageUrl
isAnimatedImage:(BOOL)isAnimated {
NSData *inputImageData = [NSData dataWithContentsOfURL:imageUrl];
expect(inputImageData).toNot.beNil();
SDImageFormat inputImageFormat = [NSData sd_imageFormatForImageData:inputImageData];
expect(inputImageFormat).toNot.equal(SDImageFormatUndefined);
expect([[SDWebImageGIFCoder sharedCoder] decompressedImageWithImage:gifImage data:nil options:nil]).to.equal(gifImage);
// 1 - check if we can decode - should be true
expect([coder canDecodeFromData:inputImageData]).to.beTruthy();
expect([[SDWebImageGIFCoder sharedCoder] canEncodeToFormat:SDImageFormatGIF]).to.beTruthy();
expect([[SDWebImageImageIOCoder sharedCoder] canEncodeToFormat:SDImageFormatGIF]).to.beTruthy();
NSData *outputData = [[SDWebImageGIFCoder sharedCoder] encodedDataWithImage:gifImage format:SDImageFormatGIF];
expect(outputData).toNot.beNil();
// 2 - decode from NSData to UIImage and check it
UIImage *inputImage = [coder decodedImageWithData:inputImageData];
expect(inputImage).toNot.beNil();
if (isAnimated) {
// 2a - check images count > 0 (only for animated images)
expect(inputImage.images.count).to.beGreaterThan(0);
// 2b - check image size and scale for each frameImage (only for animated images)
CGSize imageSize = inputImage.size;
CGFloat imageScale = inputImage.scale;
[inputImage.images enumerateObjectsUsingBlock:^(UIImage * frameImage, NSUInteger idx, BOOL * stop) {
expect(imageSize).to.equal(frameImage.size);
expect(imageScale).to.equal(frameImage.scale);
}];
}
// 3 - check if we can encode to the original format
expect([coder canEncodeToFormat:inputImageFormat]).to.beTruthy();
// 4 - encode from UIImage to NSData using the inputImageFormat and check it
NSData *outputImageData = [coder encodedDataWithImage:inputImage format:inputImageFormat];
expect(outputImageData).toNot.beNil();
UIImage *outputImage = [coder decodedImageWithData:outputImageData];
expect(outputImage.size).to.equal(inputImage.size);
expect(outputImage.scale).to.equal(inputImage.scale);
expect(outputImage.images.count).to.equal(inputImage.images.count);
}
@end