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.gestures;
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 PowerMenuPrivacyPreferenceControllerTest {
51 
52     private static final String TEST_KEY = "test_key";
53     private static final String SETTING_KEY = Settings.Secure.POWER_MENU_LOCKED_SHOW_CONTENT;
54     private static final String CARDS_AVAILABLE_KEY =
55             Settings.Secure.GLOBAL_ACTIONS_PANEL_AVAILABLE;
56     private static final String CARDS_ENABLED_KEY = Settings.Secure.GLOBAL_ACTIONS_PANEL_ENABLED;
57     private static final String CONTROLS_ENABLED_KEY = Settings.Secure.CONTROLS_ENABLED;
58 
59     private Context mContext;
60     private ContentResolver mContentResolver;
61     private ShadowPackageManager mShadowPackageManager;
62     private PowerMenuPrivacyPreferenceController mController;
63 
64     @Mock
65     private Preference mPreference;
66     @Mock
67     private LockPatternUtils mLockPatternUtils;
68 
69     @Before
setUp()70     public void setUp() {
71         MockitoAnnotations.initMocks(this);
72         mContext = RuntimeEnvironment.application;
73         mShadowPackageManager = Shadows.shadowOf(mContext.getPackageManager());
74 
75         mContentResolver = mContext.getContentResolver();
76         FakeFeatureFactory featureFactory = FakeFeatureFactory.setupForTest();
77         when(featureFactory.securityFeatureProvider.getLockPatternUtils(mContext))
78                 .thenReturn(mLockPatternUtils);
79         when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true);
80 
81         mController = new PowerMenuPrivacyPreferenceController(mContext, TEST_KEY);
82     }
83 
84     @Test
isChecked_POWER_MENU_LOCKED_SHOW_CONTENTIs1_returnTrue()85     public void isChecked_POWER_MENU_LOCKED_SHOW_CONTENTIs1_returnTrue() {
86         Settings.Secure.putInt(mContentResolver, SETTING_KEY, 1);
87 
88         assertThat(mController.isChecked()).isTrue();
89     }
90 
91     @Test
isChecked_POWER_MENU_LOCKED_SHOW_CONTENTIs0_returnFalse()92     public void isChecked_POWER_MENU_LOCKED_SHOW_CONTENTIs0_returnFalse() {
93         Settings.Secure.putInt(mContentResolver, SETTING_KEY, 0);
94 
95         assertThat(mController.isChecked()).isFalse();
96     }
97 
98     @Test
isChecked_POWER_MENU_LOCKED_SHOW_CONTENTIsNotSet_returnFalse()99     public void isChecked_POWER_MENU_LOCKED_SHOW_CONTENTIsNotSet_returnFalse() {
100         Settings.Secure.putString(mContentResolver, SETTING_KEY, null);
101 
102         assertThat(mController.isChecked()).isFalse();
103     }
104 
105     @Test
setChecked_true_POWER_MENU_LOCKED_SHOW_CONTENTIsNot0()106     public void setChecked_true_POWER_MENU_LOCKED_SHOW_CONTENTIsNot0() {
107         mController.setChecked(true);
108 
109         assertThat(Settings.Secure.getInt(mContentResolver, SETTING_KEY, 0)).isNotEqualTo(0);
110     }
111 
112     @Test
setChecked_false_POWER_MENU_LOCKED_SHOW_CONTENTIs0()113     public void setChecked_false_POWER_MENU_LOCKED_SHOW_CONTENTIs0() {
114         mController.setChecked(false);
115 
116         assertThat(Settings.Secure.getInt(mContentResolver, SETTING_KEY, 0)).isEqualTo(0);
117     }
118 
119     @Test
getSummary_notSecureLock_isPower_menu_privacy_not_secureString()120     public void getSummary_notSecureLock_isPower_menu_privacy_not_secureString() {
121         when(mLockPatternUtils.isSecure(anyInt())).thenReturn(false);
122 
123         assertThat(mController.getSummary()).isEqualTo(
124                 mContext.getText(R.string.power_menu_privacy_not_secure));
125     }
126 
127     @Test
getSummary_cardsControlsAvailable_isPower_menu_privacy_showString()128     public void getSummary_cardsControlsAvailable_isPower_menu_privacy_showString() {
129         Settings.Secure.putInt(mContentResolver, CARDS_AVAILABLE_KEY, 1);
130         mShadowPackageManager.setSystemFeature(PackageManager.FEATURE_CONTROLS, true);
131 
132         assertThat(mController.getSummary()).isEqualTo(
133                 mContext.getText(R.string.power_menu_privacy_show));
134     }
135 
136     @Test
137     public void
getSummary_cardsUnavailableControlsAvailable_isPower_menu_privacy_show_controlsString()138         getSummary_cardsUnavailableControlsAvailable_isPower_menu_privacy_show_controlsString() {
139         Settings.Secure.putInt(mContentResolver, CARDS_AVAILABLE_KEY, 0);
140         mShadowPackageManager.setSystemFeature(PackageManager.FEATURE_CONTROLS, true);
141 
142         assertThat(mController.getSummary()).isEqualTo(
143                 mContext.getText(R.string.power_menu_privacy_show_controls));
144     }
145 
146     @Test
147     public void
getSummary_cardsAvailableControlsUnavailable_isPower_menu_privacy_show_cardsString()148         getSummary_cardsAvailableControlsUnavailable_isPower_menu_privacy_show_cardsString() {
149         Settings.Secure.putInt(mContentResolver, CARDS_AVAILABLE_KEY, 1);
150         mShadowPackageManager.setSystemFeature(PackageManager.FEATURE_CONTROLS, false);
151 
152         assertThat(mController.getSummary()).isEqualTo(
153                 mContext.getText(R.string.power_menu_privacy_show_cards));
154     }
155 
156     @Test
updateState_onPreferenceRefreshed_preferenceEnabledAndSummaryChanged()157     public void updateState_onPreferenceRefreshed_preferenceEnabledAndSummaryChanged() {
158         mShadowPackageManager.setSystemFeature(PackageManager.FEATURE_CONTROLS, true);
159         Settings.Secure.putInt(mContentResolver, CARDS_AVAILABLE_KEY, 1);
160 
161         mController.updateState(mPreference);
162 
163         verify(mPreference).setEnabled(anyBoolean());
164         verify(mPreference, atLeastOnce()).setSummary(mController.getSummary());
165     }
166 
167     @Test
getAvailabilityStatus_allOnNotSecure_returnsDisabled()168     public void getAvailabilityStatus_allOnNotSecure_returnsDisabled() {
169         when(mLockPatternUtils.isSecure(anyInt())).thenReturn(false);
170 
171         mShadowPackageManager.setSystemFeature(PackageManager.FEATURE_CONTROLS, true);
172         Settings.Secure.putInt(mContentResolver, CARDS_AVAILABLE_KEY, 1);
173         Settings.Secure.putInt(mContentResolver, CARDS_ENABLED_KEY, 1);
174         Settings.Secure.putInt(mContentResolver, CONTROLS_ENABLED_KEY, 1);
175 
176         assertThat(mController.getAvailabilityStatus()).isEqualTo(
177                 BasePreferenceController.DISABLED_DEPENDENT_SETTING);
178     }
179 
180     @Test
getAvailabilityStatus_controlsDeletedSecure_retursAvailable()181     public void getAvailabilityStatus_controlsDeletedSecure_retursAvailable() {
182         Settings.Secure.putString(mContentResolver, CONTROLS_ENABLED_KEY, null);
183         mShadowPackageManager.setSystemFeature(PackageManager.FEATURE_CONTROLS, true);
184 
185         assertThat(mController.getAvailabilityStatus()).isEqualTo(
186                 BasePreferenceController.AVAILABLE);
187     }
188 }
189