1 /*
2  * Copyright (C) 2018 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.settings.accounts;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.anyString;
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.spy;
25 
26 import android.accounts.AuthenticatorDescription;
27 import android.app.Activity;
28 import android.app.admin.DevicePolicyManager;
29 import android.content.Context;
30 import android.content.SyncAdapterType;
31 import android.graphics.drawable.ColorDrawable;
32 import android.os.UserHandle;
33 
34 import androidx.fragment.app.FragmentActivity;
35 import androidx.preference.Preference;
36 import androidx.preference.PreferenceManager;
37 import androidx.preference.PreferenceScreen;
38 
39 import com.android.settings.R;
40 import com.android.settings.core.BasePreferenceController;
41 import com.android.settings.testutils.shadow.ShadowAccountManager;
42 import com.android.settings.testutils.shadow.ShadowContentResolver;
43 import com.android.settings.testutils.shadow.ShadowRestrictedLockUtilsInternal;
44 
45 import org.junit.After;
46 import org.junit.Before;
47 import org.junit.Test;
48 import org.junit.runner.RunWith;
49 import org.mockito.MockitoAnnotations;
50 import org.robolectric.Robolectric;
51 import org.robolectric.RobolectricTestRunner;
52 import org.robolectric.RuntimeEnvironment;
53 import org.robolectric.annotation.Config;
54 
55 @RunWith(RobolectricTestRunner.class)
56 @Config(shadows = {ShadowAccountManager.class, ShadowContentResolver.class,
57         ShadowRestrictedLockUtilsInternal.class})
58 public class ChooseAccountPreferenceControllerTest {
59 
60     private Context mContext;
61     private ChooseAccountPreferenceController mController;
62     private Activity mActivity;
63     private PreferenceManager mPreferenceManager;
64     private PreferenceScreen mPreferenceScreen;
65 
66     @Before
setUp()67     public void setUp() {
68         MockitoAnnotations.initMocks(this);
69         mContext = RuntimeEnvironment.application;
70         mController = spy(new ChooseAccountPreferenceController(mContext, "controller_key"));
71         mActivity = Robolectric.setupActivity(FragmentActivity.class);
72         mPreferenceManager = new PreferenceManager(mContext);
73         mPreferenceScreen = mPreferenceManager.createPreferenceScreen(mContext);
74     }
75 
76     @After
tearDown()77     public void tearDown() {
78         ShadowContentResolver.reset();
79         ShadowAccountManager.reset();
80         ShadowRestrictedLockUtilsInternal.clearDisabledTypes();
81     }
82 
83     @Test
getAvailabilityStatus_byDefault_shouldBeShown()84     public void getAvailabilityStatus_byDefault_shouldBeShown() {
85         assertThat(mController.getAvailabilityStatus()).isEqualTo(
86                 BasePreferenceController.AVAILABLE);
87     }
88 
89     @Test
handlePreferenceTreeClick_notProviderPreference_shouldReturnFalse()90     public void handlePreferenceTreeClick_notProviderPreference_shouldReturnFalse() {
91         final Preference preference = new Preference(mContext);
92         assertThat(mController.handlePreferenceTreeClick(preference)).isFalse();
93     }
94 
95     @Test
handlePreferenceTreeClick_isProviderPreference_shouldFinishFragment()96     public void handlePreferenceTreeClick_isProviderPreference_shouldFinishFragment() {
97         final ProviderPreference providerPreference = new ProviderPreference(mContext,
98                 "account_type", null /* icon */, "provider_name");
99 
100         mController.initialize(null, null, null, mActivity);
101         mController.handlePreferenceTreeClick(providerPreference);
102 
103         assertThat(mActivity.isFinishing()).isTrue();
104     }
105 
106     @Test
updateAuthDescriptions_oneProvider_shouldNotAddPreference()107     public void updateAuthDescriptions_oneProvider_shouldNotAddPreference() {
108         final AuthenticatorDescription authDesc = new AuthenticatorDescription("com.acct1",
109                 "com.android.settings",
110                 R.string.header_add_an_account, 0, 0, 0, false);
111         ShadowAccountManager.addAuthenticator(authDesc);
112 
113         final SyncAdapterType[] syncAdapters = {new SyncAdapterType("authority" /* authority */,
114                 "com.acct1" /* accountType */, false /* userVisible */,
115                 true /* supportsUploading */)};
116         ShadowContentResolver.setSyncAdapterTypes(syncAdapters);
117 
118         ShadowRestrictedLockUtilsInternal.setHasSystemFeature(true);
119         ShadowRestrictedLockUtilsInternal.setDevicePolicyManager(mock(DevicePolicyManager.class));
120         ShadowRestrictedLockUtilsInternal.setDisabledTypes(new String[] {"test_type"});
121 
122         doReturn("label").when(mController).getLabelForType(anyString());
123 
124         mController.initialize(null, null, new UserHandle(3), mActivity);
125         mController.displayPreference(mPreferenceScreen);
126 
127         assertThat(mActivity.isFinishing()).isTrue();
128         assertThat(mPreferenceScreen.getPreferenceCount()).isEqualTo(0);
129     }
130 
131     @Test
updateAuthDescriptions_oneAdminDisabledProvider_shouldNotAddPreference()132     public void updateAuthDescriptions_oneAdminDisabledProvider_shouldNotAddPreference() {
133         final AuthenticatorDescription authDesc = new AuthenticatorDescription("com.acct1",
134                 "com.android.settings",
135                 R.string.header_add_an_account, 0, 0, 0, false);
136         ShadowAccountManager.addAuthenticator(authDesc);
137 
138         final SyncAdapterType[] syncAdapters = {new SyncAdapterType("authority" /* authority */,
139                 "com.acct1" /* accountType */, false /* userVisible */,
140                 true /* supportsUploading */)};
141         ShadowContentResolver.setSyncAdapterTypes(syncAdapters);
142 
143         ShadowRestrictedLockUtilsInternal.setHasSystemFeature(true);
144         ShadowRestrictedLockUtilsInternal.setDevicePolicyManager(mock(DevicePolicyManager.class));
145         ShadowRestrictedLockUtilsInternal.setDisabledTypes(new String[] {"com.acct1"});
146 
147         doReturn("label").when(mController).getLabelForType(anyString());
148 
149         mController.initialize(null, null, new UserHandle(3), mActivity);
150         mController.displayPreference(mPreferenceScreen);
151 
152         assertThat(mActivity.isFinishing()).isTrue();
153         assertThat(mPreferenceScreen.getPreferenceCount()).isEqualTo(0);
154     }
155 
156     @Test
updateAuthDescriptions_noProvider_shouldNotAddPreference()157     public void updateAuthDescriptions_noProvider_shouldNotAddPreference() {
158         final AuthenticatorDescription authDesc = new AuthenticatorDescription("com.acct1",
159                 "com.android.settings",
160                 R.string.header_add_an_account, 0, 0, 0, false);
161         ShadowAccountManager.addAuthenticator(authDesc);
162 
163         final SyncAdapterType[] syncAdapters = {new SyncAdapterType("authority" /* authority */,
164                 "com.acct1" /* accountType */, false /* userVisible */,
165                 true /* supportsUploading */)};
166         ShadowContentResolver.setSyncAdapterTypes(syncAdapters);
167 
168         doReturn("label").when(mController).getLabelForType(anyString());
169 
170         mController.initialize(new String[] {"test_authoritiy1"}, null, new UserHandle(3),
171                 mActivity);
172         mController.displayPreference(mPreferenceScreen);
173 
174         assertThat(mActivity.isFinishing()).isTrue();
175         assertThat(mPreferenceScreen.getPreferenceCount()).isEqualTo(0);
176     }
177 
178     @Test
179     public void
updateAuthDescriptions_twoProvider_shouldAddTwoPreference()180     updateAuthDescriptions_twoProvider_shouldAddTwoPreference() {
181         final AuthenticatorDescription authDesc = new AuthenticatorDescription("com.acct1",
182                 "com.android.settings",
183                 R.string.header_add_an_account, 0, 0, 0, false);
184         final AuthenticatorDescription authDesc2 = new AuthenticatorDescription("com.acct2",
185                 "com.android.settings",
186                 R.string.header_add_an_account, 0, 0, 0, false);
187         ShadowAccountManager.addAuthenticator(authDesc);
188         ShadowAccountManager.addAuthenticator(authDesc2);
189 
190         final SyncAdapterType[] syncAdapters = {new SyncAdapterType("authority" /* authority */,
191                 "com.acct1" /* accountType */, false /* userVisible */,
192                 true /* supportsUploading */), new SyncAdapterType("authority2" /* authority */,
193                 "com.acct2" /* accountType */, false /* userVisible */,
194                 true /* supportsUploading */)};
195         ShadowContentResolver.setSyncAdapterTypes(syncAdapters);
196 
197         doReturn("label").when(mController).getLabelForType(anyString());
198         doReturn("label2").when(mController).getLabelForType(anyString());
199         doReturn(new ColorDrawable()).when(mController).getDrawableForType(anyString());
200         doReturn(new ColorDrawable()).when(mController).getDrawableForType(anyString());
201 
202         mController.initialize(null, null, new UserHandle(3), mActivity);
203         mController.displayPreference(mPreferenceScreen);
204 
205         assertThat(mPreferenceScreen.getPreferenceCount()).isEqualTo(2);
206     }
207 }
208