Преглед изворни кода

Move `manager.cachedImageExistsforURL` to `ImageCache` as discussed.

Claire Knight пре 10 година
родитељ
комит
678a02024a

+ 15 - 0
Sources/ImageCache.swift

@@ -552,6 +552,21 @@ extension ImageCache {
         public let cacheType: CacheType?
     }
     
+    /**
+     Determine if a cached image exists for the given image, as keyed by the URL. It will return true if the
+     image is found either in memory or on disk. Essentially as long as there is a cache of the image somewhere
+     true is returned. A convenience method that decodes `isImageCachedForKey`.
+     
+     - parameter url: The image URL.
+     
+     - returns: True if the image is cached, false otherwise.
+     */
+    public func cachedImageExistsforURL(url: NSURL) -> Bool {
+        let resource = Resource(downloadURL: url)
+        let result = isImageCachedForKey(resource.cacheKey)
+        return result.cached
+    }
+
     /**
     Check whether an image is cached for a key.
     

+ 0 - 15
Sources/KingfisherManager.swift

@@ -163,21 +163,6 @@ public class KingfisherManager {
         return retrieveImageWithResource(Resource(downloadURL: URL), optionsInfo: optionsInfo, progressBlock: progressBlock, completionHandler: completionHandler)
     }
     
-    /**
-     Determine if a cached image exists for the given image, as keyed by the URL. It will return true if the
-     image is found either in memory or on disk. Essentially as long as there is a cache of the image somewhere
-     true is returned.
-     
-     - parameter url: The image URL.
-     
-     - returns: True if the image is cached, false otherwise.
-     */
-    public func cachedImageExistsforURL(url: NSURL) -> Bool {
-        let resource = Resource(downloadURL: url)
-        let result = cache.isImageCachedForKey(resource.cacheKey)
-        return result.cached
-    }
-
     func downloadAndCacheImageWithURL(URL: NSURL,
                                forKey key: String,
                         retrieveImageTask: RetrieveImageTask,

+ 46 - 0
Tests/KingfisherTests/ImageCacheTests.swift

@@ -153,6 +153,52 @@ class ImageCacheTests: XCTestCase {
         waitForExpectationsWithTimeout(5, handler: nil)
     }
     
+    func testCachedFileExists() {
+        let expectation = expectationWithDescription("cache does contain image")
+        
+        let URLString = testKeys[0]
+        let URL = NSURL(string: URLString)!
+        
+        let exists = cache.cachedImageExistsforURL(URL)
+        XCTAssertFalse(exists)
+        
+        cache.retrieveImageForKey(URLString, options: nil, completionHandler: { (image, type) -> () in
+            XCTAssertNil(image, "Should not be cached yet")
+            XCTAssertEqual(type, nil)
+
+            self.cache.storeImage(testImage, forKey: URLString, toDisk: true) { () -> () in
+                self.cache.retrieveImageForKey(URLString, options: nil, completionHandler: { (image, type) -> () in
+                    XCTAssertNotNil(image, "Should be cached (memory or disk)")
+                    XCTAssertEqual(type, CacheType.Memory)
+
+                    let exists = self.cache.cachedImageExistsforURL(URL)
+                    XCTAssertTrue(exists, "Image should exist in the cache (memory or disk)")
+
+                    self.cache.clearMemoryCache()
+                    self.cache.retrieveImageForKey(URLString, options: nil, completionHandler: { (image, type) -> () in
+                        XCTAssertNotNil(image, "Should be cached (disk)")
+                        XCTAssertEqual(type, CacheType.Disk)
+                        
+                        let exists = self.cache.cachedImageExistsforURL(URL)
+                        XCTAssertTrue(exists, "Image should exist in the cache (disk)")
+                        
+                        expectation.fulfill()
+                    })
+                })
+            }
+        })
+        
+        waitForExpectationsWithTimeout(5, handler: nil)
+    }
+    
+    func testCachedFileDoesNotExist() {
+        let URLString = testKeys[0]
+        let URL = NSURL(string: URLString)!
+        
+        let exists = cache.cachedImageExistsforURL(URL)
+        XCTAssertFalse(exists)
+    }
+
     func testIsImageCachedForKey() {
         let expectation = self.expectationWithDescription("wait for caching image")
         

+ 0 - 48
Tests/KingfisherTests/KingfisherManagerTests.swift

@@ -203,52 +203,4 @@ class KingfisherManagerTests: XCTestCase {
         })
         waitForExpectationsWithTimeout(5, handler: nil)
     }
-    
-    func testCachedFileExists() {
-        let expectation = expectationWithDescription("cache does contain image")
-        let URLString = testKeys[0]
-        stubRequest("GET", URLString).andReturn(200).withBody(testImageData)
-        
-        let URL = NSURL(string: URLString)!
-        
-        let exists = manager.cachedImageExistsforURL(URL)
-        XCTAssertFalse(exists)
-        
-        manager.retrieveImageWithURL(URL, optionsInfo: nil, progressBlock: nil) {
-            image, error, cacheType, imageURL in
-            XCTAssertNotNil(image)
-            XCTAssertEqual(cacheType, CacheType.None)
-            
-            self.manager.retrieveImageWithURL(URL, optionsInfo: nil, progressBlock: nil) {
-                image, error, cacheType, imageURL in
-                XCTAssertNotNil(image)
-                XCTAssertEqual(cacheType, CacheType.Memory)
-                
-                let exists = self.manager.cachedImageExistsforURL(URL)
-                XCTAssertTrue(exists)
-
-                self.manager.cache.clearMemoryCache()
-                self.manager.retrieveImageWithURL(URL, optionsInfo: nil, progressBlock: nil) {
-                    image, error, cacheType, imageURL in
-                    XCTAssertNotNil(image)
-                    XCTAssertEqual(cacheType, CacheType.Disk)
-                    
-                    let exists = self.manager.cachedImageExistsforURL(URL)
-                    XCTAssertTrue(exists)
-
-                    expectation.fulfill()
-                }
-            }
-        }
-        
-        waitForExpectationsWithTimeout(5, handler: nil)
-    }
-    
-    func testCachedFileDoesNotExist() {
-        let URLString = testKeys[0]
-        let URL = NSURL(string: URLString)!
-        
-        let exists = manager.cachedImageExistsforURL(URL)
-        XCTAssertFalse(exists)
-    }
 }