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