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 | */ |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 16 | package com.android.contacts.test.mocks; |
Yorke Lee | 2644d94 | 2013-10-28 11:05:43 -0700 | [diff] [blame] | 17 | |
Wenyi Wang | 56c8a0c | 2016-09-30 11:11:10 -0700 | [diff] [blame] | 18 | import android.accounts.Account; |
| 19 | |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 20 | import com.android.contacts.model.AccountTypeManager; |
| 21 | import com.android.contacts.model.account.AccountType; |
| 22 | import com.android.contacts.model.account.AccountTypeWithDataSet; |
| 23 | import com.android.contacts.model.account.AccountWithDataSet; |
| 24 | import com.android.contacts.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; |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame^] | 29 | import com.google.common.util.concurrent.Futures; |
| 30 | import com.google.common.util.concurrent.ListenableFuture; |
Yorke Lee | 2644d94 | 2013-10-28 11:05:43 -0700 | [diff] [blame] | 31 | |
| 32 | import java.util.Arrays; |
| 33 | import java.util.List; |
Yorke Lee | 2644d94 | 2013-10-28 11:05:43 -0700 | [diff] [blame] | 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 |
Marcus Hagerott | 04be88c | 2016-12-07 09:55:03 -0800 | [diff] [blame^] | 78 | public ListenableFuture<List<AccountWithDataSet>> getAllAccountsAsync() { |
| 79 | return Futures.immediateFuture(Arrays.asList(mAccounts)); |
| 80 | } |
| 81 | |
| 82 | @Override |
Yorke Lee | 2644d94 | 2013-10-28 11:05:43 -0700 | [diff] [blame] | 83 | public List<AccountWithDataSet> getGroupWritableAccounts() { |
| 84 | return Arrays.asList(mAccounts); |
| 85 | } |
| 86 | |
| 87 | @Override |
Wenyi Wang | 56c8a0c | 2016-09-30 11:11:10 -0700 | [diff] [blame] | 88 | public Account getDefaultGoogleAccount() { |
| 89 | return null; |
| 90 | } |
Yorke Lee | 2644d94 | 2013-10-28 11:05:43 -0700 | [diff] [blame] | 91 | } |