1 /* 2 * Copyright (C) 2015 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.preference; 18 19 import android.content.Context; 20 import android.content.SharedPreferences; 21 import android.content.res.Resources; 22 import android.test.InstrumentationTestCase; 23 import android.test.suitebuilder.annotation.SmallTest; 24 25 import androidx.test.InstrumentationRegistry; 26 27 import com.android.contacts.model.account.AccountWithDataSet; 28 29 import junit.framework.Assert; 30 31 import org.mockito.Mock; 32 import org.mockito.Mockito; 33 import org.mockito.MockitoAnnotations; 34 35 import java.util.Arrays; 36 37 @SmallTest 38 public class ContactsPreferencesTest extends InstrumentationTestCase { 39 40 private static final String ACCOUNT_KEY = "ACCOUNT_KEY"; 41 42 @Mock private Context mContext; 43 @Mock private Resources mResources; 44 @Mock private SharedPreferences mSharedPreferences; 45 46 private ContactsPreferences mContactsPreferences; 47 48 @Override setUp()49 public void setUp() throws Exception { 50 super.setUp(); 51 System.setProperty("dexmaker.dexcache", 52 getInstrumentation().getTargetContext().getCacheDir().getPath()); 53 MockitoAnnotations.initMocks(this); 54 55 Mockito.when(mContext.getResources()).thenReturn(mResources); 56 Mockito.when(mResources.getString(Mockito.anyInt())) 57 .thenReturn(ACCOUNT_KEY); // contact_editor_default_account_key 58 59 Mockito.when(mContext.getSharedPreferences(Mockito.anyString(), Mockito.anyInt())) 60 .thenReturn(mSharedPreferences); 61 Mockito.when(mSharedPreferences.contains(ContactsPreferences.SORT_ORDER_KEY)) 62 .thenReturn(true); 63 Mockito.when(mSharedPreferences.contains(ContactsPreferences.DISPLAY_ORDER_KEY)) 64 .thenReturn(true); 65 Mockito.when(mSharedPreferences.contains(ContactsPreferences.PHONETIC_NAME_DISPLAY_KEY)) 66 .thenReturn(true); 67 68 InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() { 69 @Override 70 public void run() { 71 mContactsPreferences = new ContactsPreferences(mContext); 72 } 73 }); 74 } 75 testGetSortOrderDefault()76 public void testGetSortOrderDefault() { 77 Mockito.when(mResources.getBoolean(Mockito.anyInt())).thenReturn( 78 false, // R.bool.config_sort_order_user_changeable 79 true // R.bool.config_default_sort_order_primary 80 ); 81 Assert.assertEquals(ContactsPreferences.SORT_ORDER_PRIMARY, 82 mContactsPreferences.getSortOrder()); 83 } 84 testGetSortOrder()85 public void testGetSortOrder() { 86 Mockito.when(mResources.getBoolean(Mockito.anyInt())).thenReturn( 87 true // R.bool.config_sort_order_user_changeable 88 ); 89 Mockito.when(mSharedPreferences.getInt(Mockito.eq(ContactsPreferences.SORT_ORDER_KEY), 90 Mockito.anyInt())).thenReturn(ContactsPreferences.SORT_ORDER_PRIMARY); 91 Assert.assertEquals(ContactsPreferences.SORT_ORDER_PRIMARY, 92 mContactsPreferences.getSortOrder()); 93 } 94 testGetDisplayOrderDefault()95 public void testGetDisplayOrderDefault() { 96 Mockito.when(mResources.getBoolean(Mockito.anyInt())).thenReturn( 97 false, // R.bool.config_display_order_user_changeable 98 true // R.bool.config_default_display_order_primary 99 ); 100 Assert.assertEquals(ContactsPreferences.DISPLAY_ORDER_PRIMARY, 101 mContactsPreferences.getDisplayOrder()); 102 } 103 testGetDisplayOrder()104 public void testGetDisplayOrder() { 105 Mockito.when(mResources.getBoolean(Mockito.anyInt())).thenReturn( 106 true // R.bool.config_display_order_user_changeable 107 ); 108 Mockito.when(mSharedPreferences.getInt(Mockito.eq(ContactsPreferences.DISPLAY_ORDER_KEY), 109 Mockito.anyInt())).thenReturn(ContactsPreferences.DISPLAY_ORDER_PRIMARY); 110 Assert.assertEquals(ContactsPreferences.DISPLAY_ORDER_PRIMARY, 111 mContactsPreferences.getDisplayOrder()); 112 } 113 testGetPhoneticNameDisplayDefault()114 public void testGetPhoneticNameDisplayDefault() { 115 Mockito.when(mResources.getBoolean(Mockito.anyInt())).thenReturn( 116 false, // R.bool.config_phonetic_name_display_user_changeable 117 true // R.bool.config_default_hide_phonetic_name_if_empty 118 ); 119 Assert.assertEquals(PhoneticNameDisplayPreference.HIDE_IF_EMPTY, 120 mContactsPreferences.getPhoneticNameDisplayPreference()); 121 } 122 testGetPhoneticNameDisplay()123 public void testGetPhoneticNameDisplay() { 124 Mockito.when(mResources.getBoolean(Mockito.anyInt())).thenReturn( 125 true // R.bool.config_phonetic_name_display_user_changeable 126 ); 127 Mockito.when(mSharedPreferences.getInt( 128 Mockito.eq(ContactsPreferences.PHONETIC_NAME_DISPLAY_KEY), 129 Mockito.anyInt())).thenReturn(PhoneticNameDisplayPreference.HIDE_IF_EMPTY); 130 Assert.assertEquals(PhoneticNameDisplayPreference.HIDE_IF_EMPTY, 131 mContactsPreferences.getPhoneticNameDisplayPreference()); 132 } 133 testRefreshPhoneticNameDisplay()134 public void testRefreshPhoneticNameDisplay() throws InterruptedException { 135 Mockito.when(mResources.getBoolean(Mockito.anyInt())).thenReturn( 136 true // R.bool.config_phonetic_name_display_user_changeable 137 ); 138 Mockito.when(mSharedPreferences.getInt( 139 Mockito.eq(ContactsPreferences.PHONETIC_NAME_DISPLAY_KEY), 140 Mockito.anyInt())).thenReturn(PhoneticNameDisplayPreference.HIDE_IF_EMPTY, 141 PhoneticNameDisplayPreference.SHOW_ALWAYS); 142 143 Assert.assertEquals(PhoneticNameDisplayPreference.HIDE_IF_EMPTY, 144 mContactsPreferences.getPhoneticNameDisplayPreference()); 145 mContactsPreferences.refreshValue(ContactsPreferences.PHONETIC_NAME_DISPLAY_KEY); 146 147 Assert.assertEquals(PhoneticNameDisplayPreference.SHOW_ALWAYS, 148 mContactsPreferences.getPhoneticNameDisplayPreference()); 149 } 150 testRefreshSortOrder()151 public void testRefreshSortOrder() throws InterruptedException { 152 Mockito.when(mResources.getBoolean(Mockito.anyInt())).thenReturn( 153 true // R.bool.config_sort_order_user_changeable 154 ); 155 Mockito.when(mSharedPreferences.getInt(Mockito.eq(ContactsPreferences.SORT_ORDER_KEY), 156 Mockito.anyInt())).thenReturn(ContactsPreferences.SORT_ORDER_PRIMARY, 157 ContactsPreferences.SORT_ORDER_ALTERNATIVE); 158 159 Assert.assertEquals(ContactsPreferences.SORT_ORDER_PRIMARY, 160 mContactsPreferences.getSortOrder()); 161 mContactsPreferences.refreshValue(ContactsPreferences.SORT_ORDER_KEY); 162 163 Assert.assertEquals(ContactsPreferences.SORT_ORDER_ALTERNATIVE, 164 mContactsPreferences.getSortOrder()); 165 } 166 testRefreshDisplayOrder()167 public void testRefreshDisplayOrder() throws InterruptedException { 168 Mockito.when(mResources.getBoolean(Mockito.anyInt())).thenReturn( 169 true // R.bool.config_display_order_user_changeable 170 ); 171 Mockito.when(mSharedPreferences.getInt(Mockito.eq(ContactsPreferences.DISPLAY_ORDER_KEY), 172 Mockito.anyInt())).thenReturn(ContactsPreferences.DISPLAY_ORDER_PRIMARY, 173 ContactsPreferences.DISPLAY_ORDER_ALTERNATIVE); 174 175 Assert.assertEquals(ContactsPreferences.DISPLAY_ORDER_PRIMARY, 176 mContactsPreferences.getDisplayOrder()); 177 mContactsPreferences.refreshValue(ContactsPreferences.DISPLAY_ORDER_KEY); 178 179 Assert.assertEquals(ContactsPreferences.DISPLAY_ORDER_ALTERNATIVE, 180 mContactsPreferences.getDisplayOrder()); 181 } 182 testRefreshDefaultAccount()183 public void testRefreshDefaultAccount() throws InterruptedException { 184 mContactsPreferences = new ContactsPreferences(mContext, 185 /* isDefaultAccountUserChangeable */ true); 186 187 Mockito.when(mSharedPreferences.getString(Mockito.eq(ACCOUNT_KEY), Mockito.anyString())) 188 .thenReturn(new AccountWithDataSet("name1", "type1", "dataset1").stringify(), 189 new AccountWithDataSet("name2", "type2", "dataset2").stringify()); 190 191 Assert.assertEquals(new AccountWithDataSet("name1", "type1", "dataset1"), 192 mContactsPreferences.getDefaultAccount()); 193 mContactsPreferences.refreshValue(ACCOUNT_KEY); 194 195 Assert.assertEquals(new AccountWithDataSet("name2", "type2", "dataset2"), 196 mContactsPreferences.getDefaultAccount()); 197 } 198 testShouldShowAccountChangedNotificationIfAccountNotSaved()199 public void testShouldShowAccountChangedNotificationIfAccountNotSaved() { 200 mContactsPreferences = new ContactsPreferences(mContext, 201 /* isDefaultAccountUserChangeable */ true); 202 Mockito.when(mSharedPreferences.getString(Mockito.eq(ACCOUNT_KEY), Mockito.anyString())) 203 .thenReturn(null); 204 205 assertTrue("Should prompt to change default if no default is saved", 206 mContactsPreferences.shouldShowAccountChangedNotification(Arrays.asList( 207 new AccountWithDataSet("name1", "type1", "dataset1"), 208 new AccountWithDataSet("name2", "type2", "dataset2")))); 209 } 210 testShouldShowAccountChangedNotification()211 public void testShouldShowAccountChangedNotification() { 212 mContactsPreferences = new ContactsPreferences(mContext, 213 /* isDefaultAccountUserChangeable */ true); 214 Mockito.when(mSharedPreferences.getString(Mockito.eq(ACCOUNT_KEY), Mockito.anyString())) 215 .thenReturn(new AccountWithDataSet("name", "type", "dataset").stringify()); 216 217 assertFalse("Should not prompt to change default if current default exists", 218 mContactsPreferences.shouldShowAccountChangedNotification(Arrays.asList( 219 new AccountWithDataSet("name", "type", "dataset"), 220 new AccountWithDataSet("name1", "type1", "dataset1")))); 221 222 assertTrue("Should prompt to change default if current default does not exist", 223 mContactsPreferences.shouldShowAccountChangedNotification(Arrays.asList( 224 new AccountWithDataSet("name1", "type1", "dataset1"), 225 new AccountWithDataSet("name2", "type2", "dataset2")))); 226 } 227 testShouldShowAccountChangedNotificationWhenThereIsOneAccount()228 public void testShouldShowAccountChangedNotificationWhenThereIsOneAccount() { 229 mContactsPreferences = new ContactsPreferences(mContext, 230 /* isDefaultAccountUserChangeable */ true); 231 Mockito.when(mSharedPreferences.getString(Mockito.eq(ACCOUNT_KEY), Mockito.anyString())) 232 .thenReturn(null); 233 234 // Normally we would prompt because there is no default set but if there is just one 235 // account we should just use it. 236 assertFalse("Should not prompt to change default if there is only one account available", 237 mContactsPreferences.shouldShowAccountChangedNotification(Arrays.asList( 238 new AccountWithDataSet("name", "type", "dataset")))); 239 } 240 } 241