Revert "Verify IMS to get display and WM"

Revert submission 11823238-ims_ui_context

Reason for revert:
Broke the following tests:
android.os.cts.StrictModeTest#testIncorrectContextUse_GetDisplay
android.os.cts.StrictModeTest#testIncorrectContextUse_GetSystemService
android.os.cts.StrictModeTest#testIncorrectContextUse_GetViewConfiguration
But was submitted with a bypass.

Reverted Changes:
I688b46a92:Verify IMS to get display and WM
I172ceb2e1:Enable IMS and its config context to obtain UI com...

Bug: 157027563
Bug: 159795597
Change-Id: Iedbce852dc00127851b79dda7d0518a944027e18
diff --git a/tests/inputmethod/mockime/src/com/android/cts/mockime/ImeEventStreamTestUtils.java b/tests/inputmethod/mockime/src/com/android/cts/mockime/ImeEventStreamTestUtils.java
index 1b71ff0..f08633f 100644
--- a/tests/inputmethod/mockime/src/com/android/cts/mockime/ImeEventStreamTestUtils.java
+++ b/tests/inputmethod/mockime/src/com/android/cts/mockime/ImeEventStreamTestUtils.java
@@ -340,21 +340,4 @@
             throw new RuntimeException("notExpectEvent failed: " + stream.dump(), e);
         }
     }
-
-    /**
-     * Clear all events with  {@code eventName} in given {@code stream} and returns a forked
-     * {@link ImeEventStream} without events with {@code eventName}.
-     * <p>It is used to make sure previous events influence the test. </p>
-     *
-     * @param stream {@link ImeEventStream} to be cleared
-     * @param eventName The targeted cleared event name
-     * @return A forked {@link ImeEventStream} without event with {@code eventName}
-     */
-    public static ImeEventStream clearAllEvents(@NonNull ImeEventStream stream,
-            @NonNull String eventName) {
-        while (stream.seekToFirst(event -> eventName.equals(event.getEventName())).isPresent()) {
-            stream.skip(1);
-        }
-        return stream.copy();
-    }
 }
diff --git a/tests/inputmethod/mockime/src/com/android/cts/mockime/ImeSettings.java b/tests/inputmethod/mockime/src/com/android/cts/mockime/ImeSettings.java
index 6731e85..917f7b3 100644
--- a/tests/inputmethod/mockime/src/com/android/cts/mockime/ImeSettings.java
+++ b/tests/inputmethod/mockime/src/com/android/cts/mockime/ImeSettings.java
@@ -52,7 +52,6 @@
     private static final String INLINE_SUGGESTIONS_ENABLED = "InlineSuggestionsEnabled";
     private static final String INLINE_SUGGESTION_VIEW_CONTENT_DESC =
             "InlineSuggestionViewContentDesc";
-    private static final String STRICT_MODE_ENABLED = "StrictModeEnabled";
 
     @NonNull
     private final PersistableBundle mBundle;
@@ -128,10 +127,6 @@
         return mBundle.getString(INLINE_SUGGESTION_VIEW_CONTENT_DESC, defaultValue);
     }
 
-    public boolean isStrictModeEnabled() {
-        return mBundle.getBoolean(STRICT_MODE_ENABLED, false);
-    }
-
     static Bundle serializeToBundle(@NonNull String eventCallbackActionName,
             @Nullable Builder builder) {
         final Bundle result = new Bundle();
@@ -285,10 +280,5 @@
             return this;
         }
 
-        /** Sets whether to enable {@link android.os.StrictMode} or not. */
-        public Builder setStrictModeEnabled(boolean enabled) {
-            mBundle.putBoolean(STRICT_MODE_ENABLED, enabled);
-            return this;
-        }
     }
 }
diff --git a/tests/inputmethod/mockime/src/com/android/cts/mockime/MockIme.java b/tests/inputmethod/mockime/src/com/android/cts/mockime/MockIme.java
index a31420a..5be50de 100644
--- a/tests/inputmethod/mockime/src/com/android/cts/mockime/MockIme.java
+++ b/tests/inputmethod/mockime/src/com/android/cts/mockime/MockIme.java
@@ -36,7 +36,6 @@
 import android.os.Looper;
 import android.os.Process;
 import android.os.ResultReceiver;
-import android.os.StrictMode;
 import android.os.SystemClock;
 import android.text.TextUtils;
 import android.util.Log;
@@ -45,7 +44,6 @@
 import android.view.Gravity;
 import android.view.KeyEvent;
 import android.view.View;
-import android.view.ViewConfiguration;
 import android.view.Window;
 import android.view.WindowInsets;
 import android.view.WindowManager;
@@ -61,9 +59,9 @@
 import android.view.inputmethod.InputContentInfo;
 import android.view.inputmethod.InputMethod;
 import android.widget.FrameLayout;
-import android.widget.HorizontalScrollView;
 import android.widget.ImageView;
 import android.widget.LinearLayout;
+import android.widget.HorizontalScrollView;
 import android.widget.TextView;
 import android.widget.inline.InlinePresentationSpec;
 
@@ -290,7 +288,8 @@
                         return ImeEvent.RETURN_VALUE_UNAVAILABLE;
                     }
                     case "getDisplayId":
-                        return getDisplay().getDisplayId();
+                        return getSystemService(WindowManager.class)
+                                .getDefaultDisplay().getDisplayId();
                     case "verifyLayoutInflaterContext":
                         return getLayoutInflater().getContext() == this;
                     case "setHeight":
@@ -300,17 +299,6 @@
                     case "setInlineSuggestionsExtras":
                         mInlineSuggestionsExtras = command.getExtras();
                         return ImeEvent.RETURN_VALUE_UNAVAILABLE;
-                    case "verifyGetDisplay":
-                        Context configContext = createConfigurationContext(new Configuration());
-                        return getDisplay() != null && configContext.getDisplay() != null;
-                    case "verifyGetWindowManager":
-                        configContext = createConfigurationContext(new Configuration());
-                        return getSystemService(WindowManager.class) != null
-                                && configContext.getSystemService(WindowManager.class) != null;
-                    case "verifyGetViewConfiguration":
-                            configContext = createConfigurationContext(new Configuration());
-                            return ViewConfiguration.get(this) != null
-                                    && ViewConfiguration.get(configContext) != null;
                 }
             }
             return ImeEvent.RETURN_VALUE_UNAVAILABLE;
@@ -380,16 +368,6 @@
         mClientPackageName.set(mSettings.getClientPackageName());
         mImeEventActionName.set(mSettings.getEventCallbackActionName());
 
-        // TODO(b/159593676): consider to detect more violations
-        if (mSettings.isStrictModeEnabled()) {
-            StrictMode.setVmPolicy(
-                    new StrictMode.VmPolicy.Builder()
-                            .detectIncorrectContextUse()
-                            .penaltyLog()
-                            .build());
-            StrictMode.setViolationLogger(info -> getTracer().onStrictModeViolated(() -> {}));
-        }
-
         getTracer().onCreate(() -> {
             super.onCreate();
             mHandlerThread.start();
@@ -1081,10 +1059,5 @@
             return recordEventInternal("onInlineSuggestionsResponse", supplier::getAsBoolean,
                     arguments);
         }
-
-        void onStrictModeViolated(@NonNull Runnable runnable) {
-            final Bundle arguments = new Bundle();
-            recordEventInternal("onStrictModeViolated", runnable, arguments);
-        }
     }
 }
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 5a7d94a..699da4c 100644
--- a/tests/inputmethod/mockime/src/com/android/cts/mockime/MockImeSession.java
+++ b/tests/inputmethod/mockime/src/com/android/cts/mockime/MockImeSession.java
@@ -1006,19 +1006,4 @@
     public ImeCommand callSetInlineSuggestionsExtras(@NonNull Bundle bundle) {
         return callCommandInternal("setInlineSuggestionsExtras", bundle);
     }
-
-    @NonNull
-    public ImeCommand callVerifyGetDisplay() {
-        return callCommandInternal("verifyGetDisplay", new Bundle());
-    }
-
-    @NonNull
-    public ImeCommand callVerifyGetWindowManager() {
-        return callCommandInternal("verifyGetWindowManager", new Bundle());
-    }
-
-    @NonNull
-    public ImeCommand callVerifyGetViewConfiguration() {
-        return callCommandInternal("verifyGetViewConfiguration", new Bundle());
-    }
 }
diff --git a/tests/tests/os/Android.bp b/tests/tests/os/Android.bp
index e04630b..b118392 100644
--- a/tests/tests/os/Android.bp
+++ b/tests/tests/os/Android.bp
@@ -26,7 +26,6 @@
         "truth-prebuilt",
         "guava",
         "junit",
-        "CtsMockInputMethodLib"
     ],
     jni_uses_platform_apis: true,
     jni_libs: [
diff --git a/tests/tests/os/CtsOsTestCases.xml b/tests/tests/os/CtsOsTestCases.xml
index 7ce85d6..3718c59 100644
--- a/tests/tests/os/CtsOsTestCases.xml
+++ b/tests/tests/os/CtsOsTestCases.xml
@@ -22,15 +22,6 @@
         <option name="cleanup-apks" value="true" />
         <option name="test-file-name" value="CtsOsTestCases.apk" />
     </target_preparer>
-    <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
-        <option name="cleanup-apks" value="true" />
-        <option name="force-install-mode" value="FULL"/>
-        <option name="test-file-name" value="CtsMockInputMethod.apk" />
-    </target_preparer>
-    <target_preparer class="com.android.tradefed.targetprep.DeviceSetup">
-        <option name="screen-always-on" value="on" />
-    </target_preparer>
-
     <test class="com.android.tradefed.testtype.AndroidJUnitTest" >
         <option name="package" value="android.os.cts" />
         <option name="runtime-hint" value="3m15s" />
diff --git a/tests/tests/os/src/android/os/cts/SimpleTestActivity.java b/tests/tests/os/src/android/os/cts/SimpleTestActivity.java
index 22c706fe..42423156 100644
--- a/tests/tests/os/src/android/os/cts/SimpleTestActivity.java
+++ b/tests/tests/os/src/android/os/cts/SimpleTestActivity.java
@@ -16,30 +16,7 @@
 
 package android.os.cts;
 
-import static android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE;
-
 import android.app.Activity;
-import android.os.Bundle;
-import android.widget.EditText;
-import android.widget.LinearLayout;
 
 public class SimpleTestActivity extends Activity {
-    private EditText mEditText;
-
-    @Override
-    protected void onCreate(Bundle icicle) {
-        super.onCreate(icicle);
-        mEditText = new EditText(this);
-        final LinearLayout layout = new LinearLayout(this);
-        layout.setOrientation(LinearLayout.VERTICAL);
-        layout.addView(mEditText);
-        setContentView(layout);
-    }
-
-    @Override
-    protected void onResume() {
-        super.onResume();
-        getWindow().setSoftInputMode(SOFT_INPUT_STATE_ALWAYS_VISIBLE);
-        mEditText.requestFocus();
-    }
 }
\ No newline at end of file
diff --git a/tests/tests/os/src/android/os/cts/StrictModeTest.java b/tests/tests/os/src/android/os/cts/StrictModeTest.java
index b1023f6..8ed37ca 100644
--- a/tests/tests/os/src/android/os/cts/StrictModeTest.java
+++ b/tests/tests/os/src/android/os/cts/StrictModeTest.java
@@ -17,15 +17,9 @@
 package android.os.cts;
 
 import static android.content.Context.WINDOW_SERVICE;
-import static android.content.pm.PackageManager.FEATURE_INPUT_METHODS;
 import static android.view.Display.DEFAULT_DISPLAY;
 import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
 
-import static com.android.cts.mockime.ImeEventStreamTestUtils.clearAllEvents;
-import static com.android.cts.mockime.ImeEventStreamTestUtils.expectCommand;
-import static com.android.cts.mockime.ImeEventStreamTestUtils.expectEvent;
-import static com.android.cts.mockime.ImeEventStreamTestUtils.notExpectEvent;
-
 import static com.google.common.truth.Truth.assertThat;
 import static com.google.common.truth.Truth.assertWithMessage;
 
@@ -38,7 +32,7 @@
 import android.content.Intent;
 import android.content.ServiceConnection;
 import android.content.pm.PackageManager;
-import android.content.res.Configuration;
+import android.content.res.Resources;
 import android.hardware.display.DisplayManager;
 import android.inputmethodservice.InputMethodService;
 import android.net.TrafficStats;
@@ -69,16 +63,10 @@
 import android.view.ViewConfiguration;
 import android.view.WindowManager;
 
-import androidx.annotation.IntDef;
 import androidx.test.core.app.ApplicationProvider;
 import androidx.test.platform.app.InstrumentationRegistry;
 import androidx.test.runner.AndroidJUnit4;
 
-import com.android.cts.mockime.ImeEvent;
-import com.android.cts.mockime.ImeEventStream;
-import com.android.cts.mockime.ImeSettings;
-import com.android.cts.mockime.MockImeSession;
-
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -90,8 +78,6 @@
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
 import java.net.HttpURLConnection;
 import java.net.Socket;
 import java.net.URL;
@@ -110,49 +96,10 @@
 public class StrictModeTest {
     private static final String TAG = "StrictModeTest";
     private static final String REMOTE_SERVICE_ACTION = "android.app.REMOTESERVICE";
-    private static final long TIMEOUT = TimeUnit.SECONDS.toMillis(10); // 10 seconds
-    private static final long NOT_EXPECT_TIMEOUT = TimeUnit.SECONDS.toMillis(2);
 
     private StrictMode.ThreadPolicy mThreadPolicy;
     private StrictMode.VmPolicy mVmPolicy;
 
-    /**
-     * Verify mode to verifying if APIs violates incorrect context violation.
-     *
-     * @see #VERIFY_MODE_GET_DISPLAY
-     * @see #VERIFY_MODE_GET_WINDOW_MANAGER
-     * @see #VERIFY_MODE_GET_VIEW_CONFIGURATION
-     */
-    @Retention(RetentionPolicy.SOURCE)
-    @IntDef(flag = true, value = {
-            VERIFY_MODE_GET_DISPLAY,
-            VERIFY_MODE_GET_WINDOW_MANAGER,
-            VERIFY_MODE_GET_VIEW_CONFIGURATION,
-    })
-    private @interface VerifyMode {}
-
-    /**
-     * Verifies if {@link Context#getDisplay} from {@link InputMethodService} and context created
-     * from {@link InputMethodService#createConfigurationContext(Configuration)} violates
-     * incorrect context violation.
-     */
-    private static final int VERIFY_MODE_GET_DISPLAY = 1;
-    /**
-     * Verifies if get {@link android.view.WindowManager} from {@link InputMethodService} and
-     * context created from {@link InputMethodService#createConfigurationContext(Configuration)}
-     * violates incorrect context violation.
-     *
-     * @see Context#getSystemService(String)
-     * @see Context#getSystemService(Class)
-     */
-    private static final int VERIFY_MODE_GET_WINDOW_MANAGER = 2;
-    /**
-     * Verifies if passing {@link InputMethodService} and context created
-     * from {@link InputMethodService#createConfigurationContext(Configuration)} to
-     * {@link android.view.ViewConfiguration#get(Context)} violates incorrect context violation.
-     */
-    private static final int VERIFY_MODE_GET_VIEW_CONFIGURATION = 3;
-
     private Context getContext() {
         return ApplicationProvider.getApplicationContext();
     }
@@ -700,9 +647,6 @@
         final Activity activity = InstrumentationRegistry.getInstrumentation()
                 .startActivitySync(intent);
         assertNoViolation(() -> activity.getSystemService(WINDOW_SERVICE));
-
-        // TODO(b/159593676): move the logic to CtsInputMethodTestCases
-        verifyIms(VERIFY_MODE_GET_WINDOW_MANAGER);
     }
 
     @Test
@@ -725,13 +669,10 @@
 
         Intent intent = new Intent(getContext(), SimpleTestActivity.class);
         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-
         final Activity activity = InstrumentationRegistry.getInstrumentation()
                 .startActivitySync(intent);
         assertNoViolation(() -> activity.getDisplay());
 
-        // TODO(b/159593676): move the logic to CtsInputMethodTestCases
-        verifyIms(VERIFY_MODE_GET_DISPLAY);
         try {
             getContext().getApplicationContext().getDisplay();
         } catch (UnsupportedOperationException e) {
@@ -750,14 +691,14 @@
 
         final Context baseContext = getContext();
         assertViolation(
-                "Tried to access UI constants from a non-visual Context:",
+                "Tried to access UI constants from a non-visual Context.",
                 () -> ViewConfiguration.get(baseContext));
 
         final Display display = baseContext.getSystemService(DisplayManager.class)
                 .getDisplay(DEFAULT_DISPLAY);
         final Context displayContext = baseContext.createDisplayContext(display);
         assertViolation(
-                "Tried to access UI constants from a non-visual Context:",
+                "Tried to access UI constants from a non-visual Context.",
                 () -> ViewConfiguration.get(displayContext));
 
         final Context windowContext =
@@ -770,55 +711,14 @@
                 .startActivitySync(intent);
         assertNoViolation(() -> ViewConfiguration.get(activity));
 
-        // TODO(b/159593676): move the logic to CtsInputMethodTestCases
-        verifyIms(VERIFY_MODE_GET_VIEW_CONFIGURATION);
+        final TestInputMethodService ims = new TestInputMethodService(getContext());
+        assertNoViolation(() -> ViewConfiguration.get(ims));
     }
 
-    // TODO(b/159593676): move the logic to CtsInputMethodTestCases
-    /**
-     * Verify if APIs violates incorrect context violations by {@code mode}.
-     *
-     * @see VerifyMode
-     */
-    private void verifyIms(@VerifyMode int mode) throws Exception {
-        // If devices do not support installable IMEs, finish the test gracefully. We don't use
-        // assumeTrue here because we do pass some cases, so showing "pass" instead of "skip" makes
-        // sense here.
-        if (!supportsInstallableIme()) {
-            return;
+    private static class TestInputMethodService extends InputMethodService {
+        private TestInputMethodService(Context baseContext) {
+           attachBaseContext(baseContext);
         }
-
-        try (final MockImeSession imeSession = MockImeSession.create(getContext(),
-                InstrumentationRegistry.getInstrumentation().getUiAutomation(),
-                new ImeSettings.Builder().setStrictModeEnabled(true))) {
-            final ImeEventStream stream = imeSession.openEventStream();
-            expectEvent(stream, event -> "onStartInput".equals(event.getEventName()), TIMEOUT);
-            final ImeEventStream forkedStream = clearAllEvents(stream, "onStrictModeViolated");
-            final ImeEvent imeEvent;
-            switch (mode) {
-                case VERIFY_MODE_GET_DISPLAY:
-                    imeEvent = expectCommand(forkedStream, imeSession.callVerifyGetDisplay(),
-                            TIMEOUT);
-                    break;
-                case VERIFY_MODE_GET_WINDOW_MANAGER:
-                    imeEvent = expectCommand(forkedStream, imeSession.callVerifyGetWindowManager(),
-                            TIMEOUT);
-                    break;
-                case VERIFY_MODE_GET_VIEW_CONFIGURATION:
-                    imeEvent = expectCommand(forkedStream,
-                            imeSession.callVerifyGetViewConfiguration(), TIMEOUT);
-                    break;
-                default:
-                    imeEvent = null;
-            }
-            assertTrue(imeEvent.getReturnBooleanValue());
-            notExpectEvent(stream, event -> "onStrictModeViolated".equals(event.getEventName()),
-                    NOT_EXPECT_TIMEOUT);
-        }
-    }
-
-    private boolean supportsInstallableIme() {
-        return getContext().getPackageManager().hasSystemFeature(FEATURE_INPUT_METHODS);
     }
 
     private static void runWithRemoteServiceBound(Context context, Consumer<ISecondary> consumer)