From a02aa422560b97d5222a8a5a4b43ea3b8ca257c9 Mon Sep 17 00:00:00 2001 From: Matt Galloway Date: Mon, 15 Sep 2014 12:03:50 +0100 Subject: [PATCH] Switch to Xcode 6 async tests --- Tests/Tests/SDWebImageDownloaderTests.m | 40 +++++-------------------- 1 file changed, 8 insertions(+), 32 deletions(-) diff --git a/Tests/Tests/SDWebImageDownloaderTests.m b/Tests/Tests/SDWebImageDownloaderTests.m index 27cece8f..233711fa 100644 --- a/Tests/Tests/SDWebImageDownloaderTests.m +++ b/Tests/Tests/SDWebImageDownloaderTests.m @@ -32,21 +32,9 @@ [super tearDown]; } -- (BOOL)spinRunLoopWithTimeout:(NSTimeInterval)timeout untilBlockIsTrue:(BOOL(^)())block { - CFTimeInterval timeoutDate = CACurrentMediaTime() + 5.; - while (true) { - if (block()) { - return YES; - } - if (CACurrentMediaTime() > timeoutDate) { - return NO; - } - CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1., true); - } - return NO; -} - - (void)testThatDownloadingSameURLTwiceAndCancellingFirstWorks { + XCTestExpectation *expectation = [self expectationWithDescription:@"Correct image downloads"]; + NSURL *imageURL = [NSURL URLWithString:@"http://static2.dmcdn.net/static/video/656/177/44771656:jpeg_preview_small.jpg?20120509154705"]; id token1 = [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:imageURL @@ -57,27 +45,22 @@ }]; expect(token1).toNot.beNil(); - __block BOOL success = NO; id token2 = [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:imageURL options:0 progress:nil completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) { - success = YES; + [expectation fulfill]; }]; expect(token2).toNot.beNil(); [[SDWebImageDownloader sharedDownloader] cancel:token1]; - success = [self spinRunLoopWithTimeout:5. untilBlockIsTrue:^BOOL{ - return success; - }]; - - if (!success) { - XCTFail(@"Failed to download image"); - } + [self waitForExpectationsWithTimeout:5. handler:nil]; } - (void)testThatCancelingDownloadThenRequestingAgainWorks { + XCTestExpectation *expectation = [self expectationWithDescription:@"Correct image downloads"]; + NSURL *imageURL = [NSURL URLWithString:@"http://static2.dmcdn.net/static/video/656/177/44771656:jpeg_preview_small.jpg?20120509154705"]; id token1 = [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:imageURL @@ -90,22 +73,15 @@ [[SDWebImageDownloader sharedDownloader] cancel:token1]; - __block BOOL success = NO; id token2 = [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:imageURL options:0 progress:nil completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) { - success = YES; + [expectation fulfill]; }]; expect(token2).toNot.beNil(); - success = [self spinRunLoopWithTimeout:5. untilBlockIsTrue:^BOOL{ - return success; - }]; - - if (!success) { - XCTFail(@"Failed to download image"); - } + [self waitForExpectationsWithTimeout:5. handler:nil]; } @end