68 lines
2.0 KiB
C#
68 lines
2.0 KiB
C#
using Microsoft.Xna.Framework;
|
|
using Microsoft.Xna.Framework.Input;
|
|
using MrGameEng.Input;
|
|
using Xunit;
|
|
|
|
namespace MrGameEng.Input.Tests;
|
|
|
|
public class InputManagerTests
|
|
{
|
|
private static MouseState Mouse(int x = 0, int y = 0, int wheel = 0, ButtonState left = ButtonState.Released) =>
|
|
new(x, y, wheel, left, ButtonState.Released, ButtonState.Released, ButtonState.Released, ButtonState.Released);
|
|
|
|
private static void Frame(InputManager input, KeyboardState keyboard = default, MouseState mouse = default) =>
|
|
input.Apply(keyboard, mouse, GamePadState.Default);
|
|
|
|
[Fact]
|
|
public void KeyPressed_OnlyOnTheFrameItGoesDown()
|
|
{
|
|
var input = new InputManager();
|
|
|
|
Frame(input, new KeyboardState(Keys.Space));
|
|
Assert.True(input.IsKeyPressed(Keys.Space));
|
|
Assert.True(input.IsKeyDown(Keys.Space));
|
|
|
|
Frame(input, new KeyboardState(Keys.Space));
|
|
Assert.False(input.IsKeyPressed(Keys.Space));
|
|
Assert.True(input.IsKeyDown(Keys.Space));
|
|
}
|
|
|
|
[Fact]
|
|
public void KeyReleased_OnlyOnTheFrameItGoesUp()
|
|
{
|
|
var input = new InputManager();
|
|
|
|
Frame(input, new KeyboardState(Keys.A));
|
|
Frame(input);
|
|
|
|
Assert.True(input.IsKeyReleased(Keys.A));
|
|
Frame(input);
|
|
Assert.False(input.IsKeyReleased(Keys.A));
|
|
}
|
|
|
|
[Fact]
|
|
public void MouseDeltaAndWheelDelta_ComputedBetweenFrames()
|
|
{
|
|
var input = new InputManager();
|
|
|
|
Frame(input, mouse: Mouse(x: 10, y: 10, wheel: 0));
|
|
Frame(input, mouse: Mouse(x: 25, y: 5, wheel: 120));
|
|
|
|
Assert.Equal(new Point(15, -5), input.MouseDelta);
|
|
Assert.Equal(120, input.WheelDelta);
|
|
Assert.Equal(new Point(25, 5), input.MousePosition);
|
|
}
|
|
|
|
[Fact]
|
|
public void MousePressed_DetectsLeftButtonEdge()
|
|
{
|
|
var input = new InputManager();
|
|
|
|
Frame(input, mouse: Mouse());
|
|
Frame(input, mouse: Mouse(left: ButtonState.Pressed));
|
|
|
|
Assert.True(input.IsMousePressed(MouseButton.Left));
|
|
Assert.False(input.IsMousePressed(MouseButton.Right));
|
|
}
|
|
}
|