Update the test case to SDAnimatedImageView/Player

This commit is contained in:
DreamPiggy 2019-11-04 12:34:36 +08:00
parent 9461bd5c11
commit 4c7cbc6aa7
3 changed files with 34 additions and 5 deletions

View File

@ -44,7 +44,6 @@
// Get the current frame and loop count. // Get the current frame and loop count.
self.totalLoopCount = provider.animatedImageLoopCount; self.totalLoopCount = provider.animatedImageLoopCount;
self.animatedProvider = provider; self.animatedProvider = provider;
[self setupCurrentFrame];
#if SD_UIKIT #if SD_UIKIT
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMemoryWarning:) name:UIApplicationDidReceiveMemoryWarningNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMemoryWarning:) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
#endif #endif
@ -139,6 +138,9 @@
#pragma mark - State Control #pragma mark - State Control
- (void)setupCurrentFrame { - (void)setupCurrentFrame {
if (self.currentFrameIndex != 0) {
return;
}
if ([self.animatedProvider isKindOfClass:[UIImage class]]) { if ([self.animatedProvider isKindOfClass:[UIImage class]]) {
UIImage *image = (UIImage *)self.animatedProvider; UIImage *image = (UIImage *)self.animatedProvider;
// Use the poster image if available // Use the poster image if available
@ -149,7 +151,10 @@
#endif #endif
if (posterFrame) { if (posterFrame) {
self.currentFrame = posterFrame; self.currentFrame = posterFrame;
SD_LOCK(self.lock);
self.frameBuffer[@(self.currentFrameIndex)] = self.currentFrame; self.frameBuffer[@(self.currentFrameIndex)] = self.currentFrame;
SD_UNLOCK(self.lock);
[self handleFrameChange];
} }
} }
} }
@ -170,9 +175,16 @@
#pragma mark - Animation Control #pragma mark - Animation Control
- (void)startPlaying { - (void)startPlaying {
if (self.isPlaying) {
return;
}
[self.displayLink start]; [self.displayLink start];
// Calculate max buffer size // Calculate max buffer size
[self calculateMaxBufferCount]; [self calculateMaxBufferCount];
// Setup frame
if (self.currentFrameIndex == 0 && !self.currentFrame) {
[self setupCurrentFrame];
}
} }
- (void)stopPlaying { - (void)stopPlaying {

View File

@ -16,6 +16,12 @@ static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop coun
@interface SDAnimatedImageView () @interface SDAnimatedImageView ()
@property (nonatomic, assign) BOOL isProgressive; @property (nonatomic, assign) BOOL isProgressive;
@property (nonatomic, strong) SDAnimatedImagePlayer *player;
@end
@interface SDAnimatedImagePlayer ()
@property (nonatomic, strong) NSMutableDictionary<NSNumber *, UIImage *> *frameBuffer; @property (nonatomic, strong) NSMutableDictionary<NSNumber *, UIImage *> *frameBuffer;
@end @end
@ -102,6 +108,11 @@ static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop coun
SDAnimatedImage *image = [SDAnimatedImage imageWithData:[self testGIFData]]; SDAnimatedImage *image = [SDAnimatedImage imageWithData:[self testGIFData]];
imageView.image = image; imageView.image = image;
expect(imageView.image).notTo.beNil(); expect(imageView.image).notTo.beNil();
#if SD_MAC
imageView.animates = YES;
#else
[imageView startAnimating];
#endif
expect(imageView.currentFrame).notTo.beNil(); // current frame expect(imageView.currentFrame).notTo.beNil(); // current frame
} }
@ -110,6 +121,11 @@ static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop coun
SDAnimatedImage *image = [SDAnimatedImage imageWithData:[self testAPNGPData]]; SDAnimatedImage *image = [SDAnimatedImage imageWithData:[self testAPNGPData]];
imageView.image = image; imageView.image = image;
expect(imageView.image).notTo.beNil(); expect(imageView.image).notTo.beNil();
#if SD_MAC
imageView.animates = YES;
#else
[imageView startAnimating];
#endif
expect(imageView.currentFrame).notTo.beNil(); // current frame expect(imageView.currentFrame).notTo.beNil(); // current frame
} }
@ -312,7 +328,7 @@ static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop coun
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 0.5s is not finished, frame index should not be 0 // 0.5s is not finished, frame index should not be 0
expect(imageView.frameBuffer.count).beGreaterThan(0); expect(imageView.player.frameBuffer.count).beGreaterThan(0);
expect(imageView.currentFrameIndex).beGreaterThan(0); expect(imageView.currentFrameIndex).beGreaterThan(0);
}); });
@ -322,7 +338,7 @@ static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop coun
#else #else
imageView.animates = NO; imageView.animates = NO;
#endif #endif
expect(imageView.frameBuffer.count).beGreaterThan(0); expect(imageView.player.frameBuffer.count).beGreaterThan(0);
expect(imageView.currentFrameIndex).beGreaterThan(0); expect(imageView.currentFrameIndex).beGreaterThan(0);
[imageView removeFromSuperview]; [imageView removeFromSuperview];
@ -350,7 +366,7 @@ static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop coun
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 0.5s is not finished, frame index should not be 0 // 0.5s is not finished, frame index should not be 0
expect(imageView.frameBuffer.count).beGreaterThan(0); expect(imageView.player.frameBuffer.count).beGreaterThan(0);
expect(imageView.currentFrameIndex).beGreaterThan(0); expect(imageView.currentFrameIndex).beGreaterThan(0);
}); });
@ -360,7 +376,7 @@ static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop coun
#else #else
imageView.animates = NO; imageView.animates = NO;
#endif #endif
expect(imageView.frameBuffer.count).equal(0); expect(imageView.player.frameBuffer.count).equal(0);
expect(imageView.currentFrameIndex).equal(0); expect(imageView.currentFrameIndex).equal(0);
[imageView removeFromSuperview]; [imageView removeFromSuperview];

View File

@ -50,6 +50,7 @@
expect(duration).equal(1.0 / 60); expect(duration).equal(1.0 / 60);
[displayLink addToRunLoop:NSRunLoop.mainRunLoop forMode:NSRunLoopCommonModes]; [displayLink addToRunLoop:NSRunLoop.mainRunLoop forMode:NSRunLoopCommonModes];
[displayLink start]; [displayLink start];
expect(displayLink.isRunning).beTruthy();
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
expect(displayLink.isRunning).beTruthy(); expect(displayLink.isRunning).beTruthy();
[displayLink stop]; [displayLink stop];