diff options
Diffstat (limited to 'tests/src/com')
-rw-r--r-- | tests/src/com/android/contacts/model/Cp2DeviceLocalAccountLocatorTests.java | 218 | ||||
-rw-r--r-- | tests/src/com/android/contacts/model/RawContactDeltaListTests.java | 54 |
2 files changed, 35 insertions, 237 deletions
diff --git a/tests/src/com/android/contacts/model/Cp2DeviceLocalAccountLocatorTests.java b/tests/src/com/android/contacts/model/Cp2DeviceLocalAccountLocatorTests.java deleted file mode 100644 index 7dbf933ff..000000000 --- a/tests/src/com/android/contacts/model/Cp2DeviceLocalAccountLocatorTests.java +++ /dev/null @@ -1,218 +0,0 @@ -/* - * Copyright (C) 2016 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.android.contacts.model; - -import android.content.ContentProvider; -import android.content.ContentResolver; -import android.database.Cursor; -import android.database.MatrixCursor; -import android.net.Uri; -import android.os.CancellationSignal; -import android.provider.ContactsContract; -import android.provider.ContactsContract.RawContacts; -import androidx.annotation.Nullable; -import android.test.AndroidTestCase; -import android.test.mock.MockContentResolver; -import android.test.suitebuilder.annotation.SmallTest; - -import com.android.contacts.test.mocks.MockContentProvider; -import com.android.contacts.tests.FakeDeviceAccountTypeFactory; -import com.android.contacts.util.DeviceLocalAccountTypeFactory; - -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; - -@SmallTest -public class Cp2DeviceLocalAccountLocatorTests extends AndroidTestCase { - - // Basic smoke test that just checks that it doesn't throw when loading from CP2. We don't - // care what CP2 actually contains for this. - public void testShouldNotCrash() { - final DeviceLocalAccountLocator sut = new Cp2DeviceLocalAccountLocator( - getContext().getContentResolver(), - new DeviceLocalAccountTypeFactory.Default(getContext()), - Collections.<String>emptySet()); - sut.getDeviceLocalAccounts(); - // We didn't throw so it passed - } - - public void test_getDeviceLocalAccounts_returnsEmptyListWhenQueryReturnsNull() { - final DeviceLocalAccountLocator sut = createWithQueryResult(null); - assertTrue(sut.getDeviceLocalAccounts().isEmpty()); - } - - public void test_getDeviceLocalAccounts_returnsEmptyListWhenNoRawContactsHaveDeviceType() { - final DeviceLocalAccountLocator sut = createWithQueryResult(queryResult( - "user", "com.example", - "user", "com.example", - "user", "com.example")); - assertTrue(sut.getDeviceLocalAccounts().isEmpty()); - } - - public void test_getDeviceLocalAccounts_returnsListWithItemForNullAccount() { - final DeviceLocalAccountLocator sut = createWithQueryResult(queryResult( - "user", "com.example", - null, null, - "user", "com.example", - null, null)); - - assertEquals(1, sut.getDeviceLocalAccounts().size()); - } - - public void test_getDeviceLocalAccounts_containsItemForEachDeviceAccount() { - final DeviceLocalAccountTypeFactory stubFactory = new FakeDeviceAccountTypeFactory() - .withDeviceTypes(null, "vnd.sec.contact.phone") - .withSimTypes("vnd.sec.contact.sim"); - final DeviceLocalAccountLocator sut = createLocator(queryResult( - "user", "com.example", - "user", "com.example", - "phone_account", "vnd.sec.contact.phone", - null, null, - "phone_account", "vnd.sec.contact.phone", - "user", "com.example", - null, null, - "sim_account", "vnd.sec.contact.sim", - "sim_account_2", "vnd.sec.contact.sim" - ), stubFactory); - - - assertEquals(4, sut.getDeviceLocalAccounts().size()); - } - - public void test_getDeviceLocalAccounts_doesNotContainItemsForKnownAccountTypes() { - final Cp2DeviceLocalAccountLocator sut = new Cp2DeviceLocalAccountLocator( - getContext().getContentResolver(), new FakeDeviceAccountTypeFactory(), - new HashSet<>(Arrays.asList("com.example", "com.example.1"))); - - assertTrue("Selection should filter known accounts", - sut.getSelection().contains("NOT IN (?,?)")); - - final List<String> args = Arrays.asList(sut.getSelectionArgs()); - assertEquals(2, args.size()); - assertTrue("Selection args is missing an expected value", args.contains("com.example")); - assertTrue("Selection args is missing an expected value", args.contains("com.example.1")); - } - - public void test_getDeviceLocalAccounts_includesAccountsFromSettings() { - final DeviceLocalAccountTypeFactory stubFactory = new FakeDeviceAccountTypeFactory() - .withDeviceTypes(null, "vnd.sec.contact.phone") - .withSimTypes("vnd.sec.contact.sim"); - final DeviceLocalAccountLocator sut = createLocator(new FakeContactsProvider() - .withQueryResult(ContactsContract.Settings.CONTENT_URI, queryResult( - "phone_account", "vnd.sec.contact.phone", - "sim_account", "vnd.sec.contact.sim" - )), stubFactory); - - assertEquals(2, sut.getDeviceLocalAccounts().size()); - } - - public void test_getDeviceLocalAccounts_includesAccountsFromGroups() { - final DeviceLocalAccountTypeFactory stubFactory = new FakeDeviceAccountTypeFactory() - .withDeviceTypes(null, "vnd.sec.contact.phone") - .withSimTypes("vnd.sec.contact.sim"); - final DeviceLocalAccountLocator sut = createLocator(new FakeContactsProvider() - .withQueryResult(ContactsContract.Groups.CONTENT_URI, queryResult( - "phone_account", "vnd.sec.contact.phone", - "sim_account", "vnd.sec.contact.sim" - )), stubFactory); - - assertEquals(2, sut.getDeviceLocalAccounts().size()); - } - - private DeviceLocalAccountLocator createWithQueryResult( - Cursor cursor) { - return createLocator(cursor, new DeviceLocalAccountTypeFactory.Default(mContext)); - } - - private DeviceLocalAccountLocator createLocator(ContentProvider contactsProvider, - DeviceLocalAccountTypeFactory localAccountTypeFactory) { - final DeviceLocalAccountLocator locator = new Cp2DeviceLocalAccountLocator( - createContentResolverWithProvider(contactsProvider), - localAccountTypeFactory, Collections.<String>emptySet()); - return locator; - } - - private DeviceLocalAccountLocator createLocator(Cursor cursor, - DeviceLocalAccountTypeFactory localAccountTypeFactory) { - final DeviceLocalAccountLocator locator = new Cp2DeviceLocalAccountLocator( - createStubResolverWithContentQueryResult(cursor), - localAccountTypeFactory, - Collections.<String>emptySet()); - return locator; - } - - private ContentResolver createContentResolverWithProvider(ContentProvider contactsProvider) { - final MockContentResolver resolver = new MockContentResolver(); - resolver.addProvider(ContactsContract.AUTHORITY, contactsProvider); - return resolver; - } - - private ContentResolver createStubResolverWithContentQueryResult(Cursor cursor) { - final MockContentResolver resolver = new MockContentResolver(); - resolver.addProvider(ContactsContract.AUTHORITY, new FakeContactsProvider() - .withDefaultQueryResult(cursor)); - return resolver; - } - - private Cursor queryResult(String... nameTypePairs) { - final MatrixCursor cursor = new MatrixCursor(new String[] - { RawContacts.ACCOUNT_NAME, RawContacts.ACCOUNT_TYPE, RawContacts.DATA_SET }); - for (int i = 0; i < nameTypePairs.length; i+=2) { - cursor.newRow().add(nameTypePairs[i]).add(nameTypePairs[i+1]) - .add(null); - } - return cursor; - } - - private static class FakeContactsProvider extends MockContentProvider { - public Cursor mNextQueryResult; - public Map<Uri, Cursor> mNextResultMapping = new HashMap<>(); - - public FakeContactsProvider() {} - - public FakeContactsProvider withDefaultQueryResult(Cursor cursor) { - mNextQueryResult = cursor; - return this; - } - - public FakeContactsProvider withQueryResult(Uri uri, Cursor cursor) { - mNextResultMapping.put(uri, cursor); - return this; - } - - @Override - public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, - String sortOrder) { - return query(uri, projection, selection, selectionArgs, sortOrder, null); - } - - @Nullable - @Override - public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, - String sortOrder, CancellationSignal cancellationSignal) { - final Cursor result = mNextResultMapping.get(uri); - if (result == null) { - return mNextQueryResult; - } else { - return result; - } - } - } -} diff --git a/tests/src/com/android/contacts/model/RawContactDeltaListTests.java b/tests/src/com/android/contacts/model/RawContactDeltaListTests.java index 03e1d4f8f..d9ff184c0 100644 --- a/tests/src/com/android/contacts/model/RawContactDeltaListTests.java +++ b/tests/src/com/android/contacts/model/RawContactDeltaListTests.java @@ -17,6 +17,8 @@ package com.android.contacts.model; import android.content.ContentProviderOperation; +import android.content.ContentProviderResult; +import android.content.ContentUris; import android.content.ContentValues; import android.content.Context; import android.net.Uri; @@ -56,6 +58,8 @@ public class RawContactDeltaListTests extends AndroidTestCase { public static final long CONTACT_BOB = 10; public static final long CONTACT_MARY = 11; + public static final long INSERTED_CONTACT_ID = 3; + public static final long PHONE_RED = 20; public static final long PHONE_GREEN = 21; public static final long PHONE_BLUE = 22; @@ -182,6 +186,8 @@ public class RawContactDeltaListTests extends AndroidTestCase { static void assertDiffPattern(ArrayList<CPOWrapper> diff, CPOWrapper... pattern) { assertEquals("Unexpected operations", pattern.length, diff.size()); + + ContentProviderResult[] fakeBackReferences = new ContentProviderResult[diff.size()]; for (int i = 0; i < pattern.length; i++) { final CPOWrapper expected = pattern[i]; final CPOWrapper found = diff.get(i); @@ -193,21 +199,25 @@ public class RawContactDeltaListTests extends AndroidTestCase { final String foundType = getTypeString(found); assertEquals("Unexpected type", expectedType, foundType); - if (CompatUtils.isDeleteCompat(expected)) continue; + if (CompatUtils.isDeleteCompat(expected)) { + continue; + } - try { - final ContentValues expectedValues = getValues(expected.getOperation()); - final ContentValues foundValues = getValues(found.getOperation()); + if (CompatUtils.isInsertCompat(found)) { + fakeBackReferences[i] = new ContentProviderResult( + ContentUris.withAppendedId(RawContacts.CONTENT_URI, INSERTED_CONTACT_ID)); + } else if (CompatUtils.isUpdateCompat(found)) { + fakeBackReferences[i] = new ContentProviderResult(1); + } - expectedValues.remove(BaseColumns._ID); - foundValues.remove(BaseColumns._ID); - assertEquals("Unexpected values", expectedValues, foundValues); - } catch (NoSuchFieldException e) { - fail(e.toString()); - } catch (IllegalAccessException e) { - fail(e.toString()); - } + ContentValues expectedValues = expected.getOperation().resolveValueBackReferences( + new ContentProviderResult[0], 0); + ContentValues foundValues = found.getOperation().resolveValueBackReferences( + fakeBackReferences, fakeBackReferences.length); + expectedValues.remove(BaseColumns._ID); + foundValues.remove(BaseColumns._ID); + assertEquals("Unexpected values", expectedValues, foundValues); } } @@ -247,6 +257,7 @@ public class RawContactDeltaListTests extends AndroidTestCase { static CPOWrapper buildUpdateAggregationKeepTogether(long rawContactId) { final ContentValues values = new ContentValues(); values.put(AggregationExceptions.RAW_CONTACT_ID1, rawContactId); + values.put(AggregationExceptions.RAW_CONTACT_ID2, INSERTED_CONTACT_ID); values.put(AggregationExceptions.TYPE, AggregationExceptions.TYPE_KEEP_TOGETHER); return buildCPOWrapper(AggregationExceptions.CONTENT_URI, TYPE_UPDATE, values); } @@ -462,7 +473,8 @@ public class RawContactDeltaListTests extends AndroidTestCase { assertDiffPattern(first, buildAssertVersion(VER_FIRST), buildUpdateAggregationSuspended(), - buildCPOWrapper(Data.CONTENT_URI, TYPE_INSERT, buildDataInsert(bluePhone, CONTACT_BOB)), + buildCPOWrapper(Data.CONTENT_URI, TYPE_INSERT, + buildDataInsert(bluePhone, CONTACT_BOB)), buildUpdateAggregationDefault()); // Merge in the second version, verify that our insert remains @@ -470,7 +482,8 @@ public class RawContactDeltaListTests extends AndroidTestCase { assertDiffPattern(merged, buildAssertVersion(VER_SECOND), buildUpdateAggregationSuspended(), - buildCPOWrapper(Data.CONTENT_URI, TYPE_INSERT, buildDataInsert(bluePhone, CONTACT_BOB)), + buildCPOWrapper(Data.CONTENT_URI, TYPE_INSERT, + buildDataInsert(bluePhone, CONTACT_BOB)), buildUpdateAggregationDefault()); } @@ -479,10 +492,11 @@ public class RawContactDeltaListTests extends AndroidTestCase { VER_FIRST, buildPhone(PHONE_RED))); final RawContactDeltaList second = buildSet(buildBeforeEntity(mContext, CONTACT_BOB, VER_SECOND, buildPhone(PHONE_RED)), buildBeforeEntity(mContext, CONTACT_MARY, - VER_SECOND, buildPhone(PHONE_RED))); + VER_SECOND, buildPhone(PHONE_RED))); // Add new contact locally, should remain insert final ContentValues joePhoneInsert = buildPhone(PHONE_BLUE); + joePhoneInsert.put(Data.RAW_CONTACT_ID, INSERTED_CONTACT_ID); final RawContactDelta joeContact = buildAfterEntity(joePhoneInsert); final ContentValues joeContactInsert = joeContact.getValues().getCompleteValues(); joeContactInsert.put(RawContacts.AGGREGATION_MODE, RawContacts.AGGREGATION_MODE_SUSPENDED); @@ -542,6 +556,7 @@ public class RawContactDeltaListTests extends AndroidTestCase { buildUpdateAggregationDefault()); final ContentValues phoneInsert = phone.getCompleteValues(); + phoneInsert.put(Data.RAW_CONTACT_ID, INSERTED_CONTACT_ID); final ContentValues contactInsert = first.getByRawContactId(CONTACT_MARY).getValues() .getCompleteValues(); contactInsert.put(RawContacts.AGGREGATION_MODE, RawContacts.AGGREGATION_MODE_SUSPENDED); @@ -562,11 +577,11 @@ public class RawContactDeltaListTests extends AndroidTestCase { final RawContactDeltaList second = buildSet(buildBeforeEntity(mContext, CONTACT_BOB, VER_SECOND, buildPhone(PHONE_RED))); - assertEquals((Long)VER_FIRST, getVersion(first, CONTACT_BOB)); - assertEquals((Long)VER_SECOND, getVersion(second, CONTACT_BOB)); + assertEquals((Long) VER_FIRST, getVersion(first, CONTACT_BOB)); + assertEquals((Long) VER_SECOND, getVersion(second, CONTACT_BOB)); final RawContactDeltaList merged = RawContactDeltaList.mergeAfter(second, first); - assertEquals((Long)VER_SECOND, getVersion(merged, CONTACT_BOB)); + assertEquals((Long) VER_SECOND, getVersion(merged, CONTACT_BOB)); } public void testMergeAfterEnsureAndTrim() { @@ -585,7 +600,8 @@ public class RawContactDeltaListTests extends AndroidTestCase { assertDiffPattern(first, buildAssertVersion(VER_FIRST), buildUpdateAggregationSuspended(), - buildCPOWrapper(Data.CONTENT_URI, TYPE_INSERT, buildDataInsert(bobPhone, CONTACT_BOB)), + buildCPOWrapper(Data.CONTENT_URI, TYPE_INSERT, + buildDataInsert(bobPhone, CONTACT_BOB)), buildUpdateAggregationDefault()); // Trim values and ensure that we don't insert things |