ImageViewExtensionTests.swift 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  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: KFCrossPlatformImageView!
  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 = KFCrossPlatformImageView()
  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 = KFCrossPlatformImageView()
  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. cleanDefaultCache()
  197. let exp = expectation(description: #function)
  198. let url = testURLs[0]
  199. stub(url, data: testImageData)
  200. let key = url.cacheKey
  201. imageView.kf.setImage(with: url, options: [.targetCache(cache1)]) { result in
  202. XCTAssertTrue(cache1.imageCachedType(forKey: key).cached)
  203. XCTAssertFalse(cache2.imageCachedType(forKey: key).cached)
  204. XCTAssertFalse(KingfisherManager.shared.cache.imageCachedType(forKey: key).cached)
  205. self.imageView.kf.setImage(with: url, options: [.targetCache(cache2), .waitForCache]) { result in
  206. XCTAssertTrue(cache1.imageCachedType(forKey: key).cached)
  207. XCTAssertTrue(cache2.imageCachedType(forKey: key).cached)
  208. XCTAssertFalse(KingfisherManager.shared.cache.imageCachedType(forKey: key).cached)
  209. exp.fulfill()
  210. }
  211. }
  212. waitForExpectations(timeout: 5) { error in
  213. clearCaches([cache1, cache2])
  214. }
  215. }
  216. func testIndicatorViewExisting() {
  217. imageView.kf.indicatorType = .activity
  218. XCTAssertNotNil(imageView.kf.indicator)
  219. XCTAssertTrue(imageView.kf.indicator is ActivityIndicator)
  220. imageView.kf.indicatorType = .none
  221. XCTAssertNil(imageView.kf.indicator)
  222. }
  223. func testCustomizeStructIndicatorExisting() {
  224. struct StructIndicator: Indicator {
  225. let view = KFCrossPlatformView()
  226. func startAnimatingView() {}
  227. func stopAnimatingView() {}
  228. }
  229. imageView.kf.indicatorType = .custom(indicator: StructIndicator())
  230. XCTAssertNotNil(imageView.kf.indicator)
  231. XCTAssertTrue(imageView.kf.indicator is StructIndicator)
  232. imageView.kf.indicatorType = .none
  233. XCTAssertNil(imageView.kf.indicator)
  234. }
  235. func testActivityIndicatorViewAnimating() {
  236. imageView.kf.indicatorType = .activity
  237. let exp = expectation(description: #function)
  238. let url = testURLs[0]
  239. stub(url, data: testImageData)
  240. imageView.kf.setImage(with: url, progressBlock: { receivedSize, totalSize in
  241. let indicator = self.imageView.kf.indicator
  242. XCTAssertNotNil(indicator)
  243. XCTAssertFalse(indicator!.view.isHidden)
  244. })
  245. {
  246. result in
  247. let indicator = self.imageView.kf.indicator
  248. XCTAssertTrue(indicator!.view.isHidden)
  249. exp.fulfill()
  250. }
  251. waitForExpectations(timeout: 3, handler: nil)
  252. }
  253. func testCanUseImageIndicatorViewAnimating() {
  254. imageView.kf.indicatorType = .image(imageData: testImageData)
  255. XCTAssertTrue(imageView.kf.indicator is ImageIndicator)
  256. let image = (imageView.kf.indicator?.view as? KFCrossPlatformImageView)?.image
  257. XCTAssertNotNil(image)
  258. XCTAssertTrue(image!.renderEqual(to: testImage))
  259. let exp = expectation(description: #function)
  260. let url = testURLs[0]
  261. stub(url, data: testImageData)
  262. imageView.kf.setImage(with: url, progressBlock: { receivedSize, totalSize in
  263. let indicator = self.imageView.kf.indicator
  264. XCTAssertNotNil(indicator)
  265. XCTAssertFalse(indicator!.view.isHidden)
  266. })
  267. {
  268. result in
  269. let indicator = self.imageView.kf.indicator
  270. XCTAssertTrue(indicator!.view.isHidden)
  271. exp.fulfill()
  272. }
  273. waitForExpectations(timeout: 3, handler: nil)
  274. }
  275. func testCancelImageTask() {
  276. let exp = expectation(description: #function)
  277. let url = testURLs[0]
  278. let stub = delayedStub(url, data: testImageData)
  279. imageView.kf.setImage(with: url, progressBlock: { _, _ in XCTFail() }) { result in
  280. XCTAssertNotNil(result.error)
  281. XCTAssertTrue(result.error!.isTaskCancelled)
  282. delay(0.1) { exp.fulfill() }
  283. }
  284. self.imageView.kf.cancelDownloadTask()
  285. _ = stub.go()
  286. waitForExpectations(timeout: 3, handler: nil)
  287. }
  288. func testDownloadForMutipleURLs() {
  289. let exp = expectation(description: #function)
  290. stub(testURLs[0], data: testImageData)
  291. stub(testURLs[1], data: testImageData)
  292. let group = DispatchGroup()
  293. group.enter()
  294. imageView.kf.setImage(with: testURLs[0]) { result in
  295. // The download successed, but not the resource we want.
  296. XCTAssertNotNil(result.error)
  297. if case .imageSettingError(
  298. reason: .notCurrentSourceTask(let result, _, let source)) = result.error!
  299. {
  300. XCTAssertEqual(source.url, testURLs[0])
  301. XCTAssertNotEqual(result!.image, self.imageView.image)
  302. } else {
  303. XCTFail()
  304. }
  305. group.leave()
  306. }
  307. group.enter()
  308. self.imageView.kf.setImage(with: testURLs[1]) { result in
  309. XCTAssertNotNil(result.value?.image)
  310. XCTAssertEqual(result.value?.source.url, testURLs[1])
  311. XCTAssertEqual(result.value!.image, self.imageView.image)
  312. group.leave()
  313. }
  314. group.notify(queue: .main, execute: exp.fulfill)
  315. waitForExpectations(timeout: 3, handler: nil)
  316. }
  317. func testSettingNilURL() {
  318. let exp = expectation(description: #function)
  319. let url: URL? = nil
  320. imageView.kf.setImage(with: url, progressBlock: { _, _ in XCTFail() }) {
  321. result in
  322. XCTAssertNotNil(result.error)
  323. guard case .imageSettingError(reason: .emptySource) = result.error! else {
  324. XCTFail()
  325. fatalError()
  326. }
  327. exp.fulfill()
  328. }
  329. waitForExpectations(timeout: 3, handler: nil)
  330. }
  331. func testSettingImageWhileKeepingCurrentOne() {
  332. let exp = expectation(description: #function)
  333. let url = testURLs[0]
  334. stub(url, data: testImageData)
  335. imageView.image = testImage
  336. imageView.kf.setImage(with: url) { result in }
  337. XCTAssertNil(imageView.image)
  338. imageView.image = testImage
  339. imageView.kf.setImage(with: url, options: [.keepCurrentImageWhileLoading]) { result in
  340. XCTAssertEqual(self.imageView.image, result.value!.image)
  341. XCTAssertNotEqual(self.imageView.image, testImage)
  342. exp.fulfill()
  343. }
  344. XCTAssertEqual(testImage, imageView.image)
  345. waitForExpectations(timeout: 3, handler: nil)
  346. }
  347. func testSettingImageKeepingRespectingPlaceholder() {
  348. let exp = expectation(description: #function)
  349. let url = testURLs[0]
  350. stub(url, data: testImageData)
  351. // While current image is nil, set placeholder
  352. imageView.kf.setImage(with: url, placeholder: testImage, options: [.keepCurrentImageWhileLoading]) { result in }
  353. XCTAssertNotNil(imageView.image)
  354. XCTAssertEqual(testImage, imageView.image)
  355. // While current image is not nil, keep it
  356. let anotherImage = KFCrossPlatformImage(data: testImageJEPGData)
  357. imageView.image = anotherImage
  358. imageView.kf.setImage(with: url, placeholder: testImage, options: [.keepCurrentImageWhileLoading]) { result in
  359. XCTAssertNotEqual(self.imageView.image, anotherImage)
  360. exp.fulfill()
  361. }
  362. XCTAssertNotNil(imageView.image)
  363. XCTAssertEqual(anotherImage, imageView.image)
  364. waitForExpectations(timeout: 3, handler: nil)
  365. }
  366. func testSetGIFImageOnlyFirstFrameThenFullFrames() {
  367. let exp = expectation(description: #function)
  368. let url = testURLs[0]
  369. stub(url, data: testImageGIFData, length: 123)
  370. func loadFullGIFImage() {
  371. ImageCache.default.clearMemoryCache()
  372. imageView.kf.setImage(with: url, progressBlock: { _, _ in XCTFail() })
  373. {
  374. result in
  375. let image = result.value?.image
  376. XCTAssertNotNil(image)
  377. XCTAssertNotNil(image!.kf.images)
  378. XCTAssertEqual(image!.kf.images?.count, 8)
  379. XCTAssertEqual(result.value!.cacheType, .disk)
  380. XCTAssertTrue(Thread.isMainThread)
  381. exp.fulfill()
  382. }
  383. }
  384. var progressBlockIsCalled = false
  385. imageView.kf.setImage(with: url, options: [.onlyLoadFirstFrame, .waitForCache], progressBlock: { _, _ in
  386. progressBlockIsCalled = true
  387. XCTAssertTrue(Thread.isMainThread)
  388. })
  389. {
  390. result in
  391. XCTAssertTrue(progressBlockIsCalled)
  392. let image = result.value?.image
  393. XCTAssertNotNil(image)
  394. XCTAssertNil(image!.kf.images)
  395. XCTAssert(result.value!.cacheType == .none)
  396. let memory = KingfisherManager.shared.cache.memoryStorage.value(forKey: url.cacheKey)
  397. XCTAssertNotNil(memory)
  398. let disk = try! KingfisherManager.shared.cache.diskStorage.value(forKey: url.cacheKey)
  399. XCTAssertNotNil(disk)
  400. XCTAssertTrue(Thread.isMainThread)
  401. loadFullGIFImage()
  402. }
  403. waitForExpectations(timeout: 3, handler: nil)
  404. }
  405. // https://github.com/onevcat/Kingfisher/issues/665
  406. // The completion handler should be called even when the image view loading url gets changed.
  407. func testIssue665() {
  408. let exp = expectation(description: #function)
  409. stub(testURLs[0], data: testImageData)
  410. stub(testURLs[1], data: testImageData)
  411. let group = DispatchGroup()
  412. group.enter()
  413. imageView.kf.setImage(with: testURLs[0]) { _ in
  414. group.leave()
  415. }
  416. group.enter()
  417. imageView.kf.setImage(with: testURLs[1]) { _ in
  418. group.leave()
  419. }
  420. group.notify(queue: .main, execute: exp.fulfill)
  421. waitForExpectations(timeout: 3, handler: nil)
  422. }
  423. func testImageSettingWithPlaceholder() {
  424. let exp = expectation(description: #function)
  425. let url = testURLs[0]
  426. stub(url, data: testImageData, length: 123)
  427. let emptyImage = KFCrossPlatformImage()
  428. var processBlockCalled = false
  429. imageView.kf.setImage(
  430. with: url,
  431. placeholder: emptyImage,
  432. progressBlock: { _, _ in
  433. processBlockCalled = true
  434. XCTAssertEqual(self.imageView.image, emptyImage)
  435. })
  436. {
  437. result in
  438. XCTAssertTrue(processBlockCalled)
  439. XCTAssertTrue(self.imageView.image!.renderEqual(to: testImage))
  440. exp.fulfill()
  441. }
  442. waitForExpectations(timeout: 3, handler: nil)
  443. }
  444. func testImageSettingWithCustomizePlaceholder() {
  445. let exp = expectation(description: #function)
  446. let url = testURLs[0]
  447. stub(url, data: testImageData, length: 123)
  448. let view = KFCrossPlatformView()
  449. var processBlockCalled = false
  450. imageView.kf.setImage(
  451. with: url,
  452. placeholder: view,
  453. progressBlock: { _, _ in
  454. processBlockCalled = true
  455. XCTAssertNil(self.imageView.image)
  456. XCTAssertTrue(self.imageView.subviews.contains(view))
  457. })
  458. {
  459. result in
  460. XCTAssertTrue(processBlockCalled)
  461. XCTAssertTrue(self.imageView.image!.renderEqual(to: testImage))
  462. XCTAssertFalse(self.imageView.subviews.contains(view))
  463. exp.fulfill()
  464. }
  465. waitForExpectations(timeout: 3, handler: nil)
  466. }
  467. func testSettingNonWorkingImageWithFailureImage() {
  468. let exp = expectation(description: #function)
  469. let url = testURLs[0]
  470. stub(url, errorCode: 404)
  471. imageView.kf.setImage(with: url, options: [.onFailureImage(testImage)]) {
  472. result in
  473. XCTAssertNil(result.value)
  474. XCTAssertEqual(self.imageView.image, testImage)
  475. exp.fulfill()
  476. }
  477. XCTAssertNil(imageView.image)
  478. waitForExpectations(timeout: 5, handler: nil)
  479. }
  480. func testSettingNonWorkingImageWithEmptyFailureImage() {
  481. let exp = expectation(description: #function)
  482. let url = testURLs[0]
  483. stub(url, errorCode: 404)
  484. imageView.kf.setImage(with: url, placeholder: testImage, options: [.onFailureImage(nil)]) {
  485. result in
  486. XCTAssertNil(result.value)
  487. XCTAssertNil(self.imageView.image)
  488. exp.fulfill()
  489. }
  490. XCTAssertEqual(testImage, imageView.image)
  491. waitForExpectations(timeout: 5, handler: nil)
  492. }
  493. func testSettingNonWorkingImageWithoutFailureImage() {
  494. let exp = expectation(description: #function)
  495. let url = testURLs[0]
  496. stub(url, errorCode: 404)
  497. imageView.kf.setImage(with: url, placeholder: testImage) {
  498. result in
  499. XCTAssertNil(result.value)
  500. XCTAssertEqual(testImage, self.imageView.image)
  501. exp.fulfill()
  502. }
  503. XCTAssertEqual(testImage, imageView.image)
  504. waitForExpectations(timeout: 5, handler: nil)
  505. }
  506. // https://github.com/onevcat/Kingfisher/issues/1053
  507. func testSetSameURLWithDifferentProcessors() {
  508. let exp = expectation(description: #function)
  509. let url = testURLs[0]
  510. stub(url, data: testImageData)
  511. let size1 = CGSize(width: 10, height: 10)
  512. let p1 = ResizingImageProcessor(referenceSize: size1)
  513. let size2 = CGSize(width: 20, height: 20)
  514. let p2 = ResizingImageProcessor(referenceSize: size2)
  515. let group = DispatchGroup()
  516. group.enter()
  517. imageView.kf.setImage(with: url, options: [.processor(p1)]) { result in
  518. XCTAssertNotNil(result.error)
  519. XCTAssertTrue(result.error!.isNotCurrentTask)
  520. group.leave()
  521. }
  522. group.enter()
  523. imageView.kf.setImage(with: url, options: [.processor(p2)]) { result in
  524. XCTAssertNotNil(result.value)
  525. XCTAssertEqual(result.value!.image.size, size2)
  526. group.leave()
  527. }
  528. group.notify(queue: .main) { exp.fulfill() }
  529. waitForExpectations(timeout: 3, handler: nil)
  530. }
  531. func testMemoryImageCacheExtendingExpirationTask() {
  532. let exp = expectation(description: #function)
  533. let url = testURLs[0]
  534. stub(url, data: testImageData)
  535. let options: KingfisherOptionsInfo = [.cacheMemoryOnly, .memoryCacheExpiration(.seconds(1)), .memoryCacheAccessExtendingExpiration(.expirationTime(.seconds(100)))]
  536. imageView.kf.setImage(with: url, options: options) { result in
  537. XCTAssertNotNil(result.value?.image)
  538. XCTAssertTrue(result.value!.cacheType == .none)
  539. let cacheKey = result.value!.source.cacheKey as NSString
  540. let expirationTime1 = ImageCache.default.memoryStorage.storage.object(forKey: cacheKey)?.estimatedExpiration
  541. XCTAssertNotNil(expirationTime1)
  542. delay(0.1, block: {
  543. self.imageView.kf.setImage(with: url, options: options) { result in
  544. XCTAssertNotNil(result.value?.image)
  545. XCTAssertTrue(result.value!.cacheType == .memory)
  546. let expirationTime2 = ImageCache.default.memoryStorage.storage.object(forKey: cacheKey)?.estimatedExpiration
  547. XCTAssertNotNil(expirationTime2)
  548. XCTAssertNotEqual(expirationTime1, expirationTime2)
  549. XCTAssert(expirationTime1!.isPast(referenceDate: expirationTime2!))
  550. XCTAssertGreaterThan(expirationTime2!.timeIntervalSince(expirationTime1!), 10)
  551. exp.fulfill()
  552. }
  553. })
  554. }
  555. waitForExpectations(timeout: 3, handler: nil)
  556. }
  557. func testMemoryImageCacheNotExtendingExpirationTask() {
  558. let exp = expectation(description: #function)
  559. let url = testURLs[0]
  560. stub(url, data: testImageData)
  561. let options: KingfisherOptionsInfo = [.cacheMemoryOnly, .memoryCacheExpiration(.seconds(1)), .memoryCacheAccessExtendingExpiration(.none)]
  562. imageView.kf.setImage(with: url, options: options) { result in
  563. XCTAssertNotNil(result.value?.image)
  564. XCTAssertTrue(result.value!.cacheType == .none)
  565. let cacheKey = result.value!.source.cacheKey as NSString
  566. let expirationTime1 = ImageCache.default.memoryStorage.storage.object(forKey: cacheKey)?.estimatedExpiration
  567. XCTAssertNotNil(expirationTime1)
  568. delay(0.1, block: {
  569. self.imageView.kf.setImage(with: url, options: options) { result in
  570. XCTAssertNotNil(result.value?.image)
  571. XCTAssertTrue(result.value!.cacheType == .memory)
  572. let expirationTime2 = ImageCache.default.memoryStorage.storage.object(forKey: cacheKey)?.estimatedExpiration
  573. XCTAssertNotNil(expirationTime2)
  574. XCTAssertEqual(expirationTime1, expirationTime2)
  575. exp.fulfill()
  576. }
  577. })
  578. }
  579. waitForExpectations(timeout: 3, handler: nil)
  580. }
  581. func testDiskImageCacheExtendingExpirationTask() {
  582. let exp = expectation(description: #function)
  583. let url = testURLs[0]
  584. stub(url, data: testImageData)
  585. let options: KingfisherOptionsInfo = [.memoryCacheExpiration(.expired),
  586. .diskCacheExpiration(.seconds(2)),
  587. .diskCacheAccessExtendingExpiration(.expirationTime(.seconds(100)))]
  588. imageView.kf.setImage(with: url, options: options) { result in
  589. XCTAssertNotNil(result.value?.image)
  590. XCTAssertTrue(result.value!.cacheType == .none)
  591. delay(1, block: {
  592. self.imageView.kf.setImage(with: url, options: options) { result in
  593. XCTAssertNotNil(result.value?.image)
  594. XCTAssertTrue(result.value!.cacheType == .disk)
  595. delay(2, block: {
  596. self.imageView.kf.setImage(with: url, options: options) { result in
  597. XCTAssertNotNil(result.value?.image)
  598. XCTAssertTrue(result.value!.cacheType == .disk)
  599. exp.fulfill()
  600. }
  601. })
  602. }
  603. })
  604. }
  605. waitForExpectations(timeout: 5, handler: nil)
  606. }
  607. func testDiskImageCacheNotExtendingExpirationTask() {
  608. let exp = expectation(description: #function)
  609. let url = testURLs[0]
  610. stub(url, data: testImageData)
  611. let options: KingfisherOptionsInfo = [.memoryCacheExpiration(.expired),
  612. .diskCacheExpiration(.seconds(2)),
  613. .diskCacheAccessExtendingExpiration(.none)]
  614. imageView.kf.setImage(with: url, options: options) { result in
  615. XCTAssertNotNil(result.value?.image)
  616. XCTAssertTrue(result.value!.cacheType == .none)
  617. delay(1, block: {
  618. self.imageView.kf.setImage(with: url, options: options) { result in
  619. XCTAssertNotNil(result.value?.image)
  620. XCTAssertTrue(result.value!.cacheType == .disk)
  621. delay(2, block: {
  622. self.imageView.kf.setImage(with: url, options: options) { result in
  623. XCTAssertNotNil(result.value?.image)
  624. XCTAssertTrue(result.value!.cacheType == .none)
  625. exp.fulfill()
  626. }
  627. })
  628. }
  629. })
  630. }
  631. waitForExpectations(timeout: 5, handler: nil)
  632. }
  633. func testImageSettingWithAlternativeSource() {
  634. let exp = expectation(description: #function)
  635. let url = testURLs[0]
  636. stub(url, data: testImageData)
  637. let brokenURL = URL(string: "brokenurl")!
  638. stub(brokenURL, data: Data())
  639. imageView.kf.setImage(
  640. with: .network(brokenURL),
  641. options: [.alternativeSources([.network(url)])]
  642. ) { result in
  643. XCTAssertNotNil(result.value)
  644. XCTAssertEqual(result.value!.source.url, url)
  645. XCTAssertEqual(result.value!.originalSource.url, brokenURL)
  646. exp.fulfill()
  647. }
  648. waitForExpectations(timeout: 1, handler: nil)
  649. }
  650. func testImageSettingCanCancelAlternativeSource() {
  651. let exp = expectation(description: #function)
  652. let url = testURLs[0]
  653. let dataStub = delayedStub(url, data: testImageData)
  654. let brokenURL = testURLs[1]
  655. let brokenStub = delayedStub(brokenURL, data: Data())
  656. var finishCalled = false
  657. delay(0.1) {
  658. _ = brokenStub.go()
  659. }
  660. delay(0.3) {
  661. self.imageView.kf.cancelDownloadTask()
  662. }
  663. delay(0.5) {
  664. _ = dataStub.go()
  665. XCTAssertTrue(finishCalled)
  666. exp.fulfill()
  667. }
  668. imageView.kf.setImage(
  669. with: .network(brokenURL),
  670. options: [.alternativeSources([.network(url)])]
  671. ) { result in
  672. finishCalled = true
  673. XCTAssertNotNil(result.error)
  674. guard case .requestError(reason: .taskCancelled(let task, _)) = result.error! else {
  675. XCTFail("The error should be a task cancelled.")
  676. return
  677. }
  678. XCTAssertEqual(task.task.originalRequest?.url, url, "Should be the alternatived url cancelled.")
  679. }
  680. waitForExpectations(timeout: 1, handler: nil)
  681. }
  682. }
  683. extension KFCrossPlatformView: Placeholder {}