fix window focus race in BaseKeyListenerTest

Bug: 8000119
Change-Id: Ib82a8d93328111b1a369da5724e06e4736e48363
diff --git a/tests/tests/text/src/android/text/method/cts/BaseKeyListenerTest.java b/tests/tests/text/src/android/text/method/cts/BaseKeyListenerTest.java
index 2f629d6..a0e03f5 100644
--- a/tests/tests/text/src/android/text/method/cts/BaseKeyListenerTest.java
+++ b/tests/tests/text/src/android/text/method/cts/BaseKeyListenerTest.java
@@ -21,6 +21,7 @@
 
 import android.app.Activity;
 import android.app.Instrumentation;
+import android.cts.util.PollingCheck;
 import android.os.SystemClock;
 import android.test.ActivityInstrumentationTestCase2;
 import android.text.Editable;
@@ -54,6 +55,12 @@
         mActivity = getActivity();
         mInstrumentation = getInstrumentation();
         mTextView = (TextView) mActivity.findViewById(R.id.keylistener_textview);
+        new PollingCheck(1000) {
+            @Override
+            protected boolean check() {
+                return mTextView.hasWindowFocus();
+            }
+        }.run();
     }
 
     public void testBackspace() {