Merge pull request #2711 from dreampiggy/test_update_coverage

Added many test case to ensure the code behavior and coverage
This commit is contained in:
Bogdan Poplauschi 2019-05-06 11:30:54 +03:00 committed by GitHub
commit 6c16889628
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 312 additions and 17 deletions

View File

@ -5,3 +5,6 @@
// Implicitly include the named header. The path given should either be a project relative path or an absolute path.
GCC_PREFIX_HEADER =
// This is a list of paths to folders to be searched by the compiler for included or imported header files when compiling C, Objective-C, C++, or Objective-C++. Paths are delimited by whitespace, so any paths with spaces in them need to be properly quoted.
HEADER_SEARCH_PATHS = $(inherited) "$(SRCROOT)/../SDWebImage/Private"

View File

@ -98,6 +98,7 @@ inline UIImage * _Nullable SDScaledImageForScaleFactor(CGFloat scale, UIImage *
if (bitmapImageRep) {
NSSize size = NSMakeSize(image.size.width / scale, image.size.height / scale);
animatedImage = [[NSImage alloc] initWithSize:size];
bitmapImageRep.size = size;
[animatedImage addRepresentation:bitmapImageRep];
}
#endif

View File

@ -10,6 +10,8 @@
1E3C51E919B46E370092B5E6 /* SDWebImageDownloaderTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 1E3C51E819B46E370092B5E6 /* SDWebImageDownloaderTests.m */; };
2D7AF0601F329763000083C2 /* SDTestCase.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D7AF05F1F329763000083C2 /* SDTestCase.m */; };
320630412085A37C006E0FA4 /* SDAnimatedImageTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 32A571552037DB2D002EDAAE /* SDAnimatedImageTest.m */; };
3222417F2272F808002429DB /* SDUtilsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3222417E2272F808002429DB /* SDUtilsTests.m */; };
322241802272F808002429DB /* SDUtilsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3222417E2272F808002429DB /* SDUtilsTests.m */; };
3226ECBB20754F7700FAFACF /* SDWebImageTestDownloadOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = 3226ECBA20754F7700FAFACF /* SDWebImageTestDownloadOperation.m */; };
3226ECBC20754F7700FAFACF /* SDWebImageTestDownloadOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = 3226ECBA20754F7700FAFACF /* SDWebImageTestDownloadOperation.m */; };
323B8E1F20862322008952BE /* SDWebImageTestLoader.m in Sources */ = {isa = PBXBuildFile; fileRef = 323B8E1E20862322008952BE /* SDWebImageTestLoader.m */; };
@ -77,6 +79,7 @@
1E3C51E819B46E370092B5E6 /* SDWebImageDownloaderTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDWebImageDownloaderTests.m; sourceTree = "<group>"; };
2D7AF05E1F329763000083C2 /* SDTestCase.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDTestCase.h; sourceTree = "<group>"; };
2D7AF05F1F329763000083C2 /* SDTestCase.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDTestCase.m; sourceTree = "<group>"; };
3222417E2272F808002429DB /* SDUtilsTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SDUtilsTests.m; sourceTree = "<group>"; };
3226ECB920754F7700FAFACF /* SDWebImageTestDownloadOperation.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SDWebImageTestDownloadOperation.h; sourceTree = "<group>"; };
3226ECBA20754F7700FAFACF /* SDWebImageTestDownloadOperation.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SDWebImageTestDownloadOperation.m; sourceTree = "<group>"; };
323B8E1D20862322008952BE /* SDWebImageTestLoader.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SDWebImageTestLoader.h; sourceTree = "<group>"; };
@ -231,6 +234,7 @@
3254C31F20641077008D1022 /* SDImageTransformerTests.m */,
4369C2731D9804B1007E863A /* SDWebCacheCategoriesTests.m */,
32B99E8A203AF8690017FD66 /* SDCategoriesTests.m */,
3222417E2272F808002429DB /* SDUtilsTests.m */,
32A571552037DB2D002EDAAE /* SDAnimatedImageTest.m */,
37D122861EC48B5E00D98CEB /* SDMockFileManager.h */,
37D122871EC48B5E00D98CEB /* SDMockFileManager.m */,
@ -489,6 +493,7 @@
32B99E9D203B2F7D0017FD66 /* SDWebImageTestCoder.m in Sources */,
32B99E9E203B2F810017FD66 /* SDMockFileManager.m in Sources */,
32B99EAB203B36620017FD66 /* SDWebImageManagerTests.m in Sources */,
322241802272F808002429DB /* SDUtilsTests.m in Sources */,
32B99EA9203B34B60017FD66 /* SDImageCoderTests.m in Sources */,
3264FF30205D42CB00F6BD48 /* SDWebImageTestTransformer.m in Sources */,
320630412085A37C006E0FA4 /* SDAnimatedImageTest.m in Sources */,
@ -512,6 +517,7 @@
2D7AF0601F329763000083C2 /* SDTestCase.m in Sources */,
328BB6DD20825E9800760D6C /* SDWebImageTestCache.m in Sources */,
4369C1D11D97F80F007E863A /* SDWebImagePrefetcherTests.m in Sources */,
3222417F2272F808002429DB /* SDUtilsTests.m in Sources */,
DA248D69195475D800390AB0 /* SDImageCacheTests.m in Sources */,
DA248D6B195476AC00390AB0 /* SDWebImageManagerTests.m in Sources */,
32B99E8B203AF8690017FD66 /* SDCategoriesTests.m in Sources */,

View File

@ -27,10 +27,6 @@ static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop coun
@implementation SDAnimatedImageTest
- (void)tearDown {
[[SDImageCache sharedImageCache] removeImageForKey:kTestGIFURL fromDisk:YES withCompletion:nil];
}
- (void)test01AnimatedImageInitWithData {
NSData *invalidData = [@"invalid data" dataUsingEncoding:NSUTF8StringEncoding];
SDAnimatedImage *image = [[SDAnimatedImage alloc] initWithData:invalidData];
@ -64,6 +60,7 @@ static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop coun
- (void)test04AnimatedImageImageNamed {
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
expect([SDAnimatedImage imageNamed:@"TestImage.gif"]).beNil(); // Not in main bundle
#if SD_UIKIT
SDAnimatedImage *image = [SDAnimatedImage imageNamed:@"TestImage.gif" inBundle:bundle compatibleWithTraitCollection:nil];
#else
@ -86,6 +83,9 @@ static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop coun
// Test one frame
UIImage *frame = [image animatedImageFrameAtIndex:0];
expect(frame).notTo.beNil();
// Unload all frames
[image unloadAllFrames];
}
- (void)test06AnimatedImageViewSetImage {
@ -115,9 +115,16 @@ static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop coun
- (void)test10AnimatedImageInitWithCoder {
SDAnimatedImage *image1 = [SDAnimatedImage imageWithContentsOfFile:[self testGIFPath]];
expect(image1).notTo.beNil();
NSData *encodedData = [NSKeyedArchiver archivedDataWithRootObject:image1];
NSMutableData *encodedData = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:encodedData];
archiver.requiresSecureCoding = YES;
[archiver encodeObject:image1 forKey:NSKeyedArchiveRootObjectKey];
[archiver finishEncoding];
expect(encodedData).notTo.beNil();
SDAnimatedImage *image2 = [NSKeyedUnarchiver unarchiveObjectWithData:encodedData];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:encodedData];
unarchiver.requiresSecureCoding = YES;
SDAnimatedImage *image2 = [unarchiver decodeObjectOfClass:SDAnimatedImage.class forKey:NSKeyedArchiveRootObjectKey];
[unarchiver finishDecoding];
expect(image2).notTo.beNil();
// Check each property
@ -232,9 +239,11 @@ static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop coun
}
- (void)test23AnimatedImageViewCategoryProgressive {
XCTestExpectation *expectation = [self expectationWithDescription:@"test SDAnimatedImageView view category"];
XCTestExpectation *expectation = [self expectationWithDescription:@"test SDAnimatedImageView view category progressive"];
SDAnimatedImageView *imageView = [SDAnimatedImageView new];
NSURL *testURL = [NSURL URLWithString:kTestGIFURL];
[SDImageCache.sharedImageCache removeImageFromMemoryForKey:testURL.absoluteString];
[SDImageCache.sharedImageCache removeImageFromDiskForKey:testURL.absoluteString];
[imageView sd_setImageWithURL:testURL placeholderImage:nil options:SDWebImageProgressiveLoad progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *image = imageView.image;
@ -250,6 +259,22 @@ static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop coun
expect(error).to.beNil();
expect(image).notTo.beNil();
expect([image isKindOfClass:[SDAnimatedImage class]]).beTruthy();
expect(cacheType).equal(SDImageCacheTypeNone);
[expectation fulfill];
}];
[self waitForExpectationsWithCommonTimeout];
}
- (void)test24AnimatedImageViewCategoryDiskCache {
XCTestExpectation *expectation = [self expectationWithDescription:@"test SDAnimatedImageView view category disk cache"];
SDAnimatedImageView *imageView = [SDAnimatedImageView new];
NSURL *testURL = [NSURL URLWithString:kTestGIFURL];
[SDImageCache.sharedImageCache removeImageFromMemoryForKey:testURL.absoluteString];
[imageView sd_setImageWithURL:testURL placeholderImage:nil completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
expect(error).to.beNil();
expect(image).notTo.beNil();
expect(cacheType).equal(SDImageCacheTypeDisk);
expect([image isKindOfClass:[SDAnimatedImage class]]).beTruthy();
[expectation fulfill];
}];
[self waitForExpectationsWithCommonTimeout];

View File

@ -26,6 +26,7 @@
// Test invalid format
CFStringRef type = [NSData sd_UTTypeFromImageFormat:SDImageFormatUndefined];
expect(CFStringCompare(kUTTypePNG, type, 0)).equal(kCFCompareEqualTo);
expect([NSData sd_imageFormatFromUTType:kUTTypeImage]).equal(SDImageFormatUndefined);
}
- (void)test02UIImageMultiFormatCategory {
@ -39,6 +40,8 @@
// Test image encode PNG
data = [image sd_imageDataAsFormat:SDImageFormatPNG];
expect(data).notTo.beNil();
// Test image decode PNG
expect([UIImage sd_imageWithData:data scale:1 firstFrameOnly:YES]).notTo.beNil();
// Test image encode JPEG with compressionQuality
NSData *jpegData1 = [image sd_imageDataAsFormat:SDImageFormatJPEG compressionQuality:1];
NSData *jpegData2 = [image sd_imageDataAsFormat:SDImageFormatJPEG compressionQuality:0.5];

View File

@ -133,6 +133,7 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
XCTFail(@"Image should not be in cache");
}
}];
[[SDImageCache sharedImageCache] storeImageToMemory:image forKey:kTestImageKeyJPEG];
[[SDImageCache sharedImageCache] clearMemory];
expect([[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:kTestImageKeyJPEG]).to.beNil();
[self waitForExpectationsWithCommonTimeout];
@ -186,6 +187,18 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
[self waitForExpectationsWithCommonTimeout];
}
- (void)test13DeleteOldFiles {
XCTestExpectation *expectation = [self expectationWithDescription:@"deleteOldFiles"];
[SDImageCache sharedImageCache].config.maxDiskAge = 1; // 1 second to mark all as out-dated
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[SDImageCache sharedImageCache] deleteOldFilesWithCompletionBlock:^{
expect(SDImageCache.sharedImageCache.totalDiskCount).equal(0);
[expectation fulfill];
}];
});
[self waitForExpectationsWithCommonTimeout];
}
- (void)test20InitialCacheSize{
expect([[SDImageCache sharedImageCache] totalDiskSize]).to.equal(0);
}
@ -257,6 +270,7 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
NSData *imageData = [image sd_imageDataAsFormat:SDImageFormatJPEG];
[[SDImageCache sharedImageCache] storeImageDataToDisk:imageData forKey:kTestImageKeyJPEG];
expect([[SDImageCache sharedImageCache] diskImageDataExistsWithKey:kTestImageKeyJPEG]).beTruthy();
UIImage *storedImageFromMemory = [[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:kTestImageKeyJPEG];
expect(storedImageFromMemory).to.equal(nil);
@ -381,6 +395,68 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
expect(exist).beTruthy();
}
- (void)test45DiskCacheRemoveExpiredData {
NSString *cachePath = [[self userCacheDirectory] stringByAppendingPathComponent:@"disk"];
SDImageCacheConfig *config = SDImageCacheConfig.defaultCacheConfig;
config.maxDiskAge = 1; // 1 second
config.maxDiskSize = 10; // 10 KB
SDDiskCache *diskCache = [[SDDiskCache alloc] initWithCachePath:cachePath config:config];
[diskCache removeAllData];
expect(diskCache.totalSize).equal(0);
expect(diskCache.totalCount).equal(0);
// 20KB -> maxDiskSize
NSUInteger length = 20;
void *bytes = malloc(length);
NSData *data = [NSData dataWithBytes:bytes length:length];
free(bytes);
[diskCache setData:data forKey:@"20KB"];
expect(diskCache.totalSize).equal(length);
expect(diskCache.totalCount).equal(1);
[diskCache removeExpiredData];
expect(diskCache.totalSize).equal(0);
expect(diskCache.totalCount).equal(0);
// 1KB with 5s -> maxDiskAge
XCTestExpectation *expectation = [self expectationWithDescription:@"SDDiskCache removeExpireData timeout"];
length = 1;
bytes = malloc(length);
data = [NSData dataWithBytes:bytes length:length];
free(bytes);
[diskCache setData:data forKey:@"1KB"];
expect(diskCache.totalSize).equal(length);
expect(diskCache.totalCount).equal(1);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[diskCache removeExpiredData];
expect(diskCache.totalSize).equal(0);
expect(diskCache.totalCount).equal(0);
[expectation fulfill];
});
[self waitForExpectationsWithTimeout:5 handler:nil];
}
#if SD_UIKIT
- (void)test46MemoryCacheWeakCache {
SDMemoryCache *memoryCache = [[SDMemoryCache alloc] init];
memoryCache.config.shouldUseWeakMemoryCache = NO;
memoryCache.config.maxMemoryCost = 10;
memoryCache.config.maxMemoryCount = 5;
expect(memoryCache.countLimit).equal(5);
expect(memoryCache.totalCostLimit).equal(10);
// Don't use weak cache
NSObject *object = [NSObject new];
[memoryCache setObject:object forKey:@"1"];
[[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationDidReceiveMemoryWarningNotification object:nil];
NSObject *cachedObject = [memoryCache objectForKey:@"1"];
expect(cachedObject).beNil();
// Use weak cache
memoryCache.config.shouldUseWeakMemoryCache = YES;
object = [NSObject new];
[memoryCache setObject:object forKey:@"1"];
[[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationDidReceiveMemoryWarningNotification object:nil];
cachedObject = [memoryCache objectForKey:@"1"];
expect(object).equal(cachedObject);
}
#endif
#pragma mark - SDImageCache & SDImageCachesManager
- (void)test50SDImageCacheQueryOp {
XCTestExpectation *expectation = [self expectationWithDescription:@"SDImageCache query op works"];

View File

@ -17,6 +17,7 @@
- (void)test01ThatDecodedImageWithNilImageReturnsNil {
expect([UIImage sd_decodedImageWithImage:nil]).to.beNil();
expect([UIImage sd_decodedAndScaledDownImageWithImage:nil]).to.beNil();
}
#if SD_UIKIT
@ -120,6 +121,15 @@
}
}
- (void)test15ThatCodersManagerWorks {
SDImageCodersManager *manager = [[SDImageCodersManager alloc] init];
manager.coders = @[SDImageIOCoder.sharedCoder];
expect([manager canDecodeFromData:nil]).beTruthy(); // Image/IO will return YES for future format
expect([manager decodedImageWithData:nil options:nil]).beNil();
expect([manager canEncodeToFormat:SDImageFormatWebP]).beFalsy();
expect([manager encodedDataWithImage:nil format:SDImageFormatUndefined options:nil]).beNil();
}
- (void)verifyCoder:(id<SDImageCoder>)coder
withLocalImageURL:(NSURL *)imageUrl
supportsEncoding:(BOOL)supportsEncoding

View File

@ -8,15 +8,9 @@
*/
#import "SDTestCase.h"
#import "UIColor+HexString.h"
#import <CoreImage/CoreImage.h>
// Internal header
@interface UIColor (HexString)
@property (nonatomic, copy, readonly, nonnull) NSString *sd_hexString;
@end
@interface SDImageTransformerTests : SDTestCase
@property (nonatomic, strong) UIImage *testImage;
@ -163,13 +157,55 @@
#endif
CGFloat borderWidth = 1;
UIColor *borderCoder = [UIColor blackColor];
BOOL horizontal = YES;
BOOL vertical = YES;
CGRect cropRect = CGRectMake(0, 0, 50, 50);
UIColor *tintColor = [UIColor clearColor];
CGFloat blurRadius = 5;
SDImageResizingTransformer *transformer1 = [SDImageResizingTransformer transformerWithSize:size scaleMode:scaleMode];
SDImageRotationTransformer *transformer2 = [SDImageRotationTransformer transformerWithAngle:angle fitSize:fitSize];
SDImageRoundCornerTransformer *transformer3 = [SDImageRoundCornerTransformer transformerWithRadius:radius corners:corners borderWidth:borderWidth borderColor:borderCoder];
SDImagePipelineTransformer *pipelineTransformer = [SDImagePipelineTransformer transformerWithTransformers:@[transformer1, transformer2, transformer3]];
SDImageFlippingTransformer *transformer4 = [SDImageFlippingTransformer transformerWithHorizontal:horizontal vertical:vertical];
SDImageCroppingTransformer *transformer5 = [SDImageCroppingTransformer transformerWithRect:cropRect];
SDImageTintTransformer *transformer6 = [SDImageTintTransformer transformerWithColor:tintColor];
SDImageBlurTransformer *transformer7 = [SDImageBlurTransformer transformerWithRadius:blurRadius];
#if SD_UIKIT || SD_MAC
CIFilter *filter = [CIFilter filterWithName:@"CIColorInvert"];
SDImageFilterTransformer *transformer8 = [SDImageFilterTransformer transformerWithFilter:filter];
#endif
// Chain all built-in transformers for test case
SDImagePipelineTransformer *pipelineTransformer = [SDImagePipelineTransformer transformerWithTransformers:@[
transformer1,
transformer2,
transformer3,
transformer4,
transformer5,
transformer6,
transformer7,
#if SD_UIKIT || SD_MAC
transformer8,
#endif
]];
NSArray *transformerKeys = @[
@"SDImageResizingTransformer({100.000000,100.000000},2)",
@"SDImageRotationTransformer(0.785398,0)",
@"SDImageRoundCornerTransformer(50.000000,18446744073709551615,1.000000,#ff000000)",
@"SDImageFlippingTransformer(1,1)",
@"SDImageCroppingTransformer({0.000000,0.000000,50.000000,50.000000})",
@"SDImageTintTransformer(#00000000)",
@"SDImageBlurTransformer(5.000000)",
#if SD_UIKIT || SD_MAC
@"SDImageFilterTransformer(CIColorInvert)",
#endif
];
NSString *transformerKey = [transformerKeys componentsJoinedByString:@"-"]; // SDImageTransformerKeySeparator
expect([pipelineTransformer.transformerKey isEqualToString:transformerKey]).beTruthy();
UIImage *transformedImage = [pipelineTransformer transformedImageWithImage:self.testImage forKey:@"Test"];
expect(CGSizeEqualToSize(transformedImage.size, size)).beTruthy();
expect(transformedImage).notTo.beNil();
expect(CGSizeEqualToSize(transformedImage.size, cropRect.size)).beTruthy();
}
- (void)test10TransformerKeyForCacheKey {

View File

@ -0,0 +1,77 @@
/*
* This file is part of the SDWebImage package.
* (c) Olivier Poitrey <rs@dailymotion.com>
* (c) Matt Galloway
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
#import "SDTestCase.h"
#import "SDWeakProxy.h"
#import "SDInternalMacros.h"
@interface SDUtilsTests : SDTestCase
@end
@implementation SDUtilsTests
- (void)testSDWeakProxy {
NSObject *object = [NSObject new];
SDWeakProxy *proxy = [SDWeakProxy proxyWithTarget:object];
SEL sel = @selector(hash);
NSMethodSignature *signature = [proxy methodSignatureForSelector:sel];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[proxy forwardInvocation:invocation];
void *returnValue;
[invocation getReturnValue:&returnValue];
expect(returnValue).beNil();
expect([((NSObject *)proxy) forwardingTargetForSelector:sel]).equal(object);
expect(proxy.isProxy).beTruthy();
expect([proxy respondsToSelector:sel]).equal([object respondsToSelector:sel]);
expect([proxy isEqual:object]).beTruthy();
expect(proxy.hash).equal(object.hash);
expect(proxy.superclass).equal(object.superclass);
expect(proxy.class).equal(object.class);
expect([proxy isKindOfClass:NSObject.class]).equal([object isKindOfClass:NSObject.class]);
expect([proxy isMemberOfClass:NSObject.class]).equal([object isMemberOfClass:NSObject.class]);
expect([proxy conformsToProtocol:@protocol(NSObject)]).equal([object conformsToProtocol:@protocol(NSObject)]);
expect([proxy.description isEqualToString:object.description]).beTruthy();
expect([proxy.debugDescription isEqualToString:object.debugDescription]).beTruthy();
}
- (void)testSDScaledImageForKey {
// Test nil
expect(SDScaledImageForKey(nil, nil)).beNil();
// Test @2x
NSData *data = [NSData dataWithContentsOfFile:[self testGIFPath]];
UIImage * image = [UIImage sd_imageWithGIFData:data];
expect(image.sd_isAnimated).beTruthy();
expect(image.scale).equal(1);
UIImage *scaledImage = SDScaledImageForKey(@"test@2x.gif", image);
expect(scaledImage.scale).equal(2);
}
- (void)testInternalMacro {
@weakify(self);
@onExit {
@strongify(self);
expect(self).notTo.beNil();
};
}
#pragma mark - Helper
- (NSString *)testJPEGPath {
NSBundle *testBundle = [NSBundle bundleForClass:[self class]];
return [testBundle pathForResource:@"TestImage" ofType:@"jpg"];
}
- (NSString *)testGIFPath {
NSBundle *testBundle = [NSBundle bundleForClass:[self class]];
return [testBundle pathForResource:@"TestImage" ofType:@"gif"];
}
@end

View File

@ -31,6 +31,7 @@
expect(imageView.image).to.equal(image);
[expectation fulfill];
}];
expect(imageView.sd_imageURL).equal(originalImageURL);
[self waitForExpectationsWithCommonTimeout];
}
@ -189,6 +190,15 @@
[self waitForExpectationsWithCommonTimeout];
}
- (void)testUIViewCancelCurrentImageLoad {
UIView *imageView = [[UIView alloc] init];
NSURL *originalImageURL = [NSURL URLWithString:kTestJPEGURL];
[imageView sd_internalSetImageWithURL:originalImageURL placeholderImage:nil options:0 context:nil setImageBlock:nil progress:nil completed:nil];
[imageView sd_cancelCurrentImageLoad];
NSString *operationKey = NSStringFromClass(UIView.class);
expect([imageView sd_imageLoadOperationForKey:operationKey]).beNil();
}
- (void)testUIViewImageProgressKVOWork {
XCTestExpectation *expectation = [self expectationWithDescription:@"UIView imageProgressKVO failed"];
UIView *view = [[UIView alloc] init];

View File

@ -179,6 +179,24 @@
[self waitForExpectationsWithCommonTimeout];
}
- (void)test11ThatCancelAllDownloadWorks {
XCTestExpectation *expectation = [self expectationWithDescription:@"CancelAllDownloads"];
NSURL *imageURL = [NSURL URLWithString:kTestJPEGURL];
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:imageURL completed:nil];
expect([SDWebImageDownloader sharedDownloader].currentDownloadCount).to.equal(1);
[[SDWebImageDownloader sharedDownloader] cancelAllDownloads];
// doesn't cancel immediately - since it uses dispatch async
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, kMinDelayNanosecond), dispatch_get_main_queue(), ^{
expect([SDWebImageDownloader sharedDownloader].currentDownloadCount).to.equal(0);
[expectation fulfill];
});
[self waitForExpectationsWithCommonTimeout];
}
- (void)test12ThatWeCanUseAnotherSessionForEachDownloadOperation {
XCTestExpectation *expectation = [self expectationWithDescription:@"Owned session"];
NSURL *url = [NSURL URLWithString:kTestJPEGURL];
@ -443,6 +461,10 @@
XCTestExpectation *expectation = [self expectationWithDescription:@"Custom image not works"];
SDWebImageTestLoader *loader = [[SDWebImageTestLoader alloc] init];
NSURL *imageURL = [NSURL URLWithString:kTestJPEGURL];
expect([loader canRequestImageForURL:imageURL]).beTruthy();
NSError *imageError = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorCancelled userInfo:nil];
expect([loader shouldBlockFailedURLWithURL:imageURL error:imageError]).equal(NO);
[loader requestImageWithURL:imageURL options:0 context:nil progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
expect(targetURL).notTo.beNil();
} completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
@ -456,8 +478,17 @@
- (void)test31ThatLoadersManagerWorks {
XCTestExpectation *expectation = [self expectationWithDescription:@"Loaders manager not works"];
SDWebImageTestLoader *loader = [[SDWebImageTestLoader alloc] init];
SDImageLoadersManager *manager = [[SDImageLoadersManager alloc] init];
[manager addLoader:loader];
[manager removeLoader:loader];
manager.loaders = @[SDWebImageDownloader.sharedDownloader, loader];
NSURL *imageURL = [NSURL URLWithString:kTestJPEGURL];
[[SDImageLoadersManager sharedManager] requestImageWithURL:imageURL options:0 context:nil progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
expect([manager canRequestImageForURL:imageURL]).beTruthy();
NSError *imageError = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorCancelled userInfo:nil];
expect([manager shouldBlockFailedURLWithURL:imageURL error:imageError]).equal(NO);
[manager requestImageWithURL:imageURL options:0 context:nil progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
expect(targetURL).notTo.beNil();
} completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
expect(error).to.beNil();

View File

@ -9,6 +9,12 @@
#import "SDTestCase.h"
@interface SDWebImagePrefetcher ()
@property (strong, atomic, nonnull) NSMutableSet<SDWebImagePrefetchToken *> *runningTokens;
@end
@interface SDWebImagePrefetcherTests : SDTestCase <SDWebImagePrefetcherDelegate>
@property (nonatomic, strong) SDWebImagePrefetcher *prefetcher;
@ -134,6 +140,17 @@
[self waitForExpectationsWithTimeout:kAsyncTestTimeout * 20 handler:nil];
}
- (void)test06PrefetchCancelToken {
NSArray *imageURLs = @[@"http://via.placeholder.com/20x20.jpg",
@"http://via.placeholder.com/30x30.jpg",
@"http://via.placeholder.com/40x40.jpg"];
SDWebImagePrefetcher *prefetcher = [[SDWebImagePrefetcher alloc] init];
SDWebImagePrefetchToken *token = [prefetcher prefetchURLs:imageURLs];
expect(prefetcher.runningTokens.count).equal(1);
[token cancel];
expect(prefetcher.runningTokens.count).equal(0);
}
- (void)imagePrefetcher:(SDWebImagePrefetcher *)imagePrefetcher didFinishWithTotalCount:(NSUInteger)totalCount skippedCount:(NSUInteger)skippedCount {
expect(imagePrefetcher).to.equal(self.prefetcher);
self.skippedCount = skippedCount;