Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 16 | package com.android.contacts.model; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 17 | |
Marcus Hagerott | bc51084 | 2016-09-09 19:29:45 -0700 | [diff] [blame] | 18 | import android.content.ContentProvider; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 19 | import android.content.ContentResolver; |
| 20 | import android.database.Cursor; |
| 21 | import android.database.MatrixCursor; |
| 22 | import android.net.Uri; |
| 23 | import android.os.CancellationSignal; |
| 24 | import android.provider.ContactsContract; |
| 25 | import android.provider.ContactsContract.RawContacts; |
| 26 | import android.support.annotation.Nullable; |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 27 | import android.support.v4.util.ArraySet; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 28 | import android.test.AndroidTestCase; |
| 29 | import android.test.mock.MockContentResolver; |
| 30 | import android.test.suitebuilder.annotation.SmallTest; |
| 31 | |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 32 | import com.android.contacts.model.account.AccountWithDataSet; |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 33 | import com.android.contacts.test.mocks.MockContentProvider; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 34 | import com.android.contacts.tests.FakeDeviceAccountTypeFactory; |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 35 | import com.android.contacts.util.DeviceLocalAccountTypeFactory; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 36 | |
| 37 | import java.util.Arrays; |
| 38 | import java.util.Collections; |
Marcus Hagerott | bc51084 | 2016-09-09 19:29:45 -0700 | [diff] [blame] | 39 | import java.util.HashMap; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 40 | import java.util.List; |
Marcus Hagerott | bc51084 | 2016-09-09 19:29:45 -0700 | [diff] [blame] | 41 | import java.util.Map; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 42 | |
| 43 | @SmallTest |
Marcus Hagerott | 7a756ab | 2016-11-01 18:16:02 -0700 | [diff] [blame] | 44 | public class Cp2DeviceLocalAccountLocatorTests extends AndroidTestCase { |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 45 | |
| 46 | // Basic smoke test that just checks that it doesn't throw when loading from CP2. We don't |
| 47 | // care what CP2 actually contains for this. |
| 48 | public void testShouldNotCrash() { |
Marcus Hagerott | 7a756ab | 2016-11-01 18:16:02 -0700 | [diff] [blame] | 49 | final DeviceLocalAccountLocator sut = new Cp2DeviceLocalAccountLocator( |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 50 | getContext().getContentResolver(), |
| 51 | new DeviceLocalAccountTypeFactory.Default(getContext()), |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 52 | Collections.<String>emptySet()); |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 53 | sut.getDeviceLocalAccounts(); |
| 54 | // We didn't throw so it passed |
| 55 | } |
| 56 | |
Marcus Hagerott | bc51084 | 2016-09-09 19:29:45 -0700 | [diff] [blame] | 57 | public void test_getDeviceLocalAccounts_returnsEmptyListWhenQueryReturnsNull() { |
| 58 | final DeviceLocalAccountLocator sut = createWithQueryResult(null); |
| 59 | assertTrue(sut.getDeviceLocalAccounts().isEmpty()); |
| 60 | } |
| 61 | |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 62 | public void test_getDeviceLocalAccounts_returnsEmptyListWhenNoRawContactsHaveDeviceType() { |
| 63 | final DeviceLocalAccountLocator sut = createWithQueryResult(queryResult( |
| 64 | "user", "com.example", |
| 65 | "user", "com.example", |
| 66 | "user", "com.example")); |
| 67 | assertTrue(sut.getDeviceLocalAccounts().isEmpty()); |
| 68 | } |
| 69 | |
| 70 | public void test_getDeviceLocalAccounts_returnsListWithItemForNullAccount() { |
| 71 | final DeviceLocalAccountLocator sut = createWithQueryResult(queryResult( |
| 72 | "user", "com.example", |
| 73 | null, null, |
| 74 | "user", "com.example", |
| 75 | null, null)); |
| 76 | |
| 77 | assertEquals(1, sut.getDeviceLocalAccounts().size()); |
| 78 | } |
| 79 | |
| 80 | public void test_getDeviceLocalAccounts_containsItemForEachDeviceAccount() { |
| 81 | final DeviceLocalAccountTypeFactory stubFactory = new FakeDeviceAccountTypeFactory() |
| 82 | .withDeviceTypes(null, "vnd.sec.contact.phone") |
| 83 | .withSimTypes("vnd.sec.contact.sim"); |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 84 | final DeviceLocalAccountLocator sut = createLocator(queryResult( |
| 85 | "user", "com.example", |
| 86 | "user", "com.example", |
| 87 | "phone_account", "vnd.sec.contact.phone", |
| 88 | null, null, |
| 89 | "phone_account", "vnd.sec.contact.phone", |
| 90 | "user", "com.example", |
| 91 | null, null, |
| 92 | "sim_account", "vnd.sec.contact.sim", |
| 93 | "sim_account_2", "vnd.sec.contact.sim" |
| 94 | ), stubFactory); |
| 95 | |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 96 | |
| 97 | assertEquals(4, sut.getDeviceLocalAccounts().size()); |
| 98 | } |
| 99 | |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 100 | public void test_getDeviceLocalAccounts_doesNotContainItemsForKnownAccountTypes() { |
Marcus Hagerott | 7a756ab | 2016-11-01 18:16:02 -0700 | [diff] [blame] | 101 | final Cp2DeviceLocalAccountLocator sut = new Cp2DeviceLocalAccountLocator( |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 102 | getContext().getContentResolver(), new FakeDeviceAccountTypeFactory(), |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 103 | new ArraySet<>(Arrays.asList("com.example", "com.example.1"))); |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 104 | |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 105 | assertTrue("Selection should filter known accounts", |
| 106 | sut.getSelection().contains("NOT IN (?,?)")); |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 107 | |
| 108 | final List<String> args = Arrays.asList(sut.getSelectionArgs()); |
| 109 | assertEquals(2, args.size()); |
| 110 | assertTrue("Selection args is missing an expected value", args.contains("com.example")); |
| 111 | assertTrue("Selection args is missing an expected value", args.contains("com.example.1")); |
| 112 | } |
| 113 | |
Marcus Hagerott | bc51084 | 2016-09-09 19:29:45 -0700 | [diff] [blame] | 114 | public void test_getDeviceLocalAccounts_includesAccountsFromSettings() { |
| 115 | final DeviceLocalAccountTypeFactory stubFactory = new FakeDeviceAccountTypeFactory() |
| 116 | .withDeviceTypes(null, "vnd.sec.contact.phone") |
| 117 | .withSimTypes("vnd.sec.contact.sim"); |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 118 | final DeviceLocalAccountLocator sut = createLocator(new FakeContactsProvider() |
| 119 | .withQueryResult(ContactsContract.Settings.CONTENT_URI, queryResult( |
| 120 | "phone_account", "vnd.sec.contact.phone", |
| 121 | "sim_account", "vnd.sec.contact.sim" |
| 122 | )), stubFactory); |
Marcus Hagerott | bc51084 | 2016-09-09 19:29:45 -0700 | [diff] [blame] | 123 | |
| 124 | assertEquals(2, sut.getDeviceLocalAccounts().size()); |
| 125 | } |
| 126 | |
| 127 | public void test_getDeviceLocalAccounts_includesAccountsFromGroups() { |
| 128 | final DeviceLocalAccountTypeFactory stubFactory = new FakeDeviceAccountTypeFactory() |
| 129 | .withDeviceTypes(null, "vnd.sec.contact.phone") |
| 130 | .withSimTypes("vnd.sec.contact.sim"); |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 131 | final DeviceLocalAccountLocator sut = createLocator(new FakeContactsProvider() |
| 132 | .withQueryResult(ContactsContract.Groups.CONTENT_URI, queryResult( |
| 133 | "phone_account", "vnd.sec.contact.phone", |
| 134 | "sim_account", "vnd.sec.contact.sim" |
| 135 | )), stubFactory); |
Marcus Hagerott | bc51084 | 2016-09-09 19:29:45 -0700 | [diff] [blame] | 136 | |
| 137 | assertEquals(2, sut.getDeviceLocalAccounts().size()); |
| 138 | } |
| 139 | |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 140 | private DeviceLocalAccountLocator createWithQueryResult( |
| 141 | Cursor cursor) { |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 142 | return createLocator(cursor, new DeviceLocalAccountTypeFactory.Default(mContext)); |
| 143 | } |
| 144 | |
| 145 | private DeviceLocalAccountLocator createLocator(ContentProvider contactsProvider, |
| 146 | DeviceLocalAccountTypeFactory localAccountTypeFactory) { |
| 147 | final DeviceLocalAccountLocator locator = new Cp2DeviceLocalAccountLocator( |
| 148 | createContentResolverWithProvider(contactsProvider), |
| 149 | localAccountTypeFactory, Collections.<String>emptySet()); |
| 150 | return locator; |
| 151 | } |
| 152 | |
| 153 | private DeviceLocalAccountLocator createLocator(Cursor cursor, |
| 154 | DeviceLocalAccountTypeFactory localAccountTypeFactory) { |
Marcus Hagerott | 7a756ab | 2016-11-01 18:16:02 -0700 | [diff] [blame] | 155 | final DeviceLocalAccountLocator locator = new Cp2DeviceLocalAccountLocator( |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 156 | createStubResolverWithContentQueryResult(cursor), |
Marcus Hagerott | c2093f3 | 2016-12-12 10:18:12 -0800 | [diff] [blame] | 157 | localAccountTypeFactory, |
| 158 | Collections.<String>emptySet()); |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 159 | return locator; |
| 160 | } |
| 161 | |
Marcus Hagerott | bc51084 | 2016-09-09 19:29:45 -0700 | [diff] [blame] | 162 | private ContentResolver createContentResolverWithProvider(ContentProvider contactsProvider) { |
| 163 | final MockContentResolver resolver = new MockContentResolver(); |
| 164 | resolver.addProvider(ContactsContract.AUTHORITY, contactsProvider); |
| 165 | return resolver; |
| 166 | } |
| 167 | |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 168 | private ContentResolver createStubResolverWithContentQueryResult(Cursor cursor) { |
| 169 | final MockContentResolver resolver = new MockContentResolver(); |
Marcus Hagerott | bc51084 | 2016-09-09 19:29:45 -0700 | [diff] [blame] | 170 | resolver.addProvider(ContactsContract.AUTHORITY, new FakeContactsProvider() |
| 171 | .withDefaultQueryResult(cursor)); |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 172 | return resolver; |
| 173 | } |
| 174 | |
| 175 | private Cursor queryResult(String... nameTypePairs) { |
| 176 | final MatrixCursor cursor = new MatrixCursor(new String[] |
| 177 | { RawContacts.ACCOUNT_NAME, RawContacts.ACCOUNT_TYPE, RawContacts.DATA_SET }); |
| 178 | for (int i = 0; i < nameTypePairs.length; i+=2) { |
| 179 | cursor.newRow().add(nameTypePairs[i]).add(nameTypePairs[i+1]) |
| 180 | .add(null); |
| 181 | } |
| 182 | return cursor; |
| 183 | } |
| 184 | |
| 185 | private static class FakeContactsProvider extends MockContentProvider { |
| 186 | public Cursor mNextQueryResult; |
Marcus Hagerott | bc51084 | 2016-09-09 19:29:45 -0700 | [diff] [blame] | 187 | public Map<Uri, Cursor> mNextResultMapping = new HashMap<>(); |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 188 | |
| 189 | public FakeContactsProvider() {} |
| 190 | |
Marcus Hagerott | bc51084 | 2016-09-09 19:29:45 -0700 | [diff] [blame] | 191 | public FakeContactsProvider withDefaultQueryResult(Cursor cursor) { |
| 192 | mNextQueryResult = cursor; |
| 193 | return this; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Marcus Hagerott | bc51084 | 2016-09-09 19:29:45 -0700 | [diff] [blame] | 196 | public FakeContactsProvider withQueryResult(Uri uri, Cursor cursor) { |
| 197 | mNextResultMapping.put(uri, cursor); |
| 198 | return this; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, |
| 203 | String sortOrder) { |
| 204 | return query(uri, projection, selection, selectionArgs, sortOrder, null); |
| 205 | } |
| 206 | |
| 207 | @Nullable |
| 208 | @Override |
| 209 | public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, |
| 210 | String sortOrder, CancellationSignal cancellationSignal) { |
Marcus Hagerott | bc51084 | 2016-09-09 19:29:45 -0700 | [diff] [blame] | 211 | final Cursor result = mNextResultMapping.get(uri); |
| 212 | if (result == null) { |
| 213 | return mNextQueryResult; |
| 214 | } else { |
| 215 | return result; |
| 216 | } |
| 217 | } |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 218 | } |
| 219 | } |