using System; using System.Collections.Generic; using Glass; using UnitTestSharp; using Vellum; namespace Clave.Glass.UnitTests { public class KeyBindingsEditorTests : TestFixture { public class Setup { public readonly KeyBindingsTests.Game Game = new KeyBindingsTests.Game(); public readonly KeyBindingsEditor Editor; public Setup() { Editor = new KeyBindingsEditor(Game.Bindings); } public KeyBindings Bindings => Game.Bindings; } public static Chord Captured(KeyStateKind kind) { return new KeyCombo(KeySpace.Physical, kind, new ComboKey(KeyCode.W)).ToChord(); } public static SavedKeyBindings RoundTripped(SavedKeyBindings saved) { SaveConverters converters = SaveConverters.For(typeof(KeyBindingsEditorTests).Assembly); SaveNode save = SaveNode.NewTable(); save.Entries["value"] = converters.CreateSession().ToNode(saved); SaveNode text = LuaSaveFormat.Parse(LuaSaveFormat.Format(save)).RequireEntry("value"); return converters.CreateSession().FromNode(text); } public class CtorTests : TestFixture { public void ListsTheSources() { var setup = new Setup(); CheckEqual(1, setup.Editor.Sources.Count); CheckEqual(setup.Bindings, setup.Editor.Sources[0]); CheckFalse(setup.Editor.CanUndo); } public void TwoSourcesWithOneId_Throw() { var keys = new KeyboardManager(); CheckThrow(typeof(ArgumentException)); new KeyBindingsEditor(new KeyBindings("game", keys), new KeyBindings("game", keys)); } public void NullSources_Throw() { CheckThrow(typeof(ArgumentNullException)); new KeyBindingsEditor(null); } public void NullSource_Throws() { CheckThrow(typeof(ArgumentNullException)); new KeyBindingsEditor((KeyBindings)null); } } public class SetChordTests : TestFixture { public void ReplacesTheChordAtTheIndex() { var setup = new Setup(); Chord w = Chord.OnPress(KeyCode.W); setup.Editor.SetChord(setup.Bindings, setup.Game.Fire, 1, w); CheckEqual(setup.Game.Ctrl, setup.Bindings.ChordsOf(setup.Game.Fire)[0]); CheckEqual(w, setup.Bindings.ChordsOf(setup.Game.Fire)[1]); CheckTrue(setup.Editor.CanUndo); } public void IndexOfTheCount_AddsTheChord() { var setup = new Setup(); Chord w = Chord.OnPress(KeyCode.W); setup.Editor.SetChord(setup.Bindings, setup.Game.Jump, 1, w); CheckEqual(2, setup.Bindings.ChordsOf(setup.Game.Jump).Count); CheckEqual(w, setup.Bindings.ChordsOf(setup.Game.Jump)[1]); } public void IndexPastTheCount_Throws() { var setup = new Setup(); CheckThrow(typeof(ArgumentOutOfRangeException)); setup.Editor.SetChord(setup.Bindings, setup.Game.Jump, 2, Chord.OnPress(KeyCode.W)); } public void NegativeIndex_Throws() { var setup = new Setup(); CheckThrow(typeof(ArgumentOutOfRangeException)); setup.Editor.SetChord(setup.Bindings, setup.Game.Jump, -1, Chord.OnPress(KeyCode.W)); } public void NullChord_Throws() { var setup = new Setup(); CheckThrow(typeof(ArgumentNullException)); setup.Editor.SetChord(setup.Bindings, setup.Game.Jump, 0, null); } public void SourceNotEdited_Throws() { var setup = new Setup(); var other = new KeyBindings("other", new KeyboardManager(), setup.Game.Jump); CheckThrow(typeof(ArgumentException)); setup.Editor.SetChord(other, setup.Game.Jump, 0, Chord.OnPress(KeyCode.W)); } public void ChordThatFailsToBind_LeavesNothingToUndo() { var setup = new Setup(); CheckThrow(typeof(ArgumentException)); try { setup.Editor.SetChord(setup.Bindings, setup.Game.Jump, 0, setup.Game.Click); } finally { CheckFalse(setup.Editor.CanUndo); } } } public class RemoveChordTests : TestFixture { public void TakesOutTheChordAtTheIndex() { var setup = new Setup(); setup.Editor.RemoveChord(setup.Bindings, setup.Game.Fire, 0); CheckEqual(1, setup.Bindings.ChordsOf(setup.Game.Fire).Count); CheckEqual(setup.Game.Click, setup.Bindings.ChordsOf(setup.Game.Fire)[0]); CheckTrue(setup.Editor.CanUndo); } public void IndexOfTheCount_Throws() { var setup = new Setup(); CheckThrow(typeof(ArgumentOutOfRangeException)); setup.Editor.RemoveChord(setup.Bindings, setup.Game.Jump, 1); } public void NegativeIndex_Throws() { var setup = new Setup(); CheckThrow(typeof(ArgumentOutOfRangeException)); setup.Editor.RemoveChord(setup.Bindings, setup.Game.Jump, -1); } public void SourceNotEdited_Throws() { var setup = new Setup(); var other = new KeyBindings("other", new KeyboardManager(), setup.Game.Jump); CheckThrow(typeof(ArgumentException)); setup.Editor.RemoveChord(other, setup.Game.Jump, 0); } } public class ResetToDefaultsTests : TestFixture { public void PutsEveryChangedActionBack() { var setup = new Setup(); setup.Editor.SetChord(setup.Bindings, setup.Game.Jump, 0, Chord.OnPress(KeyCode.W)); setup.Editor.RemoveChord(setup.Bindings, setup.Game.Fire, 0); setup.Editor.ResetToDefaults(); CheckTrue(setup.Bindings.IsDefault(setup.Game.Jump)); CheckTrue(setup.Bindings.IsDefault(setup.Game.Fire)); } public void IsOneChangeToUndo() { var setup = new Setup(); Chord w = Chord.OnPress(KeyCode.W); setup.Editor.SetChord(setup.Bindings, setup.Game.Jump, 0, w); setup.Editor.RemoveChord(setup.Bindings, setup.Game.Fire, 0); setup.Editor.ResetToDefaults(); setup.Editor.Undo(); CheckEqual(w, setup.Bindings.ChordsOf(setup.Game.Jump)[0]); CheckEqual(1, setup.Bindings.ChordsOf(setup.Game.Fire).Count); } public void WhenNothingChanged_LeavesNothingToUndo() { var setup = new Setup(); setup.Editor.ResetToDefaults(); CheckFalse(setup.Editor.CanUndo); } } public class UndoTests : TestFixture { public void TakesBackTheLatestChange() { var setup = new Setup(); Chord w = Chord.OnPress(KeyCode.W); setup.Editor.SetChord(setup.Bindings, setup.Game.Jump, 0, w); setup.Editor.SetChord(setup.Bindings, setup.Game.Jump, 0, Chord.OnPress(KeyCode.Y)); setup.Editor.Undo(); CheckEqual(w, setup.Bindings.ChordsOf(setup.Game.Jump)[0]); CheckTrue(setup.Editor.CanUndo); } public void EveryChange_CanBeTakenBack() { var setup = new Setup(); setup.Editor.SetChord(setup.Bindings, setup.Game.Jump, 0, Chord.OnPress(KeyCode.W)); setup.Editor.SetChord(setup.Bindings, setup.Game.Jump, 0, Chord.OnPress(KeyCode.Y)); setup.Editor.Undo(); setup.Editor.Undo(); CheckEqual(setup.Game.Space, setup.Bindings.ChordsOf(setup.Game.Jump)[0]); CheckFalse(setup.Editor.CanUndo); } public void NothingToUndo_Throws() { CheckThrow(typeof(InvalidOperationException)); new Setup().Editor.Undo(); } } public class ClashesOfTests : TestFixture { public void ChordOfAnotherAction_BothFire() { var setup = new Setup(); Chord space = Chord.OnPress(KeyCode.Space); List clashes = setup.Editor.ClashesOf(setup.Bindings, setup.Game.Fire, space); CheckEqual(1, clashes.Count); CheckEqual(new KeyClash(ChordConflictKind.BothFire, "Jump"), clashes[0]); } public void ChordOfTheSameAction_IsLeftOut() { var setup = new Setup(); CheckEqual(0, setup.Editor.ClashesOf(setup.Bindings, setup.Game.Jump, Chord.OnPress(KeyCode.Space)).Count); } public void Tab_IsTakenByNavigation() { var setup = new Setup(); List clashes = setup.Editor.ClashesOf(setup.Bindings, setup.Game.Jump, Chord.OnPress(KeyCode.Tab)); CheckEqual(2, clashes.Count); CheckTrue(clashes.Contains(new KeyClash(ChordConflictKind.HiddenBy, KeyboardManager.NextControlName))); CheckTrue(clashes.Contains( new KeyClash(ChordConflictKind.HiddenBy, KeyboardManager.PreviousControlName))); } public void AFallbackTakenByAStrongerChord_IsLeftOut() { var setup = new Setup(); var back = new KeyAction("back", "Back", KeyStateKind.Pressed); var fallback = new KeyBindings("fallback", setup.Game.Keys, KeyPriority.Fallback, back); var editor = new KeyBindingsEditor(setup.Bindings, fallback); CheckEqual(0, editor.ClashesOf(fallback, back, Chord.OnPress(KeyCode.Space)).Count); } public void AFallbackOnTheSameKeysAsAnotherFallback_BothFire() { var setup = new Setup(); var back = new KeyAction("back", "Back", KeyStateKind.Pressed); var menu = new KeyAction("menu", "Menu", KeyStateKind.Pressed, Chord.OnPress(KeyCode.M)); var fallback = new KeyBindings("fallback", setup.Game.Keys, KeyPriority.Fallback, back, menu); var editor = new KeyBindingsEditor(fallback); List clashes = editor.ClashesOf(fallback, back, Chord.OnPress(KeyCode.M)); CheckEqual(1, clashes.Count); CheckEqual(new KeyClash(ChordConflictKind.BothFire, "Menu"), clashes[0]); } public void ChordsItTakesTheKeysFrom_AreLeftOut() { var setup = new Setup(); Chord shiftSpace = Chord.OnPress(KeyCode.Space).WithAnyHeld(KeyState.Shift); CheckEqual(0, setup.Editor.ClashesOf(setup.Bindings, setup.Game.Fire, shiftSpace).Count); } public void UnnamedShortcut_HasNoName() { var setup = new Setup(); setup.Game.Keys.AddShortcut(Chord.OnPress(KeyCode.W)); List clashes = setup.Editor.ClashesOf(setup.Bindings, setup.Game.Jump, Chord.OnPress(KeyCode.W)); CheckEqual(new KeyClash(ChordConflictKind.BothFire, null), clashes[0]); } public void TwoChordsOfOneAction_ClashOnce() { var setup = new Setup(); setup.Editor.SetChord(setup.Bindings, setup.Game.Fire, 2, Chord.OnPress(KeyCode.W)); setup.Editor.SetChord(setup.Bindings, setup.Game.Fire, 3, Chord.Hold(KeyCode.W)); List clashes = setup.Editor.ClashesOf(setup.Bindings, setup.Game.Jump, Chord.OnPress(KeyCode.W)); CheckEqual(1, clashes.Count); } public void ADisabledAction_StillClashes() { var setup = new Setup(); setup.Bindings.SetEnabled(setup.Game.Jump, false); List clashes = setup.Editor.ClashesOf(setup.Bindings, setup.Game.Fire, Chord.OnPress(KeyCode.Space)); CheckEqual(1, clashes.Count); CheckEqual(new KeyClash(ChordConflictKind.BothFire, "Jump"), clashes[0]); } public void ActionsInContextsTheGameSettled_AreLeftOut() { var setup = new Setup(); var dragging = new ChordContext("dragging"); var drawing = new ChordContext("drawing"); setup.Game.Keys.ContextRules.Settle(dragging, drawing); var cancelDrag = new KeyAction("cancelDrag", "Cancel the drag", KeyStateKind.Pressed, dragging, Chord.OnPress(KeyCode.Escape)); var cancelLine = new KeyAction("cancelLine", "Cancel a line", KeyStateKind.Pressed, drawing); var bindings = new KeyBindings("contexts", setup.Game.Keys, KeyPriority.Game, cancelDrag, cancelLine); var editor = new KeyBindingsEditor(bindings); CheckEqual(0, editor.ClashesOf(bindings, cancelLine, Chord.OnPress(KeyCode.Escape)).Count); } public void ActionsInContextsTheGameDidNotSettle_BothFire() { var setup = new Setup(); var dragging = new ChordContext("dragging"); var drawing = new ChordContext("drawing"); var cancelDrag = new KeyAction("cancelDrag", "Cancel the drag", KeyStateKind.Pressed, dragging, Chord.OnPress(KeyCode.Escape)); var cancelLine = new KeyAction("cancelLine", "Cancel a line", KeyStateKind.Pressed, drawing); var bindings = new KeyBindings("contexts", setup.Game.Keys, KeyPriority.Game, cancelDrag, cancelLine); var editor = new KeyBindingsEditor(bindings); List clashes = editor.ClashesOf(bindings, cancelLine, Chord.OnPress(KeyCode.Escape)); CheckEqual(1, clashes.Count); CheckEqual(new KeyClash(ChordConflictKind.BothFire, "Cancel the drag"), clashes[0]); } public void SourceNotEdited_Throws() { var setup = new Setup(); var other = new KeyBindings("other", new KeyboardManager()); CheckThrow(typeof(ArgumentException)); setup.Editor.ClashesOf(other, setup.Game.Jump, Chord.OnPress(KeyCode.W)); } public void NullAction_Throws() { var setup = new Setup(); CheckThrow(typeof(ArgumentNullException)); setup.Editor.ClashesOf(setup.Bindings, null, Chord.OnPress(KeyCode.W)); } } public class SaveTests : TestFixture { public void AllDefaults_SaveNothing() { CheckEqual(0, new Setup().Editor.Save().Sources.Count); } public void ChangedAction_IsSavedUnderItsSourceAndId() { var setup = new Setup(); Chord w = Chord.OnPress(KeyCode.W); setup.Editor.SetChord(setup.Bindings, setup.Game.Jump, 0, w); SavedKeyBindings saved = setup.Editor.Save(); CheckEqual(1, saved.Sources["game"].Count); CheckEqual(w, saved.Sources["game"]["jump"][0]); } public void ClearedAction_IsSavedAsNoChords() { var setup = new Setup(); setup.Editor.RemoveChord(setup.Bindings, setup.Game.Jump, 0); CheckEqual(0, setup.Editor.Save().Sources["game"]["jump"].Count); } public void ChordOfTheActionsKind_IsSavedAsFollowingIt() { var setup = new Setup(); Chord captured = Captured(KeyStateKind.Pressed); setup.Editor.SetChord(setup.Bindings, setup.Game.Jump, 0, captured); SavedKeyBindings saved = setup.Editor.Save(); CheckTrue(saved.Follows("game", "jump", captured)); } public void ChordOfAnotherKind_IsNotSavedAsFollowing() { var setup = new Setup(); Chord captured = Captured(KeyStateKind.Released); setup.Editor.SetChord(setup.Bindings, setup.Game.Jump, 0, captured); CheckFalse(setup.Editor.Save().Follows("game", "jump", captured)); } public void ChordTheActionStartsWith_IsNotSavedAsFollowing() { var setup = new Setup(); setup.Editor.SetChord(setup.Bindings, setup.Game.Jump, 1, Captured(KeyStateKind.Pressed)); SavedKeyBindings saved = setup.Editor.Save(); CheckFalse(saved.Follows("game", "jump", setup.Game.Jump.Defaults[0])); } public void ChordNoCombosMatches_IsNotSavedAsFollowing() { var setup = new Setup(); Chord either = Chord.OnPress(KeyCode.W) | Chord.OnPress(KeyCode.Q); setup.Editor.SetChord(setup.Bindings, setup.Game.Jump, 0, either); CheckFalse(setup.Editor.Save().Follows("game", "jump", either)); } public void ChordsOfAnUnpluggedAction_KeepWhetherTheyFollow() { var setup = new Setup(); Chord captured = Captured(KeyStateKind.Pressed); SavedKeyBindings holding = LoadTests.Holding("mod", "dash", captured); holding.FollowsKind["mod"] = new Dictionary> { { "dash", new List { captured } }, }; setup.Editor.Load(holding); CheckTrue(setup.Editor.Save().Follows("mod", "dash", captured)); } } public class LoadTests : TestFixture { public static SavedKeyBindings Holding(string source, string action, params Chord[] chords) { var returnMe = new SavedKeyBindings(); returnMe.Sources[source] = new Dictionary> { { action, new List(chords) } }; return returnMe; } public void BindsWhatTheSaveHolds() { var setup = new Setup(); Chord w = Chord.OnPress(KeyCode.W); setup.Editor.Load(Holding("game", "jump", w)); CheckEqual(w, setup.Bindings.ChordsOf(setup.Game.Jump)[0]); } public void ActionsTheSaveLeavesOut_GoBackToTheirDefaults() { var setup = new Setup(); setup.Editor.RemoveChord(setup.Bindings, setup.Game.Fire, 0); setup.Editor.Load(Holding("game", "jump", Chord.OnPress(KeyCode.W))); CheckTrue(setup.Bindings.IsDefault(setup.Game.Fire)); } public void ForgetsWhatThereWasToUndo() { var setup = new Setup(); setup.Editor.RemoveChord(setup.Bindings, setup.Game.Fire, 0); setup.Editor.Load(new SavedKeyBindings()); CheckFalse(setup.Editor.CanUndo); } public void ChordsThatDidNotLoad_AreLeftOut() { var setup = new Setup(); Chord w = Chord.OnPress(KeyCode.W); setup.Editor.Load(Holding("game", "jump", null, w)); CheckEqual(1, setup.Bindings.ChordsOf(setup.Game.Jump).Count); } public void ActionListThatDidNotLoad_IsNoChords() { var setup = new Setup(); SavedKeyBindings saved = Holding("game", "jump"); saved.Sources["game"]["jump"] = null; setup.Editor.Load(saved); CheckEqual(0, setup.Bindings.ChordsOf(setup.Game.Jump).Count); } public void SourcesThatDidNotLoad_AreSkipped() { var setup = new Setup(); SavedKeyBindings saved = Holding("game", "jump", Chord.OnPress(KeyCode.W)); saved.Sources["other"] = null; setup.Editor.Load(saved); CheckFalse(setup.Editor.Save().Sources.ContainsKey("other")); saved.Sources = null; setup.Editor.Load(saved); CheckTrue(setup.Bindings.IsDefault(setup.Game.Jump)); } public void SourceNotPluggedIn_IsKeptForTheNextSave() { var setup = new Setup(); Chord w = Chord.OnPress(KeyCode.W); setup.Editor.Load(Holding("mod", "dash", w)); CheckEqual(w, setup.Editor.Save().Sources["mod"]["dash"][0]); } public void ActionNoLongerThere_IsKeptForTheNextSave() { var setup = new Setup(); Chord w = Chord.OnPress(KeyCode.W); setup.Editor.Load(Holding("game", "crouch", w)); CheckEqual(w, setup.Editor.Save().Sources["game"]["crouch"][0]); CheckTrue(setup.Bindings.IsDefault(setup.Game.Jump)); } public void ALaterLoad_ForgetsWhatAnEarlierOneKept() { var setup = new Setup(); setup.Editor.Load(Holding("mod", "dash", Chord.OnPress(KeyCode.W))); setup.Editor.Load(new SavedKeyBindings()); CheckEqual(0, setup.Editor.Save().Sources.Count); } public void ChordFollowingTheKind_TakesTheKindTheActionHasNow() { var setup = new Setup(); Chord savedAsRelease = Captured(KeyStateKind.Released); SavedKeyBindings saved = Holding("game", "jump", savedAsRelease); saved.FollowsKind["game"] = new Dictionary> { { "jump", new List { savedAsRelease } }, }; setup.Editor.Load(saved); Chord bound = setup.Bindings.ChordsOf(setup.Game.Jump)[0]; CheckTrue(bound.IsEquivalentTo(Captured(KeyStateKind.Pressed))); } public void ChordNotFollowingTheKind_KeepsTheKindItWasSavedWith() { var setup = new Setup(); Chord savedAsRelease = Captured(KeyStateKind.Released); setup.Editor.Load(Holding("game", "jump", savedAsRelease)); CheckEqual(savedAsRelease, setup.Bindings.ChordsOf(setup.Game.Jump)[0]); } public void ChordThatDidNotLoad_DoesNotShiftWhichOnesFollow() { var setup = new Setup(); Chord following = Captured(KeyStateKind.Released); Chord other = Chord.OnPress(KeyCode.Q); SavedKeyBindings saved = Holding("game", "jump", null, other, following); saved.FollowsKind["game"] = new Dictionary> { { "jump", new List { following } }, }; setup.Editor.Load(saved); IReadOnlyList bound = setup.Bindings.ChordsOf(setup.Game.Jump); CheckEqual(other, bound[0]); CheckTrue(bound[1].IsEquivalentTo(Captured(KeyStateKind.Pressed))); } public void Null_Throws() { CheckThrow(typeof(ArgumentNullException)); new Setup().Editor.Load(null); } } public class TapActionTests : TestFixture { public class Scene { public readonly KeyAction Menu = new KeyAction("menu", "Menu", KeyStateKind.Tapped); public readonly KeyBindings Bindings; public readonly KeyBindingsEditor Editor; public Scene() { Bindings = new KeyBindings("taps", new KeyboardManager(), KeyPriority.Game, Menu); Editor = new KeyBindingsEditor(Bindings); } } public void ChordOfTheActionsKind_IsSavedAsFollowingIt() { var scene = new Scene(); Chord captured = Captured(KeyStateKind.Tapped); scene.Editor.SetChord(scene.Bindings, scene.Menu, 0, captured); CheckTrue(scene.Editor.Save().Follows("taps", "menu", captured)); } public void ChordOfAnotherKind_IsNotSavedAsFollowing() { var scene = new Scene(); Chord captured = Captured(KeyStateKind.Pressed); scene.Editor.SetChord(scene.Bindings, scene.Menu, 0, captured); CheckFalse(scene.Editor.Save().Follows("taps", "menu", captured)); } public void ChordFollowingTheKind_BecomesATapWhenTheActionIsATap() { var scene = new Scene(); Chord savedAsPress = Captured(KeyStateKind.Pressed); SavedKeyBindings saved = LoadTests.Holding("taps", "menu", savedAsPress); saved.FollowsKind["taps"] = new Dictionary> { { "menu", new List { savedAsPress } }, }; scene.Editor.Load(saved); Chord bound = scene.Bindings.ChordsOf(scene.Menu)[0]; CheckTrue(bound.IsEquivalentTo(Captured(KeyStateKind.Tapped))); } public void TapChord_SurvivesSavingAndLoadingAFile() { Chord tap = Chord.Tap(KeyCode.AltLeft); SavedKeyBindings loaded = RoundTripped(LoadTests.Holding("taps", "menu", tap)); CheckTrue(loaded.Sources["taps"]["menu"][0].IsEquivalentTo(tap)); } public void TapChordWithAHeldKey_SurvivesSavingAndLoadingAFile() { Chord tap = Chord.Tap(KeyCode.F4).WithAnyHeld(KeyState.Alt); SavedKeyBindings loaded = RoundTripped(LoadTests.Holding("taps", "menu", tap)); CheckTrue(loaded.Sources["taps"]["menu"][0].IsEquivalentTo(tap)); } } } }