Authored by Bogdan Poplauschi
Committed by GitHub

Merge pull request #2068 from bpoplauschi/improve_unit_tests

Fixed our tests and improved the code coverage
@@ -76,6 +76,7 @@ @@ -76,6 +76,7 @@
76 @"http://littlesvr.ca/apng/images/world-cup-2014-42.webp", 76 @"http://littlesvr.ca/apng/images/world-cup-2014-42.webp",
77 @"https://isparta.github.io/compare-webp/image/gif_webp/webp/2.webp", 77 @"https://isparta.github.io/compare-webp/image/gif_webp/webp/2.webp",
78 @"https://nr-platform.s3.amazonaws.com/uploads/platform/published_extension/branding_icon/275/AmazonS3.png", 78 @"https://nr-platform.s3.amazonaws.com/uploads/platform/published_extension/branding_icon/275/AmazonS3.png",
  79 + @"http://via.placeholder.com/200x200.jpg",
79 nil]; 80 nil];
80 81
81 for (int i=0; i<100; i++) { 82 for (int i=0; i<100; i++) {
@@ -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
@@ -26,7 +26,7 @@ @@ -26,7 +26,7 @@
26 XCTestExpectation *expectation = [self expectationWithDescription:@"UIImageView setImageWithURL"]; 26 XCTestExpectation *expectation = [self expectationWithDescription:@"UIImageView setImageWithURL"];
27 27
28 UIImageView *imageView = [[UIImageView alloc] init]; 28 UIImageView *imageView = [[UIImageView alloc] init];
29 - NSURL *originalImageURL = [NSURL URLWithString:@"https://s3.amazonaws.com/fast-image-cache/demo-images/FICDDemoImage050.jpg"]; 29 + NSURL *originalImageURL = [NSURL URLWithString:kTestJpegURL];
30 [imageView sd_setImageWithURL:originalImageURL 30 [imageView sd_setImageWithURL:originalImageURL
31 completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) { 31 completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
32 expect(image).toNot.beNil(); 32 expect(image).toNot.beNil();
@@ -42,7 +42,7 @@ @@ -42,7 +42,7 @@
42 XCTestExpectation *expectation = [self expectationWithDescription:@"UIImageView setHighlightedImageWithURL"]; 42 XCTestExpectation *expectation = [self expectationWithDescription:@"UIImageView setHighlightedImageWithURL"];
43 43
44 UIImageView *imageView = [[UIImageView alloc] init]; 44 UIImageView *imageView = [[UIImageView alloc] init];
45 - NSURL *originalImageURL = [NSURL URLWithString:@"https://s3.amazonaws.com/fast-image-cache/demo-images/FICDDemoImage051.jpg"]; 45 + NSURL *originalImageURL = [NSURL URLWithString:kTestJpegURL];
46 [imageView sd_setHighlightedImageWithURL:originalImageURL 46 [imageView sd_setHighlightedImageWithURL:originalImageURL
47 completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) { 47 completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
48 expect(image).toNot.beNil(); 48 expect(image).toNot.beNil();
@@ -58,7 +58,7 @@ @@ -58,7 +58,7 @@
58 XCTestExpectation *expectation = [self expectationWithDescription:@"MKAnnotationView setImageWithURL"]; 58 XCTestExpectation *expectation = [self expectationWithDescription:@"MKAnnotationView setImageWithURL"];
59 59
60 MKAnnotationView *annotationView = [[MKAnnotationView alloc] init]; 60 MKAnnotationView *annotationView = [[MKAnnotationView alloc] init];
61 - NSURL *originalImageURL = [NSURL URLWithString:@"https://s3.amazonaws.com/fast-image-cache/demo-images/FICDDemoImage052.jpg"]; 61 + NSURL *originalImageURL = [NSURL URLWithString:kTestJpegURL];
62 [annotationView sd_setImageWithURL:originalImageURL 62 [annotationView sd_setImageWithURL:originalImageURL
63 completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) { 63 completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
64 expect(image).toNot.beNil(); 64 expect(image).toNot.beNil();
@@ -74,7 +74,7 @@ @@ -74,7 +74,7 @@
74 XCTestExpectation *expectation = [self expectationWithDescription:@"UIButton setImageWithURL normalState"]; 74 XCTestExpectation *expectation = [self expectationWithDescription:@"UIButton setImageWithURL normalState"];
75 75
76 UIButton *button = [[UIButton alloc] init]; 76 UIButton *button = [[UIButton alloc] init];
77 - NSURL *originalImageURL = [NSURL URLWithString:@"https://s3.amazonaws.com/fast-image-cache/demo-images/FICDDemoImage053.jpg"]; 77 + NSURL *originalImageURL = [NSURL URLWithString:kTestJpegURL];
78 [button sd_setImageWithURL:originalImageURL 78 [button sd_setImageWithURL:originalImageURL
79 forState:UIControlStateNormal 79 forState:UIControlStateNormal
80 completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) { 80 completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
@@ -91,7 +91,7 @@ @@ -91,7 +91,7 @@
91 XCTestExpectation *expectation = [self expectationWithDescription:@"UIButton setImageWithURL highlightedState"]; 91 XCTestExpectation *expectation = [self expectationWithDescription:@"UIButton setImageWithURL highlightedState"];
92 92
93 UIButton *button = [[UIButton alloc] init]; 93 UIButton *button = [[UIButton alloc] init];
94 - NSURL *originalImageURL = [NSURL URLWithString:@"https://s3.amazonaws.com/fast-image-cache/demo-images/FICDDemoImage054.jpg"]; 94 + NSURL *originalImageURL = [NSURL URLWithString:kTestJpegURL];
95 [button sd_setImageWithURL:originalImageURL 95 [button sd_setImageWithURL:originalImageURL
96 forState:UIControlStateHighlighted 96 forState:UIControlStateHighlighted
97 completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) { 97 completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
@@ -108,7 +108,7 @@ @@ -108,7 +108,7 @@
108 XCTestExpectation *expectation = [self expectationWithDescription:@"UIButton setBackgroundImageWithURL normalState"]; 108 XCTestExpectation *expectation = [self expectationWithDescription:@"UIButton setBackgroundImageWithURL normalState"];
109 109
110 UIButton *button = [[UIButton alloc] init]; 110 UIButton *button = [[UIButton alloc] init];
111 - NSURL *originalImageURL = [NSURL URLWithString:@"https://s3.amazonaws.com/fast-image-cache/demo-images/FICDDemoImage055.jpg"]; 111 + NSURL *originalImageURL = [NSURL URLWithString:kTestJpegURL];
112 [button sd_setBackgroundImageWithURL:originalImageURL 112 [button sd_setBackgroundImageWithURL:originalImageURL
113 forState:UIControlStateNormal 113 forState:UIControlStateNormal
114 completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) { 114 completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
@@ -14,24 +14,16 @@ @@ -14,24 +14,16 @@
14 NSString *kImageTestKey = @"TestImageKey.jpg"; 14 NSString *kImageTestKey = @"TestImageKey.jpg";
15 15
16 @interface SDImageCacheTests : SDTestCase 16 @interface SDImageCacheTests : SDTestCase
17 -@property (strong, nonatomic) SDImageCache *sharedImageCache;  
18 @end 17 @end
19 18
20 @implementation SDImageCacheTests 19 @implementation SDImageCacheTests
21 20
22 -- (void)setUp {  
23 - [super setUp];  
24 - // Put setup code here. This method is called before the invocation of each test method in the class.  
25 - self.sharedImageCache = [SDImageCache sharedImageCache];  
26 - [self clearAllCaches];  
27 -}  
28 -  
29 - (void)test01SharedImageCache { 21 - (void)test01SharedImageCache {
30 - expect(self.sharedImageCache).toNot.beNil(); 22 + expect([SDImageCache sharedImageCache]).toNot.beNil();
31 } 23 }
32 24
33 - (void)test02Singleton{ 25 - (void)test02Singleton{
34 - expect(self.sharedImageCache).to.equal([SDImageCache sharedImageCache]); 26 + expect([SDImageCache sharedImageCache]).to.equal([SDImageCache sharedImageCache]);
35 } 27 }
36 28
37 - (void)test03ImageCacheCanBeInstantiated { 29 - (void)test03ImageCacheCanBeInstantiated {
@@ -42,16 +34,21 @@ NSString *kImageTestKey = @"TestImageKey.jpg"; @@ -42,16 +34,21 @@ NSString *kImageTestKey = @"TestImageKey.jpg";
42 - (void)test04ClearDiskCache{ 34 - (void)test04ClearDiskCache{
43 XCTestExpectation *expectation = [self expectationWithDescription:@"Clear disk cache"]; 35 XCTestExpectation *expectation = [self expectationWithDescription:@"Clear disk cache"];
44 36
45 - [self.sharedImageCache storeImage:[self imageForTesting] forKey:kImageTestKey completion:nil];  
46 - [self.sharedImageCache clearDiskOnCompletion:^{  
47 - [self.sharedImageCache diskImageExistsWithKey:kImageTestKey completion:^(BOOL isInCache) { 37 + [[SDImageCache sharedImageCache] storeImage:[self imageForTesting] forKey:kImageTestKey completion:nil];
  38 + [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
  39 + expect([[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:kImageTestKey]).to.equal([self imageForTesting]);
  40 + [[SDImageCache sharedImageCache] diskImageExistsWithKey:kImageTestKey completion:^(BOOL isInCache) {
48 if (!isInCache) { 41 if (!isInCache) {
49 - [expectation fulfill]; 42 + [[SDImageCache sharedImageCache] calculateSizeWithCompletionBlock:^(NSUInteger fileCount, NSUInteger totalSize) {
  43 + expect(fileCount).to.equal(0);
  44 + [[SDImageCache sharedImageCache] removeImageForKey:kImageTestKey withCompletion:^{
  45 + [expectation fulfill];
  46 + }];
  47 + }];
50 } else { 48 } else {
51 XCTFail(@"Image should not be in cache"); 49 XCTFail(@"Image should not be in cache");
52 } 50 }
53 }]; 51 }];
54 - expect([self.sharedImageCache imageFromMemoryCacheForKey:kImageTestKey]).to.equal([self imageForTesting]);  
55 }]; 52 }];
56 [self waitForExpectationsWithCommonTimeout]; 53 [self waitForExpectationsWithCommonTimeout];
57 } 54 }
@@ -59,15 +56,18 @@ NSString *kImageTestKey = @"TestImageKey.jpg"; @@ -59,15 +56,18 @@ NSString *kImageTestKey = @"TestImageKey.jpg";
59 - (void)test05ClearMemoryCache{ 56 - (void)test05ClearMemoryCache{
60 XCTestExpectation *expectation = [self expectationWithDescription:@"Clear memory cache"]; 57 XCTestExpectation *expectation = [self expectationWithDescription:@"Clear memory cache"];
61 58
62 - [self.sharedImageCache storeImage:[self imageForTesting] forKey:kImageTestKey completion:nil];  
63 - [self.sharedImageCache clearMemory];  
64 - expect([self.sharedImageCache imageFromMemoryCacheForKey:kImageTestKey]).to.beNil;  
65 - [self.sharedImageCache diskImageExistsWithKey:kImageTestKey completion:^(BOOL isInCache) {  
66 - if (isInCache) {  
67 - [expectation fulfill];  
68 - } else {  
69 - XCTFail(@"Image should be in cache");  
70 - } 59 + [[SDImageCache sharedImageCache] storeImage:[self imageForTesting] forKey:kImageTestKey completion:^{
  60 + [[SDImageCache sharedImageCache] clearMemory];
  61 + expect([[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:kImageTestKey]).to.beNil;
  62 + [[SDImageCache sharedImageCache] diskImageExistsWithKey:kImageTestKey completion:^(BOOL isInCache) {
  63 + if (isInCache) {
  64 + [[SDImageCache sharedImageCache] removeImageForKey:kImageTestKey withCompletion:^{
  65 + [expectation fulfill];
  66 + }];
  67 + } else {
  68 + XCTFail(@"Image should be in cache");
  69 + }
  70 + }];
71 }]; 71 }];
72 [self waitForExpectationsWithCommonTimeout]; 72 [self waitForExpectationsWithCommonTimeout];
73 } 73 }
@@ -77,11 +77,13 @@ NSString *kImageTestKey = @"TestImageKey.jpg"; @@ -77,11 +77,13 @@ NSString *kImageTestKey = @"TestImageKey.jpg";
77 XCTestExpectation *expectation = [self expectationWithDescription:@"storeImage forKey"]; 77 XCTestExpectation *expectation = [self expectationWithDescription:@"storeImage forKey"];
78 78
79 UIImage *image = [self imageForTesting]; 79 UIImage *image = [self imageForTesting];
80 - [self.sharedImageCache storeImage:image forKey:kImageTestKey completion:nil];  
81 - expect([self.sharedImageCache imageFromMemoryCacheForKey:kImageTestKey]).to.equal(image);  
82 - [self.sharedImageCache diskImageExistsWithKey:kImageTestKey completion:^(BOOL isInCache) { 80 + [[SDImageCache sharedImageCache] storeImage:image forKey:kImageTestKey completion:nil];
  81 + expect([[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:kImageTestKey]).to.equal(image);
  82 + [[SDImageCache sharedImageCache] diskImageExistsWithKey:kImageTestKey completion:^(BOOL isInCache) {
83 if (isInCache) { 83 if (isInCache) {
84 - [expectation fulfill]; 84 + [[SDImageCache sharedImageCache] removeImageForKey:kImageTestKey withCompletion:^{
  85 + [expectation fulfill];
  86 + }];
85 } else { 87 } else {
86 XCTFail(@"Image should be in cache"); 88 XCTFail(@"Image should be in cache");
87 } 89 }
@@ -90,15 +92,17 @@ NSString *kImageTestKey = @"TestImageKey.jpg"; @@ -90,15 +92,17 @@ NSString *kImageTestKey = @"TestImageKey.jpg";
90 } 92 }
91 93
92 // Testing storeImage:forKey:toDisk:YES 94 // Testing storeImage:forKey:toDisk:YES
93 -- (void)test07InsertionOfImageForcingDiskStorage{ 95 +- (void)test07InsertionOfImageForcingDiskStorage {
94 XCTestExpectation *expectation = [self expectationWithDescription:@"storeImage forKey toDisk=YES"]; 96 XCTestExpectation *expectation = [self expectationWithDescription:@"storeImage forKey toDisk=YES"];
95 97
96 UIImage *image = [self imageForTesting]; 98 UIImage *image = [self imageForTesting];
97 - [self.sharedImageCache storeImage:image forKey:kImageTestKey toDisk:YES completion:nil];  
98 - expect([self.sharedImageCache imageFromMemoryCacheForKey:kImageTestKey]).to.equal(image);  
99 - [self.sharedImageCache diskImageExistsWithKey:kImageTestKey completion:^(BOOL isInCache) { 99 + [[SDImageCache sharedImageCache] storeImage:image forKey:kImageTestKey toDisk:YES completion:nil];
  100 + expect([[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:kImageTestKey]).to.equal(image);
  101 + [[SDImageCache sharedImageCache] diskImageExistsWithKey:kImageTestKey completion:^(BOOL isInCache) {
100 if (isInCache) { 102 if (isInCache) {
101 - [expectation fulfill]; 103 + [[SDImageCache sharedImageCache] removeImageForKey:kImageTestKey withCompletion:^{
  104 + [expectation fulfill];
  105 + }];
102 } else { 106 } else {
103 XCTFail(@"Image should be in cache"); 107 XCTFail(@"Image should be in cache");
104 } 108 }
@@ -110,121 +114,129 @@ NSString *kImageTestKey = @"TestImageKey.jpg"; @@ -110,121 +114,129 @@ NSString *kImageTestKey = @"TestImageKey.jpg";
110 - (void)test08InsertionOfImageOnlyInMemory { 114 - (void)test08InsertionOfImageOnlyInMemory {
111 XCTestExpectation *expectation = [self expectationWithDescription:@"storeImage forKey toDisk=NO"]; 115 XCTestExpectation *expectation = [self expectationWithDescription:@"storeImage forKey toDisk=NO"];
112 UIImage *image = [self imageForTesting]; 116 UIImage *image = [self imageForTesting];
113 - [self.sharedImageCache storeImage:image forKey:kImageTestKey toDisk:NO completion:nil]; 117 + [[SDImageCache sharedImageCache] storeImage:image forKey:kImageTestKey toDisk:NO completion:nil];
114 118
115 - expect([self.sharedImageCache imageFromMemoryCacheForKey:kImageTestKey]).to.equal([self imageForTesting]);  
116 - [self.sharedImageCache diskImageExistsWithKey:kImageTestKey completion:^(BOOL isInCache) { 119 + expect([[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:kImageTestKey]).to.equal([self imageForTesting]);
  120 + [[SDImageCache sharedImageCache] diskImageExistsWithKey:kImageTestKey completion:^(BOOL isInCache) {
117 if (!isInCache) { 121 if (!isInCache) {
118 [expectation fulfill]; 122 [expectation fulfill];
119 } else { 123 } else {
120 XCTFail(@"Image should not be in cache"); 124 XCTFail(@"Image should not be in cache");
121 } 125 }
122 }]; 126 }];
123 - [self.sharedImageCache clearMemory];  
124 - expect([self.sharedImageCache imageFromMemoryCacheForKey:kImageTestKey]).to.beNil(); 127 + [[SDImageCache sharedImageCache] clearMemory];
  128 + expect([[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:kImageTestKey]).to.beNil();
125 [self waitForExpectationsWithCommonTimeout]; 129 [self waitForExpectationsWithCommonTimeout];
126 } 130 }
127 131
128 -- (void)test09RetrieveImageThroughNSOperation{  
129 - //- (NSOperation *)queryCacheOperationForKey:(NSString *)key done:(SDWebImageQueryCompletedBlock)doneBlock; 132 +- (void)test09RetrieveImageThroughNSOperation {
  133 + XCTestExpectation *expectation = [self expectationWithDescription:@"queryCacheOperationForKey"];
130 UIImage *imageForTesting = [self imageForTesting]; 134 UIImage *imageForTesting = [self imageForTesting];
131 - [self.sharedImageCache storeImage:imageForTesting forKey:kImageTestKey completion:nil];  
132 - NSOperation *operation = [self.sharedImageCache queryCacheOperationForKey:kImageTestKey done:^(UIImage *image, NSData *data, SDImageCacheType cacheType) { 135 + [[SDImageCache sharedImageCache] storeImage:imageForTesting forKey:kImageTestKey completion:nil];
  136 + NSOperation *operation = [[SDImageCache sharedImageCache] queryCacheOperationForKey:kImageTestKey done:^(UIImage *image, NSData *data, SDImageCacheType cacheType) {
133 expect(image).to.equal(imageForTesting); 137 expect(image).to.equal(imageForTesting);
  138 + [[SDImageCache sharedImageCache] removeImageForKey:kImageTestKey withCompletion:^{
  139 + [expectation fulfill];
  140 + }];
134 }]; 141 }];
135 expect(operation).toNot.beNil; 142 expect(operation).toNot.beNil;
  143 + [operation start];
  144 + [self waitForExpectationsWithCommonTimeout];
136 } 145 }
137 146
138 -- (void)test10RemoveImageForKeyWithCompletion{  
139 - [self.sharedImageCache storeImage:[self imageForTesting] forKey:kImageTestKey completion:nil];  
140 - [self.sharedImageCache removeImageForKey:kImageTestKey withCompletion:^{  
141 - expect([self.sharedImageCache imageFromDiskCacheForKey:kImageTestKey]).to.beNil;  
142 - expect([self.sharedImageCache imageFromMemoryCacheForKey:kImageTestKey]).to.beNil; 147 +- (void)test10RemoveImageForKeyWithCompletion {
  148 + XCTestExpectation *expectation = [self expectationWithDescription:@"removeImageForKey"];
  149 + [[SDImageCache sharedImageCache] storeImage:[self imageForTesting] forKey:kImageTestKey completion:nil];
  150 + [[SDImageCache sharedImageCache] removeImageForKey:kImageTestKey withCompletion:^{
  151 + expect([[SDImageCache sharedImageCache] imageFromDiskCacheForKey:kImageTestKey]).to.beNil;
  152 + expect([[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:kImageTestKey]).to.beNil;
  153 + [expectation fulfill];
143 }]; 154 }];
  155 + [self waitForExpectationsWithCommonTimeout];
144 } 156 }
145 157
146 - (void)test11RemoveImageforKeyNotFromDiskWithCompletion{ 158 - (void)test11RemoveImageforKeyNotFromDiskWithCompletion{
147 - [self.sharedImageCache storeImage:[self imageForTesting] forKey:kImageTestKey completion:nil];  
148 - [self.sharedImageCache removeImageForKey:kImageTestKey fromDisk:NO withCompletion:^{  
149 - expect([self.sharedImageCache imageFromDiskCacheForKey:kImageTestKey]).toNot.beNil;  
150 - expect([self.sharedImageCache imageFromMemoryCacheForKey:kImageTestKey]).to.beNil; 159 + XCTestExpectation *expectation = [self expectationWithDescription:@"removeImageForKey fromDisk:NO"];
  160 + [[SDImageCache sharedImageCache] storeImage:[self imageForTesting] forKey:kImageTestKey completion:nil];
  161 + [[SDImageCache sharedImageCache] removeImageForKey:kImageTestKey fromDisk:NO withCompletion:^{
  162 + expect([[SDImageCache sharedImageCache] imageFromDiskCacheForKey:kImageTestKey]).toNot.beNil;
  163 + expect([[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:kImageTestKey]).to.beNil;
  164 + [expectation fulfill];
151 }]; 165 }];
  166 + [self waitForExpectationsWithCommonTimeout];
152 } 167 }
153 168
154 - (void)test12RemoveImageforKeyFromDiskWithCompletion{ 169 - (void)test12RemoveImageforKeyFromDiskWithCompletion{
155 - [self.sharedImageCache storeImage:[self imageForTesting] forKey:kImageTestKey completion:nil];  
156 - [self.sharedImageCache removeImageForKey:kImageTestKey fromDisk:YES withCompletion:^{  
157 - expect([self.sharedImageCache imageFromDiskCacheForKey:kImageTestKey]).to.beNil;  
158 - expect([self.sharedImageCache imageFromMemoryCacheForKey:kImageTestKey]).to.beNil; 170 + XCTestExpectation *expectation = [self expectationWithDescription:@"removeImageForKey fromDisk:YES"];
  171 + [[SDImageCache sharedImageCache] storeImage:[self imageForTesting] forKey:kImageTestKey completion:nil];
  172 + [[SDImageCache sharedImageCache] removeImageForKey:kImageTestKey fromDisk:YES withCompletion:^{
  173 + expect([[SDImageCache sharedImageCache] imageFromDiskCacheForKey:kImageTestKey]).to.beNil;
  174 + expect([[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:kImageTestKey]).to.beNil;
  175 + [expectation fulfill];
159 }]; 176 }];
  177 + [self waitForExpectationsWithCommonTimeout];
160 } 178 }
161 179
162 - (void)test20InitialCacheSize{ 180 - (void)test20InitialCacheSize{
163 - expect([self.sharedImageCache getSize]).to.equal(0); 181 + expect([[SDImageCache sharedImageCache] getSize]).to.equal(0);
164 } 182 }
165 183
166 - (void)test21InitialDiskCount{ 184 - (void)test21InitialDiskCount{
167 - [self.sharedImageCache storeImage:[self imageForTesting] forKey:kImageTestKey completion:nil];  
168 - expect([self.sharedImageCache getDiskCount]).to.equal(1);  
169 -}  
170 -  
171 -- (void)test22DiskCountAfterInsertion{  
172 - [self.sharedImageCache storeImage:[self imageForTesting] forKey:kImageTestKey completion:nil];  
173 - expect([self.sharedImageCache getDiskCount]).to.equal(1); 185 + XCTestExpectation *expectation = [self expectationWithDescription:@"getDiskCount"];
  186 + [[SDImageCache sharedImageCache] storeImage:[self imageForTesting] forKey:kImageTestKey completion:^{
  187 + expect([[SDImageCache sharedImageCache] getDiskCount]).to.equal(1);
  188 + [[SDImageCache sharedImageCache] removeImageForKey:kImageTestKey withCompletion:^{
  189 + [expectation fulfill];
  190 + }];
  191 + }];
  192 + [self waitForExpectationsWithCommonTimeout];
174 } 193 }
175 194
176 - (void)test31DefaultCachePathForAnyKey{ 195 - (void)test31DefaultCachePathForAnyKey{
177 - NSString *path = [self.sharedImageCache defaultCachePathForKey:kImageTestKey]; 196 + NSString *path = [[SDImageCache sharedImageCache] defaultCachePathForKey:kImageTestKey];
178 expect(path).toNot.beNil; 197 expect(path).toNot.beNil;
179 } 198 }
180 199
181 - (void)test32CachePathForNonExistingKey{ 200 - (void)test32CachePathForNonExistingKey{
182 - NSString *path = [self.sharedImageCache cachePathForKey:kImageTestKey inPath:[self.sharedImageCache defaultCachePathForKey:kImageTestKey]]; 201 + NSString *path = [[SDImageCache sharedImageCache] cachePathForKey:kImageTestKey inPath:[[SDImageCache sharedImageCache] defaultCachePathForKey:kImageTestKey]];
183 expect(path).to.beNil; 202 expect(path).to.beNil;
184 } 203 }
185 204
186 - (void)test33CachePathForExistingKey{ 205 - (void)test33CachePathForExistingKey{
187 - [self.sharedImageCache storeImage:[self imageForTesting] forKey:kImageTestKey completion:nil];  
188 - NSString *path = [self.sharedImageCache cachePathForKey:kImageTestKey inPath:[self.sharedImageCache defaultCachePathForKey:kImageTestKey]];  
189 - expect(path).notTo.beNil; 206 + XCTestExpectation *expectation = [self expectationWithDescription:@"cachePathForKey inPath"];
  207 + [[SDImageCache sharedImageCache] storeImage:[self imageForTesting] forKey:kImageTestKey completion:^{
  208 + NSString *path = [[SDImageCache sharedImageCache] cachePathForKey:kImageTestKey inPath:[[SDImageCache sharedImageCache] defaultCachePathForKey:kImageTestKey]];
  209 + expect(path).notTo.beNil;
  210 + [[SDImageCache sharedImageCache] removeImageForKey:kImageTestKey withCompletion:^{
  211 + [expectation fulfill];
  212 + }];
  213 + }];
  214 + [self waitForExpectationsWithCommonTimeout];
190 } 215 }
191 216
192 -// TODO -- Testing image data insertion  
193 -  
194 -// TODO -- this test is driving me crazy keeps failing for unknown reasons, sometimes the image data is not written to disk - disabling the test for now  
195 -- (void)a40InsertionOfImageData { 217 +- (void)test40InsertionOfImageData {
196 XCTestExpectation *expectation = [self expectationWithDescription:@"Insertion of image data works"]; 218 XCTestExpectation *expectation = [self expectationWithDescription:@"Insertion of image data works"];
197 219
198 UIImage *image = [UIImage imageWithContentsOfFile:[self testImagePath]]; 220 UIImage *image = [UIImage imageWithContentsOfFile:[self testImagePath]];
199 NSData *imageData = UIImageJPEGRepresentation(image, 1.0); 221 NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
200 - [self.sharedImageCache storeImageDataToDisk:imageData forKey:kImageTestKey]; 222 + [[SDImageCache sharedImageCache] storeImageDataToDisk:imageData forKey:kImageTestKey];
201 223
202 - UIImage *storedImageFromMemory = [self.sharedImageCache imageFromMemoryCacheForKey:kImageTestKey]; 224 + UIImage *storedImageFromMemory = [[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:kImageTestKey];
203 expect(storedImageFromMemory).to.equal(nil); 225 expect(storedImageFromMemory).to.equal(nil);
204 226
205 - NSString *cachePath = [self.sharedImageCache defaultCachePathForKey:kImageTestKey]; 227 + NSString *cachePath = [[SDImageCache sharedImageCache] defaultCachePathForKey:kImageTestKey];
206 UIImage *cachedImage = [UIImage imageWithContentsOfFile:cachePath]; 228 UIImage *cachedImage = [UIImage imageWithContentsOfFile:cachePath];
207 NSData *storedImageData = UIImageJPEGRepresentation(cachedImage, 1.0); 229 NSData *storedImageData = UIImageJPEGRepresentation(cachedImage, 1.0);
208 expect(storedImageData.length).to.beGreaterThan(0); 230 expect(storedImageData.length).to.beGreaterThan(0);
209 expect(cachedImage.size).to.equal(image.size); 231 expect(cachedImage.size).to.equal(image.size);
210 // can't directly compare image and cachedImage because apparently there are some slight differences, even though the image is the same 232 // can't directly compare image and cachedImage because apparently there are some slight differences, even though the image is the same
211 233
212 - __block int blocksCalled = 0;  
213 -  
214 - [self.sharedImageCache diskImageExistsWithKey:kImageTestKey completion:^(BOOL isInCache) { 234 + [[SDImageCache sharedImageCache] diskImageExistsWithKey:kImageTestKey completion:^(BOOL isInCache) {
215 expect(isInCache).to.equal(YES); 235 expect(isInCache).to.equal(YES);
216 - blocksCalled += 1;  
217 - if (blocksCalled == 2) {  
218 - [expectation fulfill];  
219 - }  
220 - }];  
221 -  
222 - [self.sharedImageCache calculateSizeWithCompletionBlock:^(NSUInteger fileCount, NSUInteger totalSize) {  
223 - expect(fileCount).to.beLessThan(100);  
224 - blocksCalled += 1;  
225 - if (blocksCalled == 2) { 236 +
  237 + [[SDImageCache sharedImageCache] removeImageForKey:kImageTestKey withCompletion:^{
226 [expectation fulfill]; 238 [expectation fulfill];
227 - } 239 + }];
228 }]; 240 }];
229 241
230 [self waitForExpectationsWithCommonTimeout]; 242 [self waitForExpectationsWithCommonTimeout];
@@ -269,7 +281,10 @@ NSString *kImageTestKey = @"TestImageKey.jpg"; @@ -269,7 +281,10 @@ NSString *kImageTestKey = @"TestImageKey.jpg";
269 } 281 }
270 282
271 [[SDWebImageCodersManager sharedInstance] removeCoder:testDecoder]; 283 [[SDWebImageCodersManager sharedInstance] removeCoder:testDecoder];
272 - [expectation fulfill]; 284 +
  285 + [[SDImageCache sharedImageCache] removeImageForKey:key withCompletion:^{
  286 + [expectation fulfill];
  287 + }];
273 }]; 288 }];
274 289
275 [self waitForExpectationsWithCommonTimeout]; 290 [self waitForExpectationsWithCommonTimeout];
@@ -277,14 +292,6 @@ NSString *kImageTestKey = @"TestImageKey.jpg"; @@ -277,14 +292,6 @@ NSString *kImageTestKey = @"TestImageKey.jpg";
277 292
278 #pragma mark Helper methods 293 #pragma mark Helper methods
279 294
280 -- (void)clearAllCaches{  
281 - [self.sharedImageCache deleteOldFilesWithCompletionBlock:nil];  
282 -  
283 - // TODO: this is not ok, clearDiskOnCompletion will clear async, this means that when we execute the tests, the cache might not be cleared  
284 - [self.sharedImageCache clearDiskOnCompletion:nil];  
285 - [self.sharedImageCache clearMemory];  
286 -}  
287 -  
288 - (UIImage *)imageForTesting{ 295 - (UIImage *)imageForTesting{
289 static UIImage *reusableImage = nil; 296 static UIImage *reusableImage = nil;
290 if (!reusableImage) { 297 if (!reusableImage) {
@@ -13,11 +13,13 @@ @@ -13,11 +13,13 @@
13 #import <XCTest/XCTest.h> 13 #import <XCTest/XCTest.h>
14 #import <Expecta/Expecta.h> 14 #import <Expecta/Expecta.h>
15 15
16 -extern const int64_t kAsyncTestTimeout; 16 +FOUNDATION_EXPORT const int64_t kAsyncTestTimeout;
  17 +FOUNDATION_EXPORT NSString * _Nonnull const kTestJpegURL;
  18 +FOUNDATION_EXPORT NSString * _Nonnull const kTestPNGURL;
17 19
18 @interface SDTestCase : XCTestCase 20 @interface SDTestCase : XCTestCase
19 21
20 - (void)waitForExpectationsWithCommonTimeout; 22 - (void)waitForExpectationsWithCommonTimeout;
21 -- (void)waitForExpectationsWithCommonTimeoutUsingHandler:(XCWaitCompletionHandler)handler; 23 +- (void)waitForExpectationsWithCommonTimeoutUsingHandler:(nullable XCWaitCompletionHandler)handler;
22 24
23 @end 25 @end
@@ -10,6 +10,8 @@ @@ -10,6 +10,8 @@
10 #import "SDTestCase.h" 10 #import "SDTestCase.h"
11 11
12 const int64_t kAsyncTestTimeout = 5; 12 const int64_t kAsyncTestTimeout = 5;
  13 +NSString *const kTestJpegURL = @"http://via.placeholder.com/50x50.jpg";
  14 +NSString *const kTestPNGURL = @"http://via.placeholder.com/50x50.png";
13 15
14 @implementation SDTestCase 16 @implementation SDTestCase
15 17
@@ -11,6 +11,8 @@ @@ -11,6 +11,8 @@
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>
  15 +#import <SDWebImage/NSData+ImageContentType.h>
14 16
15 @interface SDWebImageDecoderTests : SDTestCase 17 @interface SDWebImageDecoderTests : SDTestCase
16 18
@@ -96,32 +98,63 @@ @@ -96,32 +98,63 @@
96 98
97 - (void)test09ThatStaticWebPCoderWorks { 99 - (void)test09ThatStaticWebPCoderWorks {
98 NSURL *staticWebPURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"TestImageStatic" withExtension:@"webp"]; 100 NSURL *staticWebPURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"TestImageStatic" withExtension:@"webp"];
99 - NSData *staticWebPData = [NSData dataWithContentsOfURL:staticWebPURL];  
100 - UIImage *staticWebPImage = [[SDWebImageWebPCoder sharedCoder] decodedImageWithData:staticWebPData];  
101 - expect(staticWebPImage).toNot.beNil();  
102 -  
103 - NSData *outputData = [[SDWebImageWebPCoder sharedCoder] encodedDataWithImage:staticWebPImage format:SDImageFormatWebP];  
104 - expect(outputData).toNot.beNil(); 101 + [self verifyCoder:[SDWebImageWebPCoder sharedCoder]
  102 + withLocalImageURL:staticWebPURL
  103 + isAnimatedImage:NO];
105 } 104 }
106 105
107 - (void)test10ThatAnimatedWebPCoderWorks { 106 - (void)test10ThatAnimatedWebPCoderWorks {
108 NSURL *animatedWebPURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"TestImageAnimated" withExtension:@"webp"]; 107 NSURL *animatedWebPURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"TestImageAnimated" withExtension:@"webp"];
109 - NSData *animatedWebPData = [NSData dataWithContentsOfURL:animatedWebPURL];  
110 - UIImage *animatedWebPImage = [[SDWebImageWebPCoder sharedCoder] decodedImageWithData:animatedWebPData];  
111 - expect(animatedWebPImage).toNot.beNil();  
112 - expect(animatedWebPImage.images.count).to.beGreaterThan(0);  
113 - CGSize imageSize = animatedWebPImage.size;  
114 - CGFloat imageScale = animatedWebPImage.scale;  
115 - [animatedWebPImage.images enumerateObjectsUsingBlock:^(UIImage * _Nonnull image, NSUInteger idx, BOOL * _Nonnull stop) {  
116 - CGSize size = image.size;  
117 - CGFloat scale = image.scale;  
118 - expect(imageSize.width).to.equal(size.width);  
119 - expect(imageSize.height).to.equal(size.height);  
120 - expect(imageScale).to.equal(scale);  
121 - }]; 108 + [self verifyCoder:[SDWebImageWebPCoder sharedCoder]
  109 + withLocalImageURL:animatedWebPURL
  110 + isAnimatedImage:YES];
  111 +}
  112 +
  113 +- (void)test20ThatOurGIFCoderWorksNotFLAnimatedImage {
  114 + NSURL *gifURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"TestImage" withExtension:@"gif"];
  115 + [self verifyCoder:[SDWebImageGIFCoder sharedCoder]
  116 + withLocalImageURL:gifURL
  117 + isAnimatedImage:YES];
  118 +}
  119 +
  120 +- (void)verifyCoder:(id<SDWebImageCoder>)coder
  121 + withLocalImageURL:(NSURL *)imageUrl
  122 + isAnimatedImage:(BOOL)isAnimated {
  123 + NSData *inputImageData = [NSData dataWithContentsOfURL:imageUrl];
  124 + expect(inputImageData).toNot.beNil();
  125 + SDImageFormat inputImageFormat = [NSData sd_imageFormatForImageData:inputImageData];
  126 + expect(inputImageFormat).toNot.equal(SDImageFormatUndefined);
  127 +
  128 + // 1 - check if we can decode - should be true
  129 + expect([coder canDecodeFromData:inputImageData]).to.beTruthy();
  130 +
  131 + // 2 - decode from NSData to UIImage and check it
  132 + UIImage *inputImage = [coder decodedImageWithData:inputImageData];
  133 + expect(inputImage).toNot.beNil();
  134 +
  135 + if (isAnimated) {
  136 + // 2a - check images count > 0 (only for animated images)
  137 + expect(inputImage.images.count).to.beGreaterThan(0);
  138 +
  139 + // 2b - check image size and scale for each frameImage (only for animated images)
  140 + CGSize imageSize = inputImage.size;
  141 + CGFloat imageScale = inputImage.scale;
  142 + [inputImage.images enumerateObjectsUsingBlock:^(UIImage * frameImage, NSUInteger idx, BOOL * stop) {
  143 + expect(imageSize).to.equal(frameImage.size);
  144 + expect(imageScale).to.equal(frameImage.scale);
  145 + }];
  146 + }
  147 +
  148 + // 3 - check if we can encode to the original format
  149 + expect([coder canEncodeToFormat:inputImageFormat]).to.beTruthy();
122 150
123 - NSData *outputData = [[SDWebImageWebPCoder sharedCoder] encodedDataWithImage:animatedWebPImage format:SDImageFormatWebP];  
124 - expect(outputData).toNot.beNil(); 151 + // 4 - encode from UIImage to NSData using the inputImageFormat and check it
  152 + NSData *outputImageData = [coder encodedDataWithImage:inputImage format:inputImageFormat];
  153 + expect(outputImageData).toNot.beNil();
  154 + UIImage *outputImage = [coder decodedImageWithData:outputImageData];
  155 + expect(outputImage.size).to.equal(inputImage.size);
  156 + expect(outputImage.scale).to.equal(inputImage.scale);
  157 + expect(outputImage.images.count).to.equal(inputImage.images.count);
125 } 158 }
126 159
127 @end 160 @end
@@ -80,7 +80,7 @@ @@ -80,7 +80,7 @@
80 80
81 - (void)test04ThatASimpleDownloadWorks { 81 - (void)test04ThatASimpleDownloadWorks {
82 XCTestExpectation *expectation = [self expectationWithDescription:@"Simple download"]; 82 XCTestExpectation *expectation = [self expectationWithDescription:@"Simple download"];
83 - NSURL *imageURL = [NSURL URLWithString:@"http://s3.amazonaws.com/fast-image-cache/demo-images/FICDDemoImage004.jpg"]; 83 + NSURL *imageURL = [NSURL URLWithString:kTestJpegURL];
84 [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:imageURL options:0 progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) { 84 [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:imageURL options:0 progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
85 if (image && data && !error && finished) { 85 if (image && data && !error && finished) {
86 [expectation fulfill]; 86 [expectation fulfill];
@@ -145,7 +145,7 @@ @@ -145,7 +145,7 @@
145 145
146 - (void)test09ThatProgressiveJPEGWorks { 146 - (void)test09ThatProgressiveJPEGWorks {
147 XCTestExpectation *expectation = [self expectationWithDescription:@"Progressive JPEG download"]; 147 XCTestExpectation *expectation = [self expectationWithDescription:@"Progressive JPEG download"];
148 - NSURL *imageURL = [NSURL URLWithString:@"http://s3.amazonaws.com/fast-image-cache/demo-images/FICDDemoImage009.jpg"]; 148 + NSURL *imageURL = [NSURL URLWithString:kTestJpegURL];
149 [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:imageURL options:SDWebImageDownloaderProgressiveDownload progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) { 149 [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:imageURL options:SDWebImageDownloaderProgressiveDownload progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
150 if (image && data && !error && finished) { 150 if (image && data && !error && finished) {
151 [expectation fulfill]; 151 [expectation fulfill];
@@ -175,7 +175,7 @@ @@ -175,7 +175,7 @@
175 - (void)test11ThatCancelWorks { 175 - (void)test11ThatCancelWorks {
176 XCTestExpectation *expectation = [self expectationWithDescription:@"Cancel"]; 176 XCTestExpectation *expectation = [self expectationWithDescription:@"Cancel"];
177 177
178 - NSURL *imageURL = [NSURL URLWithString:@"http://s3.amazonaws.com/fast-image-cache/demo-images/FICDDemoImage011.jpg"]; 178 + NSURL *imageURL = [NSURL URLWithString:kTestJpegURL];
179 SDWebImageDownloadToken *token = [[SDWebImageDownloader sharedDownloader] 179 SDWebImageDownloadToken *token = [[SDWebImageDownloader sharedDownloader]
180 downloadImageWithURL:imageURL options:0 progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) { 180 downloadImageWithURL:imageURL options:0 progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
181 XCTFail(@"Should not get here"); 181 XCTFail(@"Should not get here");
@@ -195,7 +195,7 @@ @@ -195,7 +195,7 @@
195 195
196 - (void)test12ThatWeCanUseAnotherSessionForEachDownloadOperation { 196 - (void)test12ThatWeCanUseAnotherSessionForEachDownloadOperation {
197 XCTestExpectation *expectation = [self expectationWithDescription:@"Owned session"]; 197 XCTestExpectation *expectation = [self expectationWithDescription:@"Owned session"];
198 - NSURL *imageURL = [NSURL URLWithString:@"http://s3.amazonaws.com/fast-image-cache/demo-images/FICDDemoImage012.jpg"]; 198 + NSURL *imageURL = [NSURL URLWithString:kTestJpegURL];
199 199
200 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:imageURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:15]; 200 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:imageURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:15];
201 request.HTTPShouldUsePipelining = YES; 201 request.HTTPShouldUsePipelining = YES;
@@ -221,7 +221,7 @@ @@ -221,7 +221,7 @@
221 221
222 - (void)test13ThatDownloadCanContinueWhenTheAppEntersBackground { 222 - (void)test13ThatDownloadCanContinueWhenTheAppEntersBackground {
223 XCTestExpectation *expectation = [self expectationWithDescription:@"Simple download"]; 223 XCTestExpectation *expectation = [self expectationWithDescription:@"Simple download"];
224 - NSURL *imageURL = [NSURL URLWithString:@"http://s3.amazonaws.com/fast-image-cache/demo-images/FICDDemoImage013.jpg"]; 224 + NSURL *imageURL = [NSURL URLWithString:kTestJpegURL];
225 [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:imageURL options:SDWebImageDownloaderContinueInBackground progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) { 225 [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:imageURL options:SDWebImageDownloaderContinueInBackground progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
226 if (image && data && !error && finished) { 226 if (image && data && !error && finished) {
227 [expectation fulfill]; 227 [expectation fulfill];
@@ -233,8 +233,8 @@ @@ -233,8 +233,8 @@
233 } 233 }
234 234
235 - (void)test14ThatPNGWorks { 235 - (void)test14ThatPNGWorks {
236 - XCTestExpectation *expectation = [self expectationWithDescription:@"WEBP"];  
237 - NSURL *imageURL = [NSURL URLWithString:@"https://nr-platform.s3.amazonaws.com/uploads/platform/published_extension/branding_icon/275/AmazonS3.png"]; 236 + XCTestExpectation *expectation = [self expectationWithDescription:@"PNG"];
  237 + NSURL *imageURL = [NSURL URLWithString:kTestPNGURL];
238 [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:imageURL options:0 progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) { 238 [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:imageURL options:0 progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
239 if (image && data && !error && finished) { 239 if (image && data && !error && finished) {
240 [expectation fulfill]; 240 [expectation fulfill];
@@ -282,7 +282,7 @@ @@ -282,7 +282,7 @@
282 - (void)test20ThatDownloadingSameURLTwiceAndCancellingFirstWorks { 282 - (void)test20ThatDownloadingSameURLTwiceAndCancellingFirstWorks {
283 XCTestExpectation *expectation = [self expectationWithDescription:@"Correct image downloads"]; 283 XCTestExpectation *expectation = [self expectationWithDescription:@"Correct image downloads"];
284 284
285 - NSURL *imageURL = [NSURL URLWithString:@"http://s3.amazonaws.com/fast-image-cache/demo-images/FICDDemoImage020.jpg"]; 285 + NSURL *imageURL = [NSURL URLWithString:kTestJpegURL];
286 286
287 SDWebImageDownloadToken *token1 = [[SDWebImageDownloader sharedDownloader] 287 SDWebImageDownloadToken *token1 = [[SDWebImageDownloader sharedDownloader]
288 downloadImageWithURL:imageURL 288 downloadImageWithURL:imageURL
@@ -320,7 +320,7 @@ @@ -320,7 +320,7 @@
320 - (void)test21ThatCancelingDownloadThenRequestingAgainWorks { 320 - (void)test21ThatCancelingDownloadThenRequestingAgainWorks {
321 XCTestExpectation *expectation = [self expectationWithDescription:@"Correct image downloads"]; 321 XCTestExpectation *expectation = [self expectationWithDescription:@"Correct image downloads"];
322 322
323 - NSURL *imageURL = [NSURL URLWithString:@"http://s3.amazonaws.com/fast-image-cache/demo-images/FICDDemoImage021.jpg"]; 323 + NSURL *imageURL = [NSURL URLWithString:kTestJpegURL];
324 324
325 SDWebImageDownloadToken *token1 = [[SDWebImageDownloader sharedDownloader] 325 SDWebImageDownloadToken *token1 = [[SDWebImageDownloader sharedDownloader]
326 downloadImageWithURL:imageURL 326 downloadImageWithURL:imageURL
@@ -9,8 +9,6 @@ @@ -9,8 +9,6 @@
9 #import "SDTestCase.h" 9 #import "SDTestCase.h"
10 #import <SDWebImage/SDWebImageManager.h> 10 #import <SDWebImage/SDWebImageManager.h>
11 11
12 -NSString *workingImageURL = @"http://s3.amazonaws.com/fast-image-cache/demo-images/FICDDemoImage001.jpg";  
13 -  
14 @interface SDWebImageManagerTests : SDTestCase 12 @interface SDWebImageManagerTests : SDTestCase
15 13
16 @end 14 @end
@@ -25,7 +23,7 @@ NSString *workingImageURL = @"http://s3.amazonaws.com/fast-image-cache/demo-imag @@ -25,7 +23,7 @@ NSString *workingImageURL = @"http://s3.amazonaws.com/fast-image-cache/demo-imag
25 - (void)test02ThatDownloadInvokesCompletionBlockWithCorrectParamsAsync { 23 - (void)test02ThatDownloadInvokesCompletionBlockWithCorrectParamsAsync {
26 __block XCTestExpectation *expectation = [self expectationWithDescription:@"Image download completes"]; 24 __block XCTestExpectation *expectation = [self expectationWithDescription:@"Image download completes"];
27 25
28 - NSURL *originalImageURL = [NSURL URLWithString:workingImageURL]; 26 + NSURL *originalImageURL = [NSURL URLWithString:kTestJpegURL];
29 27
30 [[SDWebImageManager sharedManager] loadImageWithURL:originalImageURL 28 [[SDWebImageManager sharedManager] loadImageWithURL:originalImageURL
31 options:SDWebImageRefreshCached 29 options:SDWebImageRefreshCached
@@ -65,7 +63,7 @@ NSString *workingImageURL = @"http://s3.amazonaws.com/fast-image-cache/demo-imag @@ -65,7 +63,7 @@ NSString *workingImageURL = @"http://s3.amazonaws.com/fast-image-cache/demo-imag
65 63
66 - (void)test04CachedImageExistsForURL { 64 - (void)test04CachedImageExistsForURL {
67 __block XCTestExpectation *expectation = [self expectationWithDescription:@"Image exists in cache"]; 65 __block XCTestExpectation *expectation = [self expectationWithDescription:@"Image exists in cache"];
68 - NSURL *imageURL = [NSURL URLWithString:workingImageURL]; 66 + NSURL *imageURL = [NSURL URLWithString:kTestJpegURL];
69 [[SDWebImageManager sharedManager] cachedImageExistsForURL:imageURL completion:^(BOOL isInCache) { 67 [[SDWebImageManager sharedManager] cachedImageExistsForURL:imageURL completion:^(BOOL isInCache) {
70 if (isInCache) { 68 if (isInCache) {
71 [expectation fulfill]; 69 [expectation fulfill];
@@ -78,7 +76,7 @@ NSString *workingImageURL = @"http://s3.amazonaws.com/fast-image-cache/demo-imag @@ -78,7 +76,7 @@ NSString *workingImageURL = @"http://s3.amazonaws.com/fast-image-cache/demo-imag
78 76
79 - (void)test05DiskImageExistsForURL { 77 - (void)test05DiskImageExistsForURL {
80 __block XCTestExpectation *expectation = [self expectationWithDescription:@"Image exists in disk cache"]; 78 __block XCTestExpectation *expectation = [self expectationWithDescription:@"Image exists in disk cache"];
81 - NSURL *imageURL = [NSURL URLWithString:workingImageURL]; 79 + NSURL *imageURL = [NSURL URLWithString:kTestJpegURL];
82 [[SDWebImageManager sharedManager] diskImageExistsForURL:imageURL completion:^(BOOL isInCache) { 80 [[SDWebImageManager sharedManager] diskImageExistsForURL:imageURL completion:^(BOOL isInCache) {
83 if (isInCache) { 81 if (isInCache) {
84 [expectation fulfill]; 82 [expectation fulfill];
@@ -92,7 +90,9 @@ NSString *workingImageURL = @"http://s3.amazonaws.com/fast-image-cache/demo-imag @@ -92,7 +90,9 @@ NSString *workingImageURL = @"http://s3.amazonaws.com/fast-image-cache/demo-imag
92 - (void)test06CancellAll { 90 - (void)test06CancellAll {
93 XCTestExpectation *expectation = [self expectationWithDescription:@"Cancel"]; 91 XCTestExpectation *expectation = [self expectationWithDescription:@"Cancel"];
94 92
95 - NSURL *imageURL = [NSURL URLWithString:@"http://s3.amazonaws.com/fast-image-cache/demo-images/FICDDemoImage006.jpg"]; 93 + // need a bigger image here, that is why we don't use kTestJpegURL
  94 + // if the image is too small, it will get downloaded before we can cancel :)
  95 + NSURL *imageURL = [NSURL URLWithString:@"https://s3.amazonaws.com/fast-image-cache/demo-images/FICDDemoImage001.jpg"];
96 [[SDWebImageManager sharedManager] loadImageWithURL:imageURL options:0 progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) { 96 [[SDWebImageManager sharedManager] loadImageWithURL:imageURL options:0 progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
97 XCTFail(@"Should not get here"); 97 XCTFail(@"Should not get here");
98 }]; 98 }];
@@ -110,7 +110,7 @@ NSString *workingImageURL = @"http://s3.amazonaws.com/fast-image-cache/demo-imag @@ -110,7 +110,7 @@ NSString *workingImageURL = @"http://s3.amazonaws.com/fast-image-cache/demo-imag
110 110
111 - (void)test07ThatLoadImageWithSDWebImageRefreshCachedWorks { 111 - (void)test07ThatLoadImageWithSDWebImageRefreshCachedWorks {
112 XCTestExpectation *expectation = [self expectationWithDescription:@"Image download twice with SDWebImageRefresh failed"]; 112 XCTestExpectation *expectation = [self expectationWithDescription:@"Image download twice with SDWebImageRefresh failed"];
113 - NSURL *originalImageURL = [NSURL URLWithString:@"http://s3.amazonaws.com/fast-image-cache/demo-images/FICDDemoImage007.jpg"]; 113 + NSURL *originalImageURL = [NSURL URLWithString:kTestJpegURL];
114 [[SDImageCache sharedImageCache] clearDiskOnCompletion:nil]; 114 [[SDImageCache sharedImageCache] clearDiskOnCompletion:nil];
115 115
116 [[SDWebImageManager sharedManager] loadImageWithURL:originalImageURL options:SDWebImageRefreshCached progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) { 116 [[SDWebImageManager sharedManager] loadImageWithURL:originalImageURL options:SDWebImageRefreshCached progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
@@ -24,13 +24,9 @@ @@ -24,13 +24,9 @@
24 - (void)test02PrefetchMultipleImages { 24 - (void)test02PrefetchMultipleImages {
25 XCTestExpectation *expectation = [self expectationWithDescription:@"Correct prefetch of multiple images"]; 25 XCTestExpectation *expectation = [self expectationWithDescription:@"Correct prefetch of multiple images"];
26 26
27 - NSMutableArray *imageURLs = [NSMutableArray array];  
28 -  
29 - for (int i=40; i<43; i++) {  
30 - NSString *imageURLString = [NSString stringWithFormat:@"https://s3.amazonaws.com/fast-image-cache/demo-images/FICDDemoImage%03d.jpg", i];  
31 - NSURL *imageURL = [NSURL URLWithString:imageURLString];  
32 - [imageURLs addObject:imageURL];  
33 - } 27 + NSArray *imageURLs = @[@"http://via.placeholder.com/20x20.jpg",
  28 + @"http://via.placeholder.com/30x30.jpg",
  29 + @"http://via.placeholder.com/40x40.jpg"];
34 30
35 __block int numberOfPrefetched = 0; 31 __block int numberOfPrefetched = 0;
36 32