Fix ContentResolverSyncTestCase#testCancelSync

Previously, the cancelSync() method just used a latch to wait for the
cancel method to be called.

However from the sync manager's POV, this is not when a sync finishes.
So we actually need to wait until the sync manager
thinks it's finished in cancelSync().

Bug: 64117972
Test: cts-tradefed run cts-dev --skip-device-info --skip-preconditions --skip-system-status-check com.android.compatibility.common.tradefed.targetprep.NetworkConnectivityChecker -a armeabi-v7a -l INFO -m CtsContentTestCases -t android.content.cts.ContentResolverSyncTestCase

Change-Id: I2c35e1f7adbc402ac585046f36020451422c516e
diff --git a/tests/tests/content/src/android/content/cts/ContentResolverSyncTestCase.java b/tests/tests/content/src/android/content/cts/ContentResolverSyncTestCase.java
index 86e66cc..bc37c59 100644
--- a/tests/tests/content/src/android/content/cts/ContentResolverSyncTestCase.java
+++ b/tests/tests/content/src/android/content/cts/ContentResolverSyncTestCase.java
@@ -26,13 +26,16 @@
 import android.content.Context;
 import android.content.SyncAdapterType;
 import android.os.Bundle;
+import android.os.SystemClock;
 import android.test.AndroidTestCase;
+import android.util.Log;
 
 import java.io.IOException;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 
 public class ContentResolverSyncTestCase extends AndroidTestCase {
+    private static final String TAG = "SyncTest";
 
     private static final String AUTHORITY = "android.content.cts.authority";
 
@@ -40,6 +43,7 @@
             MockAccountAuthenticator.ACCOUNT_TYPE);
 
     private static final int INITIAL_SYNC_TIMEOUT_MS = 60 * 1000;
+    private static final int CANCEL_TIMEOUT_MS = 60 * 1000;
     private static final int LATCH_TIMEOUT_MS = 5000;
 
     private static AccountManager sAccountManager;
@@ -133,6 +137,20 @@
         } catch (InterruptedException e) {
             fail("should not throw an InterruptedException");
         }
+        // Make sure the sync manager thinks the sync finished.
+
+        final long timeout = SystemClock.uptimeMillis() + CANCEL_TIMEOUT_MS;
+        while (SystemClock.uptimeMillis() < timeout) {
+            if (!ContentResolver.isSyncActive(ACCOUNT, AUTHORITY)
+                && !ContentResolver.isSyncPending(ACCOUNT, AUTHORITY)) {
+                break;
+            }
+            Log.i(TAG, "Waiting for sync to finish...");
+            try {
+                Thread.sleep(500);
+            } catch (InterruptedException e) {
+            }
+        }
     }
 
     private void requestSync(Account account, String authority, int latchTimeoutMillis) {