Merge "Fix coordinate count" into jb-mr2-dev
diff --git a/tests/AndroidManifest.xml b/tests/AndroidManifest.xml
index f9df338..636a945 100644
--- a/tests/AndroidManifest.xml
+++ b/tests/AndroidManifest.xml
@@ -569,7 +569,8 @@
 
         <activity android:name="android.hardware.cts.CameraStubActivity"
             android:label="CameraStubActivity"
-            android:screenOrientation="landscape">
+            android:screenOrientation="landscape"
+            android:configChanges="keyboardHidden|orientation|screenSize">
         </activity>
 
         <activity android:name="android.view.inputmethod.cts.InputMethodStubActivity"
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;
diff --git a/tests/tests/media/src/android/media/cts/DecodeEditEncodeTest.java b/tests/tests/media/src/android/media/cts/DecodeEditEncodeTest.java
index c0c21d1..be2dc54 100644
--- a/tests/tests/media/src/android/media/cts/DecodeEditEncodeTest.java
+++ b/tests/tests/media/src/android/media/cts/DecodeEditEncodeTest.java
@@ -802,7 +802,7 @@
      * mLargestColorDelta.
      */
     boolean isColorClose(int actual, int expected) {
-        final int MAX_DELTA = 7;
+        final int MAX_DELTA = 8;
         int delta = Math.abs(actual - expected);
         if (delta > mLargestColorDelta) {
             mLargestColorDelta = delta;
diff --git a/tests/tests/media/src/android/media/cts/EncodeDecodeTest.java b/tests/tests/media/src/android/media/cts/EncodeDecodeTest.java
index ddcd4b9..d788102 100644
--- a/tests/tests/media/src/android/media/cts/EncodeDecodeTest.java
+++ b/tests/tests/media/src/android/media/cts/EncodeDecodeTest.java
@@ -1117,7 +1117,7 @@
      * mLargestColorDelta.
      */
     boolean isColorClose(int actual, int expected) {
-        final int MAX_DELTA = 7;
+        final int MAX_DELTA = 8;
         int delta = Math.abs(actual - expected);
         if (delta > mLargestColorDelta) {
             mLargestColorDelta = delta;
diff --git a/tests/tests/media/src/android/media/cts/InputSurface.java b/tests/tests/media/src/android/media/cts/InputSurface.java
index a916b8f..8d5c133 100644
--- a/tests/tests/media/src/android/media/cts/InputSurface.java
+++ b/tests/tests/media/src/android/media/cts/InputSurface.java
@@ -118,7 +118,7 @@
      * Surface that was passed to our constructor.
      */
     public void release() {
-        if (EGL14.eglGetCurrentContext() == mEGLContext) {
+        if (EGL14.eglGetCurrentContext().equals(mEGLContext)) {
             // Clear the current context and surface to ensure they are discarded immediately.
             EGL14.eglMakeCurrent(mEGLDisplay, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_SURFACE,
                     EGL14.EGL_NO_CONTEXT);
diff --git a/tests/tests/media/src/android/media/cts/OutputSurface.java b/tests/tests/media/src/android/media/cts/OutputSurface.java
index 8494796..5274627 100644
--- a/tests/tests/media/src/android/media/cts/OutputSurface.java
+++ b/tests/tests/media/src/android/media/cts/OutputSurface.java
@@ -183,7 +183,7 @@
      */
     public void release() {
         if (mEGL != null) {
-            if (mEGL.eglGetCurrentContext() == mEGLContext) {
+            if (mEGL.eglGetCurrentContext().equals(mEGLContext)) {
                 // Clear the current context and surface to ensure they are discarded immediately.
                 mEGL.eglMakeCurrent(mEGLDisplay, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE,
                         EGL10.EGL_NO_CONTEXT);
diff --git a/tests/tests/net/src/android/net/cts/DnsTest.java b/tests/tests/net/src/android/net/cts/DnsTest.java
deleted file mode 100644
index cdd95aa..0000000
--- a/tests/tests/net/src/android/net/cts/DnsTest.java
+++ /dev/null
@@ -1,237 +0,0 @@
-/*
- * Copyright (C) 2013 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package android.net.cts;
-
-import android.os.SystemClock;
-import android.test.AndroidTestCase;
-import android.util.Log;
-
-import java.net.Inet4Address;
-import java.net.Inet6Address;
-import java.net.InetAddress;
-import java.net.UnknownHostException;
-import java.util.ArrayList;
-
-public class DnsTest extends AndroidTestCase {
-
-    static {
-        System.loadLibrary("nativedns_jni");
-    }
-
-    private static final boolean DBG = false;
-    private static final String TAG = "DnsTest";
-
-    /**
-     * @return true on success
-     */
-    private static native boolean testNativeDns();
-
-    /**
-     * Verify:
-     * DNS works - forwards and backwards, giving ipv4 and ipv6
-     * Test that DNS work on v4 and v6 networks
-     * Test Native dns calls (4)
-     * Todo:
-     * Cache is flushed when we change networks
-     * have per-network caches
-     * No cache when there's no network
-     * Perf - measure size of first and second tier caches and their effect
-     * Assert requires network permission
-     */
-    public void testDnsWorks() {
-        InetAddress addrs[] = {};
-        try {
-            addrs = InetAddress.getAllByName("www.google.com");
-        } catch (UnknownHostException e) {}
-        assertTrue(addrs.length != 0);
-        boolean foundV4 = false, foundV6 = false;
-        for (InetAddress addr : addrs) {
-            if (addr instanceof Inet4Address) foundV4 = true;
-            else if (addr instanceof Inet6Address) foundV6 = true;
-            if (DBG) Log.e(TAG, "www.google.com gave " + addr.toString());
-        }
-        assertTrue(foundV4);
-        assertTrue(foundV6);
-        try {
-            addrs = InetAddress.getAllByName("ipv6.google.com");
-        } catch (UnknownHostException e) {}
-        assertTrue(addrs.length != 0);
-        foundV4 = false;
-        foundV6 = false;
-        for (InetAddress addr : addrs) {
-            if (addr instanceof Inet4Address) foundV4 = true;
-            else if (addr instanceof Inet6Address) foundV6 = true;
-            if (DBG) Log.e(TAG, "ipv6.google.com gave " + addr.toString());
-        }
-        assertTrue(foundV4 == false);
-        assertTrue(foundV6 == true);
-        assertTrue(testNativeDns());
-    }
-
-    private static final String[] URLS = { "www.google.com", "ipv6.google.com", "www.yahoo.com",
-            "facebook.com", "youtube.com", "blogspot.com", "baidu.com", "wikipedia.org",
-// live.com fails rev lookup.
-            "twitter.com", "qq.com", "msn.com", "yahoo.co.jp", "linkedin.com",
-            "taobao.com", "google.co.in", "sina.com.cn", "amazon.com", "wordpress.com",
-            "google.co.uk", "ebay.com", "yandex.ru", "163.com", "google.co.jp", "google.fr",
-            "microsoft.com", "paypal.com", "google.com.br", "flickr.com",
-            "mail.ru", "craigslist.org", "fc2.com", "google.it",
-// "apple.com", fails rev lookup
-            "google.es",
-            "imdb.com", "google.ru", "soho.com", "bbc.co.uk", "vkontakte.ru", "ask.com",
-            "tumblr.com", "weibo.com", "go.com", "xvideos.com", "livejasmin.com", "cnn.com",
-            "youku.com", "blogspot.com", "soso.com", "google.ca", "aol.com", "tudou.com",
-            "xhamster.com", "megaupload.com", "ifeng.com", "zedo.com", "mediafire.com", "ameblo.jp",
-            "pornhub.com", "google.co.id", "godaddy.com", "adobe.com", "rakuten.co.jp", "about.com",
-            "espn.go.com", "4shared.com", "alibaba.com","ebay.de", "yieldmanager.com",
-            "wordpress.org", "livejournal.com", "google.com.tr", "google.com.mx", "renren.com",
-           "livedoor.com", "google.com.au", "youporn.com", "uol.com.br", "cnet.com", "conduit.com",
-            "google.pl", "myspace.com", "nytimes.com", "ebay.co.uk", "chinaz.com", "hao123.com",
-            "thepiratebay.org", "doubleclick.com", "alipay.com", "netflix.com", "cnzz.com",
-            "huffingtonpost.com", "twitpic.com", "weather.com", "babylon.com", "amazon.de",
-            "dailymotion.com", "orkut.com", "orkut.com.br", "google.com.sa", "odnoklassniki.ru",
-            "amazon.co.jp", "google.nl", "goo.ne.jp", "stumbleupon.com", "tube8.com", "tmall.com",
-            "imgur.com", "globo.com", "secureserver.net", "fileserve.com", "tianya.cn", "badoo.com",
-            "ehow.com", "photobucket.com", "imageshack.us", "xnxx.com", "deviantart.com",
-            "filestube.com", "addthis.com", "douban.com", "vimeo.com", "sogou.com",
-            "stackoverflow.com", "reddit.com", "dailymail.co.uk", "redtube.com", "megavideo.com",
-            "taringa.net", "pengyou.com", "amazon.co.uk", "fbcdn.net", "aweber.com", "spiegel.de",
-            "rapidshare.com", "mixi.jp", "360buy.com", "google.cn", "digg.com", "answers.com",
-            "bit.ly", "indiatimes.com", "skype.com", "yfrog.com", "optmd.com", "google.com.eg",
-            "google.com.pk", "58.com", "hotfile.com", "google.co.th",
-            "bankofamerica.com", "sourceforge.net", "maktoob.com", "warriorforum.com", "rediff.com",
-            "google.co.za", "56.com", "torrentz.eu", "clicksor.com", "avg.com",
-            "download.com", "ku6.com", "statcounter.com", "foxnews.com", "google.com.ar",
-            "nicovideo.jp", "reference.com", "liveinternet.ru", "ucoz.ru", "xinhuanet.com",
-            "xtendmedia.com", "naver.com", "youjizz.com", "domaintools.com", "sparkstudios.com",
-            "rambler.ru", "scribd.com", "kaixin001.com", "mashable.com", "adultfirendfinder.com",
-            "files.wordpress.com", "guardian.co.uk", "bild.de", "yelp.com", "wikimedia.org",
-            "chase.com", "onet.pl", "ameba.jp", "pconline.com.cn", "free.fr", "etsy.com",
-            "typepad.com", "youdao.com", "megaclick.com", "digitalpoint.com", "blogfa.com",
-            "salesforce.com", "adf.ly", "ganji.com", "wikia.com", "archive.org", "terra.com.br",
-            "w3schools.com", "ezinearticles.com", "wjs.com", "google.com.my", "clickbank.com",
-            "squidoo.com", "hulu.com", "repubblica.it", "google.be", "allegro.pl", "comcast.net",
-            "narod.ru", "zol.com.cn", "orange.fr", "soufun.com", "hatena.ne.jp", "google.gr",
-            "in.com", "techcrunch.com", "orkut.co.in", "xunlei.com",
-            "reuters.com", "google.com.vn", "hostgator.com", "kaskus.us", "espncricinfo.com",
-            "hootsuite.com", "qiyi.com", "gmx.net", "xing.com", "php.net", "soku.com", "web.de",
-            "libero.it", "groupon.com", "51.la", "slideshare.net", "booking.com", "seesaa.net",
-            "126.com", "telegraph.co.uk", "wretch.cc", "twimg.com", "rutracker.org", "angege.com",
-            "nba.com", "dell.com", "leboncoin.fr", "people.com", "google.com.tw", "walmart.com",
-            "daum.net", "2ch.net", "constantcontact.com", "nifty.com", "mywebsearch.com",
-            "tripadvisor.com", "google.se", "paipai.com", "google.com.ua", "ning.com", "hp.com",
-            "google.at", "joomla.org", "icio.us", "hudong.com", "csdn.net", "getfirebug.com",
-            "ups.com", "cj.com", "google.ch", "camzap.com", "wordreference.com", "tagged.com",
-            "wp.pl", "mozilla.com", "google.ru", "usps.com", "china.com", "themeforest.net",
-            "search-results.com", "tribalfusion.com", "thefreedictionary.com", "isohunt.com",
-            "linkwithin.com", "cam4.com", "plentyoffish.com", "wellsfargo.com", "metacafe.com",
-            "depositfiles.com", "freelancer.com", "opendns.com", "homeway.com", "engadget.com",
-            "10086.cn", "360.cn", "marca.com", "dropbox.com", "ign.com", "match.com", "google.pt",
-            "facemoods.com", "hardsextube.com", "google.com.ph", "lockerz.com", "istockphoto.com",
-            "partypoker.com", "netlog.com", "outbrain.com", "elpais.com", "fiverr.com",
-            "biglobe.ne.jp", "corriere.it", "love21cn.com", "yesky.com", "spankwire.com",
-            "ig.com.br", "imagevenue.com", "hubpages.com", "google.co.ve"};
-
-// TODO - this works, but is slow and cts doesn't do anything with the result.
-// Maybe require a min performance, a min cache size (detectable) and/or move
-// to perf testing
-    private static final int LOOKUP_COUNT_GOAL = URLS.length;
-    public void skiptestDnsPerf() {
-        ArrayList<String> results = new ArrayList<String>();
-        int failures = 0;
-        try {
-            for (int numberOfUrls = URLS.length; numberOfUrls > 0; numberOfUrls--) {
-                failures = 0;
-                int iterationLimit = LOOKUP_COUNT_GOAL / numberOfUrls;
-                long startTime = SystemClock.elapsedRealtimeNanos();
-                for (int iteration = 0; iteration < iterationLimit; iteration++) {
-                    for (int urlIndex = 0; urlIndex < numberOfUrls; urlIndex++) {
-                        try {
-                            InetAddress addr = InetAddress.getByName(URLS[urlIndex]);
-                        } catch (UnknownHostException e) {
-                            Log.e(TAG, "failed first lookup of " + URLS[urlIndex]);
-                            failures++;
-                            try {
-                                InetAddress addr = InetAddress.getByName(URLS[urlIndex]);
-                            } catch (UnknownHostException ee) {
-                                failures++;
-                                Log.e(TAG, "failed SECOND lookup of " + URLS[urlIndex]);
-                            }
-                        }
-                    }
-                }
-                long endTime = SystemClock.elapsedRealtimeNanos();
-                float nsPer = ((float)(endTime-startTime) / iterationLimit) / numberOfUrls/ 1000;
-                String thisResult = new String("getByName for " + numberOfUrls + " took " +
-                        (endTime - startTime)/1000 + "(" + nsPer + ") with " +
-                        failures + " failures\n");
-                Log.d(TAG, thisResult);
-                results.add(thisResult);
-            }
-            // build up a list of addresses
-            ArrayList<byte[]> addressList = new ArrayList<byte[]>();
-            for (String url : URLS) {
-                try {
-                    InetAddress addr = InetAddress.getByName(url);
-                    addressList.add(addr.getAddress());
-                } catch (UnknownHostException e) {
-                    Log.e(TAG, "Exception making reverseDNS list: " + e.toString());
-                }
-            }
-            for (int numberOfAddrs = addressList.size(); numberOfAddrs > 0; numberOfAddrs--) {
-                int iterationLimit = LOOKUP_COUNT_GOAL / numberOfAddrs;
-                failures = 0;
-                long startTime = SystemClock.elapsedRealtimeNanos();
-                for (int iteration = 0; iteration < iterationLimit; iteration++) {
-                    for (int addrIndex = 0; addrIndex < numberOfAddrs; addrIndex++) {
-                        try {
-                            InetAddress addr = InetAddress.getByAddress(addressList.get(addrIndex));
-                            String hostname = addr.getHostName();
-                        } catch (UnknownHostException e) {
-                            failures++;
-                            Log.e(TAG, "Failure doing reverse DNS lookup: " + e.toString());
-                            try {
-                                InetAddress addr =
-                                        InetAddress.getByAddress(addressList.get(addrIndex));
-                                String hostname = addr.getHostName();
-
-                            } catch (UnknownHostException ee) {
-                                failures++;
-                                Log.e(TAG, "Failure doing SECOND reverse DNS lookup: " +
-                                        ee.toString());
-                            }
-                        }
-                    }
-                }
-                long endTime = SystemClock.elapsedRealtimeNanos();
-                float nsPer = ((endTime-startTime) / iterationLimit) / numberOfAddrs / 1000;
-                String thisResult = new String("getHostName for " + numberOfAddrs + " took " +
-                        (endTime - startTime)/1000 + "(" + nsPer + ") with " +
-                        failures + " failures\n");
-                Log.d(TAG, thisResult);
-                results.add(thisResult);
-            }
-            for (String result : results) Log.d(TAG, result);
-
-            InetAddress exit = InetAddress.getByName("exitrightnow.com");
-            Log.e(TAG, " exit address= "+exit.toString());
-
-        } catch (Exception e) {
-            Log.e(TAG, "bad URL in testDnsPerf: " + e.toString());
-        }
-    }
-}
diff --git a/tests/tests/openglperf/src/android/openglperf/cts/GlVboPerfTest.java b/tests/tests/openglperf/src/android/openglperf/cts/GlVboPerfTest.java
index 2e9e10b..8b07b80 100644
--- a/tests/tests/openglperf/src/android/openglperf/cts/GlVboPerfTest.java
+++ b/tests/tests/openglperf/src/android/openglperf/cts/GlVboPerfTest.java
@@ -27,7 +27,7 @@
     private static final int NUM_FRAMES_TO_RENDER = 100;
     private static final long RENDERING_TIMEOUT = 5 * 60;
     // 10% of fps_no_vbo is allowed to compensate variations in measurement
-    private static final float FPS_COMPARISON_MARGIN = 0.1f;
+    private static final float FPS_COMPARISON_MARGIN = 0.2f;
     // the worst case should be above 70% of the best case
     private static final float FPS_MIN_MAX_COMPARISON_PERCENTILE = 0.7f;
 
diff --git a/tests/tests/permission/src/android/permission/cts/FileSystemPermissionTest.java b/tests/tests/permission/src/android/permission/cts/FileSystemPermissionTest.java
index 3a3e672..300bfaf 100644
--- a/tests/tests/permission/src/android/permission/cts/FileSystemPermissionTest.java
+++ b/tests/tests/permission/src/android/permission/cts/FileSystemPermissionTest.java
@@ -674,6 +674,7 @@
                 new File("/dev/binder"),
                 new File("/dev/full"),
                 new File("/dev/genlock"),    // b/9035217
+                new File("/dev/hw_random"),  // b/9191279
                 new File("/dev/ion"),
                 new File("/dev/kgsl-3d0"),   // b/9035217
                 new File("/dev/log/events"), // b/9035217
diff --git a/tests/tests/text/src/android/text/method/cts/DateTimeKeyListenerTest.java b/tests/tests/text/src/android/text/method/cts/DateTimeKeyListenerTest.java
index 69709f8..9aac08b 100644
--- a/tests/tests/text/src/android/text/method/cts/DateTimeKeyListenerTest.java
+++ b/tests/tests/text/src/android/text/method/cts/DateTimeKeyListenerTest.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.DateTimeKeyListener;
@@ -46,6 +47,12 @@
         super.setUp();
 
         mActivity = getActivity();
+        new PollingCheck() {
+            @Override
+            protected boolean check() {
+                return mActivity.hasWindowFocus();
+            }
+        }.run();
         mInstrumentation = getInstrumentation();
         mTextView = (TextView) mActivity.findViewById(R.id.keylistener_textview);
     }
diff --git a/tests/tests/view/src/android/view/cts/SurfaceViewTest.java b/tests/tests/view/src/android/view/cts/SurfaceViewTest.java
index 2d61559..65f05b8 100644
--- a/tests/tests/view/src/android/view/cts/SurfaceViewTest.java
+++ b/tests/tests/view/src/android/view/cts/SurfaceViewTest.java
@@ -45,7 +45,14 @@
         super.setUp();
         mInstrumentation = getInstrumentation();
         mContext = mInstrumentation.getContext();
-        mMockSurfaceView = getActivity().getSurfaceView();
+        final SurfaceViewStubActivity activity = getActivity();
+        new PollingCheck() {
+            @Override
+                protected boolean check() {
+                return activity.hasWindowFocus();
+            }
+        }.run();
+        mMockSurfaceView = activity.getSurfaceView();
     }
 
     public void testConstructor() {
diff --git a/tests/tests/view/src/android/view/cts/ViewTest.java b/tests/tests/view/src/android/view/cts/ViewTest.java
index 6313f7c..27c98cc 100644
--- a/tests/tests/view/src/android/view/cts/ViewTest.java
+++ b/tests/tests/view/src/android/view/cts/ViewTest.java
@@ -101,6 +101,12 @@
     protected void setUp() throws Exception {
         super.setUp();
         mActivity = getActivity();
+        new PollingCheck() {
+            @Override
+                protected boolean check() {
+                return mActivity.hasWindowFocus();
+            }
+        }.run();
         mResources = mActivity.getResources();
         mMockParent = new MockViewParent(mActivity);
     }
diff --git a/tests/tests/view/src/android/view/cts/ViewTreeObserverTest.java b/tests/tests/view/src/android/view/cts/ViewTreeObserverTest.java
index 1ee690b..cc05853 100644
--- a/tests/tests/view/src/android/view/cts/ViewTreeObserverTest.java
+++ b/tests/tests/view/src/android/view/cts/ViewTreeObserverTest.java
@@ -48,6 +48,12 @@
         super.setUp();
         mViewTreeObserver = null;
         mActivity = getActivity();
+        new PollingCheck() {
+            @Override
+                protected boolean check() {
+                return mActivity.hasWindowFocus();
+            }
+        }.run();
         mInstrumentation = getInstrumentation();
         layout(R.layout.viewtreeobserver_layout);
     }
diff --git a/tests/tests/view/src/android/view/inputmethod/cts/BaseInputConnectionTest.java b/tests/tests/view/src/android/view/inputmethod/cts/BaseInputConnectionTest.java
index ad6f971..247578b 100755
--- a/tests/tests/view/src/android/view/inputmethod/cts/BaseInputConnectionTest.java
+++ b/tests/tests/view/src/android/view/inputmethod/cts/BaseInputConnectionTest.java
@@ -56,6 +56,12 @@
         super.setUp();
         mInstrumentation = getInstrumentation();
         mActivity = getActivity();
+        new PollingCheck() {
+            @Override
+                protected boolean check() {
+                return mActivity.hasWindowFocus();
+            }
+        }.run();
         mWindow = mActivity.getWindow();
         mView = (EditText) mWindow.findViewById(R.id.entry);
         mConnection = new BaseInputConnection(mView, true);
diff --git a/tests/tests/webkit/src/android/webkit/cts/WebViewClientTest.java b/tests/tests/webkit/src/android/webkit/cts/WebViewClientTest.java
index e02998e..3353d50 100644
--- a/tests/tests/webkit/src/android/webkit/cts/WebViewClientTest.java
+++ b/tests/tests/webkit/src/android/webkit/cts/WebViewClientTest.java
@@ -41,7 +41,14 @@
     @Override
     protected void setUp() throws Exception {
         super.setUp();
-        mOnUiThread = new WebViewOnUiThread(this, getActivity().getWebView());
+        final WebViewStubActivity activity = getActivity();
+        new PollingCheck(TEST_TIMEOUT) {
+            @Override
+                protected boolean check() {
+                return activity.hasWindowFocus();
+            }
+        }.run();
+        mOnUiThread = new WebViewOnUiThread(this, activity.getWebView());
     }
 
     @Override
diff --git a/tests/tests/webkit/src/android/webkit/cts/WebViewTest.java b/tests/tests/webkit/src/android/webkit/cts/WebViewTest.java
index 0dab28b..0426f23 100755
--- a/tests/tests/webkit/src/android/webkit/cts/WebViewTest.java
+++ b/tests/tests/webkit/src/android/webkit/cts/WebViewTest.java
@@ -97,8 +97,15 @@
     @Override
     protected void setUp() throws Exception {
         super.setUp();
-        mWebView = getActivity().getWebView();
-        File f = getActivity().getFileStreamPath("snapshot");
+        final WebViewStubActivity activity = getActivity();
+        new PollingCheck() {
+            @Override
+                protected boolean check() {
+                return activity.hasWindowFocus();
+            }
+        }.run();
+        mWebView = activity.getWebView();
+        File f = activity.getFileStreamPath("snapshot");
         if (f.exists()) {
             f.delete();
         }
diff --git a/tests/tests/widget/src/android/widget/cts/AbsListViewTest.java b/tests/tests/widget/src/android/widget/cts/AbsListViewTest.java
index 7fa2d2f..7dcae28 100644
--- a/tests/tests/widget/src/android/widget/cts/AbsListViewTest.java
+++ b/tests/tests/widget/src/android/widget/cts/AbsListViewTest.java
@@ -81,6 +81,12 @@
         super.setUp();
 
         mActivity = getActivity();
+        new PollingCheck() {
+            @Override
+                protected boolean check() {
+                return mActivity.hasWindowFocus();
+            }
+        }.run();
         mInstrumentation = getInstrumentation();
 
         XmlPullParser parser = mActivity.getResources().getXml(R.layout.listview_layout);
diff --git a/tests/tests/widget/src/android/widget/cts/AutoCompleteTextViewTest.java b/tests/tests/widget/src/android/widget/cts/AutoCompleteTextViewTest.java
index f7cf4d3..373c03b 100755
--- a/tests/tests/widget/src/android/widget/cts/AutoCompleteTextViewTest.java
+++ b/tests/tests/widget/src/android/widget/cts/AutoCompleteTextViewTest.java
@@ -87,6 +87,12 @@
     protected void setUp() throws Exception {
         super.setUp();
         mActivity = getActivity();
+        new PollingCheck() {
+            @Override
+                protected boolean check() {
+                return mActivity.hasWindowFocus();
+            }
+        }.run();
         mInstrumentation = getInstrumentation();
         mAutoCompleteTextView = (AutoCompleteTextView) mActivity
                 .findViewById(R.id.autocompletetv_edit);
diff --git a/tests/tests/widget/src/android/widget/cts/DialerFilterTest.java b/tests/tests/widget/src/android/widget/cts/DialerFilterTest.java
index 6886ac0..1c5c7ff 100755
--- a/tests/tests/widget/src/android/widget/cts/DialerFilterTest.java
+++ b/tests/tests/widget/src/android/widget/cts/DialerFilterTest.java
@@ -24,6 +24,7 @@
 import android.app.Activity;
 import android.app.Instrumentation;
 import android.content.Context;
+import android.cts.util.PollingCheck;
 import android.test.ActivityInstrumentationTestCase2;
 import android.test.UiThreadTest;
 import android.text.Editable;
@@ -52,6 +53,12 @@
         super.setUp();
 
         mActivity = getActivity();
+        new PollingCheck() {
+            @Override
+                protected boolean check() {
+                return mActivity.hasWindowFocus();
+            }
+        }.run();
         mInstrumentation = getInstrumentation();
 
         mDialerFilter = (DialerFilter) mActivity.findViewById(R.id.dialer_filter);
diff --git a/tests/tests/widget/src/android/widget/cts/ExpandableListViewBasicTest.java b/tests/tests/widget/src/android/widget/cts/ExpandableListViewBasicTest.java
index d50897f..dd8d6a2 100644
--- a/tests/tests/widget/src/android/widget/cts/ExpandableListViewBasicTest.java
+++ b/tests/tests/widget/src/android/widget/cts/ExpandableListViewBasicTest.java
@@ -17,6 +17,7 @@
 package android.widget.cts;
 
 
+import android.cts.util.PollingCheck;
 import android.test.ActivityInstrumentationTestCase2;
 import android.test.suitebuilder.annotation.MediumTest;
 import android.view.KeyEvent;
@@ -45,6 +46,12 @@
         super.setUp();
 
         mActivity = getActivity();
+        new PollingCheck() {
+            @Override
+                protected boolean check() {
+                return mActivity.hasWindowFocus();
+            }
+        }.run();
         mExpandableListView = mActivity.getExpandableListView();
         mAdapter = mExpandableListView.getExpandableListAdapter();
         mListUtil = new ListUtil(mExpandableListView, getInstrumentation());
diff --git a/tests/tests/widget/src/android/widget/cts/ExpandableListViewWithHeadersTest.java b/tests/tests/widget/src/android/widget/cts/ExpandableListViewWithHeadersTest.java
index 950355c..7f4715f 100644
--- a/tests/tests/widget/src/android/widget/cts/ExpandableListViewWithHeadersTest.java
+++ b/tests/tests/widget/src/android/widget/cts/ExpandableListViewWithHeadersTest.java
@@ -17,6 +17,7 @@
 package android.widget.cts;
 
 
+import android.cts.util.PollingCheck;
 import android.test.ActivityInstrumentationTestCase2;
 import android.test.suitebuilder.annotation.LargeTest;
 import android.test.suitebuilder.annotation.MediumTest;
@@ -37,7 +38,14 @@
     protected void setUp() throws Exception {
         super.setUp();
 
-        mExpandableListView = getActivity().getExpandableListView();
+        final ExpandableListWithHeaders activity = getActivity();
+        new PollingCheck() {
+            @Override
+                protected boolean check() {
+                return activity.hasWindowFocus();
+            }
+        }.run();
+        mExpandableListView = activity.getExpandableListView();
         mListUtil = new ListUtil(mExpandableListView, getInstrumentation());
     }
 
diff --git a/tests/tests/widget/src/android/widget/cts/GridViewTest.java b/tests/tests/widget/src/android/widget/cts/GridViewTest.java
index 2467861..1dd4764 100755
--- a/tests/tests/widget/src/android/widget/cts/GridViewTest.java
+++ b/tests/tests/widget/src/android/widget/cts/GridViewTest.java
@@ -24,6 +24,7 @@
 import android.app.Activity;
 import android.app.Instrumentation;
 import android.content.Context;
+import android.cts.util.PollingCheck;
 import android.database.DataSetObservable;
 import android.database.DataSetObserver;
 import android.graphics.Rect;
@@ -68,6 +69,12 @@
         super.setUp();
         mGridView = null;
         mActivity = getActivity();
+        new PollingCheck() {
+            @Override
+            protected boolean check() {
+                return mActivity.hasWindowFocus();
+            }
+        }.run();
         mInstrumentation = getInstrumentation();
     }
 
diff --git a/tests/tests/widget/src/android/widget/cts/TextViewTest.java b/tests/tests/widget/src/android/widget/cts/TextViewTest.java
index 8255897..7f6efb1 100755
--- a/tests/tests/widget/src/android/widget/cts/TextViewTest.java
+++ b/tests/tests/widget/src/android/widget/cts/TextViewTest.java
@@ -119,6 +119,12 @@
     protected void setUp() throws Exception {
         super.setUp();
         mActivity = getActivity();
+        new PollingCheck() {
+            @Override
+                protected boolean check() {
+                return mActivity.hasWindowFocus();
+            }
+        }.run();
         mInstrumentation = getInstrumentation();
     }