ImageCacheTests.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. //
  2. // ImageCacheTests.swift
  3. // Kingfisher
  4. //
  5. // Created by Wei Wang on 15/4/10.
  6. //
  7. // Copyright (c) 2019 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 XCTest
  27. @testable import Kingfisher
  28. class ImageCacheTests: XCTestCase {
  29. var cache: ImageCache!
  30. var observer: NSObjectProtocol!
  31. override func setUp() {
  32. super.setUp()
  33. let uuid = UUID().uuidString
  34. let cacheName = "test-\(uuid)"
  35. cache = ImageCache(name: cacheName)
  36. }
  37. override func tearDown() {
  38. clearCaches([cache])
  39. cache = nil
  40. observer = nil
  41. super.tearDown()
  42. }
  43. func testInvalidCustomCachePath() {
  44. let customPath = "/path/to/image/cache"
  45. let url = URL(fileURLWithPath: customPath)
  46. XCTAssertThrowsError(try ImageCache(name: "test", cacheDirectoryURL: url)) { error in
  47. guard case KingfisherError.cacheError(reason: .cannotCreateDirectory(let path, _)) = error else {
  48. XCTFail("Should be KingfisherError with cacheError reason.")
  49. return
  50. }
  51. XCTAssertEqual(path, customPath + "/com.onevcat.Kingfisher.ImageCache.test")
  52. }
  53. }
  54. func testCustomCachePath() {
  55. let cacheURL = try! FileManager.default.url(
  56. for: .cachesDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
  57. let subFolder = cacheURL.appendingPathComponent("temp")
  58. let customPath = subFolder.path
  59. let cache = try! ImageCache(name: "test", cacheDirectoryURL: subFolder)
  60. XCTAssertEqual(
  61. cache.diskStorage.directoryURL.path,
  62. (customPath as NSString).appendingPathComponent("com.onevcat.Kingfisher.ImageCache.test"))
  63. clearCaches([cache])
  64. }
  65. func testCustomCachePathByBlock() {
  66. let cache = try! ImageCache(name: "test", cacheDirectoryURL: nil, diskCachePathClosure: { (url, path) -> URL in
  67. let modifiedPath = path + "-modified"
  68. return url.appendingPathComponent(modifiedPath, isDirectory: true)
  69. })
  70. let cacheURL = try! FileManager.default.url(
  71. for: .cachesDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
  72. XCTAssertEqual(
  73. cache.diskStorage.directoryURL.path,
  74. (cacheURL.path as NSString).appendingPathComponent("com.onevcat.Kingfisher.ImageCache.test-modified"))
  75. clearCaches([cache])
  76. }
  77. func testMaxCachePeriodInSecond() {
  78. cache.diskStorage.config.expiration = .seconds(1)
  79. XCTAssertEqual(cache.diskStorage.config.expiration.timeInterval, 1)
  80. }
  81. func testMaxMemorySize() {
  82. cache.memoryStorage.config.totalCostLimit = 1
  83. XCTAssert(cache.memoryStorage.config.totalCostLimit == 1, "maxMemoryCost should be able to be set.")
  84. }
  85. func testMaxDiskCacheSize() {
  86. cache.diskStorage.config.sizeLimit = 1
  87. XCTAssert(cache.diskStorage.config.sizeLimit == 1, "maxDiskCacheSize should be able to be set.")
  88. }
  89. func testClearDiskCache() {
  90. let exp = expectation(description: #function)
  91. let key = testKeys[0]
  92. cache.store(testImage, original: testImageData, forKey: key, toDisk: true) { _ in
  93. self.cache.clearMemoryCache()
  94. let cacheResult = self.cache.imageCachedType(forKey: key)
  95. XCTAssertTrue(cacheResult.cached)
  96. XCTAssertEqual(cacheResult, .disk)
  97. self.cache.clearDiskCache {
  98. let cacheResult = self.cache.imageCachedType(forKey: key)
  99. XCTAssertFalse(cacheResult.cached)
  100. exp.fulfill()
  101. }
  102. }
  103. waitForExpectations(timeout: 3, handler:nil)
  104. }
  105. func testClearMemoryCache() {
  106. let exp = expectation(description: #function)
  107. let key = testKeys[0]
  108. cache.store(testImage, original: testImageData, forKey: key, toDisk: true) { _ in
  109. self.cache.clearMemoryCache()
  110. self.cache.retrieveImage(forKey: key) { result in
  111. XCTAssertNotNil(result.value?.image)
  112. XCTAssertEqual(result.value?.cacheType, .disk)
  113. exp.fulfill()
  114. }
  115. }
  116. waitForExpectations(timeout: 3, handler: nil)
  117. }
  118. func testNoImageFound() {
  119. let exp = expectation(description: #function)
  120. cache.retrieveImage(forKey: testKeys[0]) { result in
  121. XCTAssertNotNil(result.value)
  122. XCTAssertNil(result.value!.image)
  123. exp.fulfill()
  124. }
  125. waitForExpectations(timeout: 3, handler: nil)
  126. }
  127. func testCachedFileDoesNotExist() {
  128. let URLString = testKeys[0]
  129. let url = URL(string: URLString)!
  130. let exists = cache.imageCachedType(forKey: url.cacheKey).cached
  131. XCTAssertFalse(exists)
  132. }
  133. func testStoreImageInMemory() {
  134. let exp = expectation(description: #function)
  135. let key = testKeys[0]
  136. cache.store(testImage, forKey: key, toDisk: false) { _ in
  137. self.cache.retrieveImage(forKey: key) { result in
  138. XCTAssertNotNil(result.value?.image)
  139. XCTAssertEqual(result.value?.cacheType, .memory)
  140. exp.fulfill()
  141. }
  142. }
  143. waitForExpectations(timeout: 3, handler: nil)
  144. }
  145. func testStoreMultipleImages() {
  146. let exp = expectation(description: #function)
  147. storeMultipleImages {
  148. let diskCachePath = self.cache.diskStorage.directoryURL.path
  149. var files: [String] = []
  150. do {
  151. files = try FileManager.default.contentsOfDirectory(atPath: diskCachePath)
  152. } catch _ {
  153. XCTFail()
  154. }
  155. XCTAssertEqual(files.count, testKeys.count)
  156. exp.fulfill()
  157. }
  158. waitForExpectations(timeout: 3, handler: nil)
  159. }
  160. func testCachedFileExists() {
  161. let exp = expectation(description: #function)
  162. let key = testKeys[0]
  163. let url = URL(string: key)!
  164. let exists = cache.imageCachedType(forKey: url.cacheKey).cached
  165. XCTAssertFalse(exists)
  166. cache.retrieveImage(forKey: key) { result in
  167. switch result {
  168. case .success(let value):
  169. XCTAssertNil(value.image)
  170. XCTAssertEqual(value.cacheType, .none)
  171. case .failure:
  172. XCTFail()
  173. return
  174. }
  175. self.cache.store(testImage, forKey: key, toDisk: true) { _ in
  176. self.cache.retrieveImage(forKey: key) { result in
  177. XCTAssertNotNil(result.value?.image)
  178. XCTAssertEqual(result.value?.cacheType, .memory)
  179. self.cache.clearMemoryCache()
  180. self.cache.retrieveImage(forKey: key) { result in
  181. XCTAssertNotNil(result.value?.image)
  182. XCTAssertEqual(result.value?.cacheType, .disk)
  183. exp.fulfill()
  184. }
  185. }
  186. }
  187. }
  188. waitForExpectations(timeout: 3, handler: nil)
  189. }
  190. func testCachedFileWithCustomPathExtensionExists() {
  191. cache.diskStorage.config.pathExtension = "jpg"
  192. let exp = expectation(description: #function)
  193. let key = testKeys[0]
  194. let url = URL(string: key)!
  195. cache.store(testImage, forKey: key, toDisk: true) { _ in
  196. let cachePath = self.cache.cachePath(forKey: url.cacheKey)
  197. XCTAssertTrue(cachePath.hasSuffix(".jpg"))
  198. exp.fulfill()
  199. }
  200. waitForExpectations(timeout: 3, handler: nil)
  201. }
  202. func testCachedImageIsFetchedSyncronouslyFromTheMemoryCache() {
  203. cache.store(testImage, forKey: testKeys[0], toDisk: false)
  204. var foundImage: KFCrossPlatformImage?
  205. cache.retrieveImage(forKey: testKeys[0]) { result in
  206. foundImage = result.value?.image
  207. }
  208. XCTAssertEqual(testImage, foundImage)
  209. }
  210. func testIsImageCachedForKey() {
  211. let exp = expectation(description: #function)
  212. let key = testKeys[0]
  213. XCTAssertFalse(cache.imageCachedType(forKey: key).cached)
  214. cache.store(testImage, original: testImageData, forKey: key, toDisk: true) { _ in
  215. XCTAssertTrue(self.cache.imageCachedType(forKey: key).cached)
  216. exp.fulfill()
  217. }
  218. waitForExpectations(timeout: 3, handler: nil)
  219. }
  220. func testCleanDiskCacheNotification() {
  221. let exp = expectation(description: #function)
  222. let key = testKeys[0]
  223. cache.diskStorage.config.expiration = .seconds(0.01)
  224. cache.store(testImage, original: testImageData, forKey: key, toDisk: true) { _ in
  225. self.observer = NotificationCenter.default.addObserver(
  226. forName: .KingfisherDidCleanDiskCache,
  227. object: self.cache,
  228. queue: .main) {
  229. noti in
  230. let receivedCache = noti.object as? ImageCache
  231. XCTAssertNotNil(receivedCache)
  232. XCTAssertTrue(receivedCache === self.cache)
  233. guard let hashes = noti.userInfo?[KingfisherDiskCacheCleanedHashKey] as? [String] else {
  234. XCTFail("Notification should contains Strings in key 'KingfisherDiskCacheCleanedHashKey'")
  235. exp.fulfill()
  236. return
  237. }
  238. XCTAssertEqual(hashes.count, 1)
  239. XCTAssertEqual(hashes.first!, self.cache.hash(forKey: key))
  240. guard let o = self.observer else { return }
  241. NotificationCenter.default.removeObserver(o)
  242. exp.fulfill()
  243. }
  244. delay(1) {
  245. self.cache.cleanExpiredDiskCache()
  246. }
  247. }
  248. waitForExpectations(timeout: 5, handler: nil)
  249. }
  250. func testCannotRetrieveCacheWithProcessorIdentifier() {
  251. let exp = expectation(description: #function)
  252. let key = testKeys[0]
  253. let p = RoundCornerImageProcessor(cornerRadius: 40)
  254. cache.store(testImage, original: testImageData, forKey: key, toDisk: true) { _ in
  255. self.cache.retrieveImage(forKey: key, options: [.processor(p)]) { result in
  256. XCTAssertNotNil(result.value)
  257. XCTAssertNil(result.value!.image)
  258. exp.fulfill()
  259. }
  260. }
  261. waitForExpectations(timeout: 3, handler: nil)
  262. }
  263. func testRetrieveCacheWithProcessorIdentifier() {
  264. let exp = expectation(description: #function)
  265. let key = testKeys[0]
  266. let p = RoundCornerImageProcessor(cornerRadius: 40)
  267. cache.store(
  268. testImage,
  269. original: testImageData,
  270. forKey: key,
  271. processorIdentifier: p.identifier,
  272. toDisk: true)
  273. {
  274. _ in
  275. self.cache.retrieveImage(forKey: key, options: [.processor(p)]) { result in
  276. XCTAssertNotNil(result.value?.image)
  277. exp.fulfill()
  278. }
  279. }
  280. waitForExpectations(timeout: 3, handler: nil)
  281. }
  282. func testDefaultCache() {
  283. let exp = expectation(description: #function)
  284. let key = testKeys[0]
  285. let cache = ImageCache.default
  286. cache.store(testImage, forKey: key) { _ in
  287. XCTAssertTrue(cache.memoryStorage.isCached(forKey: key))
  288. XCTAssertTrue(cache.diskStorage.isCached(forKey: key))
  289. cleanDefaultCache()
  290. exp.fulfill()
  291. }
  292. waitForExpectations(timeout: 3, handler: nil)
  293. }
  294. func testRetrieveDiskCacheSynchronously() {
  295. let exp = expectation(description: #function)
  296. let key = testKeys[0]
  297. cache.store(testImage, forKey: key, toDisk: true) { _ in
  298. var cacheType = self.cache.imageCachedType(forKey: key)
  299. XCTAssertEqual(cacheType, .memory)
  300. try! self.cache.memoryStorage.remove(forKey: key)
  301. cacheType = self.cache.imageCachedType(forKey: key)
  302. XCTAssertEqual(cacheType, .disk)
  303. var dispatched = false
  304. self.cache.retrieveImageInDiskCache(forKey: key, options: [.loadDiskFileSynchronously]) {
  305. result in
  306. XCTAssertFalse(dispatched)
  307. exp.fulfill()
  308. }
  309. // This should be called after the completion handler above.
  310. dispatched = true
  311. }
  312. waitForExpectations(timeout: 3, handler: nil)
  313. }
  314. func testRetrieveDiskCacheAsynchronously() {
  315. let exp = expectation(description: #function)
  316. let key = testKeys[0]
  317. cache.store(testImage, forKey: key, toDisk: true) { _ in
  318. var cacheType = self.cache.imageCachedType(forKey: key)
  319. XCTAssertEqual(cacheType, .memory)
  320. try! self.cache.memoryStorage.remove(forKey: key)
  321. cacheType = self.cache.imageCachedType(forKey: key)
  322. XCTAssertEqual(cacheType, .disk)
  323. var dispatched = false
  324. self.cache.retrieveImageInDiskCache(forKey: key, options: nil) {
  325. result in
  326. XCTAssertTrue(dispatched)
  327. exp.fulfill()
  328. }
  329. // This should be called before the completion handler above.
  330. dispatched = true
  331. }
  332. waitForExpectations(timeout: 3, handler: nil)
  333. }
  334. #if os(iOS) || os(tvOS) || os(watchOS)
  335. func testGettingMemoryCachedImageCouldBeModified() {
  336. let exp = expectation(description: #function)
  337. let key = testKeys[0]
  338. var modifierCalled = false
  339. let modifier = AnyImageModifier { image in
  340. modifierCalled = true
  341. return image.withRenderingMode(.alwaysTemplate)
  342. }
  343. cache.store(testImage, original: testImageData, forKey: key) { _ in
  344. self.cache.retrieveImage(forKey: key, options: [.imageModifier(modifier)]) { result in
  345. XCTAssertTrue(modifierCalled)
  346. XCTAssertEqual(result.value?.image?.renderingMode, .alwaysTemplate)
  347. exp.fulfill()
  348. }
  349. }
  350. waitForExpectations(timeout: 3, handler: nil)
  351. }
  352. func testGettingDiskCachedImageCouldBeModified() {
  353. let exp = expectation(description: #function)
  354. let key = testKeys[0]
  355. var modifierCalled = false
  356. let modifier = AnyImageModifier { image in
  357. modifierCalled = true
  358. return image.withRenderingMode(.alwaysTemplate)
  359. }
  360. cache.store(testImage, original: testImageData, forKey: key) { _ in
  361. self.cache.clearMemoryCache()
  362. self.cache.retrieveImage(forKey: key, options: [.imageModifier(modifier)]) { result in
  363. XCTAssertTrue(modifierCalled)
  364. XCTAssertEqual(result.value?.image?.renderingMode, .alwaysTemplate)
  365. exp.fulfill()
  366. }
  367. }
  368. waitForExpectations(timeout: 3, handler: nil)
  369. }
  370. #endif
  371. func testStoreToMemoryWithExpiration() {
  372. let exp = expectation(description: #function)
  373. let key = testKeys[0]
  374. cache.store(
  375. testImage,
  376. original: testImageData,
  377. forKey: key,
  378. options: KingfisherParsedOptionsInfo([.memoryCacheExpiration(.seconds(0.2))]),
  379. toDisk: true)
  380. {
  381. _ in
  382. XCTAssertEqual(self.cache.imageCachedType(forKey: key), .memory)
  383. delay(1) {
  384. XCTAssertEqual(self.cache.imageCachedType(forKey: key), .disk)
  385. exp.fulfill()
  386. }
  387. }
  388. waitForExpectations(timeout: 5, handler: nil)
  389. }
  390. func testStoreToDiskWithExpiration() {
  391. let exp = expectation(description: #function)
  392. let key = testKeys[0]
  393. cache.store(
  394. testImage,
  395. original: testImageData,
  396. forKey: key,
  397. options: KingfisherParsedOptionsInfo([.diskCacheExpiration(.expired)]),
  398. toDisk: true)
  399. {
  400. _ in
  401. XCTAssertEqual(self.cache.imageCachedType(forKey: key), .memory)
  402. self.cache.clearMemoryCache()
  403. XCTAssertEqual(self.cache.imageCachedType(forKey: key), .none)
  404. exp.fulfill()
  405. }
  406. waitForExpectations(timeout: 3, handler: nil)
  407. }
  408. // MARK: - Helper
  409. func storeMultipleImages(_ completionHandler: @escaping () -> Void) {
  410. let group = DispatchGroup()
  411. testKeys.forEach {
  412. group.enter()
  413. cache.store(testImage, original: testImageData, forKey: $0, toDisk: true) { _ in
  414. group.leave()
  415. }
  416. }
  417. group.notify(queue: .main, execute: completionHandler)
  418. }
  419. }