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