2
0

ImageExtensionTests.swift 14 KB

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