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 import android.content.Intent;
23 
24 import androidx.preference.Preference;
25 
26 import com.android.settings.R;
27 import com.android.settings.core.BasePreferenceController;
28 
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 import org.robolectric.RobolectricTestRunner;
36 import org.robolectric.RuntimeEnvironment;
37 import org.robolectric.annotation.Config;
38 
39 @RunWith(RobolectricTestRunner.class)
40 @Config(shadows = {ShadowPrivacySettingsUtils.class})
41 public class ConfigureAccountPreferenceControllerTest {
42     private Context mContext;
43     private ConfigureAccountPreferenceController mController;
44     private PrivacySettingsConfigData mPSCD;
45     private Preference mPreference;
46     private String mTestSummary;
47 
48     @Mock
49     private Intent mIntent;
50 
51     @Before
setUp()52     public void setUp() {
53         MockitoAnnotations.initMocks(this);
54         mContext = RuntimeEnvironment.application;
55         mPSCD = PrivacySettingsConfigData.getInstance();
56         mController = new ConfigureAccountPreferenceController(mContext,
57                 PrivacySettingsUtils.CONFIGURE_ACCOUNT);
58         mPreference = new Preference(mContext);
59         mTestSummary = "Summary";
60     }
61 
62     @After
tearDown()63     public void tearDown() {
64         ShadowPrivacySettingsUtils.reset();
65     }
66 
67     @Test
updateState_backupEnabled_hadConfigIntent_prefShouldBeEnabled()68     public void updateState_backupEnabled_hadConfigIntent_prefShouldBeEnabled() {
69         mPSCD.setBackupEnabled(true);
70         mPSCD.setBackupGray(false);
71         mPSCD.setConfigIntent(mIntent);
72 
73         mController.updateState(mPreference);
74         assertThat(mPreference.isEnabled()).isTrue();
75     }
76 
77     @Test
78     public void
updateState_backupEnabled_hadConfigIntent_nullConfigSummary_prefDisplayDefaultSummary()79     updateState_backupEnabled_hadConfigIntent_nullConfigSummary_prefDisplayDefaultSummary() {
80         mPSCD.setBackupEnabled(true);
81         mPSCD.setBackupGray(false);
82         mPSCD.setConfigIntent(mIntent);
83         mPSCD.setConfigSummary(null);
84 
85         mController.updateState(mPreference);
86         assertThat(mPreference.getSummary())
87                 .isEqualTo(mContext.getString(R.string.backup_configure_account_default_summary));
88     }
89 
90     @Test
91     public void
updateState_backupEnabled_hadConfigIntent_hasConfigSummary_prefDisplayConfigSummary()92     updateState_backupEnabled_hadConfigIntent_hasConfigSummary_prefDisplayConfigSummary() {
93         mPSCD.setBackupEnabled(true);
94         mPSCD.setBackupGray(false);
95         mPSCD.setConfigIntent(mIntent);
96         mPSCD.setConfigSummary(mTestSummary);
97 
98         mController.updateState(mPreference);
99         assertThat(mPreference.getSummary()).isEqualTo(mTestSummary);
100     }
101 
102     @Test
getAvailabilityStatus_isAdmiUser_isnotInvisibleKey_shouldBeAvailable()103     public void getAvailabilityStatus_isAdmiUser_isnotInvisibleKey_shouldBeAvailable() {
104         ShadowPrivacySettingsUtils.setIsAdminUser(true);
105         ShadowPrivacySettingsUtils.setIsInvisibleKey(false);
106         assertThat(mController.getAvailabilityStatus())
107                 .isEqualTo(BasePreferenceController.AVAILABLE);
108     }
109 
110     @Test
getAvailabilityStatus_isnotAdmiUser_shouldBeDisabledForUser()111     public void getAvailabilityStatus_isnotAdmiUser_shouldBeDisabledForUser() {
112         ShadowPrivacySettingsUtils.setIsAdminUser(false);
113         assertThat(mController.getAvailabilityStatus())
114                 .isEqualTo(BasePreferenceController.DISABLED_FOR_USER);
115     }
116 
117     @Test
getAvailabilityStatus_isAdmiUser_isInvisibleKey_shouldBeDisabledUnsupported()118     public void getAvailabilityStatus_isAdmiUser_isInvisibleKey_shouldBeDisabledUnsupported() {
119         ShadowPrivacySettingsUtils.setIsAdminUser(true);
120         ShadowPrivacySettingsUtils.setIsInvisibleKey(true);
121         assertThat(mController.getAvailabilityStatus())
122                 .isEqualTo(BasePreferenceController.UNSUPPORTED_ON_DEVICE);
123     }
124 }
125