add polling check for focus / dialog

bug: 9208067
Change-Id: I54bb32f968df192ba2bc208d12211196f331937c
diff --git a/tests/tests/app/src/android/app/cts/AlertDialogTest.java b/tests/tests/app/src/android/app/cts/AlertDialogTest.java
index d2edbeb..aefcf01 100644
--- a/tests/tests/app/src/android/app/cts/AlertDialogTest.java
+++ b/tests/tests/app/src/android/app/cts/AlertDialogTest.java
@@ -23,6 +23,7 @@
 import android.content.DialogInterface;
 import android.content.DialogInterface.OnCancelListener;
 import android.content.DialogInterface.OnClickListener;
+import android.cts.util.PollingCheck;
 import android.graphics.drawable.Drawable;
 import android.os.Bundle;
 import android.os.Message;
@@ -55,6 +56,12 @@
 
     protected void startDialogActivity(int dialogNumber) {
         mActivity = DialogStubActivity.startDialogActivity(this, dialogNumber);
+        new PollingCheck() {
+            @Override
+            protected boolean check() {
+                return mActivity.getDialog().isShowing();
+            }
+        }.run();
     }
 
     public void testAlertDialog() throws Throwable {
diff --git a/tests/tests/app/src/android/app/cts/AlertDialog_BuilderTest.java b/tests/tests/app/src/android/app/cts/AlertDialog_BuilderTest.java
index 4c40a62..74f85bb 100644
--- a/tests/tests/app/src/android/app/cts/AlertDialog_BuilderTest.java
+++ b/tests/tests/app/src/android/app/cts/AlertDialog_BuilderTest.java
@@ -18,6 +18,7 @@
 
 import java.util.ArrayList;
 
+import android.app.Activity;
 import android.app.AlertDialog;
 import android.app.Instrumentation;
 import android.app.AlertDialog.Builder;
@@ -29,6 +30,7 @@
 import android.content.DialogInterface.OnClickListener;
 import android.content.DialogInterface.OnKeyListener;
 import android.content.DialogInterface.OnMultiChoiceClickListener;
+import android.cts.util.PollingCheck;
 import android.database.Cursor;
 import android.database.CursorWrapper;
 import android.graphics.drawable.Drawable;
@@ -108,6 +110,13 @@
         mBuilder = null;
         mInstrumentation = getInstrumentation();
         mContext = getActivity();
+        final Activity activity = getActivity();
+        new PollingCheck() {
+            @Override
+            protected boolean check() {
+                return activity.hasWindowFocus();
+            }
+        }.run();
         mButton = null;
         mView = null;
         mListView = null;