| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- //
- // UIImageViewExtensionTests.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
- @testable import Kingfisher
- class UIImageViewExtensionTests: XCTestCase {
- var imageView: UIImageView!
-
- 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.
- imageView = UIImageView()
- KingfisherManager.sharedManager.downloader = ImageDownloader(name: "testDownloader")
- cleanDefaultCache()
- }
-
- override func tearDown() {
- // Put teardown code here. This method is called after the invocation of each test method in the class.
- LSNocilla.sharedInstance().clearStubs()
- imageView = nil
-
- cleanDefaultCache()
-
- super.tearDown()
- }
- func testImageDownloadForImageView() {
- 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
-
- imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
- progressBlockIsCalled = true
- }) { (image, error, cacheType, 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.imageView.image! == testImage, "Downloaded image should be already set to the image property.")
- XCTAssert(self.imageView.kf_webURL == imageURL, "Web URL should equal to the downloaded url.")
-
- XCTAssert(cacheType == .None, "The cache type should be none here. This image was just downloaded.")
- }
-
- waitForExpectationsWithTimeout(5, handler: nil)
- }
-
- func testImageDownloadWithResourceForImageView() {
- let expectation = expectationWithDescription("wait for downloading image")
-
- let URLString = testKeys[0]
- stubRequest("GET", URLString).andReturn(200).withBody(testImageData)
- let URL = NSURL(string: URLString)!
- let resource = Resource(downloadURL: URL)
-
- var progressBlockIsCalled = false
-
- imageView.kf_setImageWithResource(resource, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
- progressBlockIsCalled = true
- }) { (image, error, cacheType, 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.imageView.image! == testImage, "Downloaded image should be already set to the image property.")
- XCTAssert(self.imageView.kf_webURL == imageURL, "Web URL should equal to the downloaded url.")
-
- XCTAssert(cacheType == .None, "The cache type should be none here. This image was just downloaded.")
- }
-
- waitForExpectationsWithTimeout(5, handler: nil)
- }
-
- func testImageDownloadCancelForImageView() {
- 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
- var completionBlockIsCalled = false
-
- let task = imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
- progressBlockIsCalled = true
- }) { (image, error, cacheType, imageURL) -> () in
- completionBlockIsCalled = true
- }
- task.cancel()
-
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(Double(NSEC_PER_SEC) * 0.09)), dispatch_get_main_queue()) { () -> Void in
- expectation.fulfill()
- XCTAssert(progressBlockIsCalled == false, "ProgressBlock should not be called since it is canceled.")
- XCTAssert(completionBlockIsCalled == false, "CompletionBlock should not be called since it is canceled.")
- }
-
- waitForExpectationsWithTimeout(5, handler: nil)
- }
-
- func testImageDownloadCancelForImageViewAfterRequestStarted() {
- let expectation = expectationWithDescription("wait for downloading image")
-
- let URLString = testKeys[0]
- let stub = stubRequest("GET", URLString).andReturn(200).withBody(testImageData).delay()
- let URL = NSURL(string: URLString)!
-
- var progressBlockIsCalled = false
- var completionBlockIsCalled = false
-
- let task = imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
- progressBlockIsCalled = true
- }) { (image, error, cacheType, imageURL) -> () in
- completionBlockIsCalled = true
- }
-
- dispatch_async(dispatch_get_main_queue()) { () -> Void in
- task.cancel()
- stub.go()
- }
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(Double(NSEC_PER_SEC) * 0.09)), dispatch_get_main_queue()) { () -> Void in
- expectation.fulfill()
- XCTAssert(progressBlockIsCalled == false, "ProgressBlock should not be called since it is canceled.")
- XCTAssert(completionBlockIsCalled == false, "CompletionBlock should not be called since it is canceled.")
- }
-
- waitForExpectationsWithTimeout(5, handler: nil)
- }
- func testImageDownloadCancelPartialTask() {
- let expectation = expectationWithDescription("wait for downloading image")
-
- let URLString = testKeys[0]
- stubRequest("GET", URLString).andReturn(200).withBody(testImageData)
- let URL = NSURL(string: URLString)!
-
- var task1Completion = false
- var task2Completion = false
- var task3Completion = false
-
- let task1 = imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
- }) { (image, error, cacheType, imageURL) -> () in
- task1Completion = true
- }
-
- let _ = imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
-
- }) { (image, error, cacheType, imageURL) -> () in
- task2Completion = true
- }
-
- let _ = imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
-
- }) { (image, error, cacheType, imageURL) -> () in
- task3Completion = true
- }
-
- task1.cancel()
-
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(Double(NSEC_PER_SEC) * 0.09)), dispatch_get_main_queue()) { () -> Void in
- expectation.fulfill()
- XCTAssert(task1Completion == false, "Task 1 is canceled. The completion flag should be fasle.")
- XCTAssert(task2Completion == true, "Task 2 should be completed.")
- XCTAssert(task3Completion == true, "Task 3 should be completed.")
- }
-
- waitForExpectationsWithTimeout(5, handler: nil)
- }
-
- func testImageDownloadCancelPartialTaskAfterRequestStarted() {
- let expectation = expectationWithDescription("wait for downloading image")
-
- let URLString = testKeys[0]
- let stub = stubRequest("GET", URLString).andReturn(200).withBody(testImageData).delay()
- let URL = NSURL(string: URLString)!
-
- var task1Completion = false
- var task2Completion = false
- var task3Completion = false
-
- let task1 = imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
-
- }) { (image, error, cacheType, imageURL) -> () in
- task1Completion = true
- }
-
- let task2 = imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
-
- }) { (image, error, cacheType, imageURL) -> () in
- task2Completion = true
- }
-
- let task3 = imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
-
- }) { (image, error, cacheType, imageURL) -> () in
- task3Completion = true
- }
-
- // Prevent unused warning.
- print(task2)
- print(task3)
-
- dispatch_async(dispatch_get_main_queue()) { () -> Void in
- task1.cancel()
- stub.go()
- }
-
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(Double(NSEC_PER_SEC) * 0.09)), dispatch_get_main_queue()) { () -> Void in
- expectation.fulfill()
- XCTAssert(task1Completion == false, "Task 1 is canceled. The completion flag should be fasle.")
- XCTAssert(task2Completion == true, "Task 2 should be completed.")
- XCTAssert(task3Completion == true, "Task 3 should be completed.")
- }
-
- waitForExpectationsWithTimeout(5, handler: nil)
- }
-
- func testImageDownalodMultipleCaches() {
-
- let cache1 = ImageCache(name: "cache1")
- let cache2 = ImageCache(name: "cache2")
-
- let expectation = expectationWithDescription("wait for downloading image")
-
- let URLString = testKeys[0]
- stubRequest("GET", URLString).andReturn(200).withBody(testImageData)
- let URL = NSURL(string: URLString)!
-
- imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: [.TargetCache(cache1)], progressBlock: { (receivedSize, totalSize) -> () in
-
- }) { (image, error, cacheType, imageURL) -> () in
-
- XCTAssertTrue(cache1.isImageCachedForKey(URLString).cached, "This image should be cached in cache1.")
- XCTAssertFalse(cache2.isImageCachedForKey(URLString).cached, "This image should not be cached in cache2.")
- XCTAssertFalse(KingfisherManager.sharedManager.cache.isImageCachedForKey(URLString).cached, "This image should not be cached in default cache.")
-
- self.imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: [.TargetCache(cache2)], progressBlock: { (receivedSize, totalSize) -> () in
-
- }, completionHandler: { (image, error, cacheType, imageURL) -> () in
-
- XCTAssertTrue(cache1.isImageCachedForKey(URLString).cached, "This image should be cached in cache1.")
- XCTAssertTrue(cache2.isImageCachedForKey(URLString).cached, "This image should be cached in cache2.")
- XCTAssertFalse(KingfisherManager.sharedManager.cache.isImageCachedForKey(URLString).cached, "This image should not be cached in default cache.")
-
- clearCaches([cache1, cache2])
-
- expectation.fulfill()
- })
-
- }
-
- waitForExpectationsWithTimeout(5, handler: { (error) -> Void in
- clearCaches([cache1, cache2])
- })
- }
-
- func testIndicatorViewExisting() {
- imageView.kf_showIndicatorWhenLoading = true
- XCTAssertNotNil(imageView.kf_indicator, "The indicator view should exist when showIndicatorWhenLoading is true")
-
- imageView.kf_showIndicatorWhenLoading = false
- XCTAssertNil(imageView.kf_indicator, "The indicator view should be removed when showIndicatorWhenLoading set to false")
- }
-
- func testIndicatorViewAnimating() {
- imageView.kf_showIndicatorWhenLoading = true
-
- let expectation = expectationWithDescription("wait for downloading image")
-
- let URLString = testKeys[0]
- stubRequest("GET", URLString).andReturn(200).withBody(testImageData)
- let URL = NSURL(string: URLString)!
-
- imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
-
- let indicator = self.imageView.kf_indicator
- XCTAssertNotNil(indicator, "The indicator view should exist when showIndicatorWhenLoading is true")
- XCTAssertTrue(indicator!.isAnimating(), "The indicator should be animating when loading")
- }) { (image, error, cacheType, imageURL) -> () in
- let indicator = self.imageView.kf_indicator
- XCTAssertFalse(indicator!.isAnimating(), "The indicator should stop after loading")
- expectation.fulfill()
- }
-
- waitForExpectationsWithTimeout(5, handler: nil)
- }
- }
|