PartsRepresentableTests.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. XCTFail("Expected model content from invalid image to error")
  60. } catch {
  61. guard let imageError = (error as? ImageConversionError) else {
  62. XCTFail("Got unexpected error type: \(error)")
  63. return
  64. }
  65. switch imageError {
  66. case let .couldNotConvertToJPEG(source):
  67. guard case let .ciImage(ciImage) = source else {
  68. XCTFail("Unexpected image source: \(source)")
  69. return
  70. }
  71. XCTAssertEqual(ciImage, image)
  72. default:
  73. XCTFail("Expected image conversion error, got \(imageError) instead")
  74. return
  75. }
  76. }
  77. }
  78. #endif // canImport(CoreImage)
  79. #if canImport(UIKit) && !os(visionOS) // These tests are stalling in CI on visionOS.
  80. func testModelContentFromInvalidUIImageThrows() throws {
  81. let image = UIImage()
  82. do {
  83. _ = try image.tryPartsValue()
  84. XCTFail("Expected model content from invalid image to error")
  85. } catch {
  86. guard let imageError = (error as? ImageConversionError) else {
  87. XCTFail("Got unexpected error type: \(error)")
  88. return
  89. }
  90. switch imageError {
  91. case let .couldNotConvertToJPEG(source):
  92. guard case let .uiImage(uiImage) = source else {
  93. XCTFail("Unexpected image source: \(source)")
  94. return
  95. }
  96. XCTAssertEqual(uiImage, image)
  97. default:
  98. XCTFail("Expected image conversion error, got \(imageError) instead")
  99. return
  100. }
  101. }
  102. }
  103. func testModelContentFromUIImageIsNotEmpty() throws {
  104. let image = try XCTUnwrap(UIImage(systemName: "star.fill"))
  105. let modelContent = try image.tryPartsValue()
  106. XCTAssert(modelContent.count > 0, "Expected non-empty model content for UIImage: \(image)")
  107. }
  108. #elseif canImport(AppKit)
  109. func testModelContentFromNSImageIsNotEmpty() throws {
  110. let coreImage = CIImage(color: CIColor.red)
  111. .cropped(to: CGRect(origin: CGPointZero, size: CGSize(width: 16, height: 16)))
  112. let rep = NSCIImageRep(ciImage: coreImage)
  113. let image = NSImage(size: rep.size)
  114. image.addRepresentation(rep)
  115. let modelContent = try image.tryPartsValue()
  116. XCTAssert(modelContent.count > 0, "Expected non-empty model content for NSImage: \(image)")
  117. }
  118. func testModelContentFromInvalidNSImageThrows() throws {
  119. let image = NSImage()
  120. do {
  121. _ = try image.tryPartsValue()
  122. } catch {
  123. guard let imageError = (error as? ImageConversionError) else {
  124. XCTFail("Got unexpected error type: \(error)")
  125. return
  126. }
  127. switch imageError {
  128. case .invalidUnderlyingImage:
  129. // Pass
  130. return
  131. case _:
  132. XCTFail("Expected image conversion error, got \(imageError) instead")
  133. return
  134. }
  135. }
  136. XCTFail("Expected model content from invalid image to error")
  137. }
  138. #endif
  139. }