Add unit tests for GraphicsDevice.GetDefault() contract

このコミットが含まれているのは:
Sergio Pedri committed 2022-07-31 23:26:12 +02:00
1 親 27a5410a72
コミット 4b3dd0ecf9
2ファイル変更
+77 -1

検索条件に一致するファイルはありません

+1 -1
ファイルの表示
@@ -186,7 +186,7 @@ public class DeviceLostTests
/// Removes the underlying device for a given <see cref="GraphicsDevice"/> instance and waits for it to be reported.
/// </summary>
/// <param name="graphicsDevice">The target <see cref="GraphicsDevice"/> instance.</param>
private static async Task RemoveDeviceAsync(GraphicsDevice graphicsDevice)
internal static async Task RemoveDeviceAsync(GraphicsDevice graphicsDevice)
{
TaskCompletionSource<object?> tcs = new();
+76
ファイルの表示
@@ -0,0 +1,76 @@
using System;
using System.Threading.Tasks;
using ComputeSharp.Interop;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
namespace ComputeSharp.Tests.DeviceLost;
[TestClass]
[TestCategory("GetDefaultDevice")]
public class GetDefaultDeviceTests
{
[TestMethod]
public async Task GetDefault_VerifyContract()
{
GraphicsDevice device1 = GraphicsDevice.GetDefault();
// The default device is never null (or the previous call would've thrown an exception)
Assert.IsNotNull(device1);
GraphicsDevice device2 = GraphicsDevice.GetDefault();
// Assuming the device hasn't been disposed (tests don't run concurrently), calling
// GraphicsDevice.GetDefault() again will always return the same cached instance.
Assert.AreSame(device1, device2);
await DeviceLostTests.RemoveDeviceAsync(device1);
// The device is lost but not disposed, so the same instance should be returned yet again
GraphicsDevice device3 = GraphicsDevice.GetDefault();
Assert.AreSame(device1, device3);
device1.Dispose();
// Calling GraphicsDevice.GetDefault() after disposing it should always succeed...
GraphicsDevice device4 = GraphicsDevice.GetDefault();
// But it will return a new instance (assuming the device has been disposed properly).
// That is, the caller has to ensure that no other object is keeping the device alive.
Assert.AreNotSame(device1, device4);
// The LUID should match though, as the same adapter should be returned
Assert.AreEqual(device1.Luid, device4.Luid);
unsafe void GetDefault_VerifyContract_Part2()
{
using ComPtr<ID3D12Device> d3D12Device = default;
// Increment the reference count on the internal device, causing it to remain alive
InteropServices.GetID3D12Device(device4, Windows.__uuidof<ID3D12Device>(), (void**)d3D12Device.GetAddressOf());
device4.Dispose();
_ = d3D12Device.Get()->AddRef();
uint refCount = d3D12Device.Get()->Release();
// Sanity check that this is in fact the only reference keeping the device alive
Assert.AreEqual(refCount, 1u);
HRESULT removalReason = d3D12Device.Get()->GetDeviceRemovedReason();
// Sanity check that the device is in fact removed
Assert.AreEqual((int)removalReason, DXGI.DXGI_ERROR_DEVICE_REMOVED);
// Calling GraphicsDevice.GetDefault() will now throw, because the native device has not been disposed properly
Assert.ThrowsException<InvalidOperationException>(() => GraphicsDevice.GetDefault());
}
await DeviceLostTests.RemoveDeviceAsync(device4);
GetDefault_VerifyContract_Part2();
}
}