Update the test cases about the custom ImageCache protocol
This commit is contained in:
parent
ae1f6b9b8c
commit
d4da82e9c3
|
@ -793,6 +793,38 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
|
|||
[self waitForExpectationsWithCommonTimeout];
|
||||
}
|
||||
|
||||
- (void)test58CustomImageCache {
|
||||
NSString *cachePath = [[self userCacheDirectory] stringByAppendingPathComponent:@"custom"];
|
||||
SDImageCacheConfig *config = [[SDImageCacheConfig alloc] init];
|
||||
SDWebImageTestCache *cache = [[SDWebImageTestCache alloc] initWithCachePath:cachePath config:config];
|
||||
expect(cache.memoryCache).notTo.beNil();
|
||||
expect(cache.diskCache).notTo.beNil();
|
||||
|
||||
// Store
|
||||
UIImage *image1 = self.testJPEGImage;
|
||||
NSString *key1 = @"testJPEGImage";
|
||||
[cache storeImage:image1 imageData:nil forKey:key1 cacheType:SDImageCacheTypeAll completion:nil];
|
||||
// Contain
|
||||
[cache containsImageForKey:key1 cacheType:SDImageCacheTypeAll completion:^(SDImageCacheType containsCacheType) {
|
||||
expect(containsCacheType).equal(SDImageCacheTypeMemory);
|
||||
}];
|
||||
// Query
|
||||
[cache queryImageForKey:key1 options:0 context:nil cacheType:SDImageCacheTypeAll completion:^(UIImage * _Nullable image, NSData * _Nullable data, SDImageCacheType cacheType) {
|
||||
expect(image).equal(image1);
|
||||
expect(data).beNil();
|
||||
expect(cacheType).equal(SDImageCacheTypeMemory);
|
||||
}];
|
||||
// Remove
|
||||
[cache removeImageForKey:key1 cacheType:SDImageCacheTypeAll completion:nil];
|
||||
[cache containsImageForKey:key1 cacheType:SDImageCacheTypeAll completion:^(SDImageCacheType containsCacheType) {
|
||||
expect(containsCacheType).equal(SDImageCacheTypeNone);
|
||||
}];
|
||||
// Clear
|
||||
[cache clearWithCacheType:SDImageCacheTypeAll completion:nil];
|
||||
NSArray<NSString *> *cacheFiles = [cache.diskCache.fileManager contentsOfDirectoryAtPath:cachePath error:nil];
|
||||
expect(cacheFiles.count).equal(0);
|
||||
}
|
||||
|
||||
#pragma mark Helper methods
|
||||
|
||||
- (UIImage *)testJPEGImage {
|
||||
|
|
|
@ -474,7 +474,6 @@
|
|||
[self waitForExpectationsWithCommonTimeout];
|
||||
}
|
||||
|
||||
#if SD_UIKIT
|
||||
- (void)test22ThatCustomDecoderWorksForImageDownload {
|
||||
XCTestExpectation *expectation = [self expectationWithDescription:@"Custom decoder for SDWebImageDownloader not works"];
|
||||
SDWebImageDownloader *downloader = [[SDWebImageDownloader alloc] init];
|
||||
|
@ -487,8 +486,8 @@
|
|||
UIImage *testJPEGImage = [[UIImage alloc] initWithContentsOfFile:testJPEGImagePath];
|
||||
|
||||
[downloader downloadImageWithURL:testImageURL options:0 progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
|
||||
NSData *data1 = UIImagePNGRepresentation(testJPEGImage);
|
||||
NSData *data2 = UIImagePNGRepresentation(image);
|
||||
NSData *data1 = [testJPEGImage sd_imageDataAsFormat:SDImageFormatPNG];
|
||||
NSData *data2 = [image sd_imageDataAsFormat:SDImageFormatPNG];
|
||||
if (![data1 isEqualToData:data2]) {
|
||||
XCTFail(@"The image data is not equal to cutom decoder, check -[SDWebImageTestDecoder decodedImageWithData:]");
|
||||
}
|
||||
|
@ -499,7 +498,6 @@
|
|||
[self waitForExpectationsWithCommonTimeout];
|
||||
[downloader invalidateSessionAndCancel:YES];
|
||||
}
|
||||
#endif
|
||||
|
||||
- (void)test23ThatDownloadRequestModifierWorks {
|
||||
XCTestExpectation *expectation = [self expectationWithDescription:@"Download request modifier not works"];
|
||||
|
|
|
@ -9,9 +9,9 @@
|
|||
|
||||
#import <SDWebImage/SDMemoryCache.h>
|
||||
#import <SDWebImage/SDDiskCache.h>
|
||||
#import <SDWebImage/SDImageCacheDefine.h>
|
||||
|
||||
// A really naive implementation of custom memory cache and disk cache
|
||||
|
||||
@interface SDWebImageTestMemoryCache : NSObject <SDMemoryCache>
|
||||
|
||||
@property (nonatomic, strong, nonnull) SDImageCacheConfig *config;
|
||||
|
@ -26,3 +26,14 @@
|
|||
@property (nonatomic, strong, nonnull) NSFileManager *fileManager;
|
||||
|
||||
@end
|
||||
|
||||
// A really naive implementation of custom image cache using memory cache and disk cache
|
||||
@interface SDWebImageTestCache : NSObject <SDImageCache>
|
||||
|
||||
@property (nonatomic, strong, nonnull) SDImageCacheConfig *config;
|
||||
@property (nonatomic, strong, nonnull) SDWebImageTestMemoryCache *memoryCache;
|
||||
@property (nonatomic, strong, nonnull) SDWebImageTestDiskCache *diskCache;
|
||||
|
||||
- (nullable instancetype)initWithCachePath:(nonnull NSString *)cachePath config:(nonnull SDImageCacheConfig *)config;
|
||||
|
||||
@end
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
*/
|
||||
|
||||
#import "SDWebImageTestCache.h"
|
||||
#import <SDWebImage/SDImageCacheConfig.h>
|
||||
#import <SDWebImage/SDWebImage.h>
|
||||
#import "SDFileAttributeHelper.h"
|
||||
|
||||
static NSString * const SDWebImageTestDiskCacheExtendedAttributeName = @"com.hackemist.SDWebImageTestDiskCache";
|
||||
|
@ -122,3 +122,153 @@ static NSString * const SDWebImageTestDiskCacheExtendedAttributeName = @"com.hac
|
|||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation SDWebImageTestCache
|
||||
|
||||
- (instancetype)initWithCachePath:(NSString *)cachePath config:(SDImageCacheConfig *)config {
|
||||
self = [super init];
|
||||
if (self) {
|
||||
self.config = config;
|
||||
self.memoryCache = [[SDWebImageTestMemoryCache alloc] initWithConfig:config];
|
||||
self.diskCache = [[SDWebImageTestDiskCache alloc] initWithCachePath:cachePath config:config];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)clearWithCacheType:(SDImageCacheType)cacheType completion:(nullable SDWebImageNoParamsBlock)completionBlock {
|
||||
switch (cacheType) {
|
||||
case SDImageCacheTypeNone:
|
||||
break;
|
||||
case SDImageCacheTypeMemory:
|
||||
[self.memoryCache removeAllObjects];
|
||||
break;
|
||||
case SDImageCacheTypeDisk:
|
||||
[self.diskCache removeAllData];
|
||||
break;
|
||||
case SDImageCacheTypeAll:
|
||||
[self.memoryCache removeAllObjects];
|
||||
[self.diskCache removeAllData];
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (completionBlock) {
|
||||
completionBlock();
|
||||
}
|
||||
}
|
||||
|
||||
- (void)containsImageForKey:(nullable NSString *)key cacheType:(SDImageCacheType)cacheType completion:(nullable SDImageCacheContainsCompletionBlock)completionBlock {
|
||||
SDImageCacheType containsCacheType = SDImageCacheTypeNone;
|
||||
switch (cacheType) {
|
||||
case SDImageCacheTypeNone:
|
||||
break;
|
||||
case SDImageCacheTypeMemory:
|
||||
containsCacheType = [self.memoryCache objectForKey:key] ? SDImageCacheTypeMemory : SDImageCacheTypeNone;
|
||||
break;
|
||||
case SDImageCacheTypeDisk:
|
||||
containsCacheType = [self.diskCache containsDataForKey:key] ? SDImageCacheTypeDisk : SDImageCacheTypeNone;
|
||||
break;
|
||||
case SDImageCacheTypeAll:
|
||||
if ([self.memoryCache objectForKey:key]) {
|
||||
containsCacheType = SDImageCacheTypeMemory;
|
||||
} else if ([self.diskCache containsDataForKey:key]) {
|
||||
containsCacheType = SDImageCacheTypeDisk;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (completionBlock) {
|
||||
completionBlock(containsCacheType);
|
||||
}
|
||||
}
|
||||
|
||||
- (nullable id<SDWebImageOperation>)queryImageForKey:(nullable NSString *)key options:(SDWebImageOptions)options context:(nullable SDWebImageContext *)context completion:(nullable SDImageCacheQueryCompletionBlock)completionBlock {
|
||||
return [self queryImageForKey:key options:options context:context cacheType:SDImageCacheTypeAll completion:completionBlock];
|
||||
}
|
||||
|
||||
- (nullable id<SDWebImageOperation>)queryImageForKey:(nullable NSString *)key options:(SDWebImageOptions)options context:(nullable SDWebImageContext *)context cacheType:(SDImageCacheType)cacheType completion:(nullable SDImageCacheQueryCompletionBlock)completionBlock {
|
||||
UIImage *image;
|
||||
NSData *data;
|
||||
SDImageCacheType resultCacheType = SDImageCacheTypeNone;
|
||||
switch (cacheType) {
|
||||
case SDImageCacheTypeNone:
|
||||
break;
|
||||
case SDImageCacheTypeMemory:
|
||||
image = [self.memoryCache objectForKey:key];
|
||||
if (image) {
|
||||
resultCacheType = SDImageCacheTypeMemory;
|
||||
}
|
||||
break;
|
||||
case SDImageCacheTypeDisk:
|
||||
data = [self.diskCache dataForKey:key];
|
||||
image = [UIImage sd_imageWithData:data];
|
||||
if (data) {
|
||||
resultCacheType = SDImageCacheTypeDisk;
|
||||
}
|
||||
break;
|
||||
case SDImageCacheTypeAll:
|
||||
image = [self.memoryCache objectForKey:key];
|
||||
if (image) {
|
||||
resultCacheType = SDImageCacheTypeMemory;
|
||||
} else {
|
||||
data = [self.diskCache dataForKey:key];
|
||||
image = [UIImage sd_imageWithData:data];
|
||||
if (data) {
|
||||
resultCacheType = SDImageCacheTypeDisk;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (completionBlock) {
|
||||
completionBlock(image, data, resultCacheType);
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (void)removeImageForKey:(nullable NSString *)key cacheType:(SDImageCacheType)cacheType completion:(nullable SDWebImageNoParamsBlock)completionBlock {
|
||||
switch (cacheType) {
|
||||
case SDImageCacheTypeNone:
|
||||
break;
|
||||
case SDImageCacheTypeMemory:
|
||||
[self.memoryCache removeObjectForKey:key];
|
||||
break;
|
||||
case SDImageCacheTypeDisk:
|
||||
[self.diskCache removeDataForKey:key];
|
||||
break;
|
||||
case SDImageCacheTypeAll:
|
||||
[self.memoryCache removeObjectForKey:key];
|
||||
[self.diskCache removeDataForKey:key];
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (completionBlock) {
|
||||
completionBlock();
|
||||
}
|
||||
}
|
||||
|
||||
- (void)storeImage:(nullable UIImage *)image imageData:(nullable NSData *)imageData forKey:(nullable NSString *)key cacheType:(SDImageCacheType)cacheType completion:(nullable SDWebImageNoParamsBlock)completionBlock {
|
||||
switch (cacheType) {
|
||||
case SDImageCacheTypeNone:
|
||||
break;
|
||||
case SDImageCacheTypeMemory:
|
||||
[self.memoryCache setObject:image forKey:key];
|
||||
break;
|
||||
case SDImageCacheTypeDisk:
|
||||
[self.diskCache setData:imageData forKey:key];
|
||||
break;
|
||||
case SDImageCacheTypeAll:
|
||||
[self.memoryCache setObject:image forKey:key];
|
||||
[self.diskCache setData:imageData forKey:key];
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (completionBlock) {
|
||||
completionBlock();
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
Loading…
Reference in New Issue