blob: 9756a0c50b1fe3efd687545fab58affea36184e7 [file] [log] [blame]
Makoto Onuki558669d2011-09-22 18:09:28 -07001/*
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
17package com.android.contacts.editor;
18
Chiao Chenge0b2f1e2012-06-12 13:07:56 -070019import android.test.AndroidTestCase;
20import android.test.MoreAsserts;
21import android.test.suitebuilder.annotation.SmallTest;
22
Gary Mai69c182a2016-12-05 13:07:03 -080023import com.android.contacts.model.account.AccountType;
24import com.android.contacts.model.account.AccountWithDataSet;
Gary Mai0a49afa2016-12-05 15:53:58 -080025import com.android.contacts.test.mocks.MockAccountTypeManager;
26
Chiao Cheng3e435f02012-06-11 16:18:34 -070027import com.google.common.collect.Sets;
Makoto Onuki558669d2011-09-22 18:09:28 -070028
Makoto Onuki558669d2011-09-22 18:09:28 -070029import java.util.Collection;
30import java.util.Set;
31
32/**
33 * Test case for {@link ContactEditorUtils}.
34 *
35 * adb shell am instrument -w -e class com.android.contacts.editor.ContactEditorUtilsTest \
36 com.android.contacts.tests/android.test.InstrumentationTestRunner
37 */
38@SmallTest
39public class ContactEditorUtilsTest extends AndroidTestCase {
40 private MockAccountTypeManager mAccountTypes;
41 private ContactEditorUtils mTarget;
42
43 private static final MockAccountType TYPE1 = new MockAccountType("type1", null, true);
44 private static final MockAccountType TYPE2 = new MockAccountType("type2", null, true);
45 private static final MockAccountType TYPE2EX = new MockAccountType("type2", "ext", true);
46
47 // Only type 3 is "readonly".
48 private static final MockAccountType TYPE3 = new MockAccountType("type3", null, false);
49
50 private static final AccountWithDataSet ACCOUNT_1_A = new AccountWithDataSet(
51 "a", TYPE1.accountType, TYPE1.dataSet);
52 private static final AccountWithDataSet ACCOUNT_1_B = new AccountWithDataSet(
53 "b", TYPE1.accountType, TYPE1.dataSet);
54
55 private static final AccountWithDataSet ACCOUNT_2_A = new AccountWithDataSet(
56 "a", TYPE2.accountType, TYPE2.dataSet);
57 private static final AccountWithDataSet ACCOUNT_2EX_A = new AccountWithDataSet(
58 "a", TYPE2EX.accountType, TYPE2EX.dataSet);
59
60 private static final AccountWithDataSet ACCOUNT_3_C = new AccountWithDataSet(
61 "c", TYPE3.accountType, TYPE3.dataSet);
62
63 @Override
64 protected void setUp() throws Exception {
65 // Initialize with 0 types, 0 accounts.
66 mAccountTypes = new MockAccountTypeManager(new AccountType[] {},
67 new AccountWithDataSet[] {});
68 mTarget = new ContactEditorUtils(getContext(), mAccountTypes);
69
70 // Clear the preferences.
71 mTarget.cleanupForTest();
72 }
73
74 private void setAccountTypes(AccountType... types) {
75 mAccountTypes.mTypes = types;
76 }
77
78 private void setAccounts(AccountWithDataSet... accounts) {
79 mAccountTypes.mAccounts = accounts;
80 }
81
Makoto Onuki558669d2011-09-22 18:09:28 -070082 /**
83 * Test for
Marcus Hagerott949d4e82016-09-20 13:23:05 -070084 * - {@link ContactEditorUtils#saveDefaultAccount}
85 * - {@link ContactEditorUtils#getOnlyOrDefaultAccount}
Makoto Onuki558669d2011-09-22 18:09:28 -070086 */
Marcus Hagerott949d4e82016-09-20 13:23:05 -070087 public void testSaveDefaultAccount() {
Makoto Onuki558669d2011-09-22 18:09:28 -070088 // Use these account types here.
89 setAccountTypes(TYPE1, TYPE2);
90
Marcus Hagerott949d4e82016-09-20 13:23:05 -070091 mTarget.saveDefaultAccount(null);
92 assertNull(mTarget.getOnlyOrDefaultAccount());
Makoto Onuki558669d2011-09-22 18:09:28 -070093
Marcus Hagerott949d4e82016-09-20 13:23:05 -070094 mTarget.saveDefaultAccount(ACCOUNT_1_A);
95 assertEquals(ACCOUNT_1_A, mTarget.getOnlyOrDefaultAccount());
Makoto Onuki558669d2011-09-22 18:09:28 -070096 }
97
98 /**
99 * Tests for {@link ContactEditorUtils#shouldShowAccountChangedNotification()}, starting with
100 * 0 accounts.
101 */
102 public void testShouldShowAccountChangedNotification_0Accounts() {
Makoto Onuki558669d2011-09-22 18:09:28 -0700103 setAccountTypes(TYPE1);
104
105 // First launch -- always true.
106 assertTrue(mTarget.shouldShowAccountChangedNotification());
107
108 // We show the notification here, and user clicked "add account"
109 setAccounts(ACCOUNT_1_A);
110
111 // Now we open the contact editor with the new account.
112
113 // When closing the editor, we save the default account.
Marcus Hagerott949d4e82016-09-20 13:23:05 -0700114 mTarget.saveDefaultAccount(ACCOUNT_1_A);
Makoto Onuki558669d2011-09-22 18:09:28 -0700115
116 // Next time the user creates a contact, we don't show the notification.
117 assertFalse(mTarget.shouldShowAccountChangedNotification());
118
119 // User added a new writable account, ACCOUNT_1_B.
120 setAccounts(ACCOUNT_1_A, ACCOUNT_1_B);
121
Tingting Wang83ff34f2015-09-03 15:08:56 -0700122 // Since default account is still ACCOUNT_1_A, we don't show the notification.
123 assertFalse(mTarget.shouldShowAccountChangedNotification());
Makoto Onuki558669d2011-09-22 18:09:28 -0700124
125 // User saved a new contact. We update the account list and the default account.
Marcus Hagerott949d4e82016-09-20 13:23:05 -0700126 mTarget.saveDefaultAccount(ACCOUNT_1_B);
Makoto Onuki558669d2011-09-22 18:09:28 -0700127
128 // User created another contact. Now we don't show the notification.
129 assertFalse(mTarget.shouldShowAccountChangedNotification());
130
131 // User installed a new contact sync adapter...
132
133 // Added a new account type: TYPE2, and the TYPE2EX extension.
134 setAccountTypes(TYPE1, TYPE2, TYPE2EX);
135 // Add new accounts: ACCOUNT_2_A, ACCOUNT_2EX_A.
136 setAccounts(ACCOUNT_1_A, ACCOUNT_1_B, ACCOUNT_2_A, ACCOUNT_2EX_A);
137
Tingting Wang83ff34f2015-09-03 15:08:56 -0700138 // New added account but default account is still not changed, so no notification.
139 assertFalse(mTarget.shouldShowAccountChangedNotification());
Makoto Onuki558669d2011-09-22 18:09:28 -0700140
141 // User saves a new contact, with a different default account.
Marcus Hagerott949d4e82016-09-20 13:23:05 -0700142 mTarget.saveDefaultAccount(ACCOUNT_2_A);
Makoto Onuki558669d2011-09-22 18:09:28 -0700143
144 // Next time user creates a contact, no notification.
145 assertFalse(mTarget.shouldShowAccountChangedNotification());
146
147 // Remove ACCOUNT_2EX_A.
148 setAccountTypes(TYPE1, TYPE2, TYPE2EX);
149 setAccounts(ACCOUNT_1_A, ACCOUNT_1_B, ACCOUNT_2_A);
150
151 // ACCOUNT_2EX_A was not default, so no notification either.
152 assertFalse(mTarget.shouldShowAccountChangedNotification());
153
154 // Remove ACCOUNT_1_B, which is default.
155 setAccountTypes(TYPE1, TYPE2, TYPE2EX);
156 setAccounts(ACCOUNT_1_A, ACCOUNT_1_B);
157
158 // Now we show the notification.
159 assertTrue(mTarget.shouldShowAccountChangedNotification());
Tingting Wang83ff34f2015-09-03 15:08:56 -0700160
161 // Do not save the default account, and add a new account now.
162 setAccountTypes(TYPE1, TYPE2, TYPE2EX);
163 setAccounts(ACCOUNT_1_A, ACCOUNT_1_B, ACCOUNT_2EX_A);
164
165 // No default account, so show notification.
166 assertTrue(mTarget.shouldShowAccountChangedNotification());
Makoto Onuki558669d2011-09-22 18:09:28 -0700167 }
168
169 /**
170 * Tests for {@link ContactEditorUtils#shouldShowAccountChangedNotification()}, starting with
171 * 1 accounts.
172 */
173 public void testShouldShowAccountChangedNotification_1Account() {
174 setAccountTypes(TYPE1, TYPE2);
175 setAccounts(ACCOUNT_1_A);
176
Tingting Wang37ad9fb2016-06-21 14:08:53 -0700177 // Always returns false when 1 writable account.
178 assertFalse(mTarget.shouldShowAccountChangedNotification());
Makoto Onuki558669d2011-09-22 18:09:28 -0700179
180 // User saves a new contact.
Marcus Hagerott949d4e82016-09-20 13:23:05 -0700181 mTarget.saveDefaultAccount(ACCOUNT_1_A);
Makoto Onuki558669d2011-09-22 18:09:28 -0700182
183 // Next time, no notification.
184 assertFalse(mTarget.shouldShowAccountChangedNotification());
185
186 // The rest is the same...
187 }
188
189 /**
190 * Tests for {@link ContactEditorUtils#shouldShowAccountChangedNotification()}, starting with
191 * 0 accounts, and the user selected "local only".
192 */
193 public void testShouldShowAccountChangedNotification_0Account_localOnly() {
Makoto Onuki558669d2011-09-22 18:09:28 -0700194 setAccountTypes(TYPE1);
195
196 // First launch -- always true.
197 assertTrue(mTarget.shouldShowAccountChangedNotification());
198
199 // We show the notification here, and user clicked "keep local" and saved an contact.
Marcus Hagerott949d4e82016-09-20 13:23:05 -0700200 mTarget.saveDefaultAccount(AccountWithDataSet.getNullAccount());
Makoto Onuki558669d2011-09-22 18:09:28 -0700201
202 // Now there are no accounts, and default account is null.
203
204 // The user created another contact, but this we shouldn't show the notification.
205 assertFalse(mTarget.shouldShowAccountChangedNotification());
206 }
207
Makoto Onuki131e6ac2011-10-04 19:09:54 -0700208 public void testShouldShowAccountChangedNotification_sanity_check() {
209 // Prepare 1 account and save it as the default.
210 setAccountTypes(TYPE1);
211 setAccounts(ACCOUNT_1_A);
212
Marcus Hagerott949d4e82016-09-20 13:23:05 -0700213 mTarget.saveDefaultAccount(ACCOUNT_1_A);
Makoto Onuki131e6ac2011-10-04 19:09:54 -0700214
215 // Right after a save, the dialog shouldn't show up.
216 assertFalse(mTarget.shouldShowAccountChangedNotification());
217
218 // Remove the default account to emulate broken preferences.
219 mTarget.removeDefaultAccountForTest();
guanxiongliu63859fe2016-02-05 14:46:38 -0800220
221 // The dialog shouldn't show up.
222 // The logic is, if there's a writable account, we'll pick it as default
223 assertFalse(mTarget.shouldShowAccountChangedNotification());
Makoto Onuki131e6ac2011-10-04 19:09:54 -0700224 }
225
Makoto Onuki558669d2011-09-22 18:09:28 -0700226 private static <T> Set<T> toSet(Collection<T> collection) {
227 Set<T> ret = Sets.newHashSet();
228 ret.addAll(collection);
229 return ret;
230 }
231
232 private static class MockAccountType extends AccountType {
233 private boolean mAreContactsWritable;
234
235 public MockAccountType(String accountType, String dataSet, boolean areContactsWritable) {
236 this.accountType = accountType;
237 this.dataSet = dataSet;
238 mAreContactsWritable = areContactsWritable;
239 }
240
241 @Override
242 public boolean areContactsWritable() {
243 return mAreContactsWritable;
244 }
245
246 @Override
Makoto Onuki558669d2011-09-22 18:09:28 -0700247 public boolean isGroupMembershipEditable() {
248 return true;
249 }
250 }
251}