Update more test cases for code coverage

This commit is contained in:
DreamPiggy 2019-04-26 23:32:48 +08:00
parent c1d58106dc
commit bf047b6e1b
7 changed files with 77 additions and 24 deletions

View File

@ -27,10 +27,6 @@ static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop coun
@implementation SDAnimatedImageTest
- (void)tearDown {
[[SDImageCache sharedImageCache] removeImageForKey:kTestGIFURL fromDisk:YES withCompletion:nil];
}
- (void)test01AnimatedImageInitWithData {
NSData *invalidData = [@"invalid data" dataUsingEncoding:NSUTF8StringEncoding];
SDAnimatedImage *image = [[SDAnimatedImage alloc] initWithData:invalidData];
@ -119,11 +115,16 @@ static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop coun
- (void)test10AnimatedImageInitWithCoder {
SDAnimatedImage *image1 = [SDAnimatedImage imageWithContentsOfFile:[self testGIFPath]];
expect(image1).notTo.beNil();
NSData *encodedData = [NSKeyedArchiver archivedDataWithRootObject:image1];
NSMutableData *encodedData = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:encodedData];
archiver.requiresSecureCoding = YES;
[archiver encodeObject:image1 forKey:NSKeyedArchiveRootObjectKey];
[archiver finishEncoding];
expect(encodedData).notTo.beNil();
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:encodedData];
unarchiver.requiresSecureCoding = YES;
SDAnimatedImage *image2 = [unarchiver decodeObjectOfClass:SDAnimatedImage.class forKey:NSKeyedArchiveRootObjectKey];
[unarchiver finishDecoding];
expect(image2).notTo.beNil();
// Check each property
@ -238,9 +239,11 @@ static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop coun
}
- (void)test23AnimatedImageViewCategoryProgressive {
XCTestExpectation *expectation = [self expectationWithDescription:@"test SDAnimatedImageView view category"];
XCTestExpectation *expectation = [self expectationWithDescription:@"test SDAnimatedImageView view category progressive"];
SDAnimatedImageView *imageView = [SDAnimatedImageView new];
NSURL *testURL = [NSURL URLWithString:kTestGIFURL];
[SDImageCache.sharedImageCache removeImageFromMemoryForKey:testURL.absoluteString];
[SDImageCache.sharedImageCache removeImageFromDiskForKey:testURL.absoluteString];
[imageView sd_setImageWithURL:testURL placeholderImage:nil options:SDWebImageProgressiveLoad progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *image = imageView.image;
@ -261,6 +264,20 @@ static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop coun
[self waitForExpectationsWithCommonTimeout];
}
- (void)test24AnimatedImageViewCategoryDiskCache {
XCTestExpectation *expectation = [self expectationWithDescription:@"test SDAnimatedImageView view category disk cache"];
SDAnimatedImageView *imageView = [SDAnimatedImageView new];
NSURL *testURL = [NSURL URLWithString:kTestGIFURL];
[SDImageCache.sharedImageCache removeImageFromMemoryForKey:testURL.absoluteString];
[imageView sd_setImageWithURL:testURL placeholderImage:nil completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
expect(error).to.beNil();
expect(image).notTo.beNil();
expect([image isKindOfClass:[SDAnimatedImage class]]).beTruthy();
[expectation fulfill];
}];
[self waitForExpectationsWithCommonTimeout];
}
#pragma mark - Helper
- (UIWindow *)window {
if (!_window) {

View File

@ -26,6 +26,7 @@
// Test invalid format
CFStringRef type = [NSData sd_UTTypeFromImageFormat:SDImageFormatUndefined];
expect(CFStringCompare(kUTTypePNG, type, 0)).equal(kCFCompareEqualTo);
expect([NSData sd_imageFormatFromUTType:kUTTypeImage]).equal(SDImageFormatUndefined);
}
- (void)test02UIImageMultiFormatCategory {
@ -39,6 +40,8 @@
// Test image encode PNG
data = [image sd_imageDataAsFormat:SDImageFormatPNG];
expect(data).notTo.beNil();
// Test image decode PNG
expect([UIImage sd_imageWithData:data scale:1 firstFrameOnly:YES]).notTo.beNil();
// Test image encode JPEG with compressionQuality
NSData *jpegData1 = [image sd_imageDataAsFormat:SDImageFormatJPEG compressionQuality:1];
NSData *jpegData2 = [image sd_imageDataAsFormat:SDImageFormatJPEG compressionQuality:0.5];

View File

@ -133,6 +133,7 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
XCTFail(@"Image should not be in cache");
}
}];
[[SDImageCache sharedImageCache] storeImageToMemory:image forKey:kTestImageKeyJPEG];
[[SDImageCache sharedImageCache] clearMemory];
expect([[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:kTestImageKeyJPEG]).to.beNil();
[self waitForExpectationsWithCommonTimeout];
@ -269,6 +270,7 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
NSData *imageData = [image sd_imageDataAsFormat:SDImageFormatJPEG];
[[SDImageCache sharedImageCache] storeImageDataToDisk:imageData forKey:kTestImageKeyJPEG];
expect([[SDImageCache sharedImageCache] diskImageDataExistsWithKey:kTestImageKeyJPEG]).beTruthy();
UIImage *storedImageFromMemory = [[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:kTestImageKeyJPEG];
expect(storedImageFromMemory).to.equal(nil);

View File

@ -122,11 +122,12 @@
}
- (void)test15ThatCodersManagerWorks {
SDImageCodersManager.sharedManager.coders = @[SDImageIOCoder.sharedCoder];
expect([SDImageCodersManager.sharedManager canDecodeFromData:nil]).beTruthy(); // Image/IO will return YES for future format
expect([SDImageCodersManager.sharedManager decodedImageWithData:nil options:nil]).beNil();
expect([SDImageCodersManager.sharedManager canEncodeToFormat:SDImageFormatWebP]).beFalsy();
expect([SDImageCodersManager.sharedManager encodedDataWithImage:nil format:SDImageFormatUndefined options:nil]).beNil();
SDImageCodersManager *manager = [[SDImageCodersManager alloc] init];
manager.coders = @[SDImageIOCoder.sharedCoder];
expect([manager canDecodeFromData:nil]).beTruthy(); // Image/IO will return YES for future format
expect([manager decodedImageWithData:nil options:nil]).beNil();
expect([manager canEncodeToFormat:SDImageFormatWebP]).beFalsy();
expect([manager encodedDataWithImage:nil format:SDImageFormatUndefined options:nil]).beNil();
}
- (void)verifyCoder:(id<SDImageCoder>)coder

View File

@ -8,15 +8,9 @@
*/
#import "SDTestCase.h"
#import "UIColor+HexString.h"
#import <CoreImage/CoreImage.h>
// Internal header
@interface UIColor (HexString)
@property (nonatomic, copy, readonly, nonnull) NSString *sd_hexString;
@end
@interface SDImageTransformerTests : SDTestCase
@property (nonatomic, strong) UIImage *testImage;

View File

@ -179,6 +179,24 @@
[self waitForExpectationsWithCommonTimeout];
}
- (void)test11ThatCancelAllDownloadWorks {
XCTestExpectation *expectation = [self expectationWithDescription:@"CancelAllDownloads"];
NSURL *imageURL = [NSURL URLWithString:kTestJPEGURL];
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:imageURL completed:nil];
expect([SDWebImageDownloader sharedDownloader].currentDownloadCount).to.equal(1);
[[SDWebImageDownloader sharedDownloader] cancelAllDownloads];
// doesn't cancel immediately - since it uses dispatch async
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, kMinDelayNanosecond), dispatch_get_main_queue(), ^{
expect([SDWebImageDownloader sharedDownloader].currentDownloadCount).to.equal(0);
[expectation fulfill];
});
[self waitForExpectationsWithCommonTimeout];
}
- (void)test12ThatWeCanUseAnotherSessionForEachDownloadOperation {
XCTestExpectation *expectation = [self expectationWithDescription:@"Owned session"];
NSURL *url = [NSURL URLWithString:kTestJPEGURL];
@ -461,15 +479,16 @@
- (void)test31ThatLoadersManagerWorks {
XCTestExpectation *expectation = [self expectationWithDescription:@"Loaders manager not works"];
SDWebImageTestLoader *loader = [[SDWebImageTestLoader alloc] init];
[[SDImageLoadersManager sharedManager] addLoader:loader];
[[SDImageLoadersManager sharedManager] removeLoader:loader];
[SDImageLoadersManager sharedManager].loaders = @[SDWebImageDownloader.sharedDownloader, loader];
SDImageLoadersManager *manager = [[SDImageLoadersManager alloc] init];
[manager addLoader:loader];
[manager removeLoader:loader];
manager.loaders = @[SDWebImageDownloader.sharedDownloader, loader];
NSURL *imageURL = [NSURL URLWithString:kTestJPEGURL];
expect([[SDImageLoadersManager sharedManager] canRequestImageForURL:imageURL]).beTruthy();
expect([manager canRequestImageForURL:imageURL]).beTruthy();
NSError *imageError = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorCancelled userInfo:nil];
expect([loader shouldBlockFailedURLWithURL:imageURL error:imageError]).equal(NO);
expect([manager shouldBlockFailedURLWithURL:imageURL error:imageError]).equal(NO);
[[SDImageLoadersManager sharedManager] requestImageWithURL:imageURL options:0 context:nil progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
[manager requestImageWithURL:imageURL options:0 context:nil progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
expect(targetURL).notTo.beNil();
} completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
expect(error).to.beNil();

View File

@ -9,6 +9,12 @@
#import "SDTestCase.h"
@interface SDWebImagePrefetcher ()
@property (strong, atomic, nonnull) NSMutableSet<SDWebImagePrefetchToken *> *runningTokens;
@end
@interface SDWebImagePrefetcherTests : SDTestCase <SDWebImagePrefetcherDelegate>
@property (nonatomic, strong) SDWebImagePrefetcher *prefetcher;
@ -134,6 +140,17 @@
[self waitForExpectationsWithTimeout:kAsyncTestTimeout * 20 handler:nil];
}
- (void)test06PrefetchCancelToken {
NSArray *imageURLs = @[@"http://via.placeholder.com/20x20.jpg",
@"http://via.placeholder.com/30x30.jpg",
@"http://via.placeholder.com/40x40.jpg"];
SDWebImagePrefetcher *prefetcher = [[SDWebImagePrefetcher alloc] init];
SDWebImagePrefetchToken *token = [prefetcher prefetchURLs:imageURLs];
expect(prefetcher.runningTokens.count).equal(1);
[token cancel];
expect(prefetcher.runningTokens.count).equal(0);
}
- (void)imagePrefetcher:(SDWebImagePrefetcher *)imagePrefetcher didFinishWithTotalCount:(NSUInteger)totalCount skippedCount:(NSUInteger)skippedCount {
expect(imagePrefetcher).to.equal(self.prefetcher);
self.skippedCount = skippedCount;