LivePhotoSourceTests.swift 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // LivePhotoSourceTests.swift
  3. // Kingfisher
  4. //
  5. // Created by onevcat on 2024/10/01.
  6. //
  7. // Copyright (c) 2024 Wei Wang <onevcat@gmail.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. import XCTest
  27. @testable import Kingfisher
  28. class LivePhotoSourceTests: XCTestCase {
  29. func testLivePhotoResourceInitialization() {
  30. let url = URL(string: "https://example.com/photo.heic")!
  31. let resource = LivePhotoResource(downloadURL: url)
  32. XCTAssertEqual(resource.downloadURL, url)
  33. XCTAssertEqual(resource.referenceFileType, .heic)
  34. }
  35. func testLivePhotoResourceInitializationWithResource() {
  36. let url = URL(string: "https://example.com/photo.mov")!
  37. let imageResource = KF.ImageResource(downloadURL: url)
  38. let resource = LivePhotoResource(resource: imageResource)
  39. XCTAssertEqual(resource.downloadURL, url)
  40. XCTAssertEqual(resource.referenceFileType, .mov)
  41. }
  42. func testLivePhotoResourceFileExtensionByType() {
  43. let mov = LivePhotoResource.FileType.mov
  44. XCTAssertEqual(mov.determinedFileExtension(Data()), "mov")
  45. XCTAssertEqual(mov.fileExtension, "mov")
  46. let heic = LivePhotoResource.FileType.heic
  47. XCTAssertEqual(heic.determinedFileExtension(Data()), "heic")
  48. XCTAssertEqual(heic.fileExtension, "heic")
  49. let other = LivePhotoResource.FileType.other("exe")
  50. XCTAssertEqual(other.fileExtension, "exe")
  51. }
  52. func testLivePhotoResourceFileTypeDeterminationForHEIC() {
  53. let data = Data([0x00, 0x00, 0x00, 0x00, 0x66, 0x74, 0x79, 0x70, 0x68, 0x65, 0x69, 0x63])
  54. let fileType = LivePhotoResource.FileType.other("")
  55. let determinedExtension = fileType.determinedFileExtension(data)
  56. XCTAssertEqual(determinedExtension, "heic")
  57. }
  58. func testLivePhotoResourceFileTypeDeterminationForQT() {
  59. let data = Data([0x00, 0x00, 0x00, 0x00, 0x66, 0x74, 0x79, 0x70, 0x71, 0x74, 0x20, 0x20])
  60. let fileType = LivePhotoResource.FileType.other("")
  61. let determinedExtension = fileType.determinedFileExtension(data)
  62. XCTAssertEqual(determinedExtension, "mov")
  63. }
  64. func testLivePhotoResourceFileTypeDeterminationForExplicitFileType() {
  65. let data = Data([0x00, 0x00, 0x00, 0x00, 0x66, 0x74, 0x79, 0x70, 0x71, 0x74, 0x20, 0x20])
  66. let fileType = LivePhotoResource.FileType.other("ext")
  67. let determinedExtension = fileType.determinedFileExtension(data)
  68. XCTAssertEqual(determinedExtension, "ext")
  69. }
  70. func testLivePhotoResourceFileTypeDeterminationForUnknown() {
  71. let data = Data([0x00, 0x00, 0x00, 0x00, 0x66, 0x74, 0x79, 0x70, 0x71, 0x74, 0x20, 0x22])
  72. let fileType = LivePhotoResource.FileType.other("")
  73. let determinedExtension = fileType.determinedFileExtension(data)
  74. XCTAssertEqual(determinedExtension, nil)
  75. }
  76. func testLivePhotoResourceFileTypeDeterminationForNonFYTP() {
  77. let data = Data([0x00, 0x00, 0x00, 0x00, 0x12, 0x34, 0x56, 0x78, 0x71, 0x74, 0x20, 0x20])
  78. let fileType = LivePhotoResource.FileType.other("")
  79. let determinedExtension = fileType.determinedFileExtension(data)
  80. XCTAssertEqual(determinedExtension, nil)
  81. }
  82. func testLivePhotoResourceFileTypeDeterminationForNotEnoughData() {
  83. let data = Data([0x00, 0x00, 0x00, 0x00])
  84. let fileType = LivePhotoResource.FileType.other("")
  85. let determinedExtension = fileType.determinedFileExtension(data)
  86. XCTAssertEqual(determinedExtension, nil)
  87. }
  88. func testLivePhotoSourceInitializationWithResources() {
  89. let url1 = URL(string: "https://example.com/photo1.heic")!
  90. let url2 = URL(string: "https://example.com/photo2.mov")!
  91. let resources = [KF.ImageResource(downloadURL: url1), KF.ImageResource(downloadURL: url2)]
  92. let livePhotoSource = LivePhotoSource(resources: resources)
  93. XCTAssertEqual(livePhotoSource.resources.count, 2)
  94. XCTAssertEqual(livePhotoSource.resources[0].downloadURL, url1)
  95. XCTAssertEqual(livePhotoSource.resources[1].downloadURL, url2)
  96. }
  97. func testLivePhotoSourceInitializationWithURLs() {
  98. let url1 = URL(string: "https://example.com/photo1.heic")!
  99. let url2 = URL(string: "https://example.com/photo2.mov")!
  100. let livePhotoSource = LivePhotoSource(urls: [url1, url2])
  101. XCTAssertEqual(livePhotoSource.resources.count, 2)
  102. XCTAssertEqual(livePhotoSource.resources[0].downloadURL, url1)
  103. XCTAssertEqual(livePhotoSource.resources[1].downloadURL, url2)
  104. }
  105. func testLivePhotoResourceInitializationWithCacheKey() {
  106. let url = URL(string: "https://example.com/photo.heic")!
  107. let cacheKey = "customCacheKey"
  108. let resource = LivePhotoResource(downloadURL: url, cacheKey: cacheKey)
  109. XCTAssertEqual(resource.downloadURL, url)
  110. XCTAssertEqual(resource.cacheKey, cacheKey)
  111. XCTAssertEqual(resource.referenceFileType, .heic)
  112. }
  113. func testLivePhotoResourceInitializationWithFileType() {
  114. let url = URL(string: "https://example.com/photo.unknown")!
  115. let resource = LivePhotoResource(downloadURL: url, fileType: .other("unknown"))
  116. XCTAssertEqual(resource.downloadURL, url)
  117. XCTAssertEqual(resource.referenceFileType, .other("unknown"))
  118. }
  119. func testLivePhotoResourceGuessedFileType() {
  120. let url1 = URL(string: "https://example.com/photo.heic")!
  121. let url2 = URL(string: "https://example.com/photo.mov")!
  122. let url3 = URL(string: "https://example.com/photo.unknown")!
  123. let resource1 = KF.ImageResource(downloadURL: url1)
  124. let resource2 = KF.ImageResource(downloadURL: url2)
  125. let resource3 = KF.ImageResource(downloadURL: url3)
  126. XCTAssertEqual(resource1.guessedFileType, .heic)
  127. XCTAssertEqual(resource2.guessedFileType, .mov)
  128. XCTAssertEqual(resource3.guessedFileType, .other("unknown"))
  129. }
  130. func testLivePhotoSourceInitializationWithMixedResources() {
  131. let url1 = URL(string: "https://example.com/photo1.heic")!
  132. let url2 = URL(string: "https://example.com/photo2.mov")!
  133. let url3 = URL(string: "https://example.com/photo3.unknown")!
  134. let resources = [
  135. KF.ImageResource(downloadURL: url1),
  136. KF.ImageResource(downloadURL: url2),
  137. KF.ImageResource(downloadURL: url3)
  138. ]
  139. let livePhotoSource = LivePhotoSource(resources: resources)
  140. XCTAssertEqual(livePhotoSource.resources.count, 3)
  141. XCTAssertEqual(livePhotoSource.resources[0].downloadURL, url1)
  142. XCTAssertEqual(livePhotoSource.resources[1].downloadURL, url2)
  143. XCTAssertEqual(livePhotoSource.resources[2].downloadURL, url3)
  144. XCTAssertEqual(livePhotoSource.resources[0].referenceFileType, .heic)
  145. XCTAssertEqual(livePhotoSource.resources[1].referenceFileType, .mov)
  146. XCTAssertEqual(livePhotoSource.resources[2].referenceFileType, .other("unknown"))
  147. }
  148. }