• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.anyString;
23 import static org.mockito.Mockito.atLeastOnce;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27 
28 import android.content.ContentResolver;
29 import android.content.Context;
30 import android.database.Cursor;
31 import android.database.MatrixCursor;
32 import android.provider.Settings;
33 
34 import androidx.preference.Preference;
35 import androidx.preference.PreferenceScreen;
36 import androidx.test.core.app.ApplicationProvider;
37 
38 import com.android.settings.R;
39 import com.android.settings.core.BasePreferenceController;
40 
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.Mock;
45 import org.mockito.Mockito;
46 import org.mockito.MockitoAnnotations;
47 import org.mockito.stubbing.Answer;
48 import org.robolectric.RobolectricTestRunner;
49 
50 @RunWith(RobolectricTestRunner.class)
51 public class ControlsTrivialPrivacyPreferenceControllerTest {
52 
53     private static final String TEST_KEY = "test_key";
54     private static final String SETTING_KEY = Settings.Secure.LOCKSCREEN_ALLOW_TRIVIAL_CONTROLS;
55     private static final String DEPENDENCY_SETTING_KEY = Settings.Secure.LOCKSCREEN_SHOW_CONTROLS;
56 
57     private Context mContext;
58     private ContentResolver mContentResolver;
59     private ControlsTrivialPrivacyPreferenceController mController;
60 
61     @Mock
62     private Preference mPreference;
63 
64     @Mock
65     private PreferenceScreen mPreferenceScreen;
66 
67     @Before
setUp()68     public void setUp() {
69         MockitoAnnotations.initMocks(this);
70         mContext = spy(ApplicationProvider.getApplicationContext());
71         mContentResolver = spy(mContext.getContentResolver());
72         when(mContext.getContentResolver()).thenReturn(mContentResolver);
73 
74         setCustomizableLockScreenQuickAffordancesEnabled(false);
75 
76         mController = new ControlsTrivialPrivacyPreferenceController(mContext, TEST_KEY);
77     }
78 
79     @Test
isCheckedWhenSettingIsTrue()80     public void isCheckedWhenSettingIsTrue() {
81         Settings.Secure.putInt(mContentResolver, SETTING_KEY, 1);
82 
83         assertThat(mController.isChecked()).isTrue();
84     }
85 
86     @Test
isCheckedWhenSettingIsFalse()87     public void isCheckedWhenSettingIsFalse() {
88         Settings.Secure.putInt(mContentResolver, SETTING_KEY, 0);
89 
90         assertThat(mController.isChecked()).isFalse();
91     }
92 
93     @Test
isCheckedWhenSettingIsNull()94     public void isCheckedWhenSettingIsNull() {
95         Settings.Secure.putString(mContentResolver, SETTING_KEY, null);
96 
97         assertThat(mController.isChecked()).isFalse();
98     }
99 
100     @Test
checkedMeansSettingIsTrue()101     public void checkedMeansSettingIsTrue() {
102         mController.setChecked(true);
103 
104         assertThat(Settings.Secure.getInt(mContentResolver, SETTING_KEY, 0)).isNotEqualTo(0);
105     }
106 
107     @Test
uncheckedMeansSettingIsFalse()108     public void uncheckedMeansSettingIsFalse() {
109         mController.setChecked(false);
110 
111         assertThat(Settings.Secure.getInt(mContentResolver, SETTING_KEY, 0)).isEqualTo(0);
112     }
113 
114     @Test
getSummaryRequireDeviceControls()115     public void getSummaryRequireDeviceControls() {
116         Settings.Secure.putInt(mContentResolver, DEPENDENCY_SETTING_KEY, 0);
117 
118         assertThat(mController.getSummary().toString()).isEqualTo(
119                 mContext.getText(R.string.lockscreen_trivial_disabled_controls_summary));
120     }
121 
122     @Test
getSummaryDefault()123     public void getSummaryDefault() {
124         Settings.Secure.putInt(mContentResolver, DEPENDENCY_SETTING_KEY, 1);
125 
126         assertThat(mController.getSummary().toString()).isEqualTo(
127                 mContext.getText(R.string.lockscreen_trivial_controls_summary));
128     }
129 
130     @Test
updateState()131     public void updateState() {
132         Settings.Secure.putInt(mContentResolver, DEPENDENCY_SETTING_KEY, 1);
133 
134         mController.updateState(mPreference);
135 
136         verify(mPreference).setEnabled(anyBoolean());
137         verify(mPreference, atLeastOnce()).setSummary(mController.getSummary());
138     }
139 
140     @Test
updateStateWithCustomizableLockScreenQuickAffordancesEnabled()141     public void updateStateWithCustomizableLockScreenQuickAffordancesEnabled() {
142         setCustomizableLockScreenQuickAffordancesEnabled(true);
143         Settings.Secure.putInt(mContentResolver, DEPENDENCY_SETTING_KEY, 0);
144 
145         mController.updateState(mPreference);
146 
147         verify(mPreference).setEnabled(true);
148         verify(mPreference, atLeastOnce()).setSummary(
149                 mContext.getString(R.string.lockscreen_trivial_controls_summary));
150     }
151 
152     @Test
getAvailabilityStatusWithoutDeviceControls()153     public void getAvailabilityStatusWithoutDeviceControls() {
154         Settings.Secure.putInt(mContentResolver, DEPENDENCY_SETTING_KEY, 0);
155 
156         assertThat(mController.getAvailabilityStatus()).isEqualTo(
157                 BasePreferenceController.DISABLED_DEPENDENT_SETTING);
158     }
159 
160     @Test
getAvailabilityStatusWithCustomizableLockScreenQuickAffordancesEnabled()161     public void getAvailabilityStatusWithCustomizableLockScreenQuickAffordancesEnabled() {
162         setCustomizableLockScreenQuickAffordancesEnabled(true);
163         Settings.Secure.putInt(mContentResolver, DEPENDENCY_SETTING_KEY, 0);
164 
165         assertThat(mController.getAvailabilityStatus()).isEqualTo(
166                 BasePreferenceController.AVAILABLE);
167     }
168 
169     @Test
getAvailabilityStatusWithDeviceControls()170     public void getAvailabilityStatusWithDeviceControls() {
171         Settings.Secure.putInt(mContentResolver, DEPENDENCY_SETTING_KEY, 1);
172 
173 
174         assertThat(mController.getAvailabilityStatus()).isEqualTo(
175                 BasePreferenceController.AVAILABLE);
176     }
177 
178     @Test
setDependency()179     public void setDependency() {
180         Mockito.when(mPreferenceScreen
181                 .findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
182         mController.displayPreference(mPreferenceScreen);
183         verify(mPreference).setDependency(anyString());
184     }
185 
setCustomizableLockScreenQuickAffordancesEnabled(boolean isEnabled)186     private void setCustomizableLockScreenQuickAffordancesEnabled(boolean isEnabled) {
187         when(
188                 mContentResolver.query(
189                         CustomizableLockScreenUtils.FLAGS_URI, null, null, null))
190                 .thenAnswer((Answer<Cursor>) invocation -> {
191                     final MatrixCursor cursor = new MatrixCursor(
192                             new String[] {
193                                     CustomizableLockScreenUtils.NAME,
194                                     CustomizableLockScreenUtils.VALUE
195                             });
196                     cursor.addRow(
197                             new Object[] {
198                                     CustomizableLockScreenUtils.ENABLED_FLAG, isEnabled ? 1 : 0
199                             });
200                     return cursor;
201                 });
202     }
203 }
204