Add a screen to show OEM backup settings.

Moves the backup settings logic to BackupSettingsHelper and
BackupSettingsActivity:
- If the manufacturer provides the intent and the label for their backup
settings in the config.xml, a new intermediate fragment is shown for
Backup settings to let the user pick either standard backup settings or
OEM provided backup settings.
- If config.xml doesn't contain the intent, BackupSettingsActivity is
used as a trampoline activity to launch backup settings provided by the
backup transport of the default backup settings activity, i.e.
PrivacySettingsActivity.

Bug: 34700410
Bug: 33655074
Bug: 33654991
Test: make RunSettingsRoboTests
Change-Id: I78e340fbf926b2a9dc2c4e3942f9337c3c7a933c
This commit is contained in:
Anton Philippov
2017-01-25 20:37:36 +00:00
parent 0c341d7357
commit adfec5563e
12 changed files with 1017 additions and 115 deletions

View File

@@ -0,0 +1,388 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package com.android.settings.backup;
import android.app.backup.BackupManager;
import android.app.backup.IBackupManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.IBinder;
import android.os.RemoteException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import com.android.settings.R;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settingslib.drawer.SettingsDrawerActivity;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION,
shadows = {BackupSettingsHelperTest.ShadowBackupManagerStub.class})
public class BackupSettingsHelperTest {
private static final String DEFAULT_SETTINGS_CLASSNAME =
"com.android.settings.Settings$PrivacySettingsActivity";
private static final int DEFAULT_SUMMARY_RESOURCE =
R.string.backup_configure_account_default_summary;
private static final int DEFAULT_LABEL_RESOURCE =
R.string.privacy_settings_title;
private static final int MANUFACTURER_INTENT_RESOURCE = R.string.config_backup_settings_intent;
private static final int MANUFACTURER_LABEL_RESOURCE = R.string.config_backup_settings_label;
private Context mContext;
private BackupSettingsHelper mBackupSettingsHelper;
@Mock
private static IBackupManager mBackupManager;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application.getApplicationContext());
when(mBackupManager.getCurrentTransport()).thenReturn("test_transport");
mBackupSettingsHelper = new BackupSettingsHelper(mContext);
}
@Test
public void testGetIntentFromBackupTransport() throws Exception {
Intent intent = new Intent();
when(mBackupManager.getDataManagementIntent(anyString())).thenReturn(intent);
Intent backupIntent = mBackupSettingsHelper.getIntentForBackupSettingsFromTransport();
verify(mBackupManager).getDataManagementIntent(anyString());
}
@Test
public void testGetIntentFromBackupTransport_WithIntent() throws Exception {
Intent intent = mock(Intent.class);
when(mBackupManager.getDataManagementIntent(anyString())).thenReturn(intent);
Intent backupIntent = mBackupSettingsHelper.getIntentForBackupSettingsFromTransport();
assertThat(backupIntent).isEqualTo(intent);
}
@Test
public void testGetIntentFromBackupTransport_WithNullIntent() throws Exception {
when(mBackupManager.getDataManagementIntent(anyString())).thenReturn(null);
Intent backupIntent = mBackupSettingsHelper.getIntentForBackupSettingsFromTransport();
assertThat(backupIntent).isNull();
}
@Test
public void testGetIntentFromBackupTransport_RemoteException() throws Exception {
when(mBackupManager.getDataManagementIntent(anyString())).thenThrow(new RemoteException());
Intent backupIntent = mBackupSettingsHelper.getIntentForBackupSettingsFromTransport();
assertThat(backupIntent).isNull();
}
@Test
public void testGetIntentFromBackupTransport_BackupEnabled() throws Exception {
Intent intent = new Intent("test_intent");
when(mBackupManager.getDataManagementIntent(anyString())).thenReturn(intent);
when(mBackupManager.isBackupServiceActive(anyInt())).thenReturn(true);
Intent backupIntent = mBackupSettingsHelper.getIntentForBackupSettingsFromTransport();
assertThat(backupIntent.getExtras().get(BackupManager.EXTRA_BACKUP_SERVICES_AVAILABLE))
.isEqualTo(true);
}
@Test
public void testGetIntentFromBackupTransport_BackupDisabled() throws Exception {
Intent intent = new Intent("test_intent");
when(mBackupManager.getDataManagementIntent(anyString())).thenReturn(intent);
when(mBackupManager.isBackupServiceActive(anyInt())).thenReturn(false);
Intent backupIntent = mBackupSettingsHelper.getIntentForBackupSettingsFromTransport();
assertThat(backupIntent.getExtras().get(BackupManager.EXTRA_BACKUP_SERVICES_AVAILABLE))
.isEqualTo(false);
}
@Test
public void testGetIntentFromBackupTransport_BackupStatusException() throws Exception {
Intent intent = new Intent("test_intent");
when(mBackupManager.getDataManagementIntent(anyString())).thenReturn(intent);
when(mBackupManager.isBackupServiceActive(anyInt())).thenThrow(new RemoteException());
Intent backupIntent = mBackupSettingsHelper.getIntentForBackupSettingsFromTransport();
assertThat(backupIntent.getExtras().get(BackupManager.EXTRA_BACKUP_SERVICES_AVAILABLE))
.isEqualTo(false);
}
@Test
public void testIsIntentProvidedByTransport_WithNullIntent() throws Exception {
when(mBackupManager.getDataManagementIntent(anyString())).thenReturn(null);
boolean isIntentProvided = mBackupSettingsHelper.isIntentProvidedByTransport();
assertThat(isIntentProvided).isFalse();
}
@Test
public void testIsIntentProvidedByTransport_WithInvalidIntent() throws Exception {
Intent intent = mock(Intent.class);
when(mBackupManager.getDataManagementIntent(anyString())).thenReturn(intent);
PackageManager packageManager = mock(PackageManager.class);
when(mContext.getPackageManager()).thenReturn(packageManager);
when(intent.resolveActivity(packageManager)).thenReturn(null);
boolean isIntentProvided = mBackupSettingsHelper.isIntentProvidedByTransport();
assertThat(isIntentProvided).isFalse();
}
@Test
public void testIsIntentProvidedByTransport_WithIntent() throws Exception {
Intent intent = mock(Intent.class);
when(mBackupManager.getDataManagementIntent(anyString())).thenReturn(intent);
PackageManager packageManager = mock(PackageManager.class);
when(mContext.getPackageManager()).thenReturn(packageManager);
when(intent.resolveActivity(packageManager)).thenReturn(mock(ComponentName.class));
boolean isIntentProvided = mBackupSettingsHelper.isIntentProvidedByTransport();
assertThat(isIntentProvided).isTrue();
}
@Test
public void testGetSummaryFromBackupTransport() throws Exception {
String summary = "test_summary";
when(mBackupManager.getDestinationString(anyString())).thenReturn(summary);
String backupSummary = mBackupSettingsHelper.getSummaryFromBackupTransport();
assertThat(backupSummary).isEqualTo(summary);
}
@Test
public void testGetSummaryFromBackupTransport_RemoteException() throws Exception {
when(mBackupManager.getDestinationString(anyString())).thenThrow(new RemoteException());
String backupSummary = mBackupSettingsHelper.getSummaryFromBackupTransport();
assertThat(backupSummary).isNull();
}
@Test
public void testGetLabelBackupTransport() throws Exception {
String label = "test_label";
when(mBackupManager.getDataManagementLabel(anyString())).thenReturn(label);
String backupLabel = mBackupSettingsHelper.getLabelFromBackupTransport();
assertThat(backupLabel).isEqualTo(label);
}
@Test
public void testGetLabelBackupTransport_RemoteException() throws Exception {
when(mBackupManager.getDataManagementLabel(anyString())).thenThrow(new RemoteException());
String backupLabel = mBackupSettingsHelper.getLabelFromBackupTransport();
assertThat(backupLabel).isNull();
}
@Test
public void testGetIntentForBackupSettings_WithIntentFromTransport() throws Exception {
Intent intent = mock(Intent.class);
when(mBackupManager.getDataManagementIntent(anyString())).thenReturn(intent);
PackageManager packageManager = mock(PackageManager.class);
when(mContext.getPackageManager()).thenReturn(packageManager);
when(intent.resolveActivity(packageManager)).thenReturn(mock(ComponentName.class));
Intent backupIntent = mBackupSettingsHelper.getIntentForBackupSettings();
assertThat(backupIntent).isEqualTo(intent);
}
@Test
public void testGetIntentForBackupSettings_WithoutIntentFromTransport() throws Exception {
when(mBackupManager.getDataManagementIntent(anyString())).thenReturn(null);
Intent backupIntent = mBackupSettingsHelper.getIntentForBackupSettings();
assertThat(backupIntent.getComponent().getClassName()).isEqualTo(
DEFAULT_SETTINGS_CLASSNAME);
assertThat(backupIntent.getExtras().getBoolean(
SettingsDrawerActivity.EXTRA_SHOW_MENU)).isTrue();
}
@Test
public void testGetLabelForBackupSettings_WithLabelFromTransport() throws Exception {
String label = "test_label";
when(mBackupManager.getDataManagementLabel(anyString())).thenReturn(label);
String backupLabel = mBackupSettingsHelper.getLabelForBackupSettings();
assertThat(backupLabel).isEqualTo(label);
}
@Test
public void testGetLabelForBackupSettings_WithEmptyLabelFromTransport() throws Exception {
String label = "";
when(mBackupManager.getDataManagementLabel(anyString())).thenReturn(label);
String backupLabel = mBackupSettingsHelper.getLabelForBackupSettings();
assertThat(backupLabel).isEqualTo(mContext.getString(DEFAULT_LABEL_RESOURCE));
}
@Test
public void testGetLabelForBackupSettings_WithoutLabelFromTransport() throws Exception {
when(mBackupManager.getDataManagementLabel(anyString())).thenReturn(null);
String backupLabel = mBackupSettingsHelper.getLabelForBackupSettings();
assertThat(backupLabel).isEqualTo(mContext.getString(DEFAULT_LABEL_RESOURCE));
}
@Test
public void testGetSummaryForBackupSettings_WithSummaryFromTransport() throws Exception {
String summary = "test_summary";
when(mBackupManager.getDestinationString(anyString())).thenReturn(summary);
String backupSummary = mBackupSettingsHelper.getSummaryForBackupSettings();
assertThat(backupSummary).isEqualTo(summary);
}
@Test
public void testGetSummaryForBackupSettings_WithoutSummaryFromTransport() throws Exception {
when(mBackupManager.getDestinationString(anyString())).thenReturn(null);
String backupSummary = mBackupSettingsHelper.getSummaryForBackupSettings();
assertThat(backupSummary).isEqualTo(mContext.getString(DEFAULT_SUMMARY_RESOURCE));
}
@Test
public void testIsBackupProvidedByManufacturer_WithIntent() throws Exception {
String intent = "test_intent";
when(mContext.getApplicationContext()).thenReturn(mContext);
Resources spiedResources = spy(mContext.getResources());
when(mContext.getResources()).thenReturn(spiedResources);
when(spiedResources.getString(MANUFACTURER_INTENT_RESOURCE)).thenReturn(intent);
mBackupSettingsHelper = new BackupSettingsHelper(mContext);
boolean hasManufacturerIntent = mBackupSettingsHelper.isBackupProvidedByManufacturer();
assertThat(hasManufacturerIntent).isTrue();
}
@Test
public void testIsBackupProvidedByManufacturer_WithoutIntent() throws Exception {
String intent = "";
when(mContext.getApplicationContext()).thenReturn(mContext);
Resources spiedResources = spy(mContext.getResources());
when(mContext.getResources()).thenReturn(spiedResources);
when(spiedResources.getString(MANUFACTURER_INTENT_RESOURCE)).thenReturn(intent);
mBackupSettingsHelper = new BackupSettingsHelper(mContext);
boolean hasManufacturerIntent = mBackupSettingsHelper.isBackupProvidedByManufacturer();
assertThat(hasManufacturerIntent).isFalse();
}
@Test
public void testGetLabelProvidedByManufacturer() throws Exception {
String label = "test_label";
when(mContext.getApplicationContext()).thenReturn(mContext);
Resources spiedResources = spy(mContext.getResources());
when(mContext.getResources()).thenReturn(spiedResources);
when(spiedResources.getString(MANUFACTURER_LABEL_RESOURCE)).thenReturn(label);
mBackupSettingsHelper = new BackupSettingsHelper(mContext);
String manufacturerLabel = mBackupSettingsHelper.getLabelProvidedByManufacturer();
assertThat(manufacturerLabel).isEqualTo(label);
}
@Test
public void testGetIntentProvidedByManufacturer() throws Exception {
String intent = "test_intent";
when(mContext.getApplicationContext()).thenReturn(mContext);
Resources spiedResources = spy(mContext.getResources());
when(mContext.getResources()).thenReturn(spiedResources);
when(spiedResources.getString(MANUFACTURER_INTENT_RESOURCE)).thenReturn(intent);
mBackupSettingsHelper = new BackupSettingsHelper(mContext);
Intent manufacturerIntent = mBackupSettingsHelper.getIntentProvidedByManufacturer();
assertThat(manufacturerIntent).isNotNull();
}
@Implements(IBackupManager.Stub.class)
public static class ShadowBackupManagerStub {
@Implementation
public static IBackupManager asInterface(IBinder iBinder) {
return mBackupManager;
}
}
}

View File

@@ -0,0 +1,132 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package com.android.settings.backup;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.Intent;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION,
shadows = {BackupSettingsPreferenceControllerTest.ShadowBackupSettingsHelper.class})
public class BackupSettingsPreferenceControllerTest {
private static final String BACKUP_SETTINGS = "backup_settings";
private static final String MANUFACTURER_SETTINGS = "manufacturer_backup";
private Context mContext;
@Mock
private BackupSettingsHelper mBackupHelper;
@Mock
private PreferenceScreen mScreen;
@Mock
private Preference mBackupPreference;
@Mock
private Preference mManufacturerPreference;
@Mock
private static Intent mBackupIntent;
private static String mBackupLabel = "Test Backup Label";
private static String mBackupSummary = "Test Backup Summary";
private static String mManufacturerLabel = "Test Manufacturer Label";
@Mock
private static Intent mManufacturerIntent;
private BackupSettingsPreferenceController mController;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application.getApplicationContext());
mController = new BackupSettingsPreferenceController(mContext);
}
@Test
public void testDisplayPreference() {
when(mScreen.findPreference(BACKUP_SETTINGS)).thenReturn(mBackupPreference);
when(mScreen.findPreference(MANUFACTURER_SETTINGS)).thenReturn(mManufacturerPreference);
mController.displayPreference(mScreen);
verify(mBackupPreference).setIntent(mBackupIntent);
verify(mBackupPreference).setTitle(mBackupLabel);
verify(mBackupPreference).setSummary(mBackupSummary);
verify(mManufacturerPreference).setIntent(mManufacturerIntent);
verify(mManufacturerPreference).setTitle(mManufacturerLabel);
}
@Test
public void testIsAvailable_shouldAlwaysReturnTrue() {
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void getPreferenceKey_shouldReturnNull() {
assertThat(mController.getPreferenceKey()).isNull();
}
@Implements(BackupSettingsHelper.class)
public static class ShadowBackupSettingsHelper {
@Implementation
public Intent getIntentForBackupSettings() {
return mBackupIntent;
}
@Implementation
public String getLabelForBackupSettings() {
return mBackupLabel;
}
@Implementation
public String getSummaryForBackupSettings() {
return mBackupSummary;
}
@Implementation
public Intent getIntentProvidedByManufacturer() {
return mManufacturerIntent;
}
@Implementation
public String getLabelProvidedByManufacturer() {
return mManufacturerLabel;
}
}
}