Esempio n. 1
0
        public void TestReleaseAlwaysPressedToOriginalTargets()
        {
            InputReceptor receptorBelow = null;
            InputReceptor receptorAbove = null;

            AddStep("setup", () =>
            {
                Child = new TestKeyBindingContainer
                {
                    RelativeSizeAxes = Axes.Both,
                    Children         = new Drawable[]
                    {
                        receptorBelow = new InputReceptor(true)
                        {
                            Size = new Vector2(100),
                        },
                        receptorAbove = new InputReceptor(false)
                        {
                            Size     = new Vector2(100),
                            Position = new Vector2(100),
                        }
                    }
                };
            });

            // Input is positional

            AddStep("move mouse to receptorBelow", () => InputManager.MoveMouseTo(receptorBelow));
            AddStep("press keybind1", () => InputManager.PressKey(Key.Up));
            AddAssert("receptorBelow received press", () => receptorBelow.PressedReceived);

            AddStep("move mouse to receptorAbove", () => InputManager.MoveMouseTo(receptorAbove));
            AddStep("release keybind1", () => InputManager.ReleaseKey(Key.Up));
            AddAssert("receptorBelow received release", () => receptorBelow.ReleasedReceived);
        }
Esempio n. 2
0
        public void TestPressKeyBeforeKeyBindingContainerAdded()
        {
            List <TestAction> pressedActions  = new List <TestAction>();
            List <TestAction> releasedActions = new List <TestAction>();

            AddStep("press key B", () => InputManager.PressKey(Key.B));

            AddStep("add container", () =>
            {
                pressedActions.Clear();
                releasedActions.Clear();

                Child = new TestKeyBindingContainer
                {
                    Child = new TestKeyBindingReceptor
                    {
                        Pressed  = a => pressedActions.Add(a),
                        Released = a => releasedActions.Add(a)
                    }
                };
            });

            AddStep("press key A", () => InputManager.PressKey(Key.A));
            AddAssert("only one action triggered", () => pressedActions.Count == 1);
            AddAssert("ActionA triggered", () => pressedActions[0] == TestAction.ActionA);
            AddAssert("no actions released", () => releasedActions.Count == 0);

            AddStep("release key A", () => InputManager.ReleaseKey(Key.A));
            AddAssert("only one action triggered", () => pressedActions.Count == 1);
            AddAssert("only one action released", () => releasedActions.Count == 1);
            AddAssert("ActionA released", () => releasedActions[0] == TestAction.ActionA);
        }
        public void TestTriggerWithNoKeyBindings()
        {
            bool pressedReceived  = false;
            bool releasedReceived = false;

            TestKeyBindingContainer keyBindingContainer = null;

            AddStep("add container", () =>
            {
                pressedReceived  = false;
                releasedReceived = false;

                Clear();

                Add(keyBindingContainer = new TestKeyBindingContainer
                {
                    Child = new TestKeyBindingReceptor
                    {
                        Pressed  = _ => pressedReceived = true,
                        Released = _ => releasedReceived = true
                    }
                });
            });

            AddStep("trigger press", () => keyBindingContainer.TriggerPressed(TestAction.Action1));
            AddAssert("press received", () => pressedReceived);

            AddStep("trigger release", () => keyBindingContainer.TriggerReleased(TestAction.Action1));
            AddAssert("release received", () => releasedReceived);
        }
Esempio n. 4
0
        public void TestUpdateViaQueriedReference()
        {
            KeyBindingContainer testContainer = new TestKeyBindingContainer();

            keyBindingStore.Register(testContainer);

            var backBinding = query().Single(k => k.ActionInt == (int)GlobalAction.Back);

            Assert.That(backBinding.KeyCombination.Keys, Is.EquivalentTo(new[] { InputKey.Escape }));

            var tsr = ThreadSafeReference.Create(backBinding);

            using (var usage = realmContextFactory.GetForWrite())
            {
                var binding = usage.Realm.ResolveReference(tsr);
                binding.KeyCombination = new KeyCombination(InputKey.BackSpace);

                usage.Commit();
            }

            Assert.That(backBinding.KeyCombination.Keys, Is.EquivalentTo(new[] { InputKey.BackSpace }));

            // check still correct after re-query.
            backBinding = query().Single(k => k.ActionInt == (int)GlobalAction.Back);
            Assert.That(backBinding.KeyCombination.Keys, Is.EquivalentTo(new[] { InputKey.BackSpace }));
        }
        public void TestUpdateViaQueriedReference()
        {
            KeyBindingContainer testContainer = new TestKeyBindingContainer();

            keyBindingStore.Register(testContainer, Enumerable.Empty <RulesetInfo>());

            realm.Run(outerRealm =>
            {
                var backBinding = outerRealm.All <RealmKeyBinding>().Single(k => k.ActionInt == (int)GlobalAction.Back);

                Assert.That(backBinding.KeyCombination.Keys, Is.EquivalentTo(new[] { InputKey.Escape }));

                var tsr = ThreadSafeReference.Create(backBinding);

                realm.Run(innerRealm =>
                {
                    var binding = innerRealm.ResolveReference(tsr);
                    innerRealm.Write(() => binding.KeyCombination = new KeyCombination(InputKey.BackSpace));
                });

                Assert.That(backBinding.KeyCombination.Keys, Is.EquivalentTo(new[] { InputKey.BackSpace }));

                // check still correct after re-query.
                backBinding = outerRealm.All <RealmKeyBinding>().Single(k => k.ActionInt == (int)GlobalAction.Back);
                Assert.That(backBinding.KeyCombination.Keys, Is.EquivalentTo(new[] { InputKey.BackSpace }));
            });
        }
Esempio n. 6
0
        public void TestSingleKeyRepeatEvents()
        {
            bool pressedReceived  = false;
            bool repeatedReceived = false;
            bool releasedReceived = false;

            AddStep("add container", () =>
            {
                pressedReceived  = false;
                repeatedReceived = false;
                releasedReceived = false;

                Child = new TestKeyBindingContainer(true)
                {
                    Child = new TestKeyBindingReceptor
                    {
                        Pressed  = a => pressedReceived = a == TestAction.ActionA,
                        Repeated = a => repeatedReceived = a == TestAction.ActionA,
                        Released = a => releasedReceived = a == TestAction.ActionA
                    }
                };
            });

            AddStep("press A", () => InputManager.PressKey(Key.A));
            AddAssert("press received", () => pressedReceived);

            for (int i = 0; i < 10; i++)
            {
                AddUntilStep($"repeat #{1 + i} received", () => repeatedReceived);
                AddStep("reset for next repeat", () => repeatedReceived = false);
            }

            AddStep("release A", () => InputManager.ReleaseKey(Key.A));
            AddAssert("release received", () => releasedReceived);
        }
Esempio n. 7
0
        public void TestDefaultsPopulationAndQuery()
        {
            Assert.That(query().Count, Is.EqualTo(0));

            KeyBindingContainer testContainer = new TestKeyBindingContainer();

            keyBindingStore.Register(testContainer);

            Assert.That(query().Count, Is.EqualTo(3));

            Assert.That(query().Where(k => k.ActionInt == (int)GlobalAction.Back).Count, Is.EqualTo(1));
            Assert.That(query().Where(k => k.ActionInt == (int)GlobalAction.Select).Count, Is.EqualTo(2));
        }
Esempio n. 8
0
        public void TestDefaultsPopulationAndQuery()
        {
            Assert.That(queryCount(), Is.EqualTo(0));

            KeyBindingContainer testContainer = new TestKeyBindingContainer();

            keyBindingStore.Register(testContainer, Enumerable.Empty <RulesetInfo>());

            Assert.That(queryCount(), Is.EqualTo(3));

            Assert.That(queryCount(GlobalAction.Back), Is.EqualTo(1));
            Assert.That(queryCount(GlobalAction.Select), Is.EqualTo(2));
        }
Esempio n. 9
0
        public void TestKeyHandledByOtherDrawableDoesNotTrigger()
        {
            List <TestAction> pressedActions  = new List <TestAction>();
            List <TestAction> releasedActions = new List <TestAction>();

            TextBox textBox = null;

            AddStep("add children", () =>
            {
                pressedActions.Clear();
                releasedActions.Clear();

                Child = new TestKeyBindingContainer
                {
                    Children = new Drawable[]
                    {
                        textBox = new BasicTextBox
                        {
                            Anchor = Anchor.Centre,
                            Origin = Anchor.Centre,
                            Size   = new Vector2(200, 30)
                        },
                        new TestKeyBindingReceptor
                        {
                            Pressed  = a => pressedActions.Add(a),
                            Released = a => releasedActions.Add(a)
                        }
                    }
                };
            });

            AddStep("focus textbox and move mouse away", () =>
            {
                InputManager.MoveMouseTo(textBox);
                InputManager.Click(MouseButton.Left);
                InputManager.MoveMouseTo(textBox, new Vector2(0, 100));
            });

            AddStep("press enter", () => InputManager.PressKey(Key.Enter));
            AddStep("press mouse button", () => InputManager.PressButton(MouseButton.Left));

            AddStep("release enter", () => InputManager.ReleaseKey(Key.Enter));
            AddStep("release mouse button", () => InputManager.ReleaseButton(MouseButton.Left));

            AddAssert("no pressed actions", () => pressedActions.Count == 0);
            AddAssert("no released actions", () => releasedActions.Count == 0);
        }
Esempio n. 10
0
        public void TestKeyCombinationRepeatEvents()
        {
            bool pressedReceived  = false;
            bool repeatedReceived = false;
            bool releasedReceived = false;

            AddStep("add container", () =>
            {
                pressedReceived  = false;
                repeatedReceived = false;
                releasedReceived = false;

                Child = new TestKeyBindingContainer
                {
                    Child = new TestKeyBindingReceptor
                    {
                        Pressed  = a => pressedReceived = a == TestAction.ActionAB,
                        Repeated = a => repeatedReceived = a == TestAction.ActionAB,
                        Released = a => releasedReceived = a == TestAction.ActionAB,
                    }
                };
            });

            AddStep("press A+B", () =>
            {
                InputManager.PressKey(Key.A);
                InputManager.PressKey(Key.B);
            });
            AddAssert("press received", () => pressedReceived);

            for (int i = 0; i < 10; i++)
            {
                AddUntilStep($"repeat #{1 + i} received", () => repeatedReceived);
                AddStep("reset for next repeat", () => repeatedReceived = false);
            }

            AddStep("release A", () => InputManager.ReleaseKey(Key.A));
            AddAssert("release received", () => releasedReceived);

            AddStep("reset for potential repeat", () => repeatedReceived = false);
            AddWaitStep("wait", 5);
            AddAssert("no repeat received", () => !repeatedReceived);

            AddStep("release B", () => InputManager.ReleaseKey(Key.B));
        }
        public void TestReleaseDoesNotTriggerWithoutPress()
        {
            TestInputReceptor shownReceptor  = null;
            TestInputReceptor hiddenReceptor = null;

            AddStep("set children", () =>
            {
                Child = new TestKeyBindingContainer
                {
                    Children = new[]
                    {
                        shownReceptor = new TestInputReceptor("first")
                        {
                            Anchor = Anchor.Centre,
                            Origin = Anchor.Centre,
                            Size   = new Vector2(100),
                            Colour = Color4.LightPink
                        },
                        hiddenReceptor = new TestInputReceptor("second")
                        {
                            Anchor = Anchor.Centre,
                            Origin = Anchor.Centre,
                            Size   = new Vector2(100),
                            Alpha  = 0,
                            Colour = Color4.LightGreen
                        }
                    }
                };
            });

            AddStep("click-hold shown receptor", () =>
            {
                InputManager.MoveMouseTo(shownReceptor);
                InputManager.PressButton(MouseButton.Left);
            });
            AddStep("hide shown receptor", () => shownReceptor.Hide());
            AddStep("show hidden receptor", () => hiddenReceptor.Show());
            AddStep("release button", () => InputManager.ReleaseButton(MouseButton.Left));

            AddAssert("shown pressed", () => shownReceptor.Pressed);
            AddAssert("shown released", () => shownReceptor.Released);
            AddAssert("hidden not pressed", () => !hiddenReceptor.Pressed);
            AddAssert("hidden not released", () => !hiddenReceptor.Released);
        }
Esempio n. 12
0
        public void TestDefaultsPopulationRemovesExcess()
        {
            Assert.That(queryCount(), Is.EqualTo(0));

            KeyBindingContainer testContainer = new TestKeyBindingContainer();

            // Add some excess bindings for an action which only supports 1.
            realm.Write(r =>
            {
                r.Add(new RealmKeyBinding(GlobalAction.Back, new KeyCombination(InputKey.A)));
                r.Add(new RealmKeyBinding(GlobalAction.Back, new KeyCombination(InputKey.S)));
                r.Add(new RealmKeyBinding(GlobalAction.Back, new KeyCombination(InputKey.D)));
            });

            Assert.That(queryCount(GlobalAction.Back), Is.EqualTo(3));

            keyBindingStore.Register(testContainer, Enumerable.Empty <RulesetInfo>());

            Assert.That(queryCount(GlobalAction.Back), Is.EqualTo(1));
        }
Esempio n. 13
0
        public void TestKeyRepeatDoesntFireWhenNotAlive()
        {
            int  pressedReceived            = 0;
            int  repeatedReceived           = 0;
            bool releasedReceived           = false;
            TestKeyBindingReceptor receptor = null;

            AddStep("add container", () =>
            {
                pressedReceived  = 0;
                repeatedReceived = 0;
                releasedReceived = false;

                Child = new TestKeyBindingContainer
                {
                    Child = receptor = new TestKeyBindingReceptor
                    {
                        Pressed  = a => pressedReceived += a == TestAction.ActionA ? 1 : 0,
                        Repeated = a => repeatedReceived += a == TestAction.ActionA ? 1 : 0,
                        Released = a => releasedReceived = a == TestAction.ActionA
                    }
                };
            });

            AddStep("press A", () => InputManager.PressKey(Key.A));
            AddUntilStep("wait for non-zero repeated", () => repeatedReceived > 0);

            AddStep("hide receptor", () => receptor.Hide());

            int stopReceivingCheck = 0;

            AddStep("store count", () => stopReceivingCheck = repeatedReceived);
            AddWaitStep("wait some", 5);
            AddAssert("ensure not incrementing", () => stopReceivingCheck == repeatedReceived);

            AddStep("release A", () => InputManager.ReleaseKey(Key.A));
            AddAssert("release received", () => releasedReceived);
            AddAssert("only one press received", () => pressedReceived == 1);
        }
Esempio n. 14
0
        public void TestReleasingSpecificModifierDoesNotReleaseCommonBindingIfOtherKeyIsActive()
        {
            bool pressedReceived  = false;
            bool releasedReceived = false;

            AddStep("add container", () =>
            {
                pressedReceived  = false;
                releasedReceived = false;

                Child = new TestKeyBindingContainer
                {
                    Child = new TestKeyBindingReceptor
                    {
                        Pressed  = _ => pressedReceived = true,
                        Released = _ => releasedReceived = true
                    }
                };
            });

            AddStep("press lctrl", () => InputManager.PressKey(Key.LControl));
            AddAssert("press received", () => pressedReceived);

            AddStep("reset variables", () =>
            {
                pressedReceived  = false;
                releasedReceived = false;
            });

            AddStep("press rctrl", () => InputManager.PressKey(Key.RControl));
            AddAssert("press not received", () => !pressedReceived);
            AddAssert("release not received", () => !releasedReceived);

            AddStep("release rctrl", () => InputManager.ReleaseKey(Key.RControl));
            AddAssert("release not received", () => !releasedReceived);

            AddStep("release lctrl", () => InputManager.ReleaseKey(Key.LControl));
            AddAssert("release received", () => releasedReceived);
        }
Esempio n. 15
0
        public void TestSingleKeyRepeatEvents()
        {
            int  pressedReceived  = 0;
            int  repeatedReceived = 0;
            bool releasedReceived = false;

            AddStep("add container", () =>
            {
                pressedReceived  = 0;
                repeatedReceived = 0;
                releasedReceived = false;

                Child = new TestKeyBindingContainer
                {
                    Child = new TestKeyBindingReceptor
                    {
                        Pressed  = a => pressedReceived += a == TestAction.ActionA ? 1 : 0,
                        Repeated = a => repeatedReceived += a == TestAction.ActionA ? 1 : 0,
                        Released = a => releasedReceived = a == TestAction.ActionA
                    }
                };
            });

            AddStep("press A", () => InputManager.PressKey(Key.A));
            AddAssert("press received", () => pressedReceived == 1);

            for (int i = 0; i < 10; i++)
            {
                int localI = i + 1;
                AddUntilStep($"repeat #{1 + i} received", () => repeatedReceived >= localI);
            }

            AddStep("release A", () => InputManager.ReleaseKey(Key.A));
            AddAssert("release received", () => releasedReceived);

            AddAssert("only one press received", () => pressedReceived == 1);
        }
Esempio n. 16
0
        public void TestDefaultsPopulationRemovesExcess()
        {
            Assert.That(queryCount(), Is.EqualTo(0));

            KeyBindingContainer testContainer = new TestKeyBindingContainer();

            // Add some excess bindings for an action which only supports 1.
            using (var realm = realmContextFactory.CreateContext())
                using (var transaction = realm.BeginWrite())
                {
                    realm.Add(new RealmKeyBinding
                    {
                        Action         = GlobalAction.Back,
                        KeyCombination = new KeyCombination(InputKey.A)
                    });

                    realm.Add(new RealmKeyBinding
                    {
                        Action         = GlobalAction.Back,
                        KeyCombination = new KeyCombination(InputKey.S)
                    });

                    realm.Add(new RealmKeyBinding
                    {
                        Action         = GlobalAction.Back,
                        KeyCombination = new KeyCombination(InputKey.D)
                    });

                    transaction.Commit();
                }

            Assert.That(queryCount(GlobalAction.Back), Is.EqualTo(3));

            keyBindingStore.Register(testContainer, Enumerable.Empty <RulesetInfo>());

            Assert.That(queryCount(GlobalAction.Back), Is.EqualTo(1));
        }