UIButtonExtensionTests.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // UIButtonExtensionTests.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. #if canImport(UIKit)
  27. import UIKit
  28. import XCTest
  29. @testable import Kingfisher
  30. class UIButtonExtensionTests: XCTestCase {
  31. var button: UIButton!
  32. override class func setUp() {
  33. super.setUp()
  34. LSNocilla.sharedInstance().start()
  35. }
  36. override class func tearDown() {
  37. LSNocilla.sharedInstance().stop()
  38. super.tearDown()
  39. }
  40. override func setUp() {
  41. super.setUp()
  42. button = UIButton()
  43. KingfisherManager.shared.downloader = ImageDownloader(name: "testDownloader")
  44. KingfisherManager.shared.defaultOptions = [.waitForCache]
  45. cleanDefaultCache()
  46. }
  47. override func tearDown() {
  48. LSNocilla.sharedInstance().clearStubs()
  49. button = nil
  50. cleanDefaultCache()
  51. KingfisherManager.shared.defaultOptions = .empty
  52. super.tearDown()
  53. }
  54. func testDownloadAndSetImage() {
  55. let exp = expectation(description: #function)
  56. let url = testURLs[0]
  57. stub(url, data: testImageData, length: 123)
  58. var progressBlockIsCalled = false
  59. button.kf.setImage(with: url, for: .normal, progressBlock: { _, _ in
  60. progressBlockIsCalled = true
  61. })
  62. {
  63. result in
  64. XCTAssertTrue(progressBlockIsCalled)
  65. let image = result.value?.image
  66. XCTAssertNotNil(image)
  67. XCTAssertTrue(image!.renderEqual(to: testImage))
  68. XCTAssertTrue(self.button.image(for: .normal)!.renderEqual(to: testImage))
  69. //XCTAssertEqual(self.button.kf.taskIdentifier(for: .normal), Source.Identifier.current)
  70. XCTAssertEqual(result.value!.cacheType, .none)
  71. exp.fulfill()
  72. }
  73. waitForExpectations(timeout: 3, handler: nil)
  74. }
  75. func testDownloadAndSetBackgroundImage() {
  76. let exp = expectation(description: #function)
  77. let url = testURLs[0]
  78. stub(url, data: testImageData, length: 123)
  79. var progressBlockIsCalled = false
  80. button.kf.setBackgroundImage(with: url, for: .normal, progressBlock: { _, _ in
  81. progressBlockIsCalled = true
  82. })
  83. {
  84. result in
  85. XCTAssertTrue(progressBlockIsCalled)
  86. let image = result.value?.image
  87. XCTAssertNotNil(image)
  88. XCTAssertTrue(image!.renderEqual(to: testImage))
  89. XCTAssertTrue(self.button.backgroundImage(for: .normal)!.renderEqual(to: testImage))
  90. XCTAssertEqual(result.value!.cacheType, .none)
  91. exp.fulfill()
  92. }
  93. waitForExpectations(timeout: 3, handler: nil)
  94. }
  95. func testCacnelImageTask() {
  96. let exp = expectation(description: #function)
  97. let url = testURLs[0]
  98. let stub = delayedStub(url, data: testImageData)
  99. button.kf.setImage(with: url, for: .highlighted) { result in
  100. XCTAssertNotNil(result.error)
  101. XCTAssertTrue(result.error!.isTaskCancelled)
  102. delay(0.1) { exp.fulfill() }
  103. }
  104. self.button.kf.cancelImageDownloadTask()
  105. _ = stub.go()
  106. waitForExpectations(timeout: 3, handler: nil)
  107. }
  108. func testCacnelBackgroundImageTask() {
  109. let exp = expectation(description: #function)
  110. let url = testURLs[0]
  111. let stub = delayedStub(url, data: testImageData)
  112. button.kf.setBackgroundImage(with: url, for: .highlighted) { result in
  113. XCTAssertNotNil(result.error)
  114. XCTAssertTrue(result.error!.isTaskCancelled)
  115. delay(0.1) { exp.fulfill() }
  116. }
  117. self.button.kf.cancelBackgroundImageDownloadTask()
  118. _ = stub.go()
  119. waitForExpectations(timeout: 3, handler: nil)
  120. }
  121. func testSettingNilURL() {
  122. let exp = expectation(description: #function)
  123. let url: URL? = nil
  124. button.kf.setBackgroundImage(with: url, for: .normal) { result in
  125. XCTAssertNil(result.value)
  126. XCTAssertNotNil(result.error)
  127. guard case .imageSettingError(reason: .emptySource) = result.error! else {
  128. XCTFail()
  129. return
  130. }
  131. exp.fulfill()
  132. }
  133. waitForExpectations(timeout: 3, handler: nil)
  134. }
  135. func testSettingNonWorkingImageWithFailureImage() {
  136. let expectation = self.expectation(description: "wait for downloading image")
  137. let url = testURLs[0]
  138. stub(url, errorCode: 404)
  139. let state = UIControl.State()
  140. button.kf.setImage(with: url, for: state, options: [.onFailureImage(testImage)]) { (result) -> Void in
  141. XCTAssertNil(result.value)
  142. expectation.fulfill()
  143. }
  144. XCTAssertNil(button.image(for: state))
  145. waitForExpectations(timeout: 5, handler: nil)
  146. XCTAssertEqual(testImage, button.image(for: state))
  147. }
  148. func testSettingNonWorkingBackgroundImageWithFailureImage() {
  149. let expectation = self.expectation(description: "wait for downloading image")
  150. let url = testURLs[0]
  151. stub(url, errorCode: 404)
  152. let state = UIControl.State()
  153. button.kf.setBackgroundImage(with: url, for: state, options: [.onFailureImage(testImage)]) { (result) -> Void in
  154. XCTAssertNil(result.value)
  155. expectation.fulfill()
  156. }
  157. XCTAssertNil(button.backgroundImage(for: state))
  158. waitForExpectations(timeout: 5, handler: nil)
  159. XCTAssertEqual(testImage, button.backgroundImage(for: state))
  160. }
  161. }
  162. #endif