Skip to content

Commit cd25ed3

Browse files
authored
Merge branch 'main' into gsoc-development
2 parents 16e581a + cb090d3 commit cd25ed3

38 files changed

+972
-638
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ let package = Package(
254254
"Testing",
255255
],
256256
path: "Sources/Overlays/_Testing_WinSDK",
257-
swiftSettings: .packageSettings + .enableLibraryEvolution() + [.interoperabilityMode(.Cxx)]
257+
swiftSettings: .packageSettings + .enableLibraryEvolution()
258258
),
259259

260260
// Utility targets: These are utilities intended for use when developing

Sources/Overlays/_Testing_CoreGraphics/Attachments/Attachment+AttachableAsCGImage.swift

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ extension Attachment {
1717
/// Initialize an instance of this type that encloses the given image.
1818
///
1919
/// - Parameters:
20-
/// - attachableValue: The value that will be attached to the output of
21-
/// the test run.
20+
/// - image: The value that will be attached to the output of the test run.
2221
/// - preferredName: The preferred name of the attachment when writing it
2322
/// to a test report or to disk. If `nil`, the testing library attempts
2423
/// to derive a reasonable filename for the attached value.
25-
/// - imageFormat: The image format with which to encode `attachableValue`.
24+
/// - imageFormat: The image format with which to encode `image`.
2625
/// - sourceLocation: The source location of the call to this initializer.
2726
/// This value is used when recording issues associated with the
2827
/// attachment.
@@ -45,12 +44,12 @@ extension Attachment {
4544
/// correspond to an image format the operating system knows how to write, the
4645
/// testing library selects an appropriate image format for you.
4746
public init<T>(
48-
_ attachableValue: T,
47+
_ image: T,
4948
named preferredName: String? = nil,
5049
as imageFormat: AttachableImageFormat? = nil,
5150
sourceLocation: SourceLocation = #_sourceLocation
5251
) where AttachableValue == _AttachableImageWrapper<T> {
53-
let imageWrapper = _AttachableImageWrapper(image: attachableValue, imageFormat: imageFormat)
52+
let imageWrapper = _AttachableImageWrapper(image: image, imageFormat: imageFormat)
5453
self.init(imageWrapper, named: preferredName, sourceLocation: sourceLocation)
5554
}
5655

@@ -61,7 +60,7 @@ extension Attachment {
6160
/// - preferredName: The preferred name of the attachment when writing it to
6261
/// a test report or to disk. If `nil`, the testing library attempts to
6362
/// derive a reasonable filename for the attached value.
64-
/// - imageFormat: The image format with which to encode `attachableValue`.
63+
/// - imageFormat: The image format with which to encode `image`.
6564
/// - sourceLocation: The source location of the call to this function.
6665
///
6766
/// This function creates a new instance of ``Attachment`` wrapping `image`
@@ -94,4 +93,15 @@ extension Attachment {
9493
Self.record(attachment, sourceLocation: sourceLocation)
9594
}
9695
}
96+
97+
@_spi(Experimental) // STOP: not part of ST-0014
98+
@available(_uttypesAPI, *)
99+
extension Attachment where AttachableValue: AttachableWrapper, AttachableValue.Wrapped: AttachableAsCGImage {
100+
/// The image format to use when encoding the represented image.
101+
@_disfavoredOverload
102+
public var imageFormat: AttachableImageFormat? {
103+
// FIXME: no way to express `where AttachableValue == _AttachableImageWrapper<???>` on a property
104+
(attachableValue as? _AttachableImageWrapper<AttachableValue.Wrapped>)?.imageFormat
105+
}
106+
}
97107
#endif

Sources/Overlays/_Testing_WinSDK/Attachments/AttachableAsGDIPlusImage.swift

Lines changed: 0 additions & 129 deletions
This file was deleted.
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
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 os(Windows)
12+
@_spi(Experimental) import Testing
13+
14+
public import WinSDK
15+
16+
/// A protocol describing images that can be converted to instances of
17+
/// ``Testing/Attachment``.
18+
///
19+
/// Instances of types conforming to this protocol do not themselves conform to
20+
/// ``Testing/Attachable``. Instead, the testing library provides additional
21+
/// initializers on ``Testing/Attachment`` that take instances of such types and
22+
/// handle converting them to image data when needed.
23+
///
24+
/// The following system-provided image types conform to this protocol and can
25+
/// be attached to a test:
26+
///
27+
/// - [`HBITMAP`](https://learn.microsoft.com/en-us/windows/win32/gdi/bitmaps)
28+
/// - [`HICON`](https://learn.microsoft.com/en-us/windows/win32/menurc/icons)
29+
/// - [`IWICBitmap`](https://learn.microsoft.com/en-us/windows/win32/api/wincodec/nn-wincodec-iwicbitmap)
30+
///
31+
/// You do not generally need to add your own conformances to this protocol. If
32+
/// you have an image in another format that needs to be attached to a test,
33+
/// first convert it to an instance of one of the types above.
34+
@_spi(Experimental)
35+
public protocol _AttachableByAddressAsIWICBitmap {
36+
/// Create a WIC bitmap representing an instance of this type at the given
37+
/// address.
38+
///
39+
/// - Parameters:
40+
/// - imageAddress: The address of the instance of this type.
41+
/// - factory: A WIC imaging factory that can be used to create additional
42+
/// WIC objects.
43+
///
44+
/// - Returns: A pointer to a new WIC bitmap representing this image. The
45+
/// caller is responsible for releasing this image when done with it.
46+
///
47+
/// - Throws: Any error that prevented the creation of the WIC bitmap.
48+
///
49+
/// This function is not part of the public interface of the testing library.
50+
/// It may be removed in a future update.
51+
static func _copyAttachableIWICBitmap(
52+
from imageAddress: UnsafeMutablePointer<Self>,
53+
using factory: UnsafeMutablePointer<IWICImagingFactory>
54+
) throws -> UnsafeMutablePointer<IWICBitmap>
55+
56+
/// Make a copy of the instance of this type at the given address.
57+
///
58+
/// - Parameters:
59+
/// - imageAddress: The address of the instance of this type that should be
60+
/// copied.
61+
///
62+
/// - Returns: A copy of `imageAddress`, or `imageAddress` if this type does
63+
/// not support a copying operation.
64+
///
65+
/// - Throws: Any error that prevented copying the value at `imageAddress`.
66+
///
67+
/// The testing library uses this function to take ownership of image
68+
/// resources that test authors pass to it. If possible, make a copy of or add
69+
/// a reference to the value at `imageAddress`. If this type does not support
70+
/// making copies, return `imageAddress` verbatim.
71+
///
72+
/// This function is not part of the public interface of the testing library.
73+
/// It may be removed in a future update.
74+
static func _copyAttachableValue(at imageAddress: UnsafeMutablePointer<Self>) throws -> UnsafeMutablePointer<Self>
75+
76+
/// Manually deinitialize any resources at the given address.
77+
///
78+
/// - Parameters:
79+
/// - imageAddress: The address of the instance of this type.
80+
///
81+
/// The implementation of this function is responsible for balancing a
82+
/// previous call to `_copyAttachableValue(at:)` by cleaning up any resources
83+
/// (such as handles or COM objects) associated with the value at
84+
/// `imageAddress`. The testing library automatically invokes this function as
85+
/// needed. If `_copyAttachableValue(at:)` threw an error, the testing library
86+
/// does not call this function.
87+
///
88+
/// This function is not responsible for releasing the image returned from
89+
/// `_copyAttachableIWICBitmap(from:using:)`.
90+
///
91+
/// This function is not part of the public interface of the testing library.
92+
/// It may be removed in a future update.
93+
static func _deinitializeAttachableValue(at imageAddress: UnsafeMutablePointer<Self>)
94+
}
95+
96+
/// A protocol describing images that can be converted to instances of
97+
/// ``Testing/Attachment``.
98+
///
99+
/// Instances of types conforming to this protocol do not themselves conform to
100+
/// ``Testing/Attachable``. Instead, the testing library provides additional
101+
/// initializers on ``Testing/Attachment`` that take instances of such types and
102+
/// handle converting them to image data when needed.
103+
///
104+
/// The following system-provided image types conform to this protocol and can
105+
/// be attached to a test:
106+
///
107+
/// - [`HBITMAP`](https://learn.microsoft.com/en-us/windows/win32/gdi/bitmaps)
108+
/// - [`HICON`](https://learn.microsoft.com/en-us/windows/win32/menurc/icons)
109+
/// - [`IWICBitmap`](https://learn.microsoft.com/en-us/windows/win32/api/wincodec/nn-wincodec-iwicbitmap)
110+
///
111+
/// You do not generally need to add your own conformances to this protocol. If
112+
/// you have an image in another format that needs to be attached to a test,
113+
/// first convert it to an instance of one of the types above.
114+
@_spi(Experimental)
115+
public protocol AttachableAsIWICBitmap {
116+
/// Create a WIC bitmap representing an instance of this type.
117+
///
118+
/// - Parameters:
119+
/// - factory: A WIC imaging factory that can be used to create additional
120+
/// WIC objects.
121+
///
122+
/// - Returns: A pointer to a new WIC bitmap representing this image. The
123+
/// caller is responsible for releasing this image when done with it.
124+
///
125+
/// - Throws: Any error that prevented the creation of the WIC bitmap.
126+
///
127+
/// This function is not part of the public interface of the testing library.
128+
/// It may be removed in a future update.
129+
borrowing func _copyAttachableIWICBitmap(
130+
using factory: UnsafeMutablePointer<IWICImagingFactory>
131+
) throws -> UnsafeMutablePointer<IWICBitmap>
132+
133+
/// Make a copy of this instance.
134+
///
135+
/// - Returns: A copy of `self`, or `self` if this type does not support a
136+
/// copying operation.
137+
///
138+
/// - Throws: Any error that prevented copying this value.
139+
///
140+
/// The testing library uses this function to take ownership of image
141+
/// resources that test authors pass to it. If possible, make a copy of or add
142+
/// a reference to `self`. If this type does not support making copies, return
143+
/// `self` verbatim.
144+
///
145+
/// This function is not part of the public interface of the testing library.
146+
/// It may be removed in a future update.
147+
func _copyAttachableValue() throws -> Self
148+
149+
/// Manually deinitialize any resources associated with this image.
150+
///
151+
/// The implementation of this function cleans up any resources (such as
152+
/// handles or COM objects) associated with this image. The testing library
153+
/// automatically invokes this function as needed.
154+
///
155+
/// This function is not responsible for releasing the image returned from
156+
/// `_copyAttachableIWICBitmap(using:)`.
157+
///
158+
/// This function is not part of the public interface of the testing library.
159+
/// It may be removed in a future update.
160+
func _deinitializeAttachableValue()
161+
}
162+
163+
extension AttachableAsIWICBitmap {
164+
/// Create a WIC bitmap representing an instance of this type and return it as
165+
/// an instance of `IWICBitmapSource`.
166+
///
167+
/// - Parameters:
168+
/// - factory: A WIC imaging factory that can be used to create additional
169+
/// WIC objects.
170+
///
171+
/// - Returns: A pointer to a new WIC bitmap representing this image. The
172+
/// caller is responsible for releasing this image when done with it.
173+
///
174+
/// - Throws: Any error that prevented the creation of the WIC bitmap.
175+
///
176+
/// This function is a convenience over `_copyAttachableIWICBitmap(using:)`
177+
/// that casts the result of that function to `IWICBitmapSource` (as needed
178+
/// by WIC when it encodes the image.)
179+
borrowing func copyAttachableIWICBitmapSource(
180+
using factory: UnsafeMutablePointer<IWICImagingFactory>
181+
) throws -> UnsafeMutablePointer<IWICBitmapSource> {
182+
let bitmap = try _copyAttachableIWICBitmap(using: factory)
183+
defer {
184+
_ = bitmap.pointee.lpVtbl.pointee.Release(bitmap)
185+
}
186+
187+
return try withUnsafePointer(to: IID_IWICBitmapSource) { IID_IWICBitmapSource in
188+
var bitmapSource: UnsafeMutableRawPointer?
189+
let rQuery = bitmap.pointee.lpVtbl.pointee.QueryInterface(bitmap, IID_IWICBitmapSource, &bitmapSource)
190+
guard rQuery == S_OK, let bitmapSource else {
191+
throw ImageAttachmentError.queryInterfaceFailed(IWICBitmapSource.self, rQuery)
192+
}
193+
return bitmapSource.assumingMemoryBound(to: IWICBitmapSource.self)
194+
}
195+
}
196+
}
197+
#endif

0 commit comments

Comments
 (0)