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