Update the test and description to make it more clear
This commit is contained in:
parent
1ef45bace1
commit
3f3a309fb4
|
@ -21,9 +21,9 @@ FOUNDATION_EXPORT NSString * _Nonnull const SDWebImageInternalSetImageGroupKey;
|
|||
*/
|
||||
FOUNDATION_EXPORT NSString * _Nonnull const SDWebImageExternalCustomManagerKey;
|
||||
/**
|
||||
The value specify that the image progress unit count cannot be determined because the progressBlock is not been called. This value is actually 1
|
||||
The value specify that the image progress unit count cannot be determined because the progressBlock is not been called.
|
||||
*/
|
||||
FOUNDATION_EXPORT const int64_t SDWebImageProgressUnitCountUnknown;
|
||||
FOUNDATION_EXPORT const int64_t SDWebImageProgressUnitCountUnknown; /* 1LL */
|
||||
|
||||
typedef void(^SDSetImageBlock)(UIImage * _Nullable image, NSData * _Nullable imageData);
|
||||
|
||||
|
@ -37,9 +37,9 @@ typedef void(^SDSetImageBlock)(UIImage * _Nullable image, NSData * _Nullable ima
|
|||
- (nullable NSURL *)sd_imageURL;
|
||||
|
||||
/**
|
||||
* The current image loading progress associated to the view. The unit count is the received size and excepted size of download
|
||||
* The `completedUnitCount` will be reset to 0 after a new image loading start. And `completedUnitCount` will be `SDWebImageProgressUnitCountUnknown` if the progressBlock not been called but the image loading success to mark the progress finished.
|
||||
* @note You can use Key-Value Observing on the progress, but you should take care that the change to progress is from a background queue(the same as progressBlock). If you want to using KVO and update the UI, make sure to dispatch on the main queue. And it's recommand to use some KVO libs like KVOController because it's more safe and easy to use.
|
||||
* The current image loading progress associated to the view. The unit count is the received size and excepted size of download.
|
||||
* The `totalUnitCount` and `completedUnitCount` will be reset to 0 after a new image loading start (change from current queue). And they will be set to `SDWebImageProgressUnitCountUnknown` if the progressBlock not been called but the image loading success to mark the progress finished (change from main queue).
|
||||
* @note You can use Key-Value Observing on the progress, but you should take care that the change to progress is from a background queue during download(the same as progressBlock). If you want to using KVO and update the UI, make sure to dispatch on the main queue. And it's recommand to use some KVO libs like KVOController because it's more safe and easy to use.
|
||||
* @note The getter will create a progress instance if the value is nil. You can also set a custom progress instance and let it been updated during image loading
|
||||
* @note Note that because of the limitations of categories this property can get out of sync if you update the progress directly.
|
||||
*/
|
||||
|
@ -100,7 +100,7 @@ typedef void(^SDSetImageBlock)(UIImage * _Nullable image, NSData * _Nullable ima
|
|||
context:(nullable NSDictionary *)context;
|
||||
|
||||
/**
|
||||
* Cancel the current download
|
||||
* Cancel the current image load
|
||||
*/
|
||||
- (void)sd_cancelCurrentImageLoad;
|
||||
|
||||
|
|
|
@ -35,7 +35,7 @@ static char TAG_ACTIVITY_SHOW;
|
|||
- (NSProgress *)sd_imageProgress {
|
||||
NSProgress *progress = objc_getAssociatedObject(self, @selector(sd_imageProgress));
|
||||
if (!progress) {
|
||||
progress = [[NSProgress alloc] init];
|
||||
progress = [[NSProgress alloc] initWithParent:nil userInfo:nil];
|
||||
self.sd_imageProgress = progress;
|
||||
}
|
||||
return progress;
|
||||
|
@ -84,6 +84,7 @@ static char TAG_ACTIVITY_SHOW;
|
|||
}
|
||||
|
||||
// reset the progress
|
||||
self.sd_imageProgress.totalUnitCount = 0;
|
||||
self.sd_imageProgress.completedUnitCount = 0;
|
||||
|
||||
SDWebImageManager *manager;
|
||||
|
@ -106,7 +107,8 @@ static char TAG_ACTIVITY_SHOW;
|
|||
if (!sself) { return; }
|
||||
[sself sd_removeActivityIndicator];
|
||||
// if the progress not been updated, mark it to complete state
|
||||
if (finished && !error && sself.sd_imageProgress.completedUnitCount == 0) {
|
||||
if (finished && !error && sself.sd_imageProgress.totalUnitCount == 0 && sself.sd_imageProgress.completedUnitCount == 0) {
|
||||
sself.sd_imageProgress.totalUnitCount = SDWebImageProgressUnitCountUnknown;
|
||||
sself.sd_imageProgress.completedUnitCount = SDWebImageProgressUnitCountUnknown;
|
||||
}
|
||||
BOOL shouldCallCompletedBlock = finished || (options & SDWebImageAvoidAutoSetImage);
|
||||
|
|
|
@ -15,6 +15,8 @@
|
|||
#import <SDWebImage/FLAnimatedImageView+WebCache.h>
|
||||
#import <SDWebImage/UIView+WebCache.h>
|
||||
|
||||
static void * SDCategoriesTestsContext = &SDCategoriesTestsContext;
|
||||
|
||||
@interface SDCategoriesTests : SDTestCase
|
||||
|
||||
@end
|
||||
|
@ -138,32 +140,35 @@
|
|||
[self waitForExpectationsWithCommonTimeout];
|
||||
}
|
||||
|
||||
- (void)testUIViewImageProgressWorkWithKVO {
|
||||
- (void)testUIViewImageProgressKVOWork {
|
||||
XCTestExpectation *expectation = [self expectationWithDescription:@"UIView imageProgressKVO failed"];
|
||||
UIView *view = [[UIView alloc] init];
|
||||
NSURL *originalImageURL = [NSURL URLWithString:kTestJpegURL];
|
||||
|
||||
[view.sd_imageProgress addObserver:self forKeyPath:NSStringFromSelector(@selector(fractionCompleted)) options:NSKeyValueObservingOptionNew context:_cmd];
|
||||
[view.sd_imageProgress addObserver:self forKeyPath:NSStringFromSelector(@selector(fractionCompleted)) options:NSKeyValueObservingOptionNew context:SDCategoriesTestsContext];
|
||||
|
||||
// Clear the disk cache to force download from network
|
||||
[[SDImageCache sharedImageCache] removeImageForKey:kTestJpegURL withCompletion:^{
|
||||
[view sd_internalSetImageWithURL:originalImageURL placeholderImage:nil options:0 operationKey:nil setImageBlock:nil progress:nil completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
|
||||
expect(view.sd_imageProgress.fractionCompleted).equal(1.0);
|
||||
expect([view.sd_imageProgress.userInfo[NSStringFromSelector(_cmd)] boolValue]).equal(YES);
|
||||
[expectation fulfill];
|
||||
}];
|
||||
}];
|
||||
[self waitForExpectationsWithTimeout:kAsyncTestTimeout handler:^(NSError * _Nullable error) {
|
||||
[view.sd_imageProgress removeObserver:self forKeyPath:NSStringFromSelector(@selector(fractionCompleted)) context:_cmd];
|
||||
[view.sd_imageProgress removeObserver:self forKeyPath:NSStringFromSelector(@selector(fractionCompleted)) context:SDCategoriesTestsContext];
|
||||
}];
|
||||
}
|
||||
|
||||
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
|
||||
{
|
||||
if (context == @selector(testUIViewImageProgressWorkWithKVO)) {
|
||||
if (context == SDCategoriesTestsContext) {
|
||||
if ([keyPath isEqualToString:NSStringFromSelector(@selector(fractionCompleted))]) {
|
||||
NSProgress *progress = object;
|
||||
NSNumber *completedValue = change[NSKeyValueChangeNewKey];
|
||||
expect(progress.fractionCompleted).equal(completedValue.doubleValue);
|
||||
// mark that KVO is called
|
||||
[progress setUserInfoObject:@(YES) forKey:NSStringFromSelector(@selector(testUIViewImageProgressKVOWork))];
|
||||
}
|
||||
} else {
|
||||
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
#import <Expecta/Expecta.h>
|
||||
|
||||
FOUNDATION_EXPORT const int64_t kAsyncTestTimeout;
|
||||
FOUNDATION_EXPORT const int64_t kMinDelayNanosecond;
|
||||
FOUNDATION_EXPORT NSString * _Nonnull const kTestJpegURL;
|
||||
FOUNDATION_EXPORT NSString * _Nonnull const kTestPNGURL;
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
#import "SDTestCase.h"
|
||||
|
||||
const int64_t kAsyncTestTimeout = 5;
|
||||
const int64_t kMinDelayNanosecond = NSEC_PER_MSEC * 100; // 0.1s
|
||||
NSString *const kTestJpegURL = @"http://via.placeholder.com/50x50.jpg";
|
||||
NSString *const kTestPNGURL = @"http://via.placeholder.com/50x50.png";
|
||||
|
||||
|
|
|
@ -186,7 +186,7 @@
|
|||
[[SDWebImageDownloader sharedDownloader] cancel:token];
|
||||
|
||||
// doesn't cancel immediately - since it uses dispatch async
|
||||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
||||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, kMinDelayNanosecond), dispatch_get_main_queue(), ^{
|
||||
expect([SDWebImageDownloader sharedDownloader].currentDownloadCount).to.equal(0);
|
||||
[expectation fulfill];
|
||||
});
|
||||
|
|
|
@ -100,7 +100,7 @@
|
|||
[[SDWebImageManager sharedManager] cancelAll];
|
||||
|
||||
// doesn't cancel immediately - since it uses dispatch async
|
||||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
||||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, kMinDelayNanosecond), dispatch_get_main_queue(), ^{
|
||||
expect([[SDWebImageManager sharedManager] isRunning]).to.equal(NO);
|
||||
[expectation fulfill];
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue