Added the test case about using the custom cache and loader with context option to manager, full pipeline testing

This commit is contained in:
DreamPiggy 2020-04-02 12:49:15 +08:00
parent d4da82e9c3
commit 067174b1fd
6 changed files with 63 additions and 2 deletions

View File

@ -800,6 +800,8 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
expect(cache.memoryCache).notTo.beNil();
expect(cache.diskCache).notTo.beNil();
// Clear
[cache clearWithCacheType:SDImageCacheTypeAll completion:nil];
// Store
UIImage *image1 = self.testJPEGImage;
NSString *key1 = @"testJPEGImage";
@ -816,6 +818,7 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
}];
// Remove
[cache removeImageForKey:key1 cacheType:SDImageCacheTypeAll completion:nil];
// Contain
[cache containsImageForKey:key1 cacheType:SDImageCacheTypeAll completion:^(SDImageCacheType containsCacheType) {
expect(containsCacheType).equal(SDImageCacheTypeNone);
}];

View File

@ -8,6 +8,8 @@
#import "SDTestCase.h"
#import "SDWebImageTestTransformer.h"
#import "SDWebImageTestCache.h"
#import "SDWebImageTestLoader.h"
@interface SDWebImageManagerTests : SDTestCase
@ -268,6 +270,30 @@
}];
}
- (void)test14ThatCustomCacheAndLoaderWorks {
XCTestExpectation *expectation = [self expectationWithDescription:@"Custom Cache and Loader during manger query"];
NSURL *url = [NSURL URLWithString:@"http://via.placeholder.com/100x100.png"];
SDWebImageContext *context = @{
SDWebImageContextImageCache : SDWebImageTestCache.sharedCache,
SDWebImageContextImageLoader : SDWebImageTestLoader.sharedLoader
};
[SDWebImageTestCache.sharedCache clearWithCacheType:SDImageCacheTypeAll completion:nil];
[SDWebImageManager.sharedManager loadImageWithURL:url options:SDWebImageWaitStoreCache context:context progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
expect(image).notTo.beNil();
expect(image.size.width).equal(100);
expect(image.size.height).equal(100);
expect(data).notTo.beNil();
NSString *cacheKey = [SDWebImageManager.sharedManager cacheKeyForURL:imageURL];
// Check Disk Cache (SDWebImageWaitStoreCache behavior)
[SDWebImageTestCache.sharedCache containsImageForKey:cacheKey cacheType:SDImageCacheTypeDisk completion:^(SDImageCacheType containsCacheType) {
expect(containsCacheType).equal(SDImageCacheTypeDisk);
[expectation fulfill];
}];
}];
[self waitForExpectationsWithCommonTimeout];
}
- (NSString *)testJPEGPath {
NSBundle *testBundle = [NSBundle bundleForClass:[self class]];
return [testBundle pathForResource:@"TestImage" ofType:@"jpg"];

View File

@ -36,4 +36,6 @@
- (nullable instancetype)initWithCachePath:(nonnull NSString *)cachePath config:(nonnull SDImageCacheConfig *)config;
@property (nonatomic, class, readonly, nonnull) SDWebImageTestCache *sharedCache;
@end

View File

@ -49,7 +49,7 @@ static NSString * const SDWebImageTestDiskCacheExtendedAttributeName = @"com.hac
@implementation SDWebImageTestDiskCache
- (nullable NSString *)cachePathForKey:(nonnull NSString *)key {
return [self.cachePath stringByAppendingPathComponent:key];
return [self.cachePath stringByAppendingPathComponent:key.lastPathComponent];
}
- (BOOL)containsDataForKey:(nonnull NSString *)key {
@ -72,7 +72,10 @@ static NSString * const SDWebImageTestDiskCacheExtendedAttributeName = @"com.hac
}
- (void)removeAllData {
[self.fileManager removeItemAtPath:self.cachePath error:nil];
for (NSString *path in [self.fileManager subpathsAtPath:self.cachePath]) {
NSString *filePath = [self.cachePath stringByAppendingPathComponent:path];
[self.fileManager removeItemAtPath:filePath error:nil];
}
}
- (void)removeDataForKey:(nonnull NSString *)key {
@ -125,6 +128,17 @@ static NSString * const SDWebImageTestDiskCacheExtendedAttributeName = @"com.hac
@implementation SDWebImageTestCache
+ (SDWebImageTestCache *)sharedCache {
static dispatch_once_t onceToken;
static SDWebImageTestCache *cache;
dispatch_once(&onceToken, ^{
NSString *cachePath = [[self userCacheDirectory] stringByAppendingPathComponent:@"SDWebImageTestCache"];
SDImageCacheConfig *config = SDImageCacheConfig.defaultCacheConfig;
cache = [[SDWebImageTestCache alloc] initWithCachePath:cachePath config:config];
});
return cache;
}
- (instancetype)initWithCachePath:(NSString *)cachePath config:(SDImageCacheConfig *)config {
self = [super init];
if (self) {
@ -271,4 +285,9 @@ static NSString * const SDWebImageTestDiskCacheExtendedAttributeName = @"com.hac
}
}
+ (nullable NSString *)userCacheDirectory {
NSArray<NSString *> *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
return paths.firstObject;
}
@end

View File

@ -13,4 +13,6 @@
// A really naive implementation of custom image loader using `NSURLSession`
@interface SDWebImageTestLoader : NSObject <SDImageLoader>
@property (nonatomic, class, readonly, nonnull) SDWebImageTestLoader *sharedLoader;
@end

View File

@ -16,6 +16,15 @@
@implementation SDWebImageTestLoader
+ (SDWebImageTestLoader *)sharedLoader {
static dispatch_once_t onceToken;
static SDWebImageTestLoader *loader;
dispatch_once(&onceToken, ^{
loader = [[SDWebImageTestLoader alloc] init];
});
return loader;
}
- (BOOL)canRequestImageForURL:(NSURL *)url {
return YES;
}