using System; using Annulus; using Azimuth; using UnitTestSharp; using static Glass.UnitTests.TextBoxTests; namespace Glass.UnitTests { public class TextBoxCompositionTargetTests : TestFixture { public static readonly TextBoxMaterial TargetMaterial = CompositionMaterial with { CompositionTarget = new SolidColorMaterial(), CompositionTargetThickness = 4, }; public static bool ShowsTarget(TextBox box) => BandCount(box.Content.CompositionTarget) > 0; public static TextBox BoxComposing(int caret, int targetStart, int targetEnd) { TextBox box = NewBox("ad", material: TargetMaterial); box.Buffer.MoveTo(1, extend: false); box.CompositionUpdate("bcde", caret, targetStart, targetEnd); return box; } public class CompositionUpdateTests : TestFixture { public void ShowsTheClauseBeingConverted() { TextBox box = BoxComposing(caret: 4, targetStart: 1, targetEnd: 3); Check(ShowsTarget(box)); CheckEqual(1, box.CompositionTargetStart); CheckEqual(3, box.CompositionTargetEnd); } public void DrawsItAsAThickerLineUnderJustThatClause() { TextBox box = BoxComposing(caret: 4, targetStart: 1, targetEnd: 3); CheckEqual(Rect.FromPositionAndSize(new Vector(20, 6), new Vector(20, 4)), box.Content.CompositionTarget[0]); } public void TheWholeCompositionKeepsItsOwnThinnerLine() { TextBox box = BoxComposing(caret: 4, targetStart: 1, targetEnd: 3); CheckEqual(Rect.FromPositionAndSize(new Vector(10, 8), new Vector(40, 2)), box.Content.Composition[0]); } public void ATargetAtTheStart_StartsWhereTheCompositionDoes() { TextBox box = BoxComposing(caret: 2, targetStart: 0, targetEnd: 2); CheckEqual(Rect.FromPositionAndSize(new Vector(10, 6), new Vector(20, 4)), box.Content.CompositionTarget[0]); } public void ATargetAtTheEnd_EndsWhereTheCompositionDoes() { TextBox box = BoxComposing(caret: 4, targetStart: 2, targetEnd: 4); CheckEqual(Rect.FromPositionAndSize(new Vector(30, 6), new Vector(20, 4)), box.Content.CompositionTarget[0]); } public void NoTarget_HidesIt() { TextBox box = BoxComposing(caret: 4, targetStart: 0, targetEnd: 0); CheckFalse(ShowsTarget(box)); } public void AnEmptyTargetInTheMiddle_HidesIt() { TextBox box = BoxComposing(caret: 4, targetStart: 2, targetEnd: 2); CheckFalse(ShowsTarget(box)); } public void TheTwoArgumentFormOnControl_MarksNoClause() { TextBox box = NewBox("ad", material: TargetMaterial); box.Buffer.MoveTo(1, extend: false); box.CompositionUpdate("bc", 2); CheckEqual(0, box.CompositionTargetStart); CheckEqual(0, box.CompositionTargetEnd); CheckFalse(ShowsTarget(box)); } public void ANewUpdateWithoutOne_TakesTheClauseDown() { TextBox box = BoxComposing(caret: 4, targetStart: 1, targetEnd: 3); box.CompositionUpdate("bcde", 4, 0, 0); CheckFalse(ShowsTarget(box)); CheckEqual(0, box.CompositionTargetStart); CheckEqual(0, box.CompositionTargetEnd); } public void ANewClause_MovesTheLine() { TextBox box = BoxComposing(caret: 4, targetStart: 1, targetEnd: 3); box.CompositionUpdate("bcde", 4, 2, 4); CheckEqual(Rect.FromPositionAndSize(new Vector(30, 6), new Vector(20, 4)), box.Content.CompositionTarget[0]); } public void ANegativeStart_Throws() { TextBox box = NewBox(); CheckThrow(typeof(ArgumentOutOfRangeException)); box.CompositionUpdate("ab", 1, -1, 1); } public void AnEndBeforeTheStart_Throws() { TextBox box = NewBox(); CheckThrow(typeof(ArgumentOutOfRangeException)); box.CompositionUpdate("abc", 1, 2, 1); } public void AnEndPastTheText_Throws() { TextBox box = NewBox(); CheckThrow(typeof(ArgumentOutOfRangeException)); box.CompositionUpdate("ab", 1, 0, 3); } public void AClauseCoveringTheWholeText_IsAllowed() { TextBox box = NewBox(); box.CompositionUpdate("ab", 1, 0, 2); CheckEqual(2, box.CompositionTargetEnd); } public void WhileDisabled_IsIgnored() { TextBox box = NewBox("ad", material: TargetMaterial); box.Enabled = false; box.CompositionUpdate("bc", 2, 0, 2); CheckEqual(0, box.CompositionTargetEnd); CheckFalse(ShowsTarget(box)); } } public class CompositionEndTests : TestFixture { public void TakesTheClauseDown() { TextBox box = BoxComposing(caret: 4, targetStart: 1, targetEnd: 3); box.CompositionEnd(); CheckFalse(ShowsTarget(box)); CheckEqual(0, box.CompositionTargetStart); CheckEqual(0, box.CompositionTargetEnd); } } public class TextInputTests : TestFixture { public void ACommit_TakesTheClauseDown() { TextBox box = BoxComposing(caret: 4, targetStart: 1, targetEnd: 3); box.TextInput("x", new KeyboardManager()); CheckFalse(ShowsTarget(box)); CheckEqual(0, box.CompositionTargetEnd); } } public class DroppingTheCompositionTests : TestFixture { public void BeingDisabled_TakesTheClauseDown() { TextBox box = BoxComposing(caret: 4, targetStart: 1, targetEnd: 3); box.Enabled = false; CheckFalse(ShowsTarget(box)); CheckEqual(0, box.CompositionTargetEnd); } public void LosingFocus_TakesTheClauseDown() { TextBox box = BoxComposing(caret: 4, targetStart: 1, targetEnd: 3); var keys = Focus(box); keys.ClearFocus(); CheckFalse(ShowsTarget(box)); } } public class TextBoxMaterialTests : TestFixture { public void RoundTripsTheClauseLook() { var box = NewBox(); var look = new SolidColorMaterial(); box.TextBoxMaterial = new TextBoxMaterial { Scale = default, Background = default, Text = default, Selection = default, InactiveSelection = default, Caret = default, CaretWidth = default, Composition = default, CompositionThickness = default, ScrollBar = default, HorizontalScrollBar = default, CompositionTarget = look, CompositionTargetThickness = 5, }; TextBoxMaterial back = box.TextBoxMaterial; Check(ReferenceEquals(look, back.CompositionTarget)); CheckEqual((Scalar)5, back.CompositionTargetThickness); } } } }