resolve merge conflicts of 949d4e8 to master

Change-Id: I399652b6a1b756a8045c1fef092d10581d56400d
diff --git a/tests/AndroidManifest.xml b/tests/AndroidManifest.xml
index 65be28a..4e53114 100644
--- a/tests/AndroidManifest.xml
+++ b/tests/AndroidManifest.xml
@@ -98,6 +98,11 @@
         android:label="Contacts app tests">
     </instrumentation>
 
+    <instrumentation android:name="android.support.test.runner.AndroidJUnitRunner"
+        android:targetPackage="com.android.contacts"
+        android:label="Contacts app tests">
+    </instrumentation>
+
     <instrumentation android:name="com.android.contacts.ContactsLaunchPerformance"
         android:targetPackage="com.android.contacts"
         android:label="Contacts launch performance">
diff --git a/tests/src/com/android/contacts/common/model/AccountWithDataSetTest.java b/tests/src/com/android/contacts/common/model/AccountWithDataSetTest.java
index 7bfb922..9339063 100644
--- a/tests/src/com/android/contacts/common/model/AccountWithDataSetTest.java
+++ b/tests/src/com/android/contacts/common/model/AccountWithDataSetTest.java
@@ -59,11 +59,11 @@
     }
 
     public void testStringifyAndUnstringifyLocalAccount() {
-        final String stringified = AccountWithDataSet.getLocalAccount().stringify();
+        final String stringified = AccountWithDataSet.getNullAccount().stringify();
 
         final AccountWithDataSet restored = AccountWithDataSet.unstringify(stringified);
 
-        assertEquals(AccountWithDataSet.getLocalAccount(), restored);
+        assertEquals(AccountWithDataSet.getNullAccount(), restored);
     }
 
     public void testStringifyListAndUnstringify() {
diff --git a/tests/src/com/android/contacts/common/preference/ContactsPreferencesTest.java b/tests/src/com/android/contacts/common/preference/ContactsPreferencesTest.java
index a841902..8400737 100644
--- a/tests/src/com/android/contacts/common/preference/ContactsPreferencesTest.java
+++ b/tests/src/com/android/contacts/common/preference/ContactsPreferencesTest.java
@@ -31,6 +31,8 @@
 import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
 
+import java.util.Arrays;
+
 @SmallTest
 public class ContactsPreferencesTest extends InstrumentationTestCase {
 
@@ -139,9 +141,8 @@
     }
 
     public void testRefreshDefaultAccount() throws InterruptedException {
-        Mockito.when(mResources.getBoolean(Mockito.anyInt())).thenReturn(
-                true // R.bool.config_default_account_user_changeable
-        );
+        mContactsPreferences = new ContactsPreferences(mContext,
+                /* isDefaultAccountUserChangeable */ true);
 
         Mockito.when(mSharedPreferences.getString(Mockito.eq(ACCOUNT_KEY), Mockito.anyString()))
                 .thenReturn(new AccountWithDataSet("name1", "type1", "dataset1").stringify(),
@@ -154,4 +155,46 @@
         Assert.assertEquals(new AccountWithDataSet("name2", "type2", "dataset2"),
                 mContactsPreferences.getDefaultAccount());
     }
+
+    public void testShouldShowAccountChangedNotificationIfAccountNotSaved() {
+        mContactsPreferences = new ContactsPreferences(mContext,
+                /* isDefaultAccountUserChangeable */ true);
+        Mockito.when(mSharedPreferences.getString(Mockito.eq(ACCOUNT_KEY), Mockito.anyString()))
+                .thenReturn(null);
+
+        assertTrue("Should prompt to change default if no default is saved",
+                mContactsPreferences.shouldShowAccountChangedNotification(Arrays.asList(
+                        new AccountWithDataSet("name1", "type1", "dataset1"),
+                        new AccountWithDataSet("name2", "type2", "dataset2"))));
+    }
+
+    public void testShouldShowAccountChangedNotification() {
+        mContactsPreferences = new ContactsPreferences(mContext,
+                /* isDefaultAccountUserChangeable */ true);
+        Mockito.when(mSharedPreferences.getString(Mockito.eq(ACCOUNT_KEY), Mockito.anyString()))
+                .thenReturn(new AccountWithDataSet("name", "type", "dataset").stringify());
+
+        assertFalse("Should not prompt to change default if current default exists",
+                mContactsPreferences.shouldShowAccountChangedNotification(Arrays.asList(
+                        new AccountWithDataSet("name", "type", "dataset"),
+                        new AccountWithDataSet("name1", "type1", "dataset1"))));
+
+        assertTrue("Should prompt to change default if current default does not exist",
+                mContactsPreferences.shouldShowAccountChangedNotification(Arrays.asList(
+                        new AccountWithDataSet("name1", "type1", "dataset1"),
+                        new AccountWithDataSet("name2", "type2", "dataset2"))));
+    }
+
+    public void testShouldShowAccountChangedNotificationWhenThereIsOneAccount() {
+        mContactsPreferences = new ContactsPreferences(mContext,
+                /* isDefaultAccountUserChangeable */ true);
+        Mockito.when(mSharedPreferences.getString(Mockito.eq(ACCOUNT_KEY), Mockito.anyString()))
+                .thenReturn(null);
+
+        // Normally we would prompt because there is no default set but if there is just one
+        // account we should just use it.
+        assertFalse("Should not prompt to change default if there is only one account available",
+                mContactsPreferences.shouldShowAccountChangedNotification(Arrays.asList(
+                        new AccountWithDataSet("name", "type", "dataset"))));
+    }
 }
diff --git a/tests/src/com/android/contacts/editor/ContactEditorUtilsTest.java b/tests/src/com/android/contacts/editor/ContactEditorUtilsTest.java
index b5df8c9..525a85c 100644
--- a/tests/src/com/android/contacts/editor/ContactEditorUtilsTest.java
+++ b/tests/src/com/android/contacts/editor/ContactEditorUtilsTest.java
@@ -111,63 +111,18 @@
 
     /**
      * Test for
-     * - {@link ContactEditorUtils#saveDefaultAndAllAccounts}
-     * - {@link ContactEditorUtils#getDefaultAccount}
-     * - {@link ContactEditorUtils#getSavedAccounts()}
+     * - {@link ContactEditorUtils#saveDefaultAccount}
+     * - {@link ContactEditorUtils#getOnlyOrDefaultAccount}
      */
-    public void testSaveDefaultAndAllAccounts() {
+    public void testSaveDefaultAccount() {
         // Use these account types here.
         setAccountTypes(TYPE1, TYPE2);
 
-        // If none has been saved, it should return an empty list.
-        assertEquals(0, mTarget.getSavedAccounts().size());
+        mTarget.saveDefaultAccount(null);
+        assertNull(mTarget.getOnlyOrDefaultAccount());
 
-        // Save 0 accounts.
-        mAccountTypes.mAccounts = new AccountWithDataSet[]{};
-        mTarget.saveDefaultAndAllAccounts(null);
-        assertNull(mTarget.getDefaultAccount());
-        MoreAsserts.assertEquals(
-                Sets.newHashSet(mAccountTypes.mAccounts),
-                toSet(mTarget.getSavedAccounts()));
-
-        // 1 account
-        mAccountTypes.mAccounts = new AccountWithDataSet[]{ACCOUNT_1_A};
-        mTarget.saveDefaultAndAllAccounts(ACCOUNT_1_A);
-        assertEquals(ACCOUNT_1_A, mTarget.getDefaultAccount());
-        MoreAsserts.assertEquals(
-                Sets.newHashSet(mAccountTypes.mAccounts),
-                toSet(mTarget.getSavedAccounts()));
-
-        // 2 accounts
-        mAccountTypes.mAccounts = new AccountWithDataSet[]{ACCOUNT_1_A, ACCOUNT_1_B};
-        mTarget.saveDefaultAndAllAccounts(ACCOUNT_1_B);
-        assertEquals(ACCOUNT_1_B, mTarget.getDefaultAccount());
-        MoreAsserts.assertEquals(
-                Sets.newHashSet(mAccountTypes.mAccounts),
-                toSet(mTarget.getSavedAccounts()));
-
-        // 2 accounts, and save null as the default.  Even though there are accounts, the saved
-        // account list should be empty in this case.
-        mTarget.saveDefaultAndAllAccounts(null);
-        assertNull(mTarget.getDefaultAccount());
-        assertEquals(0, mTarget.getSavedAccounts().size());
-    }
-
-    public void testIsAccountValid() {
-        // Use these account types here.
-        setAccountTypes(TYPE1, TYPE2);
-
-        // 0 accounts
-        mAccountTypes.mAccounts = new AccountWithDataSet[]{};
-        assertFalse(mTarget.isValidAccount(ACCOUNT_1_A));
-        assertTrue(mTarget.isValidAccount(null)); // null is always valid
-
-        // 2 accounts
-        mAccountTypes.mAccounts = new AccountWithDataSet[]{ACCOUNT_1_A, ACCOUNT_2_A};
-        assertTrue(mTarget.isValidAccount(ACCOUNT_1_A));
-        assertTrue(mTarget.isValidAccount(ACCOUNT_2_A));
-        assertFalse(mTarget.isValidAccount(ACCOUNT_2EX_A));
-        assertTrue(mTarget.isValidAccount(null)); // null is always valid
+        mTarget.saveDefaultAccount(ACCOUNT_1_A);
+        assertEquals(ACCOUNT_1_A, mTarget.getOnlyOrDefaultAccount());
     }
 
     /**
@@ -186,7 +141,7 @@
         // Now we open the contact editor with the new account.
 
         // When closing the editor, we save the default account.
-        mTarget.saveDefaultAndAllAccounts(ACCOUNT_1_A);
+        mTarget.saveDefaultAccount(ACCOUNT_1_A);
 
         // Next time the user creates a contact, we don't show the notification.
         assertFalse(mTarget.shouldShowAccountChangedNotification());
@@ -198,7 +153,7 @@
         assertFalse(mTarget.shouldShowAccountChangedNotification());
 
         // User saved a new contact.  We update the account list and the default account.
-        mTarget.saveDefaultAndAllAccounts(ACCOUNT_1_B);
+        mTarget.saveDefaultAccount(ACCOUNT_1_B);
 
         // User created another contact.  Now we don't show the notification.
         assertFalse(mTarget.shouldShowAccountChangedNotification());
@@ -214,7 +169,7 @@
         assertFalse(mTarget.shouldShowAccountChangedNotification());
 
         // User saves a new contact, with a different default account.
-        mTarget.saveDefaultAndAllAccounts(ACCOUNT_2_A);
+        mTarget.saveDefaultAccount(ACCOUNT_2_A);
 
         // Next time user creates a contact, no notification.
         assertFalse(mTarget.shouldShowAccountChangedNotification());
@@ -253,7 +208,7 @@
         assertFalse(mTarget.shouldShowAccountChangedNotification());
 
         // User saves a new contact.
-        mTarget.saveDefaultAndAllAccounts(ACCOUNT_1_A);
+        mTarget.saveDefaultAccount(ACCOUNT_1_A);
 
         // Next time, no notification.
         assertFalse(mTarget.shouldShowAccountChangedNotification());
@@ -272,7 +227,7 @@
         assertTrue(mTarget.shouldShowAccountChangedNotification());
 
         // We show the notification here, and user clicked "keep local" and saved an contact.
-        mTarget.saveDefaultAndAllAccounts(AccountWithDataSet.getLocalAccount());
+        mTarget.saveDefaultAccount(AccountWithDataSet.getNullAccount());
 
         // Now there are no accounts, and default account is null.
 
@@ -285,7 +240,7 @@
         setAccountTypes(TYPE1);
         setAccounts(ACCOUNT_1_A);
 
-        mTarget.saveDefaultAndAllAccounts(ACCOUNT_1_A);
+        mTarget.saveDefaultAccount(ACCOUNT_1_A);
 
         // Right after a save, the dialog shouldn't show up.
         assertFalse(mTarget.shouldShowAccountChangedNotification());
diff --git a/tests/src/com/android/contacts/editor/EditorUiUtilsTest.java b/tests/src/com/android/contacts/editor/EditorUiUtilsTest.java
index e68511f..aa3f725 100644
--- a/tests/src/com/android/contacts/editor/EditorUiUtilsTest.java
+++ b/tests/src/com/android/contacts/editor/EditorUiUtilsTest.java
@@ -122,7 +122,7 @@
     }
 
   public void testGetAccountInfo_AccountType_DeviceAccount() {
-      final AccountWithDataSet deviceAccount = AccountWithDataSet.getLocalAccount();
+      final AccountWithDataSet deviceAccount = AccountWithDataSet.getNullAccount();
       final AccountDisplayInfo account = new AccountDisplayInfo(deviceAccount, "Device",
               "Device", /*icon*/ null, /*isDeviceAccount*/ true);