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

View File

@ -16,6 +16,12 @@ static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop coun
@interface SDAnimatedImageView ()
@property (nonatomic, assign) BOOL isProgressive;
@property (nonatomic, strong) SDAnimatedImagePlayer *player;
@end
@interface SDAnimatedImagePlayer ()
@property (nonatomic, strong) NSMutableDictionary<NSNumber *, UIImage *> *frameBuffer;
@end
@ -102,6 +108,11 @@ static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop coun
SDAnimatedImage *image = [SDAnimatedImage imageWithData:[self testGIFData]];
imageView.image = image;
expect(imageView.image).notTo.beNil();
#if SD_MAC
imageView.animates = YES;
#else
[imageView startAnimating];
#endif
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]];
imageView.image = image;
expect(imageView.image).notTo.beNil();
#if SD_MAC
imageView.animates = YES;
#else
[imageView startAnimating];
#endif
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(), ^{
// 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);
});
@ -322,7 +338,7 @@ static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop coun
#else
imageView.animates = NO;
#endif
expect(imageView.frameBuffer.count).beGreaterThan(0);
expect(imageView.player.frameBuffer.count).beGreaterThan(0);
expect(imageView.currentFrameIndex).beGreaterThan(0);
[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(), ^{
// 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);
});
@ -360,7 +376,7 @@ static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop coun
#else
imageView.animates = NO;
#endif
expect(imageView.frameBuffer.count).equal(0);
expect(imageView.player.frameBuffer.count).equal(0);
expect(imageView.currentFrameIndex).equal(0);
[imageView removeFromSuperview];

View File

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