Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 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.editor; |
| 18 | |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 19 | import android.test.AndroidTestCase; |
| 20 | import android.test.MoreAsserts; |
| 21 | import android.test.suitebuilder.annotation.SmallTest; |
| 22 | |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 23 | import com.android.contacts.model.account.AccountType; |
| 24 | import com.android.contacts.model.account.AccountWithDataSet; |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 25 | import com.android.contacts.test.mocks.MockAccountTypeManager; |
| 26 | |
Chiao Cheng | 3e435f0 | 2012-06-11 16:18:34 -0700 | [diff] [blame] | 27 | import com.google.common.collect.Sets; |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 28 | |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 29 | import java.util.Collection; |
| 30 | import 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 |
| 39 | public 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 Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 82 | /** |
| 83 | * Test for |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 84 | * - {@link ContactEditorUtils#saveDefaultAccount} |
| 85 | * - {@link ContactEditorUtils#getOnlyOrDefaultAccount} |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 86 | */ |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 87 | public void testSaveDefaultAccount() { |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 88 | // Use these account types here. |
| 89 | setAccountTypes(TYPE1, TYPE2); |
| 90 | |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 91 | mTarget.saveDefaultAccount(null); |
| 92 | assertNull(mTarget.getOnlyOrDefaultAccount()); |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 93 | |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 94 | mTarget.saveDefaultAccount(ACCOUNT_1_A); |
| 95 | assertEquals(ACCOUNT_1_A, mTarget.getOnlyOrDefaultAccount()); |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Tests for {@link ContactEditorUtils#shouldShowAccountChangedNotification()}, starting with |
| 100 | * 0 accounts. |
| 101 | */ |
| 102 | public void testShouldShowAccountChangedNotification_0Accounts() { |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 103 | 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 Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 114 | mTarget.saveDefaultAccount(ACCOUNT_1_A); |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 115 | |
| 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 Wang | 83ff34f | 2015-09-03 15:08:56 -0700 | [diff] [blame] | 122 | // Since default account is still ACCOUNT_1_A, we don't show the notification. |
| 123 | assertFalse(mTarget.shouldShowAccountChangedNotification()); |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 124 | |
| 125 | // User saved a new contact. We update the account list and the default account. |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 126 | mTarget.saveDefaultAccount(ACCOUNT_1_B); |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 127 | |
| 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 Wang | 83ff34f | 2015-09-03 15:08:56 -0700 | [diff] [blame] | 138 | // New added account but default account is still not changed, so no notification. |
| 139 | assertFalse(mTarget.shouldShowAccountChangedNotification()); |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 140 | |
| 141 | // User saves a new contact, with a different default account. |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 142 | mTarget.saveDefaultAccount(ACCOUNT_2_A); |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 143 | |
| 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 Wang | 83ff34f | 2015-09-03 15:08:56 -0700 | [diff] [blame] | 160 | |
| 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 Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 167 | } |
| 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 Wang | 37ad9fb | 2016-06-21 14:08:53 -0700 | [diff] [blame] | 177 | // Always returns false when 1 writable account. |
| 178 | assertFalse(mTarget.shouldShowAccountChangedNotification()); |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 179 | |
| 180 | // User saves a new contact. |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 181 | mTarget.saveDefaultAccount(ACCOUNT_1_A); |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 182 | |
| 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 Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 194 | 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 Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 200 | mTarget.saveDefaultAccount(AccountWithDataSet.getNullAccount()); |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 201 | |
| 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 Onuki | 131e6ac | 2011-10-04 19:09:54 -0700 | [diff] [blame] | 208 | 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 Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 213 | mTarget.saveDefaultAccount(ACCOUNT_1_A); |
Makoto Onuki | 131e6ac | 2011-10-04 19:09:54 -0700 | [diff] [blame] | 214 | |
| 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(); |
guanxiongliu | 63859fe | 2016-02-05 14:46:38 -0800 | [diff] [blame] | 220 | |
| 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 Onuki | 131e6ac | 2011-10-04 19:09:54 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Makoto Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 226 | 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 Onuki | 558669d | 2011-09-22 18:09:28 -0700 | [diff] [blame] | 247 | public boolean isGroupMembershipEditable() { |
| 248 | return true; |
| 249 | } |
| 250 | } |
| 251 | } |