Fix InputMethodManager Related Tests

Bug 6158858

These failures have come up several times. Move requestFocus into
runTestOnUiThread runnable and wait to sync rather than immediately
asserting that the view is focused afterwards. This same fix was
applied to many tests in HC when the ActionBar started taking
focus by default and requestFocus has to be added to many tests.

Change-Id: I96ed7aaa305010e13526b28bea7cb3d3e23c305c
diff --git a/tests/tests/view/src/android/view/inputmethod/cts/InputMethodManagerTest.java b/tests/tests/view/src/android/view/inputmethod/cts/InputMethodManagerTest.java
index 92ef3fe..f997c13 100755
--- a/tests/tests/view/src/android/view/inputmethod/cts/InputMethodManagerTest.java
+++ b/tests/tests/view/src/android/view/inputmethod/cts/InputMethodManagerTest.java
@@ -28,7 +28,6 @@
 import android.os.IBinder;
 import android.os.ResultReceiver;
 import android.test.ActivityInstrumentationTestCase2;
-import android.test.UiThreadTest;
 import android.view.KeyEvent;
 import android.view.View;
 import android.view.Window;
@@ -146,16 +145,21 @@
             args = {IBinder.class, int.class, int.class}
         )
     })
-    @UiThreadTest
-    public void testInputMethodManager() {
+    public void testInputMethodManager() throws Throwable {
         Window window = mActivity.getWindow();
-        EditText view = (EditText) window.findViewById(R.id.entry);
-        assertTrue(view.requestFocus());
+        final EditText view = (EditText) window.findViewById(R.id.entry);
+        runTestOnUiThread(new Runnable() {
+           @Override
+            public void run() {
+               view.requestFocus();
+            }
+        });
+        getInstrumentation().waitForIdleSync();
         assertTrue(view.isFocused());
 
         BaseInputConnection connection = new BaseInputConnection(view, false);
         Context context = mInstrumentation.getTargetContext();
-        InputMethodManager imManager = (InputMethodManager) context
+        final InputMethodManager imManager = (InputMethodManager) context
                 .getSystemService(Context.INPUT_METHOD_SERVICE);
         assertTrue(imManager.isActive());
         assertTrue(imManager.isAcceptingText());
@@ -166,34 +170,40 @@
         connection.reportFullscreenMode(true);
         assertTrue(imManager.isFullscreenMode());
 
-        IBinder token = view.getWindowToken();
+        runTestOnUiThread(new Runnable() {
+            @Override
+            public void run() {
+                IBinder token = view.getWindowToken();
 
-        // Show and hide input method.
-        assertTrue(imManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT));
-        assertTrue(imManager.hideSoftInputFromWindow(token, 0));
+                // Show and hide input method.
+                assertTrue(imManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT));
+                assertTrue(imManager.hideSoftInputFromWindow(token, 0));
 
-        Handler handler = new Handler();
-        ResultReceiver receiver = new ResultReceiver(handler);
-        assertTrue(imManager.showSoftInput(view, 0, receiver));
-        receiver = new ResultReceiver(handler);
-        assertTrue(imManager.hideSoftInputFromWindow(token, 0, receiver));
+                Handler handler = new Handler();
+                ResultReceiver receiver = new ResultReceiver(handler);
+                assertTrue(imManager.showSoftInput(view, 0, receiver));
+                receiver = new ResultReceiver(handler);
+                assertTrue(imManager.hideSoftInputFromWindow(token, 0, receiver));
 
-        imManager.showSoftInputFromInputMethod(token, InputMethodManager.SHOW_FORCED);
-        imManager.hideSoftInputFromInputMethod(token, InputMethodManager.HIDE_NOT_ALWAYS);
+                imManager.showSoftInputFromInputMethod(token, InputMethodManager.SHOW_FORCED);
+                imManager.hideSoftInputFromInputMethod(token, InputMethodManager.HIDE_NOT_ALWAYS);
 
-        // status: hide to show to hide
-        imManager.toggleSoftInputFromWindow(token, 0, InputMethodManager.HIDE_NOT_ALWAYS);
-        imManager.toggleSoftInputFromWindow(token, 0, InputMethodManager.HIDE_NOT_ALWAYS);
+                // status: hide to show to hide
+                imManager.toggleSoftInputFromWindow(token, 0, InputMethodManager.HIDE_NOT_ALWAYS);
+                imManager.toggleSoftInputFromWindow(token, 0, InputMethodManager.HIDE_NOT_ALWAYS);
 
-        List<InputMethodInfo> enabledImList = imManager.getEnabledInputMethodList();
-        if (enabledImList != null && enabledImList.size() > 0) {
-            imManager.setInputMethod(token, enabledImList.get(0).getId());
-            // cannot test whether setting was successful
-        }
+                List<InputMethodInfo> enabledImList = imManager.getEnabledInputMethodList();
+                if (enabledImList != null && enabledImList.size() > 0) {
+                    imManager.setInputMethod(token, enabledImList.get(0).getId());
+                    // cannot test whether setting was successful
+                }
 
-        List<InputMethodInfo> imList = imManager.getInputMethodList();
-        if (imList != null && enabledImList != null) {
-            assertTrue(imList.size() >= enabledImList.size());
-        }
+                List<InputMethodInfo> imList = imManager.getInputMethodList();
+                if (imList != null && enabledImList != null) {
+                    assertTrue(imList.size() >= enabledImList.size());
+                }
+            }
+        });
+        getInstrumentation().waitForIdleSync();
     }
 }
diff --git a/tests/tests/widget/src/android/widget/cts/TextViewTest.java b/tests/tests/widget/src/android/widget/cts/TextViewTest.java
index c714d8a..a091c05 100755
--- a/tests/tests/widget/src/android/widget/cts/TextViewTest.java
+++ b/tests/tests/widget/src/android/widget/cts/TextViewTest.java
@@ -19,8 +19,6 @@
 import com.android.cts.stub.R;
 import com.android.internal.util.FastMath;
 
-import android.graphics.Path;
-import android.graphics.RectF;
 import dalvik.annotation.TestLevel;
 import dalvik.annotation.TestTargetClass;
 import dalvik.annotation.TestTargetNew;
@@ -38,7 +36,9 @@
 import android.graphics.Bitmap;
 import android.graphics.Color;
 import android.graphics.Paint;
+import android.graphics.Path;
 import android.graphics.Rect;
+import android.graphics.RectF;
 import android.graphics.Typeface;
 import android.graphics.drawable.BitmapDrawable;
 import android.graphics.drawable.Drawable;
@@ -56,8 +56,8 @@
 import android.text.SpannableString;
 import android.text.TextPaint;
 import android.text.TextUtils;
-import android.text.TextWatcher;
 import android.text.TextUtils.TruncateAt;
+import android.text.TextWatcher;
 import android.text.method.ArrowKeyMovementMethod;
 import android.text.method.DateKeyListener;
 import android.text.method.DateTimeKeyListener;
@@ -70,22 +70,22 @@
 import android.text.method.QwertyKeyListener;
 import android.text.method.SingleLineTransformationMethod;
 import android.text.method.TextKeyListener;
+import android.text.method.TextKeyListener.Capitalize;
 import android.text.method.TimeKeyListener;
 import android.text.method.TransformationMethod;
-import android.text.method.TextKeyListener.Capitalize;
 import android.text.style.URLSpan;
 import android.text.style.cts.MockURLSpanTestActivity;
 import android.text.util.Linkify;
 import android.util.DisplayMetrics;
 import android.util.TypedValue;
 import android.view.ContextMenu;
+import android.view.ContextMenu.ContextMenuInfo;
 import android.view.Gravity;
 import android.view.KeyEvent;
 import android.view.View;
-import android.view.ViewGroup;
-import android.view.ContextMenu.ContextMenuInfo;
 import android.view.View.OnCreateContextMenuListener;
 import android.view.View.OnLongClickListener;
+import android.view.ViewGroup;
 import android.view.animation.cts.DelayedCheck;
 import android.view.inputmethod.BaseInputConnection;
 import android.view.inputmethod.EditorInfo;
@@ -3972,16 +3972,21 @@
         method = "isInputMethodTarget",
         args = {}
     )
-    @UiThreadTest
-    public void testIsInputMethodTarget() {
+    public void testIsInputMethodTarget() throws Throwable {
         mTextView = findTextView(R.id.textview_text);
         assertFalse(mTextView.isInputMethodTarget());
 
         assertFalse(mTextView.isFocused());
-        mTextView.setFocusable(true);
-        mTextView.requestFocus();
-        assertTrue(mTextView.isFocused());
+        runTestOnUiThread(new Runnable() {
+           @Override
+            public void run() {
+               mTextView.setFocusable(true);
+               mTextView.requestFocus();
+            }
+        });
+        mInstrumentation.waitForIdleSync();
 
+        assertTrue(mTextView.isFocused());
         assertTrue(mTextView.isInputMethodTarget());
     }