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