ImageDownloaderTests.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // ImageDownloaderTests.swift
  3. // Kingfisher-Demo
  4. //
  5. // Created by WANG WEI on 2015/04/10.
  6. // Copyright (c) 2015年 Wei Wang. All rights reserved.
  7. //
  8. import UIKit
  9. import XCTest
  10. import Kingfisher
  11. class ImageDownloaderTests: XCTestCase {
  12. var downloader: ImageDownloader!
  13. override class func setUp() {
  14. super.setUp()
  15. LSNocilla.sharedInstance().start()
  16. }
  17. override class func tearDown() {
  18. super.tearDown()
  19. LSNocilla.sharedInstance().stop()
  20. }
  21. override func setUp() {
  22. super.setUp()
  23. // Put setup code here. This method is called before the invocation of each test method in the class.
  24. downloader = ImageDownloader()
  25. }
  26. override func tearDown() {
  27. // Put teardown code here. This method is called after the invocation of each test method in the class.
  28. super.tearDown()
  29. LSNocilla.sharedInstance().clearStubs()
  30. downloader = nil
  31. }
  32. func testDownloadAnImage() {
  33. let expectation = expectationWithDescription("wait for downloading image")
  34. let urlString = testKeys[0]
  35. stubRequest("GET", urlString).andReturn(200).withBody(testImageData)
  36. let url = NSURL(string: urlString)!
  37. downloader.downloadImageWithURL(url, options: KingfisherManager.OptionsNone, progressBlock: { (receivedSize, totalSize) -> () in
  38. return
  39. }) { (image, error, imageURL) -> () in
  40. expectation.fulfill()
  41. XCTAssert(image != nil, "Download should be able to finished for url: \(imageURL)")
  42. }
  43. waitForExpectationsWithTimeout(1, handler: nil)
  44. }
  45. func testDownloadMultipleImages() {
  46. let expectation = expectationWithDescription("wait for all downloading finish")
  47. let group = dispatch_group_create()
  48. for urlString in testKeys {
  49. if let url = NSURL(string: urlString) {
  50. dispatch_group_enter(group)
  51. stubRequest("GET", urlString).andReturn(200).withBody(testImageData)
  52. downloader.downloadImageWithURL(url, options: KingfisherManager.OptionsNone, progressBlock: { (receivedSize, totalSize) -> () in
  53. }, completionHandler: { (image, error, imageURL) -> () in
  54. XCTAssert(image != nil, "Download should be able to finished for url: \(imageURL).")
  55. dispatch_group_leave(group)
  56. })
  57. }
  58. }
  59. dispatch_group_notify(group, dispatch_get_main_queue()) { () -> Void in
  60. expectation.fulfill()
  61. }
  62. waitForExpectationsWithTimeout(1, handler: nil)
  63. }
  64. func testDownloadAnImageWithMultipleCallback() {
  65. let expectation = expectationWithDescription("wait for downloading image")
  66. let group = dispatch_group_create()
  67. let urlString = testKeys[0]
  68. stubRequest("GET", urlString).andReturn(200).withBody(testImageData)
  69. for _ in 0...5 {
  70. dispatch_group_enter(group)
  71. downloader.downloadImageWithURL(NSURL(string: urlString)!, options: KingfisherManager.OptionsNone, progressBlock: { (receivedSize, totalSize) -> () in
  72. }) { (image, error, imageURL) -> () in
  73. XCTAssert(image != nil, "Download should be able to finished for url: \(imageURL).")
  74. dispatch_group_leave(group)
  75. }
  76. }
  77. dispatch_group_notify(group, dispatch_get_main_queue()) { () -> Void in
  78. expectation.fulfill()
  79. }
  80. waitForExpectationsWithTimeout(1, handler: nil)
  81. }
  82. }