using System; using System.Collections.Generic; using System.Windows.Forms; using Amphora; using UnitTestSharp; namespace Blacklight.Core.UnitTests { public class WinFormsFramePumpTests : TestFixture { public class Rig : IDisposable { public readonly Form Form = new Form(); public readonly WinFormsFramePump Pump; public readonly List Elapsed = new List(); public System.Action OnFrame = null; public Rig() { Pump = new WinFormsFramePump(Form, elapsed => { Elapsed.Add(elapsed); OnFrame?.Invoke(); }); } public void Run() { // The pump yields to any waiting message, and the runner's thread can have some left over. Application.DoEvents(); Pump.RunIdleFrames(); } public void Dispose() { Pump.Stop(); Form.Dispose(); } } public class StartTests : TestFixture { private Rig rig; public override void TestSetup() { rig = new Rig(); } public override void TestTeardown() { rig.Dispose(); } public void BeginsRunning() { rig.Pump.Start(); CheckTrue(rig.Pump.IsRunning); } public void CalledTwice_IsStoppedByOneStop() { rig.Pump.Start(); rig.Pump.Start(); rig.Pump.Stop(); CheckFalse(rig.Pump.IsRunning); } public void NotCalled_LeavesThePumpStopped() { CheckFalse(rig.Pump.IsRunning); } } public class StopTests : TestFixture { private Rig rig; public override void TestSetup() { rig = new Rig(); } public override void TestTeardown() { rig.Dispose(); } public void WithoutStart_ChangesNothing() { rig.Pump.Stop(); CheckFalse(rig.Pump.IsRunning); } } public class RunIdleFramesTests : TestFixture { private Rig rig; public override void TestSetup() { rig = new Rig(); } public override void TestTeardown() { rig.Dispose(); } public void NotStarted_RunsNoFrames() { rig.Run(); CheckEqual(0, rig.Elapsed.Count); } public void RunsFramesUntilAFrameStopsThePump() { rig.OnFrame = () => { if (rig.Elapsed.Count == 3) { rig.Pump.Stop(); } }; rig.Pump.Start(); rig.Run(); CheckEqual(3, rig.Elapsed.Count); } public void FirstFrame_GetsNoElapsedTime() { rig.OnFrame = rig.Pump.Stop; rig.Pump.Start(); rig.Run(); CheckEqual(Duration.Zero, rig.Elapsed[0]); } public void CloseAfterFirstFrame_ClosesTheFormAfterOneFrame() { rig.Pump.CloseAfterFirstFrame = true; rig.Pump.Start(); rig.Run(); CheckEqual(1, rig.Elapsed.Count); CheckTrue(rig.Form.IsDisposed); CheckFalse(rig.Pump.IsRunning); } public void RequestCloseInAFrame_ClosesOnceThatFrameHasEnded() { bool closedDuringFrame = true; rig.OnFrame = () => { rig.Pump.RequestClose(); closedDuringFrame = rig.Form.IsDisposed; }; rig.Pump.Start(); rig.Run(); CheckFalse(closedDuringFrame); CheckEqual(1, rig.Elapsed.Count); CheckTrue(rig.Form.IsDisposed); CheckFalse(rig.Pump.IsRunning); } public void DeviceFailure_StopsThePumpAndReportsAndClosesTheForm() { var failure = new GraphicsDeviceFailedException(GraphicsDeviceFailure.Hung, innerException: null); GraphicsDeviceFailedException reported = null; bool closedWhenReported = true; rig.OnFrame = () => throw failure; rig.Pump.DeviceFailed = exception => { reported = exception; closedWhenReported = rig.Form.IsDisposed; }; rig.Pump.Start(); rig.Run(); CheckEqual(failure, reported); CheckFalse(closedWhenReported); CheckTrue(rig.Form.IsDisposed); CheckFalse(rig.Pump.IsRunning); } public void OtherException_PropagatesAndLeavesNoFrameInProgress() { rig.OnFrame = () => throw new InvalidOperationException(); rig.Pump.Start(); CheckThrow(typeof(InvalidOperationException)); rig.Run(); } [IgnoreTest] private void RunAFrameThatThrows() { rig.OnFrame = () => throw new InvalidOperationException(); rig.Pump.Start(); try { rig.Run(); } catch (InvalidOperationException) { } } public void AfterOtherException_ThePumpIsStopped() { RunAFrameThatThrows(); CheckFalse(rig.Pump.IsRunning); } public void AfterOtherException_RequestCloseClosesAtOnce() { RunAFrameThatThrows(); rig.Pump.RequestClose(); CheckTrue(rig.Form.IsDisposed); } } public class RequestCloseTests : TestFixture { private Rig rig; public override void TestSetup() { rig = new Rig(); } public override void TestTeardown() { rig.Dispose(); } public void OutsideAFrame_ClosesTheFormAndStopsThePump() { rig.Pump.Start(); rig.Pump.RequestClose(); CheckTrue(rig.Form.IsDisposed); CheckFalse(rig.Pump.IsRunning); } } } }