Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a63c368b authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add Builder to InputDevice for testing"

parents 778986ba 4310f5d1
Loading
Loading
Loading
Loading
+137 −3
Original line number Diff line number Diff line
@@ -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.
+4 −5
Original line number Diff line number Diff line
@@ -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 {
+13 −20
Original line number Diff line number Diff line
@@ -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);
+10 −6
Original line number Diff line number Diff line
@@ -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(
+7 −5
Original line number Diff line number Diff line
@@ -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() {
Loading