NSButtonExtensionTests.swift 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 AppKit
  27. import XCTest
  28. @testable import Kingfisher
  29. class NSButtonExtensionTests: XCTestCase {
  30. var button: NSButton!
  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 = NSButton()
  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, 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! == testImage, "Downloaded image should be already set to the image for state")
  68. XCTAssert(self.button.kf.webURL == 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 testDownloadAndSetAlternateImage() {
  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 = URL(string: URLString)!
  78. var progressBlockIsCalled = false
  79. button.kf.setAlternateImage(with: url, 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.alternateImage! == testImage, "Downloaded image should be already set to the image for state")
  87. XCTAssert(self.button.kf.alternateWebURL == 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, placeholder: nil, options: nil, progressBlock: { (receivedSize, totalSize) -> () in
  98. }) { (image, error, cacheType, imageURL) -> () in
  99. XCTAssertNotNil(error)
  100. XCTAssertEqual(error?.code, NSURLErrorCancelled)
  101. // delay(0.1, block: expectation.fulfill)
  102. expectation.fulfill()
  103. }
  104. delay(0.1) {
  105. self.button.kf.cancelImageDownloadTask()
  106. _ = stub!.go()
  107. }
  108. waitForExpectations(timeout: 5, handler: nil)
  109. }
  110. func testCacnelAlternateImageTask() {
  111. let expectation = self.expectation(description: "wait for downloading image")
  112. let URLString = testKeys[0]
  113. let stub = stubRequest("GET", URLString).andReturn(200)?.withBody(testImageData)?.delay()
  114. let url = URL(string: URLString)!
  115. _ = button.kf.setAlternateImage(with: url, placeholder: nil, options: nil, progressBlock: { (receivedSize, totalSize) -> () in
  116. XCTFail("Progress block should not be called.")
  117. }) { (image, error, cacheType, imageURL) -> () in
  118. XCTAssertNotNil(error)
  119. XCTAssertEqual(error?.code, NSURLErrorCancelled)
  120. // delay(0.1, block: expectation.fulfill)
  121. expectation.fulfill()
  122. }
  123. delay(0.1) {
  124. self.button.kf.cancelAlternateImageDownloadTask()
  125. _ = stub!.go()
  126. }
  127. waitForExpectations(timeout: 5, handler: nil)
  128. }
  129. func testSettingNilURL() {
  130. let expectation = self.expectation(description: "wait for downloading image")
  131. let url: URL? = nil
  132. button.kf.setAlternateImage(with: url, placeholder: nil, options: nil, progressBlock: { (receivedSize, totalSize) -> () in
  133. XCTFail("Progress block should not be called.")
  134. }) { (image, error, cacheType, imageURL) -> () in
  135. XCTAssertNil(image)
  136. XCTAssertNil(error)
  137. XCTAssertEqual(cacheType, CacheType.none)
  138. XCTAssertNil(imageURL)
  139. expectation.fulfill()
  140. }
  141. waitForExpectations(timeout: 5, handler: nil)
  142. }
  143. }