NSButtonExtensionTests.swift 6.7 KB

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