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 | */ |
| 16 | package com.android.contacts.common.model; |
| 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; |
| 27 | import android.test.AndroidTestCase; |
| 28 | import android.test.mock.MockContentResolver; |
| 29 | import android.test.suitebuilder.annotation.SmallTest; |
| 30 | |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 31 | import com.android.contacts.common.model.account.AccountWithDataSet; |
| 32 | import com.android.contacts.common.test.mocks.MockContentProvider; |
| 33 | import com.android.contacts.common.util.DeviceLocalAccountTypeFactory; |
| 34 | import com.android.contacts.tests.FakeDeviceAccountTypeFactory; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 35 | |
| 36 | import java.util.Arrays; |
| 37 | import java.util.Collections; |
Marcus Hagerott | bc51084 | 2016-09-09 19:29:45 -0700 | [diff] [blame] | 38 | import java.util.HashMap; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 39 | import java.util.List; |
Marcus Hagerott | bc51084 | 2016-09-09 19:29:45 -0700 | [diff] [blame] | 40 | import java.util.Map; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 41 | |
| 42 | @SmallTest |
Marcus Hagerott | 7a756ab | 2016-11-01 18:16:02 -0700 | [diff] [blame] | 43 | public class Cp2DeviceLocalAccountLocatorTests extends AndroidTestCase { |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 44 | |
| 45 | // Basic smoke test that just checks that it doesn't throw when loading from CP2. We don't |
| 46 | // care what CP2 actually contains for this. |
| 47 | public void testShouldNotCrash() { |
Marcus Hagerott | 7a756ab | 2016-11-01 18:16:02 -0700 | [diff] [blame] | 48 | final DeviceLocalAccountLocator sut = new Cp2DeviceLocalAccountLocator( |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 49 | getContext().getContentResolver(), |
| 50 | new DeviceLocalAccountTypeFactory.Default(getContext()), |
| 51 | Collections.<AccountWithDataSet>emptyList()); |
| 52 | sut.getDeviceLocalAccounts(); |
| 53 | // We didn't throw so it passed |
| 54 | } |
| 55 | |
Marcus Hagerott | bc51084 | 2016-09-09 19:29:45 -0700 | [diff] [blame] | 56 | public void test_getDeviceLocalAccounts_returnsEmptyListWhenQueryReturnsNull() { |
| 57 | final DeviceLocalAccountLocator sut = createWithQueryResult(null); |
| 58 | assertTrue(sut.getDeviceLocalAccounts().isEmpty()); |
| 59 | } |
| 60 | |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 61 | public void test_getDeviceLocalAccounts_returnsEmptyListWhenNoRawContactsHaveDeviceType() { |
| 62 | final DeviceLocalAccountLocator sut = createWithQueryResult(queryResult( |
| 63 | "user", "com.example", |
| 64 | "user", "com.example", |
| 65 | "user", "com.example")); |
| 66 | assertTrue(sut.getDeviceLocalAccounts().isEmpty()); |
| 67 | } |
| 68 | |
| 69 | public void test_getDeviceLocalAccounts_returnsListWithItemForNullAccount() { |
| 70 | final DeviceLocalAccountLocator sut = createWithQueryResult(queryResult( |
| 71 | "user", "com.example", |
| 72 | null, null, |
| 73 | "user", "com.example", |
| 74 | null, null)); |
| 75 | |
| 76 | assertEquals(1, sut.getDeviceLocalAccounts().size()); |
| 77 | } |
| 78 | |
| 79 | public void test_getDeviceLocalAccounts_containsItemForEachDeviceAccount() { |
| 80 | final DeviceLocalAccountTypeFactory stubFactory = new FakeDeviceAccountTypeFactory() |
| 81 | .withDeviceTypes(null, "vnd.sec.contact.phone") |
| 82 | .withSimTypes("vnd.sec.contact.sim"); |
Marcus Hagerott | 7a756ab | 2016-11-01 18:16:02 -0700 | [diff] [blame] | 83 | final DeviceLocalAccountLocator sut = new Cp2DeviceLocalAccountLocator( |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 84 | createStubResolverWithContentQueryResult(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 | Collections.<AccountWithDataSet>emptyList()); |
| 96 | |
| 97 | assertEquals(4, sut.getDeviceLocalAccounts().size()); |
| 98 | } |
| 99 | |
| 100 | public void test_getDeviceLocalAccounts_doesNotContainItemsForKnownAccounts() { |
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(), |
| 103 | Arrays.asList(new AccountWithDataSet("user", "com.example", null), |
| 104 | new AccountWithDataSet("user1", "com.example", null), |
| 105 | new AccountWithDataSet("user", "com.example.1", null))); |
| 106 | |
| 107 | assertTrue("Selection should filter known accounts", sut.getSelection().contains("NOT IN (?,?)")); |
| 108 | |
| 109 | final List<String> args = Arrays.asList(sut.getSelectionArgs()); |
| 110 | assertEquals(2, args.size()); |
| 111 | assertTrue("Selection args is missing an expected value", args.contains("com.example")); |
| 112 | assertTrue("Selection args is missing an expected value", args.contains("com.example.1")); |
| 113 | } |
| 114 | |
Marcus Hagerott | bc51084 | 2016-09-09 19:29:45 -0700 | [diff] [blame] | 115 | public void test_getDeviceLocalAccounts_includesAccountsFromSettings() { |
| 116 | final DeviceLocalAccountTypeFactory stubFactory = new FakeDeviceAccountTypeFactory() |
| 117 | .withDeviceTypes(null, "vnd.sec.contact.phone") |
| 118 | .withSimTypes("vnd.sec.contact.sim"); |
Marcus Hagerott | 7a756ab | 2016-11-01 18:16:02 -0700 | [diff] [blame] | 119 | final DeviceLocalAccountLocator sut = new Cp2DeviceLocalAccountLocator( |
Marcus Hagerott | bc51084 | 2016-09-09 19:29:45 -0700 | [diff] [blame] | 120 | createContentResolverWithProvider(new FakeContactsProvider() |
| 121 | .withQueryResult(ContactsContract.Settings.CONTENT_URI, queryResult( |
| 122 | "phone_account", "vnd.sec.contact.phone", |
| 123 | "sim_account", "vnd.sec.contact.sim" |
| 124 | ))), stubFactory, Collections.<AccountWithDataSet>emptyList()); |
| 125 | |
| 126 | assertEquals(2, sut.getDeviceLocalAccounts().size()); |
| 127 | } |
| 128 | |
| 129 | public void test_getDeviceLocalAccounts_includesAccountsFromGroups() { |
| 130 | final DeviceLocalAccountTypeFactory stubFactory = new FakeDeviceAccountTypeFactory() |
| 131 | .withDeviceTypes(null, "vnd.sec.contact.phone") |
| 132 | .withSimTypes("vnd.sec.contact.sim"); |
Marcus Hagerott | 7a756ab | 2016-11-01 18:16:02 -0700 | [diff] [blame] | 133 | final DeviceLocalAccountLocator sut = new Cp2DeviceLocalAccountLocator( |
Marcus Hagerott | bc51084 | 2016-09-09 19:29:45 -0700 | [diff] [blame] | 134 | createContentResolverWithProvider(new FakeContactsProvider() |
| 135 | .withQueryResult(ContactsContract.Groups.CONTENT_URI, queryResult( |
| 136 | "phone_account", "vnd.sec.contact.phone", |
| 137 | "sim_account", "vnd.sec.contact.sim" |
| 138 | ))), stubFactory, Collections.<AccountWithDataSet>emptyList()); |
| 139 | |
| 140 | assertEquals(2, sut.getDeviceLocalAccounts().size()); |
| 141 | } |
| 142 | |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 143 | private DeviceLocalAccountLocator createWithQueryResult( |
| 144 | Cursor cursor) { |
Marcus Hagerott | 7a756ab | 2016-11-01 18:16:02 -0700 | [diff] [blame] | 145 | final DeviceLocalAccountLocator locator = new Cp2DeviceLocalAccountLocator( |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 146 | createStubResolverWithContentQueryResult(cursor), |
| 147 | new DeviceLocalAccountTypeFactory.Default(getContext()), |
| 148 | Collections.<AccountWithDataSet>emptyList()); |
| 149 | return locator; |
| 150 | } |
| 151 | |
Marcus Hagerott | bc51084 | 2016-09-09 19:29:45 -0700 | [diff] [blame] | 152 | private ContentResolver createContentResolverWithProvider(ContentProvider contactsProvider) { |
| 153 | final MockContentResolver resolver = new MockContentResolver(); |
| 154 | resolver.addProvider(ContactsContract.AUTHORITY, contactsProvider); |
| 155 | return resolver; |
| 156 | } |
| 157 | |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 158 | |
| 159 | private ContentResolver createStubResolverWithContentQueryResult(Cursor cursor) { |
| 160 | final MockContentResolver resolver = new MockContentResolver(); |
Marcus Hagerott | bc51084 | 2016-09-09 19:29:45 -0700 | [diff] [blame] | 161 | resolver.addProvider(ContactsContract.AUTHORITY, new FakeContactsProvider() |
| 162 | .withDefaultQueryResult(cursor)); |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 163 | return resolver; |
| 164 | } |
| 165 | |
| 166 | private Cursor queryResult(String... nameTypePairs) { |
| 167 | final MatrixCursor cursor = new MatrixCursor(new String[] |
| 168 | { RawContacts.ACCOUNT_NAME, RawContacts.ACCOUNT_TYPE, RawContacts.DATA_SET }); |
| 169 | for (int i = 0; i < nameTypePairs.length; i+=2) { |
| 170 | cursor.newRow().add(nameTypePairs[i]).add(nameTypePairs[i+1]) |
| 171 | .add(null); |
| 172 | } |
| 173 | return cursor; |
| 174 | } |
| 175 | |
| 176 | private static class FakeContactsProvider extends MockContentProvider { |
| 177 | public Cursor mNextQueryResult; |
Marcus Hagerott | bc51084 | 2016-09-09 19:29:45 -0700 | [diff] [blame] | 178 | public Map<Uri, Cursor> mNextResultMapping = new HashMap<>(); |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 179 | |
| 180 | public FakeContactsProvider() {} |
| 181 | |
Marcus Hagerott | bc51084 | 2016-09-09 19:29:45 -0700 | [diff] [blame] | 182 | public FakeContactsProvider withDefaultQueryResult(Cursor cursor) { |
| 183 | mNextQueryResult = cursor; |
| 184 | return this; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Marcus Hagerott | bc51084 | 2016-09-09 19:29:45 -0700 | [diff] [blame] | 187 | public FakeContactsProvider withQueryResult(Uri uri, Cursor cursor) { |
| 188 | mNextResultMapping.put(uri, cursor); |
| 189 | return this; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, |
| 194 | String sortOrder) { |
| 195 | return query(uri, projection, selection, selectionArgs, sortOrder, null); |
| 196 | } |
| 197 | |
| 198 | @Nullable |
| 199 | @Override |
| 200 | public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, |
| 201 | String sortOrder, CancellationSignal cancellationSignal) { |
Marcus Hagerott | bc51084 | 2016-09-09 19:29:45 -0700 | [diff] [blame] | 202 | final Cursor result = mNextResultMapping.get(uri); |
| 203 | if (result == null) { |
| 204 | return mNextQueryResult; |
| 205 | } else { |
| 206 | return result; |
| 207 | } |
| 208 | } |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 209 | } |
| 210 | } |