AccountTypeManagerTest.java revision 3e435f0e724cb3e88efce15f760e59b9bc08f0d3
1/* 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.model; 18 19import com.google.common.collect.Lists; 20import com.google.common.collect.Maps; 21 22import android.content.Context; 23import android.test.AndroidTestCase; 24import android.test.suitebuilder.annotation.SmallTest; 25 26import java.util.Collection; 27import java.util.HashMap; 28import java.util.List; 29import java.util.Map; 30 31/** 32 * Test case for {@link AccountTypeManager}. 33 * 34 * adb shell am instrument -w -e class com.android.contacts.model.AccountTypeManagerTest \ 35 com.android.contacts.tests/android.test.InstrumentationTestRunner 36 */ 37@SmallTest 38public class AccountTypeManagerTest extends AndroidTestCase { 39 public void testFindAllInvitableAccountTypes() { 40 final Context c = getContext(); 41 42 // Define account types. 43 final AccountType typeA = new MockAccountType("type1", null, null); 44 final AccountType typeB = new MockAccountType("type1", "minus", null); 45 final AccountType typeC = new MockAccountType("type2", null, "c"); 46 final AccountType typeD = new MockAccountType("type2", "minus", "d"); 47 48 // Define users 49 final AccountWithDataSet accountA1 = createAccountWithDataSet("a1", typeA); 50 final AccountWithDataSet accountC1 = createAccountWithDataSet("c1", typeC); 51 final AccountWithDataSet accountC2 = createAccountWithDataSet("c2", typeC); 52 final AccountWithDataSet accountD1 = createAccountWithDataSet("d1", typeD); 53 54 // empty - empty 55 Map<AccountTypeWithDataSet, AccountType> types = 56 AccountTypeManagerImpl.findAllInvitableAccountTypes(c, 57 buildAccounts(), buildAccountTypes()); 58 assertEquals(0, types.size()); 59 try { 60 types.clear(); 61 fail("Returned Map should be unmodifiable."); 62 } catch (UnsupportedOperationException ok) { 63 } 64 65 // No invite support, no accounts 66 verifyAccountTypes( 67 buildAccounts(), 68 buildAccountTypes(typeA, typeB) 69 /* empty */ 70 ); 71 72 // No invite support, with accounts 73 verifyAccountTypes( 74 buildAccounts(accountA1), 75 buildAccountTypes(typeA) 76 /* empty */ 77 ); 78 79 // With invite support, no accounts 80 verifyAccountTypes( 81 buildAccounts(), 82 buildAccountTypes(typeC) 83 /* empty */ 84 ); 85 86 // With invite support, 1 account 87 verifyAccountTypes( 88 buildAccounts(accountC1), 89 buildAccountTypes(typeC), 90 typeC 91 ); 92 93 // With invite support, 2 account 94 verifyAccountTypes( 95 buildAccounts(accountC1, accountC2), 96 buildAccountTypes(typeC), 97 typeC 98 ); 99 100 // Combinations... 101 verifyAccountTypes( 102 buildAccounts(accountA1), 103 buildAccountTypes(typeA, typeC) 104 /* empty */ 105 ); 106 107 verifyAccountTypes( 108 buildAccounts(accountC1, accountA1), 109 buildAccountTypes(typeA, typeC), 110 typeC 111 ); 112 113 verifyAccountTypes( 114 buildAccounts(accountC1, accountA1), 115 buildAccountTypes(typeD, typeA, typeC), 116 typeC 117 ); 118 119 verifyAccountTypes( 120 buildAccounts(accountC1, accountA1, accountD1), 121 buildAccountTypes(typeD, typeA, typeC, typeB), 122 typeC, typeD 123 ); 124 } 125 126 private static AccountWithDataSet createAccountWithDataSet(String name, AccountType type) { 127 return new AccountWithDataSet(name, type.accountType, type.dataSet); 128 } 129 130 /** 131 * Array of {@link AccountType} -> {@link Map} 132 */ 133 private static Map<AccountTypeWithDataSet, AccountType> buildAccountTypes(AccountType... types) { 134 final HashMap<AccountTypeWithDataSet, AccountType> result = Maps.newHashMap(); 135 for (AccountType type : types) { 136 result.put(type.getAccountTypeAndDataSet(), type); 137 } 138 return result; 139 } 140 141 /** 142 * Array of {@link AccountWithDataSet} -> {@link Collection} 143 */ 144 private static Collection<AccountWithDataSet> buildAccounts(AccountWithDataSet... accounts) { 145 final List<AccountWithDataSet> result = Lists.newArrayList(); 146 for (AccountWithDataSet account : accounts) { 147 result.add(account); 148 } 149 return result; 150 } 151 152 /** 153 * Executes {@link AccountTypeManagerImpl#findInvitableAccountTypes} and verifies the 154 * result. 155 */ 156 private void verifyAccountTypes( 157 Collection<AccountWithDataSet> accounts, 158 Map<AccountTypeWithDataSet, AccountType> types, 159 AccountType... expectedInvitableTypes 160 ) { 161 Map<AccountTypeWithDataSet, AccountType> result = 162 AccountTypeManagerImpl.findAllInvitableAccountTypes(getContext(), accounts, types); 163 for (AccountType type : expectedInvitableTypes) { 164 assertTrue("Result doesn't contain type=" + type.getAccountTypeAndDataSet(), 165 result.containsKey(type.getAccountTypeAndDataSet())); 166 } 167 final int numExcessTypes = result.size() - expectedInvitableTypes.length; 168 assertEquals("Result contains " + numExcessTypes + " excess type(s)", 0, numExcessTypes); 169 } 170 171 private static class MockAccountType extends AccountType { 172 private final String mInviteContactActivityClassName; 173 174 public MockAccountType(String type, String dataSet, String inviteContactActivityClassName) { 175 accountType = type; 176 this.dataSet = dataSet; 177 mInviteContactActivityClassName = inviteContactActivityClassName; 178 } 179 180 @Override 181 public String getInviteContactActivityClassName() { 182 return mInviteContactActivityClassName; 183 } 184 185 @Override 186 public boolean isGroupMembershipEditable() { 187 return false; 188 } 189 190 @Override 191 public boolean areContactsWritable() { 192 return false; 193 } 194 } 195} 196