1 /* 2 * Copyright 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.test.input 17 18 import android.hardware.input.IInputManager 19 import android.hardware.input.InputManagerGlobal 20 import org.junit.rules.ExternalResource 21 import org.mockito.Mockito 22 23 /** 24 * A test rule that temporarily replaces the [IInputManager] connection to the server with a mock to 25 * be used for testing. 26 */ 27 class MockInputManagerRule : ExternalResource() { 28 29 private lateinit var session: InputManagerGlobal.TestSession 30 31 val mock: IInputManager = Mockito.mock(IInputManager::class.java) 32 beforenull33 override fun before() { 34 session = InputManagerGlobal.createTestSession(mock) 35 } 36 afternull37 override fun after() { 38 if (this::session.isInitialized) { 39 session.close() 40 } 41 } 42 } 43