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