MouseHoldTriggerTests Create Method Delta Engine Documentation

Namespace: DeltaEngine.Input.Tests
Assembly: DeltaEngine.Input.Tests (in DeltaEngine.Input.Tests.dll) Version: 1.1.1.0 (1.1.1)
Syntax

public void Create()
Examples

[Test, CloseAfterFirstFrame]
public void Create()
{
    var trigger = new MouseHoldTrigger(Rectangle.One, 0.5f, MouseButton.Right);
    Assert.AreEqual(Rectangle.One, trigger.HoldArea);
    Assert.AreEqual(0.5f, trigger.HoldTime);
    Assert.AreEqual(MouseButton.Right, trigger.Button);
}
    [Test, CloseAfterFirstFrame]
    public void CreateFromAttributes()
    {
        var trigger = new MouseHoldTrigger(new Dictionary<string, string>
            {
                { "HoldArea", "1 2 3 4" },
                { "HoldTime", "5.5" },
                { "Button", "Right" }
            });
        Assert.AreEqual(new Rectangle(1, 2, 3, 4), trigger.HoldArea);
        Assert.AreEqual(5.5f, trigger.HoldTime);
        Assert.AreEqual(MouseButton.Right, trigger.Button);
        Assert.Throws<MouseHoldTrigger.CannotCreateMouseHoldTriggerWithoutHoldArea>(
            () => new MouseHoldTrigger(new Dictionary<string, string>()));
    }

    [Test, CloseAfterFirstFrame]
    public void HoldMouseOutsideHoldArea()
    {
        if (mouse == null)
            return; //ncrunch: no coverage
        Vector2D mousePosition = -Vector2D.One;
        new Command(position => { mousePosition = position; }).Add(
            new MouseHoldTrigger(Rectangle.HalfCentered));
        SetMouseState(State.Pressing, Vector2D.Zero);
        SetMouseState(State.Pressed, Vector2D.Zero);
        AdvanceTimeAndUpdateEntities(1.05f);
        Assert.AreEqual(-Vector2D.One, mousePosition);
    }

    private void SetMouseState(State state, Vector2D position)
    {
        if (mouse == null)
            return; //ncrunch: no coverage
        mouse.SetNativePosition(position);
        mouse.SetButtonState(MouseButton.Left, state);
        AdvanceTimeAndUpdateEntities();
    }

    [Test, CloseAfterFirstFrame]
    public void HoldMouseHovering()
    {
        var drawArea = new Rectangle(0.25f, 0.25f, 0.5f, 0.25f);
        var rect = new FilledRect(drawArea, Color.Blue);
        var trigger = new MouseHoldTrigger(drawArea);
        trigger.Invoked += () => rect.Color = Color.Red;
        trigger.Movement = new Vector2D(0.3f, 0.3f);
        Assert.IsFalse(trigger.IsHovering());
        AdvanceTimeAndUpdateEntities(1.05f);
        Assert.IsFalse(trigger.IsHovering());
    }

    [Test, CloseAfterFirstFrame]
    public void MoveMouseInsideHoldArea()
    {
        if (mouse == null)
            return; //ncrunch: no coverage
        Vector2D mousePosition = -Vector2D.One;
        new Command(position => { mousePosition = position; }).Add(
            new MouseHoldTrigger(Rectangle.HalfCentered));
        SetMouseState(State.Pressing, Vector2D.Half);
        SetMouseState(State.Pressed, Vector2D.Half);
        AdvanceTimeAndUpdateEntities(0.5f);
        SetMouseState(State.Pressed, new Vector2D(0.6f, 0.6f));
        AdvanceTimeAndUpdateEntities(0.5f);
        Assert.AreEqual(Vector2D.Half, mousePosition);
    }

    //ncrunch: no coverage start
    [Test, CloseAfterFirstFrame, Category("Slow")]
    public void HoldMouseInsideHoldArea()
    {
        if (mouse == null)
            return; //ncrunch: no coverage
        Vector2D mousePosition = -Vector2D.One;
        new Command(position => { mousePosition = position; }).Add(
            new MouseHoldTrigger(Rectangle.HalfCentered));
        SetMouseState(State.Pressing, Vector2D.Half);
        SetMouseState(State.Pressed, Vector2D.Half);
        AdvanceTimeAndUpdateEntities(1.05f);
        Assert.AreEqual(Vector2D.Half, mousePosition);
    }
}
See Also