using Amphora; using Annulus; using Azimuth; using Blacklight.Core.Cameras; using Clave; using Glass; using Glass.Blacklight; using System; using System.Collections.Generic; using UnitTestSharp; namespace Foundations.UnitTests { public class UiFrameTests : TestFixture { private static readonly Duration Frame = Duration.FromMilliseconds(16); private static BlacklightGlassWorld NewWorld() { return new BlacklightGlassWorld(new ExceptionSandbox(typeof(Screen).Assembly)); } /// /// Remembers the pointer it was last updated with. /// public class PointerWatcher : Panel { public MouseState? Seen; public override void Update(Duration elapsed, UpdateState state) { Seen = state.MouseState; base.Update(elapsed, state); } } public class ReportRig { public readonly FakeHost Host = new FakeHost(); public readonly ExceptionReporter Reporter = new ExceptionReporter(developerMode: true, quit: () => { }); public readonly BlacklightGlassWorld World; public readonly PointerWatcher Watcher = new PointerWatcher { Material = Material.Invisible }; public readonly UiFrame UiFrame; public readonly ExceptionReportDisplay Display; public ReportRig() { World = new BlacklightGlassWorld(Reporter.Sandbox); HostServices services = Host.Services(); UiFrame = new UiFrame(World, services); World.screen.Size = ExceptionReportWindowTests.RoomyScreen; World.Controls.Add(Watcher); Display = new ExceptionReportDisplay(Reporter, services, World); Display.World.screen.Size = ExceptionReportWindowTests.RoomyScreen; UiFrame.ReportDisplay = Display; Host.Pointer.PointerState = new PointerInput { PixelPos = new Vector(5, 5) }; } public void Throw() { Reporter.Catches(ExceptionReportWindowTests.Caught(), new HostWork("update")); } public void Run() { UiFrame.BeginFrame(Frame); UiFrame.BeginDraw(); UiFrame.EndDraw(); } } public class CtorTests : TestFixture { public void AppliesTheServicesToTheWorld() { var host = new FakeHost(); var world = NewWorld(); new UiFrame(world, host.Services()); CheckTrue(host.Router.Remove(world)); } public void AWorldAlreadyBound_IsNotBoundAgain() { var host = new FakeHost(); var world = NewWorld(); host.Services().ApplyTo(world); new UiFrame(world, host.Services()); CheckEqual(1, host.Router.Consumers.Count); } public void RejectsANullWorld() { CheckThrow(typeof(ArgumentNullException)); new UiFrame(null, new FakeHost().Services()); } public void RejectsNullServices() { CheckThrow(typeof(ArgumentNullException)); new UiFrame(NewWorld(), null); } } public class BeginFrameTests : TestFixture { public void AdvancesTheSandboxClock() { var host = new FakeHost(); var world = NewWorld(); var frame = new UiFrame(world, host.Services()); frame.BeginFrame(Frame); CheckEqual(Frame, world.screen.Sandbox.Now); } public void AdvancesTheClockAgainOnTheNextFrame() { var host = new FakeHost(); var world = NewWorld(); var frame = new UiFrame(world, host.Services()); frame.BeginFrame(Frame); frame.BeginDraw(); frame.EndDraw(); frame.BeginFrame(Frame); CheckEqual(Frame + Frame, world.screen.Sandbox.Now); } public void AbandonsAFrameThatWasNeverFinished() { var host = new FakeHost(); var world = NewWorld(); var frame = new UiFrame(world, host.Services()); frame.BeginFrame(Frame); frame.BeginFrame(Frame); frame.BeginDraw(); CheckEqual(new List { "FrameReset" }, host.Window.Calls); } public void DoesNotTouchTheWindow() { var host = new FakeHost(); var world = NewWorld(); var frame = new UiFrame(world, host.Services()); frame.BeginFrame(Frame); CheckEqual(0, host.Window.Calls.Count); } } public class BeginDrawTests : TestFixture { public void ThrowsBeforeBeginFrame() { var host = new FakeHost(); var frame = new UiFrame(NewWorld(), host.Services()); CheckThrow(typeof(InvalidOperationException)); frame.BeginDraw(); } public void ThrowsWhenCalledTwice() { var host = new FakeHost(); var frame = new UiFrame(NewWorld(), host.Services()); frame.BeginFrame(Frame); frame.BeginDraw(); CheckThrow(typeof(InvalidOperationException)); frame.BeginDraw(); } public void UpdatesTheWorldByTheFrameTime() { var host = new FakeHost(); var world = NewWorld(); var frame = new UiFrame(world, host.Services()); var updates = new List(); world.screen.OnUpdate += updates.Add; frame.BeginFrame(Frame); frame.BeginDraw(); CheckEqual(new List { Frame }, updates); } public void ResetsTheWindowsFrame() { var host = new FakeHost(); var frame = new UiFrame(NewWorld(), host.Services()); frame.BeginFrame(Frame); frame.BeginDraw(); CheckEqual(new List { "FrameReset" }, host.Window.Calls); } public void LeavesAHostCursorAloneWhenNothingWantsOne() { var host = new FakeHost(); var surface = new RecordingCursorSurface(); HostServices services = host.Services(); services.CursorSurface = surface; host.Pointer.PointerState = new PointerInput { PixelPos = new Vector(5, 5) }; var frame = new UiFrame(NewWorld(), services); frame.BeginFrame(Frame); frame.BeginDraw(); CheckEqual(0, surface.Set); CheckEqual(0, surface.Cleared); } public void OpensAReportThatIsWaiting() { var rig = new ReportRig(); rig.Throw(); rig.Run(); CheckTrue(rig.Display.IsOpen); CheckTrue(rig.Display.Fallback.IsOpen); } public void GivesTheGamesUiThePointerWhileNoReportIsOpen() { var rig = new ReportRig(); rig.Run(); CheckTrue(rig.Watcher.Seen.HasValue); } public void KeepsThePointerFromTheGamesUiWhileAReportIsOpen() { var rig = new ReportRig(); rig.Throw(); rig.Run(); CheckFalse(rig.Watcher.Seen.HasValue); } public void RunsWithNoPointerAndNoCursorSurface() { var host = new FakeHost(); var frame = new UiFrame(NewWorld(), host.Services()); frame.BeginFrame(Frame); frame.BeginDraw(); CheckEqual(1, host.Window.Calls.Count); } } public class EndDrawTests : TestFixture { public void ThrowsBeforeBeginFrame() { var host = new FakeHost(); var frame = new UiFrame(NewWorld(), host.Services()); CheckThrow(typeof(InvalidOperationException)); frame.EndDraw(); } public void ThrowsBeforeBeginDraw() { var host = new FakeHost(); var frame = new UiFrame(NewWorld(), host.Services()); frame.BeginFrame(Frame); CheckThrow(typeof(InvalidOperationException)); frame.EndDraw(); } public void ThrowsWhenCalledTwice() { var host = new FakeHost(); var frame = new UiFrame(NewWorld(), host.Services()); frame.BeginFrame(Frame); frame.BeginDraw(); frame.EndDraw(); CheckThrow(typeof(InvalidOperationException)); frame.EndDraw(); } public void SubmitsTheFrameAfterTheReset() { var host = new FakeHost(); var frame = new UiFrame(NewWorld(), host.Services()); frame.BeginFrame(Frame); frame.BeginDraw(); frame.EndDraw(); CheckEqual(new List { "FrameReset", "SubmitDrawCalls" }, host.Window.Calls); } public void DrawsAnOpenReport() { var rig = new ReportRig(); rig.Throw(); rig.UiFrame.BeginFrame(Frame); rig.UiFrame.BeginDraw(); rig.Display.World.renderer .RegisterRenderCallback( ExceptionReportDisplayTests.CountingMaterial.Render); var panel = new ExceptionReportDisplayTests.CountingMaterial(); rig.Display.Fallback.Material = panel; rig.UiFrame.EndDraw(); CheckEqual(1, panel.Count.Draws); } public void RunsFrameAfterFrame() { var host = new FakeHost(); var frame = new UiFrame(NewWorld(), host.Services()); for (int i = 0; i < 2; ++i) { frame.BeginFrame(Frame); frame.BeginDraw(); frame.EndDraw(); } CheckEqual(4, host.Window.Calls.Count); } } public class UnbindTests : TestFixture { public void ReleasesTheRouter() { var host = new FakeHost(); var world = NewWorld(); var frame = new UiFrame(world, host.Services()); frame.Unbind(); CheckFalse(host.Router.Remove(world)); } public void UnbindsTheReportDisplay() { var rig = new ReportRig(); rig.UiFrame.Unbind(); CheckEqual(0, rig.Host.Router.Consumers.Count); } } } }