1 /* 2 * Copyright (C) 2017 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.backup; 18 19 import android.app.backup.BackupManager; 20 import android.app.backup.IBackupManager; 21 import android.content.ComponentName; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.content.pm.PackageManager; 25 import android.content.res.Resources; 26 import android.os.IBinder; 27 import android.os.RemoteException; 28 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 import org.mockito.Mock; 33 import org.mockito.MockitoAnnotations; 34 import org.robolectric.RuntimeEnvironment; 35 import org.robolectric.annotation.Config; 36 import org.robolectric.annotation.Implementation; 37 import org.robolectric.annotation.Implements; 38 39 import static com.google.common.truth.Truth.assertThat; 40 import static org.mockito.Mockito.anyInt; 41 import static org.mockito.Mockito.anyString; 42 import static org.mockito.Mockito.mock; 43 import static org.mockito.Mockito.spy; 44 import static org.mockito.Mockito.verify; 45 import static org.mockito.Mockito.when; 46 47 import com.android.settings.R; 48 import com.android.settings.testutils.SettingsRobolectricTestRunner; 49 import com.android.settings.TestConfig; 50 import com.android.settingslib.drawer.SettingsDrawerActivity; 51 52 @RunWith(SettingsRobolectricTestRunner.class) 53 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION, 54 shadows = {BackupSettingsHelperTest.ShadowBackupManagerStub.class}) 55 public class BackupSettingsHelperTest { 56 57 private static final String DEFAULT_SETTINGS_CLASSNAME = 58 "com.android.settings.Settings$PrivacySettingsActivity"; 59 60 private static final int DEFAULT_SUMMARY_RESOURCE = 61 R.string.backup_configure_account_default_summary; 62 63 private static final int DEFAULT_LABEL_RESOURCE = 64 R.string.privacy_settings_title; 65 66 private static final int MANUFACTURER_INTENT_RESOURCE = R.string.config_backup_settings_intent; 67 68 private static final int MANUFACTURER_LABEL_RESOURCE = R.string.config_backup_settings_label; 69 70 private Context mContext; 71 72 private BackupSettingsHelper mBackupSettingsHelper; 73 74 @Mock 75 private static IBackupManager mBackupManager; 76 77 @Before setUp()78 public void setUp() throws Exception { 79 MockitoAnnotations.initMocks(this); 80 mContext = spy(RuntimeEnvironment.application.getApplicationContext()); 81 when(mBackupManager.getCurrentTransport()).thenReturn("test_transport"); 82 mBackupSettingsHelper = new BackupSettingsHelper(mContext); 83 } 84 85 @Test testGetIntentFromBackupTransport()86 public void testGetIntentFromBackupTransport() throws Exception { 87 Intent intent = new Intent(); 88 89 when(mBackupManager.getDataManagementIntent(anyString())).thenReturn(intent); 90 91 Intent backupIntent = mBackupSettingsHelper.getIntentForBackupSettingsFromTransport(); 92 93 verify(mBackupManager).getDataManagementIntent(anyString()); 94 } 95 96 @Test testGetIntentFromBackupTransport_WithIntent()97 public void testGetIntentFromBackupTransport_WithIntent() throws Exception { 98 Intent intent = mock(Intent.class); 99 100 when(mBackupManager.getDataManagementIntent(anyString())).thenReturn(intent); 101 102 Intent backupIntent = mBackupSettingsHelper.getIntentForBackupSettingsFromTransport(); 103 104 assertThat(backupIntent).isEqualTo(intent); 105 } 106 107 @Test testGetIntentFromBackupTransport_WithNullIntent()108 public void testGetIntentFromBackupTransport_WithNullIntent() throws Exception { 109 when(mBackupManager.getDataManagementIntent(anyString())).thenReturn(null); 110 111 Intent backupIntent = mBackupSettingsHelper.getIntentForBackupSettingsFromTransport(); 112 113 assertThat(backupIntent).isNull(); 114 } 115 116 @Test testGetIntentFromBackupTransport_RemoteException()117 public void testGetIntentFromBackupTransport_RemoteException() throws Exception { 118 when(mBackupManager.getDataManagementIntent(anyString())).thenThrow(new RemoteException()); 119 120 Intent backupIntent = mBackupSettingsHelper.getIntentForBackupSettingsFromTransport(); 121 122 assertThat(backupIntent).isNull(); 123 } 124 125 @Test testGetIntentFromBackupTransport_BackupEnabled()126 public void testGetIntentFromBackupTransport_BackupEnabled() throws Exception { 127 Intent intent = new Intent("test_intent"); 128 129 when(mBackupManager.getDataManagementIntent(anyString())).thenReturn(intent); 130 when(mBackupManager.isBackupServiceActive(anyInt())).thenReturn(true); 131 132 Intent backupIntent = mBackupSettingsHelper.getIntentForBackupSettingsFromTransport(); 133 134 assertThat(backupIntent.getExtras().get(BackupManager.EXTRA_BACKUP_SERVICES_AVAILABLE)) 135 .isEqualTo(true); 136 } 137 138 @Test testGetIntentFromBackupTransport_BackupDisabled()139 public void testGetIntentFromBackupTransport_BackupDisabled() throws Exception { 140 Intent intent = new Intent("test_intent"); 141 142 when(mBackupManager.getDataManagementIntent(anyString())).thenReturn(intent); 143 when(mBackupManager.isBackupServiceActive(anyInt())).thenReturn(false); 144 145 Intent backupIntent = mBackupSettingsHelper.getIntentForBackupSettingsFromTransport(); 146 147 assertThat(backupIntent.getExtras().get(BackupManager.EXTRA_BACKUP_SERVICES_AVAILABLE)) 148 .isEqualTo(false); 149 } 150 151 @Test testGetIntentFromBackupTransport_BackupStatusException()152 public void testGetIntentFromBackupTransport_BackupStatusException() throws Exception { 153 Intent intent = new Intent("test_intent"); 154 155 when(mBackupManager.getDataManagementIntent(anyString())).thenReturn(intent); 156 when(mBackupManager.isBackupServiceActive(anyInt())).thenThrow(new RemoteException()); 157 158 Intent backupIntent = mBackupSettingsHelper.getIntentForBackupSettingsFromTransport(); 159 160 assertThat(backupIntent.getExtras().get(BackupManager.EXTRA_BACKUP_SERVICES_AVAILABLE)) 161 .isEqualTo(false); 162 } 163 164 @Test testIsIntentProvidedByTransport_WithNullIntent()165 public void testIsIntentProvidedByTransport_WithNullIntent() throws Exception { 166 when(mBackupManager.getDataManagementIntent(anyString())).thenReturn(null); 167 168 boolean isIntentProvided = mBackupSettingsHelper.isIntentProvidedByTransport(); 169 170 assertThat(isIntentProvided).isFalse(); 171 } 172 173 @Test testIsIntentProvidedByTransport_WithInvalidIntent()174 public void testIsIntentProvidedByTransport_WithInvalidIntent() throws Exception { 175 Intent intent = mock(Intent.class); 176 177 when(mBackupManager.getDataManagementIntent(anyString())).thenReturn(intent); 178 179 PackageManager packageManager = mock(PackageManager.class); 180 when(mContext.getPackageManager()).thenReturn(packageManager); 181 when(intent.resolveActivity(packageManager)).thenReturn(null); 182 183 boolean isIntentProvided = mBackupSettingsHelper.isIntentProvidedByTransport(); 184 185 assertThat(isIntentProvided).isFalse(); 186 } 187 188 @Test testIsIntentProvidedByTransport_WithIntent()189 public void testIsIntentProvidedByTransport_WithIntent() throws Exception { 190 Intent intent = mock(Intent.class); 191 192 when(mBackupManager.getDataManagementIntent(anyString())).thenReturn(intent); 193 194 PackageManager packageManager = mock(PackageManager.class); 195 when(mContext.getPackageManager()).thenReturn(packageManager); 196 when(intent.resolveActivity(packageManager)).thenReturn(mock(ComponentName.class)); 197 198 boolean isIntentProvided = mBackupSettingsHelper.isIntentProvidedByTransport(); 199 200 assertThat(isIntentProvided).isTrue(); 201 } 202 203 @Test testGetSummaryFromBackupTransport()204 public void testGetSummaryFromBackupTransport() throws Exception { 205 String summary = "test_summary"; 206 207 when(mBackupManager.getDestinationString(anyString())).thenReturn(summary); 208 209 String backupSummary = mBackupSettingsHelper.getSummaryFromBackupTransport(); 210 211 assertThat(backupSummary).isEqualTo(summary); 212 } 213 214 @Test testGetSummaryFromBackupTransport_RemoteException()215 public void testGetSummaryFromBackupTransport_RemoteException() throws Exception { 216 when(mBackupManager.getDestinationString(anyString())).thenThrow(new RemoteException()); 217 218 String backupSummary = mBackupSettingsHelper.getSummaryFromBackupTransport(); 219 220 assertThat(backupSummary).isNull(); 221 } 222 223 @Test testGetLabelBackupTransport()224 public void testGetLabelBackupTransport() throws Exception { 225 String label = "test_label"; 226 227 when(mBackupManager.getDataManagementLabel(anyString())).thenReturn(label); 228 229 String backupLabel = mBackupSettingsHelper.getLabelFromBackupTransport(); 230 231 assertThat(backupLabel).isEqualTo(label); 232 } 233 234 @Test testGetLabelBackupTransport_RemoteException()235 public void testGetLabelBackupTransport_RemoteException() throws Exception { 236 when(mBackupManager.getDataManagementLabel(anyString())).thenThrow(new RemoteException()); 237 238 String backupLabel = mBackupSettingsHelper.getLabelFromBackupTransport(); 239 240 assertThat(backupLabel).isNull(); 241 } 242 243 @Test testGetIntentForBackupSettings_WithIntentFromTransport()244 public void testGetIntentForBackupSettings_WithIntentFromTransport() throws Exception { 245 Intent intent = mock(Intent.class); 246 247 when(mBackupManager.getDataManagementIntent(anyString())).thenReturn(intent); 248 249 PackageManager packageManager = mock(PackageManager.class); 250 when(mContext.getPackageManager()).thenReturn(packageManager); 251 when(intent.resolveActivity(packageManager)).thenReturn(mock(ComponentName.class)); 252 253 Intent backupIntent = mBackupSettingsHelper.getIntentForBackupSettings(); 254 255 assertThat(backupIntent).isEqualTo(intent); 256 } 257 258 @Test testGetIntentForBackupSettings_WithoutIntentFromTransport()259 public void testGetIntentForBackupSettings_WithoutIntentFromTransport() throws Exception { 260 when(mBackupManager.getDataManagementIntent(anyString())).thenReturn(null); 261 262 Intent backupIntent = mBackupSettingsHelper.getIntentForBackupSettings(); 263 264 assertThat(backupIntent.getComponent().getClassName()).isEqualTo( 265 DEFAULT_SETTINGS_CLASSNAME); 266 assertThat(backupIntent.getExtras().getBoolean( 267 SettingsDrawerActivity.EXTRA_SHOW_MENU)).isTrue(); 268 } 269 270 @Test testGetLabelForBackupSettings_WithLabelFromTransport()271 public void testGetLabelForBackupSettings_WithLabelFromTransport() throws Exception { 272 String label = "test_label"; 273 274 when(mBackupManager.getDataManagementLabel(anyString())).thenReturn(label); 275 276 String backupLabel = mBackupSettingsHelper.getLabelForBackupSettings(); 277 278 assertThat(backupLabel).isEqualTo(label); 279 } 280 281 @Test testGetLabelForBackupSettings_WithEmptyLabelFromTransport()282 public void testGetLabelForBackupSettings_WithEmptyLabelFromTransport() throws Exception { 283 String label = ""; 284 285 when(mBackupManager.getDataManagementLabel(anyString())).thenReturn(label); 286 287 String backupLabel = mBackupSettingsHelper.getLabelForBackupSettings(); 288 289 assertThat(backupLabel).isEqualTo(mContext.getString(DEFAULT_LABEL_RESOURCE)); 290 } 291 292 @Test testGetLabelForBackupSettings_WithoutLabelFromTransport()293 public void testGetLabelForBackupSettings_WithoutLabelFromTransport() throws Exception { 294 when(mBackupManager.getDataManagementLabel(anyString())).thenReturn(null); 295 296 String backupLabel = mBackupSettingsHelper.getLabelForBackupSettings(); 297 298 assertThat(backupLabel).isEqualTo(mContext.getString(DEFAULT_LABEL_RESOURCE)); 299 } 300 301 @Test testGetSummaryForBackupSettings_WithSummaryFromTransport()302 public void testGetSummaryForBackupSettings_WithSummaryFromTransport() throws Exception { 303 String summary = "test_summary"; 304 305 when(mBackupManager.getDestinationString(anyString())).thenReturn(summary); 306 307 String backupSummary = mBackupSettingsHelper.getSummaryForBackupSettings(); 308 309 assertThat(backupSummary).isEqualTo(summary); 310 } 311 312 @Test testGetSummaryForBackupSettings_WithoutSummaryFromTransport()313 public void testGetSummaryForBackupSettings_WithoutSummaryFromTransport() throws Exception { 314 when(mBackupManager.getDestinationString(anyString())).thenReturn(null); 315 316 String backupSummary = mBackupSettingsHelper.getSummaryForBackupSettings(); 317 318 assertThat(backupSummary).isEqualTo(mContext.getString(DEFAULT_SUMMARY_RESOURCE)); 319 } 320 321 @Test testIsBackupProvidedByManufacturer_WithIntent()322 public void testIsBackupProvidedByManufacturer_WithIntent() throws Exception { 323 String intent = "test_intent"; 324 325 when(mContext.getApplicationContext()).thenReturn(mContext); 326 Resources spiedResources = spy(mContext.getResources()); 327 when(mContext.getResources()).thenReturn(spiedResources); 328 when(spiedResources.getString(MANUFACTURER_INTENT_RESOURCE)).thenReturn(intent); 329 mBackupSettingsHelper = new BackupSettingsHelper(mContext); 330 331 boolean hasManufacturerIntent = mBackupSettingsHelper.isBackupProvidedByManufacturer(); 332 333 assertThat(hasManufacturerIntent).isTrue(); 334 } 335 336 @Test testIsBackupProvidedByManufacturer_WithoutIntent()337 public void testIsBackupProvidedByManufacturer_WithoutIntent() throws Exception { 338 String intent = ""; 339 340 when(mContext.getApplicationContext()).thenReturn(mContext); 341 Resources spiedResources = spy(mContext.getResources()); 342 when(mContext.getResources()).thenReturn(spiedResources); 343 when(spiedResources.getString(MANUFACTURER_INTENT_RESOURCE)).thenReturn(intent); 344 mBackupSettingsHelper = new BackupSettingsHelper(mContext); 345 346 boolean hasManufacturerIntent = mBackupSettingsHelper.isBackupProvidedByManufacturer(); 347 348 assertThat(hasManufacturerIntent).isFalse(); 349 } 350 351 @Test testGetLabelProvidedByManufacturer()352 public void testGetLabelProvidedByManufacturer() throws Exception { 353 String label = "test_label"; 354 355 when(mContext.getApplicationContext()).thenReturn(mContext); 356 Resources spiedResources = spy(mContext.getResources()); 357 when(mContext.getResources()).thenReturn(spiedResources); 358 when(spiedResources.getString(MANUFACTURER_LABEL_RESOURCE)).thenReturn(label); 359 mBackupSettingsHelper = new BackupSettingsHelper(mContext); 360 361 String manufacturerLabel = mBackupSettingsHelper.getLabelProvidedByManufacturer(); 362 363 assertThat(manufacturerLabel).isEqualTo(label); 364 } 365 366 @Test testGetIntentProvidedByManufacturer()367 public void testGetIntentProvidedByManufacturer() throws Exception { 368 String intent = "test_intent"; 369 370 when(mContext.getApplicationContext()).thenReturn(mContext); 371 Resources spiedResources = spy(mContext.getResources()); 372 when(mContext.getResources()).thenReturn(spiedResources); 373 when(spiedResources.getString(MANUFACTURER_INTENT_RESOURCE)).thenReturn(intent); 374 mBackupSettingsHelper = new BackupSettingsHelper(mContext); 375 376 Intent manufacturerIntent = mBackupSettingsHelper.getIntentProvidedByManufacturer(); 377 378 assertThat(manufacturerIntent).isNotNull(); 379 } 380 381 @Implements(IBackupManager.Stub.class) 382 public static class ShadowBackupManagerStub { 383 @Implementation asInterface(IBinder iBinder)384 public static IBackupManager asInterface(IBinder iBinder) { 385 return mBackupManager; 386 } 387 } 388 } 389