short-term fix for flaky CTS tests that need focus

A number of the tests in cts-text are flaky because a number of their
operations require window focus, but the frameworks give no guarantee
that an Activity has window focus at any given point in the Activity
lifecycle.

This commit addresses the issue by having the affected tests block in
setUp() until window focus has been obtained.

Bug: 9054507
Change-Id: I922c3649d7f378e7d3a7560937919f1b48713a59
diff --git a/tests/tests/text/src/android/text/method/cts/DateKeyListenerTest.java b/tests/tests/text/src/android/text/method/cts/DateKeyListenerTest.java
index 21321c0..6551b52 100644
--- a/tests/tests/text/src/android/text/method/cts/DateKeyListenerTest.java
+++ b/tests/tests/text/src/android/text/method/cts/DateKeyListenerTest.java
@@ -21,6 +21,7 @@
 
 import android.app.Activity;
 import android.app.Instrumentation;
+import android.cts.util.PollingCheck;
 import android.test.ActivityInstrumentationTestCase2;
 import android.text.InputType;
 import android.text.method.DateKeyListener;
@@ -47,6 +48,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 testConstructor() {
diff --git a/tests/tests/text/src/android/text/method/cts/DigitsKeyListenerTest.java b/tests/tests/text/src/android/text/method/cts/DigitsKeyListenerTest.java
index ea50294..16d9364 100644
--- a/tests/tests/text/src/android/text/method/cts/DigitsKeyListenerTest.java
+++ b/tests/tests/text/src/android/text/method/cts/DigitsKeyListenerTest.java
@@ -21,6 +21,7 @@
 
 import android.app.Activity;
 import android.app.Instrumentation;
+import android.cts.util.PollingCheck;
 import android.test.ActivityInstrumentationTestCase2;
 import android.text.InputType;
 import android.text.Spannable;
@@ -50,6 +51,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 testConstructor() {
diff --git a/tests/tests/text/src/android/text/method/cts/NumberKeyListenerTest.java b/tests/tests/text/src/android/text/method/cts/NumberKeyListenerTest.java
index b8bf7ef..795382c 100644
--- a/tests/tests/text/src/android/text/method/cts/NumberKeyListenerTest.java
+++ b/tests/tests/text/src/android/text/method/cts/NumberKeyListenerTest.java
@@ -21,6 +21,7 @@
 
 import android.app.Activity;
 import android.app.Instrumentation;
+import android.cts.util.PollingCheck;
 import android.test.ActivityInstrumentationTestCase2;
 import android.text.Editable;
 import android.text.Selection;
@@ -52,6 +53,12 @@
         mActivity = getActivity();
         mInstrumentation = getInstrumentation();
         mTextView = (TextView) mActivity.findViewById(R.id.keylistener_textview);
+        new PollingCheck(1000) {
+            @Override
+            protected boolean check() {
+                return mTextView.hasWindowFocus();
+            }
+        }.run();
     }
 
     /**
diff --git a/tests/tests/text/src/android/text/method/cts/PasswordTransformationMethodTest.java b/tests/tests/text/src/android/text/method/cts/PasswordTransformationMethodTest.java
index 6d647b2..471f67a 100755
--- a/tests/tests/text/src/android/text/method/cts/PasswordTransformationMethodTest.java
+++ b/tests/tests/text/src/android/text/method/cts/PasswordTransformationMethodTest.java
@@ -49,7 +49,7 @@
 
     private boolean isPasswordPrefSaved;
 
-    private StubActivity mActicity;
+    private StubActivity mActivity;
 
     private MockPasswordTransformationMethod mMethod;
 
@@ -64,22 +64,28 @@
     @Override
     protected void setUp() throws Exception {
         super.setUp();
-        mActicity = getActivity();
+        mActivity = getActivity();
+        new PollingCheck(1000) {
+            @Override
+            protected boolean check() {
+                return mActivity.hasWindowFocus();
+            }
+        }.run();
         mMethod = new MockPasswordTransformationMethod();
         try {
             runTestOnUiThread(new Runnable() {
                 public void run() {
-                    EditText editText = new EditText(mActicity);
+                    EditText editText = new EditText(mActivity);
                     editText.setId(EDIT_TXT_ID);
                     editText.setTransformationMethod(mMethod);
-                    Button button = new Button(mActicity);
-                    LinearLayout layout = new LinearLayout(mActicity);
+                    Button button = new Button(mActivity);
+                    LinearLayout layout = new LinearLayout(mActivity);
                     layout.setOrientation(LinearLayout.VERTICAL);
                     layout.addView(editText, new LayoutParams(LayoutParams.MATCH_PARENT,
                             LayoutParams.WRAP_CONTENT));
                     layout.addView(button, new LayoutParams(LayoutParams.MATCH_PARENT,
                             LayoutParams.WRAP_CONTENT));
-                    mActicity.setContentView(layout);
+                    mActivity.setContentView(layout);
                     editText.requestFocus();
                 }
             });
@@ -195,7 +201,7 @@
 
     private void savePasswordPref() {
         try {
-            mPasswordPrefBackUp = System.getInt(mActicity.getContentResolver(),
+            mPasswordPrefBackUp = System.getInt(mActivity.getContentResolver(),
                     System.TEXT_SHOW_PASSWORD);
             isPasswordPrefSaved = true;
         } catch (SettingNotFoundException e) {
@@ -205,13 +211,13 @@
 
     private void resumePasswordPref() {
         if (isPasswordPrefSaved) {
-            System.putInt(mActicity.getContentResolver(), System.TEXT_SHOW_PASSWORD,
+            System.putInt(mActivity.getContentResolver(), System.TEXT_SHOW_PASSWORD,
                     mPasswordPrefBackUp);
         }
     }
 
     private void switchShowPassword(boolean on) {
-        System.putInt(mActicity.getContentResolver(), System.TEXT_SHOW_PASSWORD,
+        System.putInt(mActivity.getContentResolver(), System.TEXT_SHOW_PASSWORD,
                 on ? 1 : 0);
     }
 
diff --git a/tests/tests/text/src/android/text/method/cts/TextKeyListenerTest.java b/tests/tests/text/src/android/text/method/cts/TextKeyListenerTest.java
index a95267a..9984efe 100644
--- a/tests/tests/text/src/android/text/method/cts/TextKeyListenerTest.java
+++ b/tests/tests/text/src/android/text/method/cts/TextKeyListenerTest.java
@@ -20,6 +20,7 @@
 
 
 import android.app.Instrumentation;
+import android.cts.util.PollingCheck;
 import android.os.SystemClock;
 import android.test.ActivityInstrumentationTestCase2;
 import android.test.UiThreadTest;
@@ -56,6 +57,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 testConstructor() {
diff --git a/tests/tests/text/src/android/text/method/cts/TimeKeyListenerTest.java b/tests/tests/text/src/android/text/method/cts/TimeKeyListenerTest.java
index cf80f8d..f9c172b 100644
--- a/tests/tests/text/src/android/text/method/cts/TimeKeyListenerTest.java
+++ b/tests/tests/text/src/android/text/method/cts/TimeKeyListenerTest.java
@@ -21,6 +21,7 @@
 
 import android.app.Activity;
 import android.app.Instrumentation;
+import android.cts.util.PollingCheck;
 import android.test.ActivityInstrumentationTestCase2;
 import android.text.InputType;
 import android.text.method.TimeKeyListener;
@@ -45,6 +46,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 testConstructor() {