1 /* 2 * Copyright (C) 2018 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 android.content.Context; 22 23 import androidx.preference.Preference; 24 25 import com.android.settings.R; 26 import com.android.settings.core.BasePreferenceController; 27 28 import org.junit.After; 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 import org.mockito.MockitoAnnotations; 33 import org.robolectric.RobolectricTestRunner; 34 import org.robolectric.RuntimeEnvironment; 35 import org.robolectric.annotation.Config; 36 37 @RunWith(RobolectricTestRunner.class) 38 @Config(shadows = {ShadowPrivacySettingsUtils.class}) 39 public class BackupDataPreferenceControllerTest { 40 private Context mContext; 41 private BackupDataPreferenceController mController; 42 private PrivacySettingsConfigData mPSCD; 43 private Preference mPreference; 44 45 @Before setUp()46 public void setUp() { 47 MockitoAnnotations.initMocks(this); 48 mContext = RuntimeEnvironment.application; 49 mPSCD = PrivacySettingsConfigData.getInstance(); 50 mController = new BackupDataPreferenceController(mContext, 51 PrivacySettingsUtils.BACKUP_DATA); 52 mPreference = new Preference(mContext); 53 } 54 55 @After tearDown()56 public void tearDown() { 57 ShadowPrivacySettingsUtils.reset(); 58 } 59 60 @Test updateState_backupEnabled_prefShouldBeEnabled()61 public void updateState_backupEnabled_prefShouldBeEnabled() { 62 mPSCD.setBackupEnabled(true); 63 mPSCD.setBackupGray(false); 64 65 mController.updateState(mPreference); 66 assertThat(mPreference.isEnabled()).isTrue(); 67 } 68 69 @Test updateState_backupEnabled_prefShouldDisplayOnSummary()70 public void updateState_backupEnabled_prefShouldDisplayOnSummary() { 71 mPSCD.setBackupEnabled(true); 72 mPSCD.setBackupGray(false); 73 74 mController.updateState(mPreference); 75 assertThat(mPreference.getSummary()) 76 .isEqualTo(mContext.getString(R.string.accessibility_feature_state_on)); 77 } 78 79 @Test updateState_backupDisabled_prefShouldDisplayOffSummary()80 public void updateState_backupDisabled_prefShouldDisplayOffSummary() { 81 mPSCD.setBackupEnabled(false); 82 mPSCD.setBackupGray(false); 83 84 mController.updateState(mPreference); 85 assertThat(mPreference.getSummary()) 86 .isEqualTo(mContext.getString(R.string.accessibility_feature_state_off)); 87 } 88 89 @Test getAvailabilityStatus_isAdmiUser_isnotInvisibleKey_shouldBeAvailable()90 public void getAvailabilityStatus_isAdmiUser_isnotInvisibleKey_shouldBeAvailable() { 91 ShadowPrivacySettingsUtils.setIsAdminUser(true); 92 ShadowPrivacySettingsUtils.setIsInvisibleKey(false); 93 assertThat(mController.getAvailabilityStatus()) 94 .isEqualTo(BasePreferenceController.AVAILABLE); 95 } 96 97 @Test getAvailabilityStatus_isnotAdmiUser_shouldBeDisabledForUser()98 public void getAvailabilityStatus_isnotAdmiUser_shouldBeDisabledForUser() { 99 ShadowPrivacySettingsUtils.setIsAdminUser(false); 100 assertThat(mController.getAvailabilityStatus()) 101 .isEqualTo(BasePreferenceController.DISABLED_FOR_USER); 102 } 103 104 @Test getAvailabilityStatus_isAdmiUser_isInvisibleKey_shouldBeDisabledUnsupported()105 public void getAvailabilityStatus_isAdmiUser_isInvisibleKey_shouldBeDisabledUnsupported() { 106 ShadowPrivacySettingsUtils.setIsAdminUser(true); 107 ShadowPrivacySettingsUtils.setIsInvisibleKey(true); 108 assertThat(mController.getAvailabilityStatus()) 109 .isEqualTo(BasePreferenceController.UNSUPPORTED_ON_DEVICE); 110 } 111 } 112