1 /* 2 * Copyright (C) 2016 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.tests; 17 18 import android.content.ContentProviderResult; 19 import android.content.OperationApplicationException; 20 import android.os.RemoteException; 21 22 import com.android.contacts.database.SimContactDao; 23 import com.android.contacts.model.SimCard; 24 import com.android.contacts.model.SimContact; 25 import com.android.contacts.model.account.AccountWithDataSet; 26 27 import java.util.ArrayList; 28 import java.util.Arrays; 29 import java.util.HashMap; 30 import java.util.List; 31 import java.util.Map; 32 import java.util.Set; 33 34 /** 35 * Fake implementation of SimContactDao for testing 36 */ 37 public class FakeSimContactDao extends SimContactDao { 38 39 public boolean canReadSimContacts = true; 40 public List<SimCard> simCards; 41 public Map<SimCard, ArrayList<SimContact>> simContacts; 42 public ContentProviderResult[] importResult; 43 public Map<AccountWithDataSet, Set<SimContact>> existingSimContacts; 44 FakeSimContactDao()45 public FakeSimContactDao() { 46 simCards = new ArrayList<>(); 47 simContacts = new HashMap<>(); 48 importResult = new ContentProviderResult[0]; 49 existingSimContacts = new HashMap<>(); 50 } 51 52 @Override canReadSimContacts()53 public boolean canReadSimContacts() { 54 return canReadSimContacts; 55 } 56 57 @Override getSimCards()58 public List<SimCard> getSimCards() { 59 return simCards; 60 } 61 62 @Override loadContactsForSim(SimCard sim)63 public ArrayList<SimContact> loadContactsForSim(SimCard sim) { 64 return simContacts.get(sim); 65 } 66 67 @Override importContacts(List<SimContact> contacts, AccountWithDataSet targetAccount)68 public ContentProviderResult[] importContacts(List<SimContact> contacts, 69 AccountWithDataSet targetAccount) 70 throws RemoteException, OperationApplicationException { 71 return importResult; 72 } 73 74 @Override persistSimStates(List<SimCard> simCards)75 public void persistSimStates(List<SimCard> simCards) { 76 this.simCards = simCards; 77 } 78 79 @Override getSimBySubscriptionId(int subscriptionId)80 public SimCard getSimBySubscriptionId(int subscriptionId) { 81 if (subscriptionId == SimCard.NO_SUBSCRIPTION_ID) { 82 return simCards.get(0); 83 } 84 for (SimCard sim : simCards) { 85 if (sim.getSubscriptionId() == subscriptionId) { 86 return sim; 87 } 88 } 89 return null; 90 } 91 92 @Override findAccountsOfExistingSimContacts( List<SimContact> contacts)93 public Map<AccountWithDataSet, Set<SimContact>> findAccountsOfExistingSimContacts( 94 List<SimContact> contacts) { 95 return existingSimContacts; 96 } 97 addSim(SimCard sim, SimContact... contacts)98 public FakeSimContactDao addSim(SimCard sim, SimContact... contacts) { 99 simCards.add(sim); 100 simContacts.put(sim, new ArrayList<>(Arrays.asList(contacts))); 101 return this; 102 } 103 singleSimWithContacts(SimCard sim, SimContact... contacts)104 public static FakeSimContactDao singleSimWithContacts(SimCard sim, SimContact... contacts) { 105 return new FakeSimContactDao().addSim(sim, contacts); 106 } 107 noSim()108 public static FakeSimContactDao noSim() { 109 FakeSimContactDao result = new FakeSimContactDao(); 110 result.canReadSimContacts = false; 111 return result; 112 } 113 114 } 115