MockAccountTypeManager.java revision e0b2f1e2d01d1ac52ba207dc7ce76971d853298e
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.List;
27import java.util.Map;
28
29import libcore.util.Objects;
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        for (AccountType type : mTypes) {
47            if (Objects.equal(accountTypeWithDataSet.accountType, type.accountType)
48                    && Objects.equal(accountTypeWithDataSet.dataSet, type.dataSet)) {
49                return type;
50            }
51        }
52        return null;
53    }
54
55    @Override
56    public List<AccountWithDataSet> getAccounts(boolean writableOnly) {
57        return Arrays.asList(mAccounts);
58    }
59
60    @Override
61    public List<AccountWithDataSet> getGroupWritableAccounts() {
62        return Arrays.asList(mAccounts);
63    }
64
65    @Override
66    public Map<AccountTypeWithDataSet, AccountType> getUsableInvitableAccountTypes() {
67        return Maps.newHashMap(); // Always returns empty
68    }
69
70    @Override
71    public List<AccountType> getAccountTypes(boolean writableOnly) {
72        final List<AccountType> ret = Lists.newArrayList();
73        synchronized (this) {
74            for (AccountType type : mTypes) {
75                if (!writableOnly || type.areContactsWritable()) {
76                    ret.add(type);
77                }
78            }
79        }
80        return ret;
81    }
82}
83