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