Skip to content

Commit e219820

Browse files
committed
Adding pad tests finished
1 parent 527e2b5 commit e219820

File tree

5 files changed

+400
-0
lines changed

5 files changed

+400
-0
lines changed

InputStateManager/Inputs/Pad.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ public bool OneRelease(params Buttons[] buttons)
135135

136136
public bool JustConnected
137137
=> !OldState().IsConnected && State().IsConnected;
138+
public bool StateChanged => OldState().PacketNumber != PacketNumber;
138139
}
139140

140141
[PublicAPI]

NUnitTests/NUnitTests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@
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.DPad.PressRelease.cs" />
69+
<Compile Include="Tests\Pad\PadTests.DPad.ThumbSticks.cs" />
70+
<Compile Include="Tests\Pad\PadTests.DPad.UpDown.cs" />
6871
<Compile Include="Tests\Pad\PadTests.Triggers.cs" />
6972
<Compile Include="Tests\Pad\PadTests.Connected.cs" />
7073
<Compile Include="Tests\Pad\PadTests.Was.cs" />
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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.Input;
29+
using NUnit.Framework;
30+
31+
namespace NUnitTests.Tests.Pad
32+
{
33+
[TestFixture]
34+
[Category("InputStateManager.Pad.DPad.PressRelease")]
35+
public partial class PadTests
36+
{
37+
[Test]
38+
public void DPadPressTriggers()
39+
{
40+
providerMock.SetupSequence(o => o.GetState(0))
41+
.Returns(GetDpadS(ButtonState.Pressed));
42+
input.Update();
43+
Assert.IsTrue(input.Pad().Is.DPad.Press(InputStateManager.Inputs.Pad.DPadDirection.UP));
44+
}
45+
46+
[Test]
47+
public void DPadPressIsResetWhenKeyIsReleased()
48+
{
49+
providerMock.SetupSequence(o => o.GetState(0))
50+
.Returns(GetDpadS(ButtonState.Pressed))
51+
.Returns(IdleState);
52+
input.Update();
53+
Assert.IsTrue(input.Pad().Is.DPad.Press(InputStateManager.Inputs.Pad.DPadDirection.UP));
54+
input.Update();
55+
Assert.IsFalse(input.Pad().Is.DPad.Press(InputStateManager.Inputs.Pad.DPadDirection.UP));
56+
}
57+
58+
[Test]
59+
public void DPadPressIsResetWhenKeyIsStillDown()
60+
{
61+
providerMock.SetupSequence(o => o.GetState(0))
62+
.Returns(GetDpadS(ButtonState.Pressed))
63+
.Returns(GetDpadS(ButtonState.Pressed));
64+
input.Update();
65+
Assert.IsTrue(input.Pad().Is.DPad.Press(InputStateManager.Inputs.Pad.DPadDirection.UP));
66+
input.Update();
67+
Assert.IsFalse(input.Pad().Is.DPad.Press(InputStateManager.Inputs.Pad.DPadDirection.UP));
68+
}
69+
70+
[Test]
71+
public void DPadReleaseTriggers()
72+
{
73+
providerMock.SetupSequence(o => o.GetState(0))
74+
.Returns(GetDpadS(ButtonState.Pressed))
75+
.Returns(GetDpadS(ButtonState.Pressed))
76+
.Returns(IdleState);
77+
input.Update();
78+
Assert.IsFalse(input.Pad().Is.DPad.Release(InputStateManager.Inputs.Pad.DPadDirection.UP));
79+
input.Update();
80+
Assert.IsFalse(input.Pad().Is.DPad.Release(InputStateManager.Inputs.Pad.DPadDirection.UP));
81+
input.Update();
82+
Assert.IsTrue(input.Pad().Is.DPad.Release(InputStateManager.Inputs.Pad.DPadDirection.UP));
83+
}
84+
85+
[Test]
86+
public void DPadReleaseIsResetProperly()
87+
{
88+
providerMock.SetupSequence(o => o.GetState(0))
89+
.Returns(GetDpadS(ButtonState.Pressed))
90+
.Returns(IdleState)
91+
.Returns(IdleState);
92+
input.Update();
93+
Assert.IsFalse(input.Pad().Is.DPad.Release(InputStateManager.Inputs.Pad.DPadDirection.UP));
94+
input.Update();
95+
Assert.IsTrue(input.Pad().Is.DPad.Release(InputStateManager.Inputs.Pad.DPadDirection.UP));
96+
input.Update();
97+
Assert.IsFalse(input.Pad().Is.DPad.Press(InputStateManager.Inputs.Pad.DPadDirection.UP));
98+
}
99+
100+
[Test]
101+
public void DPadPressAndReleaseWorksWithSeveralKeys()
102+
{
103+
providerMock.SetupSequence(o => o.GetState(0))
104+
.Returns(GetDpadM(ButtonState.Pressed, ButtonState.Pressed))
105+
.Returns(GetDpadS(ButtonState.Pressed));
106+
input.Update();
107+
Assert.IsTrue(input.Pad().Is.DPad.Press(InputStateManager.Inputs.Pad.DPadDirection.UP,
108+
InputStateManager.Inputs.Pad.DPadDirection.DOWN));
109+
Assert.IsFalse(input.Pad().Is.DPad.Release(InputStateManager.Inputs.Pad.DPadDirection.UP,
110+
InputStateManager.Inputs.Pad.DPadDirection.DOWN));
111+
input.Update();
112+
Assert.IsFalse(input.Pad().Is.DPad.Press(InputStateManager.Inputs.Pad.DPadDirection.UP,
113+
InputStateManager.Inputs.Pad.DPadDirection.DOWN));
114+
Assert.IsFalse(input.Pad().Is.DPad.Release(InputStateManager.Inputs.Pad.DPadDirection.UP,
115+
InputStateManager.Inputs.Pad.DPadDirection.DOWN));
116+
input.Update();
117+
Assert.IsFalse(input.Pad().Is.DPad.Press(InputStateManager.Inputs.Pad.DPadDirection.UP,
118+
InputStateManager.Inputs.Pad.DPadDirection.DOWN));
119+
// A and B were not released at once.
120+
Assert.IsFalse(input.Pad().Is.DPad.Release(InputStateManager.Inputs.Pad.DPadDirection.UP,
121+
InputStateManager.Inputs.Pad.DPadDirection.DOWN));
122+
}
123+
124+
[Test]
125+
public void DPadOnePressAndOneReleaseWorksWithSeveralKeys()
126+
{
127+
providerMock.SetupSequence(o => o.GetState(0))
128+
.Returns(GetDpadS(ButtonState.Pressed))
129+
.Returns(GetDpadM(ButtonState.Pressed, ButtonState.Pressed))
130+
.Returns(GetDpadS(ButtonState.Pressed));
131+
input.Update();
132+
Assert.IsTrue(input.Pad().Is.DPad.OnePress(InputStateManager.Inputs.Pad.DPadDirection.UP,
133+
InputStateManager.Inputs.Pad.DPadDirection.DOWN));
134+
Assert.IsFalse(input.Pad().Is.DPad.OneRelease(InputStateManager.Inputs.Pad.DPadDirection.UP,
135+
InputStateManager.Inputs.Pad.DPadDirection.DOWN));
136+
input.Update();
137+
Assert.IsTrue(input.Pad().Is.DPad.OnePress(InputStateManager.Inputs.Pad.DPadDirection.UP,
138+
InputStateManager.Inputs.Pad.DPadDirection.DOWN));
139+
Assert.IsFalse(input.Pad().Is.DPad.OneRelease(InputStateManager.Inputs.Pad.DPadDirection.UP,
140+
InputStateManager.Inputs.Pad.DPadDirection.DOWN));
141+
input.Update();
142+
Assert.IsFalse(input.Pad().Is.DPad.OnePress(InputStateManager.Inputs.Pad.DPadDirection.UP,
143+
InputStateManager.Inputs.Pad.DPadDirection.DOWN));
144+
Assert.IsTrue(input.Pad().Is.DPad.OneRelease(InputStateManager.Inputs.Pad.DPadDirection.UP,
145+
InputStateManager.Inputs.Pad.DPadDirection.DOWN));
146+
input.Update();
147+
Assert.IsFalse(input.Pad().Is.DPad.OnePress(InputStateManager.Inputs.Pad.DPadDirection.UP,
148+
InputStateManager.Inputs.Pad.DPadDirection.DOWN));
149+
Assert.IsTrue(input.Pad().Is.DPad.OneRelease(InputStateManager.Inputs.Pad.DPadDirection.UP,
150+
InputStateManager.Inputs.Pad.DPadDirection.DOWN));
151+
}
152+
}
153+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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.DPad.ThumbSticks")]
36+
public partial class PadTests
37+
{
38+
private static GamePadState GetThumb(Vector2 l, Vector2 r) => new GamePadState(
39+
new GamePadThumbSticks(l, r), new GamePadTriggers(0f, 0f), new GamePadButtons(0),
40+
new GamePadDPad(ButtonState.Released, ButtonState.Released, ButtonState.Released, ButtonState.Released));
41+
42+
[Test]
43+
public void ThumbSticksWork()
44+
{
45+
providerMock.SetupSequence(o => o.GetState(0))
46+
.Returns(IdleState)
47+
.Returns(GetThumb(Vector2.One, Vector2.One));
48+
input.Update();
49+
Assert.AreEqual(Vector2.Zero, input.Pad().Is.ThumbSticks.Left);
50+
Assert.AreEqual(Vector2.Zero, input.Pad().Is.ThumbSticks.Right);
51+
input.Update();
52+
Assert.AreEqual(Vector2.One, input.Pad().Is.ThumbSticks.Left);
53+
Assert.AreEqual(Vector2.One, input.Pad().Is.ThumbSticks.Right);
54+
}
55+
56+
[Test]
57+
public void WasThumbSticksWork()
58+
{
59+
providerMock.SetupSequence(o => o.GetState(0))
60+
.Returns(IdleState)
61+
.Returns(GetThumb(Vector2.One, Vector2.One))
62+
.Returns(IdleState);
63+
input.Update();
64+
input.Update();
65+
Assert.AreEqual(Vector2.Zero, input.Pad().Was.ThumbSticks.Left);
66+
Assert.AreEqual(Vector2.Zero, input.Pad().Was.ThumbSticks.Right);
67+
input.Update();
68+
Assert.AreEqual(Vector2.One, input.Pad().Was.ThumbSticks.Left);
69+
Assert.AreEqual(Vector2.One, input.Pad().Was.ThumbSticks.Right);
70+
}
71+
72+
[Test]
73+
public void ThumbSticksDeltasWork()
74+
{
75+
providerMock.SetupSequence(o => o.GetState(0))
76+
.Returns(IdleState)
77+
.Returns(GetThumb(Vector2.One, Vector2.One))
78+
.Returns(GetThumb(new Vector2(0.5f, 0.5f), new Vector2(0.4f, 0.4f)))
79+
.Returns(GetThumb(Vector2.One, Vector2.One))
80+
.Returns(IdleState);
81+
input.Update();
82+
Assert.AreEqual(Vector2.Zero, input.Pad().Is.ThumbSticks.LeftDelta);
83+
Assert.AreEqual(Vector2.Zero, input.Pad().Is.ThumbSticks.RightDelta);
84+
input.Update();
85+
Assert.AreEqual(Vector2.One, input.Pad().Is.ThumbSticks.LeftDelta);
86+
Assert.AreEqual(Vector2.One, input.Pad().Is.ThumbSticks.RightDelta);
87+
input.Update();
88+
Assert.AreEqual(new Vector2(-0.5f, -0.5f), input.Pad().Is.ThumbSticks.LeftDelta);
89+
Assert.AreEqual(new Vector2(-0.6f, -0.6f), input.Pad().Is.ThumbSticks.RightDelta);
90+
input.Update();
91+
Assert.AreEqual(new Vector2(0.5f, 0.5f), input.Pad().Is.ThumbSticks.LeftDelta);
92+
Assert.AreEqual(new Vector2(0.6f, 0.6f), input.Pad().Is.ThumbSticks.RightDelta);
93+
input.Update();
94+
Assert.AreEqual(-Vector2.One, input.Pad().Is.ThumbSticks.LeftDelta);
95+
Assert.AreEqual(-Vector2.One, input.Pad().Is.ThumbSticks.RightDelta);
96+
}
97+
}
98+
}

0 commit comments

Comments
 (0)