ImageViewExtensionTests.swift 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. //
  2. // UIImageViewExtensionTests.swift
  3. // Kingfisher
  4. //
  5. // Created by Wei Wang on 15/4/17.
  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 ImageViewExtensionTests: XCTestCase {
  29. var imageView: ImageView!
  30. override class func setUp() {
  31. super.setUp()
  32. LSNocilla.sharedInstance().start()
  33. }
  34. override class func tearDown() {
  35. LSNocilla.sharedInstance().stop()
  36. super.tearDown()
  37. }
  38. override func setUp() {
  39. super.setUp()
  40. imageView = ImageView()
  41. KingfisherManager.shared.downloader = ImageDownloader(name: "testDownloader")
  42. KingfisherManager.shared.defaultOptions = [.waitForCache]
  43. cleanDefaultCache()
  44. }
  45. override func tearDown() {
  46. LSNocilla.sharedInstance().clearStubs()
  47. imageView = nil
  48. cleanDefaultCache()
  49. KingfisherManager.shared.defaultOptions = .empty
  50. super.tearDown()
  51. }
  52. func testImageDownloadForImageView() {
  53. let exp = expectation(description: #function)
  54. let url = testURLs[0]
  55. stub(url, data: testImageData, length: 123)
  56. var progressBlockIsCalled = false
  57. imageView.kf.setImage(
  58. with: url,
  59. progressBlock: { _, _ in
  60. progressBlockIsCalled = true
  61. XCTAssertTrue(Thread.isMainThread)
  62. })
  63. {
  64. result in
  65. XCTAssertTrue(progressBlockIsCalled)
  66. XCTAssertNotNil(result.value)
  67. let value = result.value!
  68. XCTAssertTrue(value.image.renderEqual(to: testImage))
  69. XCTAssertTrue(self.imageView.image!.renderEqual(to: testImage))
  70. //XCTAssertEqual(self.imageView.kf.taskIdentifier, Source.Identifier.current)
  71. XCTAssertEqual(value.cacheType, .none)
  72. XCTAssertTrue(Thread.isMainThread)
  73. exp.fulfill()
  74. }
  75. waitForExpectations(timeout: 3, handler: nil)
  76. }
  77. func testImageDownloadCompletionHandlerRunningOnMainQueue() {
  78. let exp = expectation(description: #function)
  79. let url = testURLs[0]
  80. stub(url, data: testImageData)
  81. let customQueue = DispatchQueue(label: "com.kingfisher.testQueue")
  82. imageView.kf.setImage(
  83. with: url,
  84. options: [.callbackQueue(.dispatch(customQueue))],
  85. progressBlock: { _, _ in XCTAssertTrue(Thread.isMainThread) })
  86. {
  87. result in
  88. XCTAssertTrue(Thread.isMainThread)
  89. exp.fulfill()
  90. }
  91. waitForExpectations(timeout: 3, handler: nil)
  92. }
  93. func testImageDownloadWithResourceForImageView() {
  94. let exp = expectation(description: #function)
  95. let url = testURLs[0]
  96. stub(url, data: testImageData, length: 123)
  97. var progressBlockIsCalled = false
  98. let resource = ImageResource(downloadURL: url)
  99. imageView.kf.setImage(
  100. with: resource,
  101. progressBlock: { _, _ in progressBlockIsCalled = true })
  102. {
  103. result in
  104. XCTAssertTrue(progressBlockIsCalled)
  105. XCTAssertNotNil(result.value)
  106. let value = result.value!
  107. XCTAssertTrue(value.image.renderEqual(to: testImage))
  108. XCTAssertTrue(self.imageView.image!.renderEqual(to: testImage))
  109. //XCTAssertEqual(self.imageView.kf.taskIdentifier, Source.Identifier.current)
  110. XCTAssertEqual(value.cacheType, .none)
  111. XCTAssertTrue(Thread.isMainThread)
  112. exp.fulfill()
  113. }
  114. waitForExpectations(timeout: 3, handler: nil)
  115. }
  116. func testImageDownloadCancelForImageView() {
  117. let exp = expectation(description: #function)
  118. let url = testURLs[0]
  119. let stub = delayedStub(url, data: testImageData, length: 123)
  120. let task = imageView.kf.setImage(
  121. with: url,
  122. progressBlock: { _, _ in XCTFail() })
  123. {
  124. result in
  125. XCTAssertNotNil(result.error)
  126. delay(0.1) { exp.fulfill() }
  127. }
  128. XCTAssertNotNil(task)
  129. task?.cancel()
  130. _ = stub.go()
  131. waitForExpectations(timeout: 3, handler: nil)
  132. }
  133. func testImageDownloadCancelPartialTaskBeforeRequest() {
  134. let exp = expectation(description: #function)
  135. let url = testURLs[0]
  136. let stub = delayedStub(url, data: testImageData)
  137. let group = DispatchGroup()
  138. group.enter()
  139. let task1 = imageView.kf.setImage(with: url) {
  140. result in
  141. XCTAssertNil(result.value)
  142. group.leave()
  143. }
  144. group.enter()
  145. imageView.kf.setImage(with: url) { result in
  146. XCTAssertNotNil(result.value)
  147. group.leave()
  148. }
  149. group.enter()
  150. let anotherImageView = ImageView()
  151. anotherImageView.kf.setImage(with: url) { result in
  152. XCTAssertNotNil(result.value)
  153. group.leave()
  154. }
  155. task1?.cancel()
  156. _ = stub.go()
  157. group.notify(queue: .main) {
  158. delay(0.1) { exp.fulfill() }
  159. }
  160. waitForExpectations(timeout: 3, handler: nil)
  161. }
  162. func testImageDownloadCancelAllTasksAfterRequestStarted() {
  163. let exp = expectation(description: #function)
  164. let url = testURLs[0]
  165. let stub = delayedStub(url, data: testImageData)
  166. let group = DispatchGroup()
  167. group.enter()
  168. let task1 = imageView.kf.setImage(with: url) { result in
  169. XCTAssertNotNil(result.error)
  170. group.leave()
  171. }
  172. group.enter()
  173. let task2 = imageView.kf.setImage(with: url) { result in
  174. XCTAssertNotNil(result.error)
  175. group.leave()
  176. }
  177. group.enter()
  178. let task3 = imageView.kf.setImage(with: url) { result in
  179. XCTAssertNotNil(result.error)
  180. group.leave()
  181. }
  182. task1?.cancel()
  183. task2?.cancel()
  184. task3?.cancel()
  185. _ = stub.go()
  186. group.notify(queue: .main) {
  187. delay(0.1) { exp.fulfill() }
  188. }
  189. waitForExpectations(timeout: 3, handler: nil)
  190. }
  191. func testImageDownloadMultipleCaches() {
  192. let cache1 = ImageCache(name: "cache1")
  193. let cache2 = ImageCache(name: "cache2")
  194. cache1.clearDiskCache()
  195. cache2.clearDiskCache()
  196. let exp = expectation(description: #function)
  197. let url = testURLs[0]
  198. stub(url, data: testImageData)
  199. let key = url.cacheKey
  200. imageView.kf.setImage(with: url, options: [.targetCache(cache1)]) { result in
  201. XCTAssertTrue(cache1.imageCachedType(forKey: key).cached)
  202. XCTAssertFalse(cache2.imageCachedType(forKey: key).cached)
  203. XCTAssertFalse(KingfisherManager.shared.cache.imageCachedType(forKey: key).cached)
  204. self.imageView.kf.setImage(with: url, options: [.targetCache(cache2), .waitForCache]) { result in
  205. XCTAssertTrue(cache1.imageCachedType(forKey: key).cached)
  206. XCTAssertTrue(cache2.imageCachedType(forKey: key).cached)
  207. XCTAssertFalse(KingfisherManager.shared.cache.imageCachedType(forKey: key).cached)
  208. exp.fulfill()
  209. }
  210. }
  211. waitForExpectations(timeout: 5) { error in
  212. clearCaches([cache1, cache2])
  213. }
  214. }
  215. func testIndicatorViewExisting() {
  216. imageView.kf.indicatorType = .activity
  217. XCTAssertNotNil(imageView.kf.indicator)
  218. XCTAssertTrue(imageView.kf.indicator is ActivityIndicator)
  219. imageView.kf.indicatorType = .none
  220. XCTAssertNil(imageView.kf.indicator)
  221. }
  222. func testCustomizeStructIndicatorExisting() {
  223. struct StructIndicator: Indicator {
  224. let view = View()
  225. func startAnimatingView() {}
  226. func stopAnimatingView() {}
  227. }
  228. imageView.kf.indicatorType = .custom(indicator: StructIndicator())
  229. XCTAssertNotNil(imageView.kf.indicator)
  230. XCTAssertTrue(imageView.kf.indicator is StructIndicator)
  231. imageView.kf.indicatorType = .none
  232. XCTAssertNil(imageView.kf.indicator)
  233. }
  234. func testActivityIndicatorViewAnimating() {
  235. imageView.kf.indicatorType = .activity
  236. let exp = expectation(description: #function)
  237. let url = testURLs[0]
  238. stub(url, data: testImageData)
  239. imageView.kf.setImage(with: url, progressBlock: { receivedSize, totalSize in
  240. let indicator = self.imageView.kf.indicator
  241. XCTAssertNotNil(indicator)
  242. XCTAssertFalse(indicator!.view.isHidden)
  243. })
  244. {
  245. result in
  246. let indicator = self.imageView.kf.indicator
  247. XCTAssertTrue(indicator!.view.isHidden)
  248. exp.fulfill()
  249. }
  250. waitForExpectations(timeout: 3, handler: nil)
  251. }
  252. func testCanUseImageIndicatorViewAnimating() {
  253. imageView.kf.indicatorType = .image(imageData: testImageData)
  254. XCTAssertTrue(imageView.kf.indicator is ImageIndicator)
  255. let image = (imageView.kf.indicator?.view as? ImageView)?.image
  256. XCTAssertNotNil(image)
  257. XCTAssertTrue(image!.renderEqual(to: testImage))
  258. let exp = expectation(description: #function)
  259. let url = testURLs[0]
  260. stub(url, data: testImageData)
  261. imageView.kf.setImage(with: url, progressBlock: { receivedSize, totalSize in
  262. let indicator = self.imageView.kf.indicator
  263. XCTAssertNotNil(indicator)
  264. XCTAssertFalse(indicator!.view.isHidden)
  265. })
  266. {
  267. result in
  268. let indicator = self.imageView.kf.indicator
  269. XCTAssertTrue(indicator!.view.isHidden)
  270. exp.fulfill()
  271. }
  272. waitForExpectations(timeout: 3, handler: nil)
  273. }
  274. func testCancelImageTask() {
  275. let exp = expectation(description: #function)
  276. let url = testURLs[0]
  277. let stub = delayedStub(url, data: testImageData)
  278. imageView.kf.setImage(with: url, progressBlock: { _, _ in XCTFail() }) { result in
  279. XCTAssertNotNil(result.error)
  280. XCTAssertTrue(result.error!.isTaskCancelled)
  281. delay(0.1) { exp.fulfill() }
  282. }
  283. self.imageView.kf.cancelDownloadTask()
  284. _ = stub.go()
  285. waitForExpectations(timeout: 3, handler: nil)
  286. }
  287. func testDownloadForMutipleURLs() {
  288. let exp = expectation(description: #function)
  289. stub(testURLs[0], data: testImageData)
  290. stub(testURLs[1], data: testImageData)
  291. let group = DispatchGroup()
  292. group.enter()
  293. imageView.kf.setImage(with: testURLs[0]) { result in
  294. // The download successed, but not the resource we want.
  295. XCTAssertNotNil(result.error)
  296. if case .imageSettingError(
  297. reason: .notCurrentSourceTask(let result, _, let source)) = result.error!
  298. {
  299. XCTAssertEqual(source.url, testURLs[0])
  300. XCTAssertNotEqual(result!.image, self.imageView.image)
  301. } else {
  302. XCTFail()
  303. }
  304. group.leave()
  305. }
  306. group.enter()
  307. self.imageView.kf.setImage(with: testURLs[1]) { result in
  308. XCTAssertNotNil(result.value?.image)
  309. XCTAssertEqual(result.value?.source.url, testURLs[1])
  310. XCTAssertEqual(result.value!.image, self.imageView.image)
  311. group.leave()
  312. }
  313. group.notify(queue: .main, execute: exp.fulfill)
  314. waitForExpectations(timeout: 3, handler: nil)
  315. }
  316. func testSettingNilURL() {
  317. let exp = expectation(description: #function)
  318. let url: URL? = nil
  319. imageView.kf.setImage(with: url, progressBlock: { _, _ in XCTFail() }) {
  320. result in
  321. XCTAssertNotNil(result.error)
  322. guard case .imageSettingError(reason: .emptySource) = result.error! else {
  323. XCTFail()
  324. fatalError()
  325. }
  326. exp.fulfill()
  327. }
  328. waitForExpectations(timeout: 3, handler: nil)
  329. }
  330. func testSettingImageWhileKeepingCurrentOne() {
  331. let exp = expectation(description: #function)
  332. let url = testURLs[0]
  333. stub(url, data: testImageData)
  334. imageView.image = testImage
  335. imageView.kf.setImage(with: url) { result in }
  336. XCTAssertNil(imageView.image)
  337. imageView.image = testImage
  338. imageView.kf.setImage(with: url, options: [.keepCurrentImageWhileLoading]) { result in
  339. XCTAssertEqual(self.imageView.image, result.value!.image)
  340. XCTAssertNotEqual(self.imageView.image, testImage)
  341. exp.fulfill()
  342. }
  343. XCTAssertEqual(testImage, imageView.image)
  344. waitForExpectations(timeout: 3, handler: nil)
  345. }
  346. func testSettingImageKeepingRespectingPlaceholder() {
  347. let exp = expectation(description: #function)
  348. let url = testURLs[0]
  349. stub(url, data: testImageData)
  350. // While current image is nil, set placeholder
  351. imageView.kf.setImage(with: url, placeholder: testImage, options: [.keepCurrentImageWhileLoading]) { result in }
  352. XCTAssertNotNil(imageView.image)
  353. XCTAssertEqual(testImage, imageView.image)
  354. // While current image is not nil, keep it
  355. let anotherImage = Image(data: testImageJEPGData)
  356. imageView.image = anotherImage
  357. imageView.kf.setImage(with: url, placeholder: testImage, options: [.keepCurrentImageWhileLoading]) { result in
  358. XCTAssertNotEqual(self.imageView.image, anotherImage)
  359. exp.fulfill()
  360. }
  361. XCTAssertNotNil(imageView.image)
  362. XCTAssertEqual(anotherImage, imageView.image)
  363. waitForExpectations(timeout: 3, handler: nil)
  364. }
  365. func testSetGIFImageOnlyFirstFrameThenFullFrames() {
  366. let exp = expectation(description: #function)
  367. let url = testURLs[0]
  368. stub(url, data: testImageGIFData, length: 123)
  369. func loadFullGIFImage() {
  370. ImageCache.default.clearMemoryCache()
  371. imageView.kf.setImage(with: url, progressBlock: { _, _ in XCTFail() })
  372. {
  373. result in
  374. let image = result.value?.image
  375. XCTAssertNotNil(image)
  376. XCTAssertNotNil(image!.kf.images)
  377. XCTAssertEqual(image!.kf.images?.count, 8)
  378. XCTAssertEqual(result.value!.cacheType, .disk)
  379. XCTAssertTrue(Thread.isMainThread)
  380. exp.fulfill()
  381. }
  382. }
  383. var progressBlockIsCalled = false
  384. imageView.kf.setImage(with: url, options: [.onlyLoadFirstFrame, .waitForCache], progressBlock: { _, _ in
  385. progressBlockIsCalled = true
  386. XCTAssertTrue(Thread.isMainThread)
  387. })
  388. {
  389. result in
  390. XCTAssertTrue(progressBlockIsCalled)
  391. let image = result.value?.image
  392. XCTAssertNotNil(image)
  393. XCTAssertNil(image!.kf.images)
  394. XCTAssert(result.value!.cacheType == .none)
  395. let memory = KingfisherManager.shared.cache.memoryStorage.value(forKey: url.cacheKey)
  396. XCTAssertNotNil(memory)
  397. let disk = try! KingfisherManager.shared.cache.diskStorage.value(forKey: url.cacheKey)
  398. XCTAssertNotNil(disk)
  399. XCTAssertTrue(Thread.isMainThread)
  400. loadFullGIFImage()
  401. }
  402. waitForExpectations(timeout: 3, handler: nil)
  403. }
  404. // https://github.com/onevcat/Kingfisher/issues/665
  405. // The completion handler should be called even when the image view loading url gets changed.
  406. func testIssue665() {
  407. let exp = expectation(description: #function)
  408. stub(testURLs[0], data: testImageData)
  409. stub(testURLs[1], data: testImageData)
  410. let group = DispatchGroup()
  411. group.enter()
  412. imageView.kf.setImage(with: testURLs[0]) { _ in
  413. group.leave()
  414. }
  415. group.enter()
  416. imageView.kf.setImage(with: testURLs[1]) { _ in
  417. group.leave()
  418. }
  419. group.notify(queue: .main, execute: exp.fulfill)
  420. waitForExpectations(timeout: 3, handler: nil)
  421. }
  422. func testImageSettingWithPlaceholder() {
  423. let exp = expectation(description: #function)
  424. let url = testURLs[0]
  425. stub(url, data: testImageData, length: 123)
  426. let emptyImage = Image()
  427. var processBlockCalled = false
  428. imageView.kf.setImage(
  429. with: url,
  430. placeholder: emptyImage,
  431. progressBlock: { _, _ in
  432. processBlockCalled = true
  433. XCTAssertEqual(self.imageView.image, emptyImage)
  434. })
  435. {
  436. result in
  437. XCTAssertTrue(processBlockCalled)
  438. XCTAssertTrue(self.imageView.image!.renderEqual(to: testImage))
  439. exp.fulfill()
  440. }
  441. waitForExpectations(timeout: 3, handler: nil)
  442. }
  443. func testImageSettingWithCustomizePlaceholder() {
  444. let exp = expectation(description: #function)
  445. let url = testURLs[0]
  446. stub(url, data: testImageData, length: 123)
  447. let view = View()
  448. var processBlockCalled = false
  449. imageView.kf.setImage(
  450. with: url,
  451. placeholder: view,
  452. progressBlock: { _, _ in
  453. processBlockCalled = true
  454. XCTAssertNil(self.imageView.image)
  455. XCTAssertTrue(self.imageView.subviews.contains(view))
  456. })
  457. {
  458. result in
  459. XCTAssertTrue(processBlockCalled)
  460. XCTAssertTrue(self.imageView.image!.renderEqual(to: testImage))
  461. XCTAssertFalse(self.imageView.subviews.contains(view))
  462. exp.fulfill()
  463. }
  464. waitForExpectations(timeout: 3, handler: nil)
  465. }
  466. func testSettingNonWorkingImageWithFailureImage() {
  467. let exp = expectation(description: #function)
  468. let url = testURLs[0]
  469. stub(url, errorCode: 404)
  470. imageView.kf.setImage(with: url, options: [.onFailureImage(testImage)]) {
  471. result in
  472. XCTAssertNil(result.value)
  473. XCTAssertEqual(self.imageView.image, testImage)
  474. exp.fulfill()
  475. }
  476. XCTAssertNil(imageView.image)
  477. waitForExpectations(timeout: 5, handler: nil)
  478. }
  479. func testSettingNonWorkingImageWithEmptyFailureImage() {
  480. let exp = expectation(description: #function)
  481. let url = testURLs[0]
  482. stub(url, errorCode: 404)
  483. imageView.kf.setImage(with: url, placeholder: testImage, options: [.onFailureImage(nil)]) {
  484. result in
  485. XCTAssertNil(result.value)
  486. XCTAssertNil(self.imageView.image)
  487. exp.fulfill()
  488. }
  489. XCTAssertEqual(testImage, imageView.image)
  490. waitForExpectations(timeout: 5, handler: nil)
  491. }
  492. func testSettingNonWorkingImageWithoutFailureImage() {
  493. let exp = expectation(description: #function)
  494. let url = testURLs[0]
  495. stub(url, errorCode: 404)
  496. imageView.kf.setImage(with: url, placeholder: testImage) {
  497. result in
  498. XCTAssertNil(result.value)
  499. XCTAssertEqual(testImage, self.imageView.image)
  500. exp.fulfill()
  501. }
  502. XCTAssertEqual(testImage, imageView.image)
  503. waitForExpectations(timeout: 5, handler: nil)
  504. }
  505. // https://github.com/onevcat/Kingfisher/issues/1053
  506. func testSetSameURLWithDifferentProcessors() {
  507. let exp = expectation(description: #function)
  508. let url = testURLs[0]
  509. stub(url, data: testImageData)
  510. let size1 = CGSize(width: 10, height: 10)
  511. let p1 = ResizingImageProcessor(referenceSize: size1)
  512. let size2 = CGSize(width: 20, height: 20)
  513. let p2 = ResizingImageProcessor(referenceSize: size2)
  514. let group = DispatchGroup()
  515. group.enter()
  516. imageView.kf.setImage(with: url, options: [.processor(p1)]) { result in
  517. XCTAssertNotNil(result.error)
  518. XCTAssertTrue(result.error!.isNotCurrentTask)
  519. group.leave()
  520. }
  521. group.enter()
  522. imageView.kf.setImage(with: url, options: [.processor(p2)]) { result in
  523. XCTAssertNotNil(result.value)
  524. XCTAssertEqual(result.value!.image.size, size2)
  525. group.leave()
  526. }
  527. group.notify(queue: .main) { exp.fulfill() }
  528. waitForExpectations(timeout: 3, handler: nil)
  529. }
  530. func testImageCacheExtendingExpirationTask() {
  531. let exp = expectation(description: #function)
  532. let url = testURLs[0]
  533. stub(url, data: testImageData)
  534. let options: KingfisherOptionsInfo = [.cacheMemoryOnly, .memoryCacheExpiration(.seconds(1)), .memoryCacheAccessExtendingExpiration(.expirationTime(.seconds(100)))]
  535. imageView.kf.setImage(with: url, options: options) { result in
  536. XCTAssertNotNil(result.value?.image)
  537. XCTAssertTrue(result.value!.cacheType == .none)
  538. let cacheKey = result.value!.source.cacheKey as NSString
  539. let expirationTime1 = ImageCache.default.memoryStorage.storage.object(forKey: cacheKey)?.estimatedExpiration
  540. XCTAssertNotNil(expirationTime1)
  541. delay(0.1, block: {
  542. self.imageView.kf.setImage(with: url, options: options) { result in
  543. XCTAssertNotNil(result.value?.image)
  544. XCTAssertTrue(result.value!.cacheType == .memory)
  545. let expirationTime2 = ImageCache.default.memoryStorage.storage.object(forKey: cacheKey)?.estimatedExpiration
  546. XCTAssertNotNil(expirationTime2)
  547. XCTAssertNotEqual(expirationTime1, expirationTime2)
  548. XCTAssert(expirationTime1!.isPast(referenceDate: expirationTime2!))
  549. XCTAssertGreaterThan(expirationTime2!.timeIntervalSince(expirationTime1!), 10)
  550. exp.fulfill()
  551. }
  552. })
  553. }
  554. waitForExpectations(timeout: 3, handler: nil)
  555. }
  556. func testImageCacheNotExtendingExpirationTask() {
  557. let exp = expectation(description: #function)
  558. let url = testURLs[0]
  559. stub(url, data: testImageData)
  560. let options: KingfisherOptionsInfo = [.cacheMemoryOnly, .memoryCacheExpiration(.seconds(1)), .memoryCacheAccessExtendingExpiration(.none)]
  561. imageView.kf.setImage(with: url, options: options) { result in
  562. XCTAssertNotNil(result.value?.image)
  563. XCTAssertTrue(result.value!.cacheType == .none)
  564. let cacheKey = result.value!.source.cacheKey as NSString
  565. let expirationTime1 = ImageCache.default.memoryStorage.storage.object(forKey: cacheKey)?.estimatedExpiration
  566. XCTAssertNotNil(expirationTime1)
  567. delay(0.1, block: {
  568. self.imageView.kf.setImage(with: url, options: options) { result in
  569. XCTAssertNotNil(result.value?.image)
  570. XCTAssertTrue(result.value!.cacheType == .memory)
  571. let expirationTime2 = ImageCache.default.memoryStorage.storage.object(forKey: cacheKey)?.estimatedExpiration
  572. XCTAssertNotNil(expirationTime2)
  573. XCTAssertEqual(expirationTime1, expirationTime2)
  574. exp.fulfill()
  575. }
  576. })
  577. }
  578. waitForExpectations(timeout: 3, handler: nil)
  579. }
  580. }
  581. extension View: Placeholder {}