|
| 1 | +package com.jme3.input; |
| 2 | + |
| 3 | +import com.jme3.input.controls.JoyAxisTrigger; |
| 4 | +import com.jme3.input.controls.JoyButtonTrigger; |
| 5 | +import com.jme3.input.controls.Trigger; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | +import org.mockito.ArgumentCaptor; |
| 8 | + |
| 9 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 10 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 11 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 12 | +import static org.junit.jupiter.api.Assertions.assertSame; |
| 13 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 15 | +import static org.mockito.ArgumentMatchers.eq; |
| 16 | +import static org.mockito.Mockito.mock; |
| 17 | +import static org.mockito.Mockito.verify; |
| 18 | + |
| 19 | +class DefaultJoystickTest { |
| 20 | + |
| 21 | + @Test |
| 22 | + void storesAxesAndButtonsAsReadOnlyLogicalCollections() { |
| 23 | + TestJoystick joystick = new TestJoystick(mock(InputManager.class), mock(JoyInput.class), 3, "Gamepad"); |
| 24 | + DefaultJoystickAxis xAxis = new DefaultJoystickAxis(null, joystick, 0, "X Axis", |
| 25 | + JoystickAxis.X_AXIS, true, false, 0.1f); |
| 26 | + DefaultJoystickButton fire = new DefaultJoystickButton(null, joystick, 1, "Fire", |
| 27 | + JoystickButton.BUTTON_1); |
| 28 | + |
| 29 | + joystick.addTestAxis(xAxis); |
| 30 | + joystick.addTestButton(fire); |
| 31 | + |
| 32 | + assertSame(xAxis, joystick.getAxis(JoystickAxis.X_AXIS)); |
| 33 | + assertNull(joystick.getAxis(JoystickAxis.Y_AXIS)); |
| 34 | + assertSame(fire, joystick.getButton(JoystickButton.BUTTON_1)); |
| 35 | + assertNull(joystick.getButton(JoystickButton.BUTTON_2)); |
| 36 | + assertEquals(1, joystick.getAxisCount()); |
| 37 | + assertEquals(1, joystick.getButtonCount()); |
| 38 | + assertThrows(UnsupportedOperationException.class, () -> joystick.getAxes().clear()); |
| 39 | + assertThrows(UnsupportedOperationException.class, () -> joystick.getButtons().clear()); |
| 40 | + assertEquals("Joystick[name=Gamepad, id=3, buttons=1, axes=1]", joystick.toString()); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void rumbleDelegatesToJoyInput() { |
| 45 | + JoyInput joyInput = mock(JoyInput.class); |
| 46 | + TestJoystick joystick = new TestJoystick(mock(InputManager.class), joyInput, 7, "Wheel"); |
| 47 | + |
| 48 | + joystick.rumble(0.75f); |
| 49 | + |
| 50 | + verify(joyInput).setJoyRumble(7, 0.75f); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void defaultAxisExposesStateAndAssignsPositiveAndNegativeTriggers() { |
| 55 | + InputManager inputManager = mock(InputManager.class); |
| 56 | + TestJoystick joystick = new TestJoystick(inputManager, mock(JoyInput.class), 2, "Arcade Stick"); |
| 57 | + DefaultJoystickAxis axis = new DefaultJoystickAxis(inputManager, joystick, 4, "Throttle", |
| 58 | + "throttle", true, false, 0.2f); |
| 59 | + |
| 60 | + axis.setDeadZone(0.35f); |
| 61 | + axis.assignAxis("Throttle+", "Throttle-"); |
| 62 | + |
| 63 | + assertSame(joystick, axis.getJoystick()); |
| 64 | + assertEquals("Throttle", axis.getName()); |
| 65 | + assertEquals("throttle", axis.getLogicalId()); |
| 66 | + assertEquals(4, axis.getAxisId()); |
| 67 | + assertTrue(axis.isAnalog()); |
| 68 | + assertFalse(axis.isRelative()); |
| 69 | + assertEquals(0.35f, axis.getDeadZone()); |
| 70 | + assertEquals(0f, axis.getJitterThreshold()); |
| 71 | + assertEquals("JoystickAxis[name=Throttle, parent=Arcade Stick, id=4, logicalId=throttle, " |
| 72 | + + "isAnalog=true, isRelative=false, deadZone=0.35, jitterThreshold=0.0]", axis.toString()); |
| 73 | + |
| 74 | + ArgumentCaptor<Trigger[]> positive = ArgumentCaptor.forClass(Trigger[].class); |
| 75 | + ArgumentCaptor<Trigger[]> negative = ArgumentCaptor.forClass(Trigger[].class); |
| 76 | + verify(inputManager).addMapping(eq("Throttle+"), positive.capture()); |
| 77 | + verify(inputManager).addMapping(eq("Throttle-"), negative.capture()); |
| 78 | + JoyAxisTrigger positiveTrigger = (JoyAxisTrigger) positive.getValue()[0]; |
| 79 | + JoyAxisTrigger negativeTrigger = (JoyAxisTrigger) negative.getValue()[0]; |
| 80 | + assertEquals(2, positiveTrigger.getJoyId()); |
| 81 | + assertEquals(4, positiveTrigger.getAxisId()); |
| 82 | + assertFalse(positiveTrigger.isNegative()); |
| 83 | + assertTrue(negativeTrigger.isNegative()); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void defaultAxisWithUnknownIndexDoesNotAssignMappings() { |
| 88 | + InputManager inputManager = mock(InputManager.class); |
| 89 | + TestJoystick joystick = new TestJoystick(inputManager, mock(JoyInput.class), 2, "Unknown"); |
| 90 | + DefaultJoystickAxis axis = new DefaultJoystickAxis(inputManager, joystick, -1, "Unknown", |
| 91 | + "unknown", false, true, 0f); |
| 92 | + |
| 93 | + axis.assignAxis("Positive", "Negative"); |
| 94 | + |
| 95 | + org.mockito.Mockito.verifyNoInteractions(inputManager); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void defaultButtonExposesStateAndAssignsTrigger() { |
| 100 | + InputManager inputManager = mock(InputManager.class); |
| 101 | + TestJoystick joystick = new TestJoystick(inputManager, mock(JoyInput.class), 5, "Pad"); |
| 102 | + DefaultJoystickButton button = new DefaultJoystickButton(inputManager, joystick, 6, "Start", |
| 103 | + JoystickButton.BUTTON_XBOX_START); |
| 104 | + |
| 105 | + button.assignButton("Pause"); |
| 106 | + |
| 107 | + assertSame(joystick, button.getJoystick()); |
| 108 | + assertEquals("Start", button.getName()); |
| 109 | + assertEquals(JoystickButton.BUTTON_XBOX_START, button.getLogicalId()); |
| 110 | + assertEquals(6, button.getButtonId()); |
| 111 | + assertEquals("JoystickButton[name=Start, parent=Pad, id=6, logicalId=9]", button.toString()); |
| 112 | + |
| 113 | + ArgumentCaptor<Trigger[]> triggers = ArgumentCaptor.forClass(Trigger[].class); |
| 114 | + verify(inputManager).addMapping(eq("Pause"), triggers.capture()); |
| 115 | + JoyButtonTrigger trigger = (JoyButtonTrigger) triggers.getValue()[0]; |
| 116 | + assertEquals(5, trigger.getJoyId()); |
| 117 | + assertEquals(6, trigger.getAxisId()); |
| 118 | + } |
| 119 | + |
| 120 | + private static final class TestJoystick extends AbstractJoystick { |
| 121 | + private TestJoystick(InputManager inputManager, JoyInput joyInput, int joyId, String name) { |
| 122 | + super(inputManager, joyInput, joyId, name); |
| 123 | + } |
| 124 | + |
| 125 | + void addTestAxis(JoystickAxis axis) { |
| 126 | + addAxis(axis); |
| 127 | + } |
| 128 | + |
| 129 | + void addTestButton(JoystickButton button) { |
| 130 | + addButton(button); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public JoystickAxis getXAxis() { |
| 135 | + return getAxis(JoystickAxis.X_AXIS); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public JoystickAxis getYAxis() { |
| 140 | + return getAxis(JoystickAxis.Y_AXIS); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public JoystickAxis getPovXAxis() { |
| 145 | + return getAxis(JoystickAxis.POV_X); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public JoystickAxis getPovYAxis() { |
| 150 | + return getAxis(JoystickAxis.POV_Y); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments