ImageCacheTests.swift 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //
  2. // ImageCacheTests.swift
  3. // Kingfisher
  4. //
  5. // Created by Wei Wang on 15/4/10.
  6. //
  7. // Copyright (c) 2015 Wei Wang <onevcat@gmail.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. import UIKit
  27. import XCTest
  28. import Kingfisher
  29. private let cacheName = "com.onevcat.Kingfisher.ImageCache.test"
  30. class ImageCacheTests: XCTestCase {
  31. var cache: ImageCache!
  32. override func setUp() {
  33. super.setUp()
  34. // Put setup code here. This method is called before the invocation of each test method in the class.
  35. cache = ImageCache(name: "test")
  36. }
  37. override func tearDown() {
  38. // Put teardown code here. This method is called after the invocation of each test method in the class.
  39. super.tearDown()
  40. cache.clearDiskCache()
  41. cache = nil
  42. }
  43. func testMaxCachePeriodInSecond() {
  44. cache.maxCachePeriodInSecond = 1
  45. XCTAssert(cache.maxCachePeriodInSecond == 1, "maxCachePeriodInSecond should be able to be set.")
  46. }
  47. func testMaxMemorySize() {
  48. cache.maxMemoryCost = 1
  49. XCTAssert(cache.maxMemoryCost == 1, "maxMemoryCost should be able to be set.")
  50. }
  51. func testMaxDiskCacheSize() {
  52. cache.maxDiskCacheSize = 1
  53. XCTAssert(cache.maxDiskCacheSize == 1, "maxDiskCacheSize should be able to be set.")
  54. }
  55. func testClearDiskCache() {
  56. let paths = NSSearchPathForDirectoriesInDomains(.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true)
  57. let diskCachePath = paths.first!.stringByAppendingPathComponent(cacheName)
  58. let expectation = expectationWithDescription("wait for clearing disk cache")
  59. cache.storeImage(testImage, forKey: testKeys[0], toDisk: true) { () -> () in
  60. let files = NSFileManager.defaultManager().contentsOfDirectoryAtPath(diskCachePath, error:nil)
  61. XCTAssert(files?.count == 1, "Should be 1 file at the path")
  62. self.cache.clearDiskCacheWithCompletionHandler { () -> () in
  63. let files = NSFileManager.defaultManager().contentsOfDirectoryAtPath(diskCachePath, error:nil)
  64. XCTAssert(files?.count == 0, "Files should be at deleted")
  65. expectation.fulfill()
  66. }
  67. }
  68. waitForExpectationsWithTimeout(1, handler:nil)
  69. }
  70. func testClearMemoryCache() {
  71. let expectation = expectationWithDescription("wait for retriving image")
  72. cache.storeImage(testImage, forKey: testKeys[0], toDisk: true) { () -> () in
  73. self.cache.clearMemoryCache()
  74. self.cache.retrieveImageForKey(testKeys[0], options: KingfisherManager.OptionsNone, completionHandler: { (image, type) -> () in
  75. XCTAssert(image != nil && type == .Disk, "Should be cached in disk.")
  76. expectation.fulfill()
  77. })
  78. }
  79. waitForExpectationsWithTimeout(1, handler: nil)
  80. }
  81. func testNoImageFound() {
  82. let expectation = expectationWithDescription("wait for retriving image")
  83. cache.clearDiskCacheWithCompletionHandler { () -> () in
  84. self.cache.retrieveImageForKey(testKeys[0], options: KingfisherManager.OptionsNone, completionHandler: { (image, type) -> () in
  85. XCTAssert(image == nil, "Should not be cached in memory yet")
  86. expectation.fulfill()
  87. })
  88. return
  89. }
  90. waitForExpectationsWithTimeout(1, handler: nil)
  91. }
  92. func testStoreImageInMemory() {
  93. let expectation = expectationWithDescription("wait for retriving image")
  94. cache.storeImage(testImage, forKey: testKeys[0], toDisk: false) { () -> () in
  95. self.cache.retrieveImageForKey(testKeys[0], options: KingfisherManager.OptionsNone, completionHandler: { (image, type) -> () in
  96. XCTAssert(image != nil && type == .Memory, "Should be cached in memory.")
  97. expectation.fulfill()
  98. })
  99. return
  100. }
  101. waitForExpectationsWithTimeout(1, handler: nil)
  102. }
  103. func testStoreMultipleImages() {
  104. let expectation = expectationWithDescription("wait for writing image")
  105. storeMultipleImages { () -> () in
  106. let paths = NSSearchPathForDirectoriesInDomains(.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true)
  107. let diskCachePath = paths.first!.stringByAppendingPathComponent(cacheName)
  108. let files = NSFileManager.defaultManager().contentsOfDirectoryAtPath(diskCachePath, error:nil)
  109. XCTAssert(files?.count == 4, "All test images should be at locaitons. Expected 4, actually \(files?.count)")
  110. expectation.fulfill()
  111. }
  112. waitForExpectationsWithTimeout(1, handler: nil)
  113. }
  114. func testIsImageCachedForKey() {
  115. XCTAssert(cache.isImageCachedForKey(testKeys[0]).cached == false, "This image should not be cached yet.")
  116. let expectation = expectationWithDescription("wait for caching image")
  117. cache.storeImage(testImage, forKey: testKeys[0], toDisk: true) { () -> () in
  118. XCTAssert(self.cache.isImageCachedForKey(testKeys[0]).cached == true, "This image should be already cached.")
  119. expectation.fulfill()
  120. }
  121. waitForExpectationsWithTimeout(1, handler: nil)
  122. }
  123. func testRetrivingImagePerformance() {
  124. let expectation = self.expectationWithDescription("wait for retriving image")
  125. self.cache.storeImage(testImage, forKey: testKeys[0], toDisk: true) { () -> () in
  126. self.measureBlock({ () -> Void in
  127. for _ in 1 ..< 1000 {
  128. self.cache.retrieveImageInDiskCacheForKey(testKeys[0])
  129. }
  130. })
  131. expectation.fulfill()
  132. }
  133. self.waitForExpectationsWithTimeout(5, handler: nil)
  134. }
  135. // MARK: - Helper
  136. func storeMultipleImages(completionHandler:()->()) {
  137. let group = dispatch_group_create()
  138. dispatch_group_enter(group)
  139. cache.storeImage(testImage, forKey: testKeys[0], toDisk: true) { () -> () in
  140. dispatch_group_leave(group)
  141. }
  142. dispatch_group_enter(group)
  143. cache.storeImage(testImage, forKey: testKeys[1], toDisk: true) { () -> () in
  144. dispatch_group_leave(group)
  145. }
  146. dispatch_group_enter(group)
  147. cache.storeImage(testImage, forKey: testKeys[2], toDisk: true) { () -> () in
  148. dispatch_group_leave(group)
  149. }
  150. dispatch_group_enter(group)
  151. cache.storeImage(testImage, forKey: testKeys[3], toDisk: true) { () -> () in
  152. dispatch_group_leave(group)
  153. }
  154. dispatch_group_notify(group, dispatch_get_main_queue()) {
  155. completionHandler()
  156. }
  157. }
  158. }