Merge "move activities for media to media package" into jb-dev
diff --git a/hostsidetests/monkey/src/com/android/cts/monkey/MonkeyTest.java b/hostsidetests/monkey/src/com/android/cts/monkey/MonkeyTest.java
index 87e1268..17a5cf8 100644
--- a/hostsidetests/monkey/src/com/android/cts/monkey/MonkeyTest.java
+++ b/hostsidetests/monkey/src/com/android/cts/monkey/MonkeyTest.java
@@ -24,8 +24,6 @@
 
 public class MonkeyTest extends AbstractMonkeyTest {
 
-    private static final Pattern LOG_PATTERN =
-            Pattern.compile("I/MonkeyActivity\\([\\d ]+\\): (.*)");
     private static final String MONKEY = "@(>.<)@";
     private static final String HUMAN = "(^_^)";
 
@@ -49,13 +47,11 @@
         try {
             while (s.hasNextLine()) {
                 String line = s.nextLine();
-                Matcher m = LOG_PATTERN.matcher(line);
-                if (m.matches()) {
+                if (line.contains(isMonkey ? MONKEY : HUMAN)) {
                     monkeyLogsFound = true;
-                    assertEquals(isMonkey ? MONKEY : HUMAN, m.group(1));
                 }
             }
-            assertTrue("No monkey logs were found!", monkeyLogsFound);
+            assertTrue(monkeyLogsFound);
         } finally {
             s.close();
         }
diff --git a/libs/util/src/android/cts/util/PollingCheck.java b/libs/util/src/android/cts/util/PollingCheck.java
index 2990992..be6b0b0 100644
--- a/libs/util/src/android/cts/util/PollingCheck.java
+++ b/libs/util/src/android/cts/util/PollingCheck.java
@@ -16,6 +16,8 @@
 
 package android.cts.util;
 
+import java.util.concurrent.Callable;
+
 import junit.framework.Assert;
 
 public abstract class PollingCheck {
@@ -53,4 +55,18 @@
 
         Assert.fail("unexpected timeout");
     }
+
+    public static void check(CharSequence message, long timeout, Callable<Boolean> condition)
+            throws Exception {
+        while (timeout > 0) {
+            if (condition.call()) {
+                return;
+            }
+
+            Thread.sleep(TIME_SLICE);
+            timeout -= TIME_SLICE;
+        }
+
+        Assert.fail(message.toString());
+    }
 }
diff --git a/tests/src/android/app/cts/IntentServiceStub.java b/tests/src/android/app/cts/IntentServiceStub.java
index 043ff85..8f23a4f 100644
--- a/tests/src/android/app/cts/IntentServiceStub.java
+++ b/tests/src/android/app/cts/IntentServiceStub.java
@@ -27,19 +27,19 @@
         super("IntentServiceStub");
     }
 
-    public static String ISS_ADD = "add";
-    public static String ISS_VALUE = "value";
+    public static final String ISS_ADD = "add";
+    public static final String ISS_VALUE = "value";
 
-    public static int onHandleIntentCalled;
-    public static boolean onBindCalled;
-    public static boolean onCreateCalled;
-    public static boolean onDestroyCalled;
-    public static boolean onStartCalled;
-    public static int accumulator;
+    private static int onHandleIntentCalled;
+    private static boolean onBindCalled;
+    private static boolean onCreateCalled;
+    private static boolean onDestroyCalled;
+    private static boolean onStartCalled;
+    private static int accumulator;
 
     private static Throwable throwable;
 
-    public static void reset() {
+    public synchronized static void reset() {
         onHandleIntentCalled = 0;
         onBindCalled = false;
         onCreateCalled = false;
@@ -53,7 +53,9 @@
         new PollingCheck(timeout) {
             @Override
             protected boolean check() {
-                return IntentServiceStub.onDestroyCalled;
+                synchronized (IntentServiceStub.class) {
+                    return IntentServiceStub.onDestroyCalled;
+                }
             }
         }.run();
         if (throwable != null) {
@@ -63,39 +65,72 @@
 
     @Override
     protected void onHandleIntent(Intent intent) {
-        onHandleIntentCalled += 1;
-        try {
-            String action = intent.getAction();
-            if (action != null && action.equals(ISS_ADD)) {
-                accumulator += intent.getIntExtra(ISS_VALUE, 0);
+        synchronized (IntentServiceStub.class) {
+            onHandleIntentCalled += 1;
+            try {
+                String action = intent.getAction();
+                if (action != null && action.equals(ISS_ADD)) {
+                    accumulator += intent.getIntExtra(ISS_VALUE, 0);
+                }
+            } catch (Throwable t) {
+                throwable = t;
             }
-        } catch (Throwable t) {
-            throwable = t;
         }
     }
 
     @Override
     public IBinder onBind(Intent intent) {
-        onBindCalled = true;
+        synchronized (IntentServiceStub.class) {
+            onBindCalled = true;
+        }
         return new Binder();
     }
 
     @Override
     public void onCreate() {
-        onCreateCalled = true;
+        synchronized (IntentServiceStub.class) {
+            onCreateCalled = true;
+        }
         super.onCreate();
     }
 
     @Override
     public void onDestroy() {
-        onDestroyCalled = true;
+        synchronized (IntentServiceStub.class) {
+            onDestroyCalled = true;
+        }
         super.onDestroy();
     }
 
     @Override
     public void onStart(Intent intent, int startId) {
-        onStartCalled = true;
+        synchronized (IntentService.class) {
+            onStartCalled = true;
+        }
         super.onStart(intent, startId);
     }
 
+    public synchronized static int getOnHandleIntentCalledCount() {
+        return onHandleIntentCalled;
+    }
+
+    public synchronized static boolean isOnBindCalled() {
+        return onBindCalled;
+    }
+
+    public synchronized static boolean isOnCreateCalled() {
+        return onCreateCalled;
+    }
+
+    public synchronized static boolean isOnDestroyCalled() {
+        return onDestroyCalled;
+    }
+
+    public synchronized static boolean isOnStartCalled() {
+        return onStartCalled;
+    }
+
+    public synchronized static int getAccumulator() {
+        return accumulator;
+    }
 }
diff --git a/tests/tests/animation/src/android/animation/cts/ValueAnimatorTest.java b/tests/tests/animation/src/android/animation/cts/ValueAnimatorTest.java
index 509be58..7e86ce7 100644
--- a/tests/tests/animation/src/android/animation/cts/ValueAnimatorTest.java
+++ b/tests/tests/animation/src/android/animation/cts/ValueAnimatorTest.java
@@ -108,7 +108,7 @@
     public void testCancel() throws Throwable {
         startAnimation(mValueAnimator);
         Thread.sleep(100);
-        mValueAnimator.cancel();
+        cancelAnimation(mValueAnimator);
         assertFalse(mValueAnimator.isRunning());
     }
 
@@ -258,6 +258,15 @@
         this.runTestOnUiThread(animationRunnable);
     }
 
+    private void cancelAnimation(final ValueAnimator mValueAnimator) throws Throwable {
+        Thread animationRunnable = new Thread() {
+            public void run() {
+                mValueAnimator.cancel();
+            }
+        };
+        this.runTestOnUiThread(animationRunnable);
+    }
+
     private String errorMessage(float[] values) {
         StringBuilder message = new StringBuilder();
         for (int i = 0; i < values.length; i++) {
@@ -266,4 +275,3 @@
         return message.toString();
     }
 }
-
diff --git a/tests/tests/app/src/android/app/cts/IntentServiceTest.java b/tests/tests/app/src/android/app/cts/IntentServiceTest.java
index a3e8edc..42c4730 100644
--- a/tests/tests/app/src/android/app/cts/IntentServiceTest.java
+++ b/tests/tests/app/src/android/app/cts/IntentServiceTest.java
@@ -16,7 +16,6 @@
 
 package android.app.cts;
 
-import android.app.IntentService;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
@@ -24,6 +23,8 @@
 import android.cts.util.PollingCheck;
 import android.os.IBinder;
 
+import java.util.concurrent.Callable;
+
 
 public class IntentServiceTest extends ActivityTestsBase {
 
@@ -41,9 +42,7 @@
     @Override
     protected void tearDown() throws Exception {
         super.tearDown();
-        if (!IntentServiceStub.onDestroyCalled) {
-            mContext.stopService(mIntent);
-        }
+        mContext.stopService(mIntent);
     }
 
     public void testIntents() throws Throwable {
@@ -59,11 +58,27 @@
             mContext.startService(addIntent);
         }
 
-        // service should terminate automatically once all intents are handled
-        IntentServiceStub.waitToFinish(TIMEOUT_MSEC);
-        assertEquals(adds, IntentServiceStub.onHandleIntentCalled);
-        assertEquals(adds * value, IntentServiceStub.accumulator);
+        PollingCheck.check("onHandleIntentCalled not called enough", TIMEOUT_MSEC,
+                new Callable<Boolean>() {
+            @Override
+            public Boolean call() throws Exception {
+                return IntentServiceStub.getOnHandleIntentCalledCount() == adds;
+            }
+        });
 
+        PollingCheck.check("accumulator not correct", TIMEOUT_MSEC, new Callable<Boolean>() {
+            @Override
+            public Boolean call() throws Exception {
+                return IntentServiceStub.getAccumulator() == adds * value;
+            }
+        });
+
+        PollingCheck.check("onDestroyCalled not called", TIMEOUT_MSEC, new Callable<Boolean>() {
+            @Override
+            public Boolean call() throws Exception {
+                return IntentServiceStub.isOnDestroyCalled();
+            }
+        });
     }
 
     public void testIntentServiceLifeCycle() throws Throwable {
@@ -71,11 +86,11 @@
         mContext.startService(mIntent);
         new PollingCheck(TIMEOUT_MSEC) {
             protected boolean check() {
-                return IntentServiceStub.onHandleIntentCalled > 0;
+                return IntentServiceStub.getOnHandleIntentCalledCount() > 0;
             }
         }.run();
-        assertTrue(IntentServiceStub.onCreateCalled);
-        assertTrue(IntentServiceStub.onStartCalled);
+        assertTrue(IntentServiceStub.isOnCreateCalled());
+        assertTrue(IntentServiceStub.isOnStartCalled());
 
         // bind service
         ServiceConnection conn = new TestConnection();
@@ -85,7 +100,7 @@
                 return mConnected;
             }
         }.run();
-        assertTrue(IntentServiceStub.onBindCalled);
+        assertTrue(IntentServiceStub.isOnBindCalled());
 
         // unbind service
         mContext.unbindService(conn);
diff --git a/tests/tests/net/Android.mk b/tests/tests/net/Android.mk
index 3e48cd2..5c70ad4 100644
--- a/tests/tests/net/Android.mk
+++ b/tests/tests/net/Android.mk
@@ -28,7 +28,7 @@
 
 LOCAL_PACKAGE_NAME := CtsNetTestCases
 
-LOCAL_STATIC_JAVA_LIBRARIES := ctstestserver
+LOCAL_STATIC_JAVA_LIBRARIES := ctstestserver ctsutil
 
 # uncomment when dalvik.annotation.Test* are removed or part of SDK
 #LOCAL_SDK_VERSION := current
diff --git a/tests/tests/net/src/android/net/wifi/cts/WifiInfoTest.java b/tests/tests/net/src/android/net/wifi/cts/WifiInfoTest.java
index df1323b..16dc57d 100644
--- a/tests/tests/net/src/android/net/wifi/cts/WifiInfoTest.java
+++ b/tests/tests/net/src/android/net/wifi/cts/WifiInfoTest.java
@@ -21,12 +21,15 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.cts.util.PollingCheck;
 import android.net.wifi.SupplicantState;
 import android.net.wifi.WifiInfo;
 import android.net.wifi.WifiManager;
 import android.net.wifi.WifiManager.WifiLock;
 import android.test.AndroidTestCase;
 
+import java.util.concurrent.Callable;
+
 public class WifiInfoTest extends AndroidTestCase {
     private static class MySync {
         int expectedState = STATE_NULL;
@@ -127,10 +130,21 @@
         wifiInfo.getHiddenSSID();
         wifiInfo.getMacAddress();
         setWifiEnabled(false);
-        Thread.sleep(DURATION);
-        wifiInfo = mWifiManager.getConnectionInfo();
-        assertEquals(-1, wifiInfo.getNetworkId());
-        assertEquals(WifiManager.WIFI_STATE_DISABLED, mWifiManager.getWifiState());
+
+        PollingCheck.check("getNetworkId not -1", 20000, new Callable<Boolean>() {
+            @Override
+            public Boolean call() throws Exception {
+                WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
+                return wifiInfo.getNetworkId() == -1;
+            }
+        });
+
+        PollingCheck.check("getWifiState not disabled", 20000, new Callable<Boolean>() {
+           @Override
+            public Boolean call() throws Exception {
+               return mWifiManager.getWifiState() == WifiManager.WIFI_STATE_DISABLED;
+            }
+        });
     }
 
 }
diff --git a/tests/tests/view/src/android/view/cts/ViewTest.java b/tests/tests/view/src/android/view/cts/ViewTest.java
index ac88713..9065592 100644
--- a/tests/tests/view/src/android/view/cts/ViewTest.java
+++ b/tests/tests/view/src/android/view/cts/ViewTest.java
@@ -2445,6 +2445,14 @@
     public void testWindowFocusChanged() {
         final MockView view = (MockView) mActivity.findViewById(R.id.mock_view);
 
+        // Wait until the window has been focused.
+        new PollingCheck(TIMEOUT_DELTA) {
+            @Override
+            protected boolean check() {
+                return view.hasWindowFocus();
+            }
+        }.run();
+
         new PollingCheck() {
             protected boolean check() {
                 return view.hasCalledOnWindowFocusChanged();
@@ -2459,6 +2467,15 @@
         assertFalse(view.hasCalledDispatchWindowFocusChanged());
 
         StubActivity activity = launchActivity("com.android.cts.stub", StubActivity.class, null);
+
+        // Wait until the window lost focus.
+        new PollingCheck(TIMEOUT_DELTA) {
+            @Override
+            protected boolean check() {
+                return !view.hasWindowFocus();
+            }
+        }.run();
+
         assertTrue(view.hasCalledOnWindowFocusChanged());
         assertTrue(view.hasCalledDispatchWindowFocusChanged());
 
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 636c376..46f58e6 100755
--- a/tests/tests/view/src/android/view/inputmethod/cts/InputMethodManagerTest.java
+++ b/tests/tests/view/src/android/view/inputmethod/cts/InputMethodManagerTest.java
@@ -60,6 +60,14 @@
     public void testInputMethodManager() throws Throwable {
         Window window = mActivity.getWindow();
         final EditText view = (EditText) window.findViewById(R.id.entry);
+
+        new PollingCheck(1000) {
+            @Override
+            protected boolean check() {
+                return view.hasWindowFocus();
+            }
+        }.run();
+
         runTestOnUiThread(new Runnable() {
            @Override
             public void run() {