blob: 82200ed59a9f43aa1de2b5831ac72dec64623b32 [file] [log] [blame]
Chiao Cheng6c712f42012-11-26 15:35:28 -08001/*
2 * Copyright (C) 2011 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
17package com.android.contacts.common.model;
18
Wenyi Wang60042b02016-09-30 17:23:25 -070019import android.accounts.Account;
20import android.accounts.AccountManager;
Chiao Cheng6c712f42012-11-26 15:35:28 -080021import android.content.Context;
Wenyi Wang60042b02016-09-30 17:23:25 -070022import android.content.SharedPreferences;
Chiao Cheng6c712f42012-11-26 15:35:28 -080023import android.test.AndroidTestCase;
24import android.test.suitebuilder.annotation.SmallTest;
25
26import com.android.contacts.common.model.account.AccountType;
27import com.android.contacts.common.model.account.AccountTypeWithDataSet;
28import com.android.contacts.common.model.account.AccountWithDataSet;
Wenyi Wang60042b02016-09-30 17:23:25 -070029import com.android.contacts.common.model.account.GoogleAccountType;
Chiao Cheng6c712f42012-11-26 15:35:28 -080030import com.google.common.collect.Lists;
31import com.google.common.collect.Maps;
32
Wenyi Wang60042b02016-09-30 17:23:25 -070033import org.mockito.Mock;
34import org.mockito.Mockito;
35import org.mockito.MockitoAnnotations;
36
37import static org.mockito.Mockito.when;
38
Chiao Cheng6c712f42012-11-26 15:35:28 -080039import java.util.Collection;
40import java.util.HashMap;
41import java.util.List;
42import java.util.Map;
43
44/**
45 * Test case for {@link com.android.contacts.common.model.AccountTypeManager}.
46 *
47 * adb shell am instrument -w -e class com.android.contacts.model.AccountTypeManagerTest \
48 com.android.contacts.tests/android.test.InstrumentationTestRunner
49 */
50@SmallTest
51public class AccountTypeManagerTest extends AndroidTestCase {
Wenyi Wang60042b02016-09-30 17:23:25 -070052
53 private static final Account[] ACCOUNTS = new Account[2];
54 static {
55 ACCOUNTS[0] = new Account("name1", GoogleAccountType.ACCOUNT_TYPE);
56 ACCOUNTS[1] = new Account("name2", GoogleAccountType.ACCOUNT_TYPE);
57 }
58
59 @Mock private AccountManager mAccountManager;
60 @Mock private SharedPreferences mPrefs;
61
62 @Override
63 public void setUp() throws Exception {
64 super.setUp();
65 System.setProperty("dexmaker.dexcache", getContext().getCacheDir().getPath());
66 MockitoAnnotations.initMocks(this);
67 }
68
Chiao Cheng6c712f42012-11-26 15:35:28 -080069 public void testFindAllInvitableAccountTypes() {
70 final Context c = getContext();
71
72 // Define account types.
73 final AccountType typeA = new MockAccountType("type1", null, null);
74 final AccountType typeB = new MockAccountType("type1", "minus", null);
75 final AccountType typeC = new MockAccountType("type2", null, "c");
76 final AccountType typeD = new MockAccountType("type2", "minus", "d");
77
78 // Define users
79 final AccountWithDataSet accountA1 = createAccountWithDataSet("a1", typeA);
80 final AccountWithDataSet accountC1 = createAccountWithDataSet("c1", typeC);
81 final AccountWithDataSet accountC2 = createAccountWithDataSet("c2", typeC);
82 final AccountWithDataSet accountD1 = createAccountWithDataSet("d1", typeD);
83
84 // empty - empty
85 Map<AccountTypeWithDataSet, AccountType> types =
86 AccountTypeManagerImpl.findAllInvitableAccountTypes(c,
87 buildAccounts(), buildAccountTypes());
88 assertEquals(0, types.size());
89 try {
90 types.clear();
91 fail("Returned Map should be unmodifiable.");
92 } catch (UnsupportedOperationException ok) {
93 }
94
95 // No invite support, no accounts
96 verifyAccountTypes(
97 buildAccounts(),
98 buildAccountTypes(typeA, typeB)
99 /* empty */
100 );
101
102 // No invite support, with accounts
103 verifyAccountTypes(
104 buildAccounts(accountA1),
105 buildAccountTypes(typeA)
106 /* empty */
107 );
108
109 // With invite support, no accounts
110 verifyAccountTypes(
111 buildAccounts(),
112 buildAccountTypes(typeC)
113 /* empty */
114 );
115
116 // With invite support, 1 account
117 verifyAccountTypes(
118 buildAccounts(accountC1),
119 buildAccountTypes(typeC),
120 typeC
121 );
122
123 // With invite support, 2 account
124 verifyAccountTypes(
125 buildAccounts(accountC1, accountC2),
126 buildAccountTypes(typeC),
127 typeC
128 );
129
130 // Combinations...
131 verifyAccountTypes(
132 buildAccounts(accountA1),
133 buildAccountTypes(typeA, typeC)
134 /* empty */
135 );
136
137 verifyAccountTypes(
138 buildAccounts(accountC1, accountA1),
139 buildAccountTypes(typeA, typeC),
140 typeC
141 );
142
143 verifyAccountTypes(
144 buildAccounts(accountC1, accountA1),
145 buildAccountTypes(typeD, typeA, typeC),
146 typeC
147 );
148
149 verifyAccountTypes(
150 buildAccounts(accountC1, accountA1, accountD1),
151 buildAccountTypes(typeD, typeA, typeC, typeB),
152 typeC, typeD
153 );
154 }
155
156 private static AccountWithDataSet createAccountWithDataSet(String name, AccountType type) {
157 return new AccountWithDataSet(name, type.accountType, type.dataSet);
158 }
159
160 /**
161 * Array of {@link AccountType} -> {@link Map}
162 */
163 private static Map<AccountTypeWithDataSet, AccountType> buildAccountTypes(AccountType... types) {
164 final HashMap<AccountTypeWithDataSet, AccountType> result = Maps.newHashMap();
165 for (AccountType type : types) {
166 result.put(type.getAccountTypeAndDataSet(), type);
167 }
168 return result;
169 }
170
171 /**
172 * Array of {@link AccountWithDataSet} -> {@link Collection}
173 */
174 private static Collection<AccountWithDataSet> buildAccounts(AccountWithDataSet... accounts) {
175 final List<AccountWithDataSet> result = Lists.newArrayList();
176 for (AccountWithDataSet account : accounts) {
177 result.add(account);
178 }
179 return result;
180 }
181
182 /**
183 * Executes {@link AccountTypeManagerImpl#findInvitableAccountTypes} and verifies the
184 * result.
185 */
186 private void verifyAccountTypes(
187 Collection<AccountWithDataSet> accounts,
188 Map<AccountTypeWithDataSet, AccountType> types,
189 AccountType... expectedInvitableTypes
190 ) {
191 Map<AccountTypeWithDataSet, AccountType> result =
192 AccountTypeManagerImpl.findAllInvitableAccountTypes(getContext(), accounts, types);
193 for (AccountType type : expectedInvitableTypes) {
194 assertTrue("Result doesn't contain type=" + type.getAccountTypeAndDataSet(),
195 result.containsKey(type.getAccountTypeAndDataSet()));
196 }
197 final int numExcessTypes = result.size() - expectedInvitableTypes.length;
198 assertEquals("Result contains " + numExcessTypes + " excess type(s)", 0, numExcessTypes);
199 }
200
201 private static class MockAccountType extends AccountType {
202 private final String mInviteContactActivityClassName;
203
204 public MockAccountType(String type, String dataSet, String inviteContactActivityClassName) {
205 accountType = type;
206 this.dataSet = dataSet;
207 mInviteContactActivityClassName = inviteContactActivityClassName;
208 }
209
210 @Override
211 public String getInviteContactActivityClassName() {
212 return mInviteContactActivityClassName;
213 }
214
215 @Override
216 public boolean isGroupMembershipEditable() {
217 return false;
218 }
219
220 @Override
221 public boolean areContactsWritable() {
222 return false;
223 }
224 }
Wenyi Wang60042b02016-09-30 17:23:25 -0700225
226
227 public void testGetDefaultAccount_NoAccounts() {
228 assertNull(getDefaultGoogleAccountName());
229 }
230
231 public void testGetDefaultAccount_NoAccounts_DefaultPreferenceSet() {
232 when(mPrefs.getString(Mockito.anyString(), Mockito.anyString())).thenReturn(
233 getDefaultAccountPreference("name1", GoogleAccountType.ACCOUNT_TYPE));
234 assertNull(getDefaultGoogleAccountName());
235 }
236
237 public void testGetDefaultAccount_NoDefaultAccountPreferenceSet() {
238 when(mAccountManager.getAccountsByType(Mockito.anyString())).thenReturn(ACCOUNTS);
239 assertEquals("name1", getDefaultGoogleAccountName());
240 }
241
242 public void testGetDefaultAccount_DefaultAccountPreferenceSet() {
243 when(mAccountManager.getAccountsByType(Mockito.anyString())).thenReturn(ACCOUNTS);
244 when(mPrefs.getString(Mockito.anyString(), Mockito.anyString())).thenReturn(
245 getDefaultAccountPreference("name2", GoogleAccountType.ACCOUNT_TYPE));
246 assertEquals("name2", getDefaultGoogleAccountName());
247 }
248
249 public void testGetDefaultAccount_DefaultAccountPreferenceSet_NonGoogleAccountType() {
250 when(mAccountManager.getAccountsByType(Mockito.anyString())).thenReturn(ACCOUNTS);
251 when(mPrefs.getString(Mockito.anyString(), Mockito.anyString())).thenReturn(
252 getDefaultAccountPreference("name3", "type3"));
253 assertEquals("name1", getDefaultGoogleAccountName());
254 }
255
256 public void testGetDefaultAccount_DefaultAccountPreferenceSet_UnknownName() {
257 when(mAccountManager.getAccountsByType(Mockito.anyString())).thenReturn(ACCOUNTS);
258 when(mPrefs.getString(Mockito.anyString(), Mockito.anyString())).thenReturn(
259 getDefaultAccountPreference("name4",GoogleAccountType.ACCOUNT_TYPE));
260 assertEquals("name1", getDefaultGoogleAccountName());
261 }
262
263 private final String getDefaultGoogleAccountName() {
264 // We don't need the real preference key value since it's mocked
265 final Account account = AccountTypeManager.getDefaultGoogleAccount(
266 mAccountManager, mPrefs, "contact_editor_default_account_key");
267 return account == null ? null : account.name;
268 }
269
270 private static final String getDefaultAccountPreference(String name, String type) {
271 return new AccountWithDataSet(name, type, /* dataSet */ null).stringify();
272 }
Chiao Cheng6c712f42012-11-26 15:35:28 -0800273}