using System; using System.Collections.Generic; using Glass; using UnitTestSharp; namespace Clave.Glass.UnitTests { public class KeyBindingsTests : TestFixture { public static KeyboardSnapshot Pressing(KeyCode key) { return new KeyTracker().KeyDown(key, key, isRepeat: false); } // Jump on Space, and Fire on Ctrl and on the left mouse button. public class Game { public readonly KeyboardManager Keys = new KeyboardManager(); public readonly Chord Space = Chord.OnPress(KeyCode.Space); public readonly Chord Ctrl = Chord.OnPressAny(KeyState.Control); public readonly Chord Click = Chord.OnPress(KeyCode.MouseLeft); public readonly KeyAction Jump; public readonly KeyAction Fire; public readonly KeyBindings Bindings; public Game() { Jump = new KeyAction("jump", "Jump", KeyStateKind.Pressed, Space); Fire = new KeyAction("fire", "Fire", KeyStateKind.Pressed, Ctrl, Click); Bindings = new KeyBindings("game", Keys, Jump, Fire); } } public class CtorTests : TestFixture { public void BindsEachActionToItsDefaults() { var game = new Game(); CheckEqual(game.Space, game.Bindings.ChordsOf(game.Jump)[0]); CheckEqual(2, game.Bindings.ChordsOf(game.Fire).Count); CheckEqual(game.Click, game.Bindings.ChordsOf(game.Fire)[1]); } public void RegistersTheDefaultsAsNamedShortcuts() { var game = new Game(); CheckEqual("Jump", game.Keys.NameOf(game.Space)); CheckEqual("Fire", game.Keys.NameOf(game.Click)); } public void KeepsItsIdKeysAndPriority() { var keys = new KeyboardManager(); var bindings = new KeyBindings("menu", keys, KeyPriority.Fallback); CheckEqual("menu", bindings.Id); CheckEqual(keys, bindings.Keys); CheckEqual(KeyPriority.Fallback, bindings.Priority); CheckEqual(0, bindings.Actions.Count); } public void WithoutAPriority_IsAGameSource() { CheckEqual(KeyPriority.Game, new Game().Bindings.Priority); } public void ListsTheActionsInOrder() { var game = new Game(); CheckEqual(game.Jump, game.Bindings.Actions[0]); CheckEqual(game.Fire, game.Bindings.Actions[1]); } public void TwoActionsWithOneId_Throw() { CheckThrow(typeof(ArgumentException)); new KeyBindings("game", new KeyboardManager(), new KeyAction("jump", "Jump", KeyStateKind.Pressed), new KeyAction("jump", "Hop", KeyStateKind.Pressed)); } public void ADefaultSharedByTwoActions_ThrowsAndLeavesNoShortcuts() { var keys = new KeyboardManager(); Chord space = Chord.OnPress(KeyCode.Space); var jump = new KeyAction("jump", "Jump", KeyStateKind.Pressed, space); var hop = new KeyAction("hop", "Hop", KeyStateKind.Pressed, space); CheckThrow(typeof(ArgumentException)); try { new KeyBindings("game", keys, jump, hop); } finally { CheckNull(keys.NameOf(space)); } } public void EmptyId_Throws() { CheckThrow(typeof(ArgumentException)); new KeyBindings("", new KeyboardManager()); } public void NullKeys_Throw() { CheckThrow(typeof(ArgumentNullException)); new KeyBindings("game", null); } public void NullActions_Throw() { CheckThrow(typeof(ArgumentNullException)); new KeyBindings("game", new KeyboardManager(), KeyPriority.Game, null); } public void NullAction_Throws() { CheckThrow(typeof(ArgumentNullException)); new KeyBindings("game", new KeyboardManager(), (KeyAction)null); } } public class ChordsOfTests : TestFixture { public void ActionOfAnotherSource_Throws() { var game = new Game(); CheckThrow(typeof(ArgumentException)); game.Bindings.ChordsOf(new KeyAction("jump", "Jump", KeyStateKind.Pressed)); } public void Null_Throws() { CheckThrow(typeof(ArgumentNullException)); new Game().Bindings.ChordsOf(null); } } public class FrenchLabels : KeyLabeler { public override string LabelOf(KeyCode key) { return key == KeyCode.Space ? "Espace" : base.LabelOf(key); } } public class LabelOfTests : TestFixture { public void NamesTheFirstBinding() { var game = new Game(); CheckEqual("Ctrl", game.Bindings.LabelOf(game.Fire)); } public void FollowsARebind() { var game = new Game(); game.Bindings.Bind(game.Jump, new[] { Chord.OnPress(KeyCode.F5) }); CheckEqual("F5", game.Bindings.LabelOf(game.Jump)); } public void AnActionBoundToNothing_IsEmpty() { var game = new Game(); game.Bindings.Bind(game.Jump, new Chord[0]); CheckEqual("", game.Bindings.LabelOf(game.Jump)); } public void UsesTheManagersLabels() { var game = new Game(); game.Keys.KeyLabels = new FrenchLabels(); CheckEqual("Espace", game.Bindings.LabelOf(game.Jump)); } public void ActionOfAnotherSource_Throws() { CheckThrow(typeof(ArgumentException)); new Game().Bindings.LabelOf(new KeyAction("jump", "Jump", KeyStateKind.Pressed)); } } public class ActionOfTests : TestFixture { public void BoundChord_GivesItsAction() { var game = new Game(); CheckEqual(game.Fire, game.Bindings.ActionOf(game.Click)); } public void ChordNotBound_GivesNull() { CheckNull(new Game().Bindings.ActionOf(Chord.OnPress(KeyCode.Space))); } } public class IsDefaultTests : TestFixture { public void AtTheStart_IsDefault() { var game = new Game(); CheckTrue(game.Bindings.IsDefault(game.Fire)); } public void EqualChordsInAnotherOrder_AreDefault() { var game = new Game(); game.Bindings.Bind(game.Fire, new[] { Chord.OnPress(KeyCode.MouseLeft), Chord.OnPressAny(KeyState.Control) }); CheckTrue(game.Bindings.IsDefault(game.Fire)); } public void ADifferentChord_IsNot() { var game = new Game(); game.Bindings.Bind(game.Jump, new[] { Chord.OnPress(KeyCode.W) }); CheckFalse(game.Bindings.IsDefault(game.Jump)); } public void FewerChords_AreNot() { var game = new Game(); game.Bindings.Bind(game.Fire, new[] { game.Click }); CheckFalse(game.Bindings.IsDefault(game.Fire)); } } public class BindTests : TestFixture { public void NewChord_Fires() { var game = new Game(); Chord w = Chord.OnPress(KeyCode.W); game.Bindings.Bind(game.Jump, new[] { w }); CheckTrue(game.Bindings.ShouldFire(game.Jump, Pressing(KeyCode.W))); CheckEqual("Jump", game.Keys.NameOf(w)); } public void OldChord_StopsBeingAShortcut() { var game = new Game(); game.Bindings.Bind(game.Jump, new[] { Chord.OnPress(KeyCode.W) }); CheckNull(game.Keys.NameOf(game.Space)); CheckFalse(game.Bindings.ShouldFire(game.Jump, Pressing(KeyCode.Space))); } public void KeepsTheOrderGiven() { var game = new Game(); game.Bindings.Bind(game.Fire, new[] { game.Click, game.Ctrl }); CheckEqual(game.Click, game.Bindings.ChordsOf(game.Fire)[0]); CheckEqual(game.Ctrl, game.Bindings.ChordsOf(game.Fire)[1]); } public void ChordsAreCopied() { var game = new Game(); Chord w = Chord.OnPress(KeyCode.W); var chords = new List { w }; game.Bindings.Bind(game.Jump, chords); chords.Clear(); CheckEqual(w, game.Bindings.ChordsOf(game.Jump)[0]); } public void RaisesRebound() { var game = new Game(); KeyAction rebound = null; game.Bindings.Rebound += action => rebound = action; game.Bindings.Bind(game.Fire, new Chord[0]); CheckEqual(game.Fire, rebound); } public void ChordOfAnotherAction_ThrowsAndKeepsWhatItHad() { var game = new Game(); Chord w = Chord.OnPress(KeyCode.W); CheckThrow(typeof(ArgumentException)); try { game.Bindings.Bind(game.Jump, new[] { w, game.Click }); } finally { CheckEqual(game.Space, game.Bindings.ChordsOf(game.Jump)[0]); CheckEqual("Jump", game.Keys.NameOf(game.Space)); CheckNull(game.Keys.NameOf(w)); } } public void NullChords_Throw() { var game = new Game(); CheckThrow(typeof(ArgumentNullException)); game.Bindings.Bind(game.Jump, null); } public void NullChord_Throws() { var game = new Game(); CheckThrow(typeof(ArgumentNullException)); game.Bindings.Bind(game.Jump, new Chord[] { null }); } } public class IsEnabledTests : TestFixture { public void ANewAction_IsOn() { var game = new Game(); CheckTrue(game.Bindings.IsEnabled(game.Jump)); } public void ActionOfAnotherSource_Throws() { CheckThrow(typeof(ArgumentException)); new Game().Bindings.IsEnabled(new KeyAction("jump", "Jump", KeyStateKind.Pressed)); } } public class SetEnabledTests : TestFixture { public void AnActionSwitchedOff_DoesNotFire() { var game = new Game(); game.Bindings.SetEnabled(game.Fire, false); CheckFalse(game.Bindings.IsEnabled(game.Fire)); CheckFalse(game.Bindings.ShouldFire(game.Fire, Pressing(KeyCode.ControlLeft))); CheckFalse(game.Bindings.ShouldFire(game.Fire, Pressing(KeyCode.MouseLeft))); } public void LeavesTheOtherActionsOn() { var game = new Game(); game.Bindings.SetEnabled(game.Fire, false); CheckTrue(game.Bindings.ShouldFire(game.Jump, Pressing(KeyCode.Space))); } public void AnActionSwitchedBackOn_Fires() { var game = new Game(); game.Bindings.SetEnabled(game.Jump, false); game.Bindings.SetEnabled(game.Jump, true); CheckTrue(game.Bindings.ShouldFire(game.Jump, Pressing(KeyCode.Space))); } public void ARebind_KeepsItOff() { var game = new Game(); game.Bindings.SetEnabled(game.Jump, false); Chord w = Chord.OnPress(KeyCode.W); game.Bindings.Bind(game.Jump, new[] { w }); CheckFalse(game.Bindings.IsEnabled(game.Jump)); CheckFalse(game.Keys.IsShortcutEnabled(w)); } public void ARebindThatFails_KeepsTheOldChordsOff() { var game = new Game(); game.Bindings.SetEnabled(game.Jump, false); CheckThrow(typeof(ArgumentException)); try { game.Bindings.Bind(game.Jump, new[] { game.Ctrl }); } finally { CheckFalse(game.Keys.IsShortcutEnabled(game.Space)); } } public void ActionOfAnotherSource_Throws() { CheckThrow(typeof(ArgumentException)); new Game().Bindings.SetEnabled(new KeyAction("jump", "Jump", KeyStateKind.Pressed), false); } } public class ShouldFireTests : TestFixture { public void AnyChordOfTheAction_Fires() { var game = new Game(); CheckTrue(game.Bindings.ShouldFire(game.Fire, Pressing(KeyCode.ControlRight))); CheckTrue(game.Bindings.ShouldFire(game.Fire, Pressing(KeyCode.MouseLeft))); } public void OtherKeys_DoNotFire() { var game = new Game(); CheckFalse(game.Bindings.ShouldFire(game.Jump, Pressing(KeyCode.W))); } public void ActionOfAnotherSource_Throws() { var game = new Game(); CheckThrow(typeof(ArgumentException)); game.Bindings.ShouldFire(new KeyAction("jump", "Jump", KeyStateKind.Pressed), Pressing(KeyCode.W)); } } } }