blob: e1c370af4c4580552f1561229878848730cd8912 [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 */
Gary Mai0a49afa2016-12-05 15:53:58 -080016package com.android.contacts.test.mocks;
Yorke Lee2644d942013-10-28 11:05:43 -070017
Wenyi Wang56c8a0c2016-09-30 11:11:10 -070018import android.accounts.Account;
19
Gary Mai69c182a2016-12-05 13:07:03 -080020import 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;
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;
Marcus Hagerott04be88c2016-12-07 09:55:03 -080029import com.google.common.util.concurrent.Futures;
30import com.google.common.util.concurrent.ListenableFuture;
Yorke Lee2644d942013-10-28 11:05:43 -070031
32import java.util.Arrays;
33import java.util.List;
Yorke Lee2644d942013-10-28 11:05:43 -070034
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
Marcus Hagerott04be88c2016-12-07 09:55:03 -080078 public ListenableFuture<List<AccountWithDataSet>> getAllAccountsAsync() {
79 return Futures.immediateFuture(Arrays.asList(mAccounts));
80 }
81
82 @Override
Marcus Hagerotte7a71cb2016-12-09 16:26:14 -080083 public ListenableFuture<List<AccountWithDataSet>> filterAccountsByTypeAsync(
84 Predicate<AccountType> type) {
85 return Futures.immediateFuture(Arrays.asList(mAccounts));
86 }
87
88 @Override
Yorke Lee2644d942013-10-28 11:05:43 -070089 public List<AccountWithDataSet> getGroupWritableAccounts() {
90 return Arrays.asList(mAccounts);
91 }
92
93 @Override
Wenyi Wang56c8a0c2016-09-30 11:11:10 -070094 public Account getDefaultGoogleAccount() {
95 return null;
96 }
Yorke Lee2644d942013-10-28 11:05:43 -070097}