Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongwuzw committed Jul 10, 2019
1 parent c170a9f commit 5d58a20
Showing 1 changed file with 64 additions and 1 deletion.
65 changes: 64 additions & 1 deletion Tests/PINRemoteImageTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#import <objc/runtime.h>

static BOOL requestRetried = NO;
extern NSString * const PINRemoteImageCacheKey;

static inline BOOL PINImageAlphaInfoIsOpaque(CGImageAlphaInfo info) {
switch (info) {
Expand Down Expand Up @@ -59,6 +60,33 @@ - (void)swizzled_scheduleDownloadWithRequest:(NSURLRequest *)request
@end

#if DEBUG

@interface PINRemoteImageWeakContainer : NSObject

@property (nonatomic, readonly, weak) id target;

+ (PINRemoteImageWeakContainer *)weakContainerWithTarget:(id)target;
- (instancetype)initWithTarget:(id)target;

@end

@implementation PINRemoteImageWeakContainer

+ (PINRemoteImageWeakContainer *)weakContainerWithTarget:(id)target
{
return [[self alloc] initWithTarget:target];
}

- (instancetype)initWithTarget:(id)target
{
if (self) {
_target = target;
}
return self;
}

@end

@interface PINRemoteImageManager ()

@property (nonatomic, strong) PINURLSessionManager *sessionManager;
Expand All @@ -81,8 +109,10 @@ @interface PINRemoteImage_Tests : XCTestCase <PINURLSessionManagerDelegate>
@property (nonatomic, strong) PINRemoteImageManager *imageManager;
@property (nonatomic, strong) NSMutableData *data;
@property (nonatomic, strong) NSURLSessionTask *task;
@property (nonatomic, strong) PINRemoteImageDownloadTask *firstDownloadTask;
@property (nonatomic, strong) PINRemoteImageDownloadTask *secondDownloadTask;
@property (nonatomic, strong) NSError *error;

@property (nonatomic, strong) dispatch_semaphore_t semaphore;
@end

@interface PINSpeedRecorder ()
Expand Down Expand Up @@ -204,6 +234,18 @@ - (NSURL *)imageFrom404URL
- (void)didReceiveData:(NSData *)data forTask:(NSURLSessionTask *)task
{
self.task = task;
if (self.semaphore) {
PINRemoteImageWeakContainer *container = [NSURLProtocol propertyForKey:PINRemoteImageCacheKey inRequest:task.originalRequest];
PINRemoteImageTask *task = (PINRemoteImageDownloadTask *)container.target;
if (!self.firstDownloadTask) {
self.firstDownloadTask = (PINRemoteImageDownloadTask *)task;
dispatch_semaphore_signal(self.semaphore);
}
if (self.firstDownloadTask && self.firstDownloadTask != task && !self.secondDownloadTask) {
self.secondDownloadTask = (PINRemoteImageDownloadTask *)task;
dispatch_semaphore_signal(self.semaphore);
}
}
}

- (void)didCompleteTask:(NSURLSessionTask *)task withError:(NSError *)error
Expand Down Expand Up @@ -1381,6 +1423,27 @@ - (void)testRetry
method_exchangeImplementations(originalMethod, swizzledMethod);
}

- (void)testDownloadTaskWhenReDownload
{
self.semaphore = dispatch_semaphore_create(0);
self.imageManager = [[PINRemoteImageManager alloc] init];
self.imageManager.sessionManager.delegate = self;

PINRemoteImageTask *firstTask = nil;
NSUUID *firstUUID = [self.imageManager downloadImageWithURL:[self transparentPNGURL] options:0 progressDownload:nil completion:nil];
dispatch_semaphore_wait(self.semaphore, [self timeout]);
firstTask = self.firstDownloadTask;

[self.imageManager cancelTaskWithUUID:firstUUID];

PINRemoteImageTask *secondTask = nil;
[self.imageManager downloadImageWithURL:[self transparentPNGURL] options:0 progressDownload:nil completion:nil];
dispatch_semaphore_wait(self.semaphore, [self timeout]);
secondTask = self.secondDownloadTask;

XCTAssert(firstTask != secondTask);
}

@end

@implementation PINRemoteImageDownloadTask (Swizzled)
Expand Down

0 comments on commit 5d58a20

Please sign in to comment.