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

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

Merge changes I5291d5c7,Ifb91cf9a,Ie064d77f into main

* changes:
  Use a RAII-style test session to ensure input tests are hermetic
  Synchronize InputManagerGlobal static instance correctly
  Move framework input tests into the InputTests module
parents 4437eec2 948b56c0
Loading
Loading
Loading
Loading
+16 −11
Original line number Diff line number Diff line
@@ -54,6 +54,7 @@ import android.view.InputMonitor;
import android.view.PointerIcon;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.os.SomeArgs;

import java.util.ArrayList;
@@ -140,23 +141,27 @@ public final class InputManagerGlobal {
    }

    /**
     * Gets an instance of the input manager.
     *
     * @return The input manager instance.
     * A test session tracker for InputManagerGlobal.
     * @see #createTestSession(IInputManager)
     */
    public static InputManagerGlobal resetInstance(IInputManager inputManagerService) {
        synchronized (InputManager.class) {
            sInstance = new InputManagerGlobal(inputManagerService);
            return sInstance;
        }
    @VisibleForTesting
    public interface TestSession extends AutoCloseable {
        @Override
        void close();
    }

    /**
     * Clear the instance of the input manager.
     * Create and set a test instance of InputManagerGlobal.
     *
     * @return The test session. The session must be {@link TestSession#close()}-ed at the end
     * of the test.
     */
    public static void clearInstance() {
    @VisibleForTesting
    public static TestSession createTestSession(IInputManager inputManagerService) {
        synchronized (InputManagerGlobal.class) {
            sInstance = null;
            final var oldInstance = sInstance;
            sInstance = new InputManagerGlobal(inputManagerService);
            return () -> sInstance = oldInstance;
        }
    }

+6 −1
Original line number Diff line number Diff line
@@ -126,6 +126,7 @@ public class InputMethodManagerServiceTestBase {
    protected InputMethodManagerService mInputMethodManagerService;
    protected ServiceThread mServiceThread;
    protected boolean mIsLargeScreen;
    private InputManagerGlobal.TestSession mInputManagerGlobalSession;

    @BeforeClass
    public static void setupClass() {
@@ -186,7 +187,7 @@ public class InputMethodManagerServiceTestBase {

        // Injecting and mocked InputMethodBindingController and InputMethod.
        mMockInputMethodInvoker = IInputMethodInvoker.create(mMockInputMethod);
        InputManagerGlobal.resetInstance(mMockIInputManager);
        mInputManagerGlobalSession = InputManagerGlobal.createTestSession(mMockIInputManager);
        synchronized (ImfLock.class) {
            when(mMockInputMethodBindingController.getCurMethod())
                    .thenReturn(mMockInputMethodInvoker);
@@ -262,6 +263,10 @@ public class InputMethodManagerServiceTestBase {
        if (mMockingSession != null) {
            mMockingSession.finishMocking();
        }

        if (mInputManagerGlobalSession != null) {
            mInputManagerGlobalSession.close();
        }
        LocalServices.removeServiceForTest(InputMethodManagerInternal.class);
    }

+6 −0
Original line number Diff line number Diff line
@@ -44,6 +44,7 @@ import androidx.test.InstrumentationRegistry;
import com.android.server.LocalServices;
import com.android.server.input.InputManagerInternal;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -93,6 +94,11 @@ public class InputControllerTest {
                threadVerifier);
    }

    @After
    public void tearDown() {
        mInputManagerMockHelper.tearDown();
    }

    @Test
    public void registerInputDevice_deviceCreation_hasDeviceId() {
        final IBinder device1Token = new Binder("device1");
+8 −1
Original line number Diff line number Diff line
@@ -46,6 +46,7 @@ class InputManagerMockHelper {
    private final TestableLooper mTestableLooper;
    private final InputController.NativeWrapper mNativeWrapperMock;
    private final IInputManager mIInputManagerMock;
    private final InputManagerGlobal.TestSession mInputManagerGlobalSession;
    private final List<InputDevice> mDevices = new ArrayList<>();
    private IInputDevicesChangedListener mDevicesChangedListener;

@@ -77,7 +78,13 @@ class InputManagerMockHelper {

        // Set a new instance of InputManager for testing that uses the IInputManager mock as the
        // interface to the server.
        InputManagerGlobal.resetInstance(mIInputManagerMock);
        mInputManagerGlobalSession = InputManagerGlobal.createTestSession(mIInputManagerMock);
    }

    public void tearDown() {
        if (mInputManagerGlobalSession != null) {
            mInputManagerGlobalSession.close();
        }
    }

    private long handleNativeOpenInputDevice(InvocationOnMock inv) {
+1 −0
Original line number Diff line number Diff line
@@ -376,6 +376,7 @@ public class VirtualDeviceManagerServiceTest {
    @After
    public void tearDown() {
        mDeviceImpl.close();
        mInputManagerMockHelper.tearDown();
    }

    @Test
Loading