Repository: SDWebImage/SDWebImage
File: Tests/Tests/SDWebImageDownloaderTests.m (Objective-C)

Code before:
    [self waitForExpectationsWithCommonTimeout];
}

- (void)test05ThatSetAndGetMaxConcurrentDownloadsWorks {
    NSInteger initialValue = [SDWebImageDownloader sharedDownloader].maxConcurrentDownloads;
    
    [[SDWebImageDownloader sharedDownloader] setMaxConcurrentDownloads:3];
    expect([SDWebImageDownloader sharedDownloader].maxConcurrentDownloads).to.equal(3);
    
    [[SDWebImageDownloader sharedDownloader] setMaxConcurrentDownloads:initialValue];
}

- (void)test06ThatUsingACustomDownloaderOperationWorks {
    // we try to set a usual NSOperation as operation class. Should not work
    [[SDWebImageDownloader sharedDownloader] setOperationClass:[NSOperation class]];
    expect([SDWebImageDownloader sharedDownloader].operationClass).to.equal([SDWebImageDownloaderOperation class]);
    
    // setting an NSOperation subclass that conforms to SDWebImageDownloaderOperationInterface - should work
    [[SDWebImageDownloader sharedDownloader] setOperationClass:[CustomDownloaderOperation class]];
    expect([SDWebImageDownloader sharedDownloader].operationClass).to.equal([CustomDownloaderOperation class]);
    
    // back to the original value
    [[SDWebImageDownloader sharedDownloader] setOperationClass:nil];
    expect([SDWebImageDownloader sharedDownloader].operationClass).to.equal([SDWebImageDownloaderOperation class]);
}

- (void)test07ThatAddProgressCallbackCompletedBlockWithNilURLCallsTheCompletionBlockWithNils {
    XCTestExpectation *expectation = [self expectationWithDescription:@"Completion is called with nils"];
    [[SDWebImageDownloader sharedDownloader] addProgressCallback:nil completedBlock:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
        if (!image && !data && !error) {
            [expectation fulfill];
        } else {
            XCTFail(@"All params should be nil");
        }
    } forURL:nil createCallback:nil];
    [self waitForExpectationsWithTimeout:0.5 handler:nil];
}

- (void)test08ThatAHTTPAuthDownloadWorks {
    XCTestExpectation *expectation = [self expectationWithDescription:@"HTTP Auth download"];
    [SDWebImageDownloader sharedDownloader].username = @"httpwatch";
    [SDWebImageDownloader sharedDownloader].password = @"httpwatch01";
    NSURL *imageURL = [NSURL URLWithString:@"http://www.httpwatch.com/httpgallery/authentication/authenticatedimage/default.aspx?0.35786508303135633"];
    [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:imageURL options:0 progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
        if (image && data && !error && finished) {
            [expectation fulfill];
        } else {
            XCTFail(@"Something went wrong");
        }
    }];
    [self waitForExpectationsWithCommonTimeout];

Review comment (suggestion):
I think, if we change the method to just `createDownloaderOperationWithUrl`. The `url` arg should be nonnull, because when the url is nil, the caller method `downloadImageWithURL:` already handle this case and earily return the response. So actually this should be removed.

Code after:

- (void)test05ThatSetAndGetMaxConcurrentDownloadsWorks {
    NSInteger initialValue = [SDWebImageDownloader sharedDownloader].maxConcurrentDownloads;
    
    [[SDWebImageDownloader sharedDownloader] setMaxConcurrentDownloads:3];
    expect([SDWebImageDownloader sharedDownloader].maxConcurrentDownloads).to.equal(3);
    
    [[SDWebImageDownloader sharedDownloader] setMaxConcurrentDownloads:initialValue];
}

- (void)test06ThatUsingACustomDownloaderOperationWorks {
    // we try to set a usual NSOperation as operation class. Should not work
    [[SDWebImageDownloader sharedDownloader] setOperationClass:[NSOperation class]];
    expect([SDWebImageDownloader sharedDownloader].operationClass).to.equal([SDWebImageDownloaderOperation class]);
    
    // setting an NSOperation subclass that conforms to SDWebImageDownloaderOperationInterface - should work
    [[SDWebImageDownloader sharedDownloader] setOperationClass:[CustomDownloaderOperation class]];
    expect([SDWebImageDownloader sharedDownloader].operationClass).to.equal([CustomDownloaderOperation class]);
    
    // back to the original value
    [[SDWebImageDownloader sharedDownloader] setOperationClass:nil];
    expect([SDWebImageDownloader sharedDownloader].operationClass).to.equal([SDWebImageDownloaderOperation class]);
}

- (void)test07ThatCreateDownloaderOperationWithNilUrl {
    [[SDWebImageDownloader sharedDownloader] createDownloaderOperationWithUrl:nil options:0];
}

- (void)test08ThatAHTTPAuthDownloadWorks {
    XCTestExpectation *expectation = [self expectationWithDescription:@"HTTP Auth download"];
    [SDWebImageDownloader sharedDownloader].username = @"httpwatch";
    [SDWebImageDownloader sharedDownloader].password = @"httpwatch01";
    NSURL *imageURL = [NSURL URLWithString:@"http://www.httpwatch.com/httpgallery/authentication/authenticatedimage/default.aspx?0.35786508303135633"];
    [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:imageURL options:0 progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
        if (image && data && !error && finished) {
            [expectation fulfill];
        } else {
            XCTFail(@"Something went wrong");
        }
    }];
    [self waitForExpectationsWithCommonTimeout];
    [SDWebImageDownloader sharedDownloader].username = nil;
    [SDWebImageDownloader sharedDownloader].password = nil;
}

- (void)test09ThatProgressiveJPEGWorks {
    XCTestExpectation *expectation = [self expectationWithDescription:@"Progressive JPEG download"];
    NSURL *imageURL = [NSURL URLWithString:kTestJpegURL];
    [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:imageURL options:SDWebImageDownloaderProgressiveDownload progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
        if (image && data && !error && finished) {
            [expectation fulfill];