|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Linq; |
| 4 | +using NUnit.Framework; |
| 5 | +using UnityEngine; |
| 6 | +using UnityEngine.InputSystem; |
| 7 | +using UnityEngine.TestTools; |
| 8 | +using TouchPhase = UnityEngine.InputSystem.TouchPhase; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Test suite to verify test fixture API published in <see cref="InputTestFixture"/>. |
| 12 | +/// </summary> |
| 13 | +/// <remarks> |
| 14 | +/// This test fixture captures confusion around usage reported in |
| 15 | +/// https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1637. |
| 16 | +/// </remarks> |
| 17 | +internal class InputTestFixtureTests : InputTestFixture |
| 18 | +{ |
| 19 | + private Keyboard correctlyUsedKeyboard; |
| 20 | + |
| 21 | + private Keyboard incorrectlyUsedDevice; |
| 22 | + private Gamepad incorrectlyUsedGamepad; |
| 23 | + private Touchscreen incorrectlyUsedTouchscreen; |
| 24 | + |
| 25 | + [OneTimeSetUp] |
| 26 | + public void UnitySetup() |
| 27 | + { |
| 28 | + // This is incorrect use since it will add a device to the actual input system since it executes before |
| 29 | + // the InputTestFixture.Setup() method. Hence, after Setup() has executed the device is not part of |
| 30 | + // the test input system instance. |
| 31 | + incorrectlyUsedDevice = InputSystem.AddDevice<Keyboard>(); |
| 32 | + Assert.That(InputSystem.devices.Contains(incorrectlyUsedDevice), Is.True); |
| 33 | + |
| 34 | + incorrectlyUsedGamepad = InputSystem.AddDevice<Gamepad>(); |
| 35 | + Assert.That(InputSystem.devices.Contains(incorrectlyUsedGamepad), Is.True); |
| 36 | + |
| 37 | + incorrectlyUsedTouchscreen = InputSystem.AddDevice<Touchscreen>(); |
| 38 | + Assert.That(InputSystem.devices.Contains(incorrectlyUsedTouchscreen), Is.True); |
| 39 | + } |
| 40 | + |
| 41 | + [OneTimeTearDown] |
| 42 | + public void UnityTearDown() |
| 43 | + { |
| 44 | + // Once InputTestFixture.TearDown() has executed, the state stack will have been popped and the correctlyUsedKeyboard |
| 45 | + // we added before entering the test fixture should have been restored. |
| 46 | + Assert.That(InputSystem.devices.Contains(incorrectlyUsedDevice), Is.True); |
| 47 | + Assert.That(InputSystem.devices.Contains(incorrectlyUsedGamepad), Is.True); |
| 48 | + Assert.That(InputSystem.devices.Contains(incorrectlyUsedTouchscreen), Is.True); |
| 49 | + } |
| 50 | + |
| 51 | + [SetUp] |
| 52 | + public override void Setup() |
| 53 | + { |
| 54 | + // At this point we are still using the actual system so our device created from UnitySetup() should still |
| 55 | + // exist with the context. |
| 56 | + Assert.That(InputSystem.devices.Contains(incorrectlyUsedDevice), Is.True); |
| 57 | + Assert.That(InputSystem.devices.Contains(incorrectlyUsedGamepad), Is.True); |
| 58 | + Assert.That(InputSystem.devices.Contains(incorrectlyUsedTouchscreen), Is.True); |
| 59 | + |
| 60 | + // This is expected usage pattern, first calling base.Setup() when overriding Setup like this, then |
| 61 | + // creating a fake device via the test fixture instance that only lives with the test context. |
| 62 | + base.Setup(); |
| 63 | + |
| 64 | + // Since we have now entered a temporary test state our device created in UnitySetup() will no longer exist |
| 65 | + // with this context. |
| 66 | + Assert.That(InputSystem.devices.Contains(incorrectlyUsedDevice), Is.False); |
| 67 | + Assert.That(InputSystem.devices.Contains(incorrectlyUsedGamepad), Is.False); |
| 68 | + Assert.That(InputSystem.devices.Contains(incorrectlyUsedTouchscreen), Is.False); |
| 69 | + } |
| 70 | + |
| 71 | + [TearDown] |
| 72 | + public override void TearDown() |
| 73 | + { |
| 74 | + // Restore state |
| 75 | + base.TearDown(); |
| 76 | + |
| 77 | + // Our test device should no longer exist with the system since we are back to real instance |
| 78 | + Assert.That(InputSystem.devices.Contains(correctlyUsedKeyboard), Is.False); |
| 79 | + |
| 80 | + Assert.That(InputSystem.devices.Contains(incorrectlyUsedDevice), Is.True); |
| 81 | + Assert.That(InputSystem.devices.Contains(incorrectlyUsedGamepad), Is.True); |
| 82 | + Assert.That(InputSystem.devices.Contains(incorrectlyUsedTouchscreen), Is.True); |
| 83 | + } |
| 84 | + |
| 85 | + #region Editor playmode tests |
| 86 | + |
| 87 | + [Test] |
| 88 | + public void Press_ShouldMutateDeviceState_WithinPlayModeTestFixtureContext() |
| 89 | + { |
| 90 | + correctlyUsedKeyboard = InputSystem.AddDevice<Keyboard>(); |
| 91 | + Press(correctlyUsedKeyboard.spaceKey); |
| 92 | + Assert.That(correctlyUsedKeyboard.spaceKey.isPressed, Is.True); |
| 93 | + } |
| 94 | + |
| 95 | + [Test] |
| 96 | + public void Press_ShouldThrow_WithinPlayModeTestFixtureContextIfInvalidDevice() |
| 97 | + { |
| 98 | + Assert.Throws<ArgumentException>(() => Press(incorrectlyUsedDevice.spaceKey)); |
| 99 | + } |
| 100 | + |
| 101 | + [Test] |
| 102 | + public void Release_ShouldMutateDeviceState_WithinPlayModeTestFixtureContext() |
| 103 | + { |
| 104 | + correctlyUsedKeyboard = InputSystem.AddDevice<Keyboard>(); |
| 105 | + Press(correctlyUsedKeyboard.spaceKey); |
| 106 | + Release(correctlyUsedKeyboard.spaceKey); |
| 107 | + Assert.That(correctlyUsedKeyboard.spaceKey.isPressed, Is.False); |
| 108 | + } |
| 109 | + |
| 110 | + [Test] |
| 111 | + public void Release_ShouldThrow_WithinPlayModeTestFixtureContextIfInvalidDevice() |
| 112 | + { |
| 113 | + Assert.Throws<ArgumentException>(() => Release(incorrectlyUsedDevice.spaceKey)); |
| 114 | + } |
| 115 | + |
| 116 | + [Test] |
| 117 | + public void PressAndRelease_ShouldMutateDeviceState_WithinPlayModeTestFixtureContext() |
| 118 | + { |
| 119 | + correctlyUsedKeyboard = InputSystem.AddDevice<Keyboard>(); |
| 120 | + PressAndRelease(correctlyUsedKeyboard.spaceKey); |
| 121 | + Assert.That(correctlyUsedKeyboard.spaceKey.isPressed, Is.False); |
| 122 | + } |
| 123 | + |
| 124 | + [Test] |
| 125 | + public void PressAndRelease_ShouldThrow_WithinPlayModeTestFixtureContextIfInvalidDevice() |
| 126 | + { |
| 127 | + Assert.Throws<ArgumentException>(() => PressAndRelease(incorrectlyUsedDevice.spaceKey)); |
| 128 | + } |
| 129 | + |
| 130 | + [Test] |
| 131 | + public void Click_ShouldMutateDeviceState_WithinPlayModeTestFixtureContext() |
| 132 | + { |
| 133 | + correctlyUsedKeyboard = InputSystem.AddDevice<Keyboard>(); |
| 134 | + Click(correctlyUsedKeyboard.spaceKey); |
| 135 | + Assert.That(correctlyUsedKeyboard.spaceKey.isPressed, Is.False); |
| 136 | + } |
| 137 | + |
| 138 | + [Test] |
| 139 | + public void Click_ShouldThrow_WithinPlayModeTestFixtureContextIfInvalidDevice() |
| 140 | + { |
| 141 | + Assert.Throws<ArgumentException>(() => Click(incorrectlyUsedDevice.spaceKey)); |
| 142 | + } |
| 143 | + |
| 144 | + [Test] |
| 145 | + public void Move_ShouldMutateDeviceState_WithinPlayModeTestFixtureContext() |
| 146 | + { |
| 147 | + var gamepad = InputSystem.AddDevice<Gamepad>(); |
| 148 | + Move(gamepad.leftStick, new Vector2(1.0f, 0.0f)); |
| 149 | + Assert.That(gamepad.leftStick.value, Is.EqualTo(new Vector2(1.0f, 0.0f))); |
| 150 | + } |
| 151 | + |
| 152 | + [Test] |
| 153 | + public void Move_ShouldThrow_WithinPlayModeTestFixtureContextIfInvalidDevice() |
| 154 | + { |
| 155 | + Assert.Throws<ArgumentException>(() => Move(incorrectlyUsedGamepad.leftStick, new Vector2(1.0f, 0.0f))); |
| 156 | + } |
| 157 | + |
| 158 | + [Test] |
| 159 | + public void Touch_ShouldMutateDeviceState_WithinPlayModeTestFixtureContext() |
| 160 | + { |
| 161 | + var touchscreen = InputSystem.AddDevice<Touchscreen>(); |
| 162 | + BeginTouch(1, Vector2.zero, screen: touchscreen); |
| 163 | + Assert.That(touchscreen.touches.Count, Is.EqualTo(10)); |
| 164 | + Assert.That(touchscreen.touches[0].touchId.value, Is.EqualTo(1)); |
| 165 | + Assert.That(touchscreen.touches[0].position.value, Is.EqualTo(Vector2.zero)); |
| 166 | + Assert.That(touchscreen.touches[0].phase.value, Is.EqualTo(TouchPhase.Began)); |
| 167 | + |
| 168 | + MoveTouch(1, Vector2.one, screen: touchscreen); |
| 169 | + Assert.That(touchscreen.touches.Count, Is.EqualTo(10)); |
| 170 | + Assert.That(touchscreen.touches[0].touchId.value, Is.EqualTo(1)); |
| 171 | + Assert.That(touchscreen.touches[0].position.value, Is.EqualTo(Vector2.one)); |
| 172 | + Assert.That(touchscreen.touches[0].phase.value, Is.EqualTo(TouchPhase.Moved)); |
| 173 | + |
| 174 | + EndTouch(1, Vector2.zero, screen: touchscreen); |
| 175 | + Assert.That(touchscreen.touches.Count, Is.EqualTo(10)); |
| 176 | + Assert.That(touchscreen.touches[0].touchId.value, Is.EqualTo(1)); |
| 177 | + Assert.That(touchscreen.touches[0].position.value, Is.EqualTo(Vector2.zero)); |
| 178 | + Assert.That(touchscreen.touches[0].phase.value, Is.EqualTo(TouchPhase.Ended)); |
| 179 | + } |
| 180 | + |
| 181 | + [Test] |
| 182 | + public void Touch_ShouldThrow_WithinPlayModeTestFixtureContextIfInvalidDevice() |
| 183 | + { |
| 184 | + Assert.Throws<ArgumentException>(() => BeginTouch(1, Vector2.zero, screen: incorrectlyUsedTouchscreen)); |
| 185 | + Assert.Throws<ArgumentException>(() => MoveTouch(1, Vector2.zero, screen: incorrectlyUsedTouchscreen)); |
| 186 | + Assert.Throws<ArgumentException>(() => EndTouch(1, Vector2.zero, screen: incorrectlyUsedTouchscreen)); |
| 187 | + } |
| 188 | + |
| 189 | + #endregion // Playmode tests |
| 190 | + |
| 191 | + #region // Edit-mode tests |
| 192 | + |
| 193 | + [UnityTest] |
| 194 | + public IEnumerator Press_ShouldThrow_WithinEditModeTestFixtureContext() |
| 195 | + { |
| 196 | + correctlyUsedKeyboard = InputSystem.AddDevice<Keyboard>(); |
| 197 | + Assert.Throws<NotSupportedException>(() => Press(correctlyUsedKeyboard.spaceKey)); |
| 198 | + yield break; |
| 199 | + } |
| 200 | + |
| 201 | + [UnityTest] |
| 202 | + public IEnumerator Release_ShouldThrow_WithinEditModeTestFixtureContext() |
| 203 | + { |
| 204 | + correctlyUsedKeyboard = InputSystem.AddDevice<Keyboard>(); |
| 205 | + Assert.Throws<NotSupportedException>(() => Release(correctlyUsedKeyboard.spaceKey)); |
| 206 | + yield break; |
| 207 | + } |
| 208 | + |
| 209 | + [UnityTest] |
| 210 | + public IEnumerator PressAndRelease_ShouldThrow_WithinEditModeTestFixtureContext() |
| 211 | + { |
| 212 | + correctlyUsedKeyboard = InputSystem.AddDevice<Keyboard>(); |
| 213 | + Assert.Throws<NotSupportedException>(() => PressAndRelease(correctlyUsedKeyboard.spaceKey)); |
| 214 | + yield break; |
| 215 | + } |
| 216 | + |
| 217 | + [UnityTest] |
| 218 | + public IEnumerator Click_ShouldThrow_WithinEditModeTestFixtureContext() |
| 219 | + { |
| 220 | + correctlyUsedKeyboard = InputSystem.AddDevice<Keyboard>(); |
| 221 | + Assert.Throws<NotSupportedException>(() => Click(correctlyUsedKeyboard.spaceKey)); |
| 222 | + yield break; |
| 223 | + } |
| 224 | + |
| 225 | + #endregion // Edit-mode tests |
| 226 | +} |
0 commit comments