1 /* 2 * Copyright (C) 2016 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 package com.android.settings.accounts; 17 18 import static com.google.common.truth.Truth.assertThat; 19 20 import static org.mockito.Answers.RETURNS_DEEP_STUBS; 21 import static org.mockito.ArgumentMatchers.anyInt; 22 import static org.mockito.Mockito.when; 23 24 import android.accounts.Account; 25 import android.accounts.AccountManager; 26 import android.accounts.AuthenticatorDescription; 27 import android.app.Activity; 28 import android.content.Context; 29 import android.content.Intent; 30 import android.content.SyncAdapterType; 31 import android.os.UserHandle; 32 33 import androidx.preference.Preference; 34 35 import com.android.settings.R; 36 import com.android.settings.SettingsActivity; 37 import com.android.settings.testutils.shadow.ShadowAccountManager; 38 import com.android.settings.testutils.shadow.ShadowContentResolver; 39 40 import org.junit.After; 41 import org.junit.Before; 42 import org.junit.Test; 43 import org.junit.runner.RunWith; 44 import org.mockito.Mock; 45 import org.mockito.MockitoAnnotations; 46 import org.robolectric.Robolectric; 47 import org.robolectric.RobolectricTestRunner; 48 import org.robolectric.annotation.Config; 49 import org.robolectric.shadows.ShadowApplication; 50 51 @RunWith(RobolectricTestRunner.class) 52 @Config(shadows = {ShadowAccountManager.class, ShadowContentResolver.class}) 53 public class AccountSyncPreferenceControllerTest { 54 55 @Mock(answer = RETURNS_DEEP_STUBS) 56 private AccountManager mAccountManager; 57 58 private Activity mActivity; 59 private AccountSyncPreferenceController mController; 60 private Preference mPreference; 61 62 @Before setUp()63 public void setUp() { 64 MockitoAnnotations.initMocks(this); 65 mActivity = Robolectric.setupActivity(Activity.class); 66 ShadowApplication.getInstance().setSystemService(Context.ACCOUNT_SERVICE, mAccountManager); 67 68 when(mAccountManager.getAuthenticatorTypesAsUser(anyInt())).thenReturn( 69 new AuthenticatorDescription[0]); 70 when(mAccountManager.getAccountsAsUser(anyInt())).thenReturn(new Account[0]); 71 72 mPreference = new Preference(mActivity); 73 mPreference.setKey("account_sync"); 74 75 mController = new AccountSyncPreferenceController(mActivity); 76 mController.init(new Account("acct1", "type1"), new UserHandle(3)); 77 } 78 79 @After tearDown()80 public void tearDown() { 81 ShadowContentResolver.reset(); 82 } 83 84 @Test handlePreferenceTreeClick_shouldStartFragment()85 public void handlePreferenceTreeClick_shouldStartFragment() { 86 mController.handlePreferenceTreeClick(mPreference); 87 88 final Intent nextActivity = ShadowApplication.getInstance().getNextStartedActivity(); 89 90 assertThat(nextActivity.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT)) 91 .isEqualTo(AccountSyncSettings.class.getName()); 92 assertThat(nextActivity.getIntExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_TITLE_RESID, 0)) 93 .isEqualTo(R.string.account_sync_title); 94 } 95 96 @Test updateSummary_adapterInvisible_shouldNotCount()97 public void updateSummary_adapterInvisible_shouldNotCount() { 98 SyncAdapterType syncAdapterType = new SyncAdapterType("authority" /* authority */, 99 "type1" /* accountType */, false /* userVisible */, true /* supportsUploading */); 100 SyncAdapterType[] syncAdapters = {syncAdapterType}; 101 ShadowContentResolver.setSyncAdapterTypes(syncAdapters); 102 103 mController.updateSummary(mPreference); 104 105 assertThat(mPreference.getSummary()) 106 .isEqualTo(mActivity.getString(R.string.account_sync_summary_all_off)); 107 } 108 109 @Test updateSummary_notSameAccountType_shouldNotCount()110 public void updateSummary_notSameAccountType_shouldNotCount() { 111 SyncAdapterType syncAdapterType = new SyncAdapterType("authority" /* authority */, 112 "type5" /* accountType */, true /* userVisible */, true /* supportsUploading */); 113 SyncAdapterType[] syncAdapters = {syncAdapterType}; 114 ShadowContentResolver.setSyncAdapterTypes(syncAdapters); 115 116 mController.updateSummary(mPreference); 117 118 assertThat(mPreference.getSummary()) 119 .isEqualTo(mActivity.getString(R.string.account_sync_summary_all_off)); 120 } 121 122 @Test updateSummary_notSyncable_shouldNotCount()123 public void updateSummary_notSyncable_shouldNotCount() { 124 SyncAdapterType syncAdapterType = new SyncAdapterType("authority" /* authority */, 125 "type1" /* accountType */, true /* userVisible */, true /* supportsUploading */); 126 SyncAdapterType[] syncAdapters = {syncAdapterType}; 127 ShadowContentResolver.setSyncAdapterTypes(syncAdapters); 128 ShadowContentResolver.setSyncable("authority", 0); 129 130 mController.updateSummary(mPreference); 131 132 assertThat(mPreference.getSummary()) 133 .isEqualTo(mActivity.getString(R.string.account_sync_summary_all_off)); 134 } 135 136 @Test updateSummary_syncDisabled_shouldNotCount()137 public void updateSummary_syncDisabled_shouldNotCount() { 138 SyncAdapterType syncAdapterType = new SyncAdapterType("authority" /* authority */, 139 "type1" /* accountType */, true /* userVisible */, true /* supportsUploading */); 140 SyncAdapterType[] syncAdapters = {syncAdapterType}; 141 ShadowContentResolver.setSyncAdapterTypes(syncAdapters); 142 ShadowContentResolver.setSyncAutomatically("authority", false); 143 ShadowContentResolver.setMasterSyncAutomatically(3, true); 144 145 mController.updateSummary(mPreference); 146 147 assertThat(mPreference.getSummary()) 148 .isEqualTo(mActivity.getString(R.string.account_sync_summary_all_off)); 149 } 150 151 @Test updateSummary_syncEnabled_shouldCount()152 public void updateSummary_syncEnabled_shouldCount() { 153 SyncAdapterType syncAdapterType = new SyncAdapterType("authority" /* authority */, 154 "type1" /* accountType */, true /* userVisible */, true /* supportsUploading */); 155 SyncAdapterType[] syncAdapters = {syncAdapterType}; 156 ShadowContentResolver.setSyncAdapterTypes(syncAdapters); 157 158 mController.updateSummary(mPreference); 159 160 assertThat(mPreference.getSummary()) 161 .isEqualTo(mActivity.getString(R.string.account_sync_summary_all_on)); 162 } 163 164 @Test updateSummary_multipleSyncAdapters_shouldSetSummary()165 public void updateSummary_multipleSyncAdapters_shouldSetSummary() { 166 SyncAdapterType syncAdapterType1 = new SyncAdapterType("authority1" /* authority */, 167 "type1" /* accountType */, true /* userVisible */, true /* supportsUploading */); 168 SyncAdapterType syncAdapterType2 = new SyncAdapterType("authority2" /* authority */, 169 "type1" /* accountType */, true /* userVisible */, true /* supportsUploading */); 170 SyncAdapterType syncAdapterType3 = new SyncAdapterType("authority3" /* authority */, 171 "type1" /* accountType */, true /* userVisible */, true /* supportsUploading */); 172 SyncAdapterType syncAdapterType4 = new SyncAdapterType("authority4" /* authority */, 173 "type1" /* accountType */, true /* userVisible */, true /* supportsUploading */); 174 SyncAdapterType[] syncAdapters = 175 {syncAdapterType1, syncAdapterType2, syncAdapterType3, syncAdapterType4}; 176 ShadowContentResolver.setSyncAdapterTypes(syncAdapters); 177 178 ShadowContentResolver.setSyncAutomatically("authority4", false); 179 180 mController.updateSummary(mPreference); 181 182 assertThat(mPreference.getSummary()) 183 .isEqualTo(mActivity.getString(R.string.account_sync_summary_some_on, 3, 4)); 184 } 185 } 186