using System; using System.Collections.Generic; using Annulus; using Azimuth; using Glass; using UnitTestSharp; namespace Clave.Glass.UnitTests { public class KeyBindingsScreenTests : TestFixture { // The slot scene's game, with a second source holding a Pause action. public class Scene : KeyBindingSlotTests.Scene { public readonly KeyAction Pause; public readonly KeyBindings Menu; public readonly KeyBindingsEditor Both; public readonly KeyBindingsScreen Screen; public Scene() { Pause = new KeyAction("pause", "Pause", KeyStateKind.Pressed, Chord.OnPress(KeyCode.P)); Menu = new KeyBindings("menu", Keys, KeyPriority.Fallback, Pause); Both = new KeyBindingsEditor(Bindings, Menu); Screen = new KeyBindingsScreen(Both, TestLooks.Material); } } public class CtorTests : TestFixture { public void HasARowPerActionInOrder() { var scene = new Scene(); CheckEqual(3, scene.Screen.Rows.Count); CheckEqual(scene.Jump, scene.Screen.Rows[0].Action); CheckEqual(scene.Shoot, scene.Screen.Rows[1].Action); CheckEqual(scene.Pause, scene.Screen.Rows[2].Action); CheckEqual(scene.Menu, scene.Screen.Rows[2].Source); } public void NullEditor_Throws() { CheckThrow(typeof(ArgumentNullException)); new KeyBindingsScreen(null, TestLooks.Material); } } public class RowOfTests : TestFixture { public void FindsTheActionsRow() { var scene = new Scene(); CheckEqual(scene.Screen.Rows[2], scene.Screen.RowOf(scene.Menu, scene.Pause)); } public void ActionOfAnotherSource_Throws() { var scene = new Scene(); CheckThrow(typeof(ArgumentException)); scene.Screen.RowOf(scene.Bindings, scene.Pause); } } public class ChangesTests : TestFixture { public void AChange_ShowsInItsRow() { var scene = new Scene(); scene.Both.SetChord(scene.Menu, scene.Pause, 0, Chord.OnPress(KeyCode.K)); CheckEqual("K", KeyBindingSlotTests.Shown(scene.Screen.Rows[2].Slots[0])); } public void AChange_UpdatesTheNotesOfOtherRows() { var scene = new Scene(); scene.Both.SetChord(scene.Bindings, scene.Shoot, 1, Chord.OnPress(KeyCode.K)); scene.Both.SetChord(scene.Bindings, scene.Jump, 0, Chord.OnPress(KeyCode.K)); CheckEqual("K: Also Jump", scene.Screen.Rows[1].Notes[1].Text.ToString()); } public void ALoad_ShowsInTheRows() { var scene = new Scene(); var saved = new SavedKeyBindings(); saved.Sources["menu"] = new Dictionary> { { "pause", new List() } }; scene.Both.Load(saved); CheckEqual(1, scene.Screen.Rows[2].Slots.Count); } public void CaptureThroughTheScreen_BindsTheKeys() { var scene = new Scene(); scene.Activate(scene.Screen.Rows[0].Slots[0]); scene.Tap(KeyCode.K); CheckEqual(Chord.OnPress(KeyCode.K).Formula, scene.Bindings.ChordsOf(scene.Jump)[0].Formula); CheckEqual("K", KeyBindingSlotTests.Shown(scene.Screen.Rows[0].Slots[0])); } } public class CancelCaptureTests : TestFixture { public void EndsACaptureInAnyRow() { var scene = new Scene(); scene.Activate(scene.Screen.Rows[2].Slots[1]); scene.Screen.CancelCapture(); CheckFalse(scene.Keys.Capturing); CheckFalse(scene.Screen.Rows[2].Slots[1].Capturing); CheckEqual(2, scene.Screen.Rows[2].Slots.Count); } } public class LayoutTests : TestFixture { public void StacksTheRows() { var scene = new Scene(); int spacing = TestLooks.Spacing; int line = TestLooks.GlyphHeight; // Jump and Pause have one chord and an add cell, so one line each, and Shoot's three cells take two. Scalar shootTop = line + spacing; Scalar pauseTop = shootTop + 2 * line + spacing + spacing; CheckEqual(Vector.Zero, scene.Screen.Rows[0].Bounds.TopLeft); CheckEqual(new Vector(0, shootTop), scene.Screen.Rows[1].Bounds.TopLeft); CheckEqual(new Vector(0, pauseTop), scene.Screen.Rows[2].Bounds.TopLeft); CheckEqual(pauseTop + line, scene.Screen.Size.Y); } public void IsAsWideAsTheNameAndTwoCells() { var scene = new Scene(); int spacing = TestLooks.Spacing; int cell = KeyBindingRowTests.Cell; CheckEqual(TestLooks.WidthOf("Shoot") + spacing + cell + spacing + cell, scene.Screen.Size.X); } public void LinesTheCellsUpPastTheWidestName() { var scene = new Scene(); Scalar left = TestLooks.WidthOf("Shoot") + TestLooks.Spacing; CheckEqual(left, scene.Screen.Rows[0].Slots[0].Bounds.Left); CheckEqual(left, scene.Screen.Rows[2].Slots[0].Bounds.Left); } public void ABindingTooWideForTwoCells_WidensEveryRowsCells() { var scene = new Scene(); KeyCombo longest = new KeyCombo(KeySpace.Physical, KeyStateKind.Pressed, new ComboKey(KeyCode.PageDown), new ComboKey(KeyCode.ControlLeft), new ComboKey(KeyCode.AltLeft), new ComboKey(KeyCode.ShiftLeft), new ComboKey(KeyCode.PageUp), new ComboKey(KeyCode.Insert)); scene.Both.SetChord(scene.Menu, scene.Pause, 0, longest.ToChord()); Scalar widest = scene.Screen.Rows[2].WidestSlot; CheckTrue(widest > 2 * KeyBindingRowTests.Cell + TestLooks.Spacing); CheckEqual((widest - TestLooks.Spacing) / 2, scene.Screen.Rows[0].Slots[0].Bounds.Width); CheckEqual(widest, scene.Screen.Rows[2].Slots[0].Bounds.Width); } public void ARowThatGrows_MovesTheOnesUnderIt() { var scene = new Scene(); KeyBindingsScreen resized = null; scene.Screen.Resized += which => resized = which; scene.Both.SetChord(scene.Bindings, scene.Shoot, 1, Chord.OnPress(KeyCode.Tab)); int line = TestLooks.GlyphHeight; int spacing = TestLooks.Spacing; // Shoot's note goes on its own line under its first line of cells. Scalar shootTop = line + spacing; Scalar pauseTop = shootTop + 3 * line + 2 * spacing + spacing; CheckEqual(new Vector(0, pauseTop), scene.Screen.Rows[2].Bounds.TopLeft); CheckEqual(scene.Screen, resized); } public void KeepsThePosition() { var scene = new Scene(); scene.Screen.Bounds = Rect.FromPositionAndSize(new Vector(3, 4), scene.Screen.Size); scene.Screen.Layout(); CheckEqual(new Vector(3, 4), scene.Screen.Bounds.TopLeft); } } } }