Fixed bugs in test case that caused test failure.

- reversed logic in setNewLatch() was nulling out mLatch in MockSyncAdapter
immediately after setting it, so the latch never signalled.
- tests that initiated a sync weren't cleaning up after themselves, leading
to leftover state that affected subsequent tests

b/4479894

TESTED = runs on m4 without test failures

Change-Id: I038aba89a5c7a944a90ef8b6fba07fd4630d7906
diff --git a/tests/tests/content/src/android/content/cts/ContentResolverSyncTestCase.java b/tests/tests/content/src/android/content/cts/ContentResolverSyncTestCase.java
index 1cc4cfb..2da0bfb 100644
--- a/tests/tests/content/src/android/content/cts/ContentResolverSyncTestCase.java
+++ b/tests/tests/content/src/android/content/cts/ContentResolverSyncTestCase.java
@@ -57,6 +57,9 @@
         // Need to clean up created account
         removeAccount(sAccountManager, ACCOUNT, null /* callback */);
 
+        // Need to cancel any sync that was started.
+        cancelSync(null, AUTHORITY, LATCH_TIMEOUT_MS);
+
         super.tearDown();
     }
 
@@ -87,8 +90,8 @@
     }
 
     private CountDownLatch setNewLatch(CountDownLatch latch) {
-        getMockSyncAdapter().setLatch(latch);
         getMockSyncAdapter().clearData();
+        getMockSyncAdapter().setLatch(latch);
         return latch;
     }
 
@@ -101,7 +104,9 @@
 
         // Wait with timeout for the callback to do its work
         try {
-            latch.await(latchTimeoutMs, TimeUnit.MILLISECONDS);
+            if (!latch.await(latchTimeoutMs, TimeUnit.MILLISECONDS)) {
+                fail("should not time out waiting on latch");
+            }
         } catch (InterruptedException e) {
             fail("should not throw an InterruptedException");
         }