summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tests/Input/src/android/hardware/input/InputDeviceBatteryListenerTest.kt20
-rw-r--r--tests/Input/src/android/hardware/input/InputDeviceLightsManagerTest.java34
-rw-r--r--tests/Input/src/android/hardware/input/InputDeviceSensorManagerTest.java50
-rw-r--r--tests/Input/src/android/hardware/input/InputManagerTest.kt26
-rw-r--r--tests/Input/src/android/hardware/input/KeyGestureEventHandlerTest.kt24
-rw-r--r--tests/Input/src/android/hardware/input/KeyGestureEventListenerTest.kt29
-rw-r--r--tests/Input/src/android/hardware/input/KeyboardBacklightListenerTest.kt33
-rw-r--r--tests/Input/src/android/hardware/input/StickyModifierStateListenerTest.kt31
-rw-r--r--tests/Input/src/com/android/server/input/BatteryControllerTests.kt43
-rw-r--r--tests/Input/src/com/android/server/input/InputManagerServiceTests.kt6
-rw-r--r--tests/Input/src/com/android/server/input/KeyRemapperTests.kt32
-rw-r--r--tests/Input/src/com/android/server/input/KeyboardBacklightControllerTests.kt118
-rw-r--r--tests/Input/src/com/android/server/input/KeyboardGlyphManagerTests.kt29
-rw-r--r--tests/Input/src/com/android/server/input/KeyboardLayoutManagerTests.kt35
-rw-r--r--tests/Input/src/com/android/test/input/MockInputManagerRule.kt42
15 files changed, 235 insertions, 317 deletions
diff --git a/tests/Input/src/android/hardware/input/InputDeviceBatteryListenerTest.kt b/tests/Input/src/android/hardware/input/InputDeviceBatteryListenerTest.kt
index 90dff47ab706..a1e165551b5b 100644
--- a/tests/Input/src/android/hardware/input/InputDeviceBatteryListenerTest.kt
+++ b/tests/Input/src/android/hardware/input/InputDeviceBatteryListenerTest.kt
@@ -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(
diff --git a/tests/Input/src/android/hardware/input/InputDeviceLightsManagerTest.java b/tests/Input/src/android/hardware/input/InputDeviceLightsManagerTest.java
index 080186e4a2c1..3fc9ce12e718 100644
--- a/tests/Input/src/android/hardware/input/InputDeviceLightsManagerTest.java
+++ b/tests/Input/src/android/hardware/input/InputDeviceLightsManagerTest.java
@@ -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
diff --git a/tests/Input/src/android/hardware/input/InputDeviceSensorManagerTest.java b/tests/Input/src/android/hardware/input/InputDeviceSensorManagerTest.java
index 0e3c200699d2..3057f5ddb540 100644
--- a/tests/Input/src/android/hardware/input/InputDeviceSensorManagerTest.java
+++ b/tests/Input/src/android/hardware/input/InputDeviceSensorManagerTest.java
@@ -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[] {
- createInputSensorInfo(DEVICE_ID, Sensor.TYPE_ACCELEROMETER),
- createInputSensorInfo(DEVICE_ID, Sensor.TYPE_GYROSCOPE)});
+ 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()));
}
}
diff --git a/tests/Input/src/android/hardware/input/InputManagerTest.kt b/tests/Input/src/android/hardware/input/InputManagerTest.kt
index 152dde94f006..4c6bb849155c 100644
--- a/tests/Input/src/android/hardware/input/InputManagerTest.kt
+++ b/tests/Input/src/android/hardware/input/InputManagerTest.kt
@@ -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)
diff --git a/tests/Input/src/android/hardware/input/KeyGestureEventHandlerTest.kt b/tests/Input/src/android/hardware/input/KeyGestureEventHandlerTest.kt
index 072341dcefae..e99c81493394 100644
--- a/tests/Input/src/android/hardware/input/KeyGestureEventHandlerTest.kt
+++ b/tests/Input/src/android/hardware/input/KeyGestureEventHandlerTest.kt
@@ -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) {
diff --git a/tests/Input/src/android/hardware/input/KeyGestureEventListenerTest.kt b/tests/Input/src/android/hardware/input/KeyGestureEventListenerTest.kt
index ca9de6000a5a..cf0bfcc4f6df 100644
--- a/tests/Input/src/android/hardware/input/KeyGestureEventListenerTest.kt
+++ b/tests/Input/src/android/hardware/input/KeyGestureEventListenerTest.kt
@@ -26,20 +26,19 @@ 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 kotlin.test.assertEquals
+import kotlin.test.assertNotNull
+import kotlin.test.assertNull
+import kotlin.test.fail
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`
import org.mockito.junit.MockitoJUnitRunner
-import kotlin.test.assertEquals
-import kotlin.test.assertNotNull
-import kotlin.test.assertNull
-import kotlin.test.fail
/**
* Tests for [InputManager.KeyGestureEventListener].
@@ -63,21 +62,18 @@ class KeyGestureEventListenerTest {
@get:Rule
val rule = SetFlagsRule()
+ @get:Rule
+ val inputManagerRule = MockInputManagerRule()
private val testLooper = TestLooper()
private val executor = HandlerExecutor(Handler(testLooper.looper))
private var registeredListener: IKeyGestureEventListener? = 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)
@@ -92,7 +88,7 @@ class KeyGestureEventListenerTest {
}
registeredListener = listener
null
- }.`when`(iInputManagerMock).registerKeyGestureEventListener(any())
+ }.`when`(inputManagerRule.mock).registerKeyGestureEventListener(any())
// Handle key gesture event listener being unregistered.
doAnswer {
@@ -103,14 +99,7 @@ class KeyGestureEventListenerTest {
}
registeredListener = null
null
- }.`when`(iInputManagerMock).unregisterKeyGestureEventListener(any())
- }
-
- @After
- fun tearDown() {
- if (this::inputManagerGlobalSession.isInitialized) {
- inputManagerGlobalSession.close()
- }
+ }.`when`(inputManagerRule.mock).unregisterKeyGestureEventListener(any())
}
private fun notifyKeyGestureEvent(event: KeyGestureEvent) {
diff --git a/tests/Input/src/android/hardware/input/KeyboardBacklightListenerTest.kt b/tests/Input/src/android/hardware/input/KeyboardBacklightListenerTest.kt
index 23135b2550b0..d25dee1d402c 100644
--- a/tests/Input/src/android/hardware/input/KeyboardBacklightListenerTest.kt
+++ b/tests/Input/src/android/hardware/input/KeyboardBacklightListenerTest.kt
@@ -24,22 +24,20 @@ import android.os.test.TestLooper
import android.platform.test.annotations.Presubmit
import androidx.test.core.app.ApplicationProvider
import com.android.server.testutils.any
-import org.junit.After
+import com.android.test.input.MockInputManagerRule
+import java.util.concurrent.Executor
+import kotlin.test.assertEquals
+import kotlin.test.assertNotNull
+import kotlin.test.assertNull
+import kotlin.test.fail
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`
-import org.mockito.junit.MockitoJUnit
import org.mockito.junit.MockitoJUnitRunner
-import java.util.concurrent.Executor
-import kotlin.test.assertEquals
-import kotlin.test.assertNotNull
-import kotlin.test.assertNull
-import kotlin.test.fail
/**
* Tests for [InputManager.KeyboardBacklightListener].
@@ -50,23 +48,19 @@ import kotlin.test.fail
@Presubmit
@RunWith(MockitoJUnitRunner::class)
class KeyboardBacklightListenerTest {
+
@get:Rule
- val rule = MockitoJUnit.rule()!!
+ val inputManagerRule = MockInputManagerRule()
private lateinit var testLooper: TestLooper
private var registeredListener: IKeyboardBacklightListener? = null
private lateinit var executor: Executor
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)
testLooper = TestLooper()
executor = HandlerExecutor(Handler(testLooper.looper))
registeredListener = null
@@ -84,7 +78,7 @@ class KeyboardBacklightListenerTest {
}
registeredListener = listener
null
- }.`when`(iInputManagerMock).registerKeyboardBacklightListener(any())
+ }.`when`(inputManagerRule.mock).registerKeyboardBacklightListener(any())
// Handle keyboard backlight listener being unregistered.
doAnswer {
@@ -95,14 +89,7 @@ class KeyboardBacklightListenerTest {
}
registeredListener = null
null
- }.`when`(iInputManagerMock).unregisterKeyboardBacklightListener(any())
- }
-
- @After
- fun tearDown() {
- if (this::inputManagerGlobalSession.isInitialized) {
- inputManagerGlobalSession.close()
- }
+ }.`when`(inputManagerRule.mock).unregisterKeyboardBacklightListener(any())
}
private fun notifyKeyboardBacklightChanged(
diff --git a/tests/Input/src/android/hardware/input/StickyModifierStateListenerTest.kt b/tests/Input/src/android/hardware/input/StickyModifierStateListenerTest.kt
index bcd56ad0c669..1c2a0538e552 100644
--- a/tests/Input/src/android/hardware/input/StickyModifierStateListenerTest.kt
+++ b/tests/Input/src/android/hardware/input/StickyModifierStateListenerTest.kt
@@ -27,21 +27,20 @@ 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 kotlin.test.assertEquals
+import kotlin.test.assertNotNull
+import kotlin.test.assertNull
+import kotlin.test.assertTrue
+import kotlin.test.fail
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`
import org.mockito.junit.MockitoJUnitRunner
-import kotlin.test.assertEquals
-import kotlin.test.assertNotNull
-import kotlin.test.assertNull
-import kotlin.test.assertTrue
-import kotlin.test.fail
/**
* Tests for [InputManager.StickyModifierStateListener].
@@ -59,21 +58,18 @@ class StickyModifierStateListenerTest {
@get:Rule
val rule = SetFlagsRule()
+ @get:Rule
+ val inputManagerRule = MockInputManagerRule()
private val testLooper = TestLooper()
private val executor = HandlerExecutor(Handler(testLooper.looper))
private var registeredListener: IStickyModifierStateListener? = 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)
@@ -88,7 +84,7 @@ class StickyModifierStateListenerTest {
}
registeredListener = listener
null
- }.`when`(iInputManagerMock).registerStickyModifierStateListener(any())
+ }.`when`(inputManagerRule.mock).registerStickyModifierStateListener(any())
// Handle sticky modifier state listener being unregistered.
doAnswer {
@@ -99,14 +95,7 @@ class StickyModifierStateListenerTest {
}
registeredListener = null
null
- }.`when`(iInputManagerMock).unregisterStickyModifierStateListener(any())
- }
-
- @After
- fun tearDown() {
- if (this::inputManagerGlobalSession.isInitialized) {
- inputManagerGlobalSession.close()
- }
+ }.`when`(inputManagerRule.mock).unregisterStickyModifierStateListener(any())
}
private fun notifyStickyModifierStateChanged(modifierState: Int, lockedModifierState: Int) {
diff --git a/tests/Input/src/com/android/server/input/BatteryControllerTests.kt b/tests/Input/src/com/android/server/input/BatteryControllerTests.kt
index f2724e605553..044f11d6904c 100644
--- a/tests/Input/src/com/android/server/input/BatteryControllerTests.kt
+++ b/tests/Input/src/com/android/server/input/BatteryControllerTests.kt
@@ -27,7 +27,6 @@ import android.hardware.input.HostUsiVersion
import android.hardware.input.IInputDeviceBatteryListener
import android.hardware.input.IInputDeviceBatteryState
import android.hardware.input.IInputDevicesChangedListener
-import android.hardware.input.IInputManager
import android.hardware.input.InputManager
import android.hardware.input.InputManagerGlobal
import android.os.Binder
@@ -42,13 +41,13 @@ import com.android.server.input.BatteryController.BluetoothBatteryManager.Blueto
import com.android.server.input.BatteryController.POLLING_PERIOD_MILLIS
import com.android.server.input.BatteryController.UEventBatteryListener
import com.android.server.input.BatteryController.USI_BATTERY_VALIDITY_DURATION_MILLIS
+import com.android.test.input.MockInputManagerRule
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers
import org.hamcrest.TypeSafeMatcher
import org.hamcrest.core.IsEqual.equalTo
-import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
@@ -184,12 +183,12 @@ class BatteryControllerTests {
@get:Rule
val rule = MockitoJUnit.rule()!!
+ @get:Rule
+ val inputManagerRule = MockInputManagerRule()
@Mock
private lateinit var native: NativeInputManagerService
@Mock
- private lateinit var iInputManager: IInputManager
- @Mock
private lateinit var uEventManager: UEventManager
@Mock
private lateinit var bluetoothBatteryManager: BluetoothBatteryManager
@@ -205,10 +204,9 @@ class BatteryControllerTests {
fun setup() {
context = TestableContext(ApplicationProvider.getApplicationContext())
testLooper = TestLooper()
- inputManagerGlobalSession = InputManagerGlobal.createTestSession(iInputManager)
val inputManager = InputManager(context)
context.addMockSystemService(InputManager::class.java, inputManager)
- `when`(iInputManager.inputDeviceIds).then {
+ `when`(inputManagerRule.mock.inputDeviceIds).then {
deviceGenerationMap.keys.toIntArray()
}
addInputDevice(DEVICE_ID)
@@ -218,18 +216,11 @@ class BatteryControllerTests {
bluetoothBatteryManager)
batteryController.systemRunning()
val listenerCaptor = ArgumentCaptor.forClass(IInputDevicesChangedListener::class.java)
- verify(iInputManager).registerInputDevicesChangedListener(listenerCaptor.capture())
+ verify(inputManagerRule.mock).registerInputDevicesChangedListener(listenerCaptor.capture())
devicesChangedListener = listenerCaptor.value
testLooper.dispatchAll()
}
- @After
- fun tearDown() {
- if (this::inputManagerGlobalSession.isInitialized) {
- inputManagerGlobalSession.close()
- }
- }
-
private fun notifyDeviceChanged(
deviceId: Int,
hasBattery: Boolean = true,
@@ -239,7 +230,7 @@ class BatteryControllerTests {
?: throw IllegalArgumentException("Device $deviceId was never added!")
deviceGenerationMap[deviceId] = generation
- `when`(iInputManager.getInputDevice(deviceId))
+ `when`(inputManagerRule.mock.getInputDevice(deviceId))
.thenReturn(createInputDevice(deviceId, hasBattery, supportsUsi, generation))
val list = deviceGenerationMap.flatMap { listOf(it.key, it.value) }
if (::devicesChangedListener.isInitialized) {
@@ -657,9 +648,9 @@ class BatteryControllerTests {
@Test
fun testRegisterBluetoothListenerForMonitoredBluetoothDevices() {
- `when`(iInputManager.getInputDeviceBluetoothAddress(BT_DEVICE_ID))
+ `when`(inputManagerRule.mock.getInputDeviceBluetoothAddress(BT_DEVICE_ID))
.thenReturn("AA:BB:CC:DD:EE:FF")
- `when`(iInputManager.getInputDeviceBluetoothAddress(SECOND_BT_DEVICE_ID))
+ `when`(inputManagerRule.mock.getInputDeviceBluetoothAddress(SECOND_BT_DEVICE_ID))
.thenReturn("11:22:33:44:55:66")
addInputDevice(BT_DEVICE_ID)
testLooper.dispatchNext()
@@ -686,7 +677,7 @@ class BatteryControllerTests {
batteryController.unregisterBatteryListener(BT_DEVICE_ID, listener, PID)
verify(bluetoothBatteryManager, never()).removeBatteryListener(any())
- `when`(iInputManager.getInputDeviceBluetoothAddress(SECOND_BT_DEVICE_ID))
+ `when`(inputManagerRule.mock.getInputDeviceBluetoothAddress(SECOND_BT_DEVICE_ID))
.thenReturn(null)
notifyDeviceChanged(SECOND_BT_DEVICE_ID)
testLooper.dispatchNext()
@@ -695,7 +686,7 @@ class BatteryControllerTests {
@Test
fun testNotifiesBluetoothBatteryChanges() {
- `when`(iInputManager.getInputDeviceBluetoothAddress(BT_DEVICE_ID))
+ `when`(inputManagerRule.mock.getInputDeviceBluetoothAddress(BT_DEVICE_ID))
.thenReturn("AA:BB:CC:DD:EE:FF")
`when`(bluetoothBatteryManager.getBatteryLevel(eq("AA:BB:CC:DD:EE:FF"))).thenReturn(21)
addInputDevice(BT_DEVICE_ID)
@@ -716,7 +707,7 @@ class BatteryControllerTests {
@Test
fun testBluetoothBatteryIsPrioritized() {
`when`(native.getBatteryDevicePath(BT_DEVICE_ID)).thenReturn("/sys/dev/bt_device")
- `when`(iInputManager.getInputDeviceBluetoothAddress(BT_DEVICE_ID))
+ `when`(inputManagerRule.mock.getInputDeviceBluetoothAddress(BT_DEVICE_ID))
.thenReturn("AA:BB:CC:DD:EE:FF")
`when`(bluetoothBatteryManager.getBatteryLevel(eq("AA:BB:CC:DD:EE:FF"))).thenReturn(21)
`when`(native.getBatteryCapacity(BT_DEVICE_ID)).thenReturn(98)
@@ -745,7 +736,7 @@ class BatteryControllerTests {
@Test
fun testFallBackToNativeBatteryStateWhenBluetoothStateInvalid() {
`when`(native.getBatteryDevicePath(BT_DEVICE_ID)).thenReturn("/sys/dev/bt_device")
- `when`(iInputManager.getInputDeviceBluetoothAddress(BT_DEVICE_ID))
+ `when`(inputManagerRule.mock.getInputDeviceBluetoothAddress(BT_DEVICE_ID))
.thenReturn("AA:BB:CC:DD:EE:FF")
`when`(bluetoothBatteryManager.getBatteryLevel(eq("AA:BB:CC:DD:EE:FF"))).thenReturn(21)
`when`(native.getBatteryCapacity(BT_DEVICE_ID)).thenReturn(98)
@@ -776,9 +767,9 @@ class BatteryControllerTests {
@Test
fun testRegisterBluetoothMetadataListenerForMonitoredBluetoothDevices() {
- `when`(iInputManager.getInputDeviceBluetoothAddress(BT_DEVICE_ID))
+ `when`(inputManagerRule.mock.getInputDeviceBluetoothAddress(BT_DEVICE_ID))
.thenReturn("AA:BB:CC:DD:EE:FF")
- `when`(iInputManager.getInputDeviceBluetoothAddress(SECOND_BT_DEVICE_ID))
+ `when`(inputManagerRule.mock.getInputDeviceBluetoothAddress(SECOND_BT_DEVICE_ID))
.thenReturn("11:22:33:44:55:66")
addInputDevice(BT_DEVICE_ID)
testLooper.dispatchNext()
@@ -811,7 +802,7 @@ class BatteryControllerTests {
verify(bluetoothBatteryManager)
.removeMetadataListener("AA:BB:CC:DD:EE:FF", metadataListener1.value)
- `when`(iInputManager.getInputDeviceBluetoothAddress(SECOND_BT_DEVICE_ID))
+ `when`(inputManagerRule.mock.getInputDeviceBluetoothAddress(SECOND_BT_DEVICE_ID))
.thenReturn(null)
notifyDeviceChanged(SECOND_BT_DEVICE_ID)
testLooper.dispatchNext()
@@ -821,7 +812,7 @@ class BatteryControllerTests {
@Test
fun testNotifiesBluetoothMetadataBatteryChanges() {
- `when`(iInputManager.getInputDeviceBluetoothAddress(BT_DEVICE_ID))
+ `when`(inputManagerRule.mock.getInputDeviceBluetoothAddress(BT_DEVICE_ID))
.thenReturn("AA:BB:CC:DD:EE:FF")
`when`(bluetoothBatteryManager.getMetadata("AA:BB:CC:DD:EE:FF",
BluetoothDevice.METADATA_MAIN_BATTERY))
@@ -861,7 +852,7 @@ class BatteryControllerTests {
@Test
fun testBluetoothMetadataBatteryIsPrioritized() {
- `when`(iInputManager.getInputDeviceBluetoothAddress(BT_DEVICE_ID))
+ `when`(inputManagerRule.mock.getInputDeviceBluetoothAddress(BT_DEVICE_ID))
.thenReturn("AA:BB:CC:DD:EE:FF")
`when`(bluetoothBatteryManager.getBatteryLevel(eq("AA:BB:CC:DD:EE:FF"))).thenReturn(21)
`when`(bluetoothBatteryManager.getMetadata("AA:BB:CC:DD:EE:FF",
diff --git a/tests/Input/src/com/android/server/input/InputManagerServiceTests.kt b/tests/Input/src/com/android/server/input/InputManagerServiceTests.kt
index 351ec4635977..927958eb62cc 100644
--- a/tests/Input/src/com/android/server/input/InputManagerServiceTests.kt
+++ b/tests/Input/src/com/android/server/input/InputManagerServiceTests.kt
@@ -93,14 +93,12 @@ class InputManagerServiceTests {
)
}
- @JvmField
- @Rule
+ @get:Rule
val extendedMockitoRule =
ExtendedMockitoRule.Builder(this).mockStatic(LocalServices::class.java)
.mockStatic(PermissionChecker::class.java).build()!!
- @JvmField
- @Rule
+ @get:Rule
val setFlagsRule = SetFlagsRule()
@get:Rule
diff --git a/tests/Input/src/com/android/server/input/KeyRemapperTests.kt b/tests/Input/src/com/android/server/input/KeyRemapperTests.kt
index f74fd723d540..4f4c97bef4c0 100644
--- a/tests/Input/src/com/android/server/input/KeyRemapperTests.kt
+++ b/tests/Input/src/com/android/server/input/KeyRemapperTests.kt
@@ -18,16 +18,18 @@ package com.android.server.input
import android.content.Context
import android.content.ContextWrapper
-import android.hardware.input.IInputManager
import android.hardware.input.InputManager
-import android.hardware.input.InputManagerGlobal
import android.os.test.TestLooper
import android.platform.test.annotations.Presubmit
import android.provider.Settings
import android.view.InputDevice
import android.view.KeyEvent
import androidx.test.core.app.ApplicationProvider
-import org.junit.After
+import com.android.test.input.MockInputManagerRule
+import java.io.FileNotFoundException
+import java.io.FileOutputStream
+import java.io.IOException
+import java.io.InputStream
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Rule
@@ -35,10 +37,6 @@ import org.junit.Test
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.junit.MockitoJUnit
-import java.io.FileNotFoundException
-import java.io.FileOutputStream
-import java.io.IOException
-import java.io.InputStream
private fun createKeyboard(deviceId: Int): InputDevice =
InputDevice.Builder()
@@ -73,15 +71,15 @@ class KeyRemapperTests {
@get:Rule
val rule = MockitoJUnit.rule()!!
- @Mock
- private lateinit var iInputManager: IInputManager
+ @get:Rule
+ val inputManagerRule = MockInputManagerRule()
+
@Mock
private lateinit var native: NativeInputManagerService
private lateinit var mKeyRemapper: KeyRemapper
private lateinit var context: Context
private lateinit var dataStore: PersistentDataStore
private lateinit var testLooper: TestLooper
- private lateinit var inputManagerGlobalSession: InputManagerGlobal.TestSession
@Before
fun setup() {
@@ -104,25 +102,17 @@ class KeyRemapperTests {
dataStore,
testLooper.looper
)
- inputManagerGlobalSession = InputManagerGlobal.createTestSession(iInputManager)
val inputManager = InputManager(context)
Mockito.`when`(context.getSystemService(Mockito.eq(Context.INPUT_SERVICE)))
.thenReturn(inputManager)
- Mockito.`when`(iInputManager.inputDeviceIds).thenReturn(intArrayOf(DEVICE_ID))
- }
-
- @After
- fun tearDown() {
- if (this::inputManagerGlobalSession.isInitialized) {
- inputManagerGlobalSession.close()
- }
+ Mockito.`when`(inputManagerRule.mock.inputDeviceIds).thenReturn(intArrayOf(DEVICE_ID))
}
@Test
fun testKeyRemapping_whenRemappingEnabled() {
ModifierRemappingFlag(true).use {
val keyboard = createKeyboard(DEVICE_ID)
- Mockito.`when`(iInputManager.getInputDevice(DEVICE_ID)).thenReturn(keyboard)
+ Mockito.`when`(inputManagerRule.mock.getInputDevice(DEVICE_ID)).thenReturn(keyboard)
for (i in REMAPPABLE_KEYS.indices) {
val fromKeyCode = REMAPPABLE_KEYS[i]
@@ -160,7 +150,7 @@ class KeyRemapperTests {
fun testKeyRemapping_whenRemappingDisabled() {
ModifierRemappingFlag(false).use {
val keyboard = createKeyboard(DEVICE_ID)
- Mockito.`when`(iInputManager.getInputDevice(DEVICE_ID)).thenReturn(keyboard)
+ Mockito.`when`(inputManagerRule.mock.getInputDevice(DEVICE_ID)).thenReturn(keyboard)
mKeyRemapper.remapKey(REMAPPABLE_KEYS[0], REMAPPABLE_KEYS[1])
testLooper.dispatchAll()
diff --git a/tests/Input/src/com/android/server/input/KeyboardBacklightControllerTests.kt b/tests/Input/src/com/android/server/input/KeyboardBacklightControllerTests.kt
index 59aa96c46336..58fb4e1ed103 100644
--- a/tests/Input/src/com/android/server/input/KeyboardBacklightControllerTests.kt
+++ b/tests/Input/src/com/android/server/input/KeyboardBacklightControllerTests.kt
@@ -20,11 +20,9 @@ import android.animation.ValueAnimator
import android.content.Context
import android.content.ContextWrapper
import android.graphics.Color
-import android.hardware.input.IInputManager
import android.hardware.input.IKeyboardBacklightListener
import android.hardware.input.IKeyboardBacklightState
import android.hardware.input.InputManager
-import android.hardware.input.InputManagerGlobal
import android.hardware.lights.Light
import android.os.UEventObserver
import android.os.test.TestLooper
@@ -35,7 +33,11 @@ import androidx.test.core.app.ApplicationProvider
import com.android.server.input.KeyboardBacklightController.DEFAULT_BRIGHTNESS_VALUE_FOR_LEVEL
import com.android.server.input.KeyboardBacklightController.MAX_BRIGHTNESS_CHANGE_STEPS
import com.android.server.input.KeyboardBacklightController.USER_INACTIVITY_THRESHOLD_MILLIS
-import org.junit.After
+import com.android.test.input.MockInputManagerRule
+import java.io.FileNotFoundException
+import java.io.FileOutputStream
+import java.io.IOException
+import java.io.InputStream
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotEquals
@@ -52,10 +54,6 @@ import org.mockito.Mockito.eq
import org.mockito.Mockito.spy
import org.mockito.Mockito.`when`
import org.mockito.junit.MockitoJUnit
-import java.io.FileNotFoundException
-import java.io.FileOutputStream
-import java.io.IOException
-import java.io.InputStream
private fun createKeyboard(deviceId: Int): InputDevice =
InputDevice.Builder()
@@ -100,10 +98,10 @@ class KeyboardBacklightControllerTests {
@get:Rule
val rule = MockitoJUnit.rule()!!
+ @get:Rule
+ val inputManagerRule = MockInputManagerRule()
@Mock
- private lateinit var iInputManager: IInputManager
- @Mock
private lateinit var native: NativeInputManagerService
@Mock
private lateinit var uEventManager: UEventManager
@@ -111,7 +109,6 @@ class KeyboardBacklightControllerTests {
private lateinit var context: Context
private lateinit var dataStore: PersistentDataStore
private lateinit var testLooper: TestLooper
- private lateinit var inputManagerGlobalSession: InputManagerGlobal.TestSession
private var lightColorMap: HashMap<Int, Int> = HashMap()
private var lastBacklightState: KeyboardBacklightState? = null
private var sysfsNodeChanges = 0
@@ -134,10 +131,9 @@ class KeyboardBacklightControllerTests {
testLooper = TestLooper()
keyboardBacklightController = KeyboardBacklightController(context, native, dataStore,
testLooper.looper, FakeAnimatorFactory(), uEventManager)
- inputManagerGlobalSession = InputManagerGlobal.createTestSession(iInputManager)
val inputManager = InputManager(context)
`when`(context.getSystemService(eq(Context.INPUT_SERVICE))).thenReturn(inputManager)
- `when`(iInputManager.inputDeviceIds).thenReturn(intArrayOf(DEVICE_ID))
+ `when`(inputManagerRule.mock.inputDeviceIds).thenReturn(intArrayOf(DEVICE_ID))
`when`(native.setLightColor(anyInt(), anyInt(), anyInt())).then {
val args = it.arguments
lightColorMap.put(args[1] as Int, args[2] as Int)
@@ -152,13 +148,6 @@ class KeyboardBacklightControllerTests {
}
}
- @After
- fun tearDown() {
- if (this::inputManagerGlobalSession.isInitialized) {
- inputManagerGlobalSession.close()
- }
- }
-
@Test
fun testKeyboardBacklightIncrementDecrement() {
KeyboardBacklightFlags(
@@ -168,8 +157,9 @@ class KeyboardBacklightControllerTests {
).use {
val keyboardWithBacklight = createKeyboard(DEVICE_ID)
val keyboardBacklight = createLight(LIGHT_ID, Light.LIGHT_TYPE_KEYBOARD_BACKLIGHT)
- `when`(iInputManager.getInputDevice(DEVICE_ID)).thenReturn(keyboardWithBacklight)
- `when`(iInputManager.getLights(DEVICE_ID)).thenReturn(listOf(keyboardBacklight))
+ `when`(inputManagerRule.mock.getInputDevice(DEVICE_ID))
+ .thenReturn(keyboardWithBacklight)
+ `when`(inputManagerRule.mock.getLights(DEVICE_ID)).thenReturn(listOf(keyboardBacklight))
keyboardBacklightController.onInputDeviceAdded(DEVICE_ID)
assertIncrementDecrementForLevels(keyboardWithBacklight, keyboardBacklight,
@@ -186,8 +176,9 @@ class KeyboardBacklightControllerTests {
).use {
val keyboardWithoutBacklight = createKeyboard(DEVICE_ID)
val keyboardInputLight = createLight(LIGHT_ID, Light.LIGHT_TYPE_INPUT)
- `when`(iInputManager.getInputDevice(DEVICE_ID)).thenReturn(keyboardWithoutBacklight)
- `when`(iInputManager.getLights(DEVICE_ID)).thenReturn(listOf(keyboardInputLight))
+ `when`(inputManagerRule.mock.getInputDevice(DEVICE_ID))
+ .thenReturn(keyboardWithoutBacklight)
+ `when`(inputManagerRule.mock.getLights(DEVICE_ID)).thenReturn(listOf(keyboardInputLight))
keyboardBacklightController.onInputDeviceAdded(DEVICE_ID)
incrementKeyboardBacklight(DEVICE_ID)
@@ -205,8 +196,9 @@ class KeyboardBacklightControllerTests {
val keyboardWithBacklight = createKeyboard(DEVICE_ID)
val keyboardBacklight = createLight(LIGHT_ID, Light.LIGHT_TYPE_KEYBOARD_BACKLIGHT)
val keyboardInputLight = createLight(SECOND_LIGHT_ID, Light.LIGHT_TYPE_INPUT)
- `when`(iInputManager.getInputDevice(DEVICE_ID)).thenReturn(keyboardWithBacklight)
- `when`(iInputManager.getLights(DEVICE_ID)).thenReturn(
+ `when`(inputManagerRule.mock.getInputDevice(DEVICE_ID))
+ .thenReturn(keyboardWithBacklight)
+ `when`(inputManagerRule.mock.getLights(DEVICE_ID)).thenReturn(
listOf(
keyboardBacklight,
keyboardInputLight
@@ -230,8 +222,9 @@ class KeyboardBacklightControllerTests {
).use {
val keyboardWithBacklight = createKeyboard(DEVICE_ID)
val keyboardBacklight = createLight(LIGHT_ID, Light.LIGHT_TYPE_KEYBOARD_BACKLIGHT)
- `when`(iInputManager.getInputDevice(DEVICE_ID)).thenReturn(keyboardWithBacklight)
- `when`(iInputManager.getLights(DEVICE_ID)).thenReturn(listOf(keyboardBacklight))
+ `when`(inputManagerRule.mock.getInputDevice(DEVICE_ID))
+ .thenReturn(keyboardWithBacklight)
+ `when`(inputManagerRule.mock.getLights(DEVICE_ID)).thenReturn(listOf(keyboardBacklight))
for (level in 1 until DEFAULT_BRIGHTNESS_VALUE_FOR_LEVEL.size) {
dataStore.setKeyboardBacklightBrightness(
@@ -263,7 +256,8 @@ class KeyboardBacklightControllerTests {
).use {
val keyboardWithBacklight = createKeyboard(DEVICE_ID)
val keyboardBacklight = createLight(LIGHT_ID, Light.LIGHT_TYPE_KEYBOARD_BACKLIGHT)
- `when`(iInputManager.getInputDevice(DEVICE_ID)).thenReturn(keyboardWithBacklight)
+ `when`(inputManagerRule.mock.getInputDevice(DEVICE_ID))
+ .thenReturn(keyboardWithBacklight)
dataStore.setKeyboardBacklightBrightness(
keyboardWithBacklight.descriptor,
LIGHT_ID,
@@ -278,7 +272,7 @@ class KeyboardBacklightControllerTests {
lightColorMap.isEmpty()
)
- `when`(iInputManager.getLights(DEVICE_ID)).thenReturn(listOf(keyboardBacklight))
+ `when`(inputManagerRule.mock.getLights(DEVICE_ID)).thenReturn(listOf(keyboardBacklight))
keyboardBacklightController.onInputDeviceChanged(DEVICE_ID)
keyboardBacklightController.notifyUserActivity()
testLooper.dispatchNext()
@@ -300,8 +294,9 @@ class KeyboardBacklightControllerTests {
val keyboardWithBacklight = createKeyboard(DEVICE_ID)
val keyboardBacklight = createLight(LIGHT_ID, Light.LIGHT_TYPE_KEYBOARD_BACKLIGHT)
val maxLevel = DEFAULT_BRIGHTNESS_VALUE_FOR_LEVEL.size - 1
- `when`(iInputManager.getInputDevice(DEVICE_ID)).thenReturn(keyboardWithBacklight)
- `when`(iInputManager.getLights(DEVICE_ID)).thenReturn(listOf(keyboardBacklight))
+ `when`(inputManagerRule.mock.getInputDevice(DEVICE_ID))
+ .thenReturn(keyboardWithBacklight)
+ `when`(inputManagerRule.mock.getLights(DEVICE_ID)).thenReturn(listOf(keyboardBacklight))
keyboardBacklightController.onInputDeviceAdded(DEVICE_ID)
// Register backlight listener
@@ -352,8 +347,9 @@ class KeyboardBacklightControllerTests {
).use {
val keyboardWithBacklight = createKeyboard(DEVICE_ID)
val keyboardBacklight = createLight(LIGHT_ID, Light.LIGHT_TYPE_KEYBOARD_BACKLIGHT)
- `when`(iInputManager.getInputDevice(DEVICE_ID)).thenReturn(keyboardWithBacklight)
- `when`(iInputManager.getLights(DEVICE_ID)).thenReturn(listOf(keyboardBacklight))
+ `when`(inputManagerRule.mock.getInputDevice(DEVICE_ID))
+ .thenReturn(keyboardWithBacklight)
+ `when`(inputManagerRule.mock.getLights(DEVICE_ID)).thenReturn(listOf(keyboardBacklight))
dataStore.setKeyboardBacklightBrightness(
keyboardWithBacklight.descriptor,
LIGHT_ID,
@@ -388,8 +384,9 @@ class KeyboardBacklightControllerTests {
).use {
val keyboardWithBacklight = createKeyboard(DEVICE_ID)
val keyboardBacklight = createLight(LIGHT_ID, Light.LIGHT_TYPE_KEYBOARD_BACKLIGHT)
- `when`(iInputManager.getInputDevice(DEVICE_ID)).thenReturn(keyboardWithBacklight)
- `when`(iInputManager.getLights(DEVICE_ID)).thenReturn(listOf(keyboardBacklight))
+ `when`(inputManagerRule.mock.getInputDevice(DEVICE_ID))
+ .thenReturn(keyboardWithBacklight)
+ `when`(inputManagerRule.mock.getLights(DEVICE_ID)).thenReturn(listOf(keyboardBacklight))
dataStore.setKeyboardBacklightBrightness(
keyboardWithBacklight.descriptor,
LIGHT_ID,
@@ -482,8 +479,9 @@ class KeyboardBacklightControllerTests {
).use {
val keyboardWithBacklight = createKeyboard(DEVICE_ID)
val keyboardBacklight = createLight(LIGHT_ID, Light.LIGHT_TYPE_KEYBOARD_BACKLIGHT)
- `when`(iInputManager.getInputDevice(DEVICE_ID)).thenReturn(keyboardWithBacklight)
- `when`(iInputManager.getLights(DEVICE_ID)).thenReturn(listOf(keyboardBacklight))
+ `when`(inputManagerRule.mock.getInputDevice(DEVICE_ID))
+ .thenReturn(keyboardWithBacklight)
+ `when`(inputManagerRule.mock.getLights(DEVICE_ID)).thenReturn(listOf(keyboardBacklight))
keyboardBacklightController.onInputDeviceAdded(DEVICE_ID)
incrementKeyboardBacklight(DEVICE_ID)
@@ -511,8 +509,9 @@ class KeyboardBacklightControllerTests {
val suggestedLevels = intArrayOf(0, 22, 63, 135, 196, 255)
val keyboardBacklight = createLight(LIGHT_ID, Light.LIGHT_TYPE_KEYBOARD_BACKLIGHT,
suggestedLevels)
- `when`(iInputManager.getInputDevice(DEVICE_ID)).thenReturn(keyboardWithBacklight)
- `when`(iInputManager.getLights(DEVICE_ID)).thenReturn(listOf(keyboardBacklight))
+ `when`(inputManagerRule.mock.getInputDevice(DEVICE_ID))
+ .thenReturn(keyboardWithBacklight)
+ `when`(inputManagerRule.mock.getLights(DEVICE_ID)).thenReturn(listOf(keyboardBacklight))
keyboardBacklightController.onInputDeviceAdded(DEVICE_ID)
assertIncrementDecrementForLevels(keyboardWithBacklight, keyboardBacklight,
@@ -531,8 +530,9 @@ class KeyboardBacklightControllerTests {
val suggestedLevels = IntArray(MAX_BRIGHTNESS_CHANGE_STEPS + 1) { 10 * (it + 1) }
val keyboardBacklight = createLight(LIGHT_ID, Light.LIGHT_TYPE_KEYBOARD_BACKLIGHT,
suggestedLevels)
- `when`(iInputManager.getInputDevice(DEVICE_ID)).thenReturn(keyboardWithBacklight)
- `when`(iInputManager.getLights(DEVICE_ID)).thenReturn(listOf(keyboardBacklight))
+ `when`(inputManagerRule.mock.getInputDevice(DEVICE_ID))
+ .thenReturn(keyboardWithBacklight)
+ `when`(inputManagerRule.mock.getLights(DEVICE_ID)).thenReturn(listOf(keyboardBacklight))
keyboardBacklightController.onInputDeviceAdded(DEVICE_ID)
assertIncrementDecrementForLevels(keyboardWithBacklight, keyboardBacklight,
@@ -551,8 +551,9 @@ class KeyboardBacklightControllerTests {
val suggestedLevels = intArrayOf(22, 63, 135, 196)
val keyboardBacklight = createLight(LIGHT_ID, Light.LIGHT_TYPE_KEYBOARD_BACKLIGHT,
suggestedLevels)
- `when`(iInputManager.getInputDevice(DEVICE_ID)).thenReturn(keyboardWithBacklight)
- `when`(iInputManager.getLights(DEVICE_ID)).thenReturn(listOf(keyboardBacklight))
+ `when`(inputManagerRule.mock.getInputDevice(DEVICE_ID))
+ .thenReturn(keyboardWithBacklight)
+ `when`(inputManagerRule.mock.getLights(DEVICE_ID)).thenReturn(listOf(keyboardBacklight))
keyboardBacklightController.onInputDeviceAdded(DEVICE_ID)
// Framework will add the lowest and maximum levels if not provided via config
@@ -572,8 +573,10 @@ class KeyboardBacklightControllerTests {
val suggestedLevels = intArrayOf(22, 63, 135, 400, 196, 1000)
val keyboardBacklight = createLight(LIGHT_ID, Light.LIGHT_TYPE_KEYBOARD_BACKLIGHT,
suggestedLevels)
- `when`(iInputManager.getInputDevice(DEVICE_ID)).thenReturn(keyboardWithBacklight)
- `when`(iInputManager.getLights(DEVICE_ID)).thenReturn(listOf(keyboardBacklight))
+ `when`(inputManagerRule.mock.getInputDevice(DEVICE_ID))
+ .thenReturn(keyboardWithBacklight)
+ `when`(inputManagerRule.mock.getLights(DEVICE_ID))
+ .thenReturn(listOf(keyboardBacklight))
keyboardBacklightController.onInputDeviceAdded(DEVICE_ID)
// Framework will drop out of bound levels in the config
@@ -591,8 +594,9 @@ class KeyboardBacklightControllerTests {
).use {
val keyboardWithBacklight = createKeyboard(DEVICE_ID)
val keyboardBacklight = createLight(LIGHT_ID, Light.LIGHT_TYPE_KEYBOARD_BACKLIGHT)
- `when`(iInputManager.getInputDevice(DEVICE_ID)).thenReturn(keyboardWithBacklight)
- `when`(iInputManager.getLights(DEVICE_ID)).thenReturn(listOf(keyboardBacklight))
+ `when`(inputManagerRule.mock.getInputDevice(DEVICE_ID))
+ .thenReturn(keyboardWithBacklight)
+ `when`(inputManagerRule.mock.getLights(DEVICE_ID)).thenReturn(listOf(keyboardBacklight))
dataStore.setKeyboardBacklightBrightness(
keyboardWithBacklight.descriptor,
@@ -619,8 +623,9 @@ class KeyboardBacklightControllerTests {
).use {
val keyboardWithBacklight = createKeyboard(DEVICE_ID)
val keyboardBacklight = createLight(LIGHT_ID, Light.LIGHT_TYPE_KEYBOARD_BACKLIGHT)
- `when`(iInputManager.getInputDevice(DEVICE_ID)).thenReturn(keyboardWithBacklight)
- `when`(iInputManager.getLights(DEVICE_ID)).thenReturn(listOf(keyboardBacklight))
+ `when`(inputManagerRule.mock.getInputDevice(DEVICE_ID))
+ .thenReturn(keyboardWithBacklight)
+ `when`(inputManagerRule.mock.getLights(DEVICE_ID)).thenReturn(listOf(keyboardBacklight))
keyboardBacklightController.onInputDeviceAdded(DEVICE_ID)
incrementKeyboardBacklight(DEVICE_ID)
@@ -642,8 +647,9 @@ class KeyboardBacklightControllerTests {
).use {
val keyboardWithBacklight = createKeyboard(DEVICE_ID)
val keyboardBacklight = createLight(LIGHT_ID, Light.LIGHT_TYPE_KEYBOARD_BACKLIGHT)
- `when`(iInputManager.getInputDevice(DEVICE_ID)).thenReturn(keyboardWithBacklight)
- `when`(iInputManager.getLights(DEVICE_ID)).thenReturn(listOf(keyboardBacklight))
+ `when`(inputManagerRule.mock.getInputDevice(DEVICE_ID))
+ .thenReturn(keyboardWithBacklight)
+ `when`(inputManagerRule.mock.getLights(DEVICE_ID)).thenReturn(listOf(keyboardBacklight))
keyboardBacklightController.onInputDeviceAdded(DEVICE_ID)
sendAmbientBacklightValue(1)
assertEquals(
@@ -671,8 +677,9 @@ class KeyboardBacklightControllerTests {
).use {
val keyboardWithBacklight = createKeyboard(DEVICE_ID)
val keyboardBacklight = createLight(LIGHT_ID, Light.LIGHT_TYPE_KEYBOARD_BACKLIGHT)
- `when`(iInputManager.getInputDevice(DEVICE_ID)).thenReturn(keyboardWithBacklight)
- `when`(iInputManager.getLights(DEVICE_ID)).thenReturn(listOf(keyboardBacklight))
+ `when`(inputManagerRule.mock.getInputDevice(DEVICE_ID))
+ .thenReturn(keyboardWithBacklight)
+ `when`(inputManagerRule.mock.getLights(DEVICE_ID)).thenReturn(listOf(keyboardBacklight))
keyboardBacklightController.onInputDeviceAdded(DEVICE_ID)
sendAmbientBacklightValue(254)
assertEquals(
@@ -701,8 +708,9 @@ class KeyboardBacklightControllerTests {
).use {
val keyboardWithBacklight = createKeyboard(DEVICE_ID)
val keyboardBacklight = createLight(LIGHT_ID, Light.LIGHT_TYPE_KEYBOARD_BACKLIGHT)
- `when`(iInputManager.getInputDevice(DEVICE_ID)).thenReturn(keyboardWithBacklight)
- `when`(iInputManager.getLights(DEVICE_ID)).thenReturn(listOf(keyboardBacklight))
+ `when`(inputManagerRule.mock.getInputDevice(DEVICE_ID))
+ .thenReturn(keyboardWithBacklight)
+ `when`(inputManagerRule.mock.getLights(DEVICE_ID)).thenReturn(listOf(keyboardBacklight))
keyboardBacklightController.onInputDeviceAdded(DEVICE_ID)
incrementKeyboardBacklight(DEVICE_ID)
assertEquals(
diff --git a/tests/Input/src/com/android/server/input/KeyboardGlyphManagerTests.kt b/tests/Input/src/com/android/server/input/KeyboardGlyphManagerTests.kt
index c073c7aae678..ff8a9ba94353 100644
--- a/tests/Input/src/com/android/server/input/KeyboardGlyphManagerTests.kt
+++ b/tests/Input/src/com/android/server/input/KeyboardGlyphManagerTests.kt
@@ -23,9 +23,7 @@ import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
import android.content.pm.ServiceInfo
-import android.hardware.input.IInputManager
import android.hardware.input.InputManager
-import android.hardware.input.InputManagerGlobal
import android.hardware.input.KeyGlyphMap.KeyCombination
import android.os.Bundle
import android.os.test.TestLooper
@@ -36,8 +34,8 @@ import android.view.InputDevice
import android.view.KeyEvent
import androidx.test.core.app.ApplicationProvider
import com.android.hardware.input.Flags
+import com.android.test.input.MockInputManagerRule
import com.android.test.input.R
-import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
@@ -65,30 +63,24 @@ class KeyboardGlyphManagerTests {
const val RECEIVER_NAME = "DummyReceiver"
}
- @JvmField
- @Rule(order = 0)
+ @get:Rule
val setFlagsRule = SetFlagsRule()
-
- @JvmField
- @Rule(order = 1)
+ @get:Rule
val mockitoRule = MockitoJUnit.rule()!!
+ @get:Rule
+ val inputManagerRule = MockInputManagerRule()
@Mock
private lateinit var packageManager: PackageManager
- @Mock
- private lateinit var iInputManager: IInputManager
-
private lateinit var keyboardGlyphManager: KeyboardGlyphManager
private lateinit var context: Context
private lateinit var testLooper: TestLooper
- private lateinit var inputManagerGlobalSession: InputManagerGlobal.TestSession
private lateinit var keyboardDevice: InputDevice
@Before
fun setup() {
context = Mockito.spy(ContextWrapper(ApplicationProvider.getApplicationContext()))
- inputManagerGlobalSession = InputManagerGlobal.createTestSession(iInputManager)
testLooper = TestLooper()
keyboardGlyphManager = KeyboardGlyphManager(context, testLooper.looper)
@@ -98,21 +90,14 @@ class KeyboardGlyphManagerTests {
testLooper.dispatchAll()
}
- @After
- fun tearDown() {
- if (this::inputManagerGlobalSession.isInitialized) {
- inputManagerGlobalSession.close()
- }
- }
-
private fun setupInputDevices() {
val inputManager = InputManager(context)
Mockito.`when`(context.getSystemService(Mockito.eq(Context.INPUT_SERVICE)))
.thenReturn(inputManager)
keyboardDevice = createKeyboard(DEVICE_ID, VENDOR_ID, PRODUCT_ID, 0, "", "")
- Mockito.`when`(iInputManager.inputDeviceIds).thenReturn(intArrayOf(DEVICE_ID))
- Mockito.`when`(iInputManager.getInputDevice(DEVICE_ID)).thenReturn(keyboardDevice)
+ Mockito.`when`(inputManagerRule.mock.inputDeviceIds).thenReturn(intArrayOf(DEVICE_ID))
+ Mockito.`when`(inputManagerRule.mock.getInputDevice(DEVICE_ID)).thenReturn(keyboardDevice)
}
private fun setupBroadcastReceiver() {
diff --git a/tests/Input/src/com/android/server/input/KeyboardLayoutManagerTests.kt b/tests/Input/src/com/android/server/input/KeyboardLayoutManagerTests.kt
index 301c0e6a159f..d6654cceb458 100644
--- a/tests/Input/src/com/android/server/input/KeyboardLayoutManagerTests.kt
+++ b/tests/Input/src/com/android/server/input/KeyboardLayoutManagerTests.kt
@@ -24,11 +24,10 @@ import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
import android.content.pm.ServiceInfo
-import android.hardware.input.KeyboardLayoutSelectionResult
-import android.hardware.input.IInputManager
import android.hardware.input.InputManager
import android.hardware.input.InputManagerGlobal
import android.hardware.input.KeyboardLayout
+import android.hardware.input.KeyboardLayoutSelectionResult
import android.icu.util.ULocale
import android.os.Bundle
import android.os.test.TestLooper
@@ -42,8 +41,12 @@ import com.android.dx.mockito.inline.extended.ExtendedMockito
import com.android.internal.os.KeyboardConfiguredProto
import com.android.internal.util.FrameworkStatsLog
import com.android.modules.utils.testing.ExtendedMockitoRule
+import com.android.test.input.MockInputManagerRule
import com.android.test.input.R
-import org.junit.After
+import java.io.FileNotFoundException
+import java.io.FileOutputStream
+import java.io.IOException
+import java.io.InputStream
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
import org.junit.Assert.assertTrue
@@ -53,10 +56,6 @@ import org.junit.Test
import org.mockito.ArgumentMatchers
import org.mockito.Mock
import org.mockito.Mockito
-import java.io.FileNotFoundException
-import java.io.FileOutputStream
-import java.io.IOException
-import java.io.InputStream
fun createKeyboard(
deviceId: Int,
@@ -120,8 +119,8 @@ class KeyboardLayoutManagerTests {
val extendedMockitoRule = ExtendedMockitoRule.Builder(this)
.mockStatic(FrameworkStatsLog::class.java).build()!!
- @Mock
- private lateinit var iInputManager: IInputManager
+ @get:Rule
+ val inputManagerRule = MockInputManagerRule()
@Mock
private lateinit var native: NativeInputManagerService
@@ -148,7 +147,6 @@ class KeyboardLayoutManagerTests {
@Before
fun setup() {
context = Mockito.spy(ContextWrapper(ApplicationProvider.getApplicationContext()))
- inputManagerGlobalSession = InputManagerGlobal.createTestSession(iInputManager)
dataStore = PersistentDataStore(object : PersistentDataStore.Injector() {
override fun openRead(): InputStream? {
throw FileNotFoundException()
@@ -171,13 +169,6 @@ class KeyboardLayoutManagerTests {
setupIme()
}
- @After
- fun tearDown() {
- if (this::inputManagerGlobalSession.isInitialized) {
- inputManagerGlobalSession.close()
- }
- }
-
private fun setupInputDevices() {
val inputManager = InputManager(context)
Mockito.`when`(context.getSystemService(Mockito.eq(Context.INPUT_SERVICE)))
@@ -191,19 +182,19 @@ class KeyboardLayoutManagerTests {
DEFAULT_PRODUCT_ID, DEFAULT_DEVICE_BUS, "en", "dvorak")
englishQwertyKeyboardDevice = createKeyboard(ENGLISH_QWERTY_DEVICE_ID, DEFAULT_VENDOR_ID,
DEFAULT_PRODUCT_ID, DEFAULT_DEVICE_BUS, "en", "qwerty")
- Mockito.`when`(iInputManager.inputDeviceIds)
+ Mockito.`when`(inputManagerRule.mock.inputDeviceIds)
.thenReturn(intArrayOf(
DEVICE_ID,
VENDOR_SPECIFIC_DEVICE_ID,
ENGLISH_DVORAK_DEVICE_ID,
ENGLISH_QWERTY_DEVICE_ID
))
- Mockito.`when`(iInputManager.getInputDevice(DEVICE_ID)).thenReturn(keyboardDevice)
- Mockito.`when`(iInputManager.getInputDevice(VENDOR_SPECIFIC_DEVICE_ID))
+ Mockito.`when`(inputManagerRule.mock.getInputDevice(DEVICE_ID)).thenReturn(keyboardDevice)
+ Mockito.`when`(inputManagerRule.mock.getInputDevice(VENDOR_SPECIFIC_DEVICE_ID))
.thenReturn(vendorSpecificKeyboardDevice)
- Mockito.`when`(iInputManager.getInputDevice(ENGLISH_DVORAK_DEVICE_ID))
+ Mockito.`when`(inputManagerRule.mock.getInputDevice(ENGLISH_DVORAK_DEVICE_ID))
.thenReturn(englishDvorakKeyboardDevice)
- Mockito.`when`(iInputManager.getInputDevice(ENGLISH_QWERTY_DEVICE_ID))
+ Mockito.`when`(inputManagerRule.mock.getInputDevice(ENGLISH_QWERTY_DEVICE_ID))
.thenReturn(englishQwertyKeyboardDevice)
}
diff --git a/tests/Input/src/com/android/test/input/MockInputManagerRule.kt b/tests/Input/src/com/android/test/input/MockInputManagerRule.kt
new file mode 100644
index 000000000000..cef985600c40
--- /dev/null
+++ b/tests/Input/src/com/android/test/input/MockInputManagerRule.kt
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.test.input
+
+import android.hardware.input.IInputManager
+import android.hardware.input.InputManagerGlobal
+import org.junit.rules.ExternalResource
+import org.mockito.Mockito
+
+/**
+ * A test rule that temporarily replaces the [IInputManager] connection to the server with a mock
+ * to be used for testing.
+ */
+class MockInputManagerRule : ExternalResource() {
+
+ private lateinit var session: InputManagerGlobal.TestSession
+
+ val mock: IInputManager = Mockito.mock(IInputManager::class.java)
+
+ override fun before() {
+ session = InputManagerGlobal.createTestSession(mock)
+ }
+
+ override fun after() {
+ if (this::session.isInitialized) {
+ session.close()
+ }
+ }
+}