Merge pull request #1125 from pauruiz/CacheTests

Cache tests
This commit is contained in:
Bogdan Poplauschi 2015-07-14 16:39:47 +03:00
commit 0373b97f71
3 changed files with 168 additions and 5 deletions

View File

@ -7,6 +7,7 @@
objects = {
/* Begin PBXBuildFile section */
5F7F38AD1AE2A77A00B0E330 /* TestImage.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 5F7F38AC1AE2A77A00B0E330 /* TestImage.jpg */; };
ABC8501F672447AA91C788DA /* libPods-ios.a in Frameworks */ = {isa = PBXBuildFile; fileRef = EB0D107E6B4C4094BA2FEE29 /* libPods-ios.a */; };
DA248D57195472AA00390AB0 /* XCTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DA248D56195472AA00390AB0 /* XCTest.framework */; };
DA248D59195472AA00390AB0 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DA248D58195472AA00390AB0 /* Foundation.framework */; };
@ -19,6 +20,7 @@
/* Begin PBXFileReference section */
1A6DF883515E8008203AB352 /* Pods-ios.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ios.debug.xcconfig"; path = "Pods/Target Support Files/Pods-ios/Pods-ios.debug.xcconfig"; sourceTree = "<group>"; };
5F7F38AC1AE2A77A00B0E330 /* TestImage.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = TestImage.jpg; sourceTree = "<group>"; };
CA88E6BDE3581B2BFE933C10 /* Pods-ios.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ios.release.xcconfig"; path = "Pods/Target Support Files/Pods-ios/Pods-ios.release.xcconfig"; sourceTree = "<group>"; };
DA248D53195472AA00390AB0 /* Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = Tests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
DA248D56195472AA00390AB0 /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; };
@ -89,6 +91,7 @@
DA248D5C195472AA00390AB0 /* Tests */ = {
isa = PBXGroup;
children = (
5F7F38AC1AE2A77A00B0E330 /* TestImage.jpg */,
DA248D5D195472AA00390AB0 /* Supporting Files */,
DA248D68195475D800390AB0 /* SDImageCacheTests.m */,
DA248D6A195476AC00390AB0 /* SDWebImageManagerTests.m */,
@ -159,6 +162,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
5F7F38AD1AE2A77A00B0E330 /* TestImage.jpg in Resources */,
DA248D61195472AA00390AB0 /* InfoPlist.strings in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;

View File

@ -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

BIN
Tests/Tests/TestImage.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB