Skip to content

Commit c321578

Browse files
committed
Adds additional unit-tests for pad
1 parent b5d35a9 commit c321578

File tree

4 files changed

+147
-10
lines changed

4 files changed

+147
-10
lines changed

InputStateManager/InputManager.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public class InputManager
4040
public Key Key { get; }
4141
public Mouse Mouse { get; }
4242
private Pad[] pads;
43-
public Pad Pad(PlayerIndex p = PlayerIndex.One) => pads[(int)p];
43+
public Pad Pad(PlayerIndex playerIndex = PlayerIndex.One) => pads[(int)playerIndex];
44+
public Pad Pad(int playerIndex) => pads[playerIndex];
4445
public Touch Touch { get; }
4546

4647
public InputManager()

NUnitTests/NUnitTests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@
6565
<Compile Include="Tests\Key\KeyTests.PressRelease.cs" />
6666
<Compile Include="Tests\Mouse\MouseTests.Was.cs" />
6767
<Compile Include="Tests\Mouse\MouseTests.PressRelease.cs" />
68-
<Compile Include="Tests\Pad\PadTests.Others.cs" />
68+
<Compile Include="Tests\Pad\PadTests.Triggers.cs" />
69+
<Compile Include="Tests\Pad\PadTests.Connected.cs" />
6970
<Compile Include="Tests\Pad\PadTests.Was.cs" />
7071
<Compile Include="Tests\Pad\PadTests.PressRelease.cs" />
7172
<Compile Include="Tests\Touch\TouchTests.cs" />

NUnitTests/Tests/Pad/PadTests.Others.cs renamed to NUnitTests/Tests/Pad/PadTests.Connected.cs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@
3232
namespace NUnitTests.Tests.Pad
3333
{
3434
[TestFixture]
35-
[Category("InputStateManager.Pad.Others")]
35+
[Category("InputStateManager.Pad.Connected")]
3636
public partial class PadTests
3737
{
3838
[Test]
39-
public void OnlyOneGamepadIsConnected()
39+
public void ConnectingGamepadsAreDetected()
4040
{
4141
providerMock.SetupSequence(o => o.GetState(0))
4242
.Returns(IdleState)
@@ -79,14 +79,36 @@ public void OnlyOneGamepadIsConnected()
7979
Assert.IsFalse(input.Pad(PlayerIndex.Four).Is.Connected);
8080
input.Update();
8181
Assert.IsFalse(input.Pad().Is.Connected);
82-
Assert.IsFalse(input.Pad(PlayerIndex.Two).Is.Connected);
83-
Assert.IsFalse(input.Pad(PlayerIndex.Three).Is.Connected);
84-
Assert.IsTrue(input.Pad(PlayerIndex.Four).Is.Connected);
82+
Assert.IsFalse(input.Pad(1).Is.Connected);
83+
Assert.IsFalse(input.Pad(2).Is.Connected);
84+
Assert.IsTrue(input.Pad(3).Is.Connected);
85+
input.Update();
86+
Assert.IsTrue(input.Pad(0).Is.Connected);
87+
Assert.IsTrue(input.Pad(1).Is.Connected);
88+
Assert.IsTrue(input.Pad(2).Is.Connected);
89+
Assert.IsTrue(input.Pad(3).Is.Connected);
90+
}
91+
92+
[Test]
93+
public void JustConnectedWorks()
94+
{
95+
providerMock.SetupSequence(o => o.GetState(0))
96+
.Returns(GamePadState.Default)
97+
.Returns(IdleState)
98+
.Returns(IdleState)
99+
.Returns(GamePadState.Default);
100+
input.Update();
101+
Assert.IsFalse(input.Pad().Is.Connected);
102+
Assert.IsFalse(input.Pad().Is.JustConnected);
85103
input.Update();
86104
Assert.IsTrue(input.Pad().Is.Connected);
87-
Assert.IsTrue(input.Pad(PlayerIndex.Two).Is.Connected);
88-
Assert.IsTrue(input.Pad(PlayerIndex.Three).Is.Connected);
89-
Assert.IsTrue(input.Pad(PlayerIndex.Four).Is.Connected);
105+
Assert.IsTrue(input.Pad().Is.JustConnected);
106+
input.Update();
107+
Assert.IsTrue(input.Pad().Is.Connected);
108+
Assert.IsFalse(input.Pad().Is.JustConnected);
109+
input.Update();
110+
Assert.IsFalse(input.Pad().Is.Connected);
111+
Assert.IsFalse(input.Pad().Is.JustConnected);
90112
}
91113
}
92114
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// ***************************************************************************
2+
// This is free and unencumbered software released into the public domain.
3+
//
4+
// Anyone is free to copy, modify, publish, use, compile, sell, or
5+
// distribute this software, either in source code form or as a compiled
6+
// binary, for any purpose, commercial or non-commercial, and by any
7+
// means.
8+
//
9+
// In jurisdictions that recognize copyright laws, the author or authors
10+
// of this software dedicate any and all copyright interest in the
11+
// software to the public domain. We make this dedication for the benefit
12+
// of the public at large and to the detriment of our heirs and
13+
// successors. We intend this dedication to be an overt act of
14+
// relinquishment in perpetuity of all present and future rights to this
15+
// software under copyright law.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20+
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21+
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22+
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
// OTHER DEALINGS IN THE SOFTWARE.
24+
//
25+
// For more information, please refer to <http://unlicense.org>
26+
// ***************************************************************************
27+
28+
using Microsoft.Xna.Framework;
29+
using Microsoft.Xna.Framework.Input;
30+
using NUnit.Framework;
31+
32+
namespace NUnitTests.Tests.Pad
33+
{
34+
[TestFixture]
35+
[Category("InputStateManager.Pad.Triggers")]
36+
public partial class PadTests
37+
{
38+
private const float EPSILON = float.Epsilon;
39+
40+
private static GamePadState GetTriggers(float l, float r) => new GamePadState(
41+
new GamePadThumbSticks(Vector2.Zero, Vector2.Zero),
42+
new GamePadTriggers(l, r), new GamePadButtons(0),
43+
new GamePadDPad(ButtonState.Released, ButtonState.Released, ButtonState.Released, ButtonState.Released));
44+
45+
[Test]
46+
public void TriggersWork()
47+
{
48+
providerMock.SetupSequence(o => o.GetState(0))
49+
.Returns(IdleState)
50+
.Returns(GetTriggers(0.2f, 0.3f))
51+
.Returns(GetTriggers(0.3f, 0.4f))
52+
.Returns(IdleState);
53+
input.Update();
54+
Assert.AreEqual(0f, input.Pad().Is.Triggers.Left, EPSILON);
55+
Assert.AreEqual(0f, input.Pad().Is.Triggers.Right, EPSILON);
56+
input.Update();
57+
Assert.AreEqual(0.2f, input.Pad().Is.Triggers.Left, EPSILON);
58+
Assert.AreEqual(0.3f, input.Pad().Is.Triggers.Right, EPSILON);
59+
input.Update();
60+
Assert.AreEqual(0.3f, input.Pad().Is.Triggers.Left, EPSILON);
61+
Assert.AreEqual(0.4f, input.Pad().Is.Triggers.Right, EPSILON);
62+
input.Update();
63+
Assert.AreEqual(0f, input.Pad().Is.Triggers.Left, EPSILON);
64+
Assert.AreEqual(0f, input.Pad().Is.Triggers.Right, EPSILON);
65+
}
66+
67+
[Test]
68+
public void WasTriggersWork()
69+
{
70+
providerMock.SetupSequence(o => o.GetState(0))
71+
.Returns(IdleState)
72+
.Returns(GetTriggers(0.2f, 0.3f))
73+
.Returns(GetTriggers(0.3f, 0.4f))
74+
.Returns(IdleState);
75+
input.Update();
76+
input.Update();
77+
Assert.AreEqual(0f, input.Pad().Was.Triggers.Left, EPSILON);
78+
Assert.AreEqual(0f, input.Pad().Was.Triggers.Right, EPSILON);
79+
input.Update();
80+
Assert.AreEqual(0.2f, input.Pad().Was.Triggers.Left, EPSILON);
81+
Assert.AreEqual(0.3f, input.Pad().Was.Triggers.Right, EPSILON);
82+
input.Update();
83+
Assert.AreEqual(0.3f, input.Pad().Was.Triggers.Left, EPSILON);
84+
Assert.AreEqual(0.4f, input.Pad().Was.Triggers.Right, EPSILON);
85+
}
86+
87+
[Test]
88+
public void TriggersDeltasWork()
89+
{
90+
providerMock.SetupSequence(o => o.GetState(0))
91+
.Returns(IdleState)
92+
.Returns(GetTriggers(0.2f, 0.4f))
93+
.Returns(IdleState)
94+
.Returns(GetTriggers(0.3f, 0.5f))
95+
.Returns(GetTriggers(0.3f, 0.8f));
96+
input.Update();
97+
Assert.AreEqual(0f, input.Pad().Is.Triggers.LeftDelta, EPSILON);
98+
Assert.AreEqual(0f, input.Pad().Is.Triggers.RightDelta, EPSILON);
99+
input.Update();
100+
Assert.AreEqual(0.2f, input.Pad().Is.Triggers.LeftDelta, EPSILON);
101+
Assert.AreEqual(0.4f, input.Pad().Is.Triggers.RightDelta, EPSILON);
102+
input.Update();
103+
Assert.AreEqual(-0.2f, input.Pad().Is.Triggers.LeftDelta, EPSILON);
104+
Assert.AreEqual(-0.4f, input.Pad().Is.Triggers.RightDelta, EPSILON);
105+
input.Update();
106+
Assert.AreEqual(0.3f, input.Pad().Is.Triggers.LeftDelta, EPSILON);
107+
Assert.AreEqual(0.5f, input.Pad().Is.Triggers.RightDelta, EPSILON);
108+
input.Update();
109+
Assert.AreEqual(0f, input.Pad().Is.Triggers.LeftDelta, EPSILON);
110+
Assert.AreEqual(0.3f, input.Pad().Is.Triggers.RightDelta, EPSILON);
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)