MockAccountTypeManager.java revision 3e435f0e724cb3e88efce15f760e59b9bc08f0d3
1/*
2 * Copyright (C) 2010 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 */
16package com.android.contacts.tests.mocks;
17
18import com.android.contacts.model.AccountType;
19import com.android.contacts.model.AccountTypeManager;
20import com.android.contacts.model.AccountTypeWithDataSet;
21import com.android.contacts.model.AccountWithDataSet;
22import com.google.common.collect.Lists;
23import com.google.common.collect.Maps;
24
25import java.util.Arrays;
26import java.util.Collection;
27import java.util.List;
28import java.util.Map;
29
30import libcore.util.Objects;
31
32/**
33 * A mock {@link AccountTypeManager} class.
34 */
35public class MockAccountTypeManager extends AccountTypeManager {
36
37    public AccountType[] mTypes;
38    public AccountWithDataSet[] mAccounts;
39
40    public MockAccountTypeManager(AccountType[] types, AccountWithDataSet[] accounts) {
41        this.mTypes = types;
42        this.mAccounts = accounts;
43    }
44
45    @Override
46    public AccountType getAccountType(AccountTypeWithDataSet accountTypeWithDataSet) {
47        for (AccountType type : mTypes) {
48            if (Objects.equal(accountTypeWithDataSet.accountType, type.accountType)
49                    && Objects.equal(accountTypeWithDataSet.dataSet, type.dataSet)) {
50                return type;
51            }
52        }
53        return null;
54    }
55
56    @Override
57    public List<AccountWithDataSet> getAccounts(boolean writableOnly) {
58        return Arrays.asList(mAccounts);
59    }
60
61    @Override
62    public List<AccountWithDataSet> getGroupWritableAccounts() {
63        return Arrays.asList(mAccounts);
64    }
65
66    @Override
67    public Map<AccountTypeWithDataSet, AccountType> getUsableInvitableAccountTypes() {
68        return Maps.newHashMap(); // Always returns empty
69    }
70
71    @Override
72    public List<AccountType> getAccountTypes(boolean writableOnly) {
73        final List<AccountType> ret = Lists.newArrayList();
74        synchronized (this) {
75            for (AccountType type : mTypes) {
76                if (!writableOnly || type.areContactsWritable()) {
77                    ret.add(type);
78                }
79            }
80        }
81        return ret;
82    }
83}
84