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.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.Mock;
33 import org.mockito.MockitoAnnotations;
34 import org.robolectric.RobolectricTestRunner;
35 import org.robolectric.RuntimeEnvironment;
36 import org.robolectric.annotation.Config;
37 
38 @RunWith(RobolectricTestRunner.class)
39 @Config(shadows = {ShadowPrivacySettingsUtils.class})
40 public class DataManagementPreferenceControllerTest {
41     private final String KEY = "data_management";
42     private Context mContext;
43     private DataManagementPreferenceController mController;
44     private PrivacySettingsConfigData mPSCD;
45     private Preference mPreference;
46     private CharSequence mTitle;
47 
48     @Mock
49     private Intent mIntent;
50 
51     @After
tearDown()52     public void tearDown() {
53         ShadowPrivacySettingsUtils.reset();
54     }
55 
56     @Before
setUp()57     public void setUp() {
58         MockitoAnnotations.initMocks(this);
59         mContext = RuntimeEnvironment.application;
60         mPSCD = PrivacySettingsConfigData.getInstance();
61         mController = new DataManagementPreferenceController(mContext, KEY);
62         mPreference = new Preference(mContext);
63         mTitle = "Title";
64     }
65 
66     @Test
updateState_backupEnabled_hadManageIntent_hasManageLable_prefShouldBeHasTitle()67     public void updateState_backupEnabled_hadManageIntent_hasManageLable_prefShouldBeHasTitle() {
68         mPSCD.setBackupEnabled(true);
69         mPSCD.setBackupGray(false);
70         mPSCD.setManageIntent(mIntent);
71         mPSCD.setManageLabel(mTitle);
72         mController.updateState(mPreference);
73         assertThat(mPreference.getTitle()).isEqualTo(mTitle);
74     }
75 
76     @Test
getAvailabilityStatus_isAdmin_backupEnabled_hadManageIntent_shouldBeAvailable()77     public void getAvailabilityStatus_isAdmin_backupEnabled_hadManageIntent_shouldBeAvailable() {
78         ShadowPrivacySettingsUtils.setIsAdminUser(true);
79         mPSCD.setBackupEnabled(true);
80         mPSCD.setBackupGray(false);
81         mPSCD.setManageIntent(mIntent);
82         mPSCD.setManageLabel(mTitle);
83 
84         assertThat(mController.getAvailabilityStatus())
85                 .isEqualTo(BasePreferenceController.AVAILABLE);
86     }
87 
88     @Test
getAvailabilityStatus_isNotAdminUser_shouldBeDisabledForUser()89     public void getAvailabilityStatus_isNotAdminUser_shouldBeDisabledForUser() {
90         ShadowPrivacySettingsUtils.setIsAdminUser(false);
91         assertThat(mController.getAvailabilityStatus())
92                 .isEqualTo(BasePreferenceController.DISABLED_FOR_USER);
93     }
94 
95     @Test
96     public void
getAvailabilityStatus_isAdminUser_backupEnabled_nullManageIntent_shouldBeDisabledUnsupported()97     getAvailabilityStatus_isAdminUser_backupEnabled_nullManageIntent_shouldBeDisabledUnsupported() {
98         ShadowPrivacySettingsUtils.setIsAdminUser(true);
99         mPSCD.setBackupEnabled(true);
100         mPSCD.setBackupGray(false);
101         mPSCD.setManageIntent(null);
102         mPSCD.setManageLabel(mTitle);
103 
104         assertThat(mController.getAvailabilityStatus())
105                 .isEqualTo(BasePreferenceController.UNSUPPORTED_ON_DEVICE);
106     }
107 }
108