using Amphora; using Azimuth; using Blacklight.Core.Drawables; using Glass.Text; using UnitTestSharp; using Prism; namespace Glass.Blacklight.UnitTests { public class SpanLooksTests : TestFixture { public static BaseTint Base() { return BaseTintTests.Tint(); } public static DynamicArray Spans(params TextStyle[] styles) { var returnMe = new DynamicArray(capacity: styles.Length); for (int i = 0; i < styles.Length; ++i) { returnMe.Add(new StyleSpan(i, i + 1, styles[i])); } return returnMe; } public static SpanLooks ResolvedFor(params TextStyle[] styles) { var returnMe = new SpanLooks(); BaseTint tint = Base(); returnMe.Resolve(Spans(styles), in tint, colorOverridden: false); return returnMe; } public class ResolveTests : TestFixture { public void NullSpans_LeaveJustTheBaseGroup() { var looks = new SpanLooks(); BaseTint tint = Base(); looks.Resolve(null, in tint, colorOverridden: false); CheckEqual(1, looks.GroupCount); CheckEqual(SpriteBatch.Varying.None, looks.Varying); CheckTrue(tint.Look.Matches(looks.LookOf(SpanLooks.BaseGroup))); } public void NoSpans_LeaveJustTheBaseGroup() { SpanLooks looks = ResolvedFor(); CheckEqual(1, looks.GroupCount); CheckEqual(SpriteBatch.Varying.None, looks.Varying); } public void SpanColorDifferingFromTheBase_VariesTheColor() { SpanLooks looks = ResolvedFor(new TextStyle { TintColor = Colors.Red }); CheckEqual(SpriteBatch.Varying.TintColor, looks.Varying); } public void SpanColorTheSameAsTheBase_VariesNothing() { SpanLooks looks = ResolvedFor(new TextStyle { TintColor = Colors.White }); CheckEqual(SpriteBatch.Varying.None, looks.Varying); } public void SpanColorTheSameAsTheBaseByAnotherName_VariesNothing() { var looks = new SpanLooks(); BaseTint tint = Base(); tint.Color = Colors.Red; looks.Resolve(Spans(new TextStyle { TintColor = Color.FromArgb(255, 255, 0, 0) }), in tint, colorOverridden: false); CheckEqual(SpriteBatch.Varying.None, looks.Varying); } public void ColorOverridden_IgnoresSpanColors() { var looks = new SpanLooks(); BaseTint tint = Base(); looks.Resolve(Spans(new TextStyle { TintColor = Colors.Red }), in tint, colorOverridden: true); CheckEqual(SpriteBatch.Varying.None, looks.Varying); CheckEqual(Colors.White.ToArgb(), looks.ColorOf(0).ToArgb()); } public void SpanGammaDifferingFromTheBase_VariesTheGamma() { SpanLooks looks = ResolvedFor(new TextStyle { CoverageGamma = 2 }); CheckEqual(SpriteBatch.Varying.CoverageGamma, looks.Varying); } public void SpanGammaTheSameAsTheBase_VariesNothing() { SpanLooks looks = ResolvedFor(new TextStyle { CoverageGamma = 1.4 }); CheckEqual(SpriteBatch.Varying.None, looks.Varying); } public void ColorAndGammaSpans_VaryBoth() { SpanLooks looks = ResolvedFor( new TextStyle { TintColor = Colors.Red }, new TextStyle { CoverageGamma = 2 }); CheckEqual(SpriteBatch.Varying.TintColor | SpriteBatch.Varying.CoverageGamma, looks.Varying); } public void SpanTexture_MakesAGroupOfItsOwn() { var texture = new BlacklightGlassTexture(); SpanLooks looks = ResolvedFor(new TextStyle { TintTexture = texture }); CheckEqual(2, looks.GroupCount); CheckEqual(1, looks.GroupOf(0)); CheckTrue(ReferenceEquals(texture, looks.LookOf(1).Texture)); CheckEqual(SpriteBatch.Varying.None, looks.Varying); } public void SpansSharingATexture_ShareAGroup() { var texture = new BlacklightGlassTexture(); SpanLooks looks = ResolvedFor( new TextStyle { TintTexture = texture }, new TextStyle { TintTexture = texture }); CheckEqual(2, looks.GroupCount); CheckEqual(1, looks.GroupOf(0)); CheckEqual(1, looks.GroupOf(1)); } public void SpansWithDifferentTextures_GetGroupsInOrderOfFirstAppearance() { var first = new BlacklightGlassTexture(); var second = new BlacklightGlassTexture(); SpanLooks looks = ResolvedFor( new TextStyle { TintTexture = first }, new TextStyle { TintTexture = second }, new TextStyle { TintTexture = first }); CheckEqual(3, looks.GroupCount); CheckEqual(1, looks.GroupOf(0)); CheckEqual(2, looks.GroupOf(1)); CheckEqual(1, looks.GroupOf(2)); } public void SpanTextureTheSameAsTheBases_JoinsTheBaseGroup() { var texture = new BlacklightGlassTexture(); var looks = new SpanLooks(); BaseTint tint = Base(); tint.Look.Texture = texture; looks.Resolve(Spans(new TextStyle { TintTexture = texture }), in tint, colorOverridden: false); CheckEqual(1, looks.GroupCount); CheckEqual(SpanLooks.BaseGroup, looks.GroupOf(0)); } public void SpanTextureWithATransformAndSpace_TakesThem() { AffineMatrix moved = TintLookTests.Moved(); var style = new TextStyle { TintTexture = new BlacklightGlassTexture(), TintTransform = moved, TintSpace = TintSpace.Model, }; TintLook look = ResolvedFor(style).LookOf(1); CheckTrue(moved == look.Transform); CheckEqual(TintSpace.Model, look.Space); } public void SpanTextureWithoutATransformOrSpace_TakesTheBases() { var looks = new SpanLooks(); BaseTint tint = Base(); tint.Look.Transform = TintLookTests.Moved(); tint.Look.Space = TintSpace.Model; looks.Resolve(Spans(new TextStyle { TintTexture = new BlacklightGlassTexture() }), in tint, colorOverridden: false); CheckTrue(tint.Look.Transform == looks.LookOf(1).Transform); CheckEqual(TintSpace.Model, looks.LookOf(1).Space); } public void SpanTransformAndSpaceWithNoTextureAnywhere_DoNotMakeAGroup() { var style = new TextStyle { TintTransform = TintLookTests.Moved(), TintSpace = TintSpace.Model }; SpanLooks looks = ResolvedFor(style); CheckEqual(1, looks.GroupCount); CheckEqual(SpanLooks.BaseGroup, looks.GroupOf(0)); } public void SpanTransformOverTheBasesTexture_MakesAGroupOfItsOwn() { var texture = new BlacklightGlassTexture(); var looks = new SpanLooks(); BaseTint tint = Base(); tint.Look.Texture = texture; looks.Resolve(Spans(new TextStyle { TintTransform = TintLookTests.Moved() }), in tint, colorOverridden: false); CheckEqual(2, looks.GroupCount); CheckEqual(1, looks.GroupOf(0)); CheckTrue(ReferenceEquals(texture, looks.LookOf(1).Texture)); } public void ReplacesWhateverWasResolvedBefore() { SpanLooks looks = ResolvedFor( new TextStyle { TintTexture = new BlacklightGlassTexture(), TintColor = Colors.Red }); BaseTint tint = Base(); looks.Resolve(Spans(new TextStyle()), in tint, colorOverridden: false); CheckEqual(1, looks.GroupCount); CheckEqual(SpriteBatch.Varying.None, looks.Varying); CheckEqual(SpanLooks.BaseGroup, looks.GroupOf(0)); } public void MoreSpansThanBefore_GrowsTheStorage() { var styles = new TextStyle[40]; for (int i = 0; i < styles.Length; ++i) { styles[i] = new TextStyle { CoverageGamma = 2 + i }; } SpanLooks looks = ResolvedFor(styles); CheckEqual((Scalar)41, looks.GammaOf(39)); } public static readonly SpanLooks AllocationLooks = new SpanLooks(); public static readonly BaseTint AllocationTint = Base(); public static readonly DynamicArray AllocationSpans = Spans( new TextStyle { TintColor = Colors.Red, CoverageGamma = 2 }, new TextStyle { TintTexture = new BlacklightGlassTexture() }); [AllocationTest] public void AllocatesNothingOnceWarm() { AllocationLooks.Resolve(AllocationSpans, in AllocationTint, colorOverridden: false); CheckNoAllocations(); } } public class GroupOfTests : TestFixture { public void NoSpan_IsTheBaseGroup() { CheckEqual(SpanLooks.BaseGroup, ResolvedFor(new TextStyle()).GroupOf(-1)); } public void ASpan_IsItsOwnGroup() { SpanLooks looks = ResolvedFor( new TextStyle(), new TextStyle { TintTexture = new BlacklightGlassTexture() }); CheckEqual(SpanLooks.BaseGroup, looks.GroupOf(0)); CheckEqual(1, looks.GroupOf(1)); } } public class ColorOfTests : TestFixture { public void NoSpan_IsTheBaseColor() { CheckEqual(Colors.White.ToArgb(), ResolvedFor(new TextStyle { TintColor = Colors.Red }).ColorOf(-1).ToArgb()); } public void ASpanWithAColor_IsThatColor() { CheckEqual(Colors.Red.ToArgb(), ResolvedFor(new TextStyle { TintColor = Colors.Red }).ColorOf(0).ToArgb()); } public void ASpanWithoutAColor_IsTheBaseColor() { CheckEqual(Colors.White.ToArgb(), ResolvedFor(new TextStyle()).ColorOf(0).ToArgb()); } } public class GammaOfTests : TestFixture { public void NoSpan_IsTheBaseGamma() { CheckEqual(Base().Gamma, ResolvedFor(new TextStyle { CoverageGamma = 2 }).GammaOf(-1)); } public void ASpanWithAGamma_IsThatGamma() { CheckEqual((Scalar)2, ResolvedFor(new TextStyle { CoverageGamma = 2 }).GammaOf(0)); } public void ASpanWithoutAGamma_IsTheBaseGamma() { CheckEqual(Base().Gamma, ResolvedFor(new TextStyle()).GammaOf(0)); } } } }