Tests for SDWebImageManager

This commit is contained in:
Bogdan Poplauschi 2016-09-25 15:17:21 +03:00
parent fbca6a6dc3
commit ea0bd97d10
1 changed files with 53 additions and 13 deletions

View File

@ -14,6 +14,7 @@
#import "SDWebImageManager.h"
NSString *workingImageURL = @"http://s3.amazonaws.com/fast-image-cache/demo-images/FICDDemoImage001.jpg";
@interface SDWebImageManagerTests : XCTestCase
@ -21,22 +22,15 @@
@implementation SDWebImageManagerTests
- (void)setUp
{
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
- (void)test01ThatSharedManagerIsNotEqualToInitManager {
SDWebImageManager *manager = [[SDWebImageManager alloc] init];
expect(manager).toNot.equal([SDWebImageManager sharedManager]);
}
- (void)tearDown
{
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}
- (void)testThatDownloadInvokesCompletionBlockWithCorrectParamsAsync {
- (void)test02ThatDownloadInvokesCompletionBlockWithCorrectParamsAsync {
__block XCTestExpectation *expectation = [self expectationWithDescription:@"Image download completes"];
NSURL *originalImageURL = [NSURL URLWithString:@"http://s3.amazonaws.com/fast-image-cache/demo-images/FICDDemoImage001.jpg"];
NSURL *originalImageURL = [NSURL URLWithString:workingImageURL];
[[SDWebImageManager sharedManager] loadImageWithURL:originalImageURL
options:SDWebImageRefreshCached
@ -49,11 +43,12 @@
[expectation fulfill];
expectation = nil;
}];
expect([[SDWebImageManager sharedManager] isRunning]).to.equal(YES);
[self waitForExpectationsWithTimeout:kAsyncTestTimeout handler:nil];
}
- (void)testThatDownloadWithIncorrectURLInvokesCompletionBlockWithAnErrorAsync {
- (void)test03ThatDownloadWithIncorrectURLInvokesCompletionBlockWithAnErrorAsync {
__block XCTestExpectation *expectation = [self expectationWithDescription:@"Image download completes"];
NSURL *originalImageURL = [NSURL URLWithString:@"http://static2.dmcdn.net/static/video/656/177/44771656:jpeg_preview_small.png"];
@ -73,4 +68,49 @@
[self waitForExpectationsWithTimeout:kAsyncTestTimeout handler:nil];
}
- (void)test04CachedImageExistsForURL {
__block XCTestExpectation *expectation = [self expectationWithDescription:@"Image exists in cache"];
NSURL *imageURL = [NSURL URLWithString:workingImageURL];
[[SDWebImageManager sharedManager] cachedImageExistsForURL:imageURL completion:^(BOOL isInCache) {
if (isInCache) {
[expectation fulfill];
} else {
XCTFail(@"Image should be in cache");
}
}];
[self waitForExpectationsWithTimeout:kAsyncTestTimeout handler:nil];
}
- (void)test05DiskImageExistsForURL {
__block XCTestExpectation *expectation = [self expectationWithDescription:@"Image exists in disk cache"];
NSURL *imageURL = [NSURL URLWithString:workingImageURL];
[[SDWebImageManager sharedManager] diskImageExistsForURL:imageURL completion:^(BOOL isInCache) {
if (isInCache) {
[expectation fulfill];
} else {
XCTFail(@"Image should be in cache");
}
}];
[self waitForExpectationsWithTimeout:kAsyncTestTimeout handler:nil];
}
- (void)test06CancellAll {
XCTestExpectation *expectation = [self expectationWithDescription:@"Cancel"];
NSURL *imageURL = [NSURL URLWithString:@"http://s3.amazonaws.com/fast-image-cache/demo-images/FICDDemoImage006.jpg"];
[[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) {
XCTFail(@"Should not get here");
}];
[[SDWebImageManager sharedManager] cancelAll];
// doesn't cancel immediately - since it uses dispatch async
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
expect([[SDWebImageManager sharedManager] isRunning]).to.equal(NO);
[expectation fulfill];
});
[self waitForExpectationsWithTimeout:kAsyncTestTimeout handler:nil];
}
@end