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