Fix bug #2898737 (test for syncing multiple accounts)

- add unit test
- some refactoring

Change-Id: I7b9da883e8f5184e329fe65e3d4319428d61d6dc
diff --git a/tests/src/android/content/cts/MockSyncAdapter.java b/tests/src/android/content/cts/MockSyncAdapter.java
index 32b3a8d..df24749 100644
--- a/tests/src/android/content/cts/MockSyncAdapter.java
+++ b/tests/src/android/content/cts/MockSyncAdapter.java
@@ -23,13 +23,14 @@
 import android.os.Bundle;
 import android.os.RemoteException;
 
+import java.util.ArrayList;
 import java.util.concurrent.CountDownLatch;
 
 public class MockSyncAdapter extends ISyncAdapter.Stub {
 
     private static MockSyncAdapter sSyncAdapter = null;
 
-    private Account mAccount;
+    private ArrayList<Account> mAccounts = new ArrayList<Account>();
     private String mAuthority;
     private Bundle mExtras;
     private boolean mInitialized;
@@ -37,8 +38,8 @@
     private boolean mCancelSync;
     private CountDownLatch mLatch;
 
-    public Account getAccount() {
-        return mAccount;
+    public ArrayList<Account> getAccounts() {
+        return mAccounts;
     }
 
     public String getAuthority() {
@@ -62,7 +63,7 @@
     }
 
     public void clearData() {
-        mAccount = null;
+        mAccounts.clear();
         mAuthority = null;
         mExtras = null;
         mInitialized = false;
@@ -78,7 +79,7 @@
     public void startSync(ISyncContext syncContext, String authority, Account account,
             Bundle extras) throws RemoteException {
 
-        mAccount = account;
+        mAccounts.add(account);
         mAuthority = authority;
         mExtras = extras;
 
@@ -98,7 +99,7 @@
     }
 
     public void cancelSync(ISyncContext syncContext) throws RemoteException {
-        mAccount = null;
+        mAccounts.clear();
         mAuthority = null;
         mExtras = null;
 
@@ -114,7 +115,7 @@
     public void initialize(android.accounts.Account account, java.lang.String authority)
             throws android.os.RemoteException {
 
-        mAccount = account;
+        mAccounts.add(account);
         mAuthority = authority;
 
         mInitialized = true;
diff --git a/tests/tests/content/src/android/content/cts/ContentResolverSyncTestCase.java b/tests/tests/content/src/android/content/cts/ContentResolverSyncTestCase.java
index 032f10a..4d7ca6d 100644
--- a/tests/tests/content/src/android/content/cts/ContentResolverSyncTestCase.java
+++ b/tests/tests/content/src/android/content/cts/ContentResolverSyncTestCase.java
@@ -93,7 +93,7 @@
     }
 
     private void addAccountAndVerifyInitSync(Account account, String password,
-            String authority, int latchTimeoutMs) {
+            String authority, int latchTimeoutMs, int accountIndex) {
 
         CountDownLatch latch = setNewLatch(new CountDownLatch(1));
 
@@ -109,7 +109,7 @@
         assertFalse(getMockSyncAdapter().isStartSync());
         assertFalse(getMockSyncAdapter().isCancelSync());
         assertTrue(getMockSyncAdapter().isInitialized());
-        assertEquals(account, getMockSyncAdapter().getAccount());
+        assertEquals(account, getMockSyncAdapter().getAccounts().get(accountIndex));
         assertEquals(authority, getMockSyncAdapter().getAuthority());
     }
 
@@ -159,8 +159,11 @@
         ContentResolver.setMasterSyncAutomatically(false);
         assertEquals(false, ContentResolver.getMasterSyncAutomatically());
 
-        addAccountAndVerifyInitSync(ACCOUNT, MockAccountAuthenticator.ACCOUNT_PASSWORD, AUTHORITY,
-                LATCH_TIMEOUT_MS);
+        addAccountAndVerifyInitSync(ACCOUNT,
+                MockAccountAuthenticator.ACCOUNT_PASSWORD,
+                AUTHORITY,
+                LATCH_TIMEOUT_MS,
+                0);
 
         getMockSyncAdapter().clearData();
 
@@ -174,7 +177,7 @@
         assertTrue(getMockSyncAdapter().isStartSync());
         assertFalse(getMockSyncAdapter().isCancelSync());
         assertFalse(getMockSyncAdapter().isInitialized());
-        assertEquals(ACCOUNT, getMockSyncAdapter().getAccount());
+        assertEquals(ACCOUNT, getMockSyncAdapter().getAccounts().get(0));
         assertEquals(AUTHORITY, getMockSyncAdapter().getAuthority());
     }
 
@@ -188,8 +191,11 @@
         ContentResolver.setMasterSyncAutomatically(false);
         assertEquals(false, ContentResolver.getMasterSyncAutomatically());
 
-        addAccountAndVerifyInitSync(ACCOUNT, MockAccountAuthenticator.ACCOUNT_PASSWORD, AUTHORITY,
-                LATCH_TIMEOUT_MS);
+        addAccountAndVerifyInitSync(ACCOUNT,
+                MockAccountAuthenticator.ACCOUNT_PASSWORD,
+                AUTHORITY,
+                LATCH_TIMEOUT_MS,
+                0);
 
         getMockSyncAdapter().clearData();
 
@@ -315,4 +321,35 @@
             //expected.
         }
     }
+
+    /**
+     * Test to verify that a SyncAdapter is called on all the accounts accounts
+     */
+    public void testCallMultipleAccounts() {
+        // Prevent auto sync
+        ContentResolver.setMasterSyncAutomatically(false);
+        assertEquals(false, ContentResolver.getMasterSyncAutomatically());
+
+        addAccountAndVerifyInitSync(ACCOUNT,
+                MockAccountAuthenticator.ACCOUNT_PASSWORD,
+                AUTHORITY,
+                LATCH_TIMEOUT_MS,
+                0);
+
+        getMockSyncAdapter().clearData();
+
+        setIsSyncable(ACCOUNT, AUTHORITY, true);
+        cancelSync(ACCOUNT, AUTHORITY, LATCH_TIMEOUT_MS);
+
+        getMockSyncAdapter().clearData();
+
+        requestSync(null /* all accounts */, AUTHORITY, LATCH_TIMEOUT_MS);
+
+        assertTrue(getMockSyncAdapter().isStartSync());
+        assertFalse(getMockSyncAdapter().isCancelSync());
+        assertFalse(getMockSyncAdapter().isInitialized());
+        assertEquals(ACCOUNT, getMockSyncAdapter().getAccounts().get(0));
+        assertEquals(AUTHORITY, getMockSyncAdapter().getAuthority());
+
+    }
 }