Skip to content

Commit e245c54

Browse files
authored
Add a cross-import overlay with CoreImage to allow attaching CIImages. (#1195)
This PR adds on to the Core Graphics cross-import overlay added in #827 to allow attaching instances of `CIImage` to a test. > [!NOTE] > Image attachments remain an experimental feature. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent 743104e commit e245c54

File tree

8 files changed

+78
-2
lines changed

8 files changed

+78
-2
lines changed

Package.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ let package = Package(
9090
targets: [
9191
"_Testing_AppKit",
9292
"_Testing_CoreGraphics",
93+
"_Testing_CoreImage",
9394
]
9495
)
9596
]
@@ -137,6 +138,7 @@ let package = Package(
137138
"Testing",
138139
"_Testing_AppKit",
139140
"_Testing_CoreGraphics",
141+
"_Testing_CoreImage",
140142
"_Testing_Foundation",
141143
"MemorySafeTestingTests",
142144
],
@@ -218,6 +220,15 @@ let package = Package(
218220
path: "Sources/Overlays/_Testing_CoreGraphics",
219221
swiftSettings: .packageSettings + .enableLibraryEvolution()
220222
),
223+
.target(
224+
name: "_Testing_CoreImage",
225+
dependencies: [
226+
"Testing",
227+
"_Testing_CoreGraphics",
228+
],
229+
path: "Sources/Overlays/_Testing_CoreImage",
230+
swiftSettings: .packageSettings + .enableLibraryEvolution()
231+
),
221232
.target(
222233
name: "_Testing_Foundation",
223234
dependencies: [

Sources/Overlays/_Testing_AppKit/ReexportTesting.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
99
//
1010

11-
@_exported public import Testing
12-
@_exported public import _Testing_CoreGraphics
11+
@_exported @_spi(Experimental) @_spi(ForToolsIntegrationOnly) public import Testing
12+
@_exported @_spi(Experimental) @_spi(ForToolsIntegrationOnly) public import _Testing_CoreGraphics

Sources/Overlays/_Testing_CoreGraphics/Attachments/AttachableAsCGImage.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ private import ImageIO
2424
/// be attached to a test:
2525
///
2626
/// - [`CGImage`](https://developer.apple.com/documentation/coregraphics/cgimage)
27+
/// - [`CIImage`](https://developer.apple.com/documentation/coreimage/ciimage)
2728
/// - [`NSImage`](https://developer.apple.com/documentation/appkit/nsimage)
2829
/// (macOS)
2930
///

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ extension Attachment {
3131
/// ``AttachableAsCGImage`` protocol and can be attached to a test:
3232
///
3333
/// - [`CGImage`](https://developer.apple.com/documentation/coregraphics/cgimage)
34+
/// - [`CIImage`](https://developer.apple.com/documentation/coreimage/ciimage)
3435
/// - [`NSImage`](https://developer.apple.com/documentation/appkit/nsimage)
3536
/// (macOS)
3637
///
@@ -68,6 +69,7 @@ extension Attachment {
6869
/// ``AttachableAsCGImage`` protocol and can be attached to a test:
6970
///
7071
/// - [`CGImage`](https://developer.apple.com/documentation/coregraphics/cgimage)
72+
/// - [`CIImage`](https://developer.apple.com/documentation/coreimage/ciimage)
7173
/// - [`NSImage`](https://developer.apple.com/documentation/appkit/nsimage)
7274
/// (macOS)
7375
///

Sources/Overlays/_Testing_CoreGraphics/Attachments/_AttachableImageWrapper.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import UniformTypeIdentifiers
4747
/// to the ``AttachableAsCGImage`` protocol and can be attached to a test:
4848
///
4949
/// - [`CGImage`](https://developer.apple.com/documentation/coregraphics/cgimage)
50+
/// - [`CIImage`](https://developer.apple.com/documentation/coreimage/ciimage)
5051
/// - [`NSImage`](https://developer.apple.com/documentation/appkit/nsimage)
5152
/// (macOS)
5253
@_spi(Experimental)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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(CoreImage)
12+
public import CoreImage
13+
@_spi(Experimental) public import _Testing_CoreGraphics
14+
15+
@_spi(Experimental)
16+
extension CIImage: AttachableAsCGImage {
17+
public var attachableCGImage: CGImage {
18+
get throws {
19+
guard let result = CIContext().createCGImage(self, from: extent) else {
20+
throw ImageAttachmentError.couldNotCreateCGImage
21+
}
22+
return result
23+
}
24+
}
25+
26+
public func _makeCopyForAttachment() -> Self {
27+
// CIImage is documented as thread-safe, but does not conform to Sendable.
28+
// It conforms to NSCopying but does not actually copy itself, so there's no
29+
// point in calling copy().
30+
self
31+
}
32+
}
33+
#endif
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
@_exported @_spi(Experimental) @_spi(ForToolsIntegrationOnly) public import Testing
12+
@_exported @_spi(Experimental) @_spi(ForToolsIntegrationOnly) public import _Testing_CoreGraphics

Tests/TestingTests/AttachmentTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ import _Testing_Foundation
2222
import CoreGraphics
2323
@_spi(Experimental) import _Testing_CoreGraphics
2424
#endif
25+
#if canImport(CoreImage)
26+
import CoreImage
27+
@_spi(Experimental) import _Testing_CoreImage
28+
#endif
2529
#if canImport(UniformTypeIdentifiers)
2630
import UniformTypeIdentifiers
2731
#endif
@@ -598,6 +602,18 @@ extension AttachmentTests {
598602
}
599603
#endif
600604

605+
#if canImport(CoreImage)
606+
@available(_uttypesAPI, *)
607+
@Test func attachCIImage() throws {
608+
let image = CIImage(cgImage: try Self.cgImage.get())
609+
let attachment = Attachment(image, named: "diamond.jpg")
610+
#expect(attachment.attachableValue === image)
611+
try attachment.attachableValue.withUnsafeBytes(for: attachment) { buffer in
612+
#expect(buffer.count > 32)
613+
}
614+
}
615+
#endif
616+
601617
#if canImport(AppKit)
602618
static var nsImage: NSImage {
603619
get throws {

0 commit comments

Comments
 (0)