blob: b701b3aae532595d01fdc25fa0fc4dc89034401f [file] [log] [blame]
Yorke Lee2644d942013-10-28 11:05:43 -07001/*
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
Wenyi Wang56c8a0c2016-09-30 11:11:10 -070018import android.accounts.Account;
19
Yorke Lee2644d942013-10-28 11:05:43 -070020import com.android.contacts.common.model.AccountTypeManager;
21import com.android.contacts.common.model.account.AccountType;
22import com.android.contacts.common.model.account.AccountTypeWithDataSet;
23import com.android.contacts.common.model.account.AccountWithDataSet;
Zheng Fu7958e582014-08-29 16:02:44 -070024import com.android.contacts.common.model.account.BaseAccountType;
Yorke Lee2644d942013-10-28 11:05:43 -070025import com.google.common.base.Objects;
Marcus Hagerott67a06392016-10-13 15:16:58 -070026import com.google.common.base.Predicate;
27import com.google.common.collect.Collections2;
Yorke Lee2644d942013-10-28 11:05:43 -070028import com.google.common.collect.Lists;
29import com.google.common.collect.Maps;
30
31import java.util.Arrays;
32import java.util.List;
33import java.util.Map;
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) {
Zheng Fu7958e582014-08-29 16:02:44 -070050 // 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";
Yorke Lee2644d942013-10-28 11:05:43 -070058 for (AccountType type : mTypes) {
59 if (Objects.equal(accountTypeWithDataSet.accountType, type.accountType)
60 && Objects.equal(accountTypeWithDataSet.dataSet, type.dataSet)) {
61 return type;
62 }
63 }
Zheng Fu7958e582014-08-29 16:02:44 -070064 return mFallbackAccountType;
Yorke Lee2644d942013-10-28 11:05:43 -070065 }
66
67 @Override
68 public List<AccountWithDataSet> getAccounts(boolean writableOnly) {
69 return Arrays.asList(mAccounts);
70 }
71
72 @Override
Marcus Hagerott67a06392016-10-13 15:16:58 -070073 public List<AccountWithDataSet> getAccounts(Predicate<AccountWithDataSet> filter) {
74 return Lists.newArrayList(Collections2.filter(Arrays.asList(mAccounts), filter));
75 }
76
77 @Override
Gary Maiac333592016-09-28 17:27:40 -070078 public List<AccountWithDataSet> getSortedAccounts(AccountWithDataSet account,
79 boolean writableOnly) {
80 return Arrays.asList(mAccounts);
81 }
Wenyi Wangaa0e6ff2016-07-06 17:22:42 -070082
83 @Override
Yorke Lee2644d942013-10-28 11:05:43 -070084 public List<AccountWithDataSet> getGroupWritableAccounts() {
85 return Arrays.asList(mAccounts);
86 }
87
88 @Override
Wenyi Wang56c8a0c2016-09-30 11:11:10 -070089 public Account getDefaultGoogleAccount() {
90 return null;
91 }
92
93 @Override
Yorke Lee2644d942013-10-28 11:05:43 -070094 public Map<AccountTypeWithDataSet, AccountType> getUsableInvitableAccountTypes() {
95 return Maps.newHashMap(); // Always returns empty
96 }
97
98 @Override
99 public List<AccountType> getAccountTypes(boolean writableOnly) {
100 final List<AccountType> ret = Lists.newArrayList();
101 synchronized (this) {
102 for (AccountType type : mTypes) {
103 if (!writableOnly || type.areContactsWritable()) {
104 ret.add(type);
105 }
106 }
107 }
108 return ret;
109 }
110}