Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e34c4ebd authored by John Shao's avatar John Shao
Browse files

Fix unit tests in AOSP Contacts

Fixes: 142947789
Change-Id: I7b36f8066819d59db6a2291160b1713284c14d12
Test: atest ContactsTests
parent 83d09899
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -2,6 +2,8 @@

Build and install Contacts.apk

Grant Contacts permissions to Contacts.apk

Run
```shell
$ atest ContactsTests
+3 −3
Original line number Diff line number Diff line
@@ -327,9 +327,9 @@ public class SimImportActivityTest {
        mDevice.findObject(By.textContains(targetAccount.name)).click();
        mDevice.waitForIdle();

        assertTrue(mDevice.wait(Until.hasObject(By.text("Import One").checked(false).enabled(false)), TIMEOUT));
        assertTrue(mDevice.hasObject(By.text("Import Three").checked(false).enabled(false)));
        assertTrue(mDevice.hasObject(By.text("Import Six").checked(false).enabled(false)));
        assertTrue(mDevice.wait(Until.hasObject(By.text("Import One").checked(false)), TIMEOUT));
        assertTrue(mDevice.hasObject(By.text("Import Three").checked(false)));
        assertTrue(mDevice.hasObject(By.text("Import Six").checked(false)));

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            contactsProviderClient.close();
+4 −4
Original line number Diff line number Diff line
@@ -96,7 +96,7 @@ public class AccountTypeManagerTest extends AndroidTestCase {
    }

    public void testGetDefaultAccount_NoAccounts_DefaultPreferenceSet() {
        when(mPrefs.getString(Mockito.anyString(), Mockito.anyString())).thenReturn(
        when(mPrefs.getString(Mockito.anyString(), Mockito.any())).thenReturn(
                getDefaultAccountPreference("name1", GoogleAccountType.ACCOUNT_TYPE));
        assertNull(getDefaultGoogleAccountName());
    }
@@ -108,21 +108,21 @@ public class AccountTypeManagerTest extends AndroidTestCase {

    public void testGetDefaultAccount_DefaultAccountPreferenceSet() {
        when(mAccountManager.getAccountsByType(Mockito.anyString())).thenReturn(ACCOUNTS);
        when(mPrefs.getString(Mockito.anyString(), Mockito.anyString())).thenReturn(
        when(mPrefs.getString(Mockito.anyString(), Mockito.any())).thenReturn(
                getDefaultAccountPreference("name2", GoogleAccountType.ACCOUNT_TYPE));
        assertEquals("name2", getDefaultGoogleAccountName());
    }

    public void testGetDefaultAccount_DefaultAccountPreferenceSet_NonGoogleAccountType() {
        when(mAccountManager.getAccountsByType(Mockito.anyString())).thenReturn(ACCOUNTS);
        when(mPrefs.getString(Mockito.anyString(), Mockito.anyString())).thenReturn(
        when(mPrefs.getString(Mockito.anyString(), Mockito.any())).thenReturn(
                getDefaultAccountPreference("name3", "type3"));
        assertEquals("name1", getDefaultGoogleAccountName());
    }

    public void testGetDefaultAccount_DefaultAccountPreferenceSet_UnknownName() {
        when(mAccountManager.getAccountsByType(Mockito.anyString())).thenReturn(ACCOUNTS);
        when(mPrefs.getString(Mockito.anyString(), Mockito.anyString())).thenReturn(
        when(mPrefs.getString(Mockito.anyString(), Mockito.any())).thenReturn(
                getDefaultAccountPreference("name4",GoogleAccountType.ACCOUNT_TYPE));
        assertEquals("name1", getDefaultGoogleAccountName());
    }
+5 −5
Original line number Diff line number Diff line
@@ -56,7 +56,7 @@ public class ContactsPreferencesTest extends InstrumentationTestCase {
        Mockito.when(mResources.getString(Mockito.anyInt()))
                .thenReturn(ACCOUNT_KEY); // contact_editor_default_account_key

        Mockito.when(mContext.getSharedPreferences(Mockito.anyString(), Mockito.anyInt()))
        Mockito.when(mContext.getSharedPreferences(Mockito.any(), Mockito.anyInt()))
                .thenReturn(mSharedPreferences);
        Mockito.when(mSharedPreferences.contains(ContactsPreferences.SORT_ORDER_KEY))
                .thenReturn(true);
@@ -184,7 +184,7 @@ public class ContactsPreferencesTest extends InstrumentationTestCase {
        mContactsPreferences = new ContactsPreferences(mContext,
                /* isDefaultAccountUserChangeable */ true);

        Mockito.when(mSharedPreferences.getString(Mockito.eq(ACCOUNT_KEY), Mockito.anyString()))
        Mockito.when(mSharedPreferences.getString(Mockito.eq(ACCOUNT_KEY), Mockito.any()))
                .thenReturn(new AccountWithDataSet("name1", "type1", "dataset1").stringify(),
                        new AccountWithDataSet("name2", "type2", "dataset2").stringify());

@@ -199,7 +199,7 @@ public class ContactsPreferencesTest extends InstrumentationTestCase {
    public void testShouldShowAccountChangedNotificationIfAccountNotSaved() {
        mContactsPreferences = new ContactsPreferences(mContext,
                /* isDefaultAccountUserChangeable */ true);
        Mockito.when(mSharedPreferences.getString(Mockito.eq(ACCOUNT_KEY), Mockito.anyString()))
        Mockito.when(mSharedPreferences.getString(Mockito.eq(ACCOUNT_KEY), Mockito.any()))
                .thenReturn(null);

        assertTrue("Should prompt to change default if no default is saved",
@@ -211,7 +211,7 @@ public class ContactsPreferencesTest extends InstrumentationTestCase {
    public void testShouldShowAccountChangedNotification() {
        mContactsPreferences = new ContactsPreferences(mContext,
                /* isDefaultAccountUserChangeable */ true);
        Mockito.when(mSharedPreferences.getString(Mockito.eq(ACCOUNT_KEY), Mockito.anyString()))
        Mockito.when(mSharedPreferences.getString(Mockito.eq(ACCOUNT_KEY), Mockito.any()))
                .thenReturn(new AccountWithDataSet("name", "type", "dataset").stringify());

        assertFalse("Should not prompt to change default if current default exists",
@@ -228,7 +228,7 @@ public class ContactsPreferencesTest extends InstrumentationTestCase {
    public void testShouldShowAccountChangedNotificationWhenThereIsOneAccount() {
        mContactsPreferences = new ContactsPreferences(mContext,
                /* isDefaultAccountUserChangeable */ true);
        Mockito.when(mSharedPreferences.getString(Mockito.eq(ACCOUNT_KEY), Mockito.anyString()))
        Mockito.when(mSharedPreferences.getString(Mockito.eq(ACCOUNT_KEY), Mockito.any()))
                .thenReturn(null);

        // Normally we would prompt because there is no default set but if there is just one
+6 −0
Original line number Diff line number Diff line
@@ -23,8 +23,10 @@ import android.content.OperationApplicationException;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.net.Uri;
import android.os.Binder;
import android.os.Bundle;
import android.os.CancellationSignal;
import android.os.IBinder;
import android.os.ParcelFileDescriptor;
import android.os.RemoteException;
import androidx.annotation.Nullable;
@@ -203,4 +205,8 @@ public class ForwardingContentProvider extends android.test.mock.MockContentProv
            throw new RuntimeException(e);
        }
    }

    public IBinder getIContentProviderBinder() {
        return new Binder();
    }
}