Merge "Fix window focus change timing issue" into jb-dev
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/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);