Преглед изворни кода

Add unit tests for resource extension and type guessing

onevcat пре 1 година
родитељ
комит
f79bdecc00

+ 4 - 0
Kingfisher.xcodeproj/project.pbxproj

@@ -121,6 +121,7 @@
 		D1E56445219B16330057AAE3 /* ImageDataProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1E56444219B16330057AAE3 /* ImageDataProvider.swift */; };
 		D1ED2D401AD2D09F00CFC3EB /* Kingfisher.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D1ED2D351AD2D09F00CFC3EB /* Kingfisher.framework */; };
 		D1F1F6FF24625EC600910725 /* RetryStrategyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1F1F6FE24625EC600910725 /* RetryStrategyTests.swift */; };
+		D1F66CC12CB2CF2E004959F3 /* LivePhotoSourceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1F66CC02CB2CF2D004959F3 /* LivePhotoSourceTests.swift */; };
 		D8FCF6A821C5A0E500F9ABC0 /* RedirectHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8FCF6A721C5A0E500F9ABC0 /* RedirectHandler.swift */; };
 		D9638BA61C7DC71F0046523D /* ImagePrefetcherTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9638BA41C7DC71F0046523D /* ImagePrefetcherTests.swift */; };
 		E9E3ED8B2B1F66B200734CFF /* HasImageComponent+Kingfisher.swift in Sources */ = {isa = PBXBuildFile; fileRef = E9E3ED8A2B1F66B200734CFF /* HasImageComponent+Kingfisher.swift */; };
@@ -301,6 +302,7 @@
 		D1ED2D351AD2D09F00CFC3EB /* Kingfisher.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Kingfisher.framework; sourceTree = BUILT_PRODUCTS_DIR; };
 		D1ED2D3F1AD2D09F00CFC3EB /* KingfisherTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = KingfisherTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
 		D1F1F6FE24625EC600910725 /* RetryStrategyTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RetryStrategyTests.swift; sourceTree = "<group>"; };
+		D1F66CC02CB2CF2D004959F3 /* LivePhotoSourceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LivePhotoSourceTests.swift; sourceTree = "<group>"; };
 		D1F7607523097532000C5269 /* ImageBinder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageBinder.swift; sourceTree = "<group>"; };
 		D1F7607623097532000C5269 /* KFImage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KFImage.swift; sourceTree = "<group>"; };
 		D8FCF6A721C5A0E500F9ABC0 /* RedirectHandler.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RedirectHandler.swift; sourceTree = "<group>"; };
@@ -491,6 +493,7 @@
 				4BCFF7A9219932390055AAC4 /* DiskStorageTests.swift */,
 				D1E564402199C21E0057AAE3 /* StorageExpirationTests.swift */,
 				D1A1CC9E21A0F98600263AD8 /* ImageDataProviderTests.swift */,
+				D1F66CC02CB2CF2D004959F3 /* LivePhotoSourceTests.swift */,
 				D1BFED94222ACC6B009330C8 /* ImageProcessorTests.swift */,
 				4BA3BF1D228BCDD100909201 /* DataReceivingSideEffectTests.swift */,
 				D1F1F6FE24625EC600910725 /* RetryStrategyTests.swift */,
@@ -921,6 +924,7 @@
 				D16FEA5023078C63006E67D5 /* NSString+Nocilla.m in Sources */,
 				D16FEA4E23078C63006E67D5 /* LSRegexMatcher.m in Sources */,
 				F72CE9CE1FCF17ED00CC522A /* ImageModifierTests.swift in Sources */,
+				D1F66CC12CB2CF2E004959F3 /* LivePhotoSourceTests.swift in Sources */,
 				D12E0C531C47F23500AC98AD /* ImageViewExtensionTests.swift in Sources */,
 				D16FEA4023078C63006E67D5 /* LSHTTPClientHook.m in Sources */,
 				D16FEA3F23078C63006E67D5 /* LSHTTPRequestDiff.m in Sources */,

+ 2 - 2
Sources/General/ImageSource/LivePhotoSource.swift

@@ -28,7 +28,7 @@ import Foundation
 
 public struct LivePhotoResource: Sendable {
     
-    public enum FileType: Sendable {
+    public enum FileType: Sendable, Equatable {
         case heic
         case mov
         case other(String)
@@ -79,7 +79,7 @@ extension LivePhotoResource.FileType {
     
     static func guessedFileExtension(from data: Data) -> String? {
         
-        guard data.count > 12 else { return nil }
+        guard data.count >= 12 else { return nil }
         
         var buffer = [UInt8](repeating: 0, count: 12)
         data.copyBytes(to: &buffer, count: 12)

+ 1 - 1
Sources/General/KingfisherManager+LivePhoto.swift

@@ -83,7 +83,7 @@ extension KingfisherManager {
             }
         }
         
-        // TODO. We ignore the retry of live photo now to suppress the complexity.
+        // TODO. We ignore the retry of live photo and the progress now to suppress the complexity.
         
         let missingResources = missingResources(source, options: checkedOptions)
         let resourcesResult = try await downloadAndCache(resources: missingResources, options: checkedOptions)

+ 183 - 0
Tests/KingfisherTests/LivePhotoSourceTests.swift

@@ -0,0 +1,183 @@
+//
+//  LivePhotoSourceTests.swift
+//  Kingfisher
+//
+//  Created by onevcat on 2024/10/01.
+//
+//  Copyright (c) 2024 Wei Wang <onevcat@gmail.com>
+//
+//  Permission is hereby granted, free of charge, to any person obtaining a copy
+//  of this software and associated documentation files (the "Software"), to deal
+//  in the Software without restriction, including without limitation the rights
+//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+//  copies of the Software, and to permit persons to whom the Software is
+//  furnished to do so, subject to the following conditions:
+//
+//  The above copyright notice and this permission notice shall be included in
+//  all copies or substantial portions of the Software.
+//
+//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+//  THE SOFTWARE.
+
+import XCTest
+@testable import Kingfisher
+
+class LivePhotoSourceTests: XCTestCase {
+    
+    func testLivePhotoResourceInitialization() {
+        let url = URL(string: "https://example.com/photo.heic")!
+        let resource = LivePhotoResource(downloadURL: url)
+        
+        XCTAssertEqual(resource.downloadURL, url)
+        XCTAssertEqual(resource.referenceFileType, .heic)
+    }
+    
+    func testLivePhotoResourceInitializationWithResource() {
+        let url = URL(string: "https://example.com/photo.mov")!
+        let imageResource = KF.ImageResource(downloadURL: url)
+        let resource = LivePhotoResource(resource: imageResource)
+        
+        XCTAssertEqual(resource.downloadURL, url)
+        XCTAssertEqual(resource.referenceFileType, .mov)
+    }
+    
+    func testLivePhotoResourceFileExtensionByType() {
+        let mov = LivePhotoResource.FileType.mov
+        XCTAssertEqual(mov.determinedFileExtension(Data()), "mov")
+        XCTAssertEqual(mov.fileExtension, "mov")
+        
+        let heic = LivePhotoResource.FileType.heic
+        XCTAssertEqual(heic.determinedFileExtension(Data()), "heic")
+        XCTAssertEqual(heic.fileExtension, "heic")
+        
+        let other = LivePhotoResource.FileType.other("exe")
+        XCTAssertEqual(other.fileExtension, "exe")
+    }
+    
+    func testLivePhotoResourceFileTypeDeterminationForHEIC() {
+        let data = Data([0x00, 0x00, 0x00, 0x00, 0x66, 0x74, 0x79, 0x70, 0x68, 0x65, 0x69, 0x63])
+        let fileType = LivePhotoResource.FileType.other("")
+        let determinedExtension = fileType.determinedFileExtension(data)
+        
+        XCTAssertEqual(determinedExtension, "heic")
+    }
+
+    func testLivePhotoResourceFileTypeDeterminationForQT() {
+        let data = Data([0x00, 0x00, 0x00, 0x00, 0x66, 0x74, 0x79, 0x70, 0x71, 0x74, 0x20, 0x20])
+        let fileType = LivePhotoResource.FileType.other("")
+        let determinedExtension = fileType.determinedFileExtension(data)
+        
+        XCTAssertEqual(determinedExtension, "mov")
+    }
+
+    func testLivePhotoResourceFileTypeDeterminationForExplicitFileType() {
+        let data = Data([0x00, 0x00, 0x00, 0x00, 0x66, 0x74, 0x79, 0x70, 0x71, 0x74, 0x20, 0x20])
+        let fileType = LivePhotoResource.FileType.other("ext")
+        let determinedExtension = fileType.determinedFileExtension(data)
+        
+        XCTAssertEqual(determinedExtension, "ext")
+    }
+
+    func testLivePhotoResourceFileTypeDeterminationForUnknown() {
+        let data = Data([0x00, 0x00, 0x00, 0x00, 0x66, 0x74, 0x79, 0x70, 0x71, 0x74, 0x20, 0x22])
+        let fileType = LivePhotoResource.FileType.other("")
+        let determinedExtension = fileType.determinedFileExtension(data)
+        
+        XCTAssertEqual(determinedExtension, nil)
+    }
+    
+    func testLivePhotoResourceFileTypeDeterminationForNonFYTP() {
+        let data = Data([0x00, 0x00, 0x00, 0x00, 0x12, 0x34, 0x56, 0x78, 0x71, 0x74, 0x20, 0x20])
+        let fileType = LivePhotoResource.FileType.other("")
+        let determinedExtension = fileType.determinedFileExtension(data)
+        
+        XCTAssertEqual(determinedExtension, nil)
+    }
+    
+    func testLivePhotoResourceFileTypeDeterminationForNotEnoughData() {
+        let data = Data([0x00, 0x00, 0x00, 0x00])
+        let fileType = LivePhotoResource.FileType.other("")
+        let determinedExtension = fileType.determinedFileExtension(data)
+        
+        XCTAssertEqual(determinedExtension, nil)
+    }
+    
+    func testLivePhotoSourceInitializationWithResources() {
+        let url1 = URL(string: "https://example.com/photo1.heic")!
+        let url2 = URL(string: "https://example.com/photo2.mov")!
+        let resources = [KF.ImageResource(downloadURL: url1), KF.ImageResource(downloadURL: url2)]
+        let livePhotoSource = LivePhotoSource(resources: resources)
+        
+        XCTAssertEqual(livePhotoSource.resources.count, 2)
+        XCTAssertEqual(livePhotoSource.resources[0].downloadURL, url1)
+        XCTAssertEqual(livePhotoSource.resources[1].downloadURL, url2)
+    }
+    
+    func testLivePhotoSourceInitializationWithURLs() {
+        let url1 = URL(string: "https://example.com/photo1.heic")!
+        let url2 = URL(string: "https://example.com/photo2.mov")!
+        let livePhotoSource = LivePhotoSource(urls: [url1, url2])
+        
+        XCTAssertEqual(livePhotoSource.resources.count, 2)
+        XCTAssertEqual(livePhotoSource.resources[0].downloadURL, url1)
+        XCTAssertEqual(livePhotoSource.resources[1].downloadURL, url2)
+    }
+    
+    func testLivePhotoResourceInitializationWithCacheKey() {
+        let url = URL(string: "https://example.com/photo.heic")!
+        let cacheKey = "customCacheKey"
+        let resource = LivePhotoResource(downloadURL: url, cacheKey: cacheKey)
+        
+        XCTAssertEqual(resource.downloadURL, url)
+        XCTAssertEqual(resource.cacheKey, cacheKey)
+        XCTAssertEqual(resource.referenceFileType, .heic)
+    }
+
+    func testLivePhotoResourceInitializationWithFileType() {
+        let url = URL(string: "https://example.com/photo.unknown")!
+        let resource = LivePhotoResource(downloadURL: url, fileType: .other("unknown"))
+        
+        XCTAssertEqual(resource.downloadURL, url)
+        XCTAssertEqual(resource.referenceFileType, .other("unknown"))
+    }
+
+    func testLivePhotoResourceGuessedFileType() {
+        let url1 = URL(string: "https://example.com/photo.heic")!
+        let url2 = URL(string: "https://example.com/photo.mov")!
+        let url3 = URL(string: "https://example.com/photo.unknown")!
+        
+        let resource1 = KF.ImageResource(downloadURL: url1)
+        let resource2 = KF.ImageResource(downloadURL: url2)
+        let resource3 = KF.ImageResource(downloadURL: url3)
+        
+        XCTAssertEqual(resource1.guessedFileType, .heic)
+        XCTAssertEqual(resource2.guessedFileType, .mov)
+        XCTAssertEqual(resource3.guessedFileType, .other("unknown"))
+    }
+
+    func testLivePhotoSourceInitializationWithMixedResources() {
+        let url1 = URL(string: "https://example.com/photo1.heic")!
+        let url2 = URL(string: "https://example.com/photo2.mov")!
+        let url3 = URL(string: "https://example.com/photo3.unknown")!
+        let resources = [
+            KF.ImageResource(downloadURL: url1),
+            KF.ImageResource(downloadURL: url2),
+            KF.ImageResource(downloadURL: url3)
+        ]
+        let livePhotoSource = LivePhotoSource(resources: resources)
+        
+        XCTAssertEqual(livePhotoSource.resources.count, 3)
+        XCTAssertEqual(livePhotoSource.resources[0].downloadURL, url1)
+        XCTAssertEqual(livePhotoSource.resources[1].downloadURL, url2)
+        XCTAssertEqual(livePhotoSource.resources[2].downloadURL, url3)
+        XCTAssertEqual(livePhotoSource.resources[0].referenceFileType, .heic)
+        XCTAssertEqual(livePhotoSource.resources[1].referenceFileType, .mov)
+        XCTAssertEqual(livePhotoSource.resources[2].referenceFileType, .other("unknown"))
+    }
+
+}