Explorar o código

Added test case for animationTransformer

DreamPiggy hai 1 ano
pai
achega
e669dee3c3
Modificáronse 2 ficheiros con 38 adicións e 3 borrados
  1. 3 3
      SDWebImage/Core/SDAnimatedImageView.h
  2. 35 0
      Tests/Tests/SDAnimatedImageTest.m

+ 3 - 3
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<SDImageTransformer> animationTransformer;
 

+ 35 - 0
Tests/Tests/SDAnimatedImageTest.m

@@ -10,6 +10,7 @@
 #import "SDTestCase.h"
 #import "SDInternalMacros.h"
 #import "SDImageFramePool.h"
+#import "SDWebImageTestTransformer.h"
 #import <KVOController/KVOController.h>
 
 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 {