|
| 1 | +/* |
| 2 | + * This file is part of the SDWebImage package. |
| 3 | + * (c) Olivier Poitrey <[email protected]> |
| 4 | + * |
| 5 | + * For the full copyright and license information, please view the LICENSE |
| 6 | + * file that was distributed with this source code. |
| 7 | + */ |
| 8 | + |
| 9 | +@import Foundation; |
| 10 | +@import XCTest; |
| 11 | +#import <SDWebImage/SDWebImage.h> |
| 12 | +#import <SDWebImageWebPCoder/SDWebImageWebPCoder.h> |
| 13 | + |
| 14 | +const int64_t kAsyncTestTimeout = 5; |
| 15 | + |
| 16 | +@interface SDWebImageWebPCoderTests : XCTestCase |
| 17 | +@end |
| 18 | + |
| 19 | +@interface SDWebImageWebPCoderTests (Helpers) |
| 20 | +- (void)verifyCoder:(id<SDImageCoder>)coder |
| 21 | + withLocalImageURL:(NSURL *)imageUrl |
| 22 | + supportsEncoding:(BOOL)supportsEncoding |
| 23 | + isAnimatedImage:(BOOL)isAnimated; |
| 24 | +@end |
| 25 | + |
| 26 | +// Internal header |
| 27 | +@interface SDAnimatedImageView () |
| 28 | +@property (nonatomic, assign) BOOL isProgressive; |
| 29 | +@end |
| 30 | + |
| 31 | +@implementation SDWebImageWebPCoderTests |
| 32 | + |
| 33 | ++ (void)setUp { |
| 34 | + [[SDImageCodersManager sharedManager] addCoder:[SDImageWebPCoder sharedCoder]]; |
| 35 | +} |
| 36 | + |
| 37 | ++ (void)tearDown { |
| 38 | + [[SDImageCodersManager sharedManager] removeCoder:[SDImageWebPCoder sharedCoder]]; |
| 39 | +} |
| 40 | + |
| 41 | +- (void)test01ThatWEBPWorks { |
| 42 | + XCTestExpectation *expectation = [self expectationWithDescription:@"WEBP"]; |
| 43 | + NSURL *imageURL = [NSURL URLWithString:@"http://www.ioncannon.net/wp-content/uploads/2011/06/test2.webp"]; |
| 44 | + [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:imageURL options:0 progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) { |
| 45 | + if (image && data && !error && finished) { |
| 46 | + [expectation fulfill]; |
| 47 | + } else { |
| 48 | + XCTFail(@"Something went wrong"); |
| 49 | + } |
| 50 | + }]; |
| 51 | + [self waitForExpectationsWithTimeout:kAsyncTestTimeout handler:nil]; |
| 52 | +} |
| 53 | + |
| 54 | +- (void)test02ThatProgressiveWebPWorks { |
| 55 | + XCTestExpectation *expectation = [self expectationWithDescription:@"Progressive WebP download"]; |
| 56 | + NSURL *imageURL = [NSURL URLWithString:@"http://www.ioncannon.net/wp-content/uploads/2011/06/test9.webp"]; |
| 57 | + [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:imageURL options:SDWebImageDownloaderProgressiveLoad progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) { |
| 58 | + if (image && data && !error && finished) { |
| 59 | + [expectation fulfill]; |
| 60 | + } else if (finished) { |
| 61 | + XCTFail(@"Something went wrong"); |
| 62 | + } else { |
| 63 | + // progressive updates |
| 64 | + } |
| 65 | + }]; |
| 66 | + [self waitForExpectationsWithTimeout:kAsyncTestTimeout handler:nil]; |
| 67 | +} |
| 68 | + |
| 69 | +- (void)test11ThatStaticWebPCoderWorks { |
| 70 | + NSURL *staticWebPURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"TestImageStatic" withExtension:@"webp"]; |
| 71 | + [self verifyCoder:[SDImageWebPCoder sharedCoder] |
| 72 | + withLocalImageURL:staticWebPURL |
| 73 | + supportsEncoding:YES |
| 74 | + isAnimatedImage:NO]; |
| 75 | +} |
| 76 | + |
| 77 | +- (void)test12ThatAnimatedWebPCoderWorks { |
| 78 | + NSURL *animatedWebPURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"TestImageAnimated" withExtension:@"webp"]; |
| 79 | + [self verifyCoder:[SDImageWebPCoder sharedCoder] |
| 80 | + withLocalImageURL:animatedWebPURL |
| 81 | + supportsEncoding:YES |
| 82 | + isAnimatedImage:YES]; |
| 83 | +} |
| 84 | + |
| 85 | +- (void)test21UIImageWebPCategory { |
| 86 | + // Test invalid image data |
| 87 | + UIImage *image = [UIImage sd_imageWithWebPData:nil]; |
| 88 | + XCTAssertNil(image); |
| 89 | + // Test valid image data |
| 90 | + NSURL *staticWebPURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"TestImageStatic" withExtension:@"webp"]; |
| 91 | + NSData *data = [NSData dataWithContentsOfURL:staticWebPURL]; |
| 92 | + image = [UIImage sd_imageWithWebPData:data]; |
| 93 | + XCTAssertNotNil(image); |
| 94 | +} |
| 95 | + |
| 96 | +- (void)test31AnimatedImageViewSetAnimatedImageWEBP { |
| 97 | + SDAnimatedImageView *imageView = [SDAnimatedImageView new]; |
| 98 | + NSURL *animatedWebPURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"TestImageAnimated" withExtension:@"webp"]; |
| 99 | + NSData *animatedImageData = [NSData dataWithContentsOfURL:animatedWebPURL]; |
| 100 | + SDAnimatedImage *image = [SDAnimatedImage imageWithData:animatedImageData]; |
| 101 | + imageView.image = image; |
| 102 | + XCTAssertNotNil(imageView.image); |
| 103 | + XCTAssertNotNil(imageView.currentFrame); // current frame |
| 104 | +} |
| 105 | + |
| 106 | +- (void)test32AnimatedImageViewCategoryProgressive { |
| 107 | + XCTestExpectation *expectation = [self expectationWithDescription:@"test SDAnimatedImageView view category"]; |
| 108 | + SDAnimatedImageView *imageView = [SDAnimatedImageView new]; |
| 109 | + NSURL *testURL = [NSURL URLWithString:@"http://littlesvr.ca/apng/images/SteamEngine.webp"]; |
| 110 | + [imageView sd_setImageWithURL:testURL placeholderImage:nil options:SDWebImageProgressiveLoad progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) { |
| 111 | + dispatch_async(dispatch_get_main_queue(), ^{ |
| 112 | + UIImage *image = imageView.image; |
| 113 | + // Progressive image may be nil when download data is not enough |
| 114 | + if (image) { |
| 115 | + XCTAssertTrue(image.sd_isIncremental); |
| 116 | + XCTAssertTrue([image conformsToProtocol:@protocol(SDAnimatedImage)]); |
| 117 | + XCTAssertTrue(imageView.isProgressive); |
| 118 | + } |
| 119 | + }); |
| 120 | + } completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) { |
| 121 | + XCTAssertNil(error); |
| 122 | + XCTAssertNotNil(image); |
| 123 | + XCTAssertTrue([image isKindOfClass:[SDAnimatedImage class]]); |
| 124 | + [expectation fulfill]; |
| 125 | + }]; |
| 126 | + [self waitForExpectationsWithTimeout:kAsyncTestTimeout handler:nil]; |
| 127 | +} |
| 128 | + |
| 129 | + |
| 130 | +@end |
| 131 | + |
| 132 | +@implementation SDWebImageWebPCoderTests (Helpers) |
| 133 | + |
| 134 | +- (void)verifyCoder:(id<SDImageCoder>)coder |
| 135 | + withLocalImageURL:(NSURL *)imageUrl |
| 136 | + supportsEncoding:(BOOL)supportsEncoding |
| 137 | + isAnimatedImage:(BOOL)isAnimated { |
| 138 | + NSData *inputImageData = [NSData dataWithContentsOfURL:imageUrl]; |
| 139 | + XCTAssertNotNil(inputImageData, @"Input image data should not be nil"); |
| 140 | + SDImageFormat inputImageFormat = [NSData sd_imageFormatForImageData:inputImageData]; |
| 141 | + XCTAssert(inputImageFormat != SDImageFormatUndefined, @"Input image format should not be undefined"); |
| 142 | + |
| 143 | + // 1 - check if we can decode - should be true |
| 144 | + XCTAssertTrue([coder canDecodeFromData:inputImageData]); |
| 145 | + |
| 146 | + // 2 - decode from NSData to UIImage and check it |
| 147 | + UIImage *inputImage = [coder decodedImageWithData:inputImageData options:nil]; |
| 148 | + XCTAssertNotNil(inputImage, @"The decoded image from input data should not be nil"); |
| 149 | + |
| 150 | + if (isAnimated) { |
| 151 | + // 2a - check images count > 0 (only for animated images) |
| 152 | + XCTAssertTrue(inputImage.sd_isAnimated, @"The decoded image should be animated"); |
| 153 | + |
| 154 | + // 2b - check image size and scale for each frameImage (only for animated images) |
| 155 | +#if SD_UIKIT |
| 156 | + CGSize imageSize = inputImage.size; |
| 157 | + CGFloat imageScale = inputImage.scale; |
| 158 | + [inputImage.images enumerateObjectsUsingBlock:^(UIImage * frameImage, NSUInteger idx, BOOL * stop) { |
| 159 | + XCTAssertTrue(CGSizeEqualToSize(imageSize, frameImage.size), @"Each frame size should match the image size"); |
| 160 | + XCTAssertEqual(imageScale, frameImage.scale, @"Each frame scale should match the image scale"); |
| 161 | + }]; |
| 162 | +#endif |
| 163 | + } |
| 164 | + |
| 165 | + if (supportsEncoding) { |
| 166 | + // 3 - check if we can encode to the original format |
| 167 | + XCTAssertTrue([coder canEncodeToFormat:inputImageFormat], @"Coder should be able to encode"); |
| 168 | + |
| 169 | + // 4 - encode from UIImage to NSData using the inputImageFormat and check it |
| 170 | + NSData *outputImageData = [coder encodedDataWithImage:inputImage format:inputImageFormat options:nil]; |
| 171 | + XCTAssertNotNil(outputImageData, @"The encoded image data should not be nil"); |
| 172 | + UIImage *outputImage = [coder decodedImageWithData:outputImageData options:nil]; |
| 173 | + XCTAssertTrue(CGSizeEqualToSize(outputImage.size, inputImage.size), @"Output and input image size should match"); |
| 174 | + XCTAssertEqual(outputImage.scale, inputImage.scale, @"Output and input image scale should match"); |
| 175 | +#if SD_UIKIT |
| 176 | + XCTAssertEqual(outputImage.images.count, inputImage.images.count, @"Output and input image frame count should match"); |
| 177 | +#endif |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +@end |
0 commit comments