using System.Collections.Generic; using System.Reflection; using Annulus; using Blacklight.Core; using UnitTestSharp; using DrawingPoint = System.Drawing.Point; using DrawingSize = System.Drawing.Size; namespace Glass.Blacklight.UnitTests { // Draws from CursorSheet.png, which is 8 by 4, red in its left four columns and blue in its right four. public class CursorSyncTests : TestFixture { private const string Sheet = "Glass.Blacklight.UnitTests.CursorSheet.png"; [SpriteSheet(spriteSizeX: 4, spriteSizeY: 4, borderSize: 0, Sheet)] public sealed class FlatSheet { } public sealed class RecordingSurface : ICursorSurface { public List Set { get; } = new List(); public int Cleared { get; private set; } public bool CursorVisible { get; set; } public bool TrySetCursor(CursorImage cursor) { Set.Add(cursor); return true; } public void ClearCursor() { ++Cleared; } } public sealed class UnknownCursor : Cursor { } private static readonly Assembly Art = typeof(CursorSyncTests).Assembly; private static readonly SpriteCursor Red = new SpriteCursor(MaterialLibrary.CreateSprite(new Point(0, 0)), new Point(1, 2)); private static readonly SpriteCursor Blue = new SpriteCursor(MaterialLibrary.CreateSprite(new Point(1, 0)), new Point(1, 2)); public class ApplyTests : TestFixture { public void ASpriteCursor_SetsItsImageAtItsHotspot() { var surface = new RecordingSurface(); new CursorSync(surface, scale: 1, Art).Apply(Red); CheckEqual(1, surface.Set.Count); CheckEqual(new DrawingSize(4, 4), surface.Set[0].Size); CheckEqual(new DrawingPoint(1, 2), surface.Set[0].Hotspot); } public void AScale_EnlargesTheImageAndHotspot() { var surface = new RecordingSurface(); new CursorSync(surface, scale: 2, Art).Apply(Red); CheckEqual(new DrawingSize(8, 8), surface.Set[0].Size); CheckEqual(new DrawingPoint(2, 4), surface.Set[0].Hotspot); } public void TheSameCursorAgain_SetsNothingMore() { var surface = new RecordingSurface(); var sync = new CursorSync(surface, scale: 1, Art); sync.Apply(Red); sync.Apply(Red); CheckEqual(1, surface.Set.Count); CheckEqual(0, surface.Cleared); } public void ADifferentCursor_SetsItsImage() { var surface = new RecordingSurface(); var sync = new CursorSync(surface, scale: 1, Art); sync.Apply(Red); sync.Apply(Blue); CheckEqual(2, surface.Set.Count); CheckEqual(255, surface.Set[1].Rgba[2]); } public void NoCursorAtFirst_LeavesTheSurfaceAlone() { var surface = new RecordingSurface(); new CursorSync(surface, scale: 1, Art).Apply(null); CheckEqual(0, surface.Set.Count); CheckEqual(0, surface.Cleared); } public void NoCursorAfterASpriteCursor_HandsTheCursorBackOnce() { var surface = new RecordingSurface(); var sync = new CursorSync(surface, scale: 1, Art); sync.Apply(Red); sync.Apply(null); sync.Apply(null); CheckEqual(1, surface.Cleared); } public void ACursorItCannotDraw_CountsAsNone() { var surface = new RecordingSurface(); var sync = new CursorSync(surface, scale: 1, Art); sync.Apply(Red); sync.Apply(new UnknownCursor()); CheckEqual(1, surface.Cleared); } } } }