using System; using Annulus; using Azimuth; using Glass; using UnitTestSharp; namespace Clave.Glass.UnitTests { public class KeyBindingSlotTests : TestFixture { // Jump on Space and Shoot on either Ctrl with W, the second chord of Shoot being one no combo matches. public class Scene { public readonly KeyboardManager Keys = new KeyboardManager(); public readonly KeyTracker Tracker = new KeyTracker(); public readonly Chord CtrlW = Chord.OnPress(KeyCode.W).WithAnyHeld(KeyState.Control); public readonly Chord RepeatingS = Chord.OnPressOrRepeat(KeyCode.S); public readonly KeyAction Jump; public readonly KeyAction Shoot; public readonly KeyBindings Bindings; public readonly KeyBindingsEditor Editor; public Scene() { Jump = new KeyAction("jump", "Jump", KeyStateKind.Pressed, Chord.OnPress(KeyCode.Space)); Shoot = new KeyAction("shoot", "Shoot", KeyStateKind.Held, CtrlW, RepeatingS); Bindings = new KeyBindings("game", Keys, Jump, Shoot); Editor = new KeyBindingsEditor(Bindings); } public KeyBindingSlot Slot(KeyAction action, int index) { return new KeyBindingSlot(Editor, Bindings, action, index, TestLooks.Material); } public void Down(KeyCode key) { Keys.KeyDown(Tracker.KeyDown(key, key, isRepeat: false)); } public void Up(KeyCode key) { Keys.KeyUp(Tracker.KeyUp(key)); } public void Tap(KeyCode key) { Down(key); Up(key); } public void Activate(ButtonBase button) { button.Activate(Keys.Held, Keys); } } public static string TextOf(Control part) { if (part is Label label) { return label.Text.ToString(); } foreach (Control child in part.CanvasChildren.BackToFront()) { return "[" + ((Label)child).Text.ToString() + "]"; } return "[]"; } public static string Shown(KeyBindingSlot slot) { var texts = new string[slot.Parts.Count]; for (int i = 0; i < texts.Length; ++i) { texts[i] = TextOf(slot.Parts[i]); } return string.Join(" ", texts); } public class CtorTests : TestFixture { public void Combo_ShowsAButtonForASidedKeyAndALabelForTheOthers() { var scene = new Scene(); KeyBindingSlot slot = scene.Slot(scene.Shoot, 0); CheckEqual("[Ctrl] + W", Shown(slot)); CheckTrue(slot.Parts[0] is Button); CheckNotNull(slot.Combo); } public void ChordNoComboMatches_ShowsItsLabel() { var scene = new Scene(); KeyBindingSlot slot = scene.Slot(scene.Shoot, 1); CheckEqual("S", Shown(slot)); CheckNull(slot.Combo); } public void IndexOfTheCount_ShowsTheAddTextInTheEmptyLook() { var scene = new Scene(); KeyBindingSlot slot = scene.Slot(scene.Jump, 1); CheckNull(slot.Chord); CheckEqual(KeyBindingsText.English.AddChord, Shown(slot)); CheckEqual(TestLooks.Empty, slot.ButtonMaterial); } public void IsSizedToItsPartsAndItsRemoveButton() { var scene = new Scene(); KeyBindingSlot slot = scene.Slot(scene.Shoot, 0); int line = TestLooks.GlyphHeight; Scalar keysWidth = TestLooks.WidthOf("Ctrl+W"); Vector expected = new Vector(keysWidth + TestLooks.Spacing + line, line); CheckEqual(expected, slot.PreferredBoundsSize); CheckEqual(expected, slot.Size); CheckEqual(Rect.FromPositionAndSize(new Vector(TestLooks.WidthOf("Ctrl"), 0), new Vector(TestLooks.WidthOf("+"), line)), slot.Parts[1].Bounds); CheckEqual(Rect.FromPositionAndSize(new Vector(keysWidth + TestLooks.Spacing, 0), new Vector(line, line)), slot.RemoveButton.Bounds); CheckTrue(slot.RemoveButton.Visible); } public void TheAddSlot_HasNoRemoveButton() { var scene = new Scene(); KeyBindingSlot slot = scene.Slot(scene.Jump, 1); CheckFalse(slot.RemoveButton.Visible); CheckEqual(TestLooks.WidthOf(KeyBindingsText.English.AddChord), slot.Size.X); } public void ShowsTheChord() { var scene = new Scene(); CheckEqual(scene.CtrlW, scene.Slot(scene.Shoot, 0).Chord); } public void IndexPastTheCount_Throws() { var scene = new Scene(); CheckThrow(typeof(ArgumentOutOfRangeException)); scene.Slot(scene.Jump, 2); } public void NegativeIndex_Throws() { var scene = new Scene(); CheckThrow(typeof(ArgumentOutOfRangeException)); scene.Slot(scene.Jump, -1); } public void NullEditor_Throws() { var scene = new Scene(); CheckThrow(typeof(ArgumentNullException)); new KeyBindingSlot(null, scene.Bindings, scene.Jump, 0, TestLooks.Material); } public void NullSource_Throws() { var scene = new Scene(); CheckThrow(typeof(ArgumentNullException)); new KeyBindingSlot(scene.Editor, null, scene.Jump, 0, TestLooks.Material); } public void NullAction_Throws() { var scene = new Scene(); CheckThrow(typeof(ArgumentNullException)); new KeyBindingSlot(scene.Editor, scene.Bindings, null, 0, TestLooks.Material); } } public class BeginCaptureTests : TestFixture { public void Activating_StartsACapture() { var scene = new Scene(); KeyBindingSlot slot = scene.Slot(scene.Jump, 0); scene.Activate(slot); CheckTrue(slot.Capturing); CheckTrue(scene.Keys.Capturing); CheckEqual(KeyBindingsText.English.CapturePrompt, Shown(slot)); CheckEqual(TestLooks.Capturing, slot.ButtonMaterial); CheckEqual(new Vector(TestLooks.WidthOf(KeyBindingsText.English.CapturePrompt), TestLooks.GlyphHeight), slot.Size); CheckEqual(slot.Size, slot.Parts[0].Size); } public void SaysItResized() { var scene = new Scene(); KeyBindingSlot slot = scene.Slot(scene.Jump, 0); KeyBindingSlot resized = null; slot.Resized += which => resized = which; scene.Activate(slot); CheckEqual(slot, resized); } public void KeysLetGo_AreBoundAsTheActionsKind() { var scene = new Scene(); KeyBindingSlot slot = scene.Slot(scene.Shoot, 0); scene.Activate(slot); scene.Tap(KeyCode.Q); CheckEqual(Chord.Hold(KeyCode.Q).Formula, scene.Bindings.ChordsOf(scene.Shoot)[0].Formula); CheckFalse(slot.Capturing); CheckFalse(scene.Keys.Capturing); CheckEqual("Q", Shown(slot)); CheckEqual(TestLooks.Binding, slot.ButtonMaterial); } public void CapturedModifier_CountsOnEitherSide() { var scene = new Scene(); KeyBindingSlot slot = scene.Slot(scene.Jump, 0); scene.Activate(slot); scene.Down(KeyCode.ShiftRight); scene.Tap(KeyCode.J); scene.Up(KeyCode.ShiftRight); Chord expected = Chord.OnPress(KeyCode.J).WithAnyHeld(KeyState.Shift); CheckEqual(expected.Formula, scene.Bindings.ChordsOf(scene.Jump)[0].Formula); CheckEqual("[Shift] + J", Shown(slot)); } public void Escape_IsBoundLikeAnyKey() { var scene = new Scene(); KeyBindingSlot slot = scene.Slot(scene.Jump, 0); scene.Activate(slot); scene.Tap(KeyCode.Escape); CheckEqual(Chord.OnPress(KeyCode.Escape).Formula, scene.Bindings.ChordsOf(scene.Jump)[0].Formula); } public void KeyHeldAtTheStart_IsLeftOut() { var scene = new Scene(); KeyBindingSlot slot = scene.Slot(scene.Jump, 0); scene.Down(KeyCode.MainEnter); scene.Activate(slot); scene.Up(KeyCode.MainEnter); scene.Tap(KeyCode.K); CheckEqual(Chord.OnPress(KeyCode.K).Formula, scene.Bindings.ChordsOf(scene.Jump)[0].Formula); } public void SlotForTheCount_AddsTheChord() { var scene = new Scene(); KeyBindingSlot slot = scene.Slot(scene.Jump, 1); scene.Activate(slot); scene.Tap(KeyCode.K); CheckEqual(2, scene.Bindings.ChordsOf(scene.Jump).Count); CheckEqual("K", Shown(slot)); } public void TheCaptureIsOneChangeToUndo() { var scene = new Scene(); KeyBindingSlot slot = scene.Slot(scene.Jump, 0); scene.Activate(slot); scene.Tap(KeyCode.K); scene.Editor.Undo(); CheckFalse(scene.Editor.CanUndo); CheckTrue(scene.Bindings.IsDefault(scene.Jump)); } public void WhileCapturing_Throws() { var scene = new Scene(); KeyBindingSlot slot = scene.Slot(scene.Jump, 0); scene.Activate(slot); CheckThrow(typeof(InvalidOperationException)); slot.BeginCapture(scene.Keys.Held, scene.Keys); } public void WhileTheManagerCapturesForSomethingElse_ThrowsAndLeavesItAlone() { var scene = new Scene(); KeyBindingSlot slot = scene.Slot(scene.Jump, 0); scene.Keys.BeginCapture(_ => { }); CheckThrow(typeof(InvalidOperationException)); try { slot.BeginCapture(scene.Keys.Held, scene.Keys); } finally { CheckFalse(slot.Capturing); } } public void NullKeys_Throw() { var scene = new Scene(); CheckThrow(typeof(ArgumentNullException)); scene.Slot(scene.Jump, 0).BeginCapture(default, null); } } public class CancelCaptureTests : TestFixture { public void EndsTheCaptureBindingNothing() { var scene = new Scene(); KeyBindingSlot slot = scene.Slot(scene.Jump, 0); KeyBindingSlot cancelled = null; slot.CaptureCancelled += which => cancelled = which; scene.Activate(slot); slot.CancelCapture(); CheckEqual(slot, cancelled); CheckFalse(slot.Capturing); CheckFalse(scene.Keys.Capturing); CheckEqual("Space", Shown(slot)); CheckTrue(scene.Bindings.IsDefault(scene.Jump)); } public void WhenNotCapturing_DoesNothing() { var scene = new Scene(); KeyBindingSlot slot = scene.Slot(scene.Jump, 0); bool raised = false; slot.CaptureCancelled += _ => raised = true; slot.CancelCapture(); CheckFalse(raised); } } public class ShowTests : TestFixture { public void SidedKeyButton_StepsTheSideItCounts() { var scene = new Scene(); KeyBindingSlot slot = scene.Slot(scene.Shoot, 0); var ctrl = (Button)slot.Parts[0]; scene.Activate(ctrl); Chord expected = Chord.OnPress(KeyCode.W).WithAllHeld(new KeyState(KeyCode.ControlLeft)); CheckEqual(expected.Formula, scene.Bindings.ChordsOf(scene.Shoot)[0].Formula); CheckEqual("[Left Ctrl] + W", Shown(slot)); } public void SteppingASide_KeepsTheButton() { var scene = new Scene(); KeyBindingSlot slot = scene.Slot(scene.Shoot, 0); Control ctrl = slot.Parts[0]; scene.Activate((Button)ctrl); CheckEqual(ctrl, slot.Parts[0]); int line = TestLooks.GlyphHeight; CheckEqual(new Vector(TestLooks.WidthOf("Left Ctrl+W") + TestLooks.Spacing + line, line), slot.Size); } public void ThirdStep_GoesBackToEitherSide() { var scene = new Scene(); KeyBindingSlot slot = scene.Slot(scene.Shoot, 0); scene.Activate((Button)slot.Parts[0]); scene.Activate((Button)slot.Parts[0]); scene.Activate((Button)slot.Parts[0]); CheckEqual("[Ctrl] + W", Shown(slot)); } public void ChordChangedElsewhere_IsShownWhenAsked() { var scene = new Scene(); KeyBindingSlot slot = scene.Slot(scene.Jump, 0); scene.Editor.SetChord(scene.Bindings, scene.Jump, 0, Chord.OnPress(KeyCode.K).WithAnyHeld(KeyState.Alt)); slot.Show(); CheckEqual("[Alt] + K", Shown(slot)); } public void KeepsThePosition() { var scene = new Scene(); KeyBindingSlot slot = scene.Slot(scene.Jump, 0); slot.Bounds = Rect.FromPositionAndSize(new Vector(5, 6), slot.Size); slot.Show(); CheckEqual(new Vector(5, 6), slot.Bounds.TopLeft); } public void UsesTheManagersLabels() { var scene = new Scene(); scene.Keys.KeyLabels = new LowerCaseLabeler(); CheckEqual("space", Shown(scene.Slot(scene.Jump, 0))); } } public static KeyBindingSlot Focused(Scene scene, KeyBindingSlot slot) { scene.Keys.Controls.Add(slot); scene.Keys.TrySetFocus(slot); return slot; } public class RemoveChordTests : TestFixture { public void TakesTheChordOut() { var scene = new Scene(); scene.Slot(scene.Shoot, 0).RemoveChord(); CheckEqual(1, scene.Bindings.ChordsOf(scene.Shoot).Count); CheckEqual(scene.RepeatingS, scene.Bindings.ChordsOf(scene.Shoot)[0]); } public void OnTheAddSlot_DoesNothing() { var scene = new Scene(); scene.Slot(scene.Jump, 1).RemoveChord(); CheckEqual(1, scene.Bindings.ChordsOf(scene.Jump).Count); CheckFalse(scene.Editor.CanUndo); } } public class KeyDownTests : TestFixture { public void Delete_TakesTheFocusedChordOut() { var scene = new Scene(); Focused(scene, scene.Slot(scene.Jump, 0)); scene.Down(KeyCode.Delete); CheckEqual(0, scene.Bindings.ChordsOf(scene.Jump).Count); } public void Backspace_TakesTheFocusedChordOut() { var scene = new Scene(); Focused(scene, scene.Slot(scene.Jump, 0)); scene.Down(KeyCode.Backspace); CheckEqual(0, scene.Bindings.ChordsOf(scene.Jump).Count); } public void Delete_OnTheAddSlot_IsLeftForOthers() { var scene = new Scene(); KeyBindingSlot slot = Focused(scene, scene.Slot(scene.Jump, 1)); CheckFalse(slot.KeyDown(scene.Tracker.KeyDown(KeyCode.Delete, KeyCode.Delete, isRepeat: false), scene.Keys)); CheckEqual(1, scene.Bindings.ChordsOf(scene.Jump).Count); } public void Delete_WhileCapturing_IsCaptured() { var scene = new Scene(); KeyBindingSlot slot = Focused(scene, scene.Slot(scene.Jump, 0)); scene.Activate(slot); scene.Tap(KeyCode.Delete); CheckEqual(Chord.OnPress(KeyCode.Delete).Formula, scene.Bindings.ChordsOf(scene.Jump)[0].Formula); } public void Enter_StillStartsACapture() { var scene = new Scene(); KeyBindingSlot slot = Focused(scene, scene.Slot(scene.Jump, 0)); scene.Tap(KeyCode.MainEnter); CheckTrue(slot.Capturing); } } public class RemoveButtonTests : TestFixture { public void TakesTheChordOut() { var scene = new Scene(); KeyBindingSlot slot = scene.Slot(scene.Shoot, 0); scene.Activate(slot.RemoveButton); CheckEqual(1, scene.Bindings.ChordsOf(scene.Shoot).Count); } public void IsHiddenWhileCapturing() { var scene = new Scene(); KeyBindingSlot slot = scene.Slot(scene.Jump, 0); scene.Activate(slot); CheckFalse(slot.RemoveButton.Visible); } public void LabelFillsIt() { var scene = new Scene(); KeyBindingSlot slot = scene.Slot(scene.Jump, 0); foreach (Control label in slot.RemoveButton.CanvasChildren.BackToFront()) { CheckEqual(slot.RemoveButton.Size, label.Size); } } } public class ClashingTests : TestFixture { public void ShowsTheClashingLook() { var scene = new Scene(); KeyBindingSlot slot = scene.Slot(scene.Jump, 0); slot.Clashing = true; CheckTrue(slot.Clashing); CheckEqual(TestLooks.Clashing, slot.ButtonMaterial); } public void OnTheAddSlot_KeepsTheEmptyLook() { var scene = new Scene(); KeyBindingSlot slot = scene.Slot(scene.Jump, 1); slot.Clashing = true; CheckEqual(TestLooks.Empty, slot.ButtonMaterial); } public void WhileCapturing_KeepsTheCapturingLook() { var scene = new Scene(); KeyBindingSlot slot = scene.Slot(scene.Jump, 0); slot.Clashing = true; scene.Activate(slot); CheckEqual(TestLooks.Capturing, slot.ButtonMaterial); } } public class BoundsSizeForTests : TestFixture { public void Removable_AddsAGapAndTheRemoveButton() { Vector size = KeyBindingSlot.BoundsSizeFor(new Vector(10, 2), removable: true, TestLooks.Binding, TestLooks.Material); CheckEqual(new Vector(10 + TestLooks.Spacing + TestLooks.GlyphHeight, TestLooks.GlyphHeight), size); } public void NotRemovable_IsTheContent() { Vector size = KeyBindingSlot.BoundsSizeFor(new Vector(10, 2), removable: false, TestLooks.Binding, TestLooks.Material); CheckEqual(new Vector(10, 2), size); } public void IsNeverShorterThanTheLooksArt() { ButtonMaterial tall = new TextMaterial { Text = "x", Style = TestLooks.TextOf(3, 9) }; Vector size = KeyBindingSlot.BoundsSizeFor(new Vector(10, 2), removable: false, tall, TestLooks.Material); CheckEqual(9, size.Y); } public void Text_IsMeasuredInTheKeyStyle() { Vector size = KeyBindingSlot.BoundsSizeFor("abc", TestLooks.Binding, TestLooks.Material); CheckEqual(new Vector(TestLooks.WidthOf("abc"), TestLooks.GlyphHeight), size); } } public class ContentSizeForTests : TestFixture { public void IsEachKeyAndTheSeparators() { var combo = new KeyCombo(KeySpace.Physical, KeyStateKind.Pressed, new ComboKey(KeyCode.W), new ComboKey(KeyCode.ControlLeft, eitherSide: true)); Vector size = KeyBindingSlot.ContentSizeFor(combo, new KeyLabeler(), TestLooks.Material); CheckEqual(new Vector(TestLooks.WidthOf("Ctrl+W"), TestLooks.GlyphHeight), size); } public void NullCombo_Throws() { CheckThrow(typeof(ArgumentNullException)); KeyBindingSlot.ContentSizeFor(null, new KeyLabeler(), TestLooks.Material); } } public class ArrangeTests : TestFixture { public void KeysSitAtTheLeftCentredOnTheHeight() { var scene = new Scene(); KeyBindingSlot slot = scene.Slot(scene.Jump, 0); slot.Arrange(Rect.FromPositionAndSize(new Vector(5, 5), new Vector(40, 10))); CheckEqual(Rect.FromPositionAndSize(new Vector(5, 5), new Vector(40, 10)), slot.Bounds); CheckEqual(new Vector(0, 3), slot.Parts[0].Bounds.TopLeft); } } public class LowerCaseLabeler : KeyLabeler { public override string LabelOf(KeyCode key) { return base.LabelOf(key).ToLowerInvariant(); } } } }