12b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng/* 22b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng * Copyright (C) 2011 The Android Open Source Project 32b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng * 42b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng * Licensed under the Apache License, Version 2.0 (the "License"); 52b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng * you may not use this file except in compliance with the License. 62b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng * You may obtain a copy of the License at 72b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng * 82b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng * http://www.apache.org/licenses/LICENSE-2.0 92b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng * 102b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng * Unless required by applicable law or agreed to in writing, software 112b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng * distributed under the License is distributed on an "AS IS" BASIS, 122b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 132b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng * See the License for the specific language governing permissions and 142b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng * limitations under the License. 152b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng */ 162b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng 172b729670f9b5b392929102e109dcd7a2ae732508Chiao Chengpackage com.android.contacts.common.model; 182b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng 192b729670f9b5b392929102e109dcd7a2ae732508Chiao Chengimport android.content.Context; 202b729670f9b5b392929102e109dcd7a2ae732508Chiao Chengimport android.test.AndroidTestCase; 212b729670f9b5b392929102e109dcd7a2ae732508Chiao Chengimport android.test.suitebuilder.annotation.SmallTest; 222b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng 232b729670f9b5b392929102e109dcd7a2ae732508Chiao Chengimport com.android.contacts.common.model.account.AccountType; 242b729670f9b5b392929102e109dcd7a2ae732508Chiao Chengimport com.android.contacts.common.model.account.AccountTypeWithDataSet; 252b729670f9b5b392929102e109dcd7a2ae732508Chiao Chengimport com.android.contacts.common.model.account.AccountWithDataSet; 262b729670f9b5b392929102e109dcd7a2ae732508Chiao Chengimport com.google.common.collect.Lists; 272b729670f9b5b392929102e109dcd7a2ae732508Chiao Chengimport com.google.common.collect.Maps; 282b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng 292b729670f9b5b392929102e109dcd7a2ae732508Chiao Chengimport java.util.Collection; 302b729670f9b5b392929102e109dcd7a2ae732508Chiao Chengimport java.util.HashMap; 312b729670f9b5b392929102e109dcd7a2ae732508Chiao Chengimport java.util.List; 322b729670f9b5b392929102e109dcd7a2ae732508Chiao Chengimport java.util.Map; 332b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng 342b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng/** 352b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng * Test case for {@link com.android.contacts.common.model.AccountTypeManager}. 362b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng * 372b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng * adb shell am instrument -w -e class com.android.contacts.model.AccountTypeManagerTest \ 382b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng com.android.contacts.tests/android.test.InstrumentationTestRunner 392b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng */ 402b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng@SmallTest 412b729670f9b5b392929102e109dcd7a2ae732508Chiao Chengpublic class AccountTypeManagerTest extends AndroidTestCase { 422b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng public void testFindAllInvitableAccountTypes() { 432b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng final Context c = getContext(); 442b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng 452b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng // Define account types. 462b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng final AccountType typeA = new MockAccountType("type1", null, null); 472b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng final AccountType typeB = new MockAccountType("type1", "minus", null); 482b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng final AccountType typeC = new MockAccountType("type2", null, "c"); 492b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng final AccountType typeD = new MockAccountType("type2", "minus", "d"); 502b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng 512b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng // Define users 522b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng final AccountWithDataSet accountA1 = createAccountWithDataSet("a1", typeA); 532b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng final AccountWithDataSet accountC1 = createAccountWithDataSet("c1", typeC); 542b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng final AccountWithDataSet accountC2 = createAccountWithDataSet("c2", typeC); 552b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng final AccountWithDataSet accountD1 = createAccountWithDataSet("d1", typeD); 562b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng 572b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng // empty - empty 582b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng Map<AccountTypeWithDataSet, AccountType> types = 592b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng AccountTypeManagerImpl.findAllInvitableAccountTypes(c, 602b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng buildAccounts(), buildAccountTypes()); 612b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng assertEquals(0, types.size()); 622b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng try { 632b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng types.clear(); 642b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng fail("Returned Map should be unmodifiable."); 652b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng } catch (UnsupportedOperationException ok) { 662b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng } 672b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng 682b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng // No invite support, no accounts 692b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng verifyAccountTypes( 702b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng buildAccounts(), 712b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng buildAccountTypes(typeA, typeB) 722b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng /* empty */ 732b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng ); 742b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng 752b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng // No invite support, with accounts 762b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng verifyAccountTypes( 772b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng buildAccounts(accountA1), 782b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng buildAccountTypes(typeA) 792b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng /* empty */ 802b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng ); 812b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng 822b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng // With invite support, no accounts 832b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng verifyAccountTypes( 842b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng buildAccounts(), 852b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng buildAccountTypes(typeC) 862b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng /* empty */ 872b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng ); 882b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng 892b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng // With invite support, 1 account 902b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng verifyAccountTypes( 912b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng buildAccounts(accountC1), 922b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng buildAccountTypes(typeC), 932b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng typeC 942b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng ); 952b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng 962b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng // With invite support, 2 account 972b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng verifyAccountTypes( 982b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng buildAccounts(accountC1, accountC2), 992b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng buildAccountTypes(typeC), 1002b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng typeC 1012b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng ); 1022b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng 1032b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng // Combinations... 1042b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng verifyAccountTypes( 1052b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng buildAccounts(accountA1), 1062b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng buildAccountTypes(typeA, typeC) 1072b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng /* empty */ 1082b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng ); 1092b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng 1102b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng verifyAccountTypes( 1112b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng buildAccounts(accountC1, accountA1), 1122b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng buildAccountTypes(typeA, typeC), 1132b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng typeC 1142b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng ); 1152b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng 1162b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng verifyAccountTypes( 1172b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng buildAccounts(accountC1, accountA1), 1182b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng buildAccountTypes(typeD, typeA, typeC), 1192b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng typeC 1202b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng ); 1212b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng 1222b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng verifyAccountTypes( 1232b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng buildAccounts(accountC1, accountA1, accountD1), 1242b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng buildAccountTypes(typeD, typeA, typeC, typeB), 1252b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng typeC, typeD 1262b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng ); 1272b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng } 1282b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng 1292b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng private static AccountWithDataSet createAccountWithDataSet(String name, AccountType type) { 1302b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng return new AccountWithDataSet(name, type.accountType, type.dataSet); 1312b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng } 1322b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng 1332b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng /** 1342b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng * Array of {@link AccountType} -> {@link Map} 1352b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng */ 1362b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng private static Map<AccountTypeWithDataSet, AccountType> buildAccountTypes(AccountType... types) { 1372b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng final HashMap<AccountTypeWithDataSet, AccountType> result = Maps.newHashMap(); 1382b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng for (AccountType type : types) { 1392b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng result.put(type.getAccountTypeAndDataSet(), type); 1402b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng } 1412b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng return result; 1422b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng } 1432b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng 1442b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng /** 1452b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng * Array of {@link AccountWithDataSet} -> {@link Collection} 1462b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng */ 1472b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng private static Collection<AccountWithDataSet> buildAccounts(AccountWithDataSet... accounts) { 1482b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng final List<AccountWithDataSet> result = Lists.newArrayList(); 1492b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng for (AccountWithDataSet account : accounts) { 1502b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng result.add(account); 1512b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng } 1522b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng return result; 1532b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng } 1542b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng 1552b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng /** 1562b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng * Executes {@link AccountTypeManagerImpl#findInvitableAccountTypes} and verifies the 1572b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng * result. 1582b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng */ 1592b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng private void verifyAccountTypes( 1602b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng Collection<AccountWithDataSet> accounts, 1612b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng Map<AccountTypeWithDataSet, AccountType> types, 1622b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng AccountType... expectedInvitableTypes 1632b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng ) { 1642b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng Map<AccountTypeWithDataSet, AccountType> result = 1652b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng AccountTypeManagerImpl.findAllInvitableAccountTypes(getContext(), accounts, types); 1662b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng for (AccountType type : expectedInvitableTypes) { 1672b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng assertTrue("Result doesn't contain type=" + type.getAccountTypeAndDataSet(), 1682b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng result.containsKey(type.getAccountTypeAndDataSet())); 1692b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng } 1702b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng final int numExcessTypes = result.size() - expectedInvitableTypes.length; 1712b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng assertEquals("Result contains " + numExcessTypes + " excess type(s)", 0, numExcessTypes); 1722b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng } 1732b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng 1742b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng private static class MockAccountType extends AccountType { 1752b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng private final String mInviteContactActivityClassName; 1762b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng 1772b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng public MockAccountType(String type, String dataSet, String inviteContactActivityClassName) { 1782b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng accountType = type; 1792b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng this.dataSet = dataSet; 1802b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng mInviteContactActivityClassName = inviteContactActivityClassName; 1812b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng } 1822b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng 1832b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng @Override 1842b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng public String getInviteContactActivityClassName() { 1852b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng return mInviteContactActivityClassName; 1862b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng } 1872b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng 1882b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng @Override 1892b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng public boolean isGroupMembershipEditable() { 1902b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng return false; 1912b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng } 1922b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng 1932b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng @Override 1942b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng public boolean areContactsWritable() { 1952b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng return false; 1962b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng } 1972b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng } 1982b729670f9b5b392929102e109dcd7a2ae732508Chiao Cheng} 199