using System; using System.Windows.Forms; using SharpDX.Direct3D9; using UnitTestSharp; namespace Blacklight.SharpDX9.UnitTesting { // Creating a real device burns past the 500ms setup budget, so each test builds its window in its own // [CpuTimeout]-widened body and the fixture only tears it down. public class SharpDX9BatchFactoryTests : TestFixture { public static bool QuadIsOnItsOwnDevice(SharpDX9BatchFactory factory) { IntPtr expected = factory.GraphicsDevice.NativePointer; using (Device cornerOwner = factory.CornerQuadBuffer.Device) using (Device indexOwner = factory.QuadIndexBuffer.Device) using (Device declarationOwner = factory.PrimitiveDeclaration.Device) { return cornerOwner.NativePointer == expected && indexOwner.NativePointer == expected && declarationOwner.NativePointer == expected; } } public class CtorTests : TestFixture { private Module module; private Form firstWindow; private Form secondWindow; private SharpDX9WindowHandle first; private SharpDX9WindowHandle second; public override void FixtureTeardown() { first?.Dispose(); second?.Dispose(); firstWindow?.Dispose(); secondWindow?.Dispose(); module?.Dispose(); } [CpuTimeout(10000)] public void EachDevice_GetsItsOwnQuad() { module = new Module(); firstWindow = new Form(); secondWindow = new Form(); first = (SharpDX9WindowHandle)module.CreateDrawableSurfaceOnWindow(firstWindow.Handle); second = (SharpDX9WindowHandle)module.CreateDrawableSurfaceOnWindow(secondWindow.Handle); CheckTrue(QuadIsOnItsOwnDevice(first.Backend)); CheckTrue(QuadIsOnItsOwnDevice(second.Backend)); } } public class ReleaseResourcesTests : TestFixture { private Module module; private Form window; private SharpDX9WindowHandle handle; public override void FixtureTeardown() { handle?.Dispose(); window?.Dispose(); module?.Dispose(); } [CpuTimeout(5000)] public void DropsTheQuad() { module = new Module(); window = new Form(); handle = (SharpDX9WindowHandle)module.CreateDrawableSurfaceOnWindow(window.Handle); handle.Backend.ReleaseResources(); CheckNull(handle.Backend.CornerQuadBuffer); CheckNull(handle.Backend.QuadIndexBuffer); CheckNull(handle.Backend.PrimitiveDeclaration); } } public class ResetTests : TestFixture { private Module module; private Form window; private SharpDX9WindowHandle handle; public override void FixtureTeardown() { handle?.Dispose(); window?.Dispose(); module?.Dispose(); } [CpuTimeout(5000)] public void AfterARelease_RebuildsTheQuadOnTheSameDevice() { module = new Module(); window = new Form(); handle = (SharpDX9WindowHandle)module.CreateDrawableSurfaceOnWindow(window.Handle); handle.Backend.ReleaseResources(); handle.Backend.Reset(); CheckTrue(QuadIsOnItsOwnDevice(handle.Backend)); } } } }