Added many test case to ensure the code behavior. Upgrade the test coverage

This commit is contained in:
DreamPiggy 2019-04-26 19:03:29 +08:00
parent abdcc11461
commit 873d9eddce
9 changed files with 228 additions and 3 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. // Implicitly include the named header. The path given should either be a project relative path or an absolute path.
GCC_PREFIX_HEADER = 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

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

View File

@ -64,6 +64,7 @@ static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop coun
- (void)test04AnimatedImageImageNamed { - (void)test04AnimatedImageImageNamed {
NSBundle *bundle = [NSBundle bundleForClass:[self class]]; NSBundle *bundle = [NSBundle bundleForClass:[self class]];
expect([SDAnimatedImage imageNamed:@"TestImage.gif"]).beNil(); // Not in main bundle
#if SD_UIKIT #if SD_UIKIT
SDAnimatedImage *image = [SDAnimatedImage imageNamed:@"TestImage.gif" inBundle:bundle compatibleWithTraitCollection:nil]; SDAnimatedImage *image = [SDAnimatedImage imageNamed:@"TestImage.gif" inBundle:bundle compatibleWithTraitCollection:nil];
#else #else
@ -86,6 +87,9 @@ static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop coun
// Test one frame // Test one frame
UIImage *frame = [image animatedImageFrameAtIndex:0]; UIImage *frame = [image animatedImageFrameAtIndex:0];
expect(frame).notTo.beNil(); expect(frame).notTo.beNil();
// Unload all frames
[image unloadAllFrames];
} }
- (void)test06AnimatedImageViewSetImage { - (void)test06AnimatedImageViewSetImage {
@ -117,7 +121,9 @@ static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop coun
expect(image1).notTo.beNil(); expect(image1).notTo.beNil();
NSData *encodedData = [NSKeyedArchiver archivedDataWithRootObject:image1]; NSData *encodedData = [NSKeyedArchiver archivedDataWithRootObject:image1];
expect(encodedData).notTo.beNil(); 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];
expect(image2).notTo.beNil(); expect(image2).notTo.beNil();
// Check each property // Check each property

View File

@ -186,6 +186,18 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
[self waitForExpectationsWithCommonTimeout]; [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{ - (void)test20InitialCacheSize{
expect([[SDImageCache sharedImageCache] totalDiskSize]).to.equal(0); expect([[SDImageCache sharedImageCache] totalDiskSize]).to.equal(0);
} }
@ -381,6 +393,68 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
expect(exist).beTruthy(); 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 #pragma mark - SDImageCache & SDImageCachesManager
- (void)test50SDImageCacheQueryOp { - (void)test50SDImageCacheQueryOp {
XCTestExpectation *expectation = [self expectationWithDescription:@"SDImageCache query op works"]; XCTestExpectation *expectation = [self expectationWithDescription:@"SDImageCache query op works"];

View File

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

View File

@ -163,13 +163,40 @@
#endif #endif
CGFloat borderWidth = 1; CGFloat borderWidth = 1;
UIColor *borderCoder = [UIColor blackColor]; UIColor *borderCoder = [UIColor blackColor];
BOOL horizontal = YES;
BOOL vertical = YES;
CGRect cropRect = CGRectMake(0, 0, 50, 50);
UIColor *tintColor = [UIColor clearColor];
CGFloat blurRadius = 5;
CIFilter *filter = [CIFilter filterWithName:@"CIColorInvert"];
SDImageResizingTransformer *transformer1 = [SDImageResizingTransformer transformerWithSize:size scaleMode:scaleMode]; SDImageResizingTransformer *transformer1 = [SDImageResizingTransformer transformerWithSize:size scaleMode:scaleMode];
SDImageRotationTransformer *transformer2 = [SDImageRotationTransformer transformerWithAngle:angle fitSize:fitSize]; SDImageRotationTransformer *transformer2 = [SDImageRotationTransformer transformerWithAngle:angle fitSize:fitSize];
SDImageRoundCornerTransformer *transformer3 = [SDImageRoundCornerTransformer transformerWithRadius:radius corners:corners borderWidth:borderWidth borderColor:borderCoder]; 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];
SDImageFilterTransformer *transformer8 = [SDImageFilterTransformer transformerWithFilter:filter];
// Chain all built-in transformers for test case
SDImagePipelineTransformer *pipelineTransformer = [SDImagePipelineTransformer transformerWithTransformers:@[transformer1, transformer2, transformer3, transformer4, transformer5, transformer6, transformer7, transformer8]];
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)",
@"SDImageFilterTransformer(CIColorInvert)"
];
NSString *transformerKey = [transformerKeys componentsJoinedByString:@"-"]; // SDImageTransformerKeySeparator
expect([pipelineTransformer.transformerKey isEqualToString:transformerKey]).beTruthy();
UIImage *transformedImage = [pipelineTransformer transformedImageWithImage:self.testImage forKey:@"Test"]; 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 { - (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); expect(imageView.image).to.equal(image);
[expectation fulfill]; [expectation fulfill];
}]; }];
expect(imageView.sd_imageURL).equal(originalImageURL);
[self waitForExpectationsWithCommonTimeout]; [self waitForExpectationsWithCommonTimeout];
} }
@ -189,6 +190,16 @@
[self waitForExpectationsWithCommonTimeout]; [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();
[imageView sd_cancelImageLoadOperationWithKey:operationKey];
}
- (void)testUIViewImageProgressKVOWork { - (void)testUIViewImageProgressKVOWork {
XCTestExpectation *expectation = [self expectationWithDescription:@"UIView imageProgressKVO failed"]; XCTestExpectation *expectation = [self expectationWithDescription:@"UIView imageProgressKVO failed"];
UIView *view = [[UIView alloc] init]; UIView *view = [[UIView alloc] init];

View File

@ -443,6 +443,10 @@
XCTestExpectation *expectation = [self expectationWithDescription:@"Custom image not works"]; XCTestExpectation *expectation = [self expectationWithDescription:@"Custom image not works"];
SDWebImageTestLoader *loader = [[SDWebImageTestLoader alloc] init]; SDWebImageTestLoader *loader = [[SDWebImageTestLoader alloc] init];
NSURL *imageURL = [NSURL URLWithString:kTestJPEGURL]; 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) { [loader requestImageWithURL:imageURL options:0 context:nil progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
expect(targetURL).notTo.beNil(); expect(targetURL).notTo.beNil();
} completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) { } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
@ -456,7 +460,15 @@
- (void)test31ThatLoadersManagerWorks { - (void)test31ThatLoadersManagerWorks {
XCTestExpectation *expectation = [self expectationWithDescription:@"Loaders manager not works"]; XCTestExpectation *expectation = [self expectationWithDescription:@"Loaders manager not works"];
SDWebImageTestLoader *loader = [[SDWebImageTestLoader alloc] init];
[[SDImageLoadersManager sharedManager] addLoader:loader];
[[SDImageLoadersManager sharedManager] removeLoader:loader];
[SDImageLoadersManager sharedManager].loaders = @[SDWebImageDownloader.sharedDownloader, loader];
NSURL *imageURL = [NSURL URLWithString:kTestJPEGURL]; NSURL *imageURL = [NSURL URLWithString:kTestJPEGURL];
expect([[SDImageLoadersManager sharedManager] canRequestImageForURL:imageURL]).beTruthy();
NSError *imageError = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorCancelled userInfo:nil];
expect([loader shouldBlockFailedURLWithURL:imageURL error:imageError]).equal(NO);
[[SDImageLoadersManager sharedManager] requestImageWithURL:imageURL options:0 context:nil progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) { [[SDImageLoadersManager sharedManager] requestImageWithURL:imageURL options:0 context:nil progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
expect(targetURL).notTo.beNil(); expect(targetURL).notTo.beNil();
} completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) { } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {