14
14
15
15
#import " SDImageCache.h"
16
16
17
+ NSString *kImageTestKey = @" TestImageKey" ;
17
18
18
19
@interface SDImageCacheTests : XCTestCase
19
-
20
+ @property ( strong , nonatomic ) SDImageCache *sharedImageCache;
20
21
@end
21
22
22
23
@implementation SDImageCacheTests
@@ -25,6 +26,8 @@ - (void)setUp
25
26
{
26
27
[super setUp ];
27
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
33
- (void )tearDown
@@ -34,9 +37,165 @@ - (void)tearDown
34
37
}
35
38
36
39
- (void )testSharedImageCache {
37
- SDImageCache *sharedImageCache = [SDImageCache sharedImageCache ];
38
-
39
- expect (sharedImageCache).toNot .beNil ();
40
+ expect (self.sharedImageCache ).toNot .beNil ();
40
41
}
41
42
42
- @end
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
+ }
194
+
195
+ - (UIImage *)imageForTesting {
196
+ NSBundle *testBundle=[NSBundle bundleForClass: [self class ]];
197
+ NSString *testBundlePath=[testBundle pathForResource: @" TestImage" ofType: @" jpg" ];
198
+ return [UIImage imageWithContentsOfFile: testBundlePath];
199
+ }
200
+
201
+ @end
0 commit comments