Merge "Add Builder to InputDevice for testing"

This commit is contained in:
TreeHugger Robot
2022-09-12 20:19:58 +00:00
committed by Android (Google) Code Review
8 changed files with 201 additions and 56 deletions

View File

@@ -460,10 +460,8 @@ public final class InputDevice implements Parcelable {
/**
* Called by native code
* @hide
*/
@VisibleForTesting
public InputDevice(int id, int generation, int controllerNumber, String name, int vendorId,
private InputDevice(int id, int generation, int controllerNumber, String name, int vendorId,
int productId, String descriptor, boolean isExternal, int sources, int keyboardType,
KeyCharacterMap keyCharacterMap, @InputDeviceCountryCode int countryCode,
boolean hasVibrator, boolean hasMicrophone, boolean hasButtonUnderPad,
@@ -519,6 +517,142 @@ public final class InputDevice implements Parcelable {
}
}
/**
* InputDevice builder used to create an InputDevice for tests in Java.
* @hide
*/
@VisibleForTesting
public static class Builder {
private int mId = 0;
private int mGeneration = 0;
private int mControllerNumber = 0;
private String mName = "";
private int mVendorId = 0;
private int mProductId = 0;
private String mDescriptor = "";
private boolean mIsExternal = false;
private int mSources = 0;
private int mKeyboardType = 0;
private KeyCharacterMap mKeyCharacterMap = null;
private boolean mHasVibrator = false;
private boolean mHasMicrophone = false;
private boolean mHasButtonUnderPad = false;
private boolean mHasSensor = false;
private boolean mHasBattery = false;
@InputDeviceCountryCode
private int mCountryCode = InputDeviceCountryCode.INVALID;
/** @see InputDevice#getId() */
public Builder setId(int id) {
mId = id;
return this;
}
/** @see InputDevice#getGeneration() */
public Builder setGeneration(int generation) {
mGeneration = generation;
return this;
}
/** @see InputDevice#getControllerNumber() */
public Builder setControllerNumber(int controllerNumber) {
mControllerNumber = controllerNumber;
return this;
}
/** @see InputDevice#getName() */
public Builder setName(String name) {
mName = name;
return this;
}
/** @see InputDevice#getVendorId() */
public Builder setVendorId(int vendorId) {
mVendorId = vendorId;
return this;
}
/** @see InputDevice#getProductId() */
public Builder setProductId(int productId) {
mProductId = productId;
return this;
}
/** @see InputDevice#getDescriptor() */
public Builder setDescriptor(String descriptor) {
mDescriptor = descriptor;
return this;
}
/** @see InputDevice#isExternal() */
public Builder setExternal(boolean external) {
mIsExternal = external;
return this;
}
/** @see InputDevice#getSources() */
public Builder setSources(int sources) {
mSources = sources;
return this;
}
/** @see InputDevice#getKeyboardType() */
public Builder setKeyboardType(int keyboardType) {
mKeyboardType = keyboardType;
return this;
}
/** @see InputDevice#getKeyCharacterMap() */
public Builder setKeyCharacterMap(KeyCharacterMap keyCharacterMap) {
mKeyCharacterMap = keyCharacterMap;
return this;
}
/** @see InputDevice#getVibrator() */
public Builder setHasVibrator(boolean hasVibrator) {
mHasVibrator = hasVibrator;
return this;
}
/** @see InputDevice#hasMicrophone() */
public Builder setHasMicrophone(boolean hasMicrophone) {
mHasMicrophone = hasMicrophone;
return this;
}
/** @see InputDevice#hasButtonUnderPad() */
public Builder setHasButtonUnderPad(boolean hasButtonUnderPad) {
mHasButtonUnderPad = hasButtonUnderPad;
return this;
}
/** @see InputDevice#hasSensor() */
public Builder setHasSensor(boolean hasSensor) {
mHasSensor = hasSensor;
return this;
}
/** @see InputDevice#hasBattery() */
public Builder setHasBattery(boolean hasBattery) {
mHasBattery = hasBattery;
return this;
}
/** @see InputDevice#getCountryCode() */
public Builder setCountryCode(@InputDeviceCountryCode int countryCode) {
mCountryCode = countryCode;
return this;
}
/** Build {@link InputDevice}. */
public InputDevice build() {
return new InputDevice(mId, mGeneration, mControllerNumber, mName, mVendorId,
mProductId, mDescriptor, mIsExternal, mSources, mKeyboardType, mKeyCharacterMap,
mCountryCode, mHasVibrator, mHasMicrophone, mHasButtonUnderPad, mHasSensor,
mHasBattery);
}
}
/**
* Gets information about the input device with the specified id.
* @param id The device id.

View File

@@ -110,11 +110,10 @@ public class InputDeviceLightsManagerTest {
}
private InputDevice createInputDevice(int id) {
return new InputDevice(id, 0 /* generation */, 0 /* controllerNumber */, "name",
0 /* vendorId */, 0 /* productId */, "descriptor", true /* isExternal */,
0 /* sources */, 0 /* keyboardType */, null /* keyCharacterMap */,
InputDeviceCountryCode.INVALID, false /* hasVibrator */, false /* hasMicrophone */,
false /* hasButtonUnderpad */, false /* hasSensor */, false /* hasBattery */);
return new InputDevice.Builder()
.setId(id)
.setName("Test Device " + id)
.build();
}
private void mockLights(Light[] lights) throws Exception {

View File

@@ -35,7 +35,6 @@ import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.test.TestLooper;
import android.platform.test.annotations.Presubmit;
import android.view.InputDevice;
@@ -73,10 +72,7 @@ public class InputDeviceSensorManagerTest {
@Rule public final MockitoRule mockito = MockitoJUnit.rule();
private TestLooper mTestLooper;
private ContextWrapper mContextSpy;
private InputManager mInputManager;
private InputDeviceSensorManager mSensorManager;
private IInputSensorEventListener mIInputSensorEventListener;
private final Object mLock = new Object();
@@ -84,11 +80,10 @@ public class InputDeviceSensorManagerTest {
@Before
public void setUp() throws Exception {
mTestLooper = new TestLooper();
mContextSpy = spy(new ContextWrapper(InstrumentationRegistry.getContext()));
final Context context = spy(new ContextWrapper(InstrumentationRegistry.getContext()));
InputManager inputManager = InputManager.resetInstance(mIInputManagerMock);
when(mContextSpy.getSystemService(eq(Context.INPUT_SERVICE))).thenReturn(inputManager);
when(context.getSystemService(eq(Context.INPUT_SERVICE))).thenReturn(inputManager);
when(mIInputManagerMock.getInputDeviceIds()).thenReturn(new int[]{DEVICE_ID});
@@ -104,7 +99,7 @@ public class InputDeviceSensorManagerTest {
when(mIInputManagerMock.registerSensorListener(any())).thenReturn(true);
mInputManager = mContextSpy.getSystemService(InputManager.class);
mInputManager = context.getSystemService(InputManager.class);
}
@After
@@ -145,13 +140,11 @@ public class InputDeviceSensorManagerTest {
}
private InputDevice createInputDeviceWithSensor(int id) {
InputDevice d = new InputDevice(id, 0 /* generation */, 0 /* controllerNumber */, "name",
0 /* vendorId */, 0 /* productId */, "descriptor", true /* isExternal */,
0 /* sources */, 0 /* keyboardType */, null /* keyCharacterMap */,
InputDeviceCountryCode.INVALID, false /* hasVibrator */, false /* hasMicrophone */,
false /* hasButtonUnderpad */, true /* hasSensor */, false /* hasBattery */);
assertTrue(d.hasSensor());
return d;
return new InputDevice.Builder()
.setId(id)
.setName("Test Device " + id)
.setHasSensor(true)
.build();
}
private InputSensorInfo createInputSensorInfo(int id, int type) {
@@ -164,8 +157,8 @@ public class InputDeviceSensorManagerTest {
}
private InputDevice getSensorDevice(int[] deviceIds) {
for (int i = 0; i < deviceIds.length; i++) {
InputDevice device = mInputManager.getInputDevice(deviceIds[i]);
for (int deviceId : deviceIds) {
InputDevice device = mInputManager.getInputDevice(deviceId);
if (device.hasSensor()) {
return device;
}
@@ -176,7 +169,7 @@ public class InputDeviceSensorManagerTest {
@Test
public void getInputDeviceSensors_withExpectedType() throws Exception {
InputDevice device = getSensorDevice(mInputManager.getInputDeviceIds());
assertTrue(device != null);
assertNotNull(device);
SensorManager sensorManager = device.getSensorManager();
List<Sensor> accelList = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
@@ -197,7 +190,7 @@ public class InputDeviceSensorManagerTest {
public void getInputDeviceSensors_withUnexpectedType() throws Exception {
InputDevice device = getSensorDevice(mInputManager.getInputDeviceIds());
assertTrue(device != null);
assertNotNull(device);
SensorManager sensorManager = device.getSensorManager();
List<Sensor> gameRotationList = sensorManager.getSensorList(
@@ -213,7 +206,7 @@ public class InputDeviceSensorManagerTest {
@Test
public void testInputDeviceSensorListener() throws Exception {
InputDevice device = getSensorDevice(mInputManager.getInputDeviceIds());
assertTrue(device != null);
assertNotNull(device);
SensorManager sensorManager = device.getSensorManager();
Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

View File

@@ -25,7 +25,6 @@ import static org.mockito.Mockito.when;
import android.hardware.input.IInputDevicesChangedListener;
import android.hardware.input.IInputManager;
import android.hardware.input.InputDeviceCountryCode;
import android.hardware.input.InputManager;
import android.os.RemoteException;
import android.testing.TestableLooper;
@@ -81,11 +80,16 @@ class InputManagerMockHelper {
private Void handleNativeOpenInputDevice(InvocationOnMock inv) {
Objects.requireNonNull(mDevicesChangedListener,
"InputController did not register an InputDevicesChangedListener.");
// We only use a subset of the fields of InputDevice in InputController.
final InputDevice device = new InputDevice(mDevices.size() /*id*/, 1 /*generation*/, 0,
inv.getArgument(0) /*name*/, inv.getArgument(1) /*vendorId*/,
inv.getArgument(2) /*productId*/, inv.getArgument(3) /*descriptor*/, true, 0, 0,
null, InputDeviceCountryCode.INVALID, false, false, false, false, false);
final InputDevice device = new InputDevice.Builder()
.setId(mDevices.size())
.setName(inv.getArgument(0))
.setVendorId(inv.getArgument(1))
.setProductId(inv.getArgument(2))
.setDescriptor(inv.getArgument(3))
.setExternal(true)
.build();
mDevices.add(device);
try {
mDevicesChangedListener.onInputDevicesChanged(

View File

@@ -87,11 +87,13 @@ class BatteryControllerTests {
}
private fun createInputDevice(deviceId: Int): InputDevice =
InputDevice(deviceId, 0 /*generation*/, 0 /*controllerNumber*/,
"Device $deviceId" /*name*/, 0 /*vendorId*/, 0 /*productId*/, "descriptor$deviceId",
true /*isExternal*/, 0 /*sources*/, 0 /*keyboardType*/, null /*keyCharacterMap*/,
InputDeviceCountryCode.INVALID, false /*hasVibrator*/, false /*hasMicrophone*/,
false /*hasButtonUnderPad*/, false /*hasSensor*/, true /*hasBattery*/)
InputDevice.Builder()
.setId(deviceId)
.setName("Device $deviceId")
.setDescriptor("descriptor $deviceId")
.setExternal(true)
.setHasBattery(true)
.build()
@After
fun tearDown() {

View File

@@ -33,7 +33,6 @@ import android.content.Context;
import android.content.ContextWrapper;
import android.hardware.input.IInputDevicesChangedListener;
import android.hardware.input.IInputManager;
import android.hardware.input.InputDeviceCountryCode;
import android.hardware.input.InputManager;
import android.os.CombinedVibration;
import android.os.Handler;
@@ -328,10 +327,11 @@ public class InputDeviceDelegateTest {
}
private InputDevice createInputDevice(int id, boolean hasVibrator) {
return new InputDevice(id, 0, 0, "name", 0, 0, "description", false, 0, 0,
null, InputDeviceCountryCode.INVALID, hasVibrator, false, false,
false /* hasSensor */, false /* hasBattery */);
return new InputDevice.Builder()
.setId(id)
.setName("name")
.setDescriptor("descriptor")
.setHasVibrator(hasVibrator)
.build();
}
}

View File

@@ -46,7 +46,6 @@ import android.content.Context;
import android.content.ContextWrapper;
import android.content.pm.PackageManagerInternal;
import android.hardware.input.IInputManager;
import android.hardware.input.InputDeviceCountryCode;
import android.hardware.input.InputManager;
import android.hardware.vibrator.IVibrator;
import android.hardware.vibrator.IVibratorManager;
@@ -1980,9 +1979,11 @@ public class VibratorManagerServiceTest {
}
private InputDevice createInputDeviceWithVibrator(int id) {
return new InputDevice(id, 0, 0, "name", 0, 0, "description", false, 0, 0,
null, InputDeviceCountryCode.INVALID, /* hasVibrator= */ true, false, false, false,
false);
return new InputDevice.Builder()
.setId(id)
.setName("Test Device " + id)
.setHasVibrator(true)
.build();
}
private static <T> void addLocalServiceMock(Class<T> clazz, T mock) {

View File

@@ -69,13 +69,25 @@ public class InputDeviceTest {
}
private void assertInputDeviceParcelUnparcel(KeyCharacterMap keyCharacterMap) {
final InputDevice device =
new InputDevice(DEVICE_ID, 0 /* generation */, 0 /* controllerNumber */, "name",
0 /* vendorId */, 0 /* productId */, "descriptor", true /* isExternal */,
0 /* sources */, 0 /* keyboardType */, keyCharacterMap,
InputDeviceCountryCode.INTERNATIONAL, false /* hasVibrator */,
false /* hasMicrophone */, false /* hasButtonUnderpad */,
true /* hasSensor */, false /* hasBattery */);
final InputDevice device = new InputDevice.Builder()
.setId(DEVICE_ID)
.setGeneration(42)
.setControllerNumber(43)
.setName("Test Device " + DEVICE_ID)
.setVendorId(44)
.setProductId(45)
.setDescriptor("descriptor")
.setExternal(true)
.setSources(InputDevice.SOURCE_HDMI)
.setKeyboardType(InputDevice.KEYBOARD_TYPE_NON_ALPHABETIC)
.setKeyCharacterMap(keyCharacterMap)
.setHasVibrator(true)
.setHasMicrophone(true)
.setHasButtonUnderPad(true)
.setHasSensor(true)
.setHasBattery(true)
.setCountryCode(InputDeviceCountryCode.INTERNATIONAL)
.build();
Parcel parcel = Parcel.obtain();
device.writeToParcel(parcel, 0);