using System; using Annulus; using Azimuth; using Glass; using UnitTestSharp; namespace Konis.Testbed.UnitTests { public class SpriteCellTests : TestFixture { private static Rect Box(Scalar x, Scalar y, Scalar width, Scalar height) { return Rect.FromPositionAndSize(new Vector(x, y), new Vector(width, height)); } private static SpriteMaterial PictureOf(int width, int height) { return SpriteMaterial.CreateSprite( new Glass.SpriteSheetAttribute(width, height, 0, "test"), new Glass.Point(0, 0)); } private static Label CaptionOf(Scalar width, Scalar height) { return new Label { Bounds = Box(0, 0, width, height) }; } private static SpriteCell CellOf(int pictureWidth, int pictureHeight, Scalar captionWidth) { return new SpriteCell(Glass.Material.Invisible, PictureOf(pictureWidth, pictureHeight), CaptionOf(captionWidth, 10)); } public class SizeForTests : TestFixture { public void ACaptionWiderThanThePicture_MakesTheCellAsWideAsTheCaption() { CheckEqual(new Vector(60, 60), SpriteCell.SizeFor(new Vector(60, 10))); } public void ACaptionNarrowerThanThePicture_MakesTheCellAsWideAsThePicture() { CheckEqual(new Vector(48, 60), SpriteCell.SizeFor(new Vector(30, 10))); } public void AddsAGapAndTheCaptionsHeightBelowThePicture() { CheckEqual(new Vector(48, 52), SpriteCell.SizeFor(new Vector(0, 2))); } } public class CtorTests : TestFixture { public void StartsAtTheSmallestSizeThatFits() { SpriteCell cell = CellOf(32, 32, captionWidth: 60); CheckEqual(new Vector(60, 60), cell.Bounds.Size); } public void PutsTheCaptionAtTheBottom() { SpriteCell cell = CellOf(32, 32, captionWidth: 60); CheckEqual(Box(0, 50, 60, 10), cell.Caption.Bounds); } public void CentersACaptionNarrowerThanTheCell() { SpriteCell cell = CellOf(32, 32, captionWidth: 30); CheckEqual(Box(9, 50, 30, 10), cell.Caption.Bounds); } public void PutsASquareBackdropInTheMiddleOfWhatTheCaptionLeaves() { SpriteCell cell = CellOf(32, 32, captionWidth: 60); CheckEqual(Box(6, 0, 48, 48), cell.BackdropBounds); } public void ASquarePicture_FillsTheBackdrop() { SpriteCell cell = CellOf(32, 32, captionWidth: 60); CheckEqual(Box(6, 0, 48, 48), cell.PictureBounds); } public void AWidePicture_FillsTheBackdropsWidthAndKeepsItsShape() { SpriteCell cell = CellOf(64, 32, captionWidth: 60); CheckEqual(Box(6, 12, 48, 24), cell.PictureBounds); } public void ATallPicture_FillsTheBackdropsHeightAndKeepsItsShape() { SpriteCell cell = CellOf(32, 64, captionWidth: 60); CheckEqual(Box(18, 0, 24, 48), cell.PictureBounds); } public void ASmallPicture_IsScaledUpToFillTheBackdrop() { SpriteCell cell = CellOf(8, 8, captionWidth: 60); CheckEqual(Box(6, 0, 48, 48), cell.PictureBounds); } public void LeavesTheMouseToTheRowItSitsIn() { CheckFalse(CellOf(32, 32, captionWidth: 60).AbsorbsMouse); } public void ANullPicture_Throws() { CheckThrow(typeof(ArgumentNullException)); new SpriteCell(Glass.Material.Invisible, null, CaptionOf(60, 10)); } public void ANullCaption_Throws() { CheckThrow(typeof(ArgumentNullException)); new SpriteCell(Glass.Material.Invisible, PictureOf(32, 32), null); } } public class SizeChangedTests : TestFixture { public void ALargerCell_KeepsTheCaptionAtTheBottomAndCentersIt() { SpriteCell cell = CellOf(32, 32, captionWidth: 60); cell.Bounds = Box(0, 0, 100, 80); CheckEqual(Box(20, 70, 60, 10), cell.Caption.Bounds); } public void ALargerCell_CentersThePictureInWhatTheCaptionLeaves() { SpriteCell cell = CellOf(32, 32, captionWidth: 60); cell.Bounds = Box(0, 0, 100, 80); CheckEqual(Box(26, 10, 48, 48), cell.BackdropBounds); CheckEqual(Box(26, 10, 48, 48), cell.PictureBounds); } public void ALargerCell_CentersAWidePictureInItsBackdrop() { SpriteCell cell = CellOf(64, 32, captionWidth: 60); cell.Bounds = Box(0, 0, 100, 80); CheckEqual(Box(26, 22, 48, 24), cell.PictureBounds); } public void ACellNarrowerThanTheCaption_SqueezesTheCaptionToTheCell() { SpriteCell cell = CellOf(32, 32, captionWidth: 60); cell.Bounds = Box(0, 0, 40, 80); CheckEqual(Box(0, 70, 40, 10), cell.Caption.Bounds); } public void ACellShorterThanTheBackdrop_SqueezesTheBackdropToWhatTheCaptionLeaves() { SpriteCell cell = CellOf(32, 32, captionWidth: 60); cell.Bounds = Box(0, 0, 100, 40); CheckEqual(Box(26, 0, 48, 28), cell.BackdropBounds); } } } }