Fix all cache test

This commit is contained in:
DreamPiggy 2018-04-18 19:23:45 +08:00
parent 734c6821d5
commit a6bdba724e
7 changed files with 60 additions and 60 deletions

View File

@ -324,8 +324,8 @@ typedef NS_OPTIONS(NSUInteger, SDImageCacheOptions) {
@end
/**
* SDImageCache is the built-in image cache implementation for web image manager. It adopts `SDWebImageCache` protocol to provide the function for web image manager to use for image loading process.
* SDImageCache is the built-in image cache implementation for web image manager. It adopts `SDImageCache` protocol to provide the function for web image manager to use for image loading process.
*/
@interface SDImageCache (SDWebImageCache) <SDImageCache>
@interface SDImageCache (SDImageCache) <SDImageCache>
@end

View File

@ -59,10 +59,10 @@
diskCacheDirectory:(nonnull NSString *)directory
config:(nullable SDImageCacheConfig *)config {
if ((self = [super init])) {
NSString *fullNamespace = [@"com.hackemist.SDWebImageCache." stringByAppendingString:ns];
NSString *fullNamespace = [@"com.hackemist.SDImageCache." stringByAppendingString:ns];
// Create IO serial queue
_ioQueue = dispatch_queue_create("com.hackemist.SDWebImageCache", DISPATCH_QUEUE_SERIAL);
_ioQueue = dispatch_queue_create("com.hackemist.SDImageCache", DISPATCH_QUEUE_SERIAL);
if (!config) {
config = SDImageCacheConfig.defaultCacheConfig;
@ -317,7 +317,7 @@
- (nullable UIImage *)diskImageForKey:(nullable NSString *)key data:(nullable NSData *)data options:(SDImageCacheOptions)options context:(SDWebImageContext *)context {
if (data) {
UIImage *image = SDWebImageCacheDecodeImageData(data, key, [[self class] imageOptionsFromCacheOptions:options], context);
UIImage *image = SDImageCacheDecodeImageData(data, key, [[self class] imageOptionsFromCacheOptions:options], context);
return image;
} else {
return nil;
@ -568,9 +568,9 @@
@end
@implementation SDImageCache (SDWebImageCache)
@implementation SDImageCache (SDImageCache)
#pragma mark - SDWebImageCache
#pragma mark - SDImageCache
- (id<SDWebImageOperation>)queryImageForKey:(NSString *)key options:(SDWebImageOptions)options context:(nullable SDWebImageContext *)context completion:(nullable SDImageCacheQueryCompletionBlock)completionBlock {
SDImageCacheOptions cacheOptions = 0;

View File

@ -50,12 +50,12 @@ typedef void(^SDImageCacheContainsCompletionBlock)(SDImageCacheType containsCach
@param context The context arg from the input
@return The decoded image for current image data query from cache
*/
FOUNDATION_EXPORT UIImage * _Nullable SDWebImageCacheDecodeImageData(NSData * _Nonnull imageData, NSString * _Nonnull cacheKey, SDWebImageOptions options, SDWebImageContext * _Nullable context);
FOUNDATION_EXPORT UIImage * _Nullable SDImageCacheDecodeImageData(NSData * _Nonnull imageData, NSString * _Nonnull cacheKey, SDWebImageOptions options, SDWebImageContext * _Nullable context);
/**
This is the image cache protocol to provide custom image cache for `SDWebImageManager`.
Though the best practice to custom image cache, is to write your own class which conform `SDMemoryCache` or `SDDiskCache` protocol for `SDImageCache` class (See more on `SDImageCacheConfig.memoryCacheClass & SDImageCacheConfig.diskCacheClass`).
However, if your own cache implementation contains more advanced feature beyond `SDImageCache` itself, you can consider to provide this instead. For example, you can even use a cache manager like `SDWebImageCachesManager` to register multiple caches.
However, if your own cache implementation contains more advanced feature beyond `SDImageCache` itself, you can consider to provide this instead. For example, you can even use a cache manager like `SDImageCachesManager` to register multiple caches.
*/
@protocol SDImageCache <NSObject>

View File

@ -12,7 +12,7 @@
#import "SDAnimatedImage.h"
#import "UIImage+WebCache.h"
UIImage * _Nullable SDWebImageCacheDecodeImageData(NSData * _Nonnull imageData, NSString * _Nonnull cacheKey, SDWebImageOptions options, SDWebImageContext * _Nullable context) {
UIImage * _Nullable SDImageCacheDecodeImageData(NSData * _Nonnull imageData, NSString * _Nonnull cacheKey, SDWebImageOptions options, SDWebImageContext * _Nullable context) {
UIImage *image;
BOOL decodeFirstFrame = options & SDWebImageDecodeFirstFrameOnly;
NSNumber *scaleValue = [context valueForKey:SDWebImageContextImageScaleFactor];

View File

@ -116,7 +116,7 @@
self.caches = [mutableCaches copy];
}
#pragma mark - SDWebImageCache
#pragma mark - SDImageCache
- (id<SDWebImageOperation>)queryImageForKey:(NSString *)key options:(SDWebImageOptions)options context:(SDWebImageContext *)context completion:(SDImageCacheQueryCompletionBlock)completionBlock {
if (!key) {

View File

@ -9,7 +9,7 @@
#import "SDTestCase.h"
#import <SDWebImage/SDImageCache.h>
#import <SDWebImage/SDWebImageCodersManager.h>
#import <SDWebImage/SDWebImageCachesManager.h>
#import <SDWebImage/SDImageCachesManager.h>
#import "SDWebImageTestDecoder.h"
#import "SDMockFileManager.h"
#import "SDWebImageTestCache.h"
@ -30,11 +30,11 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
@implementation SDImageCacheTests
+ (void)setUp {
[[SDWebImageCachesManager sharedManager] addCache:[SDImageCache sharedImageCache]];
[[SDImageCachesManager sharedManager] addCache:[SDImageCache sharedImageCache]];
}
+ (void)tearDown {
[[SDWebImageCachesManager sharedManager] removeCache:[SDImageCache sharedImageCache]];
[[SDImageCachesManager sharedManager] removeCache:[SDImageCache sharedImageCache]];
}
- (void)test01SharedImageCache {
@ -362,20 +362,20 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
expect([diskCache isKindOfClass:[SDWebImageTestDiskCache class]]).to.beTruthy();
}
#pragma mark - SDWebImageCache & SDWebImageCachesManager
- (void)test50SDWebImageCacheQueryOp {
XCTestExpectation *expectation = [self expectationWithDescription:@"SDWebImageCache query op works"];
#pragma mark - SDImageCache & SDImageCachesManager
- (void)test50SDImageCacheQueryOp {
XCTestExpectation *expectation = [self expectationWithDescription:@"SDImageCache query op works"];
[[SDImageCache sharedImageCache] storeImage:[self testJPEGImage] forKey:kTestImageKeyJPEG toDisk:NO completion:nil];
[[SDWebImageCachesManager sharedManager] queryImageForKey:kTestImageKeyJPEG options:0 context:nil completion:^(UIImage * _Nullable image, NSData * _Nullable data, SDImageCacheType cacheType) {
[[SDImageCachesManager sharedManager] queryImageForKey:kTestImageKeyJPEG options:0 context:nil completion:^(UIImage * _Nullable image, NSData * _Nullable data, SDImageCacheType cacheType) {
expect(image).notTo.beNil();
[expectation fulfill];
}];
[self waitForExpectationsWithCommonTimeout];
}
- (void)test51SDWebImageCacheStoreOp {
XCTestExpectation *expectation = [self expectationWithDescription:@"SDWebImageCache store op works"];
[[SDWebImageCachesManager sharedManager] storeImage:[self testJPEGImage] imageData:nil forKey:kTestImageKeyJPEG cacheType:SDImageCacheTypeAll completion:^{
- (void)test51SDImageCacheStoreOp {
XCTestExpectation *expectation = [self expectationWithDescription:@"SDImageCache store op works"];
[[SDImageCachesManager sharedManager] storeImage:[self testJPEGImage] imageData:nil forKey:kTestImageKeyJPEG cacheType:SDImageCacheTypeAll completion:^{
UIImage *image = [[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:kTestImageKeyJPEG];
expect(image).notTo.beNil();
[[SDImageCache sharedImageCache] diskImageExistsWithKey:kTestImageKeyJPEG completion:^(BOOL isInCache) {
@ -386,9 +386,9 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
[self waitForExpectationsWithCommonTimeout];
}
- (void)test52SDWebImageCacheRemoveOp {
XCTestExpectation *expectation = [self expectationWithDescription:@"SDWebImageCache remove op works"];
[[SDWebImageCachesManager sharedManager] removeImageForKey:kTestImageKeyJPEG cacheType:SDImageCacheTypeDisk completion:^{
- (void)test52SDImageCacheRemoveOp {
XCTestExpectation *expectation = [self expectationWithDescription:@"SDImageCache remove op works"];
[[SDImageCachesManager sharedManager] removeImageForKey:kTestImageKeyJPEG cacheType:SDImageCacheTypeDisk completion:^{
UIImage *memoryImage = [[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:kTestImageKeyJPEG];
expect(memoryImage).notTo.beNil();
[[SDImageCache sharedImageCache] diskImageExistsWithKey:kTestImageKeyJPEG completion:^(BOOL isInCache) {
@ -399,18 +399,18 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
[self waitForExpectationsWithCommonTimeout];
}
- (void)test53SDWebImageCacheContainsOp {
XCTestExpectation *expectation = [self expectationWithDescription:@"SDWebImageCache contains op works"];
[[SDWebImageCachesManager sharedManager] containsImageForKey:kTestImageKeyJPEG cacheType:SDImageCacheTypeAll completion:^(SDImageCacheType containsCacheType) {
- (void)test53SDImageCacheContainsOp {
XCTestExpectation *expectation = [self expectationWithDescription:@"SDImageCache contains op works"];
[[SDImageCachesManager sharedManager] containsImageForKey:kTestImageKeyJPEG cacheType:SDImageCacheTypeAll completion:^(SDImageCacheType containsCacheType) {
expect(containsCacheType).equal(SDImageCacheTypeMemory);
[expectation fulfill];
}];
[self waitForExpectationsWithCommonTimeout];
}
- (void)test54SDWebImageCacheClearOp {
XCTestExpectation *expectation = [self expectationWithDescription:@"SDWebImageCache clear op works"];
[[SDWebImageCachesManager sharedManager] clearWithCacheType:SDImageCacheTypeAll completion:^{
- (void)test54SDImageCacheClearOp {
XCTestExpectation *expectation = [self expectationWithDescription:@"SDImageCache clear op works"];
[[SDImageCachesManager sharedManager] clearWithCacheType:SDImageCacheTypeAll completion:^{
UIImage *memoryImage = [[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:kTestImageKeyJPEG];
expect(memoryImage).to.beNil();
[[SDImageCache sharedImageCache] diskImageExistsWithKey:kTestImageKeyJPEG completion:^(BOOL isInCache) {
@ -421,8 +421,8 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
[self waitForExpectationsWithCommonTimeout];
}
- (void)test55SDWebImageCachesManagerOperationPolicySimple {
SDWebImageCachesManager *cachesManager = [[SDWebImageCachesManager alloc] init];
- (void)test55SDImageCachesManagerOperationPolicySimple {
SDImageCachesManager *cachesManager = [[SDImageCachesManager alloc] init];
SDImageCache *cache1 = [[SDImageCache alloc] initWithNamespace:@"cache1"];
SDImageCache *cache2 = [[SDImageCache alloc] initWithNamespace:@"cache2"];
[cachesManager addCache:cache1];
@ -432,11 +432,11 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
[[NSFileManager defaultManager] removeItemAtPath:cache2.diskCachePath error:nil];
// LowestOnly
cachesManager.queryOperationPolicy = SDWebImageCachesManagerOperationPolicyLowestOnly;
cachesManager.storeOperationPolicy = SDWebImageCachesManagerOperationPolicyLowestOnly;
cachesManager.removeOperationPolicy = SDWebImageCachesManagerOperationPolicyLowestOnly;
cachesManager.containsOperationPolicy = SDWebImageCachesManagerOperationPolicyLowestOnly;
cachesManager.clearOperationPolicy = SDWebImageCachesManagerOperationPolicyLowestOnly;
cachesManager.queryOperationPolicy = SDImageCachesManagerOperationPolicyLowestOnly;
cachesManager.storeOperationPolicy = SDImageCachesManagerOperationPolicyLowestOnly;
cachesManager.removeOperationPolicy = SDImageCachesManagerOperationPolicyLowestOnly;
cachesManager.containsOperationPolicy = SDImageCachesManagerOperationPolicyLowestOnly;
cachesManager.clearOperationPolicy = SDImageCachesManagerOperationPolicyLowestOnly;
[cachesManager queryImageForKey:kTestImageKeyJPEG options:0 context:nil completion:^(UIImage * _Nullable image, NSData * _Nullable data, SDImageCacheType cacheType) {
expect(image).to.beNil();
}];
@ -451,11 +451,11 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
[cachesManager clearWithCacheType:SDImageCacheTypeMemory completion:nil];
// HighestOnly
cachesManager.queryOperationPolicy = SDWebImageCachesManagerOperationPolicyHighestOnly;
cachesManager.storeOperationPolicy = SDWebImageCachesManagerOperationPolicyHighestOnly;
cachesManager.removeOperationPolicy = SDWebImageCachesManagerOperationPolicyHighestOnly;
cachesManager.containsOperationPolicy = SDWebImageCachesManagerOperationPolicyHighestOnly;
cachesManager.clearOperationPolicy = SDWebImageCachesManagerOperationPolicyHighestOnly;
cachesManager.queryOperationPolicy = SDImageCachesManagerOperationPolicyHighestOnly;
cachesManager.storeOperationPolicy = SDImageCachesManagerOperationPolicyHighestOnly;
cachesManager.removeOperationPolicy = SDImageCachesManagerOperationPolicyHighestOnly;
cachesManager.containsOperationPolicy = SDImageCachesManagerOperationPolicyHighestOnly;
cachesManager.clearOperationPolicy = SDImageCachesManagerOperationPolicyHighestOnly;
[cachesManager queryImageForKey:kTestImageKeyPNG options:0 context:nil completion:^(UIImage * _Nullable image, NSData * _Nullable data, SDImageCacheType cacheType) {
expect(image).to.beNil();
}];
@ -470,9 +470,9 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
[cachesManager clearWithCacheType:SDImageCacheTypeMemory completion:nil];
}
- (void)test56SDWebImageCachesManagerOperationPolicyConcurrent {
XCTestExpectation *expectation = [self expectationWithDescription:@"SDWebImageCachesManager operation cocurrent policy works"];
SDWebImageCachesManager *cachesManager = [[SDWebImageCachesManager alloc] init];
- (void)test56SDImageCachesManagerOperationPolicyConcurrent {
XCTestExpectation *expectation = [self expectationWithDescription:@"SDImageCachesManager operation cocurrent policy works"];
SDImageCachesManager *cachesManager = [[SDImageCachesManager alloc] init];
SDImageCache *cache1 = [[SDImageCache alloc] initWithNamespace:@"cache1"];
SDImageCache *cache2 = [[SDImageCache alloc] initWithNamespace:@"cache2"];
[cachesManager addCache:cache1];
@ -485,11 +485,11 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
// Cocurrent
// Check all concurrent op
cachesManager.queryOperationPolicy = SDWebImageCachesManagerOperationPolicyConcurrent;
cachesManager.storeOperationPolicy = SDWebImageCachesManagerOperationPolicyConcurrent;
cachesManager.removeOperationPolicy = SDWebImageCachesManagerOperationPolicyConcurrent;
cachesManager.containsOperationPolicy = SDWebImageCachesManagerOperationPolicyConcurrent;
cachesManager.clearOperationPolicy = SDWebImageCachesManagerOperationPolicyConcurrent;
cachesManager.queryOperationPolicy = SDImageCachesManagerOperationPolicyConcurrent;
cachesManager.storeOperationPolicy = SDImageCachesManagerOperationPolicyConcurrent;
cachesManager.removeOperationPolicy = SDImageCachesManagerOperationPolicyConcurrent;
cachesManager.containsOperationPolicy = SDImageCachesManagerOperationPolicyConcurrent;
cachesManager.clearOperationPolicy = SDImageCachesManagerOperationPolicyConcurrent;
[cachesManager queryImageForKey:kConcurrentTestImageKey options:0 context:nil completion:nil];
[cachesManager storeImage:[self testJPEGImage] imageData:nil forKey:kConcurrentTestImageKey cacheType:SDImageCacheTypeMemory completion:nil];
[cachesManager removeImageForKey:kConcurrentTestImageKey cacheType:SDImageCacheTypeMemory completion:nil];
@ -511,9 +511,9 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
[self waitForExpectationsWithCommonTimeout];
}
- (void)test57SDWebImageCachesManagerOperationPolicySerial {
XCTestExpectation *expectation = [self expectationWithDescription:@"SDWebImageCachesManager operation serial policy works"];
SDWebImageCachesManager *cachesManager = [[SDWebImageCachesManager alloc] init];
- (void)test57SDImageCachesManagerOperationPolicySerial {
XCTestExpectation *expectation = [self expectationWithDescription:@"SDImageCachesManager operation serial policy works"];
SDImageCachesManager *cachesManager = [[SDImageCachesManager alloc] init];
SDImageCache *cache1 = [[SDImageCache alloc] initWithNamespace:@"cache1"];
SDImageCache *cache2 = [[SDImageCache alloc] initWithNamespace:@"cache2"];
[cachesManager addCache:cache1];
@ -526,11 +526,11 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
// Serial
// Check all serial op
cachesManager.queryOperationPolicy = SDWebImageCachesManagerOperationPolicySerial;
cachesManager.storeOperationPolicy = SDWebImageCachesManagerOperationPolicySerial;
cachesManager.removeOperationPolicy = SDWebImageCachesManagerOperationPolicySerial;
cachesManager.containsOperationPolicy = SDWebImageCachesManagerOperationPolicySerial;
cachesManager.clearOperationPolicy = SDWebImageCachesManagerOperationPolicySerial;
cachesManager.queryOperationPolicy = SDImageCachesManagerOperationPolicySerial;
cachesManager.storeOperationPolicy = SDImageCachesManagerOperationPolicySerial;
cachesManager.removeOperationPolicy = SDImageCachesManagerOperationPolicySerial;
cachesManager.containsOperationPolicy = SDImageCachesManagerOperationPolicySerial;
cachesManager.clearOperationPolicy = SDImageCachesManagerOperationPolicySerial;
[cachesManager queryImageForKey:kSerialTestImageKey options:0 context:nil completion:nil];
[cachesManager storeImage:[self testJPEGImage] imageData:nil forKey:kSerialTestImageKey cacheType:SDImageCacheTypeMemory completion:nil];
[cachesManager removeImageForKey:kSerialTestImageKey cacheType:SDImageCacheTypeMemory completion:nil];

View File

@ -28,8 +28,8 @@ FOUNDATION_EXPORT const unsigned char WebImageVersionString[];
#import <SDWebImage/SDImageCache.h>
#import <SDWebImage/SDMemoryCache.h>
#import <SDWebImage/SDDiskCache.h>
#import <SDWebImage/SDWebImageCache.h>
#import <SDWebImage/SDWebImageCachesManager.h>
#import <SDWebImage/SDImageCacheDefine.h>
#import <SDWebImage/SDImageCachesManager.h>
#import <SDWebImage/UIView+WebCache.h>
#import <SDWebImage/UIImageView+WebCache.h>
#import <SDWebImage/UIImageView+HighlightedWebCache.h>