using Annulus; using Azimuth; using System; using System.Collections.Generic; using UnitTestSharp; namespace Glass.UnitTests { public class ProgressBarTests : TestFixture { public static readonly Rect HorizontalBounds = At(10, 10, 100, 20); public static readonly Rect VerticalBounds = At(10, 10, 20, 100); public static Rect At(Scalar x, Scalar y, Scalar width, Scalar height) { return Rect.FromPositionAndSize(new Vector(x, y), new Vector(width, height)); } [IgnoreTest] private static ProgressBar Bar(Rect bounds, Anchor fillFrom, Scalar progress) { return new ProgressBar { Bounds = bounds, FillFrom = fillFrom, Progress = progress }; } [IgnoreTest] private static List OrderOf(ControlContainer controls) { return ControlContainerTests.BackToFrontOrderOf(controls); } public class CtorTests : TestFixture { public void Always_DrawsEmptyContentThenFilledContentThenTheBarsOwnChildren() { var bar = new ProgressBar(); List order = OrderOf(bar.DirectChildren); CheckEqual(3, order.Count); CheckEqual(bar.EmptyContent, order[0]); CheckEqual(bar.FilledContent, order[1]); CheckEqual(bar.CanvasChildren, order[2].CanvasChildren); } } public class CanvasChildrenTests : TestFixture { public void AddedChildren_StayOutOfDirectChildren() { var bar = new ProgressBar(); var child = bar.CanvasChildren.Add(new Control("child")); CheckFalse(OrderOf(bar.DirectChildren).Contains(child)); } public void AddedChildren_AreLaidOutAgainstTheWholeTrack() { ProgressBar bar = Bar(HorizontalBounds, Anchor.Left, progress: 0.25); CheckEqual(At(0, 0, 100, 20), OrderOf(bar.DirectChildren)[2].Bounds); } public void ClicksOffAddedChildren_ReachTheContentBeneath() { var manager = new MouseManager(); ProgressBar bar = manager.Controls.Add(Bar(At(0, 0, 100, 20), Anchor.Left, progress: 0.5)); var filledChild = bar.FilledContent.CanvasChildren.Add(new Control("filled child") { Bounds = At(0, 0, 100, 20), }); bool clicked = false; filledChild.OnClick += (in MouseState _, MouseButtonFlags __) => clicked = true; bar.CanvasChildren.Add(new Control("added child") { Bounds = At(0, 0, 100, 20) }); manager.MouseDown(new MouseState { Position = new Vector(25, 10) }, MouseButtonFlags.Left); manager.MouseUp(new MouseState { Position = new Vector(25, 10) }, MouseButtonFlags.Left); CheckTrue(clicked); } } public class EmptyContentTests : TestFixture { public void Children_AreClippedToTheEmptyPart() { var manager = new MouseManager(); ProgressBar bar = manager.Controls.Add(Bar(At(0, 0, 100, 20), Anchor.Left, progress: 0.5)); var emptyChild = bar.EmptyContent.CanvasChildren.Add(new Control("empty child") { Bounds = At(0, 0, 100, 20), }); bool clicked = false; emptyChild.OnClick += (in MouseState _, MouseButtonFlags __) => clicked = true; manager.MouseDown(new MouseState { Position = new Vector(25, 10) }, MouseButtonFlags.Left); manager.MouseUp(new MouseState { Position = new Vector(25, 10) }, MouseButtonFlags.Left); CheckFalse(clicked); manager.MouseDown(new MouseState { Position = new Vector(75, 10) }, MouseButtonFlags.Left); manager.MouseUp(new MouseState { Position = new Vector(75, 10) }, MouseButtonFlags.Left); CheckTrue(clicked); } } public class ProgressTests : TestFixture { public void ByDefault_IsZero() { CheckEqual(0, new ProgressBar().Progress); } public void WithinZeroToOne_IsKept() { CheckEqual(0.25, new ProgressBar { Progress = 0.25 }.Progress); } public void AboveOne_ClampsToOne() { CheckEqual(1, new ProgressBar { Progress = 1.5 }.Progress); } public void BelowZero_ClampsToZero() { CheckEqual(0, new ProgressBar { Progress = -0.5 }.Progress); } public void NaN_Throws() { var bar = new ProgressBar(); CheckThrow(typeof(ArgumentException)); bar.Progress = Scalar.NaN; } public void Zero_LeavesAnEmptyFillAtTheAnchor() { ProgressBar bar = Bar(HorizontalBounds, Anchor.Right, progress: 0); CheckEqual(At(100, 0, 0, 20), bar.FilledContent.Bounds); CheckEqual(At(0, 0, 100, 20), bar.EmptyContent.Bounds); } public void One_LeavesAnEmptyPartAtTheFarEnd() { ProgressBar bar = Bar(HorizontalBounds, Anchor.Left, progress: 1); CheckEqual(At(0, 0, 100, 20), bar.FilledContent.Bounds); CheckEqual(At(100, 0, 0, 20), bar.EmptyContent.Bounds); } } public class FillFromTests : TestFixture { public void ByDefault_IsLeft() { CheckEqual(Anchor.Left, new ProgressBar().FillFrom); } public void Left_FillsRightwardsFromTheLeftEdge() { ProgressBar bar = Bar(HorizontalBounds, Anchor.Left, progress: 0.25); CheckEqual(At(0, 0, 25, 20), bar.FilledContent.Bounds); CheckEqual(At(25, 0, 75, 20), bar.EmptyContent.Bounds); } public void Right_FillsLeftwardsFromTheRightEdge() { ProgressBar bar = Bar(HorizontalBounds, Anchor.Right, progress: 0.25); CheckEqual(At(75, 0, 25, 20), bar.FilledContent.Bounds); CheckEqual(At(0, 0, 75, 20), bar.EmptyContent.Bounds); } public void Top_FillsDownwardsFromTheTopEdge() { ProgressBar bar = Bar(VerticalBounds, Anchor.Top, progress: 0.25); CheckEqual(At(0, 0, 20, 25), bar.FilledContent.Bounds); CheckEqual(At(0, 25, 20, 75), bar.EmptyContent.Bounds); } public void Bottom_FillsUpwardsFromTheBottomEdge() { ProgressBar bar = Bar(VerticalBounds, Anchor.Bottom, progress: 0.25); CheckEqual(At(0, 75, 20, 25), bar.FilledContent.Bounds); CheckEqual(At(0, 0, 20, 75), bar.EmptyContent.Bounds); } public void Changed_LaysOutAgain() { var bar = new ProgressBar { Bounds = HorizontalBounds, Progress = 0.25 }; bar.FillFrom = Anchor.Right; CheckEqual(At(75, 0, 25, 20), bar.FilledContent.Bounds); } public void Always_GivesBothPartsTheWholeTrack() { ProgressBar bar = Bar(VerticalBounds, Anchor.Bottom, progress: 0.25); CheckEqual(new Vector(20, 100), bar.EmptyContent.TrackSize); CheckEqual(new Vector(20, 100), bar.FilledContent.TrackSize); } } public class ProgressBarMaterialTests : TestFixture { public static readonly Material Background = new Material(); public static readonly Material Empty = new Material(); public static readonly Material Fill = new Material(); public static readonly ProgressBarMaterial FullSet = new ProgressBarMaterial { Background = Background, Empty = Empty, Fill = Fill }; public void Setter_PutsEachMaterialOnItsPart() { var bar = new ProgressBar { ProgressBarMaterial = FullSet }; CheckEqual(Background, bar.Material); CheckEqual(Empty, bar.EmptyContent.Material); CheckEqual(Fill, bar.FilledContent.Material); } public void Getter_RoundTripsWhatTheSetterAssigned() { var bar = new ProgressBar { ProgressBarMaterial = FullSet }; CheckEqual(FullSet, bar.ProgressBarMaterial); } public void BackgroundBorder_InsetsTheTrack() { var bar = new ProgressBar { Bounds = HorizontalBounds, Progress = 0.5 }; bar.ProgressBarMaterial = new ProgressBarMaterial { Background = ButtonMaterialTests.WithBorder(top: 2, bottom: 2, left: 5, right: 5), }; CheckEqual(At(0, 0, 45, 16), bar.FilledContent.Bounds); CheckEqual(At(45, 0, 45, 16), bar.EmptyContent.Bounds); CheckEqual(new Vector(90, 16), bar.FilledContent.TrackSize); } } public class BoundsTests : TestFixture { public void Changed_LaysOutAgain() { ProgressBar bar = Bar(HorizontalBounds, Anchor.Left, progress: 0.25); bar.Bounds = At(10, 10, 200, 30); CheckEqual(At(0, 0, 50, 30), bar.FilledContent.Bounds); CheckEqual(new Vector(200, 30), bar.EmptyContent.TrackSize); } } } }