NSButtonExtensionTests.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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(AppKit) && !targetEnvironment(macCatalyst)
  27. import AppKit
  28. import XCTest
  29. @testable import Kingfisher
  30. class NSButtonExtensionTests: XCTestCase {
  31. var button: NSButton!
  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 = NSButton()
  43. KingfisherManager.shared.downloader = ImageDownloader(name: "testDownloader")
  44. KingfisherManager.shared.defaultOptions = [.waitForCache]
  45. cleanDefaultCache()
  46. }
  47. override func tearDown() {
  48. // Put teardown code here. This method is called after the invocation of each test method in the class.
  49. LSNocilla.sharedInstance().clearStubs()
  50. button = nil
  51. cleanDefaultCache()
  52. KingfisherManager.shared.defaultOptions = .empty
  53. super.tearDown()
  54. }
  55. @MainActor
  56. func testDownloadAndSetImage() {
  57. let exp = expectation(description: #function)
  58. let url = testURLs[0]
  59. stub(url, data: testImageData, length: 123)
  60. var progressBlockIsCalled = false
  61. button.kf.setImage(with: url, progressBlock: { _, _ in progressBlockIsCalled = true }) {
  62. result in
  63. XCTAssertTrue(progressBlockIsCalled)
  64. let image = result.value?.image
  65. XCTAssertNotNil(image)
  66. XCTAssertTrue(image!.renderEqual(to: testImage))
  67. XCTAssertTrue(self.button.image!.renderEqual(to: testImage))
  68. XCTAssertEqual(result.value!.cacheType, .none)
  69. exp.fulfill()
  70. }
  71. waitForExpectations(timeout: 3, handler: nil)
  72. }
  73. @MainActor
  74. func testDownloadAndSetAlternateImage() {
  75. let exp = expectation(description: #function)
  76. let url = testURLs[0]
  77. stub(url, data: testImageData, length: 123)
  78. var progressBlockIsCalled = false
  79. button.kf.setAlternateImage(with: url, progressBlock: { _, _ in progressBlockIsCalled = true }) {
  80. result in
  81. XCTAssertTrue(progressBlockIsCalled)
  82. let image = result.value?.image
  83. XCTAssertNotNil(image)
  84. XCTAssertTrue(image!.renderEqual(to: testImage))
  85. XCTAssertTrue(self.button.alternateImage!.renderEqual(to: testImage))
  86. XCTAssertEqual(result.value!.cacheType, .none)
  87. exp.fulfill()
  88. }
  89. waitForExpectations(timeout: 5, handler: nil)
  90. }
  91. @MainActor
  92. func testCancelImageTask() {
  93. let exp = expectation(description: #function)
  94. let url = testURLs[0]
  95. let stub = delayedStub(url, data: testImageData)
  96. button.kf.setImage(with: url, completionHandler: { result in
  97. XCTAssertNotNil(result.error)
  98. XCTAssertTrue(result.error!.isTaskCancelled)
  99. delay(0.1) { exp.fulfill() }
  100. })
  101. self.button.kf.cancelImageDownloadTask()
  102. _ = stub.go()
  103. waitForExpectations(timeout: 3, handler: nil)
  104. }
  105. @MainActor
  106. func testCancelAlternateImageTask() {
  107. let exp = expectation(description: #function)
  108. let url = testURLs[0]
  109. let stub = delayedStub(url, data: testImageData)
  110. button.kf.setAlternateImage(with: url, completionHandler: { result in
  111. XCTAssertNotNil(result.error)
  112. XCTAssertTrue(result.error!.isTaskCancelled)
  113. delay(0.1) { exp.fulfill() }
  114. })
  115. self.button.kf.cancelAlternateImageDownloadTask()
  116. _ = stub.go()
  117. waitForExpectations(timeout: 3, handler: nil)
  118. }
  119. @MainActor
  120. func testSettingNilURL() {
  121. let exp = expectation(description: #function)
  122. let url: URL? = nil
  123. button.kf.setAlternateImage(with: url, progressBlock: { _, _ in XCTFail() }) {
  124. result in
  125. XCTAssertNil(result.value)
  126. XCTAssertNotNil(result.error)
  127. guard case .imageSettingError(reason: .emptySource) = result.error! else {
  128. XCTFail()
  129. fatalError()
  130. }
  131. exp.fulfill()
  132. }
  133. waitForExpectations(timeout: 3, handler: nil)
  134. }
  135. @MainActor
  136. func testSettingNonWorkingImageWithFailureImage() {
  137. let expectation = self.expectation(description: "wait for downloading image")
  138. let url = testURLs[0]
  139. stub(url, errorCode: 404)
  140. button.kf.setImage(with: url, options: [.onFailureImage(testImage)], completionHandler: { result in
  141. XCTAssertNil(result.value)
  142. expectation.fulfill()
  143. })
  144. XCTAssertNil(button.image)
  145. waitForExpectations(timeout: 5, handler: nil)
  146. XCTAssertEqual(testImage, button.image)
  147. }
  148. @MainActor
  149. func testSettingNonWorkingAlternateImageWithFailureImage() {
  150. let expectation = self.expectation(description: "wait for downloading image")
  151. let url = testURLs[0]
  152. stub(url, errorCode: 404)
  153. button.kf.setAlternateImage(with: url, options: [.onFailureImage(testImage)], completionHandler: { result in
  154. XCTAssertNil(result.value)
  155. expectation.fulfill()
  156. })
  157. XCTAssertNil(button.alternateImage)
  158. waitForExpectations(timeout: 5, handler: nil)
  159. XCTAssertEqual(testImage, button.alternateImage)
  160. }
  161. }
  162. #endif