Repository: SDWebImage/SDWebImage
File: Tests/Tests/SDWebImageDecoderTests.m (Objective-C)

Code before:

- (void)test20ThatOurGIFCoderWorksNotFLAnimatedImage {
    NSURL *gifURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"TestImage" withExtension:@"gif"];
    NSData *gifData = [NSData dataWithContentsOfURL:gifURL];
    expect([[SDWebImageGIFCoder sharedCoder] canDecodeFromData:gifData]).to.beTruthy();
    // the IOCoder can also decode GIF
    expect([[SDWebImageImageIOCoder sharedCoder] canDecodeFromData:gifData]).to.beTruthy();
    UIImage *gifImage = [[SDWebImageGIFCoder sharedCoder] decodedImageWithData:gifData];
    expect(gifImage).toNot.beNil();
    expect(gifImage.images.count).to.beGreaterThan(0);
    CGSize imageSize = gifImage.size;
    CGFloat imageScale = gifImage.scale;
    [gifImage.images enumerateObjectsUsingBlock:^(UIImage * _Nonnull image, NSUInteger idx, BOOL * _Nonnull stop) {
        CGSize size = image.size;
        CGFloat scale = image.scale;
        expect(imageSize.width).to.equal(size.width);
        expect(imageSize.height).to.equal(size.height);
        expect(imageScale).to.equal(scale);
    }];
    
    expect([[SDWebImageGIFCoder sharedCoder] decompressedImageWithImage:gifImage data:nil  options:nil]).to.equal(gifImage);
    
    expect([[SDWebImageGIFCoder sharedCoder] canEncodeToFormat:SDImageFormatGIF]).to.beTruthy();
    expect([[SDWebImageImageIOCoder sharedCoder] canEncodeToFormat:SDImageFormatGIF]).to.beTruthy();
    NSData *outputData = [[SDWebImageGIFCoder sharedCoder] encodedDataWithImage:gifImage format:SDImageFormatGIF];
    expect(outputData).toNot.beNil();
}

@end


Review comment (suggestion):
Sorry...I forget this task. By the way, what about decode this `outputData` again and check frame count, image size and so on(these properties should equal to `gifImage`'s properties) ? This can ensure that it works for both decoding & encoding process :D

Code after:
    expect(inputImage).toNot.beNil();
    
    if (isAnimated) {
        // 2a - check images count > 0 (only for animated images)
        expect(inputImage.images.count).to.beGreaterThan(0);
        
        // 2b - check image size and scale for each frameImage (only for animated images)
        CGSize imageSize = inputImage.size;
        CGFloat imageScale = inputImage.scale;
        [inputImage.images enumerateObjectsUsingBlock:^(UIImage * frameImage, NSUInteger idx, BOOL * stop) {
            expect(imageSize).to.equal(frameImage.size);
            expect(imageScale).to.equal(frameImage.scale);
        }];
    }
    
    // 3 - check if we can encode to the original format
    expect([coder canEncodeToFormat:inputImageFormat]).to.beTruthy();
    
    // 4 - encode from UIImage to NSData using the inputImageFormat and check it
    NSData *outputImageData = [coder encodedDataWithImage:inputImage format:inputImageFormat];
    expect(outputImageData).toNot.beNil();
    UIImage *outputImage = [coder decodedImageWithData:outputImageData];
    expect(outputImage.size).to.equal(inputImage.size);
    expect(outputImage.scale).to.equal(inputImage.scale);
    expect(outputImage.images.count).to.equal(inputImage.images.count);
}

@end