Skip to content

Commit

Permalink
Merge branch '1.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
onevcat committed May 6, 2015
2 parents b46e059 + 4dcf563 commit b17f972
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 37 deletions.
2 changes: 2 additions & 0 deletions Kingfisher/ImageCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -513,11 +513,13 @@ public extension ImageCache {
:returns: The check result.
*/
public func isImageCachedForKey(key: String) -> CacheCheckResult {

if memoryCache.objectForKey(key) != nil {
return CacheCheckResult(cached: true, cacheType: .Memory)
}

let filePath = cachePathForKey(key)

if fileManager.fileExistsAtPath(filePath) {
return CacheCheckResult(cached: true, cacheType: .Disk)
}
Expand Down
3 changes: 2 additions & 1 deletion Kingfisher/KingfisherManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,11 @@ public class KingfisherManager {
return
}

completionHandler?(image: image, error: error, cacheType: .None, imageURL: URL)
if let image = image {
targetCache.storeImage(image, forKey: key, toDisk: !options.cacheMemoryOnly, completionHandler: nil)
}

completionHandler?(image: image, error: error, cacheType: .None, imageURL: URL)
}
}
}
Expand Down
51 changes: 27 additions & 24 deletions KingfisherTests/ImageCacheTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class ImageCacheTests: XCTestCase {
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
cache.clearMemoryCache()
cache.clearDiskCache()
cache = nil
observer = nil
Expand All @@ -70,20 +71,21 @@ class ImageCacheTests: XCTestCase {
let diskCachePath = paths.first!.stringByAppendingPathComponent(cacheName)

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

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()
}
}
waitForExpectationsWithTimeout(1, handler:nil)
waitForExpectationsWithTimeout(10, handler:nil)
}

func testClearMemoryCache() {
Expand All @@ -97,7 +99,7 @@ class ImageCacheTests: XCTestCase {
})
}

waitForExpectationsWithTimeout(1, handler: nil)
waitForExpectationsWithTimeout(5, handler: nil)
}

func testNoImageFound() {
Expand All @@ -111,7 +113,7 @@ class ImageCacheTests: XCTestCase {
return
}

waitForExpectationsWithTimeout(1, handler: nil)
waitForExpectationsWithTimeout(5, handler: nil)
}

func testStoreImageInMemory() {
Expand All @@ -125,7 +127,7 @@ class ImageCacheTests: XCTestCase {
return
}

waitForExpectationsWithTimeout(1, handler: nil)
waitForExpectationsWithTimeout(5, handler: nil)
}

func testStoreMultipleImages() {
Expand All @@ -141,25 +143,26 @@ class ImageCacheTests: XCTestCase {
expectation.fulfill()
}

waitForExpectationsWithTimeout(1, handler: nil)
waitForExpectationsWithTimeout(5, handler: nil)
}

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() {

let expectation = self.expectationWithDescription("wait for retriving image")

self.cache.storeImage(testImage, forKey: testKeys[0], toDisk: true) { () -> () in
self.measureBlock({ () -> Void in
for _ in 1 ..< 1000 {
Expand All @@ -169,7 +172,7 @@ class ImageCacheTests: XCTestCase {
expectation.fulfill()
}

self.waitForExpectationsWithTimeout(15, handler: nil)
self.waitForExpectationsWithTimeout(20, handler: nil)
}

func testCleanDiskCacheNotification() {
Expand All @@ -194,7 +197,7 @@ class ImageCacheTests: XCTestCase {
self.cache.cleanExpiredDiskCache()
}

waitForExpectationsWithTimeout(1, handler: nil)
waitForExpectationsWithTimeout(5, handler: nil)
}

// MARK: - Helper
Expand Down
12 changes: 6 additions & 6 deletions KingfisherTests/ImageDownloaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class ImageDownloaderTests: XCTestCase {
XCTAssert(image != nil, "Download should be able to finished for URL: \(imageURL)")
}

waitForExpectationsWithTimeout(1, handler: nil)
waitForExpectationsWithTimeout(5, handler: nil)
}

func testDownloadMultipleImages() {
Expand All @@ -94,7 +94,7 @@ class ImageDownloaderTests: XCTestCase {
expectation.fulfill()
}

waitForExpectationsWithTimeout(1, handler: nil)
waitForExpectationsWithTimeout(5, handler: nil)
}

func testDownloadAnImageWithMultipleCallback() {
Expand All @@ -119,7 +119,7 @@ class ImageDownloaderTests: XCTestCase {
expectation.fulfill()
}

waitForExpectationsWithTimeout(1, handler: nil)
waitForExpectationsWithTimeout(5, handler: nil)
}

func testDownloadWithModifyingRequest() {
Expand All @@ -141,7 +141,7 @@ class ImageDownloaderTests: XCTestCase {
XCTAssertEqual(imageURL!, NSURL(string: URLString)!, "The returned imageURL should be the replaced one")
expectation.fulfill()
}
waitForExpectationsWithTimeout(1, handler: nil)
waitForExpectationsWithTimeout(5, handler: nil)
}

func testServerNotModifiedResponse() {
Expand All @@ -157,7 +157,7 @@ class ImageDownloaderTests: XCTestCase {
XCTAssertEqual(error!.code, KingfisherError.NotModified.rawValue, "The error should be NotModified.")
expectation.fulfill()
}
waitForExpectationsWithTimeout(1, handler: nil)
waitForExpectationsWithTimeout(5, handler: nil)
}

// Since we could not receive one challage, no test for trusted hosts currently.
Expand All @@ -176,7 +176,7 @@ class ImageDownloaderTests: XCTestCase {
LSNocilla.sharedInstance().start()
})

waitForExpectationsWithTimeout(10) { (error) in
waitForExpectationsWithTimeout(20) { (error) in
XCTAssertNil(error, "\(error)")
LSNocilla.sharedInstance().start()
}
Expand Down
4 changes: 2 additions & 2 deletions KingfisherTests/UIButtonExtensionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class UIButtonExtensionTests: XCTestCase {
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.")
}
waitForExpectationsWithTimeout(1, handler: nil)
waitForExpectationsWithTimeout(5, handler: nil)
}

func testDownloadAndSetBackgroundImage() {
Expand All @@ -103,6 +103,6 @@ class UIButtonExtensionTests: XCTestCase {
XCTAssert(cacheType == .None, "cacheType should be .None since the image was just downloaded.")

}
waitForExpectationsWithTimeout(1, handler: nil)
waitForExpectationsWithTimeout(5, handler: nil)
}
}
9 changes: 5 additions & 4 deletions KingfisherTests/UIImageViewExtensionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class UIImageViewExtensionTests: XCTestCase {
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() {
Expand All @@ -108,7 +108,7 @@ class UIImageViewExtensionTests: XCTestCase {
XCTAssert(completionBlockIsCalled == false, "CompletionBlock should not be called since it is canceled.")
}

waitForExpectationsWithTimeout(0.1, handler: nil)
waitForExpectationsWithTimeout(5, handler: nil)
}

func testImageDownloadCacelPartialTask() {
Expand Down Expand Up @@ -149,7 +149,7 @@ class UIImageViewExtensionTests: XCTestCase {
XCTAssert(task3Completion == true, "Task 3 should be completed.")
}

waitForExpectationsWithTimeout(0.1, handler: nil)
waitForExpectationsWithTimeout(5, handler: nil)
}

func testImageDownalodMultipleCaches() {
Expand All @@ -174,6 +174,7 @@ class UIImageViewExtensionTests: XCTestCase {
self.imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: [.TargetCache: cache2], progressBlock: { (receivedSize, totalSize) -> () in

}, completionHandler: { (image, error, cacheType, imageURL) -> () in

XCTAssertTrue(cache1.isImageCachedForKey(URLString).cached, "This image should be cached in cache1.")
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.")
Expand All @@ -185,7 +186,7 @@ class UIImageViewExtensionTests: XCTestCase {

}

waitForExpectationsWithTimeout(0.1, handler: { (error) -> Void in
waitForExpectationsWithTimeout(5, handler: { (error) -> Void in
clearCaches([cache1, cache2])
})
}
Expand Down

0 comments on commit b17f972

Please sign in to comment.