blob: 982517b1b9562554f2564599ba959873f5e0c857 [file] [log] [blame]
Chiao Cheng6c712f42012-11-26 15:35:28 -08001/*
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
Gary Mai69c182a2016-12-05 13:07:03 -080017package com.android.contacts.model;
Chiao Cheng6c712f42012-11-26 15:35:28 -080018
Wenyi Wang60042b02016-09-30 17:23:25 -070019import android.accounts.Account;
20import android.accounts.AccountManager;
Wenyi Wang60042b02016-09-30 17:23:25 -070021import android.content.SharedPreferences;
Chiao Cheng6c712f42012-11-26 15:35:28 -080022import android.test.AndroidTestCase;
23import android.test.suitebuilder.annotation.SmallTest;
24
Gary Mai69c182a2016-12-05 13:07:03 -080025import com.android.contacts.model.account.AccountType;
26import com.android.contacts.model.account.AccountTypeWithDataSet;
27import com.android.contacts.model.account.AccountWithDataSet;
28import com.android.contacts.model.account.GoogleAccountType;
Chiao Cheng6c712f42012-11-26 15:35:28 -080029import com.google.common.collect.Lists;
30import com.google.common.collect.Maps;
31
Wenyi Wang60042b02016-09-30 17:23:25 -070032import org.mockito.Mock;
33import org.mockito.Mockito;
34import org.mockito.MockitoAnnotations;
35
Chiao Cheng6c712f42012-11-26 15:35:28 -080036import java.util.Collection;
37import java.util.HashMap;
38import java.util.List;
39import java.util.Map;
40
Marcus Hagerott881ffc02016-12-09 13:34:03 -080041import static org.mockito.Mockito.when;
42
Chiao Cheng6c712f42012-11-26 15:35:28 -080043/**
Gary Mai69c182a2016-12-05 13:07:03 -080044 * Test case for {@link com.android.contacts.model.AccountTypeManager}.
Chiao Cheng6c712f42012-11-26 15:35:28 -080045 *
46 * adb shell am instrument -w -e class com.android.contacts.model.AccountTypeManagerTest \
47 com.android.contacts.tests/android.test.InstrumentationTestRunner
48 */
49@SmallTest
50public class AccountTypeManagerTest extends AndroidTestCase {
Wenyi Wang60042b02016-09-30 17:23:25 -070051
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
Chiao Cheng6c712f42012-11-26 15:35:28 -080068 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
Wenyi Wang60042b02016-09-30 17:23:25 -070094 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 }
Chiao Cheng6c712f42012-11-26 15:35:28 -0800140}