Add test for custom memory cache and disk cache

This commit is contained in:
DreamPiggy 2018-04-15 00:08:08 +08:00
parent 62058d209d
commit 2d27300e00
4 changed files with 177 additions and 0 deletions

View File

@ -20,6 +20,8 @@
3264FF30205D42CB00F6BD48 /* SDWebImageTestTransformer.m in Sources */ = {isa = PBXBuildFile; fileRef = 3264FF2E205D42CB00F6BD48 /* SDWebImageTestTransformer.m */; };
327054E2206CEFF3006EA328 /* TestImageAnimated.apng in Resources */ = {isa = PBXBuildFile; fileRef = 327054E1206CEFF3006EA328 /* TestImageAnimated.apng */; };
327054E3206CEFF3006EA328 /* TestImageAnimated.apng in Resources */ = {isa = PBXBuildFile; fileRef = 327054E1206CEFF3006EA328 /* TestImageAnimated.apng */; };
328BB6DD20825E9800760D6C /* SDWebImageTestCache.m in Sources */ = {isa = PBXBuildFile; fileRef = 328BB6DC20825E9800760D6C /* SDWebImageTestCache.m */; };
328BB6DE20825E9800760D6C /* SDWebImageTestCache.m in Sources */ = {isa = PBXBuildFile; fileRef = 328BB6DC20825E9800760D6C /* SDWebImageTestCache.m */; };
32A571562037DB2D002EDAAE /* SDAnimatedImageTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 32A571552037DB2D002EDAAE /* SDAnimatedImageTest.m */; };
32B99E8B203AF8690017FD66 /* SDCategoriesTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 32B99E8A203AF8690017FD66 /* SDCategoriesTests.m */; };
32B99E9B203B2EDD0017FD66 /* SDTestCase.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D7AF05F1F329763000083C2 /* SDTestCase.m */; };
@ -71,6 +73,8 @@
3264FF2D205D42CB00F6BD48 /* SDWebImageTestTransformer.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SDWebImageTestTransformer.h; sourceTree = "<group>"; };
3264FF2E205D42CB00F6BD48 /* SDWebImageTestTransformer.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SDWebImageTestTransformer.m; sourceTree = "<group>"; };
327054E1206CEFF3006EA328 /* TestImageAnimated.apng */ = {isa = PBXFileReference; lastKnownFileType = file; path = TestImageAnimated.apng; sourceTree = "<group>"; };
328BB6DB20825E9800760D6C /* SDWebImageTestCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDWebImageTestCache.h; sourceTree = "<group>"; };
328BB6DC20825E9800760D6C /* SDWebImageTestCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDWebImageTestCache.m; sourceTree = "<group>"; };
32A571552037DB2D002EDAAE /* SDAnimatedImageTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SDAnimatedImageTest.m; sourceTree = "<group>"; };
32B99E8A203AF8690017FD66 /* SDCategoriesTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SDCategoriesTests.m; sourceTree = "<group>"; };
32B99E92203B2DF90017FD66 /* Tests Mac.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Tests Mac.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
@ -210,6 +214,8 @@
37D122871EC48B5E00D98CEB /* SDMockFileManager.m */,
2D7AF05E1F329763000083C2 /* SDTestCase.h */,
2D7AF05F1F329763000083C2 /* SDTestCase.m */,
328BB6DB20825E9800760D6C /* SDWebImageTestCache.h */,
328BB6DC20825E9800760D6C /* SDWebImageTestCache.m */,
3226ECB920754F7700FAFACF /* SDWebImageTestDownloadOperation.h */,
3226ECBA20754F7700FAFACF /* SDWebImageTestDownloadOperation.m */,
32E6F0301F3A1B4700A945E6 /* SDWebImageTestDecoder.h */,
@ -465,6 +471,7 @@
files = (
32B99EAC203B36650017FD66 /* SDWebImageDownloaderTests.m in Sources */,
3254C32120641077008D1022 /* SDWebImageTransformerTests.m in Sources */,
328BB6DE20825E9800760D6C /* SDWebImageTestCache.m in Sources */,
32B99E9C203B2EE40017FD66 /* SDCategoriesTests.m in Sources */,
32B99EAA203B365F0017FD66 /* SDImageCacheTests.m in Sources */,
32B99EAD203B36690017FD66 /* SDWebImagePrefetcherTests.m in Sources */,
@ -491,6 +498,7 @@
37D122881EC48B5E00D98CEB /* SDMockFileManager.m in Sources */,
4369C2741D9804B1007E863A /* SDWebCacheCategoriesTests.m in Sources */,
2D7AF0601F329763000083C2 /* SDTestCase.m in Sources */,
328BB6DD20825E9800760D6C /* SDWebImageTestCache.m in Sources */,
4369C1D11D97F80F007E863A /* SDWebImagePrefetcherTests.m in Sources */,
DA248D69195475D800390AB0 /* SDImageCacheTests.m in Sources */,
DA248D6B195476AC00390AB0 /* SDWebImageManagerTests.m in Sources */,

View File

@ -11,9 +11,17 @@
#import <SDWebImage/SDWebImageCodersManager.h>
#import "SDWebImageTestDecoder.h"
#import "SDMockFileManager.h"
#import "SDWebImageTestCache.h"
NSString *kImageTestKey = @"TestImageKey.jpg";
@interface SDImageCache ()
@property (nonatomic, strong, nonnull) id<SDMemoryCache> memCache;
@property (nonatomic, strong, nonnull) id<SDDiskCache> diskCache;
@end
@interface SDImageCacheTests : SDTestCase
@end
@ -323,6 +331,27 @@ NSString *kImageTestKey = @"TestImageKey.jpg";
expect(fileManager.lastError).equal(targetError);
}
#pragma mark - SDMemoryCache & SDDiskCache
- (void)test42CustomMemoryCache {
SDImageCacheConfig *config = [[SDImageCacheConfig alloc] init];
config.memoryCacheClass = [SDWebImageTestMemoryCache class];
NSString *nameSpace = @"SDWebImageTestMemoryCache";
NSString *cacheDictionary = [self makeDiskCachePath:nameSpace];
SDImageCache *cache = [[SDImageCache alloc] initWithNamespace:nameSpace diskCacheDirectory:cacheDictionary config:config];
SDWebImageTestMemoryCache *memCache = cache.memCache;
expect([memCache isKindOfClass:[SDWebImageTestMemoryCache class]]).to.beTruthy();
}
- (void)test43CustomDiskCache {
SDImageCacheConfig *config = [[SDImageCacheConfig alloc] init];
config.diskCacheClass = [SDWebImageTestDiskCache class];
NSString *nameSpace = @"SDWebImageTestDiskCache";
NSString *cacheDictionary = [self makeDiskCachePath:nameSpace];
SDImageCache *cache = [[SDImageCache alloc] initWithNamespace:nameSpace diskCacheDirectory:cacheDictionary config:config];
SDWebImageTestDiskCache *diskCache = cache.diskCache;
expect([diskCache isKindOfClass:[SDWebImageTestDiskCache class]]).to.beTruthy();
}
#pragma mark Helper methods
- (UIImage *)imageForTesting{
@ -339,4 +368,9 @@ NSString *kImageTestKey = @"TestImageKey.jpg";
return [testBundle pathForResource:@"TestImage" ofType:@"jpg"];
}
- (nullable NSString *)makeDiskCachePath:(nonnull NSString*)fullNamespace {
NSArray<NSString *> *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
return [paths[0] stringByAppendingPathComponent:fullNamespace];
}
@end

View File

@ -0,0 +1,28 @@
/*
* 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 <SDWebImage/SDMemoryCache.h>
#import <SDWebImage/SDDiskCache.h>
// A really naive implementation of custom memory cache and disk cache
@interface SDWebImageTestMemoryCache : NSObject <SDMemoryCache>
@property (nonatomic, strong, nonnull) SDImageCacheConfig *config;
@property (nonatomic, strong, nonnull) NSCache *cache;
@end
@interface SDWebImageTestDiskCache : NSObject <SDDiskCache>
@property (nonatomic, strong, nonnull) SDImageCacheConfig *config;
@property (nonatomic, copy, nonnull) NSString *cachePath;
@property (nonatomic, strong, nonnull) NSFileManager *fileManager;
@end

View File

@ -0,0 +1,107 @@
/*
* 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 "SDWebImageTestCache.h"
#import <SDWebImage/SDImageCacheConfig.h>
@implementation SDWebImageTestMemoryCache
- (nonnull instancetype)initWithConfig:(nonnull SDImageCacheConfig *)config {
self = [super init];
if (self) {
self.config = config;
self.cache = [[NSCache alloc] init];
}
return self;
}
- (nullable id)objectForKey:(nonnull id)key {
return [self.cache objectForKey:key];
}
- (void)removeAllObjects {
[self.cache removeAllObjects];
}
- (void)removeObjectForKey:(nonnull id)key {
[self.cache removeObjectForKey:key];
}
- (void)setObject:(nullable id)object forKey:(nonnull id)key {
[self.cache setObject:object forKey:key];
}
- (void)setObject:(nullable id)object forKey:(nonnull id)key cost:(NSUInteger)cost {
[self.cache setObject:object forKey:key cost:cost];
}
@end
@implementation SDWebImageTestDiskCache
- (nullable NSString *)cachePathForKey:(nonnull NSString *)key {
return [self.cachePath stringByAppendingPathComponent:key];
}
- (BOOL)containsDataForKey:(nonnull NSString *)key {
return [self.fileManager fileExistsAtPath:[self cachePathForKey:key]];
}
- (nullable NSData *)dataForKey:(nonnull NSString *)key {
return [self.fileManager contentsAtPath:[self cachePathForKey:key]];
}
- (nullable instancetype)initWithCachePath:(nonnull NSString *)cachePath config:(nonnull SDImageCacheConfig *)config {
self = [super init];
if (self) {
self.cachePath = cachePath;
self.config = config;
self.fileManager = config.fileManager ? config.fileManager : [NSFileManager new];
[self.fileManager createDirectoryAtPath:self.cachePath withIntermediateDirectories:YES attributes:nil error:nil];
}
return self;
}
- (void)removeAllData {
[self.fileManager removeItemAtPath:self.cachePath error:nil];
}
- (void)removeDataForKey:(nonnull NSString *)key {
[self.fileManager removeItemAtPath:[self cachePathForKey:key] error:nil];
}
- (void)removeExpiredData {
NSDate *expirationDate = [NSDate dateWithTimeIntervalSinceNow:-self.config.maxCacheAge];
for (NSString *fileName in [self.fileManager enumeratorAtPath:self.cachePath]) {
NSString *filePath = [self.cachePath stringByAppendingPathComponent:fileName];
NSDate *modificationDate = [[self.fileManager attributesOfItemAtPath:filePath error:nil] objectForKey:NSFileModificationDate];
if ([[modificationDate laterDate:expirationDate] isEqualToDate:expirationDate]) {
[self.fileManager removeItemAtPath:filePath error:nil];
}
}
}
- (void)setData:(nullable NSData *)data forKey:(nonnull NSString *)key {
[self.fileManager createFileAtPath:[self cachePathForKey:key] contents:data attributes:nil];
}
- (NSInteger)totalCount {
return [self.fileManager contentsOfDirectoryAtPath:self.cachePath error:nil].count;
}
- (NSInteger)totalSize {
NSUInteger size = 0;
for (NSString *fileName in [self.fileManager enumeratorAtPath:self.cachePath]) {
NSString *filePath = [self.cachePath stringByAppendingPathComponent:fileName];
size += [[[self.fileManager attributesOfItemAtPath:filePath error:nil] objectForKey:NSFileSize] unsignedIntegerValue];
}
return size;
}
@end