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