ImageProcessorTests.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // ImageProcessorTests.swift
  3. // Kingfisher
  4. //
  5. // Created by onevcat on 2019/03/02.
  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. @testable import Kingfisher
  28. class ImageProcessorTests: XCTestCase {
  29. // Issue 1125. https://github.com/onevcat/Kingfisher/issues/1125
  30. func testDownsamplingSizes() {
  31. XCTAssertEqual(testImage.size, CGSize(width: 64, height: 64))
  32. let emptyOption = KingfisherParsedOptionsInfo(nil)
  33. let targetSize = CGSize(width: 20, height: 40)
  34. let downsamplingProcessor = DownsamplingImageProcessor(size: targetSize)
  35. let resultFromData = downsamplingProcessor.process(item: .data(testImageData), options: emptyOption)
  36. XCTAssertEqual(resultFromData!.size, CGSize(width: 40, height: 40))
  37. let resultFromImage = downsamplingProcessor.process(item: .image(testImage), options: emptyOption)
  38. XCTAssertEqual(resultFromImage!.size, CGSize(width: 40, height: 40))
  39. }
  40. func testProcessorConcating() {
  41. let p1 = BlurImageProcessor(blurRadius: 10)
  42. let p2 = RoundCornerImageProcessor(cornerRadius: 10)
  43. let p3 = TintImageProcessor(tint: .blue)
  44. let two = p1 |> p2
  45. let three = p1 |> p2 |> p3
  46. XCTAssertNotNil(two)
  47. XCTAssertNotNil(three)
  48. }
  49. func testParsingColorRGBA() {
  50. let sRGB = KFCrossPlatformColor(red: 0.5, green: 0.6, blue: 0.7, alpha: 0.8)
  51. let rgba = sRGB.rgba
  52. XCTAssertEqual(rgba.r, 0.5, accuracy: 0.01)
  53. XCTAssertEqual(rgba.g, 0.6, accuracy: 0.01)
  54. XCTAssertEqual(rgba.b, 0.7, accuracy: 0.01)
  55. XCTAssertEqual(rgba.a, 0.8, accuracy: 0.01)
  56. let extended = KFCrossPlatformColor(displayP3Red: 0, green: 1, blue: 0, alpha: 0.8)
  57. let rgbaExt = extended.rgba
  58. // extended sRGB
  59. XCTAssertTrue(rgbaExt.r < 0)
  60. XCTAssertTrue(rgbaExt.g > 1)
  61. XCTAssertTrue(rgbaExt.b < 0)
  62. XCTAssertEqual(rgbaExt.a, 0.8)
  63. let blackWhite = KFCrossPlatformColor(white: 0.3, alpha: 1.0)
  64. let rgbaBlackWhite = blackWhite.rgba
  65. XCTAssertEqual(rgbaBlackWhite.r, 0.3, accuracy: 0.01)
  66. XCTAssertEqual(rgbaBlackWhite.g, 0.3, accuracy: 0.01)
  67. XCTAssertEqual(rgbaBlackWhite.b, 0.3, accuracy: 0.01)
  68. XCTAssertEqual(rgbaBlackWhite.a, 1.0, accuracy: 0.01)
  69. }
  70. }