-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add object lifetime tests to validate that C++ and Swift objects are …
…destroyed (#428)
- Loading branch information
1 parent
97274d2
commit 48d49e1
Showing
4 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import XCTest | ||
import WindowsRuntime | ||
import WinRTComponent | ||
|
||
class LifetimeTests: WinRTTestCase { | ||
func testActivatedObjectDestroyedWhenUnreferencedFromSwift() throws { | ||
var destroyed: Bool = false | ||
withExtendedLifetime(try DestructionCallback { destroyed = true }) { | ||
XCTAssertFalse(destroyed) | ||
} | ||
XCTAssertTrue(destroyed) | ||
} | ||
|
||
func testActivatedObjectDestroyedWhenUnreferencedFromWinRT() throws { | ||
var destroyed: Bool = false | ||
let objectReferencer = try ObjectReferencer( | ||
try DestructionCallback { destroyed = true }) | ||
XCTAssertFalse(destroyed) | ||
try objectReferencer.clear() | ||
XCTAssertTrue(destroyed) | ||
} | ||
|
||
class ComposedDestructionCallback: DestructionCallback, @unchecked Sendable { | ||
private let deinitCallback: () -> Void | ||
|
||
public init(winRT: @escaping () -> Void, swift: @escaping () -> Void) throws { | ||
self.deinitCallback = swift | ||
try super.init(winRT) | ||
} | ||
|
||
deinit { | ||
deinitCallback() | ||
} | ||
} | ||
|
||
func testComposedObjectDestroyedWhenUnreferencedFromSwift() throws { | ||
var destroyed: Bool = false | ||
var deinited: Bool = false | ||
withExtendedLifetime(try ComposedDestructionCallback( | ||
winRT: { destroyed = true }, | ||
swift: { deinited = true })) { | ||
XCTAssertFalse(destroyed) | ||
XCTAssertFalse(deinited) | ||
} | ||
XCTAssertTrue(destroyed) | ||
XCTAssertTrue(deinited) | ||
} | ||
|
||
func testComposedObjectDestroyedWhenUnreferencedFromWinRT() throws { | ||
var destroyed: Bool = false | ||
var deinited: Bool = false | ||
let objectReferencer = try ObjectReferencer( | ||
try ComposedDestructionCallback( | ||
winRT: { destroyed = true }, | ||
swift: { deinited = true })) | ||
XCTAssertFalse(destroyed) | ||
XCTAssertFalse(deinited) | ||
try objectReferencer.clear() | ||
XCTAssertTrue(destroyed) | ||
XCTAssertTrue(deinited) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include "pch.h" | ||
#include "DestructionCallback.g.h" | ||
|
||
namespace winrt::WinRTComponent::implementation | ||
{ | ||
struct DestructionCallback : DestructionCallbackT<DestructionCallback> | ||
{ | ||
DestructionCallback(winrt::WinRTComponent::MinimalDelegate const& callback) : m_callback(callback) {} | ||
~DestructionCallback() { m_callback(); } | ||
|
||
winrt::WinRTComponent::MinimalDelegate m_callback; | ||
}; | ||
} | ||
namespace winrt::WinRTComponent::factory_implementation | ||
{ | ||
struct DestructionCallback : DestructionCallbackT<DestructionCallback, implementation::DestructionCallback> | ||
{ | ||
}; | ||
} | ||
|
||
#include "DestructionCallback.g.cpp" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#include "MinimalTypes.idl" | ||
|
||
namespace WinRTComponent | ||
{ | ||
[default_interface] | ||
unsealed runtimeclass DestructionCallback | ||
{ | ||
DestructionCallback(MinimalDelegate callback); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters