|
10 | 10 | @import XCTest;
|
11 | 11 | #import <SDWebImage/SDWebImage.h>
|
12 | 12 | #import <SDWebImageWebPCoder/SDWebImageWebPCoder.h>
|
| 13 | +#import <Expecta/Expecta.h> |
13 | 14 | #import <objc/runtime.h>
|
14 | 15 |
|
15 | 16 | const int64_t kAsyncTestTimeout = 5;
|
@@ -193,45 +194,83 @@ - (void)verifyCoder:(id<SDImageCoder>)coder
|
193 | 194 | withLocalImageURL:(NSURL *)imageUrl
|
194 | 195 | supportsEncoding:(BOOL)supportsEncoding
|
195 | 196 | isAnimatedImage:(BOOL)isAnimated {
|
| 197 | + SDImageFormat encodingFormat = SDImageFormatWebP; |
| 198 | + |
196 | 199 | NSData *inputImageData = [NSData dataWithContentsOfURL:imageUrl];
|
197 |
| - XCTAssertNotNil(inputImageData, @"Input image data should not be nil"); |
| 200 | + expect(inputImageData).toNot.beNil(); |
198 | 201 | SDImageFormat inputImageFormat = [NSData sd_imageFormatForImageData:inputImageData];
|
199 |
| - XCTAssert(inputImageFormat != SDImageFormatUndefined, @"Input image format should not be undefined"); |
| 202 | + expect(inputImageFormat).toNot.equal(SDImageFormatUndefined); |
200 | 203 |
|
201 | 204 | // 1 - check if we can decode - should be true
|
202 |
| - XCTAssertTrue([coder canDecodeFromData:inputImageData]); |
| 205 | + expect([coder canDecodeFromData:inputImageData]).to.beTruthy(); |
203 | 206 |
|
204 | 207 | // 2 - decode from NSData to UIImage and check it
|
205 | 208 | 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(); |
207 | 210 |
|
208 | 211 | if (isAnimated) {
|
209 | 212 | // 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(); |
211 | 214 |
|
212 | 215 | // 2b - check image size and scale for each frameImage (only for animated images)
|
213 | 216 | #if SD_UIKIT
|
214 | 217 | CGSize imageSize = inputImage.size;
|
215 | 218 | CGFloat imageScale = inputImage.scale;
|
216 | 219 | [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); |
219 | 222 | }];
|
220 | 223 | #endif
|
221 | 224 | }
|
222 | 225 |
|
| 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 | + |
223 | 259 | 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(); |
226 | 265 |
|
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(); |
230 | 269 | 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); |
233 | 272 | #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); |
235 | 274 | #endif
|
236 | 275 | }
|
237 | 276 | }
|
|
0 commit comments