NSButtonExtensionTests.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. func testDownloadAndSetImage() {
  56. let exp = expectation(description: #function)
  57. let url = testURLs[0]
  58. stub(url, data: testImageData, length: 123)
  59. var progressBlockIsCalled = false
  60. button.kf.setImage(with: url, progressBlock: { _, _ in progressBlockIsCalled = true }) {
  61. result in
  62. XCTAssertTrue(progressBlockIsCalled)
  63. let image = result.value?.image
  64. XCTAssertNotNil(image)
  65. XCTAssertTrue(image!.renderEqual(to: testImage))
  66. XCTAssertTrue(self.button.image!.renderEqual(to: testImage))
  67. XCTAssertEqual(result.value!.cacheType, .none)
  68. exp.fulfill()
  69. }
  70. waitForExpectations(timeout: 3, handler: nil)
  71. }
  72. func testDownloadAndSetAlternateImage() {
  73. let exp = expectation(description: #function)
  74. let url = testURLs[0]
  75. stub(url, data: testImageData, length: 123)
  76. var progressBlockIsCalled = false
  77. button.kf.setAlternateImage(with: url, progressBlock: { _, _ in progressBlockIsCalled = true }) {
  78. result in
  79. XCTAssertTrue(progressBlockIsCalled)
  80. let image = result.value?.image
  81. XCTAssertNotNil(image)
  82. XCTAssertTrue(image!.renderEqual(to: testImage))
  83. XCTAssertTrue(self.button.alternateImage!.renderEqual(to: testImage))
  84. XCTAssertEqual(result.value!.cacheType, .none)
  85. exp.fulfill()
  86. }
  87. waitForExpectations(timeout: 5, handler: nil)
  88. }
  89. func testCacnelImageTask() {
  90. let exp = expectation(description: #function)
  91. let url = testURLs[0]
  92. let stub = delayedStub(url, data: testImageData)
  93. button.kf.setImage(with: url, completionHandler: { result in
  94. XCTAssertNotNil(result.error)
  95. XCTAssertTrue(result.error!.isTaskCancelled)
  96. delay(0.1) { exp.fulfill() }
  97. })
  98. self.button.kf.cancelImageDownloadTask()
  99. _ = stub.go()
  100. waitForExpectations(timeout: 3, handler: nil)
  101. }
  102. func testCacnelAlternateImageTask() {
  103. let exp = expectation(description: #function)
  104. let url = testURLs[0]
  105. let stub = delayedStub(url, data: testImageData)
  106. button.kf.setAlternateImage(with: url, completionHandler: { result in
  107. XCTAssertNotNil(result.error)
  108. XCTAssertTrue(result.error!.isTaskCancelled)
  109. delay(0.1) { exp.fulfill() }
  110. })
  111. self.button.kf.cancelAlternateImageDownloadTask()
  112. _ = stub.go()
  113. waitForExpectations(timeout: 3, handler: nil)
  114. }
  115. func testSettingNilURL() {
  116. let exp = expectation(description: #function)
  117. let url: URL? = nil
  118. button.kf.setAlternateImage(with: url, progressBlock: { _, _ in XCTFail() }) {
  119. result in
  120. XCTAssertNil(result.value)
  121. XCTAssertNotNil(result.error)
  122. guard case .imageSettingError(reason: .emptySource) = result.error! else {
  123. XCTFail()
  124. fatalError()
  125. }
  126. exp.fulfill()
  127. }
  128. waitForExpectations(timeout: 3, handler: nil)
  129. }
  130. func testSettingNonWorkingImageWithFailureImage() {
  131. let expectation = self.expectation(description: "wait for downloading image")
  132. let url = testURLs[0]
  133. stub(url, errorCode: 404)
  134. button.kf.setImage(with: url, options: [.onFailureImage(testImage)], completionHandler: { result in
  135. XCTAssertNil(result.value)
  136. expectation.fulfill()
  137. })
  138. XCTAssertNil(button.image)
  139. waitForExpectations(timeout: 5, handler: nil)
  140. XCTAssertEqual(testImage, button.image)
  141. }
  142. func testSettingNonWorkingAlternateImageWithFailureImage() {
  143. let expectation = self.expectation(description: "wait for downloading image")
  144. let url = testURLs[0]
  145. stub(url, errorCode: 404)
  146. button.kf.setAlternateImage(with: url, options: [.onFailureImage(testImage)], completionHandler: { result in
  147. XCTAssertNil(result.value)
  148. expectation.fulfill()
  149. })
  150. XCTAssertNil(button.alternateImage)
  151. waitForExpectations(timeout: 5, handler: nil)
  152. XCTAssertEqual(testImage, button.alternateImage)
  153. }
  154. }
  155. #endif