1 /*
2  * Copyright (C) 2020 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.display;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.anyBoolean;
22 import static org.mockito.ArgumentMatchers.anyInt;
23 import static org.mockito.Mockito.atLeastOnce;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26 
27 import android.content.ContentResolver;
28 import android.content.Context;
29 import android.content.pm.PackageManager;
30 import android.provider.Settings;
31 
32 import androidx.preference.Preference;
33 
34 import com.android.internal.widget.LockPatternUtils;
35 import com.android.settings.R;
36 import com.android.settings.core.BasePreferenceController;
37 import com.android.settings.testutils.FakeFeatureFactory;
38 
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.Mock;
43 import org.mockito.MockitoAnnotations;
44 import org.robolectric.RobolectricTestRunner;
45 import org.robolectric.RuntimeEnvironment;
46 import org.robolectric.Shadows;
47 import org.robolectric.shadows.ShadowPackageManager;
48 
49 @RunWith(RobolectricTestRunner.class)
50 public class ControlsPrivacyPreferenceControllerTest {
51 
52     private static final String TEST_KEY = "test_key";
53     private static final String SETTING_KEY = Settings.Secure.LOCKSCREEN_SHOW_CONTROLS;
54 
55     private Context mContext;
56     private ContentResolver mContentResolver;
57     private ShadowPackageManager mShadowPackageManager;
58     private ControlsPrivacyPreferenceController mController;
59 
60     @Mock
61     private Preference mPreference;
62     @Mock
63     private LockPatternUtils mLockPatternUtils;
64 
65     @Before
setUp()66     public void setUp() {
67         MockitoAnnotations.initMocks(this);
68         mContext = RuntimeEnvironment.application;
69         mShadowPackageManager = Shadows.shadowOf(mContext.getPackageManager());
70 
71         mContentResolver = mContext.getContentResolver();
72         FakeFeatureFactory featureFactory = FakeFeatureFactory.setupForTest();
73         when(featureFactory.securityFeatureProvider.getLockPatternUtils(mContext))
74                 .thenReturn(mLockPatternUtils);
75         when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true);
76 
77         mController = new ControlsPrivacyPreferenceController(mContext, TEST_KEY);
78     }
79 
80     @Test
isChecked_SettingIs1_returnTrue()81     public void isChecked_SettingIs1_returnTrue() {
82         Settings.Secure.putInt(mContentResolver, SETTING_KEY, 1);
83 
84         assertThat(mController.isChecked()).isTrue();
85     }
86 
87     @Test
isChecked_SettingIs0_returnFalse()88     public void isChecked_SettingIs0_returnFalse() {
89         Settings.Secure.putInt(mContentResolver, SETTING_KEY, 0);
90 
91         assertThat(mController.isChecked()).isFalse();
92     }
93 
94     @Test
isChecked_SettingIsNotSet_returnFalse()95     public void isChecked_SettingIsNotSet_returnFalse() {
96         Settings.Secure.putString(mContentResolver, SETTING_KEY, null);
97 
98         assertThat(mController.isChecked()).isFalse();
99     }
100 
101     @Test
setChecked_true_SettingIsNot0()102     public void setChecked_true_SettingIsNot0() {
103         mController.setChecked(true);
104 
105         assertThat(Settings.Secure.getInt(mContentResolver, SETTING_KEY, 0)).isNotEqualTo(0);
106     }
107 
108     @Test
setChecked_false_SettingIs0()109     public void setChecked_false_SettingIs0() {
110         mController.setChecked(false);
111 
112         assertThat(Settings.Secure.getInt(mContentResolver, SETTING_KEY, 0)).isEqualTo(0);
113     }
114 
115     @Test
getSummary_notSecureLock_lockscreen_privacy_not_secureString()116     public void getSummary_notSecureLock_lockscreen_privacy_not_secureString() {
117         when(mLockPatternUtils.isSecure(anyInt())).thenReturn(false);
118 
119         assertThat(mController.getSummary()).isEqualTo(
120                 mContext.getText(R.string.lockscreen_privacy_not_secure));
121     }
122 
123     @Test
getSummary_ControlsAvailable_lockscreen_privacy_showString()124     public void getSummary_ControlsAvailable_lockscreen_privacy_showString() {
125         mShadowPackageManager.setSystemFeature(PackageManager.FEATURE_CONTROLS, true);
126 
127         assertThat(mController.getSummary()).isEqualTo(
128                 mContext.getText(R.string.lockscreen_privacy_controls_summary));
129     }
130 
131     @Test
updateState_onPreferenceRefreshed_preferenceEnabledAndSummaryChanged()132     public void updateState_onPreferenceRefreshed_preferenceEnabledAndSummaryChanged() {
133         mShadowPackageManager.setSystemFeature(PackageManager.FEATURE_CONTROLS, true);
134 
135         mController.updateState(mPreference);
136 
137         verify(mPreference).setEnabled(anyBoolean());
138         verify(mPreference, atLeastOnce()).setSummary(mController.getSummary());
139     }
140 
141     @Test
getAvailabilityStatus_ControlsOnNotSecure_returnsDisabled()142     public void getAvailabilityStatus_ControlsOnNotSecure_returnsDisabled() {
143         when(mLockPatternUtils.isSecure(anyInt())).thenReturn(false);
144 
145         mShadowPackageManager.setSystemFeature(PackageManager.FEATURE_CONTROLS, true);
146 
147         assertThat(mController.getAvailabilityStatus()).isEqualTo(
148                 BasePreferenceController.DISABLED_DEPENDENT_SETTING);
149     }
150 
151     @Test
getAvailabilityStatus_ControlsOffSecure_returnsDisabled()152     public void getAvailabilityStatus_ControlsOffSecure_returnsDisabled() {
153         when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true);
154 
155         mShadowPackageManager.setSystemFeature(PackageManager.FEATURE_CONTROLS, false);
156 
157         assertThat(mController.getAvailabilityStatus()).isEqualTo(
158                 BasePreferenceController.DISABLED_DEPENDENT_SETTING);
159     }
160 }
161