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

Commit fd646fe8 authored by Lan Wei's avatar Lan Wei
Browse files

Add unit tests for InputMethodManagerService

Injecting and mocking dependencies to InputMethodManagerService to make
it unit testable.

Add InputMethodManagerServiceTestBase base class to help unit test IMMS.

Add the first batch of unit tests in InputMethodManagerServiceWindowGainedFocusTest to cover the behavior of IMMS#startInputOrWindowGainedFocus(), which should help us catch any regressions caused by future changes to the code.

Bug: 240359838
Test: atest com.android.server.inputmethod.InputMethodManagerServiceWindowGainedFocusTest
Change-Id: Id61087f1e176738910f5acfe912f8682f727255c
parent f233145d
Loading
Loading
Loading
Loading
+38 −7
Original line number Diff line number Diff line
@@ -144,6 +144,7 @@ import android.view.inputmethod.InputMethodSubtype;
import android.window.ImeOnBackInvokedDispatcher;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.content.PackageMonitor;
import com.android.internal.infra.AndroidFuture;
import com.android.internal.inputmethod.DirectBootAwareness;
@@ -1592,8 +1593,13 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
        private final InputMethodManagerService mService;

        public Lifecycle(Context context) {
            this(context, new InputMethodManagerService(context));
        }

        public Lifecycle(
                Context context, @NonNull InputMethodManagerService inputMethodManagerService) {
            super(context);
            mService = new InputMethodManagerService(context);
            mService = inputMethodManagerService;
        }

        @Override
@@ -1668,12 +1674,25 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
    }

    public InputMethodManagerService(Context context) {
        this(context, null, null);
    }

    @VisibleForTesting
    InputMethodManagerService(
            Context context,
            @Nullable ServiceThread serviceThreadForTesting,
            @Nullable InputMethodBindingController bindingControllerForTesting) {
        mContext = context;
        mRes = context.getResources();
        // TODO(b/196206770): Disallow I/O on this thread. Currently it's needed for loading
        // additional subtypes in switchUserOnHandlerLocked().
        final ServiceThread thread = new ServiceThread(
                HANDLER_THREAD_NAME, Process.THREAD_PRIORITY_FOREGROUND, true /* allowIo */);
        final ServiceThread thread =
                serviceThreadForTesting != null
                        ? serviceThreadForTesting
                        : new ServiceThread(
                                HANDLER_THREAD_NAME,
                                Process.THREAD_PRIORITY_FOREGROUND,
                                true /* allowIo */);
        thread.start();
        mHandler = Handler.createAsync(thread.getLooper(), this);
        // Note: SettingsObserver doesn't register observers in its constructor.
@@ -1701,10 +1720,13 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub

        updateCurrentProfileIds();
        AdditionalSubtypeUtils.load(mAdditionalSubtypeMap, userId);
        mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
                mSettings, context);
        mSwitchingController =
                InputMethodSubtypeSwitchingController.createInstanceLocked(mSettings, context);
        mMenuController = new InputMethodMenuController(this);
        mBindingController = new InputMethodBindingController(this);
        mBindingController =
                bindingControllerForTesting != null
                        ? bindingControllerForTesting
                        : new InputMethodBindingController(this);
        mAutofillController = new AutofillSuggestionsController(this);
        mPreventImeStartupUnlessTextEditor = mRes.getBoolean(
                com.android.internal.R.bool.config_preventImeStartupUnlessTextEditor);
@@ -3673,6 +3695,7 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
        // UI for input.
        if (isTextEditor && editorInfo != null
                && shouldRestoreImeVisibility(windowToken, softInputMode)) {
            if (DEBUG) Slog.v(TAG, "Will show input to restore visibility");
            res = startInputUncheckedLocked(cs, inputContext, remoteAccessibilityInputConnection,
                    editorInfo, startInputFlags, startInputReason, unverifiedTargetSdkVersion,
                    imeDispatcher);
@@ -3719,11 +3742,17 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
                                imeDispatcher);
                        didStart = true;
                    }
                    showCurrentInputLocked(windowToken, InputMethodManager.SHOW_IMPLICIT, null,
                    showCurrentInputLocked(
                            windowToken,
                            InputMethodManager.SHOW_IMPLICIT,
                            null,
                            SoftInputShowHideReason.SHOW_AUTO_EDITOR_FORWARD_NAV);
                }
                break;
            case LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
                if (DEBUG) {
                    Slog.v(TAG, "Window asks to keep the input in whatever state it was last in");
                }
                // Do nothing.
                break;
            case LayoutParams.SOFT_INPUT_STATE_HIDDEN:
@@ -3794,6 +3823,7 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
                    // To maintain compatibility, we are now hiding the IME when we don't have
                    // an editor upon refocusing a window.
                    if (startInputByWinGainedFocus) {
                        if (DEBUG) Slog.v(TAG, "Same window without editor will hide input");
                        hideCurrentInputLocked(mCurFocusedWindow, 0, null,
                                SoftInputShowHideReason.HIDE_SAME_WINDOW_FOCUSED_WITHOUT_EDITOR);
                    }
@@ -3807,6 +3837,7 @@ public final class InputMethodManagerService extends IInputMethodManager.Stub
                    // 1) SOFT_INPUT_STATE_UNCHANGED state without an editor
                    // 2) SOFT_INPUT_STATE_VISIBLE state without an editor
                    // 3) SOFT_INPUT_STATE_ALWAYS_VISIBLE state without an editor
                    if (DEBUG) Slog.v(TAG, "Window without editor will hide input");
                    hideCurrentInputLocked(mCurFocusedWindow, 0, null,
                            SoftInputShowHideReason.HIDE_WINDOW_GAINED_FOCUS_WITHOUT_EDITOR);
                }
+62 −0
Original line number Diff line number Diff line
// Copyright (C) 2022 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 {
    // See: http://go/android-license-faq
    // A large-scale-change added 'default_applicable_licenses' to import
    // all of the 'license_kinds' from "frameworks_base_license"
    // to get the below license kinds:
    //   SPDX-license-identifier-Apache-2.0
    default_applicable_licenses: ["frameworks_base_license"],
}

android_test {
    name: "FrameworksInputMethodSystemServerTests",
    defaults: [
        "modules-utils-testable-device-config-defaults",
    ],

    srcs: [
        "src/**/*.java",
    ],

    static_libs: [
        "androidx.test.core",
        "androidx.test.runner",
        "androidx.test.espresso.core",
        "androidx.test.espresso.contrib",
        "androidx.test.ext.truth",
        "frameworks-base-testutils",
        "mockito-target-extended-minus-junit4",
        "platform-test-annotations",
        "services.core",
        "servicestests-core-utils",
        "servicestests-utils-mockito-extended",
        "truth-prebuilt",
    ],

    libs: [
        "android.test.mock",
        "android.test.base",
        "android.test.runner",
    ],

    certificate: "platform",
    platform_apis: true,
    test_suites: ["device-tests"],

    optimize: {
        enabled: false,
    },
}
+42 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2022 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.
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.android.frameworks.inputmethodtests">

    <uses-sdk android:targetSdkVersion="31" />

    <!-- Permissions required for granting and logging -->
    <uses-permission android:name="android.permission.LOG_COMPAT_CHANGE"/>
    <uses-permission android:name="android.permission.READ_COMPAT_CHANGE_CONFIG"/>
    <uses-permission android:name="android.permission.OVERRIDE_COMPAT_CHANGE_CONFIG"/>
    <uses-permission android:name="android.permission.OVERRIDE_COMPAT_CHANGE_CONFIG_ON_RELEASE_BUILD"/>

    <!-- Permissions for reading system info -->
    <uses-permission android:name="android.permission.READ_DEVICE_CONFIG" />
    <uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" />

    <application android:testOnly="true"
                 android:debuggable="true">
        <uses-library android:name="android.test.runner" />
    </application>

    <instrumentation
        android:name="androidx.test.runner.AndroidJUnitRunner"
        android:targetPackage="com.android.frameworks.inputmethodtests"
        android:label="Frameworks InputMethod System Service Tests" />

</manifest>
+39 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2022 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.
-->
<configuration description="Runs Frameworks InputMethod System Services Tests.">
    <option name="test-suite-tag" value="apct" />
    <option name="test-suite-tag" value="apct-instrumentation" />

    <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
        <option name="cleanup-apks" value="true" />
        <option name="install-arg" value="-t" />
        <option name="test-file-name" value="FrameworksInputMethodSystemServerTests.apk" />
    </target_preparer>

    <option name="test-tag" value="FrameworksInputMethodSystemServerTests" />

    <test class="com.android.tradefed.testtype.AndroidJUnitTest" >
        <option name="package" value="com.android.frameworks.inputmethodtests" />
        <option name="runner" value="androidx.test.runner.AndroidJUnitRunner" />
        <option name="hidden-api-checks" value="false"/>
    </test>

    <!-- Collect the files in the dump directory for debugging -->
    <metrics_collector class="com.android.tradefed.device.metric.FilePullerLogCollector">
        <option name="directory-keys" value="/sdcard/FrameworksInputMethodSystemServerTests/" />
        <option name="collect-on-run-ended-only" value="true" />
    </metrics_collector>
</configuration>
+1 −0
Original line number Diff line number Diff line
include /services/core/java/com/android/server/inputmethod/OWNERS
Loading