UIButtonExtensionTests.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // UIButtonExtensionTests.swift
  3. // Kingfisher
  4. //
  5. // Created by Wei Wang on 15/4/17.
  6. //
  7. // Copyright (c) 2017 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 UIButtonExtensionTests: XCTestCase {
  30. var button: UIButton!
  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. button = UIButton()
  43. KingfisherManager.shared.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. button = nil
  50. cleanDefaultCache()
  51. super.tearDown()
  52. }
  53. func testDownloadAndSetImage() {
  54. let expectation = self.expectation(description: "wait for downloading image")
  55. let URLString = testKeys[0]
  56. _ = stubRequest("GET", URLString).andReturn(200)?.withBody(testImageData)
  57. let url = URL(string: URLString)!
  58. var progressBlockIsCalled = false
  59. cleanDefaultCache()
  60. button.kf.setImage(with: url, for: .highlighted, placeholder: nil, options: nil, progressBlock: { (receivedSize, totalSize) -> () in
  61. progressBlockIsCalled = true
  62. }) { (image, error, cacheType, imageURL) -> () in
  63. expectation.fulfill()
  64. XCTAssert(progressBlockIsCalled, "progressBlock should be called at least once.")
  65. XCTAssert(image != nil, "Downloaded image should exist.")
  66. XCTAssert(image! == testImage, "Downloaded image should be the same as test image.")
  67. XCTAssert(self.button.image(for: UIControlState.highlighted)! == testImage, "Downloaded image should be already set to the image for state")
  68. XCTAssert(self.button.kf.webURL(for: .highlighted) == imageURL, "Web URL should equal to the downloaded url.")
  69. XCTAssert(cacheType == .none, "The cache type should be none here. This image was just downloaded. But now is: \(cacheType)")
  70. }
  71. waitForExpectations(timeout: 5, handler: nil)
  72. }
  73. func testDownloadAndSetBackgroundImage() {
  74. let expectation = self.expectation(description: "wait for downloading image")
  75. let URLString = testKeys[0]
  76. _ = stubRequest("GET", URLString).andReturn(200)?.withBody(testImageData)
  77. let url = Foundation.URL(string: URLString)!
  78. var progressBlockIsCalled = false
  79. button.kf.setBackgroundImage(with: url, for: .normal, placeholder: nil, options: 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.button.backgroundImage(for: .normal)! == testImage, "Downloaded image should be already set to the image for state")
  87. XCTAssert(self.button.kf.backgroundWebURL(for: .normal) == imageURL, "Web URL should equal to the downloaded url.")
  88. XCTAssert(cacheType == .none, "cacheType should be .None since the image was just downloaded.")
  89. }
  90. waitForExpectations(timeout: 5, handler: nil)
  91. }
  92. func testCacnelImageTask() {
  93. let expectation = self.expectation(description: "wait for downloading image")
  94. let URLString = testKeys[0]
  95. let stub = stubRequest("GET", URLString).andReturn(200)?.withBody(testImageData)?.delay()
  96. let url = URL(string: URLString)!
  97. button.kf.setImage(with: url, for: UIControlState.highlighted, placeholder: nil, options: nil, progressBlock: { (receivedSize, totalSize) -> () in
  98. }) { (image, error, cacheType, imageURL) -> () in
  99. XCTAssertNotNil(error)
  100. XCTAssertEqual(error?.code, NSURLErrorCancelled)
  101. expectation.fulfill()
  102. }
  103. delay(0.1) {
  104. self.button.kf.cancelImageDownloadTask()
  105. _ = stub!.go()
  106. }
  107. waitForExpectations(timeout: 5, handler: nil)
  108. }
  109. func testCacnelBackgroundImageTask() {
  110. let expectation = self.expectation(description: "wait for downloading image")
  111. let URLString = testKeys[0]
  112. let stub = stubRequest("GET", URLString).andReturn(200)?.withBody(testImageData)?.delay()
  113. let url = URL(string: URLString)!
  114. button.kf.setBackgroundImage(with: url, for: UIControlState(), placeholder: nil, options: nil, progressBlock: { (receivedSize, totalSize) -> () in
  115. XCTFail("Progress block should not be called.")
  116. }) { (image, error, cacheType, imageURL) -> () in
  117. XCTAssertNotNil(error)
  118. XCTAssertEqual(error?.code, NSURLErrorCancelled)
  119. expectation.fulfill()
  120. }
  121. delay(0.1) {
  122. self.button.kf.cancelBackgroundImageDownloadTask()
  123. _ = stub!.go()
  124. }
  125. waitForExpectations(timeout: 5, handler: nil)
  126. }
  127. func testSettingNilURL() {
  128. let expectation = self.expectation(description: "wait for downloading image")
  129. let url: URL? = nil
  130. button.kf.setBackgroundImage(with: url, for: UIControlState(), placeholder: nil, options: nil, progressBlock: { (receivedSize, totalSize) -> () in
  131. XCTFail("Progress block should not be called.")
  132. }) { (image, error, cacheType, imageURL) -> () in
  133. XCTAssertNil(image)
  134. XCTAssertNil(error)
  135. XCTAssertEqual(cacheType, CacheType.none)
  136. XCTAssertNil(imageURL)
  137. expectation.fulfill()
  138. }
  139. waitForExpectations(timeout: 5, handler: nil)
  140. }
  141. }