ImageExtensionTests.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. //
  2. // ImageExtensionTests.swift
  3. // Kingfisher
  4. //
  5. // Created by Wei Wang on 15/10/24.
  6. //
  7. // Copyright (c) 2019 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. import ImageIO
  28. @testable import Kingfisher
  29. class ImageExtensionTests: XCTestCase {
  30. func testImageFormat() {
  31. var format: ImageFormat
  32. format = testImageJEPGData.kf.imageFormat
  33. XCTAssertEqual(format, .JPEG)
  34. format = testImagePNGData.kf.imageFormat
  35. XCTAssertEqual(format, .PNG)
  36. format = testImageGIFData.kf.imageFormat
  37. XCTAssertEqual(format, .GIF)
  38. let raw: [UInt8] = [1, 2, 3, 4, 5, 6, 7, 8]
  39. format = Data(raw).kf.imageFormat
  40. XCTAssertEqual(format, .unknown)
  41. }
  42. func testGenerateJPEGImage() {
  43. let options = ImageCreatingOptions()
  44. let image = KingfisherWrapper<KFCrossPlatformImage>.image(data: testImageJEPGData, options: options)
  45. XCTAssertNotNil(image)
  46. XCTAssertNil(image?.kf.imageFrameCount)
  47. XCTAssertTrue(image!.renderEqual(to: KFCrossPlatformImage(data: testImageJEPGData)!))
  48. }
  49. func testGenerateGIFImage() {
  50. let options = ImageCreatingOptions()
  51. let image = KingfisherWrapper<KFCrossPlatformImage>.animatedImage(data: testImageGIFData, options: options)
  52. XCTAssertNotNil(image)
  53. #if os(iOS) || os(tvOS) || os(visionOS)
  54. XCTAssertEqual(image!.kf.imageFrameCount!, 8)
  55. #else
  56. XCTAssertEqual(image!.kf.images!.count, 8)
  57. XCTAssertEqual(image!.kf.duration, 0.8, accuracy: 0.001)
  58. #endif
  59. }
  60. #if os(iOS) || os(tvOS) || os(visionOS)
  61. func testScaleForGIFImage() {
  62. let options = ImageCreatingOptions(scale: 2.0, duration: 0.0, preloadAll: false, onlyFirstFrame: false)
  63. let image = KingfisherWrapper<KFCrossPlatformImage>.animatedImage(data: testImageGIFData, options: options)
  64. XCTAssertNotNil(image)
  65. XCTAssertEqual(image!.scale, 2.0)
  66. }
  67. #endif
  68. func testGIFRepresentation() {
  69. let options = ImageCreatingOptions()
  70. let image = KingfisherWrapper<KFCrossPlatformImage>.animatedImage(data: testImageGIFData, options: options)!
  71. let data = image.kf.gifRepresentation()
  72. XCTAssertNotNil(data)
  73. XCTAssertEqual(data?.kf.imageFormat, ImageFormat.GIF)
  74. let preloadOptions = ImageCreatingOptions(preloadAll: true)
  75. let allLoadImage = KingfisherWrapper<KFCrossPlatformImage>.animatedImage(data: data!, options: preloadOptions)!
  76. let allLoadData = allLoadImage.kf.gifRepresentation()
  77. XCTAssertNotNil(allLoadData)
  78. XCTAssertEqual(allLoadData?.kf.imageFormat, ImageFormat.GIF)
  79. }
  80. func testGenerateSingleFrameGIFImage() {
  81. let options = ImageCreatingOptions()
  82. let image = KingfisherWrapper<KFCrossPlatformImage>.animatedImage(data: testImageSingleFrameGIFData, options: options)
  83. XCTAssertNotNil(image)
  84. #if os(iOS) || os(tvOS) || os(visionOS)
  85. XCTAssertEqual(image!.kf.imageFrameCount!, 1)
  86. #else
  87. XCTAssertEqual(image!.kf.images!.count, 1)
  88. XCTAssertEqual(image!.kf.duration, Double.infinity)
  89. #endif
  90. }
  91. func testGenerateFromNonImage() {
  92. let data = "hello".data(using: .utf8)!
  93. let options = ImageCreatingOptions()
  94. let image = KingfisherWrapper<KFCrossPlatformImage>.image(data: data, options: options)
  95. XCTAssertNil(image)
  96. }
  97. func testPreloadAllAnimationData() {
  98. let preloadOptions = ImageCreatingOptions(preloadAll: true)
  99. let image = KingfisherWrapper<KFCrossPlatformImage>.animatedImage(data: testImageSingleFrameGIFData, options: preloadOptions)!
  100. XCTAssertNotNil(image, "The image should be initiated.")
  101. #if os(iOS) || os(tvOS) || os(visionOS)
  102. XCTAssertNil(image.kf.imageSource, "Image source should be nil")
  103. #endif
  104. XCTAssertEqual(image.kf.duration, image.kf.duration)
  105. XCTAssertEqual(image.kf.images!.count, image.kf.images!.count)
  106. }
  107. func testLoadOnlyFirstFrame() {
  108. let preloadOptions = ImageCreatingOptions(preloadAll: true, onlyFirstFrame: true)
  109. let image = KingfisherWrapper<KFCrossPlatformImage>.animatedImage(data: testImageGIFData, options: preloadOptions)!
  110. XCTAssertNotNil(image, "The image should be initiated.")
  111. XCTAssertNil(image.kf.images, "The image should be nil")
  112. }
  113. func testSizeContent() {
  114. func getRatio(image: KFCrossPlatformImage) -> CGFloat {
  115. return image.size.height / image.size.width
  116. }
  117. let image = testImage
  118. let ratio = getRatio(image: image)
  119. let targetSize = CGSize(width: 100, height: 50)
  120. let fillImage = image.kf.resize(to: targetSize, for: .aspectFill)
  121. XCTAssertEqual(getRatio(image: fillImage), ratio)
  122. XCTAssertEqual(max(fillImage.size.width, fillImage.size.height), 100)
  123. let fitImage = image.kf.resize(to: targetSize, for: .aspectFit)
  124. XCTAssertEqual(getRatio(image: fitImage), ratio)
  125. XCTAssertEqual(max(fitImage.size.width, fitImage.size.height), 50)
  126. let resizeImage = image.kf.resize(to: targetSize)
  127. XCTAssertEqual(resizeImage.size.width, 100)
  128. XCTAssertEqual(resizeImage.size.height, 50)
  129. }
  130. func testSizeConstraintByAnchor() {
  131. let size = CGSize(width: 100, height: 100)
  132. let topLeft = CGPoint(x: 0, y: 0)
  133. let top = CGPoint(x: 0.5, y: 0)
  134. let topRight = CGPoint(x: 1, y: 0)
  135. let center = CGPoint(x: 0.5, y: 0.5)
  136. let bottomRight = CGPoint(x: 1, y: 1)
  137. let invalidAnchor = CGPoint(x: -1, y: 2)
  138. let inSize = CGSize(width: 20, height: 20)
  139. let outX = CGSize(width: 120, height: 20)
  140. let outY = CGSize(width: 20, height: 120)
  141. let outSize = CGSize(width: 120, height: 120)
  142. let kf = size.kf
  143. XCTAssertEqual(
  144. kf.constrainedRect(for: inSize, anchor: topLeft),
  145. CGRect(x: 0, y: 0, width: 20, height: 20))
  146. XCTAssertEqual(
  147. kf.constrainedRect(for: outX, anchor: topLeft),
  148. CGRect(x: 0, y: 0, width: 100, height: 20))
  149. XCTAssertEqual(
  150. kf.constrainedRect(for: outY, anchor: topLeft),
  151. CGRect(x: 0, y: 0, width: 20, height: 100))
  152. XCTAssertEqual(
  153. kf.constrainedRect(for: outSize, anchor: topLeft),
  154. CGRect(x: 0, y: 0, width: 100, height: 100))
  155. XCTAssertEqual(
  156. kf.constrainedRect(for: inSize, anchor: top),
  157. CGRect(x: 40, y: 0, width: 20, height: 20))
  158. XCTAssertEqual(
  159. kf.constrainedRect(for: outX, anchor: top),
  160. CGRect(x: 0, y: 0, width: 100, height: 20))
  161. XCTAssertEqual(
  162. kf.constrainedRect(for: outY, anchor: top),
  163. CGRect(x: 40, y: 0, width: 20, height: 100))
  164. XCTAssertEqual(
  165. kf.constrainedRect(for: outSize, anchor: top),
  166. CGRect(x: 0, y: 0, width: 100, height: 100))
  167. XCTAssertEqual(
  168. kf.constrainedRect(for: inSize, anchor: topRight),
  169. CGRect(x: 80, y: 0, width: 20, height: 20))
  170. XCTAssertEqual(
  171. kf.constrainedRect(for: outX, anchor: topRight),
  172. CGRect(x: 0, y: 0, width: 100, height: 20))
  173. XCTAssertEqual(
  174. kf.constrainedRect(for: outY, anchor: topRight),
  175. CGRect(x: 80, y: 0, width: 20, height: 100))
  176. XCTAssertEqual(
  177. kf.constrainedRect(for: outSize, anchor: topRight),
  178. CGRect(x: 0, y: 0, width: 100, height: 100))
  179. XCTAssertEqual(
  180. kf.constrainedRect(for: inSize, anchor: center),
  181. CGRect(x: 40, y: 40, width: 20, height: 20))
  182. XCTAssertEqual(
  183. kf.constrainedRect(for: outX, anchor: center),
  184. CGRect(x: 0, y: 40, width: 100, height: 20))
  185. XCTAssertEqual(
  186. kf.constrainedRect(for: outY, anchor: center),
  187. CGRect(x: 40, y: 0, width: 20, height: 100))
  188. XCTAssertEqual(
  189. kf.constrainedRect(for: outSize, anchor: center),
  190. CGRect(x: 0, y: 0, width: 100, height: 100))
  191. XCTAssertEqual(
  192. kf.constrainedRect(for: inSize, anchor: bottomRight),
  193. CGRect(x: 80, y: 80, width: 20, height: 20))
  194. XCTAssertEqual(
  195. kf.constrainedRect(for: outX, anchor: bottomRight),
  196. CGRect(x: 0, y: 80, width: 100, height: 20))
  197. XCTAssertEqual(
  198. kf.constrainedRect(for: outY, anchor: bottomRight),
  199. CGRect(x:80, y: 0, width: 20, height: 100))
  200. XCTAssertEqual(
  201. kf.constrainedRect(for: outSize, anchor: bottomRight),
  202. CGRect(x: 0, y: 0, width: 100, height: 100))
  203. XCTAssertEqual(
  204. kf.constrainedRect(for: inSize, anchor: invalidAnchor),
  205. CGRect(x: 0, y: 80, width: 20, height: 20))
  206. XCTAssertEqual(
  207. kf.constrainedRect(for: outX, anchor: invalidAnchor),
  208. CGRect(x: 0, y: 80, width: 100, height: 20))
  209. XCTAssertEqual(
  210. kf.constrainedRect(for: outY, anchor: invalidAnchor),
  211. CGRect(x:0, y: 0, width: 20, height: 100))
  212. XCTAssertEqual(
  213. kf.constrainedRect(for: outSize, anchor: invalidAnchor),
  214. CGRect(x: 0, y: 0, width: 100, height: 100))
  215. }
  216. func testDecodeScale() {
  217. #if os(iOS) || os(tvOS) || os(visionOS)
  218. let image = testImage
  219. XCTAssertEqual(image.size, CGSize(width: 64, height: 64))
  220. XCTAssertEqual(image.scale, 1.0)
  221. let image_2x = KingfisherWrapper<KFCrossPlatformImage>.image(cgImage: image.cgImage!, scale: 2.0, refImage: image)
  222. XCTAssertEqual(image_2x.size, CGSize(width: 32, height: 32))
  223. XCTAssertEqual(image_2x.scale, 2.0)
  224. let decoded = image.kf.decoded
  225. XCTAssertEqual(decoded.size, CGSize(width: 64, height: 64))
  226. XCTAssertEqual(decoded.scale, 1.0)
  227. let decodedDifferentScale = image.kf.decoded(scale: 2.0)
  228. XCTAssertEqual(decodedDifferentScale.size, CGSize(width: 32, height: 32))
  229. XCTAssertEqual(decodedDifferentScale.scale, 2.0)
  230. let decoded_2x = image_2x.kf.decoded
  231. XCTAssertEqual(decoded_2x.size, CGSize(width: 32, height: 32))
  232. XCTAssertEqual(decoded_2x.scale, 2.0)
  233. #endif
  234. }
  235. func testNormalized() {
  236. // Full loaded GIF image should not be normalized since it is a set of images.
  237. let options = ImageCreatingOptions()
  238. let gifImage = KingfisherWrapper<KFCrossPlatformImage>.animatedImage(data: testImageGIFData, options: options)
  239. XCTAssertNotNil(gifImage)
  240. XCTAssertEqual(gifImage!.kf.normalized, gifImage!)
  241. #if os(iOS) || os(tvOS) || os(visionOS)
  242. // No need to normalize up orientation image.
  243. let normalImage = testImage
  244. XCTAssertEqual(normalImage.imageOrientation, .up)
  245. XCTAssertEqual(normalImage.kf.normalized, testImage)
  246. let colorImage = UIImage.from(color: .red, size: CGSize(width: 100, height: 200))
  247. let rotatedImage = UIImage(cgImage: colorImage.cgImage!, scale: colorImage.scale, orientation: .right)
  248. XCTAssertEqual(rotatedImage.imageOrientation, .right)
  249. let rotatedNormalizedImage = rotatedImage.kf.normalized
  250. XCTAssertEqual(rotatedNormalizedImage.imageOrientation, .up)
  251. XCTAssertEqual(rotatedNormalizedImage.size, CGSize(width: 200, height: 100))
  252. #endif
  253. }
  254. func testDownsampling() {
  255. let size = CGSize(width: 15, height: 15)
  256. XCTAssertEqual(testImage.size, CGSize(width: 64, height: 64))
  257. XCTAssertEqual(testImage.kf.scale, 1.0)
  258. let image = KingfisherWrapper<KFCrossPlatformImage>.downsampledImage(data: testImageData, to: size, scale: 1)
  259. XCTAssertEqual(image?.size, size)
  260. XCTAssertEqual(image?.kf.scale, 1.0)
  261. }
  262. func testDownsamplingWithScale() {
  263. let size = CGSize(width: 15, height: 15)
  264. XCTAssertEqual(testImage.size, CGSize(width: 64, height: 64))
  265. XCTAssertEqual(testImage.kf.scale, 1.0)
  266. let image2x = KingfisherWrapper<KFCrossPlatformImage>.downsampledImage(data: testImageData, to: size, scale: 2)
  267. #if os(macOS)
  268. XCTAssertEqual(image2x?.size, CGSize(width: 30, height: 30))
  269. XCTAssertEqual(image2x?.kf.scale, 1.0)
  270. #else
  271. XCTAssertEqual(image2x?.size, size)
  272. XCTAssertEqual(image2x?.kf.scale, 2.0)
  273. #endif
  274. let image3x = KingfisherWrapper<KFCrossPlatformImage>.downsampledImage(data: testImageData, to: size, scale: 3)
  275. #if os(macOS)
  276. XCTAssertEqual(image3x?.size, CGSize(width: 45, height: 45))
  277. XCTAssertEqual(image3x?.kf.scale, 1.0)
  278. #else
  279. XCTAssertEqual(image3x?.size, size)
  280. XCTAssertEqual(image3x?.kf.scale, 3.0)
  281. #endif
  282. }
  283. func testDownsamplingWithEdgeCaseSize() {
  284. // Zero size would fail downsampling before iOS 17.4.
  285. let result = KingfisherWrapper<KFCrossPlatformImage>.downsampledImage(data: testImageData, to: .zero, scale: 1)
  286. if #available(iOS 17.4, macOS 14.4, tvOS 17.4, *) {
  287. XCTAssertEqual(result?.size, CGSize(width: 64, height: 64))
  288. } else {
  289. XCTAssertNil(result)
  290. }
  291. let largerSize = CGSize(width: 100, height: 100)
  292. let largerImage = KingfisherWrapper<KFCrossPlatformImage>.downsampledImage(data: testImageData, to: largerSize, scale: 1)
  293. // You can not "downsample" an image to a larger size.
  294. XCTAssertEqual(largerImage?.size, CGSize(width: 64, height: 64))
  295. }
  296. }