Merge pull request #1125 from pauruiz/CacheTests
Cache tests
Showing
3 changed files
with
166 additions
and
3 deletions
@@ -7,6 +7,7 @@ | @@ -7,6 +7,7 @@ | ||
7 | objects = { | 7 | objects = { |
8 | 8 | ||
9 | /* Begin PBXBuildFile section */ | 9 | /* Begin PBXBuildFile section */ |
10 | + 5F7F38AD1AE2A77A00B0E330 /* TestImage.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 5F7F38AC1AE2A77A00B0E330 /* TestImage.jpg */; }; | ||
10 | ABC8501F672447AA91C788DA /* libPods-ios.a in Frameworks */ = {isa = PBXBuildFile; fileRef = EB0D107E6B4C4094BA2FEE29 /* libPods-ios.a */; }; | 11 | ABC8501F672447AA91C788DA /* libPods-ios.a in Frameworks */ = {isa = PBXBuildFile; fileRef = EB0D107E6B4C4094BA2FEE29 /* libPods-ios.a */; }; |
11 | DA248D57195472AA00390AB0 /* XCTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DA248D56195472AA00390AB0 /* XCTest.framework */; }; | 12 | DA248D57195472AA00390AB0 /* XCTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DA248D56195472AA00390AB0 /* XCTest.framework */; }; |
12 | DA248D59195472AA00390AB0 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DA248D58195472AA00390AB0 /* Foundation.framework */; }; | 13 | DA248D59195472AA00390AB0 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DA248D58195472AA00390AB0 /* Foundation.framework */; }; |
@@ -19,6 +20,7 @@ | @@ -19,6 +20,7 @@ | ||
19 | 20 | ||
20 | /* Begin PBXFileReference section */ | 21 | /* Begin PBXFileReference section */ |
21 | 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>"; }; | 22 | 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>"; }; |
23 | + 5F7F38AC1AE2A77A00B0E330 /* TestImage.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = TestImage.jpg; sourceTree = "<group>"; }; | ||
22 | 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>"; }; | 24 | 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>"; }; |
23 | DA248D53195472AA00390AB0 /* Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = Tests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; | 25 | DA248D53195472AA00390AB0 /* Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = Tests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; |
24 | DA248D56195472AA00390AB0 /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; }; | 26 | DA248D56195472AA00390AB0 /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; }; |
@@ -89,6 +91,7 @@ | @@ -89,6 +91,7 @@ | ||
89 | DA248D5C195472AA00390AB0 /* Tests */ = { | 91 | DA248D5C195472AA00390AB0 /* Tests */ = { |
90 | isa = PBXGroup; | 92 | isa = PBXGroup; |
91 | children = ( | 93 | children = ( |
94 | + 5F7F38AC1AE2A77A00B0E330 /* TestImage.jpg */, | ||
92 | DA248D5D195472AA00390AB0 /* Supporting Files */, | 95 | DA248D5D195472AA00390AB0 /* Supporting Files */, |
93 | DA248D68195475D800390AB0 /* SDImageCacheTests.m */, | 96 | DA248D68195475D800390AB0 /* SDImageCacheTests.m */, |
94 | DA248D6A195476AC00390AB0 /* SDWebImageManagerTests.m */, | 97 | DA248D6A195476AC00390AB0 /* SDWebImageManagerTests.m */, |
@@ -159,6 +162,7 @@ | @@ -159,6 +162,7 @@ | ||
159 | isa = PBXResourcesBuildPhase; | 162 | isa = PBXResourcesBuildPhase; |
160 | buildActionMask = 2147483647; | 163 | buildActionMask = 2147483647; |
161 | files = ( | 164 | files = ( |
165 | + 5F7F38AD1AE2A77A00B0E330 /* TestImage.jpg in Resources */, | ||
162 | DA248D61195472AA00390AB0 /* InfoPlist.strings in Resources */, | 166 | DA248D61195472AA00390AB0 /* InfoPlist.strings in Resources */, |
163 | ); | 167 | ); |
164 | runOnlyForDeploymentPostprocessing = 0; | 168 | runOnlyForDeploymentPostprocessing = 0; |
@@ -14,9 +14,10 @@ | @@ -14,9 +14,10 @@ | ||
14 | 14 | ||
15 | #import "SDImageCache.h" | 15 | #import "SDImageCache.h" |
16 | 16 | ||
17 | +NSString *kImageTestKey = @"TestImageKey"; | ||
17 | 18 | ||
18 | @interface SDImageCacheTests : XCTestCase | 19 | @interface SDImageCacheTests : XCTestCase |
19 | - | 20 | +@property (strong, nonatomic) SDImageCache *sharedImageCache; |
20 | @end | 21 | @end |
21 | 22 | ||
22 | @implementation SDImageCacheTests | 23 | @implementation SDImageCacheTests |
@@ -25,6 +26,8 @@ | @@ -25,6 +26,8 @@ | ||
25 | { | 26 | { |
26 | [super setUp]; | 27 | [super setUp]; |
27 | // Put setup code here. This method is called before the invocation of each test method in the class. | 28 | // Put setup code here. This method is called before the invocation of each test method in the class. |
29 | + self.sharedImageCache = [SDImageCache sharedImageCache]; | ||
30 | + [self clearAllCaches]; | ||
28 | } | 31 | } |
29 | 32 | ||
30 | - (void)tearDown | 33 | - (void)tearDown |
@@ -34,9 +37,165 @@ | @@ -34,9 +37,165 @@ | ||
34 | } | 37 | } |
35 | 38 | ||
36 | - (void)testSharedImageCache { | 39 | - (void)testSharedImageCache { |
37 | - SDImageCache *sharedImageCache = [SDImageCache sharedImageCache]; | 40 | + expect(self.sharedImageCache).toNot.beNil(); |
41 | +} | ||
42 | + | ||
43 | +- (void)testSingleton{ | ||
44 | + expect(self.sharedImageCache).to.equal([SDImageCache sharedImageCache]); | ||
45 | +} | ||
46 | + | ||
47 | +- (void)testClearDiskCache{ | ||
48 | + [self.sharedImageCache storeImage:[self imageForTesting] forKey:kImageTestKey]; | ||
49 | + [self.sharedImageCache clearDiskOnCompletion:^{ | ||
50 | + expect([self.sharedImageCache diskImageExistsWithKey:kImageTestKey]).to.equal(NO); | ||
51 | + expect([self.sharedImageCache imageFromMemoryCacheForKey:kImageTestKey]).to.equal([self imageForTesting]); | ||
52 | + }]; | ||
53 | +} | ||
54 | + | ||
55 | +- (void)testClearMemoryCache{ | ||
56 | + [self.sharedImageCache storeImage:[self imageForTesting] forKey:kImageTestKey]; | ||
57 | + [self.sharedImageCache clearMemory]; | ||
58 | + expect([self.sharedImageCache imageFromMemoryCacheForKey:kImageTestKey]).to.beNil; | ||
59 | + // Seems not able to access the files correctly (maybe only from test?) | ||
60 | + //expect([self.sharedImageCache diskImageExistsWithKey:kImageTestKey]).to.equal(YES); | ||
61 | + [self.sharedImageCache diskImageExistsWithKey:kImageTestKey completion:^(BOOL isInCache) { | ||
62 | + expect(isInCache).to.equal(YES); | ||
63 | + }]; | ||
64 | +} | ||
65 | + | ||
66 | +// Testing storeImage:forKey: | ||
67 | +- (void)testInsertionOfImage { | ||
68 | + UIImage *image = [self imageForTesting]; | ||
69 | + [self.sharedImageCache storeImage:image forKey:kImageTestKey]; | ||
70 | + expect([self.sharedImageCache imageFromMemoryCacheForKey:kImageTestKey]).to.equal(image); | ||
71 | + expect([self.sharedImageCache imageFromDiskCacheForKey:kImageTestKey]).to.equal(image); | ||
72 | +} | ||
73 | + | ||
74 | +// Testing storeImage:forKey:toDisk:YES | ||
75 | +- (void)testInsertionOfImageForcingDiskStorage{ | ||
76 | + UIImage *image = [self imageForTesting]; | ||
77 | + [self.sharedImageCache storeImage:image forKey:kImageTestKey toDisk:YES]; | ||
78 | + expect([self.sharedImageCache imageFromMemoryCacheForKey:kImageTestKey]).to.equal(image); | ||
79 | + // Seems not able to access the files correctly (maybe only from test?) | ||
80 | + //expect([self.sharedImageCache diskImageExistsWithKey:kImageTestKey]).to.equal(YES); | ||
81 | + [self.sharedImageCache diskImageExistsWithKey:kImageTestKey completion:^(BOOL isInCache) { | ||
82 | + expect(isInCache).to.equal(YES); | ||
83 | + }]; | ||
84 | +} | ||
85 | + | ||
86 | +// Testing storeImage:forKey:toDisk:NO | ||
87 | +- (void)testInsertionOfImageOnlyInMemory { | ||
88 | + UIImage *image = [self imageForTesting]; | ||
89 | + [self.sharedImageCache storeImage:image forKey:@"TestImage" toDisk:NO]; | ||
90 | + [self.sharedImageCache diskImageExistsWithKey:@"TestImage" completion:^(BOOL isInCache) { | ||
91 | + expect(isInCache).to.equal(YES); | ||
92 | + }]; | ||
93 | + [self.sharedImageCache clearMemory]; | ||
94 | + [self.sharedImageCache diskImageExistsWithKey:@"TestImage" completion:^(BOOL isInCache) { | ||
95 | + expect(isInCache).to.equal(NO); | ||
96 | + }]; | ||
97 | +} | ||
98 | + | ||
99 | +- (void)testRetrievalImageThroughNSOperation{ | ||
100 | + //- (NSOperation *)queryDiskCacheForKey:(NSString *)key done:(SDWebImageQueryCompletedBlock)doneBlock; | ||
101 | + UIImage *imageForTesting = [self imageForTesting]; | ||
102 | + [self.sharedImageCache storeImage:imageForTesting forKey:kImageTestKey]; | ||
103 | + NSOperation *operation = [self.sharedImageCache queryDiskCacheForKey:kImageTestKey done:^(UIImage *image, SDImageCacheType cacheType) { | ||
104 | + expect(image).to.equal(imageForTesting); | ||
105 | + }]; | ||
106 | + expect(operation).toNot.beNil; | ||
107 | +} | ||
108 | + | ||
109 | +- (void)testRemoveImageForKey{ | ||
110 | + [self.sharedImageCache storeImage:[self imageForTesting] forKey:kImageTestKey]; | ||
111 | + [self.sharedImageCache removeImageForKey:kImageTestKey]; | ||
112 | + expect([self.sharedImageCache imageFromMemoryCacheForKey:kImageTestKey]).to.beNil; | ||
113 | + expect([self.sharedImageCache imageFromDiskCacheForKey:kImageTestKey]).to.beNil; | ||
114 | +} | ||
115 | + | ||
116 | +- (void)testRemoveImageForKeyWithCompletion{ | ||
117 | + [self.sharedImageCache storeImage:[self imageForTesting] forKey:kImageTestKey]; | ||
118 | + [self.sharedImageCache removeImageForKey:kImageTestKey withCompletion:^{ | ||
119 | + expect([self.sharedImageCache imageFromDiskCacheForKey:kImageTestKey]).to.beNil; | ||
120 | + expect([self.sharedImageCache imageFromMemoryCacheForKey:kImageTestKey]).to.beNil; | ||
121 | + }]; | ||
122 | +} | ||
123 | + | ||
124 | +- (void)testRemoveImageForKeyNotFromDisk{ | ||
125 | + [self.sharedImageCache storeImage:[self imageForTesting] forKey:kImageTestKey]; | ||
126 | + [self.sharedImageCache removeImageForKey:kImageTestKey fromDisk:NO]; | ||
127 | + expect([self.sharedImageCache imageFromDiskCacheForKey:kImageTestKey]).toNot.beNil; | ||
128 | + expect([self.sharedImageCache imageFromMemoryCacheForKey:kImageTestKey]).to.beNil; | ||
129 | +} | ||
130 | + | ||
131 | +- (void)testRemoveImageForKeyFromDisk{ | ||
132 | + [self.sharedImageCache storeImage:[self imageForTesting] forKey:kImageTestKey]; | ||
133 | + [self.sharedImageCache removeImageForKey:kImageTestKey fromDisk:NO]; | ||
134 | + expect([self.sharedImageCache imageFromDiskCacheForKey:kImageTestKey]).to.beNil; | ||
135 | + expect([self.sharedImageCache imageFromMemoryCacheForKey:kImageTestKey]).to.beNil; | ||
136 | +} | ||
137 | + | ||
138 | +- (void)testRemoveImageforKeyNotFromDiskWithCompletion{ | ||
139 | + [self.sharedImageCache storeImage:[self imageForTesting] forKey:kImageTestKey]; | ||
140 | + [self.sharedImageCache removeImageForKey:kImageTestKey fromDisk:NO withCompletion:^{ | ||
141 | + expect([self.sharedImageCache imageFromDiskCacheForKey:kImageTestKey]).toNot.beNil; | ||
142 | + expect([self.sharedImageCache imageFromMemoryCacheForKey:kImageTestKey]).to.beNil; | ||
143 | + }]; | ||
144 | +} | ||
145 | + | ||
146 | +- (void)testRemoveImageforKeyFromDiskWithCompletion{ | ||
147 | + [self.sharedImageCache storeImage:[self imageForTesting] forKey:kImageTestKey]; | ||
148 | + [self.sharedImageCache removeImageForKey:kImageTestKey fromDisk:YES withCompletion:^{ | ||
149 | + expect([self.sharedImageCache imageFromDiskCacheForKey:kImageTestKey]).to.beNil; | ||
150 | + expect([self.sharedImageCache imageFromMemoryCacheForKey:kImageTestKey]).to.beNil; | ||
151 | + }]; | ||
152 | +} | ||
153 | + | ||
154 | +// TODO -- Testing insertion with recalculate | ||
155 | +- (void)testInsertionOfImageOnlyInDisk { | ||
156 | +} | ||
157 | + | ||
158 | +- (void)testInitialCacheSize{ | ||
159 | + expect([self.sharedImageCache getSize]).to.equal(0); | ||
160 | +} | ||
161 | + | ||
162 | +- (void)testInitialDiskCount{ | ||
163 | + [self.sharedImageCache storeImage:[self imageForTesting] forKey:kImageTestKey]; | ||
164 | + expect([self.sharedImageCache getDiskCount]).to.equal(1); | ||
165 | +} | ||
166 | + | ||
167 | +- (void)testDiskCountAfterInsertion{ | ||
168 | + [self.sharedImageCache storeImage:[self imageForTesting] forKey:kImageTestKey]; | ||
169 | + expect([self.sharedImageCache getDiskCount]).to.equal(1); | ||
170 | +} | ||
171 | + | ||
172 | +- (void)testDefaultCachePathForAnyKey{ | ||
173 | + NSString *path = [self.sharedImageCache defaultCachePathForKey:kImageTestKey]; | ||
174 | + expect(path).toNot.beNil; | ||
175 | +} | ||
176 | + | ||
177 | +- (void)testCachePathForNonExistingKey{ | ||
178 | + NSString *path = [self.sharedImageCache cachePathForKey:kImageTestKey inPath:[self.sharedImageCache defaultCachePathForKey:kImageTestKey]]; | ||
179 | + expect(path).to.beNil; | ||
180 | +} | ||
181 | + | ||
182 | +- (void)testCachePathForExistingKey{ | ||
183 | + [self.sharedImageCache storeImage:[self imageForTesting] forKey:kImageTestKey]; | ||
184 | + NSString *path = [self.sharedImageCache cachePathForKey:kImageTestKey inPath:[self.sharedImageCache defaultCachePathForKey:kImageTestKey]]; | ||
185 | + expect(path).notTo.beNil; | ||
186 | +} | ||
187 | + | ||
188 | +#pragma mark Helper methods | ||
189 | + | ||
190 | +- (void)clearAllCaches{ | ||
191 | + [self.sharedImageCache clearDisk]; | ||
192 | + [self.sharedImageCache clearMemory]; | ||
193 | +} | ||
38 | 194 | ||
39 | - expect(sharedImageCache).toNot.beNil(); | 195 | +- (UIImage *)imageForTesting{ |
196 | + NSBundle *testBundle=[NSBundle bundleForClass:[self class]]; | ||
197 | + NSString *testBundlePath=[testBundle pathForResource:@"TestImage" ofType:@"jpg"]; | ||
198 | + return [UIImage imageWithContentsOfFile:testBundlePath]; | ||
40 | } | 199 | } |
41 | 200 | ||
42 | @end | 201 | @end |
Tests/Tests/TestImage.jpg
0 → 100644

3.13 KB
-
Please register or login to post a comment