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

Commit 3f9ef73c authored by Antonio Kantek's avatar Antonio Kantek Committed by Android (Google) Code Review
Browse files

Merge "Add tests for ClientController#verifyClientAndPackageMatch" into main

parents c38c1aab 0ea09d37
Loading
Loading
Loading
Loading
+39 −3
Original line number Diff line number Diff line
@@ -22,6 +22,8 @@ import static com.google.common.truth.Truth.assertWithMessage;

import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.anyLong;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -52,6 +54,7 @@ public final class ClientControllerTest {
    private static final int ANY_DISPLAY_ID = Display.DEFAULT_DISPLAY;
    private static final int ANY_CALLER_UID = 1;
    private static final int ANY_CALLER_PID = 1;
    private static final String SOME_PACKAGE_NAME = "some.package";

    @Rule
    public final RavenwoodRule mRavenwood = new RavenwoodRule.Builder()
@@ -80,7 +83,8 @@ public final class ClientControllerTest {
    }

    @Test
    // TODO(b/314150112): Enable host side mode for this test once b/315544364 is fixed.
    // TODO(b/314150112): Enable host side mode for this test once Ravenwood is enabled for
    //  inputmethod server classes.
    @IgnoreUnderRavenwood(blockedBy = {InputBinding.class, IInputMethodClientInvoker.class})
    public void testAddClient_cannotAddTheSameClientTwice() {
        var invoker = IInputMethodClientInvoker.create(mClient, mHandler);
@@ -102,7 +106,8 @@ public final class ClientControllerTest {
    }

    @Test
    // TODO(b/314150112): Enable host side mode for this test once b/315544364 is fixed.
    // TODO(b/314150112): Enable host side mode for this test once Ravenwood is enabled for
    //  inputmethod server classes.
    @IgnoreUnderRavenwood(blockedBy = {InputBinding.class, IInputMethodClientInvoker.class})
    public void testAddClient() throws Exception {
        synchronized (ImfLock.class) {
@@ -116,7 +121,8 @@ public final class ClientControllerTest {
    }

    @Test
    // TODO(b/314150112): Enable host side mode for this test once b/315544364 is fixed.
    // TODO(b/314150112): Enable host side mode for this test once Ravenwood is enabled for
    //  inputmethod server classes.
    @IgnoreUnderRavenwood(blockedBy = {InputBinding.class, IInputMethodClientInvoker.class})
    public void testRemoveClient() {
        var callback = new TestClientControllerCallback();
@@ -136,6 +142,36 @@ public final class ClientControllerTest {
        assertThat(removed).isSameInstanceAs(added);
    }

    @Test
    // TODO(b/314150112): Enable host side mode for this test once Ravenwood is enabled for
    //  inputmethod server classes and updated to newer Mockito with static mock support (mock
    //  InputMethodUtils#checkIfPackageBelongsToUid instead of PackageManagerInternal#isSameApp)
    @IgnoreUnderRavenwood(blockedBy = {InputMethodUtils.class})
    public void testVerifyClientAndPackageMatch() {
        when(mMockPackageManagerInternal.isSameApp(eq(SOME_PACKAGE_NAME),  /* flags= */
                anyLong(), eq(ANY_CALLER_UID), /* userId= */ anyInt())).thenReturn(true);

        synchronized (ImfLock.class) {
            var invoker = IInputMethodClientInvoker.create(mClient, mHandler);
            mController.addClient(invoker, mConnection, ANY_DISPLAY_ID, ANY_CALLER_UID,
                    ANY_CALLER_PID);
            assertThat(
                    mController.verifyClientAndPackageMatch(mClient, SOME_PACKAGE_NAME)).isTrue();
        }
    }

    @Test
    public void testVerifyClientAndPackageMatch_unknownClient() {
        synchronized (ImfLock.class) {
            assertThrows(IllegalArgumentException.class,
                    () -> {
                        synchronized (ImfLock.class) {
                            mController.verifyClientAndPackageMatch(mClient, SOME_PACKAGE_NAME);
                        }
                    });
        }
    }

    private static class TestClientControllerCallback implements ClientControllerCallback {

        private final CountDownLatch mLatch = new CountDownLatch(1);