From ca5e1bd67b4180f2ccd658b98764d34845e5e508 Mon Sep 17 00:00:00 2001 From: Anton Philippov Date: Fri, 21 Apr 2017 10:15:52 +0100 Subject: [PATCH] Add summary for Backup menu in Settings. 'On'/'Off' depending on whether the backup is enabled in settings. Refactors BackupSettingsActivity to be shown on System fragment via xml/PreferenceController instead of category in the manifest. Bug: 36891827 Test: make RunSettingsRoboTests Change-Id: I61425fed4d63055c6c386be38792ae074aa72679 --- AndroidManifest.xml | 2 - res/xml/system_dashboard_fragment.xml | 10 ++ ...pSettingsActivityPreferenceController.java | 64 +++++++++ .../system/SystemDashboardFragment.java | 2 + ...tingsActivityPreferenceControllerTest.java | 122 ++++++++++++++++++ 5 files changed, 198 insertions(+), 2 deletions(-) create mode 100644 src/com/android/settings/backup/BackupSettingsActivityPreferenceController.java create mode 100644 tests/robotests/src/com/android/settings/backup/BackupSettingsActivityPreferenceControllerTest.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 8535b2fc445..9b49276f4c9 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -2933,8 +2933,6 @@ - + + + + + controllers = new ArrayList<>(); controllers.add(new SystemUpdatePreferenceController(context, UserManager.get(context))); controllers.add(new AdditionalSystemUpdatePreferenceController(context)); + controllers.add(new BackupSettingsActivityPreferenceController(context)); return controllers; } diff --git a/tests/robotests/src/com/android/settings/backup/BackupSettingsActivityPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/backup/BackupSettingsActivityPreferenceControllerTest.java new file mode 100644 index 00000000000..a58b67db114 --- /dev/null +++ b/tests/robotests/src/com/android/settings/backup/BackupSettingsActivityPreferenceControllerTest.java @@ -0,0 +1,122 @@ +/* + * 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 static com.google.common.truth.Truth.assertThat; + +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import android.accounts.AccountManager; +import android.app.backup.BackupManager; +import android.content.Context; +import android.content.Intent; +import android.os.RemoteException; +import android.os.UserManager; +import android.support.v7.preference.Preference; +import android.support.v7.preference.PreferenceScreen; + +import com.android.settings.R; +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; + +@RunWith(SettingsRobolectricTestRunner.class) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION, + shadows = {BackupSettingsActivityPreferenceControllerTest.ShadowBackupManager.class}) +public class BackupSettingsActivityPreferenceControllerTest { + private static final String KEY_BACKUP_SETTINGS = "backup_settings"; + + private Context mContext; + @Mock + private UserManager mUserManager; + + @Mock + private PreferenceScreen mScreen; + @Mock + private Preference mBackupPreference; + + private BackupSettingsActivityPreferenceController mController; + + private static boolean mBackupEnabled; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + mContext = spy(RuntimeEnvironment.application.getApplicationContext()); + when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); + + mController = new BackupSettingsActivityPreferenceController(mContext); + } + + @Test + public void updateState_backupOn() throws RemoteException { + mBackupEnabled = true; + + mController.updateState(mBackupPreference); + + verify(mBackupPreference).setSummary(R.string.accessibility_feature_state_on); + } + + @Test + public void updateState_backupOff() throws RemoteException { + mBackupEnabled = false; + + mController.updateState(mBackupPreference); + + verify(mBackupPreference).setSummary(R.string.accessibility_feature_state_off); + } + + @Test + public void isAvailable_systemUser() { + when(mUserManager.isAdminUser()).thenReturn(true); + + assertThat(mController.isAvailable()).isTrue(); + } + + @Test + public void isAvailable_nonSystemUser() { + when(mUserManager.isAdminUser()).thenReturn(false); + + assertThat(mController.isAvailable()).isFalse(); + } + + @Test + public void getPreferenceKey() { + assertThat(mController.getPreferenceKey()).isEqualTo(KEY_BACKUP_SETTINGS); + } + + @Implements(BackupManager.class) + public static class ShadowBackupManager { + + @Implementation + public boolean isBackupEnabled() { + return mBackupEnabled; + } + } +}