using Annulus; using Azimuth; using Glass.UnitTests.Materials; using System.Collections.Generic; using System.Linq; using UnitTestSharp; namespace Glass.UnitTests { public class GroupBoxTests : TestFixture { // "ab" in this style is 26 wide and 14 tall, with its baseline 10 below its top. public static TextMaterialStyle CaptionStyle() { return new TextMaterialStyle { Font = BakedTextMaterialTests.SharedFont, HorizontalMargin = 3, VerticalMargin = 2, }; } public static GroupBox Captioned(GroupBoxMaterial material = null) { return new GroupBox { Bounds = Rect.FromPositionAndSize(Vector.Zero, new Vector(200, 100)), Material = material ?? GroupBoxMaterialTests.Captioned(), Text = "ab", }; } public static List Children(ControlContainer container) { var returnMe = new List(); foreach (Control child in container.BackToFront()) { returnMe.Add(child); } return returnMe; } public static GroupBox.GapState Gap(GroupBox box) { return (GroupBox.GapState)box.MaterialState; } public static Control Content(GroupBox box) { return Children(box.DirectChildren)[0]; } public static Label Caption(GroupBox box) { return (Label)Children(box.DirectChildren).Last(); } public class CtorTests : TestFixture { public void HoldsTheContentBeneathTheCaption() { List children = Children(new GroupBox().DirectChildren); CheckEqual(2, children.Count); CheckTrue(children[0] is Panel); CheckTrue(children[1] is Label); } public void DrawsNothingOfItsOwnForTheContent() { CheckTrue(ReferenceEquals(Material.Invisible, ((Panel)Content(new GroupBox())).Material)); } public void LetsTheCaptionDrawOutsideTheCanvas() { CheckFalse(new GroupBox().ClipsCanvas); } public void HasNoMaterialYet() { CheckNull(new GroupBox().Material); CheckNull(((Control)new GroupBox()).Material); } public void HasNoStateYet() { CheckNull(new GroupBox().MaterialState); } } public class CanvasChildrenTests : TestFixture { public void AddedChildren_GoInTheContentNotBesideTheCaption() { GroupBox box = Captioned(); var child = new Panel(); box.CanvasChildren.Add(child); CheckEqual(2, box.DirectChildren.Count); CheckEqual(1, Content(box).CanvasChildren.Count); CheckTrue(ReferenceEquals(child, Children(Content(box).CanvasChildren).Single())); } } public class MaterialTests : TestFixture { public void ReadsBackTheMaterialSet() { GroupBoxMaterial material = GroupBoxMaterialTests.Captioned(); var box = new GroupBox { Material = material }; CheckTrue(ReferenceEquals(material, box.Material)); CheckTrue(ReferenceEquals(material, ((Control)box).Material)); } public void SetBeforeTheText_StillMakesRoomForIt() { var box = new GroupBox { Bounds = Rect.FromPositionAndSize(Vector.Zero, new Vector(200, 100)), Text = "ab", }; box.Material = GroupBoxMaterialTests.Captioned(); CheckEqual(new Vector(194, 84), Content(box).Bounds.Size); CheckEqual(new Vector(26, 14), Caption(box).Bounds.Size); } public void CaptionDrawsInTheMaterialsStyleOnOneLine() { GroupBox box = Captioned(); CheckEqual(CaptionStyle() with { Multiline = false }, Caption(box).Style); CheckFalse(Caption(box).Style.Multiline); } public void StyleChange_ResizesTheCaption() { GroupBox box = Captioned(); box.Material = GroupBoxMaterialTests.Captioned() with { CaptionStyle = CaptionStyle() with { HorizontalMargin = 5 }, }; CheckEqual(30, Caption(box).Bounds.Width); } public void Null_DrawsNothingAndLeavesTheContentTheWholeBox() { GroupBox box = Captioned(); box.Material = null; CheckNull(((Control)box).Material); CheckEqual(box.Bounds.Size, Content(box).Bounds.Size); CheckNull(Caption(box).Style.Font); CheckNull(box.MaterialState); } } public class TextTests : TestFixture { public void SizesTheCaptionToItsText() { GroupBox box = Captioned(); box.Text = "abab"; CheckEqual(46, Caption(box).Bounds.Width); } public void ReadsBackTheTextSet() { CheckEqual("ab", Captioned().Text.ToString()); } public void Empty_KeepsTheCaptionStripButShowsNoGap() { GroupBox box = Captioned(); box.Text = ""; CheckEqual(new Vector(194, 84), Content(box).Bounds.Size); CheckEqual(Vector.Zero, Caption(box).Bounds.Size); CheckEqual(0, Gap(box).Width); } } public class GapTests : TestFixture { public void Material_IsGivenTheBoxsGapState() { GroupBox box = Captioned(); CheckNotNull(box.MaterialState); CheckTrue(ReferenceEquals(Gap(box), box.MaterialState)); } public void KeepsTheSameStateAcrossMaterials() { GroupBox box = Captioned(); object first = box.MaterialState; box.Material = GroupBoxMaterialTests.Captioned() with { FrameInset = 6 }; CheckTrue(ReferenceEquals(first, box.MaterialState)); } public void Width_IsTheCaptionsWidth() { CheckEqual(26, Gap(Captioned()).Width); } public void Width_FollowsTheText() { GroupBox box = Captioned(); box.Text = "abab"; CheckEqual(46, Gap(box).Width); } public void Width_IsZeroWithNoText() { GroupBox box = Captioned(); box.Text = ""; CheckEqual(0, Gap(box).Width); } public void CaptionAbove_HasNoGap() { GroupBox box = Captioned( GroupBoxMaterialTests.Captioned() with { CaptionPlacement = CaptionPlacement.Above }); CheckEqual(0, Gap(box).Width); } } public class CornerTests : TestFixture { private static GroupBox WithCorners() { return Captioned(GroupBoxMaterialTests.Captioned(GroupBoxMaterialTests.Cornered())); } public void Caption_StartsRightOfTheLeftCorner() { CheckEqual(8, Caption(WithCorners()).Bounds.Left + WithCorners().CanvasOrigin.X); } public void BoundsSizeFor_LeavesRoomForTheRightCorner() { CheckEqual(42, WithCorners().BoundsSizeFor(new Vector(5, 5)).X); } } public class FrameInsetTests : TestFixture { public void MovesTheCaptionInWithTheFrame() { GroupBox box = Captioned(GroupBoxMaterialTests.Captioned() with { FrameInset = 6 }); CheckEqual(10, Caption(box).Bounds.Left + box.CanvasOrigin.X); } public void BoundsSizeFor_LeavesRoomOnBothSides() { GroupBox box = Captioned(GroupBoxMaterialTests.Captioned() with { FrameInset = 6 }); CheckEqual(new Vector(68, 62), box.BoundsSizeFor(new Vector(50, 40))); } } public class CaptionOffsetTests : TestFixture { public void MovesTheCaption() { GroupBox box = Captioned(GroupBoxMaterialTests.Captioned() with { CaptionOffset = 20 }); CheckEqual(20, Caption(box).Bounds.Left + box.CanvasOrigin.X); } } public class ScaleTests : TestFixture { public void MaterialScale_SizesTheCaptionAndItsGapScaled() { GroupBox box = Captioned(GroupBoxMaterialTests.Captioned() with { Scale = 2 }); CheckEqual(new Vector(52, 28), Caption(box).Bounds.Size); CheckEqual(52, Gap(box).Width); } public void MaterialScale_CompoundsWithTheCaptionStylesOwn() { GroupBox box = Captioned(GroupBoxMaterialTests.Captioned() with { CaptionStyle = CaptionStyle() with { Scale = 3 }, Scale = 2, }); CheckEqual(new Vector(156, 84), Caption(box).Bounds.Size); } public void MaterialScale_StartsTheCaptionAtTheScaledCornerAndOffset() { GroupBox box = Captioned(GroupBoxMaterialTests.Captioned(GroupBoxMaterialTests.Cornered()) with { Scale = 2, }); CheckEqual(16, Caption(box).Bounds.Left + box.CanvasOrigin.X); } public void MaterialScaleChanged_ResizesTheCaption() { GroupBox box = Captioned(); box.Material = GroupBoxMaterialTests.Captioned() with { Scale = 2 }; CheckEqual(new Vector(52, 28), Caption(box).Bounds.Size); } } public class BoundsTests : TestFixture { public void ContentFillsTheCanvasBelowTheCaption() { GroupBox box = Captioned(); CheckEqual(new Vector(194, 84), Content(box).Bounds.Size); CheckEqual(Vector.Zero, Content(box).Bounds.Position); } public void CaptionSitsAboveTheCanvas() { CheckEqual( Rect.FromPositionAndSize(new Vector(1, -14), new Vector(26, 14)), Caption(Captioned()).Bounds); } public void CaptionSitsAboveTheCanvas_WhereverTheBoxIs() { GroupBox box = Captioned(); box.Bounds = Rect.FromPositionAndSize(new Vector(30, 40), new Vector(200, 100)); CheckEqual(Rect.FromPositionAndSize(new Vector(1, -14), new Vector(26, 14)), Caption(box).Bounds); CheckEqual(Vector.Zero, Content(box).Bounds.Position); } public void Resizing_LaysTheContentOutAgain() { GroupBox box = Captioned(); box.Bounds = Rect.FromPositionAndSize(Vector.Zero, new Vector(120, 60)); CheckEqual(new Vector(114, 44), Content(box).Bounds.Size); } } public class BoundsSizeForTests : TestFixture { public void RoomyContent_AddsTheFrameAndTheCaptionStrip() { CheckEqual(new Vector(56, 56), Captioned().BoundsSizeFor(new Vector(50, 40))); } public void NarrowContent_IsWidenedToHoldTheCaption() { CheckEqual(new Vector(34, 21), Captioned().BoundsSizeFor(new Vector(5, 5))); } public void NoMaterial_IsTheContentSize() { CheckEqual(new Vector(50, 40), new GroupBox().BoundsSizeFor(new Vector(50, 40))); } } } }