PartsRepresentableTests.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2024 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import CoreGraphics
  15. import CoreImage
  16. import FirebaseVertexAI
  17. import XCTest
  18. #if canImport(UIKit)
  19. import UIKit
  20. #elseif canImport(AppKit)
  21. import AppKit
  22. #endif
  23. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, *)
  24. final class PartsRepresentableTests: XCTestCase {
  25. func testModelContentFromCGImageIsNotEmpty() throws {
  26. // adapted from https://forums.swift.org/t/creating-a-cgimage-from-color-array/18634/2
  27. var srgbArray = [UInt32](repeating: 0xFFFF_FFFF, count: 8 * 8)
  28. let image = srgbArray.withUnsafeMutableBytes { ptr -> CGImage in
  29. let ctx = CGContext(
  30. data: ptr.baseAddress,
  31. width: 8,
  32. height: 8,
  33. bitsPerComponent: 8,
  34. bytesPerRow: 4 * 8,
  35. space: CGColorSpace(name: CGColorSpace.sRGB)!,
  36. bitmapInfo: CGBitmapInfo.byteOrder32Little.rawValue +
  37. CGImageAlphaInfo.premultipliedFirst.rawValue
  38. )!
  39. return ctx.makeImage()!
  40. }
  41. let modelContent = try image.tryPartsValue()
  42. XCTAssert(modelContent.count > 0, "Expected non-empty model content for CGImage: \(image)")
  43. }
  44. func testModelContentFromCIImageIsNotEmpty() throws {
  45. let image = CIImage(color: CIColor.red)
  46. .cropped(to: CGRect(origin: CGPointZero, size: CGSize(width: 16, height: 16)))
  47. let modelContent = try image.tryPartsValue()
  48. XCTAssert(modelContent.count > 0, "Expected non-empty model content for CGImage: \(image)")
  49. }
  50. func testModelContentFromInvalidCIImageThrows() throws {
  51. let image = CIImage.empty()
  52. do {
  53. let _ = try image.tryPartsValue()
  54. } catch {
  55. guard let imageError = (error as? ImageConversionError) else {
  56. XCTFail("Got unexpected error type: \(error)")
  57. return
  58. }
  59. switch imageError {
  60. case let .couldNotConvertToJPEG(source):
  61. // String(describing:) works around a type error.
  62. XCTAssertEqual(String(describing: source), String(describing: image))
  63. return
  64. case _:
  65. XCTFail("Expected image conversion error, got \(imageError) instead")
  66. return
  67. }
  68. }
  69. XCTFail("Expected model content from invlaid image to error")
  70. }
  71. #if canImport(UIKit) && !os(visionOS) // These tests are stalling in CI on visionOS.
  72. func testModelContentFromInvalidUIImageThrows() throws {
  73. let image = UIImage()
  74. do {
  75. _ = try image.tryPartsValue()
  76. } catch {
  77. guard let imageError = (error as? ImageConversionError) else {
  78. XCTFail("Got unexpected error type: \(error)")
  79. return
  80. }
  81. switch imageError {
  82. case let .couldNotConvertToJPEG(source):
  83. // String(describing:) works around a type error.
  84. XCTAssertEqual(String(describing: source), String(describing: image))
  85. return
  86. case _:
  87. XCTFail("Expected image conversion error, got \(imageError) instead")
  88. return
  89. }
  90. }
  91. XCTFail("Expected model content from invlaid image to error")
  92. }
  93. func testModelContentFromUIImageIsNotEmpty() throws {
  94. let coreImage = CIImage(color: CIColor.red)
  95. .cropped(to: CGRect(origin: CGPointZero, size: CGSize(width: 16, height: 16)))
  96. let image = UIImage(ciImage: coreImage)
  97. let modelContent = try image.tryPartsValue()
  98. XCTAssert(modelContent.count > 0, "Expected non-empty model content for UIImage: \(image)")
  99. }
  100. #elseif canImport(AppKit)
  101. func testModelContentFromNSImageIsNotEmpty() throws {
  102. let coreImage = CIImage(color: CIColor.red)
  103. .cropped(to: CGRect(origin: CGPointZero, size: CGSize(width: 16, height: 16)))
  104. let rep = NSCIImageRep(ciImage: coreImage)
  105. let image = NSImage(size: rep.size)
  106. image.addRepresentation(rep)
  107. let modelContent = try image.tryPartsValue()
  108. XCTAssert(modelContent.count > 0, "Expected non-empty model content for NSImage: \(image)")
  109. }
  110. func testModelContentFromInvalidNSImageThrows() throws {
  111. let image = NSImage()
  112. do {
  113. _ = try image.tryPartsValue()
  114. } catch {
  115. guard let imageError = (error as? ImageConversionError) else {
  116. XCTFail("Got unexpected error type: \(error)")
  117. return
  118. }
  119. switch imageError {
  120. case .invalidUnderlyingImage:
  121. // Pass
  122. return
  123. case _:
  124. XCTFail("Expected image conversion error, got \(imageError) instead")
  125. return
  126. }
  127. }
  128. XCTFail("Expected model content from invlaid image to error")
  129. }
  130. #endif
  131. }