2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2020-07-15 00:33:57 -07:00
|
|
|
|
2025-11-07 19:55:43 -05:00
|
|
|
package org.wpilib.driverstation;
|
2020-07-15 00:33:57 -07:00
|
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
|
|
2025-11-07 19:55:43 -05:00
|
|
|
import org.wpilib.hardware.hal.HAL;
|
|
|
|
|
import org.wpilib.simulation.XboxControllerSim;
|
2021-08-12 02:04:14 -04:00
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
import org.junit.jupiter.params.ParameterizedTest;
|
2021-08-14 20:00:46 +03:00
|
|
|
import org.junit.jupiter.params.provider.EnumSource;
|
2020-12-29 22:45:16 -08:00
|
|
|
|
2020-07-15 00:33:57 -07:00
|
|
|
class XboxControllerTest {
|
2021-08-12 02:04:14 -04:00
|
|
|
@ParameterizedTest
|
2021-08-14 20:00:46 +03:00
|
|
|
@EnumSource(value = XboxController.Button.class)
|
2021-12-09 12:20:08 -08:00
|
|
|
void testButtons(XboxController.Button button)
|
2021-08-12 02:04:14 -04:00
|
|
|
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
2020-07-15 00:33:57 -07:00
|
|
|
HAL.initialize(500, 0);
|
|
|
|
|
XboxController joy = new XboxController(2);
|
|
|
|
|
XboxControllerSim joysim = new XboxControllerSim(joy);
|
|
|
|
|
|
2021-08-14 20:00:46 +03:00
|
|
|
var buttonName = button.toString();
|
|
|
|
|
|
|
|
|
|
String simSetMethodName = "set" + buttonName;
|
|
|
|
|
String joyGetMethodName = "get" + buttonName;
|
|
|
|
|
String joyPressedMethodName = "get" + buttonName + "Pressed";
|
|
|
|
|
String joyReleasedMethodName = "get" + buttonName + "Released";
|
2021-08-12 02:04:14 -04:00
|
|
|
|
2022-09-24 00:13:55 -07:00
|
|
|
final Method simSetMethod = joysim.getClass().getMethod(simSetMethodName, boolean.class);
|
|
|
|
|
final Method joyGetMethod = joy.getClass().getMethod(joyGetMethodName);
|
|
|
|
|
final Method joyPressedMethod = joy.getClass().getMethod(joyPressedMethodName);
|
|
|
|
|
final Method joyReleasedMethod = joy.getClass().getMethod(joyReleasedMethodName);
|
2021-08-12 02:04:14 -04:00
|
|
|
|
|
|
|
|
simSetMethod.invoke(joysim, false);
|
2020-07-15 00:33:57 -07:00
|
|
|
joysim.notifyNewData();
|
2021-08-12 02:04:14 -04:00
|
|
|
assertFalse((Boolean) joyGetMethod.invoke(joy));
|
2020-07-15 00:33:57 -07:00
|
|
|
// need to call pressed and released to clear flags
|
2021-08-12 02:04:14 -04:00
|
|
|
joyPressedMethod.invoke(joy);
|
|
|
|
|
joyReleasedMethod.invoke(joy);
|
2020-07-15 00:33:57 -07:00
|
|
|
|
2021-08-12 02:04:14 -04:00
|
|
|
simSetMethod.invoke(joysim, true);
|
2020-07-15 00:33:57 -07:00
|
|
|
joysim.notifyNewData();
|
2021-08-12 02:04:14 -04:00
|
|
|
assertTrue((Boolean) joyGetMethod.invoke(joy));
|
|
|
|
|
assertTrue((Boolean) joyPressedMethod.invoke(joy));
|
|
|
|
|
assertFalse((Boolean) joyReleasedMethod.invoke(joy));
|
|
|
|
|
|
|
|
|
|
simSetMethod.invoke(joysim, false);
|
|
|
|
|
joysim.notifyNewData();
|
|
|
|
|
assertFalse((Boolean) joyGetMethod.invoke(joy));
|
|
|
|
|
assertFalse((Boolean) joyPressedMethod.invoke(joy));
|
|
|
|
|
assertTrue((Boolean) joyReleasedMethod.invoke(joy));
|
2020-07-15 00:33:57 -07:00
|
|
|
}
|
|
|
|
|
|
2021-08-12 02:04:14 -04:00
|
|
|
@ParameterizedTest
|
2021-08-14 20:00:46 +03:00
|
|
|
@EnumSource(value = XboxController.Axis.class)
|
2021-12-09 12:20:08 -08:00
|
|
|
void testAxes(XboxController.Axis axis)
|
2021-08-12 02:04:14 -04:00
|
|
|
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
2020-07-15 00:33:57 -07:00
|
|
|
HAL.initialize(500, 0);
|
|
|
|
|
XboxController joy = new XboxController(2);
|
|
|
|
|
XboxControllerSim joysim = new XboxControllerSim(joy);
|
|
|
|
|
|
2021-08-14 20:00:46 +03:00
|
|
|
var axisName = axis.toString();
|
2021-08-12 02:04:14 -04:00
|
|
|
|
2021-08-14 20:00:46 +03:00
|
|
|
String simSetMethodName = "set" + axisName;
|
|
|
|
|
String joyGetMethodName = "get" + axisName;
|
2021-08-12 02:04:14 -04:00
|
|
|
|
2021-08-14 20:00:46 +03:00
|
|
|
Method simSetMethod = joysim.getClass().getMethod(simSetMethodName, double.class);
|
|
|
|
|
Method joyGetMethod = joy.getClass().getMethod(joyGetMethodName);
|
2020-07-15 00:33:57 -07:00
|
|
|
|
2021-08-14 20:00:46 +03:00
|
|
|
simSetMethod.invoke(joysim, 0.35);
|
2020-07-15 00:33:57 -07:00
|
|
|
joysim.notifyNewData();
|
2021-08-14 20:00:46 +03:00
|
|
|
assertEquals(0.35, (Double) joyGetMethod.invoke(joy), 0.001);
|
2020-07-15 00:33:57 -07:00
|
|
|
}
|
|
|
|
|
}
|