Make MockIME optional.

MockIME is not available in some devices, so we need to fall back to "native" IME,
otherwise the tests won't run.

Test: atest CtsAutoFillServiceTestCases:android.autofillservice.cts.DatasetFilteringTest#testFilter_usingKeyboard
Bug: 137963114

Change-Id: Iea8a903b6fba7917da2a88d8f4ec67120fc825d5
diff --git a/tests/autofillservice/src/android/autofillservice/cts/DatasetFilteringTest.java b/tests/autofillservice/src/android/autofillservice/cts/DatasetFilteringTest.java
index 86a8c7d..10df1cd 100644
--- a/tests/autofillservice/src/android/autofillservice/cts/DatasetFilteringTest.java
+++ b/tests/autofillservice/src/android/autofillservice/cts/DatasetFilteringTest.java
@@ -25,6 +25,8 @@
 import static com.android.cts.mockime.ImeEventStreamTestUtils.expectCommand;
 import static com.android.cts.mockime.ImeEventStreamTestUtils.expectEvent;
 
+import static org.junit.Assume.assumeTrue;
+
 import android.autofillservice.cts.CannedFillResponse.CannedDataset;
 import android.content.IntentSender;
 import android.os.Process;
@@ -172,12 +174,13 @@
 
     @Test
     public void testFilter_usingKeyboard() throws Exception {
+        final MockImeSession mockImeSession = sMockImeSessionRule.getMockImeSession();
+        assumeTrue("MockIME not available", mockImeSession != null);
+
         final String aa = "Two A's";
         final String ab = "A and B";
         final String b = "Only B";
 
-        final MockImeSession mockImeSession = sMockImeSessionRule.getMockImeSession();
-
         enableService();
 
         // Set expectations.
diff --git a/tests/inputmethod/mockime/src/com/android/cts/mockime/MockImeSession.java b/tests/inputmethod/mockime/src/com/android/cts/mockime/MockImeSession.java
index c63fe4a..5e653c6 100644
--- a/tests/inputmethod/mockime/src/com/android/cts/mockime/MockImeSession.java
+++ b/tests/inputmethod/mockime/src/com/android/cts/mockime/MockImeSession.java
@@ -48,6 +48,8 @@
 
 import com.android.compatibility.common.util.PollingCheck;
 
+import org.junit.AssumptionViolatedException;
+
 import java.io.IOException;
 import java.util.concurrent.TimeUnit;
 
@@ -238,8 +240,10 @@
             @NonNull Context context,
             @NonNull UiAutomation uiAutomation,
             @Nullable ImeSettings.Builder imeSettings) throws Exception {
-        assumeTrue("Device must support installable IMEs that implement InputMethodService API",
-                context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_INPUT_METHODS));
+        final String unavailabilityReason = getUnavailabilityReason(context);
+        if (unavailabilityReason != null) {
+            throw new AssumptionViolatedException(unavailabilityReason);
+        }
 
         final MockImeSession client = new MockImeSession(context, uiAutomation);
         client.initialize(imeSettings);
@@ -247,6 +251,19 @@
     }
 
     /**
+     * Checks if the {@link MockIme} can be used in this device.
+     *
+     * @return {@code null} if it can be used, or message describing why if it cannot.
+     */
+    @Nullable
+    public static String getUnavailabilityReason(@NonNull Context context) {
+        if (!context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_INPUT_METHODS)) {
+            return "Device must support installable IMEs that implement InputMethodService API";
+        }
+        return null;
+    }
+
+    /**
      * @return {@link ImeEventStream} object that stores events sent from {@link MockIme} since the
      *         session is created.
      */
diff --git a/tests/inputmethod/mockime/src/com/android/cts/mockime/MockImeSessionRule.java b/tests/inputmethod/mockime/src/com/android/cts/mockime/MockImeSessionRule.java
index 041d2b8..3eccac3 100644
--- a/tests/inputmethod/mockime/src/com/android/cts/mockime/MockImeSessionRule.java
+++ b/tests/inputmethod/mockime/src/com/android/cts/mockime/MockImeSessionRule.java
@@ -62,7 +62,12 @@
                 if (Log.isLoggable(TAG, Log.VERBOSE)) {
                     Log.v(TAG, "Creating MockImeSession on " + description.getDisplayName());
                 }
-                mMockImeSession = MockImeSession.create(mContext, mUiAutomation, mImeSettings);
+                final String errorMsg = MockImeSession.getUnavailabilityReason(mContext);
+                if (errorMsg != null) {
+                    Log.w(TAG, "Mock IME not available: " + errorMsg);
+                } else {
+                    mMockImeSession = MockImeSession.create(mContext, mUiAutomation, mImeSettings);
+                }
                 try {
                     base.evaluate();
                 } finally {