UIButtonExtensionTests.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // UIButtonExtensionTests.swift
  3. // Kingfisher
  4. //
  5. // Created by Wei Wang on 15/4/17.
  6. //
  7. // Copyright (c) 2015 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 UIKit
  27. import XCTest
  28. @testable import Kingfisher
  29. class UIButtonExtensionTests: XCTestCase {
  30. var button: UIButton!
  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 = UIButton()
  43. KingfisherManager.sharedManager.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 = expectationWithDescription("wait for downloading image")
  55. let URLString = testKeys[0]
  56. stubRequest("GET", URLString).andReturn(200).withBody(testImageData)
  57. let URL = NSURL(string: URLString)!
  58. var progressBlockIsCalled = false
  59. button.kf_setImageWithURL(URL, forState: UIControlState.Highlighted, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
  60. progressBlockIsCalled = true
  61. }) { (image, error, cacheType, imageURL) -> () in
  62. expectation.fulfill()
  63. XCTAssert(progressBlockIsCalled, "progressBlock should be called at least once.")
  64. XCTAssert(image != nil, "Downloaded image should exist.")
  65. XCTAssert(image! == testImage, "Downloaded image should be the same as test image.")
  66. XCTAssert(self.button.imageForState(UIControlState.Highlighted)! == testImage, "Downloaded image should be already set to the image for state")
  67. XCTAssert(self.button.kf_webURLForState(UIControlState.Highlighted) == imageURL, "Web URL should equal to the downloaded url.")
  68. XCTAssert(cacheType == .None, "cacheType should be .None since the image was just downloaded.")
  69. }
  70. waitForExpectationsWithTimeout(5, handler: nil)
  71. }
  72. func testDownloadAndSetBackgroundImage() {
  73. let expectation = expectationWithDescription("wait for downloading image")
  74. let URLString = testKeys[0]
  75. stubRequest("GET", URLString).andReturn(200).withBody(testImageData)
  76. let URL = NSURL(string: URLString)!
  77. var progressBlockIsCalled = false
  78. button.kf_setBackgroundImageWithURL(URL, forState: UIControlState.Normal, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
  79. progressBlockIsCalled = true
  80. }) { (image, error, cacheType, imageURL) -> () in
  81. expectation.fulfill()
  82. XCTAssert(progressBlockIsCalled, "progressBlock should be called at least once.")
  83. XCTAssert(image != nil, "Downloaded image should exist.")
  84. XCTAssert(image! == testImage, "Downloaded image should be the same as test image.")
  85. XCTAssert(self.button.backgroundImageForState(UIControlState.Normal)! == testImage, "Downloaded image should be already set to the image for state")
  86. XCTAssert(self.button.kf_backgroundWebURLForState(UIControlState.Normal) == imageURL, "Web URL should equal to the downloaded url.")
  87. XCTAssert(cacheType == .None, "cacheType should be .None since the image was just downloaded.")
  88. }
  89. waitForExpectationsWithTimeout(5, handler: nil)
  90. }
  91. }