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