|
|
@@ -153,24 +153,26 @@ class UIImageViewExtensionTests: XCTestCase {
|
|
|
let task = imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
|
|
|
progressBlockIsCalled = true
|
|
|
}) { (image, error, cacheType, imageURL) -> () in
|
|
|
+ XCTAssertNotNil(error)
|
|
|
+ XCTAssertEqual(error?.code, NSURLErrorCancelled)
|
|
|
completionBlockIsCalled = true
|
|
|
}
|
|
|
|
|
|
- dispatch_async(dispatch_get_main_queue()) { () -> Void in
|
|
|
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(Double(NSEC_PER_SEC) * 0.1)), 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
|
|
|
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(Double(NSEC_PER_SEC) * 0.2)), 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.")
|
|
|
+ XCTAssert(completionBlockIsCalled == true, "CompletionBlock should not be called since it is canceled.")
|
|
|
}
|
|
|
|
|
|
waitForExpectationsWithTimeout(5, handler: nil)
|
|
|
}
|
|
|
|
|
|
- func testImageDownloadCancelPartialTask() {
|
|
|
+ func testImageDownloadCancelPartialTaskBeforeRequest() {
|
|
|
let expectation = expectationWithDescription("wait for downloading image")
|
|
|
|
|
|
let URLString = testKeys[0]
|
|
|
@@ -190,20 +192,22 @@ class UIImageViewExtensionTests: XCTestCase {
|
|
|
let _ = imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
|
|
|
|
|
|
}) { (image, error, cacheType, imageURL) -> () in
|
|
|
+ XCTAssertNotNil(image)
|
|
|
task2Completion = true
|
|
|
}
|
|
|
|
|
|
let _ = imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
|
|
|
|
|
|
}) { (image, error, cacheType, imageURL) -> () in
|
|
|
+ XCTAssertNotNil(image)
|
|
|
task3Completion = true
|
|
|
}
|
|
|
|
|
|
task1.cancel()
|
|
|
|
|
|
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(Double(NSEC_PER_SEC) * 0.09)), dispatch_get_main_queue()) { () -> Void in
|
|
|
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(Double(NSEC_PER_SEC) * 0.2)), dispatch_get_main_queue()) { () -> Void in
|
|
|
expectation.fulfill()
|
|
|
- XCTAssert(task1Completion == false, "Task 1 is canceled. The completion flag should be fasle.")
|
|
|
+ XCTAssert(task1Completion == false, "Task 1 should be not completed since it is cancelled before downloading started.")
|
|
|
XCTAssert(task2Completion == true, "Task 2 should be completed.")
|
|
|
XCTAssert(task3Completion == true, "Task 3 should be completed.")
|
|
|
}
|
|
|
@@ -225,33 +229,32 @@ class UIImageViewExtensionTests: XCTestCase {
|
|
|
let task1 = imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
|
|
|
|
|
|
}) { (image, error, cacheType, imageURL) -> () in
|
|
|
+ XCTAssertNotNil(image)
|
|
|
task1Completion = true
|
|
|
}
|
|
|
|
|
|
- let task2 = imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
|
|
|
+ let _ = imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
|
|
|
|
|
|
}) { (image, error, cacheType, imageURL) -> () in
|
|
|
+ XCTAssertNotNil(image)
|
|
|
task2Completion = true
|
|
|
}
|
|
|
|
|
|
- let task3 = imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
|
|
|
+ let _ = imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
|
|
|
|
|
|
}) { (image, error, cacheType, imageURL) -> () in
|
|
|
+ XCTAssertNotNil(image)
|
|
|
task3Completion = true
|
|
|
}
|
|
|
|
|
|
- // Prevent unused warning.
|
|
|
- print(task2)
|
|
|
- print(task3)
|
|
|
-
|
|
|
- dispatch_async(dispatch_get_main_queue()) { () -> Void in
|
|
|
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(Double(NSEC_PER_SEC) * 0.1)), 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
|
|
|
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(Double(NSEC_PER_SEC) * 0.2)), dispatch_get_main_queue()) { () -> Void in
|
|
|
expectation.fulfill()
|
|
|
- XCTAssert(task1Completion == false, "Task 1 is canceled. The completion flag should be fasle.")
|
|
|
+ XCTAssert(task1Completion == true, "Task 1 should be completed since task 2 and 3 are not cancelled and they are sharing the same downloading process.")
|
|
|
XCTAssert(task2Completion == true, "Task 2 should be completed.")
|
|
|
XCTAssert(task3Completion == true, "Task 3 should be completed.")
|
|
|
}
|
|
|
@@ -259,11 +262,66 @@ class UIImageViewExtensionTests: XCTestCase {
|
|
|
waitForExpectationsWithTimeout(5, handler: nil)
|
|
|
}
|
|
|
|
|
|
+ func testImageDownloadCancelAllTasksAfterRequestStarted() {
|
|
|
+ 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
|
|
|
+ XCTAssertNotNil(error)
|
|
|
+ XCTAssertEqual(error?.code, NSURLErrorCancelled)
|
|
|
+ task1Completion = true
|
|
|
+ }
|
|
|
+
|
|
|
+ let task2 = imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
|
|
|
+
|
|
|
+ }) { (image, error, cacheType, imageURL) -> () in
|
|
|
+ XCTAssertNotNil(error)
|
|
|
+ XCTAssertEqual(error?.code, NSURLErrorCancelled)
|
|
|
+ task2Completion = true
|
|
|
+ }
|
|
|
+
|
|
|
+ let task3 = imageView.kf_setImageWithURL(URL, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
|
|
|
+
|
|
|
+ }) { (image, error, cacheType, imageURL) -> () in
|
|
|
+ XCTAssertNotNil(error)
|
|
|
+ XCTAssertEqual(error?.code, NSURLErrorCancelled)
|
|
|
+ task3Completion = true
|
|
|
+ }
|
|
|
+
|
|
|
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(Double(NSEC_PER_SEC) * 0.1)), dispatch_get_main_queue()) { () -> Void in
|
|
|
+ task1.cancel()
|
|
|
+ task2.cancel()
|
|
|
+ task3.cancel()
|
|
|
+ stub.go()
|
|
|
+ }
|
|
|
+
|
|
|
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(Double(NSEC_PER_SEC) * 0.2)), dispatch_get_main_queue()) { () -> Void in
|
|
|
+ expectation.fulfill()
|
|
|
+ XCTAssert(task1Completion == true, "Task 1 should be completed with error.")
|
|
|
+ XCTAssert(task2Completion == true, "Task 2 should be completed with error.")
|
|
|
+ XCTAssert(task3Completion == true, "Task 3 should be completed with error.")
|
|
|
+ }
|
|
|
+
|
|
|
+ waitForExpectationsWithTimeout(5, handler: nil)
|
|
|
+ }
|
|
|
+
|
|
|
func testImageDownalodMultipleCaches() {
|
|
|
|
|
|
let cache1 = ImageCache(name: "cache1")
|
|
|
let cache2 = ImageCache(name: "cache2")
|
|
|
|
|
|
+ cache1.clearDiskCache(true)
|
|
|
+ cache2.clearDiskCache(true)
|
|
|
+
|
|
|
let expectation = expectationWithDescription("wait for downloading image")
|
|
|
|
|
|
let URLString = testKeys[0]
|