Try to improve ViewTest#testInputMethodConnection

Bug 6292690

Change-Id: Ib3a0c585b7a4f4df5ecd1f536cfe222c2d2ceb96
diff --git a/tests/tests/view/src/android/view/cts/ViewTest.java b/tests/tests/view/src/android/view/cts/ViewTest.java
index 04ccc09..e636451 100644
--- a/tests/tests/view/src/android/view/cts/ViewTest.java
+++ b/tests/tests/view/src/android/view/cts/ViewTest.java
@@ -92,7 +92,7 @@
     private Activity mActivity;
 
     /** timeout delta when wait in case the system is sluggish */
-    private static final long TIMEOUT_DELTA = 1000;
+    private static final long TIMEOUT_DELTA = 10000;
 
     private static final String LOG_TAG = "ViewTest";
 
@@ -3072,38 +3072,48 @@
         assertTrue(view.performHapticFeedback(LONG_PRESS, FLAG_IGNORE_GLOBAL_SETTING));
     }
 
-    @UiThreadTest
-    public void testInputConnection() {
+    public void testInputConnection() throws Throwable {
         final InputMethodManager imm = InputMethodManager.getInstance(getActivity());
         final MockView view = (MockView) mActivity.findViewById(R.id.mock_view);
         final ViewGroup viewGroup = (ViewGroup) mActivity.findViewById(R.id.viewlayout_root);
         final MockEditText editText = new MockEditText(mActivity);
 
-        viewGroup.addView(editText);
-        editText.requestFocus();
+        runTestOnUiThread(new Runnable() {
+            @Override
+            public void run() {
+                viewGroup.addView(editText);
+                editText.requestFocus();
+            }
+        });
+        getInstrumentation().waitForIdleSync();
+        assertTrue(editText.isFocused());
+
+        runTestOnUiThread(new Runnable() {
+            @Override
+            public void run() {
+                imm.showSoftInput(editText, 0);
+            }
+        });
+        getInstrumentation().waitForIdleSync();
 
         new PollingCheck(TIMEOUT_DELTA) {
             @Override
             protected boolean check() {
-                return editText.isFocused();
-            }
-        }.run();
-
-        imm.showSoftInput(editText, 0);
-
-        new PollingCheck() {
-            @Override
-            protected boolean check() {
                 return editText.hasCalledOnCreateInputConnection();
             }
         }.run();
 
         assertTrue(editText.hasCalledOnCheckIsTextEditor());
-        assertTrue(imm.isActive(editText));
 
-        assertFalse(editText.hasCalledCheckInputConnectionProxy());
-        imm.isActive(view);
-        assertTrue(editText.hasCalledCheckInputConnectionProxy());
+        runTestOnUiThread(new Runnable() {
+            @Override
+            public void run() {
+                assertTrue(imm.isActive(editText));
+                assertFalse(editText.hasCalledCheckInputConnectionProxy());
+                imm.isActive(view);
+                assertTrue(editText.hasCalledCheckInputConnectionProxy());
+            }
+        });
     }
 
     @UiThreadTest