From 79eb6e77c59d83a372a05ccf258dccca58634405 Mon Sep 17 00:00:00 2001 From: Pau Ruiz Date: Sun, 19 Apr 2015 10:01:35 +0100 Subject: [PATCH] Added all methods of the cache module, almost all directly and some of them indirectly used Unable to test sync call to diskImageExistsWithKey, hoped due to problems with the file manager and the test bundle being different from the main bundle --- Tests/Tests/SDImageCacheTests.m | 169 +++++++++++++++++++++++++++++++- 1 file changed, 164 insertions(+), 5 deletions(-) diff --git a/Tests/Tests/SDImageCacheTests.m b/Tests/Tests/SDImageCacheTests.m index 9451e685..8edbdbd6 100644 --- a/Tests/Tests/SDImageCacheTests.m +++ b/Tests/Tests/SDImageCacheTests.m @@ -14,9 +14,10 @@ #import "SDImageCache.h" +NSString *kImageTestKey = @"TestImageKey"; @interface SDImageCacheTests : XCTestCase - +@property (strong, nonatomic) SDImageCache *sharedImageCache; @end @implementation SDImageCacheTests @@ -25,6 +26,8 @@ { [super setUp]; // Put setup code here. This method is called before the invocation of each test method in the class. + self.sharedImageCache = [SDImageCache sharedImageCache]; + [self clearAllCaches]; } - (void)tearDown @@ -34,9 +37,165 @@ } - (void)testSharedImageCache { - SDImageCache *sharedImageCache = [SDImageCache sharedImageCache]; - - expect(sharedImageCache).toNot.beNil(); + expect(self.sharedImageCache).toNot.beNil(); } -@end +- (void)testSingleton{ + expect(self.sharedImageCache).to.equal([SDImageCache sharedImageCache]); +} + +- (void)testClearDiskCache{ + [self.sharedImageCache storeImage:[self imageForTesting] forKey:kImageTestKey]; + [self.sharedImageCache clearDiskOnCompletion:^{ + expect([self.sharedImageCache diskImageExistsWithKey:kImageTestKey]).to.equal(NO); + expect([self.sharedImageCache imageFromMemoryCacheForKey:kImageTestKey]).to.equal([self imageForTesting]); + }]; +} + +- (void)testClearMemoryCache{ + [self.sharedImageCache storeImage:[self imageForTesting] forKey:kImageTestKey]; + [self.sharedImageCache clearMemory]; + expect([self.sharedImageCache imageFromMemoryCacheForKey:kImageTestKey]).to.beNil; + // Seems not able to access the files correctly (maybe only from test?) + //expect([self.sharedImageCache diskImageExistsWithKey:kImageTestKey]).to.equal(YES); + [self.sharedImageCache diskImageExistsWithKey:kImageTestKey completion:^(BOOL isInCache) { + expect(isInCache).to.equal(YES); + }]; +} + +// Testing storeImage:forKey: +- (void)testInsertionOfImage { + UIImage *image = [self imageForTesting]; + [self.sharedImageCache storeImage:image forKey:kImageTestKey]; + expect([self.sharedImageCache imageFromMemoryCacheForKey:kImageTestKey]).to.equal(image); + expect([self.sharedImageCache imageFromDiskCacheForKey:kImageTestKey]).to.equal(image); +} + +// Testing storeImage:forKey:toDisk:YES +- (void)testInsertionOfImageForcingDiskStorage{ + UIImage *image = [self imageForTesting]; + [self.sharedImageCache storeImage:image forKey:kImageTestKey toDisk:YES]; + expect([self.sharedImageCache imageFromMemoryCacheForKey:kImageTestKey]).to.equal(image); + // Seems not able to access the files correctly (maybe only from test?) + //expect([self.sharedImageCache diskImageExistsWithKey:kImageTestKey]).to.equal(YES); + [self.sharedImageCache diskImageExistsWithKey:kImageTestKey completion:^(BOOL isInCache) { + expect(isInCache).to.equal(YES); + }]; +} + +// Testing storeImage:forKey:toDisk:NO +- (void)testInsertionOfImageOnlyInMemory { + UIImage *image = [self imageForTesting]; + [self.sharedImageCache storeImage:image forKey:@"TestImage" toDisk:NO]; + [self.sharedImageCache diskImageExistsWithKey:@"TestImage" completion:^(BOOL isInCache) { + expect(isInCache).to.equal(YES); + }]; + [self.sharedImageCache clearMemory]; + [self.sharedImageCache diskImageExistsWithKey:@"TestImage" completion:^(BOOL isInCache) { + expect(isInCache).to.equal(NO); + }]; +} + +- (void)testRetrievalImageThroughNSOperation{ + //- (NSOperation *)queryDiskCacheForKey:(NSString *)key done:(SDWebImageQueryCompletedBlock)doneBlock; + UIImage *imageForTesting = [self imageForTesting]; + [self.sharedImageCache storeImage:imageForTesting forKey:kImageTestKey]; + NSOperation *operation = [self.sharedImageCache queryDiskCacheForKey:kImageTestKey done:^(UIImage *image, SDImageCacheType cacheType) { + expect(image).to.equal(imageForTesting); + }]; + expect(operation).toNot.beNil; +} + +- (void)testRemoveImageForKey{ + [self.sharedImageCache storeImage:[self imageForTesting] forKey:kImageTestKey]; + [self.sharedImageCache removeImageForKey:kImageTestKey]; + expect([self.sharedImageCache imageFromMemoryCacheForKey:kImageTestKey]).to.beNil; + expect([self.sharedImageCache imageFromDiskCacheForKey:kImageTestKey]).to.beNil; +} + +- (void)testRemoveImageForKeyWithCompletion{ + [self.sharedImageCache storeImage:[self imageForTesting] forKey:kImageTestKey]; + [self.sharedImageCache removeImageForKey:kImageTestKey withCompletion:^{ + expect([self.sharedImageCache imageFromDiskCacheForKey:kImageTestKey]).to.beNil; + expect([self.sharedImageCache imageFromMemoryCacheForKey:kImageTestKey]).to.beNil; + }]; +} + +- (void)testRemoveImageForKeyNotFromDisk{ + [self.sharedImageCache storeImage:[self imageForTesting] forKey:kImageTestKey]; + [self.sharedImageCache removeImageForKey:kImageTestKey fromDisk:NO]; + expect([self.sharedImageCache imageFromDiskCacheForKey:kImageTestKey]).toNot.beNil; + expect([self.sharedImageCache imageFromMemoryCacheForKey:kImageTestKey]).to.beNil; +} + +- (void)testRemoveImageForKeyFromDisk{ + [self.sharedImageCache storeImage:[self imageForTesting] forKey:kImageTestKey]; + [self.sharedImageCache removeImageForKey:kImageTestKey fromDisk:NO]; + expect([self.sharedImageCache imageFromDiskCacheForKey:kImageTestKey]).to.beNil; + expect([self.sharedImageCache imageFromMemoryCacheForKey:kImageTestKey]).to.beNil; +} + +- (void)testRemoveImageforKeyNotFromDiskWithCompletion{ + [self.sharedImageCache storeImage:[self imageForTesting] forKey:kImageTestKey]; + [self.sharedImageCache removeImageForKey:kImageTestKey fromDisk:NO withCompletion:^{ + expect([self.sharedImageCache imageFromDiskCacheForKey:kImageTestKey]).toNot.beNil; + expect([self.sharedImageCache imageFromMemoryCacheForKey:kImageTestKey]).to.beNil; + }]; +} + +- (void)testRemoveImageforKeyFromDiskWithCompletion{ + [self.sharedImageCache storeImage:[self imageForTesting] forKey:kImageTestKey]; + [self.sharedImageCache removeImageForKey:kImageTestKey fromDisk:YES withCompletion:^{ + expect([self.sharedImageCache imageFromDiskCacheForKey:kImageTestKey]).to.beNil; + expect([self.sharedImageCache imageFromMemoryCacheForKey:kImageTestKey]).to.beNil; + }]; +} + +// TODO -- Testing insertion with recalculate +- (void)testInsertionOfImageOnlyInDisk { +} + +- (void)testInitialCacheSize{ + expect([self.sharedImageCache getSize]).to.equal(0); +} + +- (void)testInitialDiskCount{ + [self.sharedImageCache storeImage:[self imageForTesting] forKey:kImageTestKey]; + expect([self.sharedImageCache getDiskCount]).to.equal(1); +} + +- (void)testDiskCountAfterInsertion{ + [self.sharedImageCache storeImage:[self imageForTesting] forKey:kImageTestKey]; + expect([self.sharedImageCache getDiskCount]).to.equal(1); +} + +- (void)testDefaultCachePathForAnyKey{ + NSString *path = [self.sharedImageCache defaultCachePathForKey:kImageTestKey]; + expect(path).toNot.beNil; +} + +- (void)testCachePathForNonExistingKey{ + NSString *path = [self.sharedImageCache cachePathForKey:kImageTestKey inPath:[self.sharedImageCache defaultCachePathForKey:kImageTestKey]]; + expect(path).to.beNil; +} + +- (void)testCachePathForExistingKey{ + [self.sharedImageCache storeImage:[self imageForTesting] forKey:kImageTestKey]; + NSString *path = [self.sharedImageCache cachePathForKey:kImageTestKey inPath:[self.sharedImageCache defaultCachePathForKey:kImageTestKey]]; + expect(path).notTo.beNil; +} + +#pragma mark Helper methods + +- (void)clearAllCaches{ + [self.sharedImageCache clearDisk]; + [self.sharedImageCache clearMemory]; +} + +- (UIImage *)imageForTesting{ + NSBundle *testBundle=[NSBundle bundleForClass:[self class]]; + NSString *testBundlePath=[testBundle pathForResource:@"TestImage" ofType:@"jpg"]; + return [UIImage imageWithContentsOfFile:testBundlePath]; +} + +@end \ No newline at end of file