blob: 525a85c15878f136787d34eb2ea507703e42be49 [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
Chiao Cheng428f0082012-11-13 18:38:56 -080023import com.android.contacts.common.model.account.AccountType;
24import com.android.contacts.common.model.account.AccountWithDataSet;
Yorke Leecd321f62013-10-28 15:20:15 -070025import com.android.contacts.common.test.mocks.MockAccountTypeManager;
Chiao Cheng3e435f02012-06-11 16:18:34 -070026import com.google.common.collect.Sets;
Makoto Onuki558669d2011-09-22 18:09:28 -070027
Makoto Onuki558669d2011-09-22 18:09:28 -070028import java.util.Collection;
29import java.util.Set;
30
31/**
32 * Test case for {@link ContactEditorUtils}.
33 *
34 * adb shell am instrument -w -e class com.android.contacts.editor.ContactEditorUtilsTest \
35 com.android.contacts.tests/android.test.InstrumentationTestRunner
36 */
37@SmallTest
38public class ContactEditorUtilsTest extends AndroidTestCase {
39 private MockAccountTypeManager mAccountTypes;
40 private ContactEditorUtils mTarget;
41
42 private static final MockAccountType TYPE1 = new MockAccountType("type1", null, true);
43 private static final MockAccountType TYPE2 = new MockAccountType("type2", null, true);
44 private static final MockAccountType TYPE2EX = new MockAccountType("type2", "ext", true);
45
46 // Only type 3 is "readonly".
47 private static final MockAccountType TYPE3 = new MockAccountType("type3", null, false);
48
49 private static final AccountWithDataSet ACCOUNT_1_A = new AccountWithDataSet(
50 "a", TYPE1.accountType, TYPE1.dataSet);
51 private static final AccountWithDataSet ACCOUNT_1_B = new AccountWithDataSet(
52 "b", TYPE1.accountType, TYPE1.dataSet);
53
54 private static final AccountWithDataSet ACCOUNT_2_A = new AccountWithDataSet(
55 "a", TYPE2.accountType, TYPE2.dataSet);
56 private static final AccountWithDataSet ACCOUNT_2EX_A = new AccountWithDataSet(
57 "a", TYPE2EX.accountType, TYPE2EX.dataSet);
58
59 private static final AccountWithDataSet ACCOUNT_3_C = new AccountWithDataSet(
60 "c", TYPE3.accountType, TYPE3.dataSet);
61
62 @Override
63 protected void setUp() throws Exception {
64 // Initialize with 0 types, 0 accounts.
65 mAccountTypes = new MockAccountTypeManager(new AccountType[] {},
66 new AccountWithDataSet[] {});
67 mTarget = new ContactEditorUtils(getContext(), mAccountTypes);
68
69 // Clear the preferences.
70 mTarget.cleanupForTest();
71 }
72
73 private void setAccountTypes(AccountType... types) {
74 mAccountTypes.mTypes = types;
75 }
76
77 private void setAccounts(AccountWithDataSet... accounts) {
78 mAccountTypes.mAccounts = accounts;
79 }
80
81 public void testGetWritableAccountTypeStrings() {
82 String[] types;
83
84 // 0 writable types
85 setAccountTypes();
86
87 types = mTarget.getWritableAccountTypeStrings();
88 MoreAsserts.assertEquals(types, new String[0]);
89
90 // 1 writable type
91 setAccountTypes(TYPE1);
92
93 types = mTarget.getWritableAccountTypeStrings();
94 MoreAsserts.assertEquals(Sets.newHashSet(TYPE1.accountType), Sets.newHashSet(types));
95
96 // 2 writable types
97 setAccountTypes(TYPE1, TYPE2EX);
98
99 types = mTarget.getWritableAccountTypeStrings();
100 MoreAsserts.assertEquals(Sets.newHashSet(TYPE1.accountType, TYPE2EX.accountType),
101 Sets.newHashSet(types));
102
103 // 3 writable types + 1 readonly type
104 setAccountTypes(TYPE1, TYPE2, TYPE2EX, TYPE3);
105
106 types = mTarget.getWritableAccountTypeStrings();
107 MoreAsserts.assertEquals(
108 Sets.newHashSet(TYPE1.accountType, TYPE2.accountType, TYPE2EX.accountType),
109 Sets.newHashSet(types));
110 }
111
112 /**
113 * Test for
Marcus Hagerott949d4e82016-09-20 13:23:05 -0700114 * - {@link ContactEditorUtils#saveDefaultAccount}
115 * - {@link ContactEditorUtils#getOnlyOrDefaultAccount}
Makoto Onuki558669d2011-09-22 18:09:28 -0700116 */
Marcus Hagerott949d4e82016-09-20 13:23:05 -0700117 public void testSaveDefaultAccount() {
Makoto Onuki558669d2011-09-22 18:09:28 -0700118 // Use these account types here.
119 setAccountTypes(TYPE1, TYPE2);
120
Marcus Hagerott949d4e82016-09-20 13:23:05 -0700121 mTarget.saveDefaultAccount(null);
122 assertNull(mTarget.getOnlyOrDefaultAccount());
Makoto Onuki558669d2011-09-22 18:09:28 -0700123
Marcus Hagerott949d4e82016-09-20 13:23:05 -0700124 mTarget.saveDefaultAccount(ACCOUNT_1_A);
125 assertEquals(ACCOUNT_1_A, mTarget.getOnlyOrDefaultAccount());
Makoto Onuki558669d2011-09-22 18:09:28 -0700126 }
127
128 /**
129 * Tests for {@link ContactEditorUtils#shouldShowAccountChangedNotification()}, starting with
130 * 0 accounts.
131 */
132 public void testShouldShowAccountChangedNotification_0Accounts() {
Makoto Onuki558669d2011-09-22 18:09:28 -0700133 setAccountTypes(TYPE1);
134
135 // First launch -- always true.
136 assertTrue(mTarget.shouldShowAccountChangedNotification());
137
138 // We show the notification here, and user clicked "add account"
139 setAccounts(ACCOUNT_1_A);
140
141 // Now we open the contact editor with the new account.
142
143 // When closing the editor, we save the default account.
Marcus Hagerott949d4e82016-09-20 13:23:05 -0700144 mTarget.saveDefaultAccount(ACCOUNT_1_A);
Makoto Onuki558669d2011-09-22 18:09:28 -0700145
146 // Next time the user creates a contact, we don't show the notification.
147 assertFalse(mTarget.shouldShowAccountChangedNotification());
148
149 // User added a new writable account, ACCOUNT_1_B.
150 setAccounts(ACCOUNT_1_A, ACCOUNT_1_B);
151
Tingting Wang83ff34f2015-09-03 15:08:56 -0700152 // Since default account is still ACCOUNT_1_A, we don't show the notification.
153 assertFalse(mTarget.shouldShowAccountChangedNotification());
Makoto Onuki558669d2011-09-22 18:09:28 -0700154
155 // User saved a new contact. We update the account list and the default account.
Marcus Hagerott949d4e82016-09-20 13:23:05 -0700156 mTarget.saveDefaultAccount(ACCOUNT_1_B);
Makoto Onuki558669d2011-09-22 18:09:28 -0700157
158 // User created another contact. Now we don't show the notification.
159 assertFalse(mTarget.shouldShowAccountChangedNotification());
160
161 // User installed a new contact sync adapter...
162
163 // Added a new account type: TYPE2, and the TYPE2EX extension.
164 setAccountTypes(TYPE1, TYPE2, TYPE2EX);
165 // Add new accounts: ACCOUNT_2_A, ACCOUNT_2EX_A.
166 setAccounts(ACCOUNT_1_A, ACCOUNT_1_B, ACCOUNT_2_A, ACCOUNT_2EX_A);
167
Tingting Wang83ff34f2015-09-03 15:08:56 -0700168 // New added account but default account is still not changed, so no notification.
169 assertFalse(mTarget.shouldShowAccountChangedNotification());
Makoto Onuki558669d2011-09-22 18:09:28 -0700170
171 // User saves a new contact, with a different default account.
Marcus Hagerott949d4e82016-09-20 13:23:05 -0700172 mTarget.saveDefaultAccount(ACCOUNT_2_A);
Makoto Onuki558669d2011-09-22 18:09:28 -0700173
174 // Next time user creates a contact, no notification.
175 assertFalse(mTarget.shouldShowAccountChangedNotification());
176
177 // Remove ACCOUNT_2EX_A.
178 setAccountTypes(TYPE1, TYPE2, TYPE2EX);
179 setAccounts(ACCOUNT_1_A, ACCOUNT_1_B, ACCOUNT_2_A);
180
181 // ACCOUNT_2EX_A was not default, so no notification either.
182 assertFalse(mTarget.shouldShowAccountChangedNotification());
183
184 // Remove ACCOUNT_1_B, which is default.
185 setAccountTypes(TYPE1, TYPE2, TYPE2EX);
186 setAccounts(ACCOUNT_1_A, ACCOUNT_1_B);
187
188 // Now we show the notification.
189 assertTrue(mTarget.shouldShowAccountChangedNotification());
Tingting Wang83ff34f2015-09-03 15:08:56 -0700190
191 // Do not save the default account, and add a new account now.
192 setAccountTypes(TYPE1, TYPE2, TYPE2EX);
193 setAccounts(ACCOUNT_1_A, ACCOUNT_1_B, ACCOUNT_2EX_A);
194
195 // No default account, so show notification.
196 assertTrue(mTarget.shouldShowAccountChangedNotification());
Makoto Onuki558669d2011-09-22 18:09:28 -0700197 }
198
199 /**
200 * Tests for {@link ContactEditorUtils#shouldShowAccountChangedNotification()}, starting with
201 * 1 accounts.
202 */
203 public void testShouldShowAccountChangedNotification_1Account() {
204 setAccountTypes(TYPE1, TYPE2);
205 setAccounts(ACCOUNT_1_A);
206
Tingting Wang37ad9fb2016-06-21 14:08:53 -0700207 // Always returns false when 1 writable account.
208 assertFalse(mTarget.shouldShowAccountChangedNotification());
Makoto Onuki558669d2011-09-22 18:09:28 -0700209
210 // User saves a new contact.
Marcus Hagerott949d4e82016-09-20 13:23:05 -0700211 mTarget.saveDefaultAccount(ACCOUNT_1_A);
Makoto Onuki558669d2011-09-22 18:09:28 -0700212
213 // Next time, no notification.
214 assertFalse(mTarget.shouldShowAccountChangedNotification());
215
216 // The rest is the same...
217 }
218
219 /**
220 * Tests for {@link ContactEditorUtils#shouldShowAccountChangedNotification()}, starting with
221 * 0 accounts, and the user selected "local only".
222 */
223 public void testShouldShowAccountChangedNotification_0Account_localOnly() {
Makoto Onuki558669d2011-09-22 18:09:28 -0700224 setAccountTypes(TYPE1);
225
226 // First launch -- always true.
227 assertTrue(mTarget.shouldShowAccountChangedNotification());
228
229 // We show the notification here, and user clicked "keep local" and saved an contact.
Marcus Hagerott949d4e82016-09-20 13:23:05 -0700230 mTarget.saveDefaultAccount(AccountWithDataSet.getNullAccount());
Makoto Onuki558669d2011-09-22 18:09:28 -0700231
232 // Now there are no accounts, and default account is null.
233
234 // The user created another contact, but this we shouldn't show the notification.
235 assertFalse(mTarget.shouldShowAccountChangedNotification());
236 }
237
Makoto Onuki131e6ac2011-10-04 19:09:54 -0700238 public void testShouldShowAccountChangedNotification_sanity_check() {
239 // Prepare 1 account and save it as the default.
240 setAccountTypes(TYPE1);
241 setAccounts(ACCOUNT_1_A);
242
Marcus Hagerott949d4e82016-09-20 13:23:05 -0700243 mTarget.saveDefaultAccount(ACCOUNT_1_A);
Makoto Onuki131e6ac2011-10-04 19:09:54 -0700244
245 // Right after a save, the dialog shouldn't show up.
246 assertFalse(mTarget.shouldShowAccountChangedNotification());
247
248 // Remove the default account to emulate broken preferences.
249 mTarget.removeDefaultAccountForTest();
guanxiongliu63859fe2016-02-05 14:46:38 -0800250
251 // The dialog shouldn't show up.
252 // The logic is, if there's a writable account, we'll pick it as default
253 assertFalse(mTarget.shouldShowAccountChangedNotification());
Makoto Onuki131e6ac2011-10-04 19:09:54 -0700254 }
255
Makoto Onuki558669d2011-09-22 18:09:28 -0700256 private static <T> Set<T> toSet(Collection<T> collection) {
257 Set<T> ret = Sets.newHashSet();
258 ret.addAll(collection);
259 return ret;
260 }
261
262 private static class MockAccountType extends AccountType {
263 private boolean mAreContactsWritable;
264
265 public MockAccountType(String accountType, String dataSet, boolean areContactsWritable) {
266 this.accountType = accountType;
267 this.dataSet = dataSet;
268 mAreContactsWritable = areContactsWritable;
269 }
270
271 @Override
272 public boolean areContactsWritable() {
273 return mAreContactsWritable;
274 }
275
276 @Override
Makoto Onuki558669d2011-09-22 18:09:28 -0700277 public boolean isGroupMembershipEditable() {
278 return true;
279 }
280 }
281}