| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- //
- // ImageCacheTests.swift
- // Kingfisher
- //
- // Created by Wei Wang on 15/4/10.
- //
- // Copyright (c) 2015 Wei Wang <onevcat@gmail.com>
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- import UIKit
- import XCTest
- @testable import Kingfisher
- private let cacheName = "com.onevcat.Kingfisher.ImageCache.test"
- class ImageCacheTests: XCTestCase {
- var cache: ImageCache!
- var observer: NSObjectProtocol!
-
- override func setUp() {
- super.setUp()
- // Put setup code here. This method is called before the invocation of each test method in the class.
- cache = ImageCache(name: "test")
- clearCaches([cache])
- }
-
- override func tearDown() {
- // Put teardown code here. This method is called after the invocation of each test method in the class.
- super.tearDown()
-
- clearCaches([cache])
- cache = nil
- observer = nil
- }
-
- func testCustomCachePath() {
- let customPath = "/path/to/image/cache"
- let cache = ImageCache(name: "test", path: customPath)
- XCTAssertEqual(cache.diskCachePath, customPath + "/com.onevcat.Kingfisher.ImageCache.test", "Custom disk cache path set correctly")
- }
-
- func testMaxCachePeriodInSecond() {
- cache.maxCachePeriodInSecond = 1
- XCTAssert(cache.maxCachePeriodInSecond == 1, "maxCachePeriodInSecond should be able to be set.")
- }
-
- func testMaxMemorySize() {
- cache.maxMemoryCost = 1
- XCTAssert(cache.maxMemoryCost == 1, "maxMemoryCost should be able to be set.")
- }
-
- func testMaxDiskCacheSize() {
- cache.maxDiskCacheSize = 1
- XCTAssert(cache.maxDiskCacheSize == 1, "maxDiskCacheSize should be able to be set.")
- }
-
- func testClearDiskCache() {
-
- let expectation = expectationWithDescription("wait for clearing disk cache")
- let key = testKeys[0]
-
- cache.storeImage(testImage, originalData: testImageData, 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")
-
- self.cache.clearDiskCacheWithCompletionHandler { () -> () in
- let cacheResult = self.cache.isImageCachedForKey(key)
- XCTAssertFalse(cacheResult.cached, "Should be not cached")
- expectation.fulfill()
- }
- }
- waitForExpectationsWithTimeout(10, handler:nil)
- }
-
- func testClearMemoryCache() {
- let expectation = expectationWithDescription("wait for retrieving image")
-
- cache.storeImage(testImage, originalData: testImageData, forKey: testKeys[0], toDisk: true) { () -> () in
- self.cache.clearMemoryCache()
- self.cache.retrieveImageForKey(testKeys[0], options: KingfisherManager.OptionsNone, completionHandler: { (image, type) -> () in
- XCTAssert(image != nil && type == .Disk, "Should be cached in disk. But \(type)")
- expectation.fulfill()
- })
- }
-
- waitForExpectationsWithTimeout(5, handler: nil)
- }
-
- func testNoImageFound() {
- let expectation = expectationWithDescription("wait for retrieving image")
-
- cache.clearDiskCacheWithCompletionHandler { () -> () in
- self.cache.retrieveImageForKey(testKeys[0], options: KingfisherManager.OptionsNone, completionHandler: { (image, type) -> () in
- XCTAssert(image == nil, "Should not be cached in memory yet")
- expectation.fulfill()
- })
- return
- }
-
- waitForExpectationsWithTimeout(5, handler: nil)
- }
-
- func testStoreImageInMemory() {
- let expectation = expectationWithDescription("wait for retrieving image")
-
- cache.storeImage(testImage, forKey: testKeys[0], toDisk: false) { () -> () in
- self.cache.retrieveImageForKey(testKeys[0], options: KingfisherManager.OptionsNone, completionHandler: { (image, type) -> () in
- XCTAssert(image != nil && type == .Memory, "Should be cached in memory.")
- expectation.fulfill()
- })
- return
- }
-
- waitForExpectationsWithTimeout(5, handler: nil)
- }
-
- func testStoreMultipleImages() {
- let expectation = expectationWithDescription("wait for writing image")
-
- storeMultipleImages { () -> () in
- let paths = NSSearchPathForDirectoriesInDomains(.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true)
- let diskCachePath = (paths.first! as NSString).stringByAppendingPathComponent(cacheName)
-
- let files: [AnyObject]?
- do {
- files = try NSFileManager.defaultManager().contentsOfDirectoryAtPath(diskCachePath)
- } catch _ {
- files = nil
- }
- XCTAssert(files?.count == 4, "All test images should be at locaitons. Expected 4, actually \(files?.count)")
-
- expectation.fulfill()
- }
-
- waitForExpectationsWithTimeout(5, handler: nil)
- }
-
- func testIsImageCachedForKey() {
- let expectation = self.expectationWithDescription("wait for caching image")
-
- XCTAssert(self.cache.isImageCachedForKey(testKeys[0]).cached == false, "This image should not be cached yet.")
- self.cache.storeImage(testImage, originalData: testImageData, 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 testRetrievingImagePerformance() {
- let expectation = self.expectationWithDescription("wait for retrieving image")
- self.cache.storeImage(testImage, originalData: testImageData, forKey: testKeys[0], toDisk: true) { () -> () in
- self.measureBlock({ () -> Void in
- for _ in 1 ..< 1000 {
- self.cache.retrieveImageInDiskCacheForKey(testKeys[0])
- }
- })
- expectation.fulfill()
- }
-
- self.waitForExpectationsWithTimeout(20, handler: nil)
- }
-
- func testCleanDiskCacheNotification() {
- let expectation = expectationWithDescription("wait for retrieving image")
-
- cache.storeImage(testImage, originalData: testImageData, forKey: testKeys[0], toDisk: true) { () -> () in
- self.observer = NSNotificationCenter.defaultCenter().addObserverForName(KingfisherDidCleanDiskCacheNotification, object: self.cache, queue: NSOperationQueue.mainQueue(), usingBlock: { (noti) -> Void in
- XCTAssert(noti.object === self.cache, "The object of notification should be the cache object.")
-
- guard let hashes = noti.userInfo?[KingfisherDiskCacheCleanedHashKey] as? [String] else {
- XCTFail("The clean disk cache notification should contains Strings in key 'KingfisherDiskCacheCleanedHashKey'")
- expectation.fulfill()
- return
- }
-
- XCTAssertEqual(1, hashes.count, "There should be one and only one file cleaned")
- XCTAssertEqual(hashes.first!, self.cache.hashForKey(testKeys[0]), "The cleaned file should be the stored one.")
-
- NSNotificationCenter.defaultCenter().removeObserver(self.observer)
- expectation.fulfill()
- })
-
- self.cache.maxCachePeriodInSecond = 0
- self.cache.cleanExpiredDiskCache()
- }
-
- waitForExpectationsWithTimeout(5, handler: nil)
- }
- // MARK: - Helper
- func storeMultipleImages(completionHandler:()->()) {
-
- let group = dispatch_group_create()
-
- dispatch_group_enter(group)
- cache.storeImage(testImage, originalData: testImageData, forKey: testKeys[0], toDisk: true) { () -> () in
- dispatch_group_leave(group)
- }
- dispatch_group_enter(group)
- cache.storeImage(testImage, originalData: testImageData, forKey: testKeys[1], toDisk: true) { () -> () in
- dispatch_group_leave(group)
- }
- dispatch_group_enter(group)
- cache.storeImage(testImage, originalData: testImageData, forKey: testKeys[2], toDisk: true) { () -> () in
- dispatch_group_leave(group)
- }
- dispatch_group_enter(group)
- cache.storeImage(testImage, originalData: testImageData, forKey: testKeys[3], toDisk: true) { () -> () in
- dispatch_group_leave(group)
- }
-
- dispatch_group_notify(group, dispatch_get_main_queue()) {
- completionHandler()
- }
- }
- }
|