using System; using Annulus; using Azimuth; using Glass; using UnitTestSharp; namespace Clave.Glass.UnitTests { public class KeyBindingRowTests : TestFixture { /// /// The width of a cell in the test looks: the default sizing binding in label order, then a gap and the remove /// button. /// public static readonly int Cell = TestLooks.WidthOf("Shift+I+O+P+U") + TestLooks.Spacing + TestLooks.GlyphHeight; public static KeyBindingRow Row(KeyBindingSlotTests.Scene scene, KeyAction action) { return new KeyBindingRow(scene.Editor, scene.Bindings, action, TestLooks.Material); } // Ctrl+Alt+Shift+Page Down, too wide for one cell but not for two. public static Chord Wide() { return Chord.OnPress(KeyCode.PageDown) .WithAllHeld(new KeyState(KeyCode.ControlLeft, KeyCode.AltLeft, KeyCode.ShiftLeft)); } public class CtorTests : TestFixture { public void HasASlotPerChordAndOneToAddWith() { var scene = new KeyBindingSlotTests.Scene(); KeyBindingRow row = Row(scene, scene.Shoot); CheckEqual(3, row.Slots.Count); CheckEqual(2, row.Slots[2].Index); CheckNull(row.Slots[2].Chord); CheckEqual(3, row.Notes.Count); CheckEqual("Shoot", row.NameLabel.Text.ToString()); CheckEqual(scene.Bindings, row.Source); CheckEqual(scene.Shoot, row.Action); } public void NoClash_HidesEveryNote() { var scene = new KeyBindingSlotTests.Scene(); KeyBindingRow row = Row(scene, scene.Jump); CheckFalse(row.Notes[0].Visible); CheckFalse(row.Notes[1].Visible); CheckFalse(row.Slots[0].Clashing); } public void NullEditor_Throws() { var scene = new KeyBindingSlotTests.Scene(); CheckThrow(typeof(ArgumentNullException)); new KeyBindingRow(null, scene.Bindings, scene.Jump, TestLooks.Material); } public void NullSource_Throws() { var scene = new KeyBindingSlotTests.Scene(); CheckThrow(typeof(ArgumentNullException)); new KeyBindingRow(scene.Editor, null, scene.Jump, TestLooks.Material); } public void NullAction_Throws() { var scene = new KeyBindingSlotTests.Scene(); CheckThrow(typeof(ArgumentNullException)); new KeyBindingRow(scene.Editor, scene.Bindings, null, TestLooks.Material); } } public class AddCellTests : TestFixture { public void CapturedKeys_AreAddedAndANewAddCellFollows() { var scene = new KeyBindingSlotTests.Scene(); KeyBindingRow row = Row(scene, scene.Jump); KeyBindingSlot add = row.Slots[1]; scene.Activate(add); scene.Tap(KeyCode.K); row.Refresh(); CheckEqual(2, scene.Bindings.ChordsOf(scene.Jump).Count); CheckEqual(add, row.Slots[1]); CheckEqual("K", KeyBindingSlotTests.Shown(row.Slots[1])); CheckEqual(3, row.Slots.Count); CheckEqual(3, row.Notes.Count); } public void CancelledCapture_LeavesTheAddCell() { var scene = new KeyBindingSlotTests.Scene(); KeyBindingRow row = Row(scene, scene.Jump); scene.Activate(row.Slots[1]); row.Slots[1].CancelCapture(); CheckEqual(2, row.Slots.Count); CheckEqual(KeyBindingsText.English.AddChord, KeyBindingSlotTests.Shown(row.Slots[1])); } } public class RefreshTests : TestFixture { public void SameChordAsAnotherAction_IsNotedForThatChord() { var scene = new KeyBindingSlotTests.Scene(); KeyBindingRow row = Row(scene, scene.Shoot); scene.Editor.SetChord(scene.Bindings, scene.Shoot, 1, Chord.OnPress(KeyCode.Space)); row.Refresh(); CheckFalse(row.Notes[0].Visible); CheckTrue(row.Notes[1].Visible); CheckEqual("Space: Also Jump", row.Notes[1].Text.ToString()); CheckTrue(row.Slots[1].Clashing); CheckEqual(TestLooks.Clashing, row.Slots[1].ButtonMaterial); CheckFalse(row.Slots[0].Clashing); } public void ChordTheInterfaceTakes_IsNoted() { var scene = new KeyBindingSlotTests.Scene(); KeyBindingRow row = Row(scene, scene.Jump); scene.Editor.SetChord(scene.Bindings, scene.Jump, 0, Chord.OnPress(KeyCode.Tab)); row.Refresh(); string expected = $"Tab: Taken by {KeyboardManager.NextControlName}. " + $"Taken by {KeyboardManager.PreviousControlName}"; CheckEqual(expected, row.Notes[0].Text.ToString()); } public void AClashThatEnds_TakesItsNoteAway() { var scene = new KeyBindingSlotTests.Scene(); KeyBindingRow row = Row(scene, scene.Shoot); scene.Editor.SetChord(scene.Bindings, scene.Shoot, 1, Chord.OnPress(KeyCode.Space)); row.Refresh(); scene.Editor.Undo(); row.Refresh(); CheckFalse(row.Notes[1].Visible); CheckFalse(row.Slots[1].Clashing); } public void ChordsAddedElsewhere_GetCells() { var scene = new KeyBindingSlotTests.Scene(); KeyBindingRow row = Row(scene, scene.Jump); scene.Editor.SetChord(scene.Bindings, scene.Jump, 1, Chord.OnPress(KeyCode.K)); row.Refresh(); CheckEqual(3, row.Slots.Count); CheckEqual("K", KeyBindingSlotTests.Shown(row.Slots[1])); } public void ChordsTakenOutElsewhere_LoseTheirCells() { var scene = new KeyBindingSlotTests.Scene(); KeyBindingRow row = Row(scene, scene.Shoot); scene.Editor.RemoveChord(scene.Bindings, scene.Shoot, 0); row.Refresh(); CheckEqual(2, row.Slots.Count); CheckEqual(2, row.Notes.Count); CheckEqual("S", KeyBindingSlotTests.Shown(row.Slots[0])); } } public class LineHeightTests : TestFixture { public void IsABindingsHeight() { var scene = new KeyBindingSlotTests.Scene(); CheckEqual(TestLooks.GlyphHeight, Row(scene, scene.Jump).LineHeight); } public void TallerKeyText_RaisesIt() { var scene = new KeyBindingSlotTests.Scene(); KeyBindingsMaterial look = TestLooks.Material; look.KeyTextStyle = TestLooks.TextOf(3, 8); var row = new KeyBindingRow(scene.Editor, scene.Bindings, scene.Jump, look); CheckEqual(8, row.LineHeight); } } public class CellWidthTests : TestFixture { public void FitsTheSizingBindingAndItsRemoveButton() { var scene = new KeyBindingSlotTests.Scene(); CheckEqual(Cell, Row(scene, scene.Jump).CellWidth); } public void ASmallerSizingBinding_LeavesTheCapturePromptWidest() { var scene = new KeyBindingSlotTests.Scene(); KeyBindingsMaterial look = TestLooks.Material; look.SizingCombo = new KeyCombo(KeySpace.Physical, KeyStateKind.Pressed, new ComboKey(KeyCode.Z)); var row = new KeyBindingRow(scene.Editor, scene.Bindings, scene.Jump, look); CheckEqual(TestLooks.WidthOf(KeyBindingsText.English.CapturePrompt), row.CellWidth); } } public class WidestSlotTests : TestFixture { public void IsTheWidestBindingWithItsRemoveButton() { var scene = new KeyBindingSlotTests.Scene(); Scalar expected = TestLooks.WidthOf("Ctrl+W") + TestLooks.Spacing + TestLooks.GlyphHeight; CheckEqual(expected, Row(scene, scene.Shoot).WidestSlot); } } public class LayoutTests : TestFixture { public void PutsTwoCellsALineAfterTheNameColumn() { var scene = new KeyBindingSlotTests.Scene(); KeyBindingRow row = Row(scene, scene.Jump); row.Layout(nameColumnWidth: 30, cellWidth: 0); int spacing = TestLooks.Spacing; int line = TestLooks.GlyphHeight; Scalar left = 30 + spacing; CheckEqual(Rect.FromPositionAndSize(Vector.Zero, new Vector(TestLooks.WidthOf("Jump"), line)), row.NameLabel.Bounds); CheckEqual(Rect.FromPositionAndSize(new Vector(left, 0), new Vector(Cell, line)), row.Slots[0].Bounds); CheckEqual(Rect.FromPositionAndSize(new Vector(left + Cell + spacing, 0), new Vector(Cell, line)), row.Slots[1].Bounds); CheckEqual(new Vector(left + Cell + spacing + Cell, line), row.Size); } public void AThirdCell_WrapsUnderTheFirst() { var scene = new KeyBindingSlotTests.Scene(); KeyBindingRow row = Row(scene, scene.Shoot); row.Layout(nameColumnWidth: 0, cellWidth: 0); Scalar step = TestLooks.GlyphHeight + TestLooks.Spacing; CheckEqual(new Vector(row.Slots[0].Bounds.Left, step), row.Slots[2].Bounds.TopLeft); CheckEqual(2 * TestLooks.GlyphHeight + TestLooks.Spacing, row.Size.Y); } public void ABindingWiderThanACell_TakesAWholeLineOfItsOwn() { var scene = new KeyBindingSlotTests.Scene(); scene.Editor.SetChord(scene.Bindings, scene.Jump, 1, Wide()); KeyBindingRow row = Row(scene, scene.Jump); row.Layout(nameColumnWidth: 0, cellWidth: 0); Scalar step = TestLooks.GlyphHeight + TestLooks.Spacing; Scalar left = row.Slots[0].Bounds.Left; CheckEqual(Rect.FromPositionAndSize(new Vector(left, step), new Vector(Cell + TestLooks.Spacing + Cell, TestLooks.GlyphHeight)), row.Slots[1].Bounds); CheckEqual(new Vector(left, 2 * step), row.Slots[2].Bounds.TopLeft); } public void AWiderCellWidth_IsUsed() { var scene = new KeyBindingSlotTests.Scene(); KeyBindingRow row = Row(scene, scene.Jump); row.Layout(nameColumnWidth: 0, cellWidth: 60); CheckEqual(60, row.Slots[0].Bounds.Width); CheckEqual(60, row.Slots[1].Bounds.Width); } public void NameWiderThanTheColumn_PushesTheCellsOver() { var scene = new KeyBindingSlotTests.Scene(); KeyBindingRow row = Row(scene, scene.Jump); row.Layout(nameColumnWidth: 0, cellWidth: 0); CheckEqual(TestLooks.WidthOf("Jump") + TestLooks.Spacing, row.Slots[0].Bounds.Left); } public void ATallLine_CentresTheName() { var scene = new KeyBindingSlotTests.Scene(); KeyBindingsMaterial look = TestLooks.Material; look.KeyTextStyle = TestLooks.TextOf(3, 8); var row = new KeyBindingRow(scene.Editor, scene.Bindings, scene.Jump, look); row.Layout(nameColumnWidth: 0, cellWidth: 0); CheckEqual(2, row.NameLabel.Bounds.Top); } public void Note_GoesOnTheLineUnderItsBinding() { var scene = new KeyBindingSlotTests.Scene(); KeyBindingRow row = Row(scene, scene.Shoot); scene.Editor.SetChord(scene.Bindings, scene.Shoot, 1, Chord.OnPress(KeyCode.Space)); row.Refresh(); row.Layout(nameColumnWidth: 0, cellWidth: 0); Scalar step = TestLooks.GlyphHeight + TestLooks.Spacing; Scalar left = TestLooks.WidthOf("Shoot") + TestLooks.Spacing; CheckEqual(Rect.FromPositionAndSize(new Vector(left, step), new Vector(TestLooks.WidthOf("Space: Also Jump"), TestLooks.GlyphHeight)), row.Notes[1].Bounds); CheckEqual(2 * step, row.Slots[2].Bounds.Top); CheckEqual(3 * TestLooks.GlyphHeight + 2 * TestLooks.Spacing, row.Size.Y); } public void KeepsThePositionAndSaysItResized() { var scene = new KeyBindingSlotTests.Scene(); KeyBindingRow row = Row(scene, scene.Jump); row.Bounds = Rect.FromPositionAndSize(new Vector(7, 8), row.Size); KeyBindingRow resized = null; row.Resized += which => resized = which; row.Layout(nameColumnWidth: 0, cellWidth: 0); CheckEqual(new Vector(7, 8), row.Bounds.TopLeft); CheckEqual(row, resized); } public void ASlotThatResizes_LaysTheRowOutAgain() { var scene = new KeyBindingSlotTests.Scene(); KeyBindingRow row = Row(scene, scene.Jump); row.Layout(nameColumnWidth: 0, cellWidth: 0); KeyBindingRow resized = null; row.Resized += which => resized = which; scene.Activate(row.Slots[0]); CheckEqual(row, resized); CheckEqual(Cell, row.Slots[0].Bounds.Width); } } } }