blob: 7da9099ce0a762c941e6d839d58bd0316d69a834 [file] [log] [blame]
Brandon Maxwelle8862172015-10-22 16:19:46 -07001/*
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 Mai69c182a2016-12-05 13:07:03 -080017package com.android.contacts.preference;
Brandon Maxwelle8862172015-10-22 16:19:46 -070018
19import android.content.Context;
20import android.content.SharedPreferences;
21import android.content.res.Resources;
Arthur Wang9589ef62016-09-20 19:10:42 -070022import android.support.test.InstrumentationRegistry;
Brandon Maxwell22f3d092015-11-11 17:46:46 -080023import android.test.InstrumentationTestCase;
Marcus Hagerott38212ae2016-09-13 09:18:09 -070024import android.test.suitebuilder.annotation.SmallTest;
Brandon Maxwelle8862172015-10-22 16:19:46 -070025
Gary Mai69c182a2016-12-05 13:07:03 -080026import com.android.contacts.model.account.AccountWithDataSet;
Brandon Maxwelle8862172015-10-22 16:19:46 -070027
28import junit.framework.Assert;
29
30import org.mockito.Mock;
31import org.mockito.Mockito;
32import org.mockito.MockitoAnnotations;
33
Marcus Hagerott949d4e82016-09-20 13:23:05 -070034import java.util.Arrays;
35
Marcus Hagerott38212ae2016-09-13 09:18:09 -070036@SmallTest
Brandon Maxwell22f3d092015-11-11 17:46:46 -080037public class ContactsPreferencesTest extends InstrumentationTestCase {
Brandon Maxwelle8862172015-10-22 16:19:46 -070038
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 Maxwell22f3d092015-11-11 17:46:46 -080050 System.setProperty("dexmaker.dexcache",
51 getInstrumentation().getTargetContext().getCacheDir().getPath());
Brandon Maxwelle8862172015-10-22 16:19:46 -070052 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 Wang9589ef62016-09-20 19:10:42 -070065 InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
66 @Override
67 public void run() {
68 mContactsPreferences = new ContactsPreferences(mContext);
69 }
70 });
Brandon Maxwelle8862172015-10-22 16:19:46 -070071 }
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 Hagerott949d4e82016-09-20 13:23:05 -0700144 mContactsPreferences = new ContactsPreferences(mContext,
145 /* isDefaultAccountUserChangeable */ true);
Brandon Maxwelle8862172015-10-22 16:19:46 -0700146
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 Hagerottfac695a2016-08-24 17:02:40 -0700151 Assert.assertEquals(new AccountWithDataSet("name1", "type1", "dataset1"),
152 mContactsPreferences.getDefaultAccount());
Brandon Maxwelle8862172015-10-22 16:19:46 -0700153 mContactsPreferences.refreshValue(ACCOUNT_KEY);
154
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700155 Assert.assertEquals(new AccountWithDataSet("name2", "type2", "dataset2"),
156 mContactsPreferences.getDefaultAccount());
Brandon Maxwelle8862172015-10-22 16:19:46 -0700157 }
Marcus Hagerott949d4e82016-09-20 13:23:05 -0700158
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 Maxwelle8862172015-10-22 16:19:46 -0700200}