using System.Collections.Generic; using Annulus; using Azimuth; using Blacklight.Core; using Blacklight.Core.Drawables; using Prism; using UnitTestSharp; namespace Glass.Blacklight.UnitTests { public class VectorShapesTests : TestFixture { public static readonly Rect Area = Rect.FromPositionAndSize(new Vector(10, 20), new Vector(40, 30)); public static readonly Rect PartOfArea = Rect.FromPositionAndSize(new Vector(10, 20), new Vector(20, 30)); public class Drawn : EntityContainer.IVisitor { public readonly List Drawables = new List(); public readonly List Placements = new List(); void EntityContainer.IVisitor.Visit(in Entity entity) { Drawables.Add(entity.drawable); Placements.Add(entity.modelToWorld); } } public static Drawn Draw(VectorShapes shapes, Material material, Rect rect, DrawState drawState) { var renderer = new BlacklightRenderer(); shapes.Register(renderer); var cel = new Cel(); renderer.Render(cel, material, rect, drawState); var returnMe = new Drawn(); cel.ForEach(returnMe); return returnMe; } public static Drawn Draw(Material material, Rect rect) { return Draw(new VectorShapes(), material, rect, new DrawState()); } public static Drawn Draw(Material material, Rect rect, DrawState drawState) { return Draw(new VectorShapes(), material, rect, drawState); } public static DrawState Clipped => new DrawState { ClipWindow = PartOfArea }; public static AffineMatrix AtAreasTopLeft => AffineMatrix.BuildFromTranslation(Area.TopLeft); public class RegisterTests : TestFixture { public void TeachesTheRendererEveryMaterial() { var shapes = new VectorShapes(); var materials = new Material[] { new RoundedRectMaterial(), new RadioIndicatorMaterial(), new ArrowIndicatorMaterial(), new CheckIndicatorMaterial { IsChecked = true }, }; foreach (Material material in materials) { CheckGreater(Draw(shapes, material, Area, new DrawState()).Drawables.Count, 0); } } } public class RoundedRectTests : TestFixture { public void DrawsOneCurveAtTheRectsTopLeft() { Drawn drawn = Draw(new RoundedRectMaterial(), Area); CheckEqual(1, drawn.Drawables.Count); Check(drawn.Drawables[0] is ClosedQuadraticBezierCurve); CheckEqual(AtAreasTopLeft, drawn.Placements[0]); } public void CarriesTheFillOutlineAndOutlineWidth() { Drawn drawn = Draw(new RoundedRectMaterial { FillColor = Colors.Red, OutlineColor = Colors.Blue, OutlineWidth = 2, }, Area); var curve = (ClosedQuadraticBezierCurve)drawn.Drawables[0]; CheckEqual(Colors.Red.ToArgb(), curve.InsideColor.ToArgb()); CheckEqual(Colors.Blue.ToArgb(), curve.ShellColor.ToArgb()); CheckEqual((Scalar)2, curve.ShellModelSpaceThickness); } public void TheSameLookTwice_ReusesTheCurve() { var shapes = new VectorShapes(); var material = new RoundedRectMaterial { FillColor = Colors.Red }; Drawn first = Draw(shapes, material, Area, new DrawState()); Drawn second = Draw(shapes, material, Area, new DrawState()); Check(ReferenceEquals(first.Drawables[0], second.Drawables[0])); } public void ADifferentFill_BuildsAnotherCurve() { var shapes = new VectorShapes(); Drawn first = Draw(shapes, new RoundedRectMaterial { FillColor = Colors.Red }, Area, new DrawState()); Drawn second = Draw(shapes, new RoundedRectMaterial { FillColor = Colors.Green }, Area, new DrawState()); Check(!ReferenceEquals(first.Drawables[0], second.Drawables[0])); } public void ADifferentSize_BuildsAnotherCurve() { var shapes = new VectorShapes(); var material = new RoundedRectMaterial(); Drawn first = Draw(shapes, material, Area, new DrawState()); Drawn second = Draw(shapes, material, Rect.FromPositionAndSize(Vector.Zero, new Vector(41, 30)), new DrawState()); Check(!ReferenceEquals(first.Drawables[0], second.Drawables[0])); } public void AZeroWidthRect_DrawsNothing() { Drawn drawn = Draw(new RoundedRectMaterial(), Rect.FromPositionAndSize(Vector.Zero, new Vector(0, 5))); CheckEqual(0, drawn.Drawables.Count); } public void AZeroHeightRect_DrawsNothing() { Drawn drawn = Draw(new RoundedRectMaterial(), Rect.FromPositionAndSize(Vector.Zero, new Vector(5, 0))); CheckEqual(0, drawn.Drawables.Count); } public void ARadiusTooBigForTheRect_StillDraws() { Drawn drawn = Draw(new RoundedRectMaterial { CornerRadius = 100 }, Area); CheckEqual(1, drawn.Drawables.Count); Check(drawn.Drawables[0] is ClosedQuadraticBezierCurve); } public void ARectCutByTheClipWindow_DrawsASolidRectangleInsteadOfTheCurve() { Drawn drawn = Draw(new RoundedRectMaterial { FillColor = Colors.Red }, Area, Clipped); CheckEqual(1, drawn.Drawables.Count); Check(drawn.Drawables[0] is Rectangle); } } public class RadioTests : TestFixture { public void Unchecked_DrawsOneCircle() { Drawn drawn = Draw(new RadioIndicatorMaterial(), Area); CheckEqual(1, drawn.Drawables.Count); Check(drawn.Drawables[0] is Ellipse); } public void Checked_DrawsTheCircleThenTheDot() { Drawn drawn = Draw(new RadioIndicatorMaterial { IsChecked = true, FillColor = Colors.Red, OutlineColor = Colors.Blue, DotColor = Colors.Green, OutlineWidth = 3, }, Area); var circle = (Ellipse)drawn.Drawables[0]; var dot = (Ellipse)drawn.Drawables[1]; CheckEqual(2, drawn.Drawables.Count); CheckEqual(Colors.Red.ToArgb(), circle.InsideColor.ToArgb()); CheckEqual(Colors.Blue.ToArgb(), circle.ShellColor.ToArgb()); CheckEqual((Scalar)3, circle.ShellPixelThickness); CheckEqual(Colors.Green.ToArgb(), dot.InsideColor.ToArgb()); CheckEqual(Colors.Green.ToArgb(), dot.ShellColor.ToArgb()); CheckEqual((Scalar)0, dot.ShellPixelThickness); } public void TheDotIsHalfTheCirclesSize() { Drawn drawn = Draw(new RadioIndicatorMaterial { IsChecked = true }, Area); var center = new Vector(30, 35); CheckEqual(Ellipse.ModelToWorld(new Vector(30, 30), position: center), drawn.Placements[0]); CheckEqual(Ellipse.ModelToWorld(new Vector(15, 15), position: center), drawn.Placements[1]); } public void ARectCutByTheClipWindow_DrawsNothing() { Drawn drawn = Draw(new RadioIndicatorMaterial { IsChecked = true }, Area, Clipped); CheckEqual(0, drawn.Drawables.Count); } } public class CheckTests : TestFixture { public void Unchecked_DrawsOnlyTheBox() { Drawn drawn = Draw(new CheckIndicatorMaterial(), Area); CheckEqual(1, drawn.Drawables.Count); Check(drawn.Drawables[0] is ClosedQuadraticBezierCurve); } public void TheBoxCarriesItsFillOutlineAndOutlineWidth() { Drawn drawn = Draw(new CheckIndicatorMaterial { FillColor = Colors.Red, OutlineColor = Colors.Blue, OutlineWidth = 2, }, Area); var box = (ClosedQuadraticBezierCurve)drawn.Drawables[0]; CheckEqual(Colors.Red.ToArgb(), box.InsideColor.ToArgb()); CheckEqual(Colors.Blue.ToArgb(), box.ShellColor.ToArgb()); CheckEqual((Scalar)2, box.ShellModelSpaceThickness); } public void Checked_DrawsTheBoxThenTheTick() { Drawn drawn = Draw(new CheckIndicatorMaterial { IsChecked = true, TickColor = Colors.Green }, Area); var tick = (Polyline)drawn.Drawables[1]; CheckEqual(2, drawn.Drawables.Count); CheckEqual(Colors.Green.ToArgb(), tick.InsideColor.ToArgb()); CheckEqual(AtAreasTopLeft, drawn.Placements[1]); } public void TheTickIsScaledToTheRectsWidth() { Drawn drawn = Draw(new CheckIndicatorMaterial { IsChecked = true }, Area); var tick = (Polyline)drawn.Drawables[1]; CheckEqual((Scalar)(0.09 * 40), tick.InsidePixelThickness); } public void TheSameTickTwice_ReusesIt() { var shapes = new VectorShapes(); var material = new CheckIndicatorMaterial { IsChecked = true }; Drawn first = Draw(shapes, material, Area, new DrawState()); Drawn second = Draw(shapes, material, Area, new DrawState()); Check(ReferenceEquals(first.Drawables[1], second.Drawables[1])); } public void ADifferentTickColor_BuildsAnotherTick() { var shapes = new VectorShapes(); Drawn first = Draw(shapes, new CheckIndicatorMaterial { IsChecked = true, TickColor = Colors.Red }, Area, new DrawState()); Drawn second = Draw(shapes, new CheckIndicatorMaterial { IsChecked = true, TickColor = Colors.Blue }, Area, new DrawState()); Check(!ReferenceEquals(first.Drawables[1], second.Drawables[1])); } public void ARectCutByTheClipWindow_DrawsNothing() { Drawn drawn = Draw(new CheckIndicatorMaterial { IsChecked = true }, Area, Clipped); CheckEqual(0, drawn.Drawables.Count); } } public class ArrowTests : TestFixture { public void DrawsTheBoxThenTheChevron() { Drawn drawn = Draw(new ArrowIndicatorMaterial { FillColor = Colors.Red, OutlineColor = Colors.Blue, ArrowColor = Colors.Green, }, Area); var box = (ClosedQuadraticBezierCurve)drawn.Drawables[0]; var chevron = (Polyline)drawn.Drawables[1]; CheckEqual(2, drawn.Drawables.Count); CheckEqual(Colors.Red.ToArgb(), box.InsideColor.ToArgb()); CheckEqual(Colors.Blue.ToArgb(), box.ShellColor.ToArgb()); CheckEqual((Scalar)1, box.ShellModelSpaceThickness); CheckEqual(Colors.Green.ToArgb(), chevron.InsideColor.ToArgb()); CheckEqual(AtAreasTopLeft, drawn.Placements[1]); } public void TheChevronsStrokeIsScaledToTheRectsHeight() { Drawn drawn = Draw(new ArrowIndicatorMaterial(), Area); var chevron = (Polyline)drawn.Drawables[1]; CheckEqual((Scalar)(0.16 * 30), chevron.InsidePixelThickness); } public void PointingUpAndPointingDown_BuildDifferentChevrons() { var shapes = new VectorShapes(); Drawn up = Draw(shapes, new ArrowIndicatorMaterial { PointsUp = true }, Area, new DrawState()); Drawn down = Draw(shapes, new ArrowIndicatorMaterial { PointsUp = false }, Area, new DrawState()); Check(!ReferenceEquals(up.Drawables[1], down.Drawables[1])); } public void TheSameChevronTwice_ReusesIt() { var shapes = new VectorShapes(); var material = new ArrowIndicatorMaterial { PointsUp = true }; Drawn first = Draw(shapes, material, Area, new DrawState()); Drawn second = Draw(shapes, material, Area, new DrawState()); Check(ReferenceEquals(first.Drawables[1], second.Drawables[1])); } public void ARectCutByTheClipWindow_DrawsNothing() { Drawn drawn = Draw(new ArrowIndicatorMaterial(), Area, Clipped); CheckEqual(0, drawn.Drawables.Count); } } } }