浏览代码

Add UIButton extension tests.

onevcat 10 年之前
父节点
当前提交
2858c93599

+ 4 - 0
Kingfisher.xcodeproj/project.pbxproj

@@ -9,6 +9,7 @@
 /* Begin PBXBuildFile section */
 		0D9C68098E20AB4F19D7C313 /* libPods-KingfisherTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = A9E621E297FEFAD35D39C34E /* libPods-KingfisherTests.a */; };
 		4B6D4F651AE0A46D0084D15B /* UIImageViewExtensionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B6D4F641AE0A46D0084D15B /* UIImageViewExtensionTests.swift */; };
+		4B6D4F671AE0B82A0084D15B /* UIButtonExtensionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B6D4F661AE0B82A0084D15B /* UIButtonExtensionTests.swift */; };
 		4B82DDFC1AD7701900074995 /* ImageCacheTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B82DDFB1AD7701900074995 /* ImageCacheTests.swift */; };
 		4B945B211ADBB20200D1545E /* UIButton+Kingfisher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B945B201ADBB20200D1545E /* UIButton+Kingfisher.swift */; };
 		4BAFBA3D1AD671E400FB0300 /* CollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BAFBA3C1AD671E400FB0300 /* CollectionViewCell.swift */; };
@@ -72,6 +73,7 @@
 
 /* Begin PBXFileReference section */
 		4B6D4F641AE0A46D0084D15B /* UIImageViewExtensionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIImageViewExtensionTests.swift; sourceTree = "<group>"; };
+		4B6D4F661AE0B82A0084D15B /* UIButtonExtensionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIButtonExtensionTests.swift; sourceTree = "<group>"; };
 		4B82DDFB1AD7701900074995 /* ImageCacheTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageCacheTests.swift; sourceTree = "<group>"; };
 		4B945B201ADBB20200D1545E /* UIButton+Kingfisher.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIButton+Kingfisher.swift"; sourceTree = "<group>"; };
 		4BAFBA3C1AD671E400FB0300 /* CollectionViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CollectionViewCell.swift; sourceTree = "<group>"; };
@@ -214,6 +216,7 @@
 				4BBA04C11AD795C500A5CF82 /* ImageDownloaderTests.swift */,
 				4B82DDFB1AD7701900074995 /* ImageCacheTests.swift */,
 				4B6D4F641AE0A46D0084D15B /* UIImageViewExtensionTests.swift */,
+				4B6D4F661AE0B82A0084D15B /* UIButtonExtensionTests.swift */,
 				D1ED2D461AD2D09F00CFC3EB /* Supporting Files */,
 				4BBA04BD1AD77F7E00A5CF82 /* KingfisherTests-Bridging-Header.h */,
 				4BBA04C31AD7986100A5CF82 /* KingfisherTestHelper.swift */,
@@ -444,6 +447,7 @@
 				4BBA04C21AD795C500A5CF82 /* ImageDownloaderTests.swift in Sources */,
 				4B6D4F651AE0A46D0084D15B /* UIImageViewExtensionTests.swift in Sources */,
 				4BBA04C41AD7986100A5CF82 /* KingfisherTestHelper.swift in Sources */,
+				4B6D4F671AE0B82A0084D15B /* UIButtonExtensionTests.swift in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

文件差异内容过多而无法显示
+ 1 - 0
KingfisherTests/KingfisherTestHelper.swift


+ 103 - 0
KingfisherTests/UIButtonExtensionTests.swift

@@ -0,0 +1,103 @@
+//
+//  UIButtonExtensionTests.swift
+//  Kingfisher
+//
+//  Created by Wei Wang on 15/4/17.
+//
+//  Copyright (c) 2015 Wei Wang <onevcat@gmail.com>
+//
+//  Permission is hereby granted, free of charge, to any person obtaining a copy
+//  of this software and associated documentation files (the "Software"), to deal
+//  in the Software without restriction, including without limitation the rights
+//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+//  copies of the Software, and to permit persons to whom the Software is
+//  furnished to do so, subject to the following conditions:
+//
+//  The above copyright notice and this permission notice shall be included in
+//  all copies or substantial portions of the Software.
+//
+//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+//  THE SOFTWARE.
+
+import UIKit
+import XCTest
+import Kingfisher
+
+class UIButtonExtensionTests: XCTestCase {
+
+    var button: UIButton!
+    
+    override class func setUp() {
+        super.setUp()
+        LSNocilla.sharedInstance().start()
+    }
+    
+    override class func tearDown() {
+        super.tearDown()
+        LSNocilla.sharedInstance().stop()
+    }
+    
+    override func setUp() {
+        super.setUp()
+        // Put setup code here. This method is called before the invocation of each test method in the class.
+        button = UIButton()
+    }
+    
+    override func tearDown() {
+        // Put teardown code here. This method is called after the invocation of each test method in the class.
+        LSNocilla.sharedInstance().clearStubs()
+        button = nil
+        cleanDefaultCache()
+        
+        super.tearDown()
+    }
+
+    func testDownloadAndSetImage() {
+        let expectation = expectationWithDescription("wait for downloading image")
+        
+        let URLString = testKeys[0]
+        stubRequest("GET", URLString).andReturn(200).withBody(testImageData)
+        let URL = NSURL(string: URLString)!
+        
+        var progressBlockIsCalled = false
+        button.kf_setImageWithURL(URL, forState: UIControlState.Highlighted, placeholderImage: nil, options: KingfisherOptions.None, progressBlock: { (receivedSize, totalSize) -> () in
+            progressBlockIsCalled = true
+        }) { (image, error, imageURL) -> () in
+            expectation.fulfill()
+            
+            XCTAssert(progressBlockIsCalled, "progressBlock should be called at least once.")
+            XCTAssert(image != nil, "Downloaded image should exist.")
+            XCTAssert(image! == testImage, "Downloaded image should be the same as test image.")
+            XCTAssert(self.button.imageForState(UIControlState.Highlighted)! == testImage, "Downloaded image should be already set to the image for state")
+            XCTAssert(self.button.kf_webURLForState(UIControlState.Highlighted) == imageURL, "Web URL should equal to the downloaded url.")
+        }
+        waitForExpectationsWithTimeout(1, handler: nil)
+    }
+    
+    func testDownloadAndSetBackgroundImage() {
+        let expectation = expectationWithDescription("wait for downloading image")
+        
+        let URLString = testKeys[0]
+        stubRequest("GET", URLString).andReturn(200).withBody(testImageData)
+        let URL = NSURL(string: URLString)!
+        
+        var progressBlockIsCalled = false
+        button.kf_setBackgroundImageWithURL(URL, forState: UIControlState.Normal, placeholderImage: nil, options: KingfisherOptions.None, progressBlock: { (receivedSize, totalSize) -> () in
+            progressBlockIsCalled = true
+            }) { (image, error, imageURL) -> () in
+                expectation.fulfill()
+                
+                XCTAssert(progressBlockIsCalled, "progressBlock should be called at least once.")
+                XCTAssert(image != nil, "Downloaded image should exist.")
+                XCTAssert(image! == testImage, "Downloaded image should be the same as test image.")
+                XCTAssert(self.button.backgroundImageForState(UIControlState.Normal)! == testImage, "Downloaded image should be already set to the image for state")
+                XCTAssert(self.button.kf_backgroundWebURLForState(UIControlState.Normal) == imageURL, "Web URL should equal to the downloaded url.")
+        }
+        waitForExpectationsWithTimeout(1, handler: nil)
+    }
+}

+ 1 - 2
KingfisherTests/UIImageViewExtensionTests.swift

@@ -53,8 +53,7 @@ class UIImageViewExtensionTests: XCTestCase {
         LSNocilla.sharedInstance().clearStubs()
         imageView = nil
         
-        KingfisherManager.sharedManager.cache.clearDiskCache()
-        KingfisherManager.sharedManager.cache.clearMemoryCache()
+        cleanDefaultCache()
         
         super.tearDown()
     }

部分文件因为文件数量过多而无法显示