Merge pull request #2438 from dreampiggy/test_view_category

Add the test cases for View Transition and View Indicator, both iOS && macOS
This commit is contained in:
DreamPiggy 2018-08-17 14:46:42 +08:00 committed by GitHub
commit 26ebbba807
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 157 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,120 @@
[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)];
// Cover each convenience method
imageView.sd_imageTransition = SDWebImageTransition.fadeTransition;
imageView.sd_imageTransition = SDWebImageTransition.flipFromTopTransition;
imageView.sd_imageTransition = SDWebImageTransition.flipFromLeftTransition;
imageView.sd_imageTransition = SDWebImageTransition.flipFromBottomTransition;
imageView.sd_imageTransition = SDWebImageTransition.flipFromRightTransition;
imageView.sd_imageTransition = SDWebImageTransition.curlUpTransition;
imageView.sd_imageTransition = SDWebImageTransition.curlDownTransition;
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.grayIndicator;
// Cover each convience method, finally use progress indicator for test
imageView.sd_imageIndicator = SDWebImageActivityIndicator.grayLargeIndicator;
imageView.sd_imageIndicator = SDWebImageActivityIndicator.whiteIndicator;
imageView.sd_imageIndicator = SDWebImageActivityIndicator.whiteLargeIndicator;
#if SD_IOS
imageView.sd_imageIndicator = SDWebImageProgressIndicator.barIndicator;
#endif
imageView.sd_imageIndicator = SDWebImageProgressIndicator.defaultIndicator;
// Test setter trigger removeFromSuperView
expect(imageView.subviews.count).equal(1);
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;
}
// Base on current implementation, since we dispatch the progressBlock to main queue, the indicator's progress state should be synchonized
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