Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private import ImageIO
/// @Available(Swift, introduced: 6.3)
/// }
@available(_uttypesAPI, *)
public protocol AttachableAsCGImage: SendableMetatype {
public protocol AttachableAsCGImage: _AttachableAsImage, SendableMetatype {
/// An instance of `CGImage` representing this image.
///
/// - Throws: Any error that prevents the creation of an image.
Expand Down Expand Up @@ -68,22 +68,6 @@ public protocol AttachableAsCGImage: SendableMetatype {
/// This property is not part of the public interface of the testing
/// library. It may be removed in a future update.
var _attachmentScaleFactor: CGFloat { get }

/// Make a copy of this instance to pass to an attachment.
///
/// - Returns: A copy of `self`, or `self` if no copy is needed.
///
/// The testing library uses this function to take ownership of image
/// resources that test authors pass to it. If possible, make a copy of or add
/// a reference to `self`. If this type does not support making copies, return
/// `self` verbatim.
///
/// The default implementation of this function when `Self` conforms to
/// `Sendable` simply returns `self`.
///
/// This function is not part of the public interface of the testing library.
/// It may be removed in a future update.
func _copyAttachableValue() -> Self
}

@available(_uttypesAPI, *)
Expand All @@ -95,6 +79,8 @@ extension AttachableAsCGImage {
public var _attachmentScaleFactor: CGFloat {
1.0
}

public func _deinitializeAttachableValue() {}
}

@available(_uttypesAPI, *)
Expand Down
1 change: 0 additions & 1 deletion Sources/Overlays/_Testing_CoreGraphics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ if(APPLE)
Attachments/_AttachableImageWrapper+AttachableWrapper.swift
Attachments/AttachableAsCGImage.swift
Attachments/AttachableImageFormat+UTType.swift
Attachments/Attachment+AttachableAsCGImage.swift
Attachments/CGImage+AttachableAsCGImage.swift
ReexportTesting.swift)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public protocol _AttachableByAddressAsIWICBitmapSource {
/// you have an image in another format that needs to be attached to a test,
/// first convert it to an instance of one of the types above.
@_spi(Experimental)
public protocol AttachableAsIWICBitmapSource: SendableMetatype {
public protocol AttachableAsIWICBitmapSource: _AttachableAsImage, SendableMetatype {
/// Create a WIC bitmap source representing an instance of this type.
///
/// - Returns: A pointer to a new WIC bitmap source representing this image.
Expand Down Expand Up @@ -145,39 +145,6 @@ public protocol AttachableAsIWICBitmapSource: SendableMetatype {
func _copyAttachableIWICBitmapSource(
using factory: UnsafeMutablePointer<IWICImagingFactory>
) throws -> UnsafeMutablePointer<IWICBitmapSource>

/// Make a copy of this instance.
///
/// - Returns: A copy of `self`, or `self` if this type does not support a
/// copying operation.
///
/// The testing library uses this function to take ownership of image
/// resources that test authors pass to it. If possible, make a copy of or add
/// a reference to `self`. If this type does not support making copies, return
/// `self` verbatim.
///
/// The default implementation of this function when `Self` conforms to
/// `Sendable` simply returns `self`.
///
/// This function is not part of the public interface of the testing library.
/// It may be removed in a future update.
func _copyAttachableValue() -> Self

/// Manually deinitialize any resources associated with this image.
///
/// The implementation of this function cleans up any resources (such as
/// handles or COM objects) associated with this image. The testing library
/// automatically invokes this function as needed.
///
/// This function is not responsible for releasing the image returned from
/// `_copyAttachableIWICBitmapSource(using:)`.
///
/// The default implementation of this function when `Self` conforms to
/// `Sendable` does nothing.
///
/// This function is not part of the public interface of the testing library.
/// It may be removed in a future update.
func _deinitializeAttachableValue()
}

extension AttachableAsIWICBitmapSource {
Expand All @@ -187,12 +154,4 @@ extension AttachableAsIWICBitmapSource {
try copyAttachableIWICBitmapSource()
}
}

extension AttachableAsIWICBitmapSource where Self: Sendable {
public func _copyAttachableValue() -> Self {
self
}

public func _deinitializeAttachableValue() {}
}
#endif

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ private import Testing
public import WinSDK

@_spi(Experimental)
extension UnsafeMutablePointer: AttachableAsIWICBitmapSource where Pointee: _AttachableByAddressAsIWICBitmapSource {
extension UnsafeMutablePointer: _AttachableAsImage, AttachableAsIWICBitmapSource where Pointee: _AttachableByAddressAsIWICBitmapSource {
public func copyAttachableIWICBitmapSource() throws -> UnsafeMutablePointer<IWICBitmapSource> {
let factory = try IWICImagingFactory.create()
defer {
Expand All @@ -30,7 +30,7 @@ extension UnsafeMutablePointer: AttachableAsIWICBitmapSource where Pointee: _Att
Pointee._copyAttachableValue(at: self)
}

public consuming func _deinitializeAttachableValue() {
public func _deinitializeAttachableValue() {
Pointee._deinitializeAttachableValue(at: self)
}
}
Expand Down
1 change: 0 additions & 1 deletion Sources/Overlays/_Testing_WinSDK/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
Attachments/_AttachableImageWrapper+AttachableWrapper.swift
Attachments/AttachableAsIWICBitmapSource.swift
Attachments/AttachableImageFormat+CLSID.swift
Attachments/Attachment+AttachableAsIWICBitmapSource.swift
Attachments/HBITMAP+AttachableAsIWICBitmapSource.swift
Attachments/HICON+AttachableAsIWICBitmapSource.swift
Attachments/IWICBitmapSource+AttachableAsIWICBitmapSource.swift
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//

#if SWT_TARGET_OS_APPLE && canImport(CoreGraphics)
public import Testing

@available(_uttypesAPI, *)
extension Attachment {
/// Initialize an instance of this type that encloses the given image.
Expand Down Expand Up @@ -52,12 +49,8 @@ extension Attachment {
named preferredName: String? = nil,
as imageFormat: AttachableImageFormat? = nil,
sourceLocation: SourceLocation = #_sourceLocation
) where T: AttachableAsCGImage, AttachableValue == _AttachableImageWrapper<T> {
let imageWrapper = _AttachableImageWrapper(
image: image._copyAttachableValue(),
imageFormat: imageFormat,
deinitializingWith: { _ in }
)
) where AttachableValue: _AttachableImageWrapper<T> & AttachableWrapper {
let imageWrapper = AttachableValue(image: image, imageFormat: imageFormat)
self.init(imageWrapper, named: preferredName, sourceLocation: sourceLocation)
}

Expand Down Expand Up @@ -99,7 +92,7 @@ extension Attachment {
named preferredName: String? = nil,
as imageFormat: AttachableImageFormat? = nil,
sourceLocation: SourceLocation = #_sourceLocation
) where T: AttachableAsCGImage, AttachableValue == _AttachableImageWrapper<T> {
) where AttachableValue: _AttachableImageWrapper<T> & AttachableWrapper {
let attachment = Self(image, named: preferredName, as: imageFormat, sourceLocation: sourceLocation)
Self.record(attachment, sourceLocation: sourceLocation)
}
Expand All @@ -109,11 +102,10 @@ extension Attachment {

@_spi(Experimental) // STOP: not part of ST-0014
@available(_uttypesAPI, *)
extension Attachment where AttachableValue: AttachableWrapper, AttachableValue.Wrapped: AttachableAsCGImage {
extension Attachment where AttachableValue: AttachableWrapper, AttachableValue.Wrapped: _AttachableAsImage {
/// The image format to use when encoding the represented image.
@_disfavoredOverload public var imageFormat: AttachableImageFormat? {
// FIXME: no way to express `where AttachableValue == _AttachableImageWrapper<???>` on a property (see rdar://47559973)
(attachableValue as? _AttachableImageWrapper<AttachableValue.Wrapped>)?.imageFormat
}
}
#endif
54 changes: 54 additions & 0 deletions Sources/Testing/Attachments/Images/_AttachableAsImage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024–2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//

/// A protocol describing images that can be converted to instances of
/// [`Attachment`](https://developer.apple.com/documentation/testing/attachment).
///
/// This protocol acts as an abstract, platform-independent base protocol for
/// ``AttachableAsCGImage`` and ``AttachableAsIWICBitmapSource``.
///
/// @Comment {
/// A future Swift Evolution proposal will promote this protocol to API so
/// that we don't need to underscore its name.
/// }
@available(_uttypesAPI, *)
public protocol _AttachableAsImage: SendableMetatype {
/// Make a copy of this instance to pass to an attachment.
///
/// - Returns: A copy of `self`, or `self` if no copy is needed.
///
/// The testing library uses this function to take ownership of image
/// resources that test authors pass to it. If possible, make a copy of or add
/// a reference to `self`. If this type does not support making copies, return
/// `self` verbatim.
///
/// The default implementation of this function when `Self` conforms to
/// `Sendable` simply returns `self`.
///
/// This function is not part of the public interface of the testing library.
/// It may be removed in a future update.
func _copyAttachableValue() -> Self

/// Manually deinitialize any resources associated with this image.
///
/// The implementation of this function cleans up any resources (such as
/// handles or COM objects) associated with this image. The testing library
/// automatically invokes this function as needed.
///
/// This function is not responsible for releasing the image returned from
/// `_copyAttachableIWICBitmapSource(using:)`.
///
/// The default implementation of this function when `Self` conforms to
/// `Sendable` does nothing.
///
/// This function is not part of the public interface of the testing library.
/// It may be removed in a future update.
func _deinitializeAttachableValue()
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,20 @@
/// | Windows | [`HBITMAP`](https://learn.microsoft.com/en-us/windows/win32/gdi/bitmaps), [`HICON`](https://learn.microsoft.com/en-us/windows/win32/menurc/icons), [`IWICBitmapSource`](https://learn.microsoft.com/en-us/windows/win32/api/wincodec/nn-wincodec-iwicbitmapsource) (including its subclasses declared by Windows Imaging Component) |
/// }
@available(_uttypesAPI, *)
public final class _AttachableImageWrapper<Image>: Sendable {
public final class _AttachableImageWrapper<Image>: Sendable where Image: _AttachableAsImage {
/// The underlying image.
private nonisolated(unsafe) let _image: Image

/// The image format to use when encoding the represented image.
package let imageFormat: AttachableImageFormat?

/// A deinitializer function to call to clean up `image`.
private let _deinit: @Sendable (consuming Image) -> Void

package init(image: Image, imageFormat: AttachableImageFormat?, deinitializingWith `deinit`: @escaping @Sendable (consuming Image) -> Void) {
self._image = image
init(image: Image, imageFormat: AttachableImageFormat?) {
self._image = image._copyAttachableValue()
self.imageFormat = imageFormat
self._deinit = `deinit`
}

deinit {
_deinit(_image)
_image._deinitializeAttachableValue()
}
}

Expand Down
2 changes: 2 additions & 0 deletions Sources/Testing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ add_library(Testing
ABI/Encoded/ABI.EncodedIssue.swift
ABI/Encoded/ABI.EncodedMessage.swift
ABI/Encoded/ABI.EncodedTest.swift
Attachments/Images/_AttachableAsImage.swift
Attachments/Images/_AttachableImageWrapper.swift
Attachments/Images/AttachableImageFormat.swift
Attachments/Images/Attachment+_AttachableAsImage.swift
Attachments/Images/ImageAttachmentError.swift
Attachments/Attachable.swift
Attachments/AttachableWrapper.swift
Expand Down