UIImageViewExtensionTests.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. //
  2. // UIImageViewExtensionTests.swift
  3. // Kingfisher
  4. //
  5. // Created by Wei Wang on 15/4/17.
  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. @testable import Kingfisher
  29. class UIImageViewExtensionTests: XCTestCase {
  30. var imageView: UIImageView!
  31. override class func setUp() {
  32. super.setUp()
  33. LSNocilla.sharedInstance().start()
  34. }
  35. override class func tearDown() {
  36. super.tearDown()
  37. LSNocilla.sharedInstance().stop()
  38. }
  39. override func setUp() {
  40. super.setUp()
  41. // Put setup code here. This method is called before the invocation of each test method in the class.
  42. imageView = UIImageView()
  43. KingfisherManager.sharedManager.downloader = ImageDownloader(name: "testDownloader")
  44. cleanDefaultCache()
  45. }
  46. override func tearDown() {
  47. // Put teardown code here. This method is called after the invocation of each test method in the class.
  48. LSNocilla.sharedInstance().clearStubs()
  49. imageView = nil
  50. cleanDefaultCache()
  51. super.tearDown()
  52. }
  53. func testImageDownloadForImageView() {
  54. let expectation = expectationWithDescription("wait for downloading image")
  55. let URLString = testKeys[0]
  56. stubRequest("GET", URLString).andReturn(200).withBody(testImageData)
  57. let URL = NSURL(string: URLString)!
  58. var progressBlockIsCalled = false
  59. imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
  60. progressBlockIsCalled = true
  61. }) { (image, error, cacheType, imageURL) -> () in
  62. expectation.fulfill()
  63. XCTAssert(progressBlockIsCalled, "progressBlock should be called at least once.")
  64. XCTAssert(image != nil, "Downloaded image should exist.")
  65. XCTAssert(image! == testImage, "Downloaded image should be the same as test image.")
  66. XCTAssert(self.imageView.image! == testImage, "Downloaded image should be already set to the image property.")
  67. XCTAssert(self.imageView.kf_webURL == imageURL, "Web URL should equal to the downloaded url.")
  68. XCTAssert(cacheType == .None, "The cache type should be none here. This image was just downloaded.")
  69. }
  70. waitForExpectationsWithTimeout(5, handler: nil)
  71. }
  72. func testImageDownloadWithResourceForImageView() {
  73. let expectation = expectationWithDescription("wait for downloading image")
  74. let URLString = testKeys[0]
  75. stubRequest("GET", URLString).andReturn(200).withBody(testImageData)
  76. let URL = NSURL(string: URLString)!
  77. let resource = Resource(downloadURL: URL)
  78. var progressBlockIsCalled = false
  79. imageView.kf_setImageWithResource(resource, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
  80. progressBlockIsCalled = true
  81. }) { (image, error, cacheType, imageURL) -> () in
  82. expectation.fulfill()
  83. XCTAssert(progressBlockIsCalled, "progressBlock should be called at least once.")
  84. XCTAssert(image != nil, "Downloaded image should exist.")
  85. XCTAssert(image! == testImage, "Downloaded image should be the same as test image.")
  86. XCTAssert(self.imageView.image! == testImage, "Downloaded image should be already set to the image property.")
  87. XCTAssert(self.imageView.kf_webURL == imageURL, "Web URL should equal to the downloaded url.")
  88. XCTAssert(cacheType == .None, "The cache type should be none here. This image was just downloaded.")
  89. }
  90. waitForExpectationsWithTimeout(5, handler: nil)
  91. }
  92. func testImageDownloadCancelForImageView() {
  93. let expectation = expectationWithDescription("wait for downloading image")
  94. let URLString = testKeys[0]
  95. stubRequest("GET", URLString).andReturn(200).withBody(testImageData)
  96. let URL = NSURL(string: URLString)!
  97. var progressBlockIsCalled = false
  98. var completionBlockIsCalled = false
  99. let task = imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
  100. progressBlockIsCalled = true
  101. }) { (image, error, cacheType, imageURL) -> () in
  102. completionBlockIsCalled = true
  103. }
  104. task.cancel()
  105. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(Double(NSEC_PER_SEC) * 0.09)), dispatch_get_main_queue()) { () -> Void in
  106. expectation.fulfill()
  107. XCTAssert(progressBlockIsCalled == false, "ProgressBlock should not be called since it is canceled.")
  108. XCTAssert(completionBlockIsCalled == false, "CompletionBlock should not be called since it is canceled.")
  109. }
  110. waitForExpectationsWithTimeout(5, handler: nil)
  111. }
  112. func testImageDownloadCancelForImageViewAfterRequestStarted() {
  113. let expectation = expectationWithDescription("wait for downloading image")
  114. let URLString = testKeys[0]
  115. let stub = stubRequest("GET", URLString).andReturn(200).withBody(testImageData).delay()
  116. let URL = NSURL(string: URLString)!
  117. var progressBlockIsCalled = false
  118. var completionBlockIsCalled = false
  119. let task = imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
  120. progressBlockIsCalled = true
  121. }) { (image, error, cacheType, imageURL) -> () in
  122. completionBlockIsCalled = true
  123. }
  124. dispatch_async(dispatch_get_main_queue()) { () -> Void in
  125. task.cancel()
  126. stub.go()
  127. }
  128. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(Double(NSEC_PER_SEC) * 0.09)), dispatch_get_main_queue()) { () -> Void in
  129. expectation.fulfill()
  130. XCTAssert(progressBlockIsCalled == false, "ProgressBlock should not be called since it is canceled.")
  131. XCTAssert(completionBlockIsCalled == false, "CompletionBlock should not be called since it is canceled.")
  132. }
  133. waitForExpectationsWithTimeout(5, handler: nil)
  134. }
  135. func testImageDownloadCancelPartialTask() {
  136. let expectation = expectationWithDescription("wait for downloading image")
  137. let URLString = testKeys[0]
  138. stubRequest("GET", URLString).andReturn(200).withBody(testImageData)
  139. let URL = NSURL(string: URLString)!
  140. var task1Completion = false
  141. var task2Completion = false
  142. var task3Completion = false
  143. let task1 = imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
  144. }) { (image, error, cacheType, imageURL) -> () in
  145. task1Completion = true
  146. }
  147. let _ = imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
  148. }) { (image, error, cacheType, imageURL) -> () in
  149. task2Completion = true
  150. }
  151. let _ = imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
  152. }) { (image, error, cacheType, imageURL) -> () in
  153. task3Completion = true
  154. }
  155. task1.cancel()
  156. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(Double(NSEC_PER_SEC) * 0.09)), dispatch_get_main_queue()) { () -> Void in
  157. expectation.fulfill()
  158. XCTAssert(task1Completion == false, "Task 1 is canceled. The completion flag should be fasle.")
  159. XCTAssert(task2Completion == true, "Task 2 should be completed.")
  160. XCTAssert(task3Completion == true, "Task 3 should be completed.")
  161. }
  162. waitForExpectationsWithTimeout(5, handler: nil)
  163. }
  164. func testImageDownloadCancelPartialTaskAfterRequestStarted() {
  165. let expectation = expectationWithDescription("wait for downloading image")
  166. let URLString = testKeys[0]
  167. let stub = stubRequest("GET", URLString).andReturn(200).withBody(testImageData).delay()
  168. let URL = NSURL(string: URLString)!
  169. var task1Completion = false
  170. var task2Completion = false
  171. var task3Completion = false
  172. let task1 = imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
  173. }) { (image, error, cacheType, imageURL) -> () in
  174. task1Completion = true
  175. }
  176. let task2 = imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
  177. }) { (image, error, cacheType, imageURL) -> () in
  178. task2Completion = true
  179. }
  180. let task3 = imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
  181. }) { (image, error, cacheType, imageURL) -> () in
  182. task3Completion = true
  183. }
  184. // Prevent unused warning.
  185. print(task2)
  186. print(task3)
  187. dispatch_async(dispatch_get_main_queue()) { () -> Void in
  188. task1.cancel()
  189. stub.go()
  190. }
  191. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(Double(NSEC_PER_SEC) * 0.09)), dispatch_get_main_queue()) { () -> Void in
  192. expectation.fulfill()
  193. XCTAssert(task1Completion == false, "Task 1 is canceled. The completion flag should be fasle.")
  194. XCTAssert(task2Completion == true, "Task 2 should be completed.")
  195. XCTAssert(task3Completion == true, "Task 3 should be completed.")
  196. }
  197. waitForExpectationsWithTimeout(5, handler: nil)
  198. }
  199. func testImageDownalodMultipleCaches() {
  200. let cache1 = ImageCache(name: "cache1")
  201. let cache2 = ImageCache(name: "cache2")
  202. let expectation = expectationWithDescription("wait for downloading image")
  203. let URLString = testKeys[0]
  204. stubRequest("GET", URLString).andReturn(200).withBody(testImageData)
  205. let URL = NSURL(string: URLString)!
  206. imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: [.TargetCache(cache1)], progressBlock: { (receivedSize, totalSize) -> () in
  207. }) { (image, error, cacheType, imageURL) -> () in
  208. XCTAssertTrue(cache1.isImageCachedForKey(URLString).cached, "This image should be cached in cache1.")
  209. XCTAssertFalse(cache2.isImageCachedForKey(URLString).cached, "This image should not be cached in cache2.")
  210. XCTAssertFalse(KingfisherManager.sharedManager.cache.isImageCachedForKey(URLString).cached, "This image should not be cached in default cache.")
  211. self.imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: [.TargetCache(cache2)], progressBlock: { (receivedSize, totalSize) -> () in
  212. }, completionHandler: { (image, error, cacheType, imageURL) -> () in
  213. XCTAssertTrue(cache1.isImageCachedForKey(URLString).cached, "This image should be cached in cache1.")
  214. XCTAssertTrue(cache2.isImageCachedForKey(URLString).cached, "This image should be cached in cache2.")
  215. XCTAssertFalse(KingfisherManager.sharedManager.cache.isImageCachedForKey(URLString).cached, "This image should not be cached in default cache.")
  216. clearCaches([cache1, cache2])
  217. expectation.fulfill()
  218. })
  219. }
  220. waitForExpectationsWithTimeout(5, handler: { (error) -> Void in
  221. clearCaches([cache1, cache2])
  222. })
  223. }
  224. func testIndicatorViewExisting() {
  225. imageView.kf_showIndicatorWhenLoading = true
  226. XCTAssertNotNil(imageView.kf_indicator, "The indicator view should exist when showIndicatorWhenLoading is true")
  227. imageView.kf_showIndicatorWhenLoading = false
  228. XCTAssertNil(imageView.kf_indicator, "The indicator view should be removed when showIndicatorWhenLoading set to false")
  229. }
  230. func testIndicatorViewAnimating() {
  231. imageView.kf_showIndicatorWhenLoading = true
  232. let expectation = expectationWithDescription("wait for downloading image")
  233. let URLString = testKeys[0]
  234. stubRequest("GET", URLString).andReturn(200).withBody(testImageData)
  235. let URL = NSURL(string: URLString)!
  236. imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
  237. let indicator = self.imageView.kf_indicator
  238. XCTAssertNotNil(indicator, "The indicator view should exist when showIndicatorWhenLoading is true")
  239. XCTAssertTrue(indicator!.isAnimating(), "The indicator should be animating when loading")
  240. }) { (image, error, cacheType, imageURL) -> () in
  241. let indicator = self.imageView.kf_indicator
  242. XCTAssertFalse(indicator!.isAnimating(), "The indicator should stop after loading")
  243. expectation.fulfill()
  244. }
  245. waitForExpectationsWithTimeout(5, handler: nil)
  246. }
  247. }