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