Authored by Bogdan Poplauschi

Tests for SDWebImageManager

@@ -14,6 +14,7 @@ @@ -14,6 +14,7 @@
14 14
15 #import "SDWebImageManager.h" 15 #import "SDWebImageManager.h"
16 16
  17 +NSString *workingImageURL = @"http://s3.amazonaws.com/fast-image-cache/demo-images/FICDDemoImage001.jpg";
17 18
18 @interface SDWebImageManagerTests : XCTestCase 19 @interface SDWebImageManagerTests : XCTestCase
19 20
@@ -21,22 +22,15 @@ @@ -21,22 +22,15 @@
21 22
22 @implementation SDWebImageManagerTests 23 @implementation SDWebImageManagerTests
23 24
24 -- (void)setUp  
25 -{  
26 - [super setUp];  
27 - // Put setup code here. This method is called before the invocation of each test method in the class. 25 +- (void)test01ThatSharedManagerIsNotEqualToInitManager {
  26 + SDWebImageManager *manager = [[SDWebImageManager alloc] init];
  27 + expect(manager).toNot.equal([SDWebImageManager sharedManager]);
28 } 28 }
29 29
30 -- (void)tearDown  
31 -{  
32 - // Put teardown code here. This method is called after the invocation of each test method in the class.  
33 - [super tearDown];  
34 -}  
35 -  
36 -- (void)testThatDownloadInvokesCompletionBlockWithCorrectParamsAsync { 30 +- (void)test02ThatDownloadInvokesCompletionBlockWithCorrectParamsAsync {
37 __block XCTestExpectation *expectation = [self expectationWithDescription:@"Image download completes"]; 31 __block XCTestExpectation *expectation = [self expectationWithDescription:@"Image download completes"];
38 32
39 - NSURL *originalImageURL = [NSURL URLWithString:@"http://s3.amazonaws.com/fast-image-cache/demo-images/FICDDemoImage001.jpg"]; 33 + NSURL *originalImageURL = [NSURL URLWithString:workingImageURL];
40 34
41 [[SDWebImageManager sharedManager] loadImageWithURL:originalImageURL 35 [[SDWebImageManager sharedManager] loadImageWithURL:originalImageURL
42 options:SDWebImageRefreshCached 36 options:SDWebImageRefreshCached
@@ -49,11 +43,12 @@ @@ -49,11 +43,12 @@
49 [expectation fulfill]; 43 [expectation fulfill];
50 expectation = nil; 44 expectation = nil;
51 }]; 45 }];
  46 + expect([[SDWebImageManager sharedManager] isRunning]).to.equal(YES);
52 47
53 [self waitForExpectationsWithTimeout:kAsyncTestTimeout handler:nil]; 48 [self waitForExpectationsWithTimeout:kAsyncTestTimeout handler:nil];
54 } 49 }
55 50
56 -- (void)testThatDownloadWithIncorrectURLInvokesCompletionBlockWithAnErrorAsync { 51 +- (void)test03ThatDownloadWithIncorrectURLInvokesCompletionBlockWithAnErrorAsync {
57 __block XCTestExpectation *expectation = [self expectationWithDescription:@"Image download completes"]; 52 __block XCTestExpectation *expectation = [self expectationWithDescription:@"Image download completes"];
58 53
59 NSURL *originalImageURL = [NSURL URLWithString:@"http://static2.dmcdn.net/static/video/656/177/44771656:jpeg_preview_small.png"]; 54 NSURL *originalImageURL = [NSURL URLWithString:@"http://static2.dmcdn.net/static/video/656/177/44771656:jpeg_preview_small.png"];
@@ -73,4 +68,49 @@ @@ -73,4 +68,49 @@
73 [self waitForExpectationsWithTimeout:kAsyncTestTimeout handler:nil]; 68 [self waitForExpectationsWithTimeout:kAsyncTestTimeout handler:nil];
74 } 69 }
75 70
  71 +- (void)test04CachedImageExistsForURL {
  72 + __block XCTestExpectation *expectation = [self expectationWithDescription:@"Image exists in cache"];
  73 + NSURL *imageURL = [NSURL URLWithString:workingImageURL];
  74 + [[SDWebImageManager sharedManager] cachedImageExistsForURL:imageURL completion:^(BOOL isInCache) {
  75 + if (isInCache) {
  76 + [expectation fulfill];
  77 + } else {
  78 + XCTFail(@"Image should be in cache");
  79 + }
  80 + }];
  81 + [self waitForExpectationsWithTimeout:kAsyncTestTimeout handler:nil];
  82 +}
  83 +
  84 +- (void)test05DiskImageExistsForURL {
  85 + __block XCTestExpectation *expectation = [self expectationWithDescription:@"Image exists in disk cache"];
  86 + NSURL *imageURL = [NSURL URLWithString:workingImageURL];
  87 + [[SDWebImageManager sharedManager] diskImageExistsForURL:imageURL completion:^(BOOL isInCache) {
  88 + if (isInCache) {
  89 + [expectation fulfill];
  90 + } else {
  91 + XCTFail(@"Image should be in cache");
  92 + }
  93 + }];
  94 + [self waitForExpectationsWithTimeout:kAsyncTestTimeout handler:nil];
  95 +}
  96 +
  97 +- (void)test06CancellAll {
  98 + XCTestExpectation *expectation = [self expectationWithDescription:@"Cancel"];
  99 +
  100 + NSURL *imageURL = [NSURL URLWithString:@"http://s3.amazonaws.com/fast-image-cache/demo-images/FICDDemoImage006.jpg"];
  101 + [[SDWebImageManager sharedManager] loadImageWithURL:imageURL options:0 progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
  102 + XCTFail(@"Should not get here");
  103 + }];
  104 +
  105 + [[SDWebImageManager sharedManager] cancelAll];
  106 +
  107 + // doesn't cancel immediately - since it uses dispatch async
  108 + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  109 + expect([[SDWebImageManager sharedManager] isRunning]).to.equal(NO);
  110 + [expectation fulfill];
  111 + });
  112 +
  113 + [self waitForExpectationsWithTimeout:kAsyncTestTimeout handler:nil];
  114 +}
  115 +
76 @end 116 @end