Added the test case about using the custom cache and loader with context option to manager, full pipeline testing
This commit is contained in:
parent
d4da82e9c3
commit
067174b1fd
|
@ -800,6 +800,8 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
|
||||||
expect(cache.memoryCache).notTo.beNil();
|
expect(cache.memoryCache).notTo.beNil();
|
||||||
expect(cache.diskCache).notTo.beNil();
|
expect(cache.diskCache).notTo.beNil();
|
||||||
|
|
||||||
|
// Clear
|
||||||
|
[cache clearWithCacheType:SDImageCacheTypeAll completion:nil];
|
||||||
// Store
|
// Store
|
||||||
UIImage *image1 = self.testJPEGImage;
|
UIImage *image1 = self.testJPEGImage;
|
||||||
NSString *key1 = @"testJPEGImage";
|
NSString *key1 = @"testJPEGImage";
|
||||||
|
@ -816,6 +818,7 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
|
||||||
}];
|
}];
|
||||||
// Remove
|
// Remove
|
||||||
[cache removeImageForKey:key1 cacheType:SDImageCacheTypeAll completion:nil];
|
[cache removeImageForKey:key1 cacheType:SDImageCacheTypeAll completion:nil];
|
||||||
|
// Contain
|
||||||
[cache containsImageForKey:key1 cacheType:SDImageCacheTypeAll completion:^(SDImageCacheType containsCacheType) {
|
[cache containsImageForKey:key1 cacheType:SDImageCacheTypeAll completion:^(SDImageCacheType containsCacheType) {
|
||||||
expect(containsCacheType).equal(SDImageCacheTypeNone);
|
expect(containsCacheType).equal(SDImageCacheTypeNone);
|
||||||
}];
|
}];
|
||||||
|
|
|
@ -8,6 +8,8 @@
|
||||||
|
|
||||||
#import "SDTestCase.h"
|
#import "SDTestCase.h"
|
||||||
#import "SDWebImageTestTransformer.h"
|
#import "SDWebImageTestTransformer.h"
|
||||||
|
#import "SDWebImageTestCache.h"
|
||||||
|
#import "SDWebImageTestLoader.h"
|
||||||
|
|
||||||
@interface SDWebImageManagerTests : SDTestCase
|
@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 {
|
- (NSString *)testJPEGPath {
|
||||||
NSBundle *testBundle = [NSBundle bundleForClass:[self class]];
|
NSBundle *testBundle = [NSBundle bundleForClass:[self class]];
|
||||||
return [testBundle pathForResource:@"TestImage" ofType:@"jpg"];
|
return [testBundle pathForResource:@"TestImage" ofType:@"jpg"];
|
||||||
|
|
|
@ -36,4 +36,6 @@
|
||||||
|
|
||||||
- (nullable instancetype)initWithCachePath:(nonnull NSString *)cachePath config:(nonnull SDImageCacheConfig *)config;
|
- (nullable instancetype)initWithCachePath:(nonnull NSString *)cachePath config:(nonnull SDImageCacheConfig *)config;
|
||||||
|
|
||||||
|
@property (nonatomic, class, readonly, nonnull) SDWebImageTestCache *sharedCache;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
|
@ -49,7 +49,7 @@ static NSString * const SDWebImageTestDiskCacheExtendedAttributeName = @"com.hac
|
||||||
@implementation SDWebImageTestDiskCache
|
@implementation SDWebImageTestDiskCache
|
||||||
|
|
||||||
- (nullable NSString *)cachePathForKey:(nonnull NSString *)key {
|
- (nullable NSString *)cachePathForKey:(nonnull NSString *)key {
|
||||||
return [self.cachePath stringByAppendingPathComponent:key];
|
return [self.cachePath stringByAppendingPathComponent:key.lastPathComponent];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (BOOL)containsDataForKey:(nonnull NSString *)key {
|
- (BOOL)containsDataForKey:(nonnull NSString *)key {
|
||||||
|
@ -72,7 +72,10 @@ static NSString * const SDWebImageTestDiskCacheExtendedAttributeName = @"com.hac
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)removeAllData {
|
- (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 {
|
- (void)removeDataForKey:(nonnull NSString *)key {
|
||||||
|
@ -125,6 +128,17 @@ static NSString * const SDWebImageTestDiskCacheExtendedAttributeName = @"com.hac
|
||||||
|
|
||||||
@implementation SDWebImageTestCache
|
@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 {
|
- (instancetype)initWithCachePath:(NSString *)cachePath config:(SDImageCacheConfig *)config {
|
||||||
self = [super init];
|
self = [super init];
|
||||||
if (self) {
|
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
|
@end
|
||||||
|
|
|
@ -13,4 +13,6 @@
|
||||||
// A really naive implementation of custom image loader using `NSURLSession`
|
// A really naive implementation of custom image loader using `NSURLSession`
|
||||||
@interface SDWebImageTestLoader : NSObject <SDImageLoader>
|
@interface SDWebImageTestLoader : NSObject <SDImageLoader>
|
||||||
|
|
||||||
|
@property (nonatomic, class, readonly, nonnull) SDWebImageTestLoader *sharedLoader;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
|
@ -16,6 +16,15 @@
|
||||||
|
|
||||||
@implementation SDWebImageTestLoader
|
@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 {
|
- (BOOL)canRequestImageForURL:(NSURL *)url {
|
||||||
return YES;
|
return YES;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue