diff --git a/Tests/Tests/SDImageCacheTests.m b/Tests/Tests/SDImageCacheTests.m index 0b195f89..96fb0f48 100644 --- a/Tests/Tests/SDImageCacheTests.m +++ b/Tests/Tests/SDImageCacheTests.m @@ -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); }]; diff --git a/Tests/Tests/SDWebImageManagerTests.m b/Tests/Tests/SDWebImageManagerTests.m index c75fc950..ba61529f 100644 --- a/Tests/Tests/SDWebImageManagerTests.m +++ b/Tests/Tests/SDWebImageManagerTests.m @@ -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"]; diff --git a/Tests/Tests/SDWebImageTestCache.h b/Tests/Tests/SDWebImageTestCache.h index 8029ff4d..6c900c6f 100644 --- a/Tests/Tests/SDWebImageTestCache.h +++ b/Tests/Tests/SDWebImageTestCache.h @@ -36,4 +36,6 @@ - (nullable instancetype)initWithCachePath:(nonnull NSString *)cachePath config:(nonnull SDImageCacheConfig *)config; +@property (nonatomic, class, readonly, nonnull) SDWebImageTestCache *sharedCache; + @end diff --git a/Tests/Tests/SDWebImageTestCache.m b/Tests/Tests/SDWebImageTestCache.m index 143f2c80..d9e34e3d 100644 --- a/Tests/Tests/SDWebImageTestCache.m +++ b/Tests/Tests/SDWebImageTestCache.m @@ -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 *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); + return paths.firstObject; +} + @end diff --git a/Tests/Tests/SDWebImageTestLoader.h b/Tests/Tests/SDWebImageTestLoader.h index d6a3f5f9..bd343cd8 100644 --- a/Tests/Tests/SDWebImageTestLoader.h +++ b/Tests/Tests/SDWebImageTestLoader.h @@ -13,4 +13,6 @@ // A really naive implementation of custom image loader using `NSURLSession` @interface SDWebImageTestLoader : NSObject +@property (nonatomic, class, readonly, nonnull) SDWebImageTestLoader *sharedLoader; + @end diff --git a/Tests/Tests/SDWebImageTestLoader.m b/Tests/Tests/SDWebImageTestLoader.m index 22978edb..14f6f7e5 100644 --- a/Tests/Tests/SDWebImageTestLoader.m +++ b/Tests/Tests/SDWebImageTestLoader.m @@ -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; }