Ver Fonte

Merge branch '1.4.0'

onevcat há 10 anos atrás
pai
commit
b17f972e58

+ 2 - 0
Kingfisher/ImageCache.swift

@@ -513,11 +513,13 @@ public extension ImageCache {
     :returns: The check result.
     :returns: The check result.
     */
     */
     public func isImageCachedForKey(key: String) -> CacheCheckResult {
     public func isImageCachedForKey(key: String) -> CacheCheckResult {
+        
         if memoryCache.objectForKey(key) != nil {
         if memoryCache.objectForKey(key) != nil {
             return CacheCheckResult(cached: true, cacheType: .Memory)
             return CacheCheckResult(cached: true, cacheType: .Memory)
         }
         }
         
         
         let filePath = cachePathForKey(key)
         let filePath = cachePathForKey(key)
+        
         if fileManager.fileExistsAtPath(filePath) {
         if fileManager.fileExistsAtPath(filePath) {
             return CacheCheckResult(cached: true, cacheType: .Disk)
             return CacheCheckResult(cached: true, cacheType: .Disk)
         }
         }

+ 2 - 1
Kingfisher/KingfisherManager.swift

@@ -192,10 +192,11 @@ public class KingfisherManager {
                 return
                 return
             }
             }
             
             
-            completionHandler?(image: image, error: error, cacheType: .None, imageURL: URL)
             if let image = image {
             if let image = image {
                 targetCache.storeImage(image, forKey: key, toDisk: !options.cacheMemoryOnly, completionHandler: nil)
                 targetCache.storeImage(image, forKey: key, toDisk: !options.cacheMemoryOnly, completionHandler: nil)
             }
             }
+            
+            completionHandler?(image: image, error: error, cacheType: .None, imageURL: URL)
         }
         }
     }
     }
 }
 }

+ 27 - 24
KingfisherTests/ImageCacheTests.swift

@@ -45,6 +45,7 @@ class ImageCacheTests: XCTestCase {
     override func tearDown() {
     override func tearDown() {
         // Put teardown code here. This method is called after the invocation of each test method in the class.
         // Put teardown code here. This method is called after the invocation of each test method in the class.
         super.tearDown()
         super.tearDown()
+        cache.clearMemoryCache()
         cache.clearDiskCache()
         cache.clearDiskCache()
         cache = nil
         cache = nil
         observer = nil
         observer = nil
@@ -70,20 +71,21 @@ class ImageCacheTests: XCTestCase {
         let diskCachePath = paths.first!.stringByAppendingPathComponent(cacheName)
         let diskCachePath = paths.first!.stringByAppendingPathComponent(cacheName)
         
         
         let expectation = expectationWithDescription("wait for clearing disk cache")
         let expectation = expectationWithDescription("wait for clearing disk cache")
+        let key = testKeys[0]
+        
+        cache.storeImage(testImage, forKey: key, toDisk: true) { () -> () in
+            self.cache.clearMemoryCache()
+            let cacheResult = self.cache.isImageCachedForKey(key)
+            XCTAssertTrue(cacheResult.cached, "Should be cached")
+            XCTAssert(cacheResult.cacheType == .Disk, "Should be cached in disk")
         
         
-        cache.storeImage(testImage, forKey: testKeys[0], toDisk: true) { () -> () in
-            
-            let files = NSFileManager.defaultManager().contentsOfDirectoryAtPath(diskCachePath, error:nil)
-            XCTAssert(files?.count == 1, "Should be 1 file at the path")
-            
             self.cache.clearDiskCacheWithCompletionHandler { () -> () in
             self.cache.clearDiskCacheWithCompletionHandler { () -> () in
-                
-                let files = NSFileManager.defaultManager().contentsOfDirectoryAtPath(diskCachePath, error:nil)
-                XCTAssert(files?.count == 0, "Files should be at deleted")
+                let cacheResult = self.cache.isImageCachedForKey(key)
+                XCTAssertFalse(cacheResult.cached, "Should be not cached")
                 expectation.fulfill()
                 expectation.fulfill()
             }
             }
         }
         }
-        waitForExpectationsWithTimeout(1, handler:nil)
+        waitForExpectationsWithTimeout(10, handler:nil)
     }
     }
     
     
     func testClearMemoryCache() {
     func testClearMemoryCache() {
@@ -97,7 +99,7 @@ class ImageCacheTests: XCTestCase {
             })
             })
         }
         }
         
         
-        waitForExpectationsWithTimeout(1, handler: nil)
+        waitForExpectationsWithTimeout(5, handler: nil)
     }
     }
     
     
     func testNoImageFound() {
     func testNoImageFound() {
@@ -111,7 +113,7 @@ class ImageCacheTests: XCTestCase {
             return
             return
         }
         }
         
         
-        waitForExpectationsWithTimeout(1, handler: nil)
+        waitForExpectationsWithTimeout(5, handler: nil)
     }
     }
     
     
     func testStoreImageInMemory() {
     func testStoreImageInMemory() {
@@ -125,7 +127,7 @@ class ImageCacheTests: XCTestCase {
             return
             return
         }
         }
         
         
-        waitForExpectationsWithTimeout(1, handler: nil)
+        waitForExpectationsWithTimeout(5, handler: nil)
     }
     }
     
     
     func testStoreMultipleImages() {
     func testStoreMultipleImages() {
@@ -141,25 +143,26 @@ class ImageCacheTests: XCTestCase {
             expectation.fulfill()
             expectation.fulfill()
         }
         }
         
         
-        waitForExpectationsWithTimeout(1, handler: nil)
+        waitForExpectationsWithTimeout(5, handler: nil)
     }
     }
     
     
     func testIsImageCachedForKey() {
     func testIsImageCachedForKey() {
-        XCTAssert(cache.isImageCachedForKey(testKeys[0]).cached == false, "This image should not be cached yet.")
-
-        let expectation = expectationWithDescription("wait for caching image")
-        cache.storeImage(testImage, forKey: testKeys[0], toDisk: true) { () -> () in
-            XCTAssert(self.cache.isImageCachedForKey(testKeys[0]).cached == true, "This image should be already cached.")
-            expectation.fulfill()
-        }
+        let expectation = self.expectationWithDescription("wait for caching image")
         
         
-        waitForExpectationsWithTimeout(1, handler: nil)
+        let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(0.5 * Double(NSEC_PER_SEC)))
+        dispatch_after(delayTime, dispatch_get_main_queue()) {
+            XCTAssert(self.cache.isImageCachedForKey(testKeys[0]).cached == false, "This image should not be cached yet.")
+            self.cache.storeImage(testImage, forKey: testKeys[0], toDisk: true) { () -> () in
+                XCTAssert(self.cache.isImageCachedForKey(testKeys[0]).cached == true, "This image should be already cached.")
+                expectation.fulfill()
+            }
+        }
+        self.waitForExpectationsWithTimeout(5, handler: nil)
     }
     }
     
     
     func testRetrivingImagePerformance() {
     func testRetrivingImagePerformance() {
 
 
         let expectation = self.expectationWithDescription("wait for retriving image")
         let expectation = self.expectationWithDescription("wait for retriving image")
-        
         self.cache.storeImage(testImage, forKey: testKeys[0], toDisk: true) { () -> () in
         self.cache.storeImage(testImage, forKey: testKeys[0], toDisk: true) { () -> () in
             self.measureBlock({ () -> Void in
             self.measureBlock({ () -> Void in
                 for _ in 1 ..< 1000 {
                 for _ in 1 ..< 1000 {
@@ -169,7 +172,7 @@ class ImageCacheTests: XCTestCase {
             expectation.fulfill()
             expectation.fulfill()
         }
         }
         
         
-        self.waitForExpectationsWithTimeout(15, handler: nil)
+        self.waitForExpectationsWithTimeout(20, handler: nil)
     }
     }
     
     
     func testCleanDiskCacheNotification() {
     func testCleanDiskCacheNotification() {
@@ -194,7 +197,7 @@ class ImageCacheTests: XCTestCase {
             self.cache.cleanExpiredDiskCache()
             self.cache.cleanExpiredDiskCache()
         }
         }
         
         
-        waitForExpectationsWithTimeout(1, handler: nil)
+        waitForExpectationsWithTimeout(5, handler: nil)
     }
     }
 
 
     // MARK: - Helper
     // MARK: - Helper

+ 6 - 6
KingfisherTests/ImageDownloaderTests.swift

@@ -69,7 +69,7 @@ class ImageDownloaderTests: XCTestCase {
             XCTAssert(image != nil, "Download should be able to finished for URL: \(imageURL)")
             XCTAssert(image != nil, "Download should be able to finished for URL: \(imageURL)")
         }
         }
         
         
-        waitForExpectationsWithTimeout(1, handler: nil)
+        waitForExpectationsWithTimeout(5, handler: nil)
     }
     }
     
     
     func testDownloadMultipleImages() {
     func testDownloadMultipleImages() {
@@ -94,7 +94,7 @@ class ImageDownloaderTests: XCTestCase {
             expectation.fulfill()
             expectation.fulfill()
         }
         }
         
         
-        waitForExpectationsWithTimeout(1, handler: nil)
+        waitForExpectationsWithTimeout(5, handler: nil)
     }
     }
     
     
     func testDownloadAnImageWithMultipleCallback() {
     func testDownloadAnImageWithMultipleCallback() {
@@ -119,7 +119,7 @@ class ImageDownloaderTests: XCTestCase {
             expectation.fulfill()
             expectation.fulfill()
         }
         }
         
         
-        waitForExpectationsWithTimeout(1, handler: nil)
+        waitForExpectationsWithTimeout(5, handler: nil)
     }
     }
     
     
     func testDownloadWithModifyingRequest() {
     func testDownloadWithModifyingRequest() {
@@ -141,7 +141,7 @@ class ImageDownloaderTests: XCTestCase {
             XCTAssertEqual(imageURL!, NSURL(string: URLString)!, "The returned imageURL should be the replaced one")
             XCTAssertEqual(imageURL!, NSURL(string: URLString)!, "The returned imageURL should be the replaced one")
             expectation.fulfill()
             expectation.fulfill()
         }
         }
-        waitForExpectationsWithTimeout(1, handler: nil)
+        waitForExpectationsWithTimeout(5, handler: nil)
     }
     }
     
     
     func testServerNotModifiedResponse() {
     func testServerNotModifiedResponse() {
@@ -157,7 +157,7 @@ class ImageDownloaderTests: XCTestCase {
             XCTAssertEqual(error!.code, KingfisherError.NotModified.rawValue, "The error should be NotModified.")
             XCTAssertEqual(error!.code, KingfisherError.NotModified.rawValue, "The error should be NotModified.")
             expectation.fulfill()
             expectation.fulfill()
         }
         }
-        waitForExpectationsWithTimeout(1, handler: nil)
+        waitForExpectationsWithTimeout(5, handler: nil)
     }
     }
     
     
     // Since we could not receive one challage, no test for trusted hosts currently.
     // Since we could not receive one challage, no test for trusted hosts currently.
@@ -176,7 +176,7 @@ class ImageDownloaderTests: XCTestCase {
             LSNocilla.sharedInstance().start()
             LSNocilla.sharedInstance().start()
         })
         })
         
         
-        waitForExpectationsWithTimeout(10) { (error) in
+        waitForExpectationsWithTimeout(20) { (error) in
             XCTAssertNil(error, "\(error)")
             XCTAssertNil(error, "\(error)")
             LSNocilla.sharedInstance().start()
             LSNocilla.sharedInstance().start()
         }
         }

+ 2 - 2
KingfisherTests/UIButtonExtensionTests.swift

@@ -79,7 +79,7 @@ class UIButtonExtensionTests: XCTestCase {
             XCTAssert(self.button.kf_webURLForState(UIControlState.Highlighted) == imageURL, "Web URL should equal to the downloaded url.")
             XCTAssert(self.button.kf_webURLForState(UIControlState.Highlighted) == imageURL, "Web URL should equal to the downloaded url.")
             XCTAssert(cacheType == .None, "cacheType should be .None since the image was just downloaded.")
             XCTAssert(cacheType == .None, "cacheType should be .None since the image was just downloaded.")
         }
         }
-        waitForExpectationsWithTimeout(1, handler: nil)
+        waitForExpectationsWithTimeout(5, handler: nil)
     }
     }
     
     
     func testDownloadAndSetBackgroundImage() {
     func testDownloadAndSetBackgroundImage() {
@@ -103,6 +103,6 @@ class UIButtonExtensionTests: XCTestCase {
                 XCTAssert(cacheType == .None, "cacheType should be .None since the image was just downloaded.")
                 XCTAssert(cacheType == .None, "cacheType should be .None since the image was just downloaded.")
 
 
         }
         }
-        waitForExpectationsWithTimeout(1, handler: nil)
+        waitForExpectationsWithTimeout(5, handler: nil)
     }
     }
 }
 }

+ 5 - 4
KingfisherTests/UIImageViewExtensionTests.swift

@@ -82,7 +82,7 @@ class UIImageViewExtensionTests: XCTestCase {
             XCTAssert(cacheType == .None, "The cache type should be none here. This image was just downloaded.")
             XCTAssert(cacheType == .None, "The cache type should be none here. This image was just downloaded.")
         }
         }
         
         
-        waitForExpectationsWithTimeout(1, handler: nil)
+        waitForExpectationsWithTimeout(5, handler: nil)
     }
     }
     
     
     func testImageDownloadCancelForImageView() {
     func testImageDownloadCancelForImageView() {
@@ -108,7 +108,7 @@ class UIImageViewExtensionTests: XCTestCase {
             XCTAssert(completionBlockIsCalled == false, "CompletionBlock should not be called since it is canceled.")
             XCTAssert(completionBlockIsCalled == false, "CompletionBlock should not be called since it is canceled.")
         }
         }
         
         
-        waitForExpectationsWithTimeout(0.1, handler: nil)
+        waitForExpectationsWithTimeout(5, handler: nil)
     }
     }
 
 
     func testImageDownloadCacelPartialTask() {
     func testImageDownloadCacelPartialTask() {
@@ -149,7 +149,7 @@ class UIImageViewExtensionTests: XCTestCase {
             XCTAssert(task3Completion == true, "Task 3 should be completed.")
             XCTAssert(task3Completion == true, "Task 3 should be completed.")
         }
         }
         
         
-        waitForExpectationsWithTimeout(0.1, handler: nil)
+        waitForExpectationsWithTimeout(5, handler: nil)
     }
     }
     
     
     func testImageDownalodMultipleCaches() {
     func testImageDownalodMultipleCaches() {
@@ -174,6 +174,7 @@ class UIImageViewExtensionTests: XCTestCase {
             self.imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: [.TargetCache: cache2], progressBlock: { (receivedSize, totalSize) -> () in
             self.imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: [.TargetCache: cache2], progressBlock: { (receivedSize, totalSize) -> () in
                 
                 
             }, completionHandler: { (image, error, cacheType, imageURL) -> () in
             }, completionHandler: { (image, error, cacheType, imageURL) -> () in
+                
                 XCTAssertTrue(cache1.isImageCachedForKey(URLString).cached, "This image should be cached in cache1.")
                 XCTAssertTrue(cache1.isImageCachedForKey(URLString).cached, "This image should be cached in cache1.")
                 XCTAssertTrue(cache2.isImageCachedForKey(URLString).cached, "This image should be cached in cache2.")
                 XCTAssertTrue(cache2.isImageCachedForKey(URLString).cached, "This image should be cached in cache2.")
                 XCTAssertFalse(KingfisherManager.sharedManager.cache.isImageCachedForKey(URLString).cached, "This image should not be cached in default cache.")
                 XCTAssertFalse(KingfisherManager.sharedManager.cache.isImageCachedForKey(URLString).cached, "This image should not be cached in default cache.")
@@ -185,7 +186,7 @@ class UIImageViewExtensionTests: XCTestCase {
             
             
         }
         }
         
         
-        waitForExpectationsWithTimeout(0.1, handler: { (error) -> Void in
+        waitForExpectationsWithTimeout(5, handler: { (error) -> Void in
             clearCaches([cache1, cache2])
             clearCaches([cache1, cache2])
         })
         })
     }
     }