Skip to content

Windows image attachments #1245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 30 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
03d8390
[WIP] GDI+ Image overlay
grynspan Jul 9, 2025
a2f76fc
Merge branch 'main' into jgrynspan/gdiplus-image-overlay
Jul 24, 2025
bfcf2e0
More work
grynspan Jul 29, 2025
3b77380
HBITMAP working now
grynspan Jul 29, 2025
7737882
Documentation comments etc.
grynspan Jul 29, 2025
fa90923
Opt-out of calling GdiplusStartup() if needed
grynspan Jul 29, 2025
e02d699
Merge remote-tracking branch 'origin/main' into jgrynspan/gdiplus-ima…
grynspan Jul 29, 2025
00475be
Add HICON support, describe errors
grynspan Jul 30, 2025
c5dc10b
Refactor around pointers
grynspan Jul 30, 2025
3a420d7
Missing files
grynspan Jul 30, 2025
9d860e6
Lots of documentation, rearranging of modules, and general cleanup
grynspan Jul 30, 2025
2aa478c
Don't use _Pointer in the interface
grynspan Jul 30, 2025
af7086e
Make UnsafeMutablePointer recursively conformant to AttachableAsGDIPl…
grynspan Jul 30, 2025
c2e3f04
Missing a file, sigh
grynspan Jul 30, 2025
2ec1708
Get type inference working
grynspan Jul 30, 2025
fd22406
Define an abstraction over OpaquePointer to use until Gdiplus.Image c…
grynspan Jul 30, 2025
16c9e02
Stop forgetting my files!
grynspan Jul 30, 2025
97c7150
Fix typo
grynspan Jul 30, 2025
4750c9d
Work around SIL crash, add init(pathExtension:), fix some whitespace …
grynspan Jul 31, 2025
d2d1bec
Cleanup, _withGDIPlusImage -> _copyAttachableGDIPlusImage
grynspan Jul 31, 2025
a8f3c68
Fix typos
grynspan Jul 31, 2025
fb4ce8e
Simplify the GDI+ image logic by having the protocol just create them…
grynspan Jul 31, 2025
a9840b6
Remove some redundant logic around CLSIDs
grynspan Jul 31, 2025
0a079db
Get rid of some redundant code
grynspan Jul 31, 2025
f2dc015
Microsoft defines DWORD as unsigned long, not unsigned int (despite t…
grynspan Jul 31, 2025
241f5ba
Suppress some warnings
grynspan Jul 31, 2025
031ba94
Merge branch 'main' into jgrynspan/gdiplus-image-overlay
grynspan Jul 31, 2025
125989b
Oh right, that buffer is used
grynspan Jul 31, 2025
e175ccd
Merge remote-tracking branch 'origin' into jgrynspan/gdiplus-image-ov…
grynspan Jul 31, 2025
ec54a9e
Merge branch 'jgrynspan/gdiplus-image-overlay' of github.com:swiftlan…
grynspan Jul 31, 2025
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
10 changes: 10 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ let package = Package(
"_Testing_CoreGraphics",
"_Testing_CoreImage",
"_Testing_UIKit",
"_Testing_WinSDK",
]
)
]
Expand Down Expand Up @@ -135,6 +136,7 @@ let package = Package(
"_Testing_CoreImage",
"_Testing_Foundation",
"_Testing_UIKit",
"_Testing_WinSDK",
"MemorySafeTestingTests",
],
swiftSettings: .packageSettings
Expand Down Expand Up @@ -246,6 +248,14 @@ let package = Package(
path: "Sources/Overlays/_Testing_UIKit",
swiftSettings: .packageSettings + .enableLibraryEvolution()
),
.target(
name: "_Testing_WinSDK",
dependencies: [
"Testing",
],
path: "Sources/Overlays/_Testing_WinSDK",
swiftSettings: .packageSettings + .enableLibraryEvolution() + [.interoperabilityMode(.Cxx)]
),

// Utility targets: These are utilities intended for use when developing
// this package, not for distribution.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 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
//

#if os(Windows)
@_spi(Experimental) import Testing
private import _TestingInternals.GDIPlus

internal import WinSDK

/// A protocol describing images that can be converted to instances of
/// ``Testing/Attachment``.
///
/// Instances of types conforming to this protocol do not themselves conform to
/// ``Testing/Attachable``. Instead, the testing library provides additional
/// initializers on ``Testing/Attachment`` that take instances of such types and
/// handle converting them to image data when needed.
///
/// The following system-provided image types conform to this protocol and can
/// be attached to a test:
///
/// - [`HBITMAP`](https://learn.microsoft.com/en-us/windows/win32/gdi/bitmaps)
/// - [`HICON`](https://learn.microsoft.com/en-us/windows/win32/menurc/icons)
///
/// You do not generally need to add your own conformances to this protocol. If
/// 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 _AttachableByAddressAsGDIPlusImage {
/// Create a GDI+ image representing an instance of this type at the given
/// address.
///
/// - Parameters:
/// - imageAddress: The address of the instance of this type.
///
/// - Returns: A pointer to a new GDI+ image representing this image. The
/// caller is responsible for deleting this image when done with it.
///
/// - Throws: Any error that prevented the creation of the GDI+ image.
///
/// - Note: This function returns a value of C++ type `Gdiplus::Image *`. That
/// type cannot be directly represented in Swift. If this function returns a
/// value of any other concrete type, the result is undefined.
///
/// The testing library automatically calls `GdiplusStartup()` and
/// `GdiplusShutdown()` before and after calling this function. This function
/// can therefore assume that GDI+ is correctly configured on the current
/// thread when it is called.
///
/// This function is not part of the public interface of the testing library.
/// It may be removed in a future update.
static func _copyAttachableGDIPlusImage(at imageAddress: UnsafeMutablePointer<Self>) throws -> OpaquePointer

/// Clean up any resources at the given address.
///
/// - Parameters:
/// - imageAddress: The address of the instance of this type.
///
/// The implementation of this function cleans up any resources (such as
/// handles or COM objects) associated with this value. The testing library
/// automatically invokes this function as needed.
///
/// This function is not responsible for deleting the image returned from
/// `_copyAttachableGDIPlusImage(at:)`.
///
/// This function is not part of the public interface of the testing library.
/// It may be removed in a future update.
static func _cleanUpAttachment(at imageAddress: UnsafeMutablePointer<Self>)
}

/// A protocol describing images that can be converted to instances of
/// ``Testing/Attachment``.
///
/// Instances of types conforming to this protocol do not themselves conform to
/// ``Testing/Attachable``. Instead, the testing library provides additional
/// initializers on ``Testing/Attachment`` that take instances of such types and
/// handle converting them to image data when needed.
///
/// The following system-provided image types conform to this protocol and can
/// be attached to a test:
///
/// - [`HBITMAP`](https://learn.microsoft.com/en-us/windows/win32/gdi/bitmaps)
/// - [`HICON`](https://learn.microsoft.com/en-us/windows/win32/menurc/icons)
///
/// You do not generally need to add your own conformances to this protocol. If
/// 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 AttachableAsGDIPlusImage {
/// Create a GDI+ image representing this instance.
///
/// - Returns: A pointer to a new GDI+ image representing this image. The
/// caller is responsible for deleting this image when done with it.
///
/// - Throws: Any error that prevented the creation of the GDI+ image.
///
/// - Note: This function returns a value of C++ type `Gdiplus::Image *`. That
/// type cannot be directly represented in Swift. If this function returns a
/// value of any other concrete type, the result is undefined.
///
/// The testing library automatically calls `GdiplusStartup()` and
/// `GdiplusShutdown()` before and after calling this function. This function
/// can therefore assume that GDI+ is correctly configured on the current
/// thread when it is called.
///
/// This function is not part of the public interface of the testing library.
/// It may be removed in a future update.
func _copyAttachableGDIPlusImage() throws -> OpaquePointer

/// Clean up any resources associated with this instance.
///
/// The implementation of this function cleans up any resources (such as
/// handles or COM objects) associated with this value. The testing library
/// automatically invokes this function as needed.
///
/// This function is not responsible for deleting the image returned from
/// `_copyAttachableGDIPlusImage()`.
///
/// This function is not part of the public interface of the testing library.
/// It may be removed in a future update.
func _cleanUpAttachment()
}
#endif
Loading