Browse Source

Fix tests

onevcat 9 years ago
parent
commit
905c8411ff
2 changed files with 27 additions and 18 deletions
  1. 8 6
      Sources/Image.swift
  2. 19 12
      Tests/KingfisherTests/ImageExtensionTests.swift

+ 8 - 6
Sources/Image.swift

@@ -188,10 +188,10 @@ func ImageJPEGRepresentation(image: Image, _ compressionQuality: CGFloat) -> NSD
 
 // MARK: - GIF
 func ImageGIFRepresentation(image: Image) -> NSData? {
-#if os(iOS)
-    return image.kf_animatedImageData
-#else
+#if os(OSX)
     return ImageGIFRepresentation(image, duration: 0.0, repeatCount: 0)
+#else
+    return image.kf_animatedImageData
 #endif
 }
 
@@ -283,11 +283,13 @@ extension Image {
             guard let (images, gifDuration) = decodeFromSource(imageSource, options: options) else {
                 return nil
             }
-            return Image.kf_animatedImageWithImages(images, duration: duration <= 0.0 ? gifDuration : duration)
+            let image = Image.kf_animatedImageWithImages(images, duration: duration <= 0.0 ? gifDuration : duration)
+            image?.kf_animatedImageData = data
+            return image
         } else {
             let image = Image(data: data)
-            image?.kf_imageSource = ImageSource(ref: imageSource)
             image?.kf_animatedImageData = data
+            image?.kf_imageSource = ImageSource(ref: imageSource)
             return image
         }
 #endif
@@ -303,7 +305,7 @@ extension Image {
             switch data.kf_imageFormat {
             case .JPEG: image = Image(data: data)
             case .PNG: image = Image(data: data)
-            case .GIF: image = Image.kf_animatedImageWithGIFData(gifData: data, scale: scale, duration: 0.0)
+            case .GIF: image = Image.kf_animatedImageWithGIFData(gifData: data, scale: scale, duration: 0.0, preloadAll: preloadAllGIFData)
             case .Unknown: image = Image(data: data)
             }
         #else

+ 19 - 12
Tests/KingfisherTests/ImageExtensionTests.swift

@@ -56,9 +56,9 @@ class ImageExtensionTests: XCTestCase {
     }
     
     func testGenerateGIFImage() {
-        let image = Image.kf_animatedImageWithGIFData(gifData: testImageGIFData)
+        let image = Image.kf_animatedImageWithGIFData(gifData: testImageGIFData, preloadAll: false)
         XCTAssertNotNil(image, "The image should be initiated.")
-#if os(iOS)
+#if os(iOS) || os(tvOS)
         let count = ImagesCountWithImageSource(image!.kf_imageSource!.imageRef!)
         XCTAssertEqual(count, 8, "There should be 8 frames.")
 #else
@@ -69,25 +69,22 @@ class ImageExtensionTests: XCTestCase {
     }
     
     func testGIFRepresentation() {
-        let image = Image.kf_animatedImageWithGIFData(gifData: testImageGIFData)!
+        let image = Image.kf_animatedImageWithGIFData(gifData: testImageGIFData, preloadAll: false)!
         let data = ImageGIFRepresentation(image)
         
         XCTAssertNotNil(data, "Data should not be nil")
         XCTAssertEqual(data?.kf_imageFormat, ImageFormat.GIF)
         
-        let image1 = Image.kf_animatedImageWithGIFData(gifData: data!)!
-#if os(iOS)
-        XCTAssertNotNil(image1.kf_imageSource!.imageRef, "Image source should not be nil")
-#else
-        XCTAssertEqual(image1.kf_duration, image.kf_duration)
-        XCTAssertEqual(image1.kf_images!.count, image.kf_images!.count)
-#endif
+        let allLoadImage = Image.kf_animatedImageWithGIFData(gifData: data!, preloadAll: true)!
+        let allLoadData = ImageGIFRepresentation(allLoadImage)
+        XCTAssertNotNil(allLoadData, "Data1 should not be nil")
+        XCTAssertEqual(allLoadData?.kf_imageFormat, ImageFormat.GIF)
     }
     
     func testGenerateSingleFrameGIFImage() {
-        let image = Image.kf_animatedImageWithGIFData(gifData: testImageSingleFrameGIFData)
+        let image = Image.kf_animatedImageWithGIFData(gifData: testImageSingleFrameGIFData, preloadAll: false)
         XCTAssertNotNil(image, "The image should be initiated.")
-#if os(iOS)
+#if os(iOS) || os(tvOS)
         let count = ImagesCountWithImageSource(image!.kf_imageSource!.imageRef!)
         XCTAssertEqual(count, 1, "There should be 1 frames.")
 #else
@@ -96,4 +93,14 @@ class ImageExtensionTests: XCTestCase {
         XCTAssertEqual(image!.kf_duration, Double.infinity, "The image duration should be 0 since it is not animated image.")
 #endif
     }
+    
+    func testPreloadAllGIFData() {
+        let image = Image.kf_animatedImageWithGIFData(gifData: testImageSingleFrameGIFData, preloadAll: true)!
+        XCTAssertNotNil(image, "The image should be initiated.")
+#if os(iOS) || os(tvOS)
+        XCTAssertNil(image.kf_imageSource, "Image source should be nil")
+#endif
+        XCTAssertEqual(image.kf_duration, image.kf_duration)
+        XCTAssertEqual(image.kf_images!.count, image.kf_images!.count)
+    }
 }