Brandon Maxwell | e886217 | 2015-10-22 16:19:46 -0700 | [diff] [blame] | 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 | |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 17 | package com.android.contacts.preference; |
Brandon Maxwell | e886217 | 2015-10-22 16:19:46 -0700 | [diff] [blame] | 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.SharedPreferences; |
| 21 | import android.content.res.Resources; |
Arthur Wang | 9589ef6 | 2016-09-20 19:10:42 -0700 | [diff] [blame] | 22 | import android.support.test.InstrumentationRegistry; |
Brandon Maxwell | 22f3d09 | 2015-11-11 17:46:46 -0800 | [diff] [blame] | 23 | import android.test.InstrumentationTestCase; |
Marcus Hagerott | 38212ae | 2016-09-13 09:18:09 -0700 | [diff] [blame] | 24 | import android.test.suitebuilder.annotation.SmallTest; |
Brandon Maxwell | e886217 | 2015-10-22 16:19:46 -0700 | [diff] [blame] | 25 | |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 26 | import com.android.contacts.model.account.AccountWithDataSet; |
Brandon Maxwell | e886217 | 2015-10-22 16:19:46 -0700 | [diff] [blame] | 27 | |
| 28 | import junit.framework.Assert; |
| 29 | |
| 30 | import org.mockito.Mock; |
| 31 | import org.mockito.Mockito; |
| 32 | import org.mockito.MockitoAnnotations; |
| 33 | |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 34 | import java.util.Arrays; |
| 35 | |
Marcus Hagerott | 38212ae | 2016-09-13 09:18:09 -0700 | [diff] [blame] | 36 | @SmallTest |
Brandon Maxwell | 22f3d09 | 2015-11-11 17:46:46 -0800 | [diff] [blame] | 37 | public class ContactsPreferencesTest extends InstrumentationTestCase { |
Brandon Maxwell | e886217 | 2015-10-22 16:19:46 -0700 | [diff] [blame] | 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 |
| 48 | public void setUp() throws Exception { |
| 49 | super.setUp(); |
Brandon Maxwell | 22f3d09 | 2015-11-11 17:46:46 -0800 | [diff] [blame] | 50 | System.setProperty("dexmaker.dexcache", |
| 51 | getInstrumentation().getTargetContext().getCacheDir().getPath()); |
Brandon Maxwell | e886217 | 2015-10-22 16:19:46 -0700 | [diff] [blame] | 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 | |
Arthur Wang | 9589ef6 | 2016-09-20 19:10:42 -0700 | [diff] [blame] | 65 | InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() { |
| 66 | @Override |
| 67 | public void run() { |
| 68 | mContactsPreferences = new ContactsPreferences(mContext); |
| 69 | } |
| 70 | }); |
Brandon Maxwell | e886217 | 2015-10-22 16:19:46 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | public void testGetSortOrderDefault() { |
| 74 | Mockito.when(mResources.getBoolean(Mockito.anyInt())).thenReturn( |
| 75 | false, // R.bool.config_sort_order_user_changeable |
| 76 | true // R.bool.config_default_sort_order_primary |
| 77 | ); |
| 78 | Assert.assertEquals(ContactsPreferences.SORT_ORDER_PRIMARY, |
| 79 | mContactsPreferences.getSortOrder()); |
| 80 | } |
| 81 | |
| 82 | public void testGetSortOrder() { |
| 83 | Mockito.when(mResources.getBoolean(Mockito.anyInt())).thenReturn( |
| 84 | true // R.bool.config_sort_order_user_changeable |
| 85 | ); |
| 86 | Mockito.when(mSharedPreferences.getInt(Mockito.eq(ContactsPreferences.SORT_ORDER_KEY), |
| 87 | Mockito.anyInt())).thenReturn(ContactsPreferences.SORT_ORDER_PRIMARY); |
| 88 | Assert.assertEquals(ContactsPreferences.SORT_ORDER_PRIMARY, |
| 89 | mContactsPreferences.getSortOrder()); |
| 90 | } |
| 91 | |
| 92 | public void testGetDisplayOrderDefault() { |
| 93 | Mockito.when(mResources.getBoolean(Mockito.anyInt())).thenReturn( |
| 94 | false, // R.bool.config_display_order_user_changeable |
| 95 | true // R.bool.config_default_display_order_primary |
| 96 | ); |
| 97 | Assert.assertEquals(ContactsPreferences.DISPLAY_ORDER_PRIMARY, |
| 98 | mContactsPreferences.getDisplayOrder()); |
| 99 | } |
| 100 | |
| 101 | public void testGetDisplayOrder() { |
| 102 | Mockito.when(mResources.getBoolean(Mockito.anyInt())).thenReturn( |
| 103 | true // R.bool.config_display_order_user_changeable |
| 104 | ); |
| 105 | Mockito.when(mSharedPreferences.getInt(Mockito.eq(ContactsPreferences.DISPLAY_ORDER_KEY), |
| 106 | Mockito.anyInt())).thenReturn(ContactsPreferences.DISPLAY_ORDER_PRIMARY); |
| 107 | Assert.assertEquals(ContactsPreferences.DISPLAY_ORDER_PRIMARY, |
| 108 | mContactsPreferences.getDisplayOrder()); |
| 109 | } |
| 110 | |
| 111 | public void testRefreshSortOrder() throws InterruptedException { |
| 112 | Mockito.when(mResources.getBoolean(Mockito.anyInt())).thenReturn( |
| 113 | true // R.bool.config_sort_order_user_changeable |
| 114 | ); |
| 115 | Mockito.when(mSharedPreferences.getInt(Mockito.eq(ContactsPreferences.SORT_ORDER_KEY), |
| 116 | Mockito.anyInt())).thenReturn(ContactsPreferences.SORT_ORDER_PRIMARY, |
| 117 | ContactsPreferences.SORT_ORDER_ALTERNATIVE); |
| 118 | |
| 119 | Assert.assertEquals(ContactsPreferences.SORT_ORDER_PRIMARY, |
| 120 | mContactsPreferences.getSortOrder()); |
| 121 | mContactsPreferences.refreshValue(ContactsPreferences.SORT_ORDER_KEY); |
| 122 | |
| 123 | Assert.assertEquals(ContactsPreferences.SORT_ORDER_ALTERNATIVE, |
| 124 | mContactsPreferences.getSortOrder()); |
| 125 | } |
| 126 | |
| 127 | public void testRefreshDisplayOrder() throws InterruptedException { |
| 128 | Mockito.when(mResources.getBoolean(Mockito.anyInt())).thenReturn( |
| 129 | true // R.bool.config_display_order_user_changeable |
| 130 | ); |
| 131 | Mockito.when(mSharedPreferences.getInt(Mockito.eq(ContactsPreferences.DISPLAY_ORDER_KEY), |
| 132 | Mockito.anyInt())).thenReturn(ContactsPreferences.DISPLAY_ORDER_PRIMARY, |
| 133 | ContactsPreferences.DISPLAY_ORDER_ALTERNATIVE); |
| 134 | |
| 135 | Assert.assertEquals(ContactsPreferences.DISPLAY_ORDER_PRIMARY, |
| 136 | mContactsPreferences.getDisplayOrder()); |
| 137 | mContactsPreferences.refreshValue(ContactsPreferences.DISPLAY_ORDER_KEY); |
| 138 | |
| 139 | Assert.assertEquals(ContactsPreferences.DISPLAY_ORDER_ALTERNATIVE, |
| 140 | mContactsPreferences.getDisplayOrder()); |
| 141 | } |
| 142 | |
| 143 | public void testRefreshDefaultAccount() throws InterruptedException { |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 144 | mContactsPreferences = new ContactsPreferences(mContext, |
| 145 | /* isDefaultAccountUserChangeable */ true); |
Brandon Maxwell | e886217 | 2015-10-22 16:19:46 -0700 | [diff] [blame] | 146 | |
| 147 | Mockito.when(mSharedPreferences.getString(Mockito.eq(ACCOUNT_KEY), Mockito.anyString())) |
| 148 | .thenReturn(new AccountWithDataSet("name1", "type1", "dataset1").stringify(), |
| 149 | new AccountWithDataSet("name2", "type2", "dataset2").stringify()); |
| 150 | |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 151 | Assert.assertEquals(new AccountWithDataSet("name1", "type1", "dataset1"), |
| 152 | mContactsPreferences.getDefaultAccount()); |
Brandon Maxwell | e886217 | 2015-10-22 16:19:46 -0700 | [diff] [blame] | 153 | mContactsPreferences.refreshValue(ACCOUNT_KEY); |
| 154 | |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 155 | Assert.assertEquals(new AccountWithDataSet("name2", "type2", "dataset2"), |
| 156 | mContactsPreferences.getDefaultAccount()); |
Brandon Maxwell | e886217 | 2015-10-22 16:19:46 -0700 | [diff] [blame] | 157 | } |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 158 | |
| 159 | public void testShouldShowAccountChangedNotificationIfAccountNotSaved() { |
| 160 | mContactsPreferences = new ContactsPreferences(mContext, |
| 161 | /* isDefaultAccountUserChangeable */ true); |
| 162 | Mockito.when(mSharedPreferences.getString(Mockito.eq(ACCOUNT_KEY), Mockito.anyString())) |
| 163 | .thenReturn(null); |
| 164 | |
| 165 | assertTrue("Should prompt to change default if no default is saved", |
| 166 | mContactsPreferences.shouldShowAccountChangedNotification(Arrays.asList( |
| 167 | new AccountWithDataSet("name1", "type1", "dataset1"), |
| 168 | new AccountWithDataSet("name2", "type2", "dataset2")))); |
| 169 | } |
| 170 | |
| 171 | public void testShouldShowAccountChangedNotification() { |
| 172 | mContactsPreferences = new ContactsPreferences(mContext, |
| 173 | /* isDefaultAccountUserChangeable */ true); |
| 174 | Mockito.when(mSharedPreferences.getString(Mockito.eq(ACCOUNT_KEY), Mockito.anyString())) |
| 175 | .thenReturn(new AccountWithDataSet("name", "type", "dataset").stringify()); |
| 176 | |
| 177 | assertFalse("Should not prompt to change default if current default exists", |
| 178 | mContactsPreferences.shouldShowAccountChangedNotification(Arrays.asList( |
| 179 | new AccountWithDataSet("name", "type", "dataset"), |
| 180 | new AccountWithDataSet("name1", "type1", "dataset1")))); |
| 181 | |
| 182 | assertTrue("Should prompt to change default if current default does not exist", |
| 183 | mContactsPreferences.shouldShowAccountChangedNotification(Arrays.asList( |
| 184 | new AccountWithDataSet("name1", "type1", "dataset1"), |
| 185 | new AccountWithDataSet("name2", "type2", "dataset2")))); |
| 186 | } |
| 187 | |
| 188 | public void testShouldShowAccountChangedNotificationWhenThereIsOneAccount() { |
| 189 | mContactsPreferences = new ContactsPreferences(mContext, |
| 190 | /* isDefaultAccountUserChangeable */ true); |
| 191 | Mockito.when(mSharedPreferences.getString(Mockito.eq(ACCOUNT_KEY), Mockito.anyString())) |
| 192 | .thenReturn(null); |
| 193 | |
| 194 | // Normally we would prompt because there is no default set but if there is just one |
| 195 | // account we should just use it. |
| 196 | assertFalse("Should not prompt to change default if there is only one account available", |
| 197 | mContactsPreferences.shouldShowAccountChangedNotification(Arrays.asList( |
| 198 | new AccountWithDataSet("name", "type", "dataset")))); |
| 199 | } |
Brandon Maxwell | e886217 | 2015-10-22 16:19:46 -0700 | [diff] [blame] | 200 | } |