1 /* 2 * Copyright (C) 2011 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 17 package com.android.contacts.model; 18 19 import android.accounts.Account; 20 import android.accounts.AccountManager; 21 import android.content.SharedPreferences; 22 import android.test.AndroidTestCase; 23 import android.test.suitebuilder.annotation.SmallTest; 24 25 import com.android.contacts.model.account.AccountType; 26 import com.android.contacts.model.account.AccountTypeWithDataSet; 27 import com.android.contacts.model.account.AccountWithDataSet; 28 import com.android.contacts.model.account.GoogleAccountType; 29 import com.google.common.collect.Lists; 30 import com.google.common.collect.Maps; 31 32 import org.mockito.Mock; 33 import org.mockito.Mockito; 34 import org.mockito.MockitoAnnotations; 35 36 import java.util.Collection; 37 import java.util.HashMap; 38 import java.util.List; 39 import java.util.Map; 40 41 import static org.mockito.Mockito.when; 42 43 /** 44 * Test case for {@link com.android.contacts.model.AccountTypeManager}. 45 * 46 * adb shell am instrument -w -e class com.android.contacts.model.AccountTypeManagerTest \ 47 com.android.contacts.tests/android.test.InstrumentationTestRunner 48 */ 49 @SmallTest 50 public class AccountTypeManagerTest extends AndroidTestCase { 51 52 private static final Account[] ACCOUNTS = new Account[2]; 53 static { 54 ACCOUNTS[0] = new Account("name1", GoogleAccountType.ACCOUNT_TYPE); 55 ACCOUNTS[1] = new Account("name2", GoogleAccountType.ACCOUNT_TYPE); 56 } 57 58 @Mock private AccountManager mAccountManager; 59 @Mock private SharedPreferences mPrefs; 60 61 @Override 62 public void setUp() throws Exception { 63 super.setUp(); 64 System.setProperty("dexmaker.dexcache", getContext().getCacheDir().getPath()); 65 MockitoAnnotations.initMocks(this); 66 } 67 68 private static AccountWithDataSet createAccountWithDataSet(String name, AccountType type) { 69 return new AccountWithDataSet(name, type.accountType, type.dataSet); 70 } 71 72 /** 73 * Array of {@link AccountType} -> {@link Map} 74 */ 75 private static Map<AccountTypeWithDataSet, AccountType> buildAccountTypes(AccountType... types) { 76 final HashMap<AccountTypeWithDataSet, AccountType> result = Maps.newHashMap(); 77 for (AccountType type : types) { 78 result.put(type.getAccountTypeAndDataSet(), type); 79 } 80 return result; 81 } 82 83 /** 84 * Array of {@link AccountWithDataSet} -> {@link Collection} 85 */ 86 private static Collection<AccountWithDataSet> buildAccounts(AccountWithDataSet... accounts) { 87 final List<AccountWithDataSet> result = Lists.newArrayList(); 88 for (AccountWithDataSet account : accounts) { 89 result.add(account); 90 } 91 return result; 92 } 93 94 public void testGetDefaultAccount_NoAccounts() { 95 assertNull(getDefaultGoogleAccountName()); 96 } 97 98 public void testGetDefaultAccount_NoAccounts_DefaultPreferenceSet() { 99 when(mPrefs.getString(Mockito.anyString(), Mockito.anyString())).thenReturn( 100 getDefaultAccountPreference("name1", GoogleAccountType.ACCOUNT_TYPE)); 101 assertNull(getDefaultGoogleAccountName()); 102 } 103 104 public void testGetDefaultAccount_NoDefaultAccountPreferenceSet() { 105 when(mAccountManager.getAccountsByType(Mockito.anyString())).thenReturn(ACCOUNTS); 106 assertEquals("name1", getDefaultGoogleAccountName()); 107 } 108 109 public void testGetDefaultAccount_DefaultAccountPreferenceSet() { 110 when(mAccountManager.getAccountsByType(Mockito.anyString())).thenReturn(ACCOUNTS); 111 when(mPrefs.getString(Mockito.anyString(), Mockito.anyString())).thenReturn( 112 getDefaultAccountPreference("name2", GoogleAccountType.ACCOUNT_TYPE)); 113 assertEquals("name2", getDefaultGoogleAccountName()); 114 } 115 116 public void testGetDefaultAccount_DefaultAccountPreferenceSet_NonGoogleAccountType() { 117 when(mAccountManager.getAccountsByType(Mockito.anyString())).thenReturn(ACCOUNTS); 118 when(mPrefs.getString(Mockito.anyString(), Mockito.anyString())).thenReturn( 119 getDefaultAccountPreference("name3", "type3")); 120 assertEquals("name1", getDefaultGoogleAccountName()); 121 } 122 123 public void testGetDefaultAccount_DefaultAccountPreferenceSet_UnknownName() { 124 when(mAccountManager.getAccountsByType(Mockito.anyString())).thenReturn(ACCOUNTS); 125 when(mPrefs.getString(Mockito.anyString(), Mockito.anyString())).thenReturn( 126 getDefaultAccountPreference("name4",GoogleAccountType.ACCOUNT_TYPE)); 127 assertEquals("name1", getDefaultGoogleAccountName()); 128 } 129 130 private final String getDefaultGoogleAccountName() { 131 // We don't need the real preference key value since it's mocked 132 final Account account = AccountTypeManager.getDefaultGoogleAccount( 133 mAccountManager, mPrefs, "contact_editor_default_account_key"); 134 return account == null ? null : account.name; 135 } 136 137 private static final String getDefaultAccountPreference(String name, String type) { 138 return new AccountWithDataSet(name, type, /* dataSet */ null).stringify(); 139 } 140 } 141