Authored by Bogdan Poplauschi

Improved the code coverage of our unit tests (given that we disabled test40InsertionOfImageData).

Also updated the Tests Podfile so we don't get a warning (xcodeproj was renamed to project)
@@ -2,7 +2,7 @@ source 'https://github.com/CocoaPods/Specs.git' @@ -2,7 +2,7 @@ source 'https://github.com/CocoaPods/Specs.git'
2 2
3 use_frameworks! 3 use_frameworks!
4 4
5 -xcodeproj 'SDWebImage Tests' 5 +project 'SDWebImage Tests'
6 workspace '../SDWebImage' 6 workspace '../SDWebImage'
7 7
8 target 'Tests' do 8 target 'Tests' do
@@ -46,11 +46,15 @@ NSString *kImageTestKey = @"TestImageKey.jpg"; @@ -46,11 +46,15 @@ NSString *kImageTestKey = @"TestImageKey.jpg";
46 [self.sharedImageCache clearDiskOnCompletion:^{ 46 [self.sharedImageCache clearDiskOnCompletion:^{
47 [self.sharedImageCache diskImageExistsWithKey:kImageTestKey completion:^(BOOL isInCache) { 47 [self.sharedImageCache diskImageExistsWithKey:kImageTestKey completion:^(BOOL isInCache) {
48 if (!isInCache) { 48 if (!isInCache) {
  49 + [self.sharedImageCache calculateSizeWithCompletionBlock:^(NSUInteger fileCount, NSUInteger totalSize) {
  50 + expect(fileCount).to.beLessThan(100);
49 [expectation fulfill]; 51 [expectation fulfill];
  52 + }];
50 } else { 53 } else {
51 XCTFail(@"Image should not be in cache"); 54 XCTFail(@"Image should not be in cache");
52 } 55 }
53 }]; 56 }];
  57 +
54 expect([self.sharedImageCache imageFromMemoryCacheForKey:kImageTestKey]).to.equal([self imageForTesting]); 58 expect([self.sharedImageCache imageFromMemoryCacheForKey:kImageTestKey]).to.equal([self imageForTesting]);
55 }]; 59 }];
56 [self waitForExpectationsWithCommonTimeout]; 60 [self waitForExpectationsWithCommonTimeout];
@@ -11,6 +11,7 @@ @@ -11,6 +11,7 @@
11 #import <SDWebImage/SDWebImageImageIOCoder.h> 11 #import <SDWebImage/SDWebImageImageIOCoder.h>
12 #import <SDWebImage/SDWebImageWebPCoder.h> 12 #import <SDWebImage/SDWebImageWebPCoder.h>
13 #import <SDWebImage/UIImage+ForceDecode.h> 13 #import <SDWebImage/UIImage+ForceDecode.h>
  14 +#import <SDWebImage/SDWebImageGIFCoder.h>
14 15
15 @interface SDWebImageDecoderTests : SDTestCase 16 @interface SDWebImageDecoderTests : SDTestCase
16 17
@@ -97,9 +98,13 @@ @@ -97,9 +98,13 @@
97 - (void)test09ThatStaticWebPCoderWorks { 98 - (void)test09ThatStaticWebPCoderWorks {
98 NSURL *staticWebPURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"TestImageStatic" withExtension:@"webp"]; 99 NSURL *staticWebPURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"TestImageStatic" withExtension:@"webp"];
99 NSData *staticWebPData = [NSData dataWithContentsOfURL:staticWebPURL]; 100 NSData *staticWebPData = [NSData dataWithContentsOfURL:staticWebPURL];
  101 + expect([[SDWebImageWebPCoder sharedCoder] canDecodeFromData:staticWebPData]).to.beTruthy();
  102 + expect([[SDWebImageImageIOCoder sharedCoder] canDecodeFromData:staticWebPData]).to.beFalsy();
100 UIImage *staticWebPImage = [[SDWebImageWebPCoder sharedCoder] decodedImageWithData:staticWebPData]; 103 UIImage *staticWebPImage = [[SDWebImageWebPCoder sharedCoder] decodedImageWithData:staticWebPData];
101 expect(staticWebPImage).toNot.beNil(); 104 expect(staticWebPImage).toNot.beNil();
102 105
  106 + expect([[SDWebImageWebPCoder sharedCoder] canEncodeToFormat:SDImageFormatWebP]).to.beTruthy();
  107 + expect([[SDWebImageImageIOCoder sharedCoder] canEncodeToFormat:SDImageFormatWebP]).to.beFalsy();
103 NSData *outputData = [[SDWebImageWebPCoder sharedCoder] encodedDataWithImage:staticWebPImage format:SDImageFormatWebP]; 108 NSData *outputData = [[SDWebImageWebPCoder sharedCoder] encodedDataWithImage:staticWebPImage format:SDImageFormatWebP];
104 expect(outputData).toNot.beNil(); 109 expect(outputData).toNot.beNil();
105 } 110 }
@@ -107,6 +112,7 @@ @@ -107,6 +112,7 @@
107 - (void)test10ThatAnimatedWebPCoderWorks { 112 - (void)test10ThatAnimatedWebPCoderWorks {
108 NSURL *animatedWebPURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"TestImageAnimated" withExtension:@"webp"]; 113 NSURL *animatedWebPURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"TestImageAnimated" withExtension:@"webp"];
109 NSData *animatedWebPData = [NSData dataWithContentsOfURL:animatedWebPURL]; 114 NSData *animatedWebPData = [NSData dataWithContentsOfURL:animatedWebPURL];
  115 + expect([[SDWebImageWebPCoder sharedCoder] canDecodeFromData:animatedWebPData]).to.beTruthy();
110 UIImage *animatedWebPImage = [[SDWebImageWebPCoder sharedCoder] decodedImageWithData:animatedWebPData]; 116 UIImage *animatedWebPImage = [[SDWebImageWebPCoder sharedCoder] decodedImageWithData:animatedWebPData];
111 expect(animatedWebPImage).toNot.beNil(); 117 expect(animatedWebPImage).toNot.beNil();
112 expect(animatedWebPImage.images.count).to.beGreaterThan(0); 118 expect(animatedWebPImage.images.count).to.beGreaterThan(0);
@@ -120,8 +126,36 @@ @@ -120,8 +126,36 @@
120 expect(imageScale).to.equal(scale); 126 expect(imageScale).to.equal(scale);
121 }]; 127 }];
122 128
  129 + expect([[SDWebImageWebPCoder sharedCoder] canEncodeToFormat:SDImageFormatWebP]).to.beTruthy();
123 NSData *outputData = [[SDWebImageWebPCoder sharedCoder] encodedDataWithImage:animatedWebPImage format:SDImageFormatWebP]; 130 NSData *outputData = [[SDWebImageWebPCoder sharedCoder] encodedDataWithImage:animatedWebPImage format:SDImageFormatWebP];
124 expect(outputData).toNot.beNil(); 131 expect(outputData).toNot.beNil();
125 } 132 }
126 133
  134 +- (void)test20ThatOurGIFCoderWorksNotFLAnimatedImage {
  135 + NSURL *gifURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"TestImage" withExtension:@"gif"];
  136 + NSData *gifData = [NSData dataWithContentsOfURL:gifURL];
  137 + expect([[SDWebImageGIFCoder sharedCoder] canDecodeFromData:gifData]).to.beTruthy();
  138 + // the IOCoder can also decode GIF
  139 + expect([[SDWebImageImageIOCoder sharedCoder] canDecodeFromData:gifData]).to.beTruthy();
  140 + UIImage *gifImage = [[SDWebImageGIFCoder sharedCoder] decodedImageWithData:gifData];
  141 + expect(gifImage).toNot.beNil();
  142 + expect(gifImage.images.count).to.beGreaterThan(0);
  143 + CGSize imageSize = gifImage.size;
  144 + CGFloat imageScale = gifImage.scale;
  145 + [gifImage.images enumerateObjectsUsingBlock:^(UIImage * _Nonnull image, NSUInteger idx, BOOL * _Nonnull stop) {
  146 + CGSize size = image.size;
  147 + CGFloat scale = image.scale;
  148 + expect(imageSize.width).to.equal(size.width);
  149 + expect(imageSize.height).to.equal(size.height);
  150 + expect(imageScale).to.equal(scale);
  151 + }];
  152 +
  153 + expect([[SDWebImageGIFCoder sharedCoder] decompressedImageWithImage:gifImage data:nil options:nil]).to.equal(gifImage);
  154 +
  155 + expect([[SDWebImageGIFCoder sharedCoder] canEncodeToFormat:SDImageFormatGIF]).to.beTruthy();
  156 + expect([[SDWebImageImageIOCoder sharedCoder] canEncodeToFormat:SDImageFormatGIF]).to.beTruthy();
  157 + NSData *outputData = [[SDWebImageGIFCoder sharedCoder] encodedDataWithImage:gifImage format:SDImageFormatGIF];
  158 + expect(outputData).toNot.beNil();
  159 +}
  160 +
127 @end 161 @end