PartsRepresentableTests.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 FirebaseVertexAI
  16. import XCTest
  17. #if canImport(UIKit)
  18. import UIKit
  19. #elseif canImport(AppKit)
  20. import AppKit
  21. #endif
  22. #if canImport(CoreImage)
  23. import CoreImage
  24. #endif // canImport(CoreImage)
  25. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  26. final class PartsRepresentableTests: XCTestCase {
  27. #if !os(watchOS)
  28. func testModelContentFromCGImageIsNotEmpty() throws {
  29. // adapted from https://forums.swift.org/t/creating-a-cgimage-from-color-array/18634/2
  30. var srgbArray = [UInt32](repeating: 0xFFFF_FFFF, count: 8 * 8)
  31. let image = srgbArray.withUnsafeMutableBytes { ptr -> CGImage in
  32. let ctx = CGContext(
  33. data: ptr.baseAddress,
  34. width: 8,
  35. height: 8,
  36. bitsPerComponent: 8,
  37. bytesPerRow: 4 * 8,
  38. space: CGColorSpace(name: CGColorSpace.sRGB)!,
  39. bitmapInfo: CGBitmapInfo.byteOrder32Little.rawValue +
  40. CGImageAlphaInfo.premultipliedFirst.rawValue
  41. )!
  42. return ctx.makeImage()!
  43. }
  44. let modelContent = try image.tryPartsValue()
  45. XCTAssert(modelContent.count > 0, "Expected non-empty model content for CGImage: \(image)")
  46. }
  47. #endif // !os(watchOS)
  48. #if canImport(CoreImage)
  49. func testModelContentFromCIImageIsNotEmpty() throws {
  50. let image = CIImage(color: CIColor.red)
  51. .cropped(to: CGRect(origin: CGPointZero, size: CGSize(width: 16, height: 16)))
  52. let modelContent = try image.tryPartsValue()
  53. XCTAssert(modelContent.count > 0, "Expected non-empty model content for CGImage: \(image)")
  54. }
  55. func testModelContentFromInvalidCIImageThrows() throws {
  56. let image = CIImage.empty()
  57. do {
  58. _ = try image.tryPartsValue()
  59. } catch {
  60. guard let imageError = (error as? ImageConversionError) else {
  61. XCTFail("Got unexpected error type: \(error)")
  62. return
  63. }
  64. switch imageError {
  65. case let .couldNotConvertToJPEG(source):
  66. // String(describing:) works around a type error.
  67. XCTAssertEqual(String(describing: source), String(describing: image))
  68. return
  69. case _:
  70. XCTFail("Expected image conversion error, got \(imageError) instead")
  71. return
  72. }
  73. }
  74. XCTFail("Expected model content from invlaid image to error")
  75. }
  76. #endif // canImport(CoreImage)
  77. #if canImport(UIKit) && !os(visionOS) // These tests are stalling in CI on visionOS.
  78. func testModelContentFromInvalidUIImageThrows() throws {
  79. let image = UIImage()
  80. do {
  81. _ = try image.tryPartsValue()
  82. } catch {
  83. guard let imageError = (error as? ImageConversionError) else {
  84. XCTFail("Got unexpected error type: \(error)")
  85. return
  86. }
  87. switch imageError {
  88. case let .couldNotConvertToJPEG(source):
  89. // String(describing:) works around a type error.
  90. XCTAssertEqual(String(describing: source), String(describing: image))
  91. return
  92. case _:
  93. XCTFail("Expected image conversion error, got \(imageError) instead")
  94. return
  95. }
  96. }
  97. XCTFail("Expected model content from invlaid image to error")
  98. }
  99. func testModelContentFromUIImageIsNotEmpty() throws {
  100. let image = try XCTUnwrap(UIImage(systemName: "star.fill"))
  101. let modelContent = try image.tryPartsValue()
  102. XCTAssert(modelContent.count > 0, "Expected non-empty model content for UIImage: \(image)")
  103. }
  104. #elseif canImport(AppKit)
  105. func testModelContentFromNSImageIsNotEmpty() throws {
  106. let coreImage = CIImage(color: CIColor.red)
  107. .cropped(to: CGRect(origin: CGPointZero, size: CGSize(width: 16, height: 16)))
  108. let rep = NSCIImageRep(ciImage: coreImage)
  109. let image = NSImage(size: rep.size)
  110. image.addRepresentation(rep)
  111. let modelContent = try image.tryPartsValue()
  112. XCTAssert(modelContent.count > 0, "Expected non-empty model content for NSImage: \(image)")
  113. }
  114. func testModelContentFromInvalidNSImageThrows() throws {
  115. let image = NSImage()
  116. do {
  117. _ = try image.tryPartsValue()
  118. } catch {
  119. guard let imageError = (error as? ImageConversionError) else {
  120. XCTFail("Got unexpected error type: \(error)")
  121. return
  122. }
  123. switch imageError {
  124. case .invalidUnderlyingImage:
  125. // Pass
  126. return
  127. case _:
  128. XCTFail("Expected image conversion error, got \(imageError) instead")
  129. return
  130. }
  131. }
  132. XCTFail("Expected model content from invlaid image to error")
  133. }
  134. #endif
  135. }