|
| 1 | +// |
| 2 | +// This source file is part of the Swift.org open source project |
| 3 | +// |
| 4 | +// Copyright (c) 2024 Apple Inc. and the Swift project authors |
| 5 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | +// |
| 7 | +// See https://swift.org/LICENSE.txt for license information |
| 8 | +// See https://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +// |
| 10 | + |
| 11 | +#if SWT_TARGET_OS_APPLE && canImport(CoreGraphics) |
| 12 | +@_spi(ForSwiftTestingOnly) @_spi(Experimental) public import Testing |
| 13 | + |
| 14 | +public import UniformTypeIdentifiers |
| 15 | + |
| 16 | +extension Attachment { |
| 17 | + /// Initialize an instance of this type that encloses the given image. |
| 18 | + /// |
| 19 | + /// - Parameters: |
| 20 | + /// - attachableValue: The value that will be attached to the output of |
| 21 | + /// the test run. |
| 22 | + /// - preferredName: The preferred name of the attachment when writing it |
| 23 | + /// to a test report or to disk. If `nil`, the testing library attempts |
| 24 | + /// to derive a reasonable filename for the attached value. |
| 25 | + /// - contentType: The image format with which to encode `attachableValue`. |
| 26 | + /// If this type does not conform to [`UTType.image`](https://developer.apple.com/documentation/uniformtypeidentifiers/uttype-swift.struct/image), |
| 27 | + /// the result is undefined. Pass `nil` to let the testing library decide |
| 28 | + /// which image format to use. |
| 29 | + /// - encodingQuality: The encoding quality to use when encoding the image. |
| 30 | + /// If the image format used for encoding (specified by the `contentType` |
| 31 | + /// argument) does not support variable-quality encoding, the value of |
| 32 | + /// this argument is ignored. |
| 33 | + /// - sourceLocation: The source location of the call to this initializer. |
| 34 | + /// This value is used when recording issues associated with the |
| 35 | + /// attachment. |
| 36 | + /// |
| 37 | + /// This is the designated initializer for this type when attaching an image |
| 38 | + /// that conforms to ``AttachableAsCGImage``. |
| 39 | + fileprivate init<T>( |
| 40 | + attachableValue: T, |
| 41 | + named preferredName: String?, |
| 42 | + contentType: (any Sendable)?, |
| 43 | + encodingQuality: Float, |
| 44 | + sourceLocation: SourceLocation |
| 45 | + ) where AttachableValue == _AttachableImageContainer<T> { |
| 46 | + var imageContainer = _AttachableImageContainer(image: attachableValue, encodingQuality: encodingQuality) |
| 47 | + |
| 48 | + // Update the preferred name to include an extension appropriate for the |
| 49 | + // given content type. (Note the `else` branch duplicates the logic in |
| 50 | + // `preferredContentType(forEncodingQuality:)` but will go away once our |
| 51 | + // minimum deployment targets include the UniformTypeIdentifiers framework.) |
| 52 | + var preferredName = preferredName ?? Self.defaultPreferredName |
| 53 | + if #available(_uttypesAPI, *) { |
| 54 | + let contentType: UTType = contentType |
| 55 | + .map { $0 as! UTType } |
| 56 | + .flatMap { contentType in |
| 57 | + if UTType.image.conforms(to: contentType) { |
| 58 | + // This type is an abstract base type of .image (or .image itself.) |
| 59 | + // We'll infer the concrete type based on other arguments. |
| 60 | + return nil |
| 61 | + } |
| 62 | + return contentType |
| 63 | + } ?? .preferred(forEncodingQuality: encodingQuality) |
| 64 | + preferredName = (preferredName as NSString).appendingPathExtension(for: contentType) |
| 65 | + imageContainer.contentType = contentType |
| 66 | + } else { |
| 67 | + // The caller can't provide a content type, so we'll pick one for them. |
| 68 | + let ext = if encodingQuality < 1.0 { |
| 69 | + "jpg" |
| 70 | + } else { |
| 71 | + "png" |
| 72 | + } |
| 73 | + if (preferredName as NSString).pathExtension.caseInsensitiveCompare(ext) != .orderedSame { |
| 74 | + preferredName = (preferredName as NSString).appendingPathExtension(ext) ?? preferredName |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + self.init(imageContainer, named: preferredName, sourceLocation: sourceLocation) |
| 79 | + } |
| 80 | + |
| 81 | + /// Initialize an instance of this type that encloses the given image. |
| 82 | + /// |
| 83 | + /// - Parameters: |
| 84 | + /// - attachableValue: The value that will be attached to the output of |
| 85 | + /// the test run. |
| 86 | + /// - preferredName: The preferred name of the attachment when writing it |
| 87 | + /// to a test report or to disk. If `nil`, the testing library attempts |
| 88 | + /// to derive a reasonable filename for the attached value. |
| 89 | + /// - contentType: The image format with which to encode `attachableValue`. |
| 90 | + /// If this type does not conform to [`UTType.image`](https://developer.apple.com/documentation/uniformtypeidentifiers/uttype-swift.struct/image), |
| 91 | + /// the result is undefined. Pass `nil` to let the testing library decide |
| 92 | + /// which image format to use. |
| 93 | + /// - encodingQuality: The encoding quality to use when encoding the image. |
| 94 | + /// If the image format used for encoding (specified by the `contentType` |
| 95 | + /// argument) does not support variable-quality encoding, the value of |
| 96 | + /// this argument is ignored. |
| 97 | + /// - sourceLocation: The source location of the call to this initializer. |
| 98 | + /// This value is used when recording issues associated with the |
| 99 | + /// attachment. |
| 100 | + /// |
| 101 | + /// The following system-provided image types conform to the |
| 102 | + /// ``AttachableAsCGImage`` protocol and can be attached to a test: |
| 103 | + /// |
| 104 | + /// - [`CGImage`](https://developer.apple.com/documentation/coregraphics/cgimage) |
| 105 | + @_spi(Experimental) |
| 106 | + @available(_uttypesAPI, *) |
| 107 | + public init<T>( |
| 108 | + _ attachableValue: T, |
| 109 | + named preferredName: String? = nil, |
| 110 | + as contentType: UTType?, |
| 111 | + encodingQuality: Float = 1.0, |
| 112 | + sourceLocation: SourceLocation = #_sourceLocation |
| 113 | + ) where AttachableValue == _AttachableImageContainer<T> { |
| 114 | + self.init(attachableValue: attachableValue, named: preferredName, contentType: contentType, encodingQuality: encodingQuality, sourceLocation: sourceLocation) |
| 115 | + } |
| 116 | + |
| 117 | + /// Initialize an instance of this type that encloses the given image. |
| 118 | + /// |
| 119 | + /// - Parameters: |
| 120 | + /// - attachableValue: The value that will be attached to the output of |
| 121 | + /// the test run. |
| 122 | + /// - preferredName: The preferred name of the attachment when writing it |
| 123 | + /// to a test report or to disk. If `nil`, the testing library attempts |
| 124 | + /// to derive a reasonable filename for the attached value. |
| 125 | + /// - encodingQuality: The encoding quality to use when encoding the image. |
| 126 | + /// If the image format used for encoding (specified by the `contentType` |
| 127 | + /// argument) does not support variable-quality encoding, the value of |
| 128 | + /// this argument is ignored. |
| 129 | + /// - sourceLocation: The source location of the call to this initializer. |
| 130 | + /// This value is used when recording issues associated with the |
| 131 | + /// attachment. |
| 132 | + /// |
| 133 | + /// The following system-provided image types conform to the |
| 134 | + /// ``AttachableAsCGImage`` protocol and can be attached to a test: |
| 135 | + /// |
| 136 | + /// - [`CGImage`](https://developer.apple.com/documentation/coregraphics/cgimage) |
| 137 | + @_spi(Experimental) |
| 138 | + public init<T>( |
| 139 | + _ attachableValue: T, |
| 140 | + named preferredName: String? = nil, |
| 141 | + encodingQuality: Float = 1.0, |
| 142 | + sourceLocation: SourceLocation = #_sourceLocation |
| 143 | + ) where AttachableValue == _AttachableImageContainer<T> { |
| 144 | + self.init(attachableValue: attachableValue, named: preferredName, contentType: nil, encodingQuality: encodingQuality, sourceLocation: sourceLocation) |
| 145 | + } |
| 146 | +} |
| 147 | +#endif |
0 commit comments