| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592 |
- //
- // UIImageViewExtensionTests.swift
- // Kingfisher
- //
- // Created by Wei Wang on 15/4/17.
- //
- // Copyright (c) 2017 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 XCTest
- @testable import Kingfisher
- class ImageViewExtensionTests: XCTestCase {
- var imageView: ImageView!
-
- 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 = ImageView()
- KingfisherManager.shared.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 = self.expectation(description: "wait for downloading image")
-
- let URLString = testKeys[0]
- _ = stubRequest("GET", URLString).andReturn(200)?.withBody(testImageData)
- let url = URL(string: URLString)!
-
- var progressBlockIsCalled = false
-
- imageView.kf.setImage(with: url, placeholder: nil, options: nil, progressBlock: { (receivedSize, totalSize) -> () in
- progressBlockIsCalled = true
- XCTAssertTrue(Thread.isMainThread)
- }) { (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.")
- XCTAssertTrue(Thread.isMainThread)
- }
-
- waitForExpectations(timeout: 5, handler: nil)
- }
-
- func testImageDownloadCompletionHandlerRunningOnMainQueue() {
- let expectation = self.expectation(description: "wait for downloading image")
-
- let URLString = testKeys[0]
- _ = stubRequest("GET", URLString).andReturn(200)?.withBody(testImageData)
- let url = URL(string: URLString)!
-
- let customQueue = DispatchQueue(label: "com.kingfisher.testQueue")
- imageView.kf.setImage(with: url, placeholder: nil, options: [.callbackDispatchQueue(customQueue)], progressBlock: { (receivedSize, totalSize) -> () in
- XCTAssertTrue(Thread.isMainThread)
- }) { (image, error, cacheType, imageURL) -> () in
- XCTAssertTrue(Thread.isMainThread, "The image extension callback should be always in main queue.")
- expectation.fulfill()
- }
-
- waitForExpectations(timeout: 5, handler: nil)
- }
-
- func testImageDownloadWithResourceForImageView() {
- let expectation = self.expectation(description: "wait for downloading image")
-
- let URLString = testKeys[0]
- _ = stubRequest("GET", URLString).andReturn(200)?.withBody(testImageData)
- let url = URL(string: URLString)!
- let resource = ImageResource(downloadURL: url)
-
- var progressBlockIsCalled = false
-
- cleanDefaultCache()
-
- _ = imageView.kf.setImage(with: resource, placeholder: nil, options: 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. But now is: \(cacheType)")
- }
-
- waitForExpectations(timeout: 5, handler: nil)
- }
-
- func testImageDownloadCancelForImageView() {
- let expectation = self.expectation(description: "wait for downloading image")
- let URLString = testKeys[0]
- _ = stubRequest("GET", URLString).andReturn(200)?.withBody(testImageData)
- let url = URL(string: URLString)!
-
- var progressBlockIsCalled = false
- let task = imageView.kf.setImage(with: url, placeholder: nil, options: nil, progressBlock: { (receivedSize, totalSize) -> () in
- progressBlockIsCalled = true
- }) { (image, error, cacheType, imageURL) -> () in
- XCTAssertEqual(error?.code, KingfisherError.downloadCancelledBeforeStarting.rawValue, "The error should be downloadCancelledBeforeStarting")
- XCTAssert(progressBlockIsCalled == false, "ProgressBlock should not be called since it is canceled.")
- expectation.fulfill()
- }
- task.cancel()
- waitForExpectations(timeout: 5, handler: nil)
- }
-
- func testImageDownloadCancelForImageViewAfterRequestStarted() {
- let expectation = self.expectation(description: "wait for downloading image")
-
- let URLString = testKeys[0]
- let stub = stubRequest("GET", URLString).andReturn(200)?.withBody(testImageData)?.delay()
- let url = URL(string: URLString)!
-
- var progressBlockIsCalled = false
-
- cleanDefaultCache()
-
- let task = imageView.kf.setImage(with: url, placeholder: nil, options: nil, progressBlock: { (receivedSize, totalSize) -> () in
- progressBlockIsCalled = true
- }) { (image, error, cacheType, imageURL) -> () in
- XCTAssertNotNil(error)
- XCTAssertEqual(error?.code, NSURLErrorCancelled)
- XCTAssert(progressBlockIsCalled == false, "ProgressBlock should not be called since it is canceled.")
- expectation.fulfill()
- }
-
- delay(0.1) {
- task.cancel()
- _ = stub!.go()
- }
- waitForExpectations(timeout: 5, handler: nil)
- }
- func testImageDownloadCancelPartialTaskBeforeRequest() {
- let expectation = self.expectation(description: "wait for downloading image")
-
- let URLString = testKeys[0]
- let stub = stubRequest("GET", URLString).andReturn(200)?.withBody(testImageData)?.delay()
- let url = URL(string: URLString)!
-
- let group = DispatchGroup()
-
- group.enter()
- let task1 = imageView.kf.setImage(with: url, placeholder: nil, options: nil, progressBlock: { (receivedSize, totalSize) -> () in
- }) { (image, error, cacheType, imageURL) -> () in
- XCTAssertNil(image)
- XCTAssertEqual(error?.code, KingfisherError.downloadCancelledBeforeStarting.rawValue, "The error should be downloadCancelledBeforeStarting")
- group.leave()
- }
-
- group.enter()
- imageView.kf.setImage(with: url, placeholder: nil, options: nil, progressBlock: { (receivedSize, totalSize) -> () in
-
- }) { (image, error, cacheType, imageURL) -> () in
- XCTAssertNotNil(image)
- group.leave()
- }
-
- group.enter()
- imageView.kf.setImage(with: url, placeholder: nil, options: nil, progressBlock: { (receivedSize, totalSize) -> () in
-
- }) { (image, error, cacheType, imageURL) -> () in
- XCTAssertNotNil(image)
- group.leave()
- }
-
- task1.cancel()
- delay(0.1) { _ = stub!.go() }
-
- group.notify(queue: .main, execute: expectation.fulfill)
- waitForExpectations(timeout: 5, handler: nil)
- }
-
- func testImageDownloadCancelPartialTaskAfterRequestStarted() {
- let expectation = self.expectation(description: "wait for downloading image")
-
- let URLString = testKeys[0]
- let stub = stubRequest("GET", URLString).andReturn(200)?.withBody(testImageData)?.delay()
- let url = URL(string: URLString)!
-
- let group = DispatchGroup()
-
- group.enter()
- let task1 = imageView.kf.setImage(with: url, placeholder: nil, options: nil, progressBlock: { (receivedSize, totalSize) -> () in
-
- }) { (image, error, cacheType, imageURL) -> () in
- XCTAssertNotNil(image)
- group.leave()
- }
-
- group.enter()
- imageView.kf.setImage(with: url, placeholder: nil, options: nil, progressBlock: { (receivedSize, totalSize) -> () in
-
- }) { (image, error, cacheType, imageURL) -> () in
- XCTAssertNotNil(image)
- group.leave()
- }
-
- group.enter()
- imageView.kf.setImage(with: url, placeholder: nil, options: nil, progressBlock: { (receivedSize, totalSize) -> () in
-
- }) { (image, error, cacheType, imageURL) -> () in
- XCTAssertNotNil(image)
- group.leave()
- }
-
- delay(0.1) {
- task1.cancel()
- _ = stub!.go()
- }
-
- group.notify(queue: .main, execute: expectation.fulfill)
- waitForExpectations(timeout: 5, handler: nil)
- }
-
- func testImageDownloadCancelAllTasksAfterRequestStarted() {
- let expectation = self.expectation(description: "wait for downloading image")
-
- let URLString = testKeys[0]
- let stub = stubRequest("GET", URLString).andReturn(200)?.withBody(testImageData)?.delay()
- let url = URL(string: URLString)!
-
-
- let group = DispatchGroup()
-
- group.enter()
- let task1 = imageView.kf.setImage(with: url, placeholder: nil, options: nil, progressBlock: { (receivedSize, totalSize) -> () in
-
- }) { (image, error, cacheType, imageURL) -> () in
- XCTAssertNotNil(error)
- XCTAssertEqual(error?.code, NSURLErrorCancelled)
- group.leave()
- }
-
- group.enter()
- let task2 = imageView.kf.setImage(with: url, placeholder: nil, options: nil, progressBlock: { (receivedSize, totalSize) -> () in
-
- }) { (image, error, cacheType, imageURL) -> () in
- XCTAssertNotNil(error)
- XCTAssertEqual(error?.code, NSURLErrorCancelled)
- group.leave()
- }
-
- group.enter()
- let task3 = imageView.kf.setImage(with: url, placeholder: nil, options: nil, progressBlock: { (receivedSize, totalSize) -> () in
-
- }) { (image, error, cacheType, imageURL) -> () in
- XCTAssertNotNil(error)
- XCTAssertEqual(error?.code, NSURLErrorCancelled)
- group.leave()
- }
-
- delay(0.1) {
- task1.cancel()
- task2.cancel()
- task3.cancel()
- _ = stub!.go()
- }
-
- group.notify(queue: .main, execute: expectation.fulfill)
- waitForExpectations(timeout: 5, handler: nil)
- }
-
- func testImageDownloadMultipleCaches() {
-
- let cache1 = ImageCache(name: "cache1")
- let cache2 = ImageCache(name: "cache2")
-
- cache1.clearDiskCache()
- cache2.clearDiskCache()
-
- let expectation = self.expectation(description: "wait for downloading image")
-
- let URLString = testKeys[0]
- _ = stubRequest("GET", URLString).andReturn(200)?.withBody(testImageData)
- let url = URL(string: URLString)!
-
- imageView.kf.setImage(with: url, placeholder: nil, options: [.targetCache(cache1)], progressBlock: { (receivedSize, totalSize) -> () in
-
- }) { (image, error, cacheType, imageURL) -> () in
-
- XCTAssertTrue(cache1.isImageCached(forKey: URLString).cached, "This image should be cached in cache1.")
- XCTAssertFalse(cache2.isImageCached(forKey: URLString).cached, "This image should not be cached in cache2.")
- XCTAssertFalse(KingfisherManager.shared.cache.isImageCached(forKey: URLString).cached, "This image should not be cached in default cache.")
-
- self.imageView.kf.setImage(with: url, placeholder: nil, options: [.targetCache(cache2)], progressBlock: { (receivedSize, totalSize) -> () in
-
- }, completionHandler: { (image, error, cacheType, imageURL) -> () in
-
- XCTAssertTrue(cache1.isImageCached(forKey: URLString).cached, "This image should be cached in cache1.")
- XCTAssertTrue(cache2.isImageCached(forKey: URLString).cached, "This image should be cached in cache2.")
- XCTAssertFalse(KingfisherManager.shared.cache.isImageCached(forKey: URLString).cached, "This image should not be cached in default cache.")
-
- clearCaches([cache1, cache2])
-
- expectation.fulfill()
- })
-
- }
-
- waitForExpectations(timeout: 5, handler: { (error) -> Void in
- clearCaches([cache1, cache2])
- })
- }
-
- func testIndicatorViewExisting() {
- imageView.kf.indicatorType = .activity
- XCTAssertNotNil(imageView.kf.indicator, "The indicator should exist when indicatorType is different than .none")
- XCTAssertTrue(imageView.kf.indicator is ActivityIndicator)
- imageView.kf.indicatorType = .none
- XCTAssertNil(imageView.kf.indicator, "The indicator should be removed when indicatorType is .none")
- }
-
- func testActivityIndicatorViewAnimating() {
- imageView.kf.indicatorType = .activity
-
- let expectation = self.expectation(description: "wait for downloading image")
-
- let URLString = testKeys[0]
- _ = stubRequest("GET", URLString).andReturn(200)?.withBody(testImageData)
- let url = URL(string: URLString)!
-
- imageView.kf.setImage(with: url, placeholder: nil, options: nil, progressBlock: { (receivedSize, totalSize) -> () in
-
- let indicator = self.imageView.kf.indicator
- XCTAssertNotNil(indicator, "The indicator view should exist when showIndicatorWhenLoading is true")
- XCTAssertFalse(indicator!.view.isHidden, "The indicator should be shown and animating when loading")
- }) { (image, error, cacheType, imageURL) -> () in
- let indicator = self.imageView.kf.indicator
- XCTAssertTrue(indicator!.view.isHidden, "The indicator should stop and hidden after loading")
- expectation.fulfill()
- }
-
- waitForExpectations(timeout: 5, handler: nil)
- }
-
- func testCanUseImageIndicatorViewAnimating() {
-
- imageView.kf.indicatorType = .image(imageData: testImageData! as Data)
- XCTAssertTrue(imageView.kf.indicator is ImageIndicator)
- let image = (imageView.kf.indicator?.view as? ImageView)?.image
- XCTAssertNotNil(image)
- XCTAssertTrue(image!.renderEqual(to: testImage))
-
- let expectation = self.expectation(description: "wait for downloading image")
-
- let URLString = testKeys[0]
- _ = stubRequest("GET", URLString).andReturn(200)?.withBody(testImageData)
- let url = URL(string: URLString)!
-
- imageView.kf.setImage(with: url, placeholder: nil, options: nil, progressBlock: { (receivedSize, totalSize) -> () in
-
- let indicator = self.imageView.kf.indicator
- XCTAssertNotNil(indicator, "The indicator view should exist when showIndicatorWhenLoading is true")
- XCTAssertFalse(indicator!.view.isHidden, "The indicator should be shown and animating when loading")
-
- }) { (image, error, cacheType, imageURL) -> () in
- let indicator = self.imageView.kf.indicator
- XCTAssertTrue(indicator!.view.isHidden, "The indicator should stop and hidden after loading")
- expectation.fulfill()
- }
-
- waitForExpectations(timeout: 5, handler: nil)
- }
-
- func testCacnelImageTask() {
- let expectation = self.expectation(description: "wait for downloading image")
-
- let URLString = testKeys[0]
- let stub = stubRequest("GET", URLString).andReturn(200)?.withBody(testImageData)?.delay()
- let url = URL(string: URLString)!
-
- imageView.kf.setImage(with: url, placeholder: nil, options: nil, progressBlock: { (receivedSize, totalSize) -> () in
- XCTFail("Progress block should not be called.")
- }) { (image, error, cacheType, imageURL) -> () in
- XCTAssertNotNil(error)
- XCTAssertEqual(error?.code, NSURLErrorCancelled)
-
- expectation.fulfill()
- }
-
- delay(0.1) {
- self.imageView.kf.cancelDownloadTask()
- _ = stub!.go()
- }
-
- waitForExpectations(timeout: 5, handler: nil)
- }
-
- func testDownloadForMutipleURLs() {
- let expectation = self.expectation(description: "wait for downloading image")
-
- let URLStrings = [testKeys[0], testKeys[1]]
- _ = stubRequest("GET", URLStrings[0]).andReturn(200)?.withBody(testImageData)
- _ = stubRequest("GET", URLStrings[1]).andReturn(200)?.withBody(testImageData)
- let URLs = URLStrings.map{URL(string: $0)!}
-
- let group = DispatchGroup()
-
- group.enter()
- imageView.kf.setImage(with: URLs[0], placeholder: nil, options: nil) {
- image, error, cacheType, imageURL in
- XCTAssertNotNil(image)
- XCTAssertEqual(imageURL, URLs[0])
- XCTAssertNotEqual(self.imageView.image, image)
- group.leave()
- }
-
- group.enter()
- self.imageView.kf.setImage(with: URLs[1], placeholder: nil, options: nil) {
- image, error, cacheType, imageURL in
- XCTAssertNotNil(image)
- XCTAssertEqual(imageURL, URLs[1])
- XCTAssertEqual(self.imageView.image, image)
- group.leave()
- }
-
- group.notify(queue: .main, execute: expectation.fulfill)
- waitForExpectations(timeout: 5, handler: nil)
- }
-
- func testSettingNilURL() {
- let expectation = self.expectation(description: "wait for downloading image")
-
- let url: URL? = nil
- imageView.kf.setImage(with: url, placeholder: nil, options: nil, progressBlock: { (receivedSize, totalSize) -> () in
- XCTFail("Progress block should not be called.")
- }) { (image, error, cacheType, imageURL) -> () in
- XCTAssertNil(image)
- XCTAssertNil(error)
- XCTAssertEqual(cacheType, CacheType.none)
- XCTAssertNil(imageURL)
-
- expectation.fulfill()
- }
-
- waitForExpectations(timeout: 5, handler: nil)
- }
-
- func testSettingImageWhileKeepingCurrentOne() {
- let expectation = self.expectation(description: "wait for downloading image")
- let URLString = testKeys[0]
- _ = stubRequest("GET", URLString).andReturn(200)?.withBody(testImageData)
- let url = URL(string: URLString)!
-
- imageView.image = testImage
- imageView.kf.setImage(with: url, placeholder: nil, options: nil)
- XCTAssertNil(imageView.image)
-
- imageView.image = testImage
- imageView.kf.setImage(with: url, placeholder: nil, options: [.keepCurrentImageWhileLoading])
- XCTAssertEqual(testImage, imageView.image)
-
- // Wait request finished. Ensure tests timing order.
- delay(0.1, block: expectation.fulfill)
- waitForExpectations(timeout: 5, handler: nil)
- }
-
- func testSetGIFImageOnlyFirstFrameThenFullFrames() {
- let expectation = self.expectation(description: "wait for downloading image")
-
- let URLString = testKeys[0]
-
- _ = stubRequest("GET", URLString).andReturn(200)?.withBody(NSData(data: testImageGIFData))
- let url = URL(string: URLString)!
-
- func loadFullGIFImage() {
- var progressBlockIsCalled = false
- ImageCache.default.clearMemoryCache()
-
- imageView.kf.setImage(with: url, placeholder: nil, options: [], progressBlock: { (receivedSize, totalSize) -> () in
- progressBlockIsCalled = true
- XCTAssertTrue(Thread.isMainThread)
- }) { (image, error, cacheType, imageURL) -> () in
-
- XCTAssertFalse(progressBlockIsCalled, "progressBlock should not be called since the image is cached.")
- XCTAssertNotNil(image, "Downloaded image should exist.")
- XCTAssertNotNil(image!.kf.images, "images should exist since we load full GIF.")
- XCTAssertEqual(image!.kf.images?.count, 8, "There are 8 frames in total.")
-
- XCTAssert(cacheType == .disk, "We should find it cached in disk")
- XCTAssertTrue(Thread.isMainThread)
-
- expectation.fulfill()
- }
- }
-
- var progressBlockIsCalled = false
- imageView.kf.setImage(with: url, placeholder: nil, options: [.onlyLoadFirstFrame], progressBlock: { (receivedSize, totalSize) -> () in
- progressBlockIsCalled = true
- XCTAssertTrue(Thread.isMainThread)
- }) { (image, error, cacheType, imageURL) -> () in
- XCTAssertTrue(progressBlockIsCalled, "progressBlock should be called at least once.")
- XCTAssertNotNil(image, "Downloaded image should exist.")
- XCTAssertNil(image!.kf.images, "images should not exist since we set only load first frame.")
-
- XCTAssert(cacheType == .none, "The cache type should be none here. This image was just downloaded.")
- XCTAssertTrue(Thread.isMainThread)
-
- loadFullGIFImage()
- }
-
-
- waitForExpectations(timeout: 5, handler: nil)
- }
-
- // https://github.com/onevcat/Kingfisher/issues/665
- // The completion handler should be called even when the image view loading url gets changed.
- func testIssue665() {
- let expectation = self.expectation(description: "wait for downloading image")
-
- let URLString1 = testKeys[0]
- let URLString2 = testKeys[1]
-
- _ = stubRequest("GET", URLString1).andReturn(200)?.withBody(testImageData)
- _ = stubRequest("GET", URLString2).andReturn(200)?.withBody(testImageData)
-
- let url1 = URL(string: URLString1)!
- let url2 = URL(string: URLString2)!
-
- let group = DispatchGroup()
-
- group.enter()
- imageView.kf.setImage(with: url1) { (image, _, cacheType, url) in
- group.leave()
- }
-
- group.enter()
- imageView.kf.setImage(with: url2) { (image, _, cacheType, url) in
- group.leave()
- }
-
- group.notify(queue: .main, execute: expectation.fulfill)
- waitForExpectations(timeout: 1, handler: nil)
- }
- }
|