Add the test cases for View Transition and View Indicator, both iOS && macOS

This commit is contained in:
DreamPiggy 2018-08-10 22:19:24 +08:00
parent 64a2453527
commit 7ee0d06cf7
3 changed files with 142 additions and 5 deletions

View File

@ -10,11 +10,6 @@
#import "SDTestCase.h"
#import <KVOController/KVOController.h>
#if SD_MAC
#define UIWindow NSWindow
#define UIScreen NSScreen
#endif
static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop count
// Internal header

View File

@ -14,6 +14,11 @@
#import <Expecta/Expecta.h>
#import <SDWebImage/SDWebImage.h>
#if SD_MAC
#define UIWindow NSWindow
#define UIScreen NSScreen
#endif
FOUNDATION_EXPORT const int64_t kAsyncTestTimeout;
FOUNDATION_EXPORT const int64_t kMinDelayNanosecond;
FOUNDATION_EXPORT NSString * _Nonnull const kTestJpegURL;

View File

@ -12,6 +12,8 @@
@interface SDWebCacheCategoriesTests : SDTestCase
@property (nonatomic, strong) UIWindow *window;
@end
@implementation SDWebCacheCategoriesTests
@ -119,6 +121,40 @@
}
#endif
#if SD_MAC
- (void)testNSButtonSetImageWithURL {
XCTestExpectation *expectation = [self expectationWithDescription:@"NSButton setImageWithURL"];
NSButton *button = [[NSButton alloc] init];
NSURL *originalImageURL = [NSURL URLWithString:kTestJpegURL];
[button sd_setImageWithURL:originalImageURL
completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
expect(image).toNot.beNil();
expect(error).to.beNil();
expect(originalImageURL).to.equal(imageURL);
expect(button.image).to.equal(image);
[expectation fulfill];
}];
[self waitForExpectationsWithCommonTimeout];
}
- (void)testNSButtonSetAlternateImageWithURL {
XCTestExpectation *expectation = [self expectationWithDescription:@"NSButton setAlternateImageWithURL"];
NSButton *button = [[NSButton alloc] init];
NSURL *originalImageURL = [NSURL URLWithString:kTestJpegURL];
[button sd_setAlternateImageWithURL:originalImageURL
completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
expect(image).toNot.beNil();
expect(error).to.beNil();
expect(originalImageURL).to.equal(imageURL);
expect(button.alternateImage).to.equal(image);
[expectation fulfill];
}];
[self waitForExpectationsWithCommonTimeout];
}
#endif
- (void)testUIViewImageProgressKVOWork {
XCTestExpectation *expectation = [self expectationWithDescription:@"UIView imageProgressKVO failed"];
UIView *view = [[UIView alloc] init];
@ -143,4 +179,105 @@
[self waitForExpectationsWithCommonTimeout];
}
- (void)testUIViewTransitionWork {
XCTestExpectation *expectation = [self expectationWithDescription:@"UIView transition does not work"];
// Attach a window, or CALayer will not submit drawing
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
imageView.sd_imageTransition = SDWebImageTransition.fadeTransition;
imageView.sd_imageTransition.duration = 1;
#if SD_UIKIT
[self.window addSubview:imageView];
#else
imageView.wantsLayer = YES;
[self.window.contentView addSubview:imageView];
#endif
UIImage *placeholder = [[UIImage alloc] initWithContentsOfFile:[self testJPEGPath]];
NSURL *originalImageURL = [NSURL URLWithString:kTestJpegURL];
__weak typeof(imageView) wimageView = imageView;
[imageView sd_setImageWithURL:originalImageURL
placeholderImage:placeholder
options:SDWebImageForceTransition
completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
__strong typeof(wimageView) simageView = imageView;
// Delay to let CALayer commit the transition in next runloop
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, kMinDelayNanosecond), dispatch_get_main_queue(), ^{
// Check current view contains layer animation
NSArray *animationKeys = simageView.layer.animationKeys;
expect(animationKeys.count).beGreaterThan(0);
[expectation fulfill];
});
}];
[self waitForExpectationsWithCommonTimeout];
}
- (void)testUIViewIndicatorWork {
XCTestExpectation *expectation = [self expectationWithDescription:@"UIView indicator does not work"];
UIImageView *imageView = [[UIImageView alloc] init];
imageView.sd_imageIndicator = SDWebImageActivityIndicator.grayLargeIndicator;
// Test setter trigger removeFromSuperView
imageView.sd_imageIndicator = SDWebImageProgressIndicator.defaultIndicator;
NSURL *originalImageURL = [NSURL URLWithString:kTestJpegURL];
__weak typeof(imageView) wimageView = imageView;
[imageView sd_setImageWithURL:originalImageURL
placeholderImage:nil options:SDWebImageFromLoaderOnly progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
dispatch_async(dispatch_get_main_queue(), ^{
__strong typeof(wimageView) simageView = imageView;
UIView *indicatorView = simageView.subviews.firstObject;
expect(indicatorView).equal(simageView.sd_imageIndicator.indicatorView);
if (receivedSize <= 0 || expectedSize <= 0) {
return;
}
// Current progess indicator update is called after progressBlock
double progress = 0;
double imageProgress = (double)receivedSize / (double)expectedSize;
#if SD_UIKIT
progress = ((UIProgressView *)simageView.sd_imageIndicator.indicatorView).progress;
#else
progress = ((NSProgressIndicator *)simageView.sd_imageIndicator.indicatorView).doubleValue / 100;
#endif
expect(progress).equal(imageProgress);
});
} completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
__strong typeof(wimageView) simageView = imageView;
double progress = 0;
#if SD_UIKIT
progress = ((UIProgressView *)simageView.sd_imageIndicator.indicatorView).progress;
#else
progress = ((NSProgressIndicator *)simageView.sd_imageIndicator.indicatorView).doubleValue / 100;
#endif
// Finish progress is 1
expect(progress).equal(1);
[expectation fulfill];
}];
[self waitForExpectationsWithCommonTimeout];
}
#pragma mark - Helper
- (UIWindow *)window {
if (!_window) {
UIScreen *mainScreen = [UIScreen mainScreen];
#if SD_UIKIT
_window = [[UIWindow alloc] initWithFrame:mainScreen.bounds];
#else
_window = [[NSWindow alloc] initWithContentRect:mainScreen.frame styleMask:0 backing:NSBackingStoreBuffered defer:NO screen:mainScreen];
#endif
}
return _window;
}
- (NSString *)testJPEGPath {
NSBundle *testBundle = [NSBundle bundleForClass:[self class]];
return [testBundle pathForResource:@"TestImage" ofType:@"jpg"];
}
@end