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

Commit ed465be2 authored by Cosmin Băieș's avatar Cosmin Băieș
Browse files

Include inputViewShown in verifyInputViewStatus

Currently verifyInputViewStatus includes a few checks, but does not
incldue checking isInputViewShown. This refactors the method to also
check this value, to reduce duplication.

Additionally re-orders the methods in TestActivity and
InputMethodServiceWrapper, as well as storing the static instance of the
wrapper in a weak reference.

Flag: EXEMPT testfix
Bug: 394328311
Test: atest InputMethodServiceTest
Change-Id: Idd844caadedec49b0165d3cf4ed6ca3e61638783
parent 3158d159
Loading
Loading
Loading
Loading
+122 −265

File changed.

Preview size limit exceeded, changes collapsed.

+34 −33
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import androidx.annotation.Nullable;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.ref.WeakReference;
import java.util.concurrent.CountDownLatch;

/** Wrapper of {@link InputMethodService} to expose interfaces for testing purpose. */
@@ -35,8 +36,8 @@ public class InputMethodServiceWrapper extends InputMethodService {
    private static final String TAG = "InputMethodServiceWrapper";

    /** Last created instance of this wrapper. */
    @Nullable
    private static InputMethodServiceWrapper sInstance;
    @NonNull
    private static WeakReference<InputMethodServiceWrapper> sInstance = new WeakReference<>(null);

    /** IME show event ({@link #onStartInputView}). */
    public static final int EVENT_SHOW = 0;
@@ -68,32 +69,11 @@ public class InputMethodServiceWrapper extends InputMethodService {
    @Nullable
    private CountDownLatch mCountDownLatch;

    /** Gets the last created instance of this wrapper, if available. */
    @Nullable
    public static InputMethodServiceWrapper getInstance() {
        return sInstance;
    }

    public boolean getCurrentInputViewStarted() {
        return mInputViewStarted;
    }

    /**
     * Sets the latch used to wait for the IME event.
     *
     * @param latch      the latch to wait on.
     * @param latchEvent the event to set the latch on.
     */
    public void setCountDownLatchForTesting(@Nullable CountDownLatch latch, @Event int latchEvent) {
        mCountDownLatch = latch;
        mLatchEvent = latchEvent;
    }

    @Override
    public void onCreate() {
        Log.i(TAG, "onCreate()");
        super.onCreate();
        sInstance = this;
        sInstance = new WeakReference<>(this);
    }

    @Override
@@ -102,6 +82,12 @@ public class InputMethodServiceWrapper extends InputMethodService {
        super.onStartInput(info, restarting);
    }

    @Override
    public void onFinishInput() {
        Log.i(TAG, "onFinishInput()");
        super.onFinishInput();
    }

    @Override
    public void onStartInputView(EditorInfo info, boolean restarting) {
        Log.i(TAG, "onStartInputView() editor=" + dumpEditorInfo(info)
@@ -113,12 +99,6 @@ public class InputMethodServiceWrapper extends InputMethodService {
        }
    }

    @Override
    public void onFinishInput() {
        Log.i(TAG, "onFinishInput()");
        super.onFinishInput();
    }

    @Override
    public void onFinishInputView(boolean finishingInput) {
        Log.i(TAG, "onFinishInputView()");
@@ -146,14 +126,35 @@ public class InputMethodServiceWrapper extends InputMethodService {
        }
    }

    public boolean getCurrentInputViewStarted() {
        return mInputViewStarted;
    }

    /**
     * Sets the latch used to wait for the IME event.
     *
     * @param latch      the latch to wait on.
     * @param latchEvent the event to set the latch on.
     */
    public void setCountDownLatchForTesting(@Nullable CountDownLatch latch, @Event int latchEvent) {
        mCountDownLatch = latch;
        mLatchEvent = latchEvent;
    }

    /** Gets the last created instance of this wrapper, if available. */
    @Nullable
    public static InputMethodServiceWrapper getInstance() {
        return sInstance.get();
    }

    /**
     * Gets the string representation of the IME event that is being waited on.
     *
     * @param event the IME event.
     * @param eventType the IME event type.
     */
    @NonNull
    public static String eventToString(@Event int event) {
        return switch (event) {
    public static String eventToString(@Event int eventType) {
        return switch (eventType) {
            case EVENT_SHOW -> "onStartInputView";
            case EVENT_HIDE -> "onFinishInputView";
            case EVENT_CONFIG -> "onConfigurationChanged";
+27 −24
Original line number Diff line number Diff line
@@ -45,30 +45,13 @@ import java.lang.ref.WeakReference;
public final class TestActivity extends Activity {

    private static final String TAG = "TestActivity";
    private static WeakReference<TestActivity> sLastCreatedInstance = new WeakReference<>(null);

    /**
     * Start a new test activity with an editor and wait for it to begin running before returning.
     *
     * @param instrumentation application instrumentation
     * @return the newly started activity
     */
    /** Last created instance of this activity. */
    @NonNull
    public static TestActivity startSync(@NonNull Instrumentation instrumentation) {
        final var intent = new Intent()
                .setAction(Intent.ACTION_MAIN)
                .setClass(instrumentation.getTargetContext(), TestActivity.class)
                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        return (TestActivity) instrumentation.startActivitySync(intent);
    }
    private static WeakReference<TestActivity> sInstance = new WeakReference<>(null);

    private EditText mEditText;

    public EditText getEditText() {
        return mEditText;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
@@ -80,13 +63,11 @@ public final class TestActivity extends Activity {
        rootView.setFitsSystemWindows(true);
        setContentView(rootView);
        mEditText.requestFocus();
        sLastCreatedInstance = new WeakReference<>(this);
        sInstance = new WeakReference<>(this);
    }

    /** Get the last created TestActivity instance, if available. */
    @Nullable
    public static TestActivity getLastCreatedInstance() {
        return sLastCreatedInstance.get();
    public EditText getEditText() {
        return mEditText;
    }

    /** Shows soft keyboard via InputMethodManager. */
@@ -118,4 +99,26 @@ public final class TestActivity extends Activity {
        controller.hide(WindowInsets.Type.ime());
        Log.i(TAG, "hideIme() via WindowInsetsController");
    }

    /** Gets the last created instance of this activity, if available. */
    @Nullable
    public static TestActivity getInstance() {
        return sInstance.get();
    }

    /**
     * Start a new test activity with an editor and wait for it to begin running before returning.
     *
     * @param instrumentation application instrumentation.
     * @return the newly started activity.
     */
    @NonNull
    public static TestActivity startSync(@NonNull Instrumentation instrumentation) {
        final var intent = new Intent()
                .setAction(Intent.ACTION_MAIN)
                .setClass(instrumentation.getTargetContext(), TestActivity.class)
                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        return (TestActivity) instrumentation.startActivitySync(intent);
    }
}