From e669dee3c35ad2db8a73a67efd4a841de48b8234 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Fri, 1 Nov 2024 18:57:12 +0800 Subject: [PATCH] Added test case for animationTransformer --- SDWebImage/Core/SDAnimatedImageView.h | 6 ++--- Tests/Tests/SDAnimatedImageTest.m | 35 +++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/SDWebImage/Core/SDAnimatedImageView.h b/SDWebImage/Core/SDAnimatedImageView.h index 73141e38..aa315066 100644 --- a/SDWebImage/Core/SDAnimatedImageView.h +++ b/SDWebImage/Core/SDAnimatedImageView.h @@ -32,13 +32,13 @@ NS_SWIFT_UI_ACTOR /** The transformer for each decoded animated image frame. We supports post-transform on animated image frame from version 5.20. - When you configure the transformer on `SDAnimatedImageView` and animation is playing, the `transformedImageWithImage:forKey:` will be called just after the frame is decoded. (The `key` arg is always empty for backward-compatible) + When you configure the transformer on `SDAnimatedImageView` and animation is playing, the `transformedImageWithImage:forKey:` will be called just after the frame is decoded. (note: The `key` arg is always empty for backward-compatible and may be removed in the future) - Example to tint the animated image with alpha channel into template: + Example to tint the alpha animated image frame with a black color: * @code imageView.animationTransformer = [SDImageTintTransformer transformerWithColor:UIColor.blackColor]; * @endcode - @note The `transformerKey` property is used to ensure the buffer cache available. So make sure it's correct value match the actual logic on transformer. + @note The `transformerKey` property is used to ensure the buffer cache available. So make sure it's correct value match the actual logic on transformer. Which means, for the `same frame index + same transformer key`, the transformed image should always be the same. */ @property (nonatomic, strong, nullable) id animationTransformer; diff --git a/Tests/Tests/SDAnimatedImageTest.m b/Tests/Tests/SDAnimatedImageTest.m index 450f611e..d533453d 100644 --- a/Tests/Tests/SDAnimatedImageTest.m +++ b/Tests/Tests/SDAnimatedImageTest.m @@ -10,6 +10,7 @@ #import "SDTestCase.h" #import "SDInternalMacros.h" #import "SDImageFramePool.h" +#import "SDWebImageTestTransformer.h" #import static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop count @@ -808,6 +809,40 @@ static BOOL _isCalled; expect(scaledImage).notTo.equal(image); } +- (void)testAnimationTransformerWorks { + XCTestExpectation *expectation = [self expectationWithDescription:@"test SDAnimatedImageView animationTransformer works"]; + SDAnimatedImageView *imageView = [SDAnimatedImageView new]; + // Setup transformer, showing which hook all frames into the test image + UIImage *testImage = [[UIImage alloc] initWithData:[self testJPEGData]]; + SDWebImageTestTransformer *transformer = [SDWebImageTestTransformer new]; + transformer.testImage = testImage; + imageView.animationTransformer = transformer; + +#if SD_UIKIT + [self.window addSubview:imageView]; +#else + [self.window.contentView addSubview:imageView]; +#endif + SDAnimatedImage *image = [SDAnimatedImage imageWithData:[self testGIFData]]; + imageView.image = image; +#if SD_UIKIT + [imageView startAnimating]; +#else + imageView.animates = YES; +#endif + 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.player.framePool.currentFrameCount).beGreaterThan(0); + expect(imageView.currentFrameIndex).beGreaterThan(0); + // Test the current frame image is hooked by transformer + expect(imageView.currentFrame).equal(testImage); + + [expectation fulfill]; + }); + + [self waitForExpectationsWithCommonTimeout]; +} + #pragma mark - Helper - (NSString *)testGIFPath {