Yorke Lee | 2644d94 | 2013-10-28 11:05:43 -0700 | [diff] [blame] | 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 | */ |
| 16 | package com.android.contacts.common.test.mocks; |
| 17 | |
Wenyi Wang | 56c8a0c | 2016-09-30 11:11:10 -0700 | [diff] [blame] | 18 | import android.accounts.Account; |
| 19 | |
Yorke Lee | 2644d94 | 2013-10-28 11:05:43 -0700 | [diff] [blame] | 20 | import com.android.contacts.common.model.AccountTypeManager; |
| 21 | import com.android.contacts.common.model.account.AccountType; |
| 22 | import com.android.contacts.common.model.account.AccountTypeWithDataSet; |
| 23 | import com.android.contacts.common.model.account.AccountWithDataSet; |
Zheng Fu | 7958e58 | 2014-08-29 16:02:44 -0700 | [diff] [blame] | 24 | import com.android.contacts.common.model.account.BaseAccountType; |
Yorke Lee | 2644d94 | 2013-10-28 11:05:43 -0700 | [diff] [blame] | 25 | import com.google.common.base.Objects; |
Marcus Hagerott | 67a0639 | 2016-10-13 15:16:58 -0700 | [diff] [blame] | 26 | import com.google.common.base.Predicate; |
| 27 | import com.google.common.collect.Collections2; |
Yorke Lee | 2644d94 | 2013-10-28 11:05:43 -0700 | [diff] [blame] | 28 | import com.google.common.collect.Lists; |
| 29 | import com.google.common.collect.Maps; |
| 30 | |
| 31 | import java.util.Arrays; |
| 32 | import java.util.List; |
| 33 | import java.util.Map; |
| 34 | |
| 35 | /** |
| 36 | * A mock {@link AccountTypeManager} class. |
| 37 | */ |
| 38 | public 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 Fu | 7958e58 | 2014-08-29 16:02:44 -0700 | [diff] [blame] | 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"; |
Yorke Lee | 2644d94 | 2013-10-28 11:05:43 -0700 | [diff] [blame] | 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 | } |
Zheng Fu | 7958e58 | 2014-08-29 16:02:44 -0700 | [diff] [blame] | 64 | return mFallbackAccountType; |
Yorke Lee | 2644d94 | 2013-10-28 11:05:43 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public List<AccountWithDataSet> getAccounts(boolean writableOnly) { |
| 69 | return Arrays.asList(mAccounts); |
| 70 | } |
| 71 | |
| 72 | @Override |
Marcus Hagerott | 67a0639 | 2016-10-13 15:16:58 -0700 | [diff] [blame] | 73 | public List<AccountWithDataSet> getAccounts(Predicate<AccountWithDataSet> filter) { |
| 74 | return Lists.newArrayList(Collections2.filter(Arrays.asList(mAccounts), filter)); |
| 75 | } |
| 76 | |
| 77 | @Override |
Gary Mai | ac33359 | 2016-09-28 17:27:40 -0700 | [diff] [blame] | 78 | public List<AccountWithDataSet> getSortedAccounts(AccountWithDataSet account, |
| 79 | boolean writableOnly) { |
| 80 | return Arrays.asList(mAccounts); |
| 81 | } |
Wenyi Wang | aa0e6ff | 2016-07-06 17:22:42 -0700 | [diff] [blame] | 82 | |
| 83 | @Override |
Yorke Lee | 2644d94 | 2013-10-28 11:05:43 -0700 | [diff] [blame] | 84 | public List<AccountWithDataSet> getGroupWritableAccounts() { |
| 85 | return Arrays.asList(mAccounts); |
| 86 | } |
| 87 | |
| 88 | @Override |
Wenyi Wang | 56c8a0c | 2016-09-30 11:11:10 -0700 | [diff] [blame] | 89 | public Account getDefaultGoogleAccount() { |
| 90 | return null; |
| 91 | } |
| 92 | |
| 93 | @Override |
Yorke Lee | 2644d94 | 2013-10-28 11:05:43 -0700 | [diff] [blame] | 94 | 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 | } |