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

Commit 8463b471 authored by Prabir Pradhan's avatar Prabir Pradhan Committed by Android (Google) Code Review
Browse files

Merge "InputTests: Use a test rule to mock the IInputManager instance" into main

parents ff72c930 aeadbf20
Loading
Loading
Loading
Loading
+5 −15
Original line number Diff line number Diff line
@@ -24,17 +24,16 @@ import android.os.test.TestLooper
import android.platform.test.annotations.Presubmit
import androidx.test.core.app.ApplicationProvider
import com.android.server.testutils.any
import com.android.test.input.MockInputManagerRule
import java.util.concurrent.Executor
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
import kotlin.test.fail
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.Mockito.anyInt
import org.mockito.Mockito.doAnswer
@@ -61,9 +60,8 @@ class InputDeviceBatteryListenerTest {
    private lateinit var context: Context
    private lateinit var inputManager: InputManager

    @Mock
    private lateinit var iInputManagerMock: IInputManager
    private lateinit var inputManagerGlobalSession: InputManagerGlobal.TestSession
    @get:Rule
    val inputManagerRule = MockInputManagerRule()

    @Before
    fun setUp() {
@@ -72,7 +70,6 @@ class InputDeviceBatteryListenerTest {
        executor = HandlerExecutor(Handler(testLooper.looper))
        registeredListener = null
        monitoredDevices.clear()
        inputManagerGlobalSession = InputManagerGlobal.createTestSession(iInputManagerMock)
        inputManager = InputManager(context)
        `when`(context.getSystemService(Mockito.eq(Context.INPUT_SERVICE)))
                .thenReturn(inputManager)
@@ -92,7 +89,7 @@ class InputDeviceBatteryListenerTest {
            monitoredDevices.add(deviceId)
            registeredListener = listener
            null
        }.`when`(iInputManagerMock).registerBatteryListener(anyInt(), any())
        }.`when`(inputManagerRule.mock).registerBatteryListener(anyInt(), any())

        // Handle battery listener being unregistered.
        doAnswer {
@@ -108,14 +105,7 @@ class InputDeviceBatteryListenerTest {
            if (monitoredDevices.isEmpty()) {
                registeredListener = null
            }
        }.`when`(iInputManagerMock).unregisterBatteryListener(anyInt(), any())
    }

    @After
    fun tearDown() {
        if (this::inputManagerGlobalSession.isInitialized) {
            inputManagerGlobalSession.close()
        }
        }.`when`(inputManagerRule.mock).unregisterBatteryListener(anyInt(), any())
    }

    private fun notifyBatteryStateChanged(
+16 −18
Original line number Diff line number Diff line
@@ -45,15 +45,14 @@ import android.view.InputDevice;

import androidx.test.platform.app.InstrumentationRegistry;

import com.android.test.input.MockInputManagerRule;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.MockitoRule;

import java.util.ArrayList;
import java.util.Arrays;
@@ -73,23 +72,22 @@ public class InputDeviceLightsManagerTest {
    private static final int DEVICE_ID = 1000;
    private static final int PLAYER_ID = 3;

    @Rule public final MockitoRule mockito = MockitoJUnit.rule();
    @Rule
    public final MockInputManagerRule mInputManagerRule = new MockInputManagerRule();

    private InputManager mInputManager;

    @Mock private IInputManager mIInputManagerMock;
    private InputManagerGlobal.TestSession mInputManagerGlobalSession;

    @Before
    public void setUp() throws Exception {
        final Context context = spy(
                new ContextWrapper(InstrumentationRegistry.getInstrumentation().getContext()));
        when(mIInputManagerMock.getInputDeviceIds()).thenReturn(new int[]{DEVICE_ID});
        when(mInputManagerRule.getMock().getInputDeviceIds()).thenReturn(new int[]{DEVICE_ID});

        when(mIInputManagerMock.getInputDevice(eq(DEVICE_ID))).thenReturn(
        when(mInputManagerRule.getMock().getInputDevice(eq(DEVICE_ID))).thenReturn(
                createInputDevice(DEVICE_ID));

        mInputManagerGlobalSession = InputManagerGlobal.createTestSession(mIInputManagerMock);
        mInputManager = new InputManager(context);
        when(context.getSystemService(eq(Context.INPUT_SERVICE))).thenReturn(mInputManager);

@@ -102,7 +100,7 @@ public class InputDeviceLightsManagerTest {
                lightStatesById.put(lightIds[i], lightStates[i]);
            }
            return null;
        }).when(mIInputManagerMock).setLightStates(eq(DEVICE_ID),
        }).when(mInputManagerRule.getMock()).setLightStates(eq(DEVICE_ID),
                any(int[].class), any(LightState[].class), any(IBinder.class));

        doAnswer(invocation -> {
@@ -111,7 +109,7 @@ public class InputDeviceLightsManagerTest {
                return lightStatesById.get(lightId);
            }
            return new LightState(0);
        }).when(mIInputManagerMock).getLightState(eq(DEVICE_ID), anyInt());
        }).when(mInputManagerRule.getMock()).getLightState(eq(DEVICE_ID), anyInt());
    }

    @After
@@ -130,7 +128,7 @@ public class InputDeviceLightsManagerTest {

    private void mockLights(Light[] lights) throws Exception {
        // Mock the Lights returned form InputManagerService
        when(mIInputManagerMock.getLights(eq(DEVICE_ID))).thenReturn(
        when(mInputManagerRule.getMock().getLights(eq(DEVICE_ID))).thenReturn(
                new ArrayList(Arrays.asList(lights)));
    }

@@ -151,7 +149,7 @@ public class InputDeviceLightsManagerTest {

        LightsManager lightsManager = device.getLightsManager();
        List<Light> lights = lightsManager.getLights();
        verify(mIInputManagerMock).getLights(eq(DEVICE_ID));
        verify(mInputManagerRule.getMock()).getLights(eq(DEVICE_ID));
        assertEquals(lights, Arrays.asList(mockedLights));
    }

@@ -185,9 +183,9 @@ public class InputDeviceLightsManagerTest {
                .build());
        IBinder token = session.getToken();

        verify(mIInputManagerMock).openLightSession(eq(DEVICE_ID),
        verify(mInputManagerRule.getMock()).openLightSession(eq(DEVICE_ID),
                any(String.class), eq(token));
        verify(mIInputManagerMock).setLightStates(eq(DEVICE_ID), eq(new int[]{1, 2, 3}),
        verify(mInputManagerRule.getMock()).setLightStates(eq(DEVICE_ID), eq(new int[]{1, 2, 3}),
                eq(states), eq(token));

        // Then all 3 should turn on.
@@ -204,7 +202,7 @@ public class InputDeviceLightsManagerTest {

        // close session
        session.close();
        verify(mIInputManagerMock).closeLightSession(eq(DEVICE_ID), eq(token));
        verify(mInputManagerRule.getMock()).closeLightSession(eq(DEVICE_ID), eq(token));
    }

    @Test
@@ -232,9 +230,9 @@ public class InputDeviceLightsManagerTest {
                .build());
        IBinder token = session.getToken();

        verify(mIInputManagerMock).openLightSession(eq(DEVICE_ID),
        verify(mInputManagerRule.getMock()).openLightSession(eq(DEVICE_ID),
                any(String.class), eq(token));
        verify(mIInputManagerMock).setLightStates(eq(DEVICE_ID), eq(new int[]{1}),
        verify(mInputManagerRule.getMock()).setLightStates(eq(DEVICE_ID), eq(new int[]{1}),
                eq(states), eq(token));

        // Verify the light state
@@ -245,7 +243,7 @@ public class InputDeviceLightsManagerTest {

        // close session
        session.close();
        verify(mIInputManagerMock).closeLightSession(eq(DEVICE_ID), eq(token));
        verify(mInputManagerRule.getMock()).closeLightSession(eq(DEVICE_ID), eq(token));
    }

    @Test
+19 −31
Original line number Diff line number Diff line
@@ -41,16 +41,13 @@ import android.view.InputDevice;
import androidx.test.platform.app.InstrumentationRegistry;

import com.android.internal.annotations.GuardedBy;
import com.android.test.input.MockInputManagerRule;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.MockitoRule;

import java.util.List;
import java.util.concurrent.BlockingQueue;
@@ -70,43 +67,34 @@ public class InputDeviceSensorManagerTest {

    private static final int DEVICE_ID = 1000;

    @Rule public final MockitoRule mockito = MockitoJUnit.rule();
    @Rule
    public final MockInputManagerRule mInputManagerRule = new MockInputManagerRule();

    private InputManager mInputManager;
    private IInputSensorEventListener mIInputSensorEventListener;
    private final Object mLock = new Object();

    @Mock private IInputManager mIInputManagerMock;
    private InputManagerGlobal.TestSession mInputManagerGlobalSession;

    @Before
    public void setUp() throws Exception {
        final Context context = spy(
                new ContextWrapper(InstrumentationRegistry.getInstrumentation().getContext()));
        mInputManagerGlobalSession = InputManagerGlobal.createTestSession(mIInputManagerMock);
        mInputManager = new InputManager(context);
        when(context.getSystemService(eq(Context.INPUT_SERVICE))).thenReturn(mInputManager);

        when(mIInputManagerMock.getInputDeviceIds()).thenReturn(new int[]{DEVICE_ID});
        when(mInputManagerRule.getMock().getInputDeviceIds()).thenReturn(new int[]{DEVICE_ID});

        when(mIInputManagerMock.getInputDevice(eq(DEVICE_ID))).thenReturn(
        when(mInputManagerRule.getMock().getInputDevice(eq(DEVICE_ID))).thenReturn(
                createInputDeviceWithSensor(DEVICE_ID));

        when(mIInputManagerMock.getSensorList(eq(DEVICE_ID))).thenReturn(new InputSensorInfo[] {
        when(mInputManagerRule.getMock().getSensorList(eq(DEVICE_ID))).thenReturn(
                new InputSensorInfo[]{
                        createInputSensorInfo(DEVICE_ID, Sensor.TYPE_ACCELEROMETER),
                        createInputSensorInfo(DEVICE_ID, Sensor.TYPE_GYROSCOPE)});

        when(mIInputManagerMock.enableSensor(eq(DEVICE_ID), anyInt(), anyInt(), anyInt()))
        when(mInputManagerRule.getMock().enableSensor(eq(DEVICE_ID), anyInt(), anyInt(), anyInt()))
                .thenReturn(true);

        when(mIInputManagerMock.registerSensorListener(any())).thenReturn(true);
    }

    @After
    public void tearDown() {
        if (mInputManagerGlobalSession != null) {
            mInputManagerGlobalSession.close();
        }
        when(mInputManagerRule.getMock().registerSensorListener(any())).thenReturn(true);
    }

    private class InputTestSensorEventListener implements SensorEventListener {
@@ -175,13 +163,13 @@ public class InputDeviceSensorManagerTest {

        SensorManager sensorManager = device.getSensorManager();
        List<Sensor> accelList = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
        verify(mIInputManagerMock).getSensorList(eq(DEVICE_ID));
        verify(mInputManagerRule.getMock()).getSensorList(eq(DEVICE_ID));
        assertEquals(1, accelList.size());
        assertEquals(DEVICE_ID, accelList.get(0).getId());
        assertEquals(Sensor.TYPE_ACCELEROMETER, accelList.get(0).getType());

        List<Sensor> gyroList = sensorManager.getSensorList(Sensor.TYPE_GYROSCOPE);
        verify(mIInputManagerMock).getSensorList(eq(DEVICE_ID));
        verify(mInputManagerRule.getMock()).getSensorList(eq(DEVICE_ID));
        assertEquals(1, gyroList.size());
        assertEquals(DEVICE_ID, gyroList.get(0).getId());
        assertEquals(Sensor.TYPE_GYROSCOPE, gyroList.get(0).getType());
@@ -197,11 +185,11 @@ public class InputDeviceSensorManagerTest {

        List<Sensor> gameRotationList = sensorManager.getSensorList(
                Sensor.TYPE_GAME_ROTATION_VECTOR);
        verify(mIInputManagerMock).getSensorList(eq(DEVICE_ID));
        verify(mInputManagerRule.getMock()).getSensorList(eq(DEVICE_ID));
        assertEquals(0, gameRotationList.size());

        List<Sensor> gravityList = sensorManager.getSensorList(Sensor.TYPE_GRAVITY);
        verify(mIInputManagerMock).getSensorList(eq(DEVICE_ID));
        verify(mInputManagerRule.getMock()).getSensorList(eq(DEVICE_ID));
        assertEquals(0, gravityList.size());
    }

@@ -218,13 +206,13 @@ public class InputDeviceSensorManagerTest {
            mIInputSensorEventListener = invocation.getArgument(0);
            assertNotNull(mIInputSensorEventListener);
            return true;
        }).when(mIInputManagerMock).registerSensorListener(any());
        }).when(mInputManagerRule.getMock()).registerSensorListener(any());

        InputTestSensorEventListener listener = new InputTestSensorEventListener();
        assertTrue(sensorManager.registerListener(listener, sensor,
                SensorManager.SENSOR_DELAY_NORMAL));
        verify(mIInputManagerMock).registerSensorListener(any());
        verify(mIInputManagerMock).enableSensor(eq(DEVICE_ID), eq(sensor.getType()),
        verify(mInputManagerRule.getMock()).registerSensorListener(any());
        verify(mInputManagerRule.getMock()).enableSensor(eq(DEVICE_ID), eq(sensor.getType()),
                anyInt(), anyInt());

        float[] values = new float[] {0.12f, 9.8f, 0.2f};
@@ -240,7 +228,7 @@ public class InputDeviceSensorManagerTest {
        }

        sensorManager.unregisterListener(listener);
        verify(mIInputManagerMock).disableSensor(eq(DEVICE_ID), eq(sensor.getType()));
        verify(mInputManagerRule.getMock()).disableSensor(eq(DEVICE_ID), eq(sensor.getType()));
    }

}
+6 −20
Original line number Diff line number Diff line
@@ -23,18 +23,16 @@ import android.view.Display
import android.view.DisplayInfo
import android.view.InputDevice
import androidx.test.core.app.ApplicationProvider
import org.junit.After
import org.junit.Assert.assertNotNull
import com.android.test.input.MockInputManagerRule
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.Mockito.eq
import org.mockito.Mockito.`when`
import org.mockito.junit.MockitoJUnit
import org.mockito.junit.MockitoJUnitRunner

/**
@@ -54,35 +52,23 @@ class InputManagerTest {
    }

    @get:Rule
    val rule = MockitoJUnit.rule()!!
    val inputManagerRule = MockInputManagerRule()

    private lateinit var devicesChangedListener: IInputDevicesChangedListener
    private val deviceGenerationMap = mutableMapOf<Int /*deviceId*/, Int /*generation*/>()
    private lateinit var context: Context
    private lateinit var inputManager: InputManager

    @Mock
    private lateinit var iInputManager: IInputManager
    private lateinit var inputManagerGlobalSession: InputManagerGlobal.TestSession

    @Before
    fun setUp() {
        context = Mockito.spy(ContextWrapper(ApplicationProvider.getApplicationContext()))
        inputManagerGlobalSession = InputManagerGlobal.createTestSession(iInputManager)
        inputManager = InputManager(context)
        `when`(context.getSystemService(eq(Context.INPUT_SERVICE))).thenReturn(inputManager)
        `when`(iInputManager.inputDeviceIds).then {
        `when`(inputManagerRule.mock.inputDeviceIds).then {
            deviceGenerationMap.keys.toIntArray()
        }
    }

    @After
    fun tearDown() {
        if (this::inputManagerGlobalSession.isInitialized) {
            inputManagerGlobalSession.close()
        }
    }

    private fun notifyDeviceChanged(
        deviceId: Int,
        associatedDisplayId: Int,
@@ -92,7 +78,7 @@ class InputManagerTest {
            ?: throw IllegalArgumentException("Device $deviceId was never added!")
        deviceGenerationMap[deviceId] = generation

        `when`(iInputManager.getInputDevice(deviceId))
        `when`(inputManagerRule.mock.getInputDevice(deviceId))
            .thenReturn(createInputDevice(deviceId, associatedDisplayId, usiVersion, generation))
        val list = deviceGenerationMap.flatMap { listOf(it.key, it.value) }
        if (::devicesChangedListener.isInitialized) {
@@ -125,7 +111,7 @@ class InputManagerTest {
    fun testUsiVersionFallBackToDisplayConfig() {
        addInputDevice(DEVICE_ID, Display.DEFAULT_DISPLAY, null)

        `when`(iInputManager.getHostUsiVersionFromDisplayConfig(eq(42)))
        `when`(inputManagerRule.mock.getHostUsiVersionFromDisplayConfig(eq(42)))
            .thenReturn(HostUsiVersion(9, 8))
        val usiVersion = inputManager.getHostUsiVersion(createDisplay(42))
        assertEquals(HostUsiVersion(9, 8), usiVersion)
+5 −19
Original line number Diff line number Diff line
@@ -18,20 +18,17 @@ package android.hardware.input

import android.content.Context
import android.content.ContextWrapper
import android.os.Handler
import android.os.IBinder
import android.os.test.TestLooper
import android.platform.test.annotations.Presubmit
import android.platform.test.flag.junit.SetFlagsRule
import android.view.KeyEvent
import androidx.test.core.app.ApplicationProvider
import com.android.server.testutils.any
import org.junit.After
import com.android.test.input.MockInputManagerRule
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.Mockito.doAnswer
import org.mockito.Mockito.`when`
@@ -69,20 +66,16 @@ class KeyGestureEventHandlerTest {

    @get:Rule
    val rule = SetFlagsRule()
    @get:Rule
    val inputManagerRule = MockInputManagerRule()

    private val testLooper = TestLooper()
    private var registeredListener: IKeyGestureHandler? = null
    private lateinit var context: Context
    private lateinit var inputManager: InputManager
    private lateinit var inputManagerGlobalSession: InputManagerGlobal.TestSession

    @Mock
    private lateinit var iInputManagerMock: IInputManager

    @Before
    fun setUp() {
        context = Mockito.spy(ContextWrapper(ApplicationProvider.getApplicationContext()))
        inputManagerGlobalSession = InputManagerGlobal.createTestSession(iInputManagerMock)
        inputManager = InputManager(context)
        `when`(context.getSystemService(Mockito.eq(Context.INPUT_SERVICE)))
                .thenReturn(inputManager)
@@ -97,7 +90,7 @@ class KeyGestureEventHandlerTest {
            }
            registeredListener = listener
            null
        }.`when`(iInputManagerMock).registerKeyGestureHandler(any())
        }.`when`(inputManagerRule.mock).registerKeyGestureHandler(any())

        // Handle key gesture handler being unregistered.
        doAnswer {
@@ -108,14 +101,7 @@ class KeyGestureEventHandlerTest {
            }
            registeredListener = null
            null
        }.`when`(iInputManagerMock).unregisterKeyGestureHandler(any())
    }

    @After
    fun tearDown() {
        if (this::inputManagerGlobalSession.isInitialized) {
            inputManagerGlobalSession.close()
        }
        }.`when`(inputManagerRule.mock).unregisterKeyGestureHandler(any())
    }

    private fun handleKeyGestureEvent(event: KeyGestureEvent) {
Loading