1 /* 2 * Copyright (C) 2019 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.notification; 18 19 import static android.app.admin.DevicePolicyManager.KEYGUARD_DISABLE_SECURE_NOTIFICATIONS; 20 import static android.app.admin.DevicePolicyManager.KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS; 21 import static android.provider.Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS; 22 import static android.provider.Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS; 23 24 import static com.android.settings.core.BasePreferenceController.AVAILABLE; 25 import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE; 26 import static com.android.settings.core.BasePreferenceController.DISABLED_DEPENDENT_SETTING; 27 28 import static com.google.common.truth.Truth.assertThat; 29 30 import static org.mockito.ArgumentMatchers.anyInt; 31 import static org.mockito.ArgumentMatchers.anyString; 32 import static org.mockito.ArgumentMatchers.eq; 33 import static org.mockito.Mockito.verify; 34 import static org.mockito.Mockito.when; 35 36 import android.app.KeyguardManager; 37 import android.app.admin.DevicePolicyManager; 38 import android.content.Context; 39 import android.os.UserHandle; 40 import android.os.UserManager; 41 import android.provider.Settings; 42 import android.service.notification.Adjustment; 43 44 import com.android.internal.widget.LockPatternUtils; 45 import com.android.settings.testutils.FakeFeatureFactory; 46 47 import org.junit.Before; 48 import org.junit.Test; 49 import org.junit.runner.RunWith; 50 import org.mockito.Mock; 51 import org.mockito.MockitoAnnotations; 52 import org.robolectric.RobolectricTestRunner; 53 import org.robolectric.RuntimeEnvironment; 54 55 import java.util.ArrayList; 56 import java.util.List; 57 58 import androidx.preference.Preference; 59 import androidx.preference.PreferenceScreen; 60 61 @RunWith(RobolectricTestRunner.class) 62 public class RedactNotificationPreferenceControllerTest { 63 64 @Mock 65 private DevicePolicyManager mDpm; 66 @Mock 67 UserManager mUm; 68 @Mock 69 KeyguardManager mKm; 70 @Mock 71 private PreferenceScreen mScreen; 72 @Mock 73 private LockPatternUtils mLockPatternUtils; 74 @Mock 75 private Context mMockContext; 76 77 private Context mContext; 78 private RedactNotificationPreferenceController mController; 79 private RedactNotificationPreferenceController mWorkController; 80 private Preference mPreference; 81 private Preference mWorkPreference; 82 83 @Before setUp()84 public void setUp() { 85 MockitoAnnotations.initMocks(this); 86 mContext = RuntimeEnvironment.application; 87 88 FakeFeatureFactory featureFactory = FakeFeatureFactory.setupForTest(); 89 when(featureFactory.securityFeatureProvider.getLockPatternUtils(mMockContext)) 90 .thenReturn(mLockPatternUtils); 91 when(mMockContext.getContentResolver()).thenReturn(mContext.getContentResolver()); 92 when(mMockContext.getSystemService(UserManager.class)).thenReturn(mUm); 93 when(mMockContext.getSystemService(DevicePolicyManager.class)).thenReturn(mDpm); 94 when(mMockContext.getSystemService(KeyguardManager.class)).thenReturn(mKm); 95 when(mUm.getProfileIdsWithDisabled(anyInt())).thenReturn(new int[] {10}); 96 97 mController = new RedactNotificationPreferenceController( 98 mMockContext, RedactNotificationPreferenceController.KEY_LOCKSCREEN_REDACT); 99 mPreference = new Preference(mContext); 100 mPreference.setKey(mController.getPreferenceKey()); 101 when(mScreen.findPreference( 102 mController.getPreferenceKey())).thenReturn(mPreference); 103 104 mWorkController = new RedactNotificationPreferenceController(mMockContext, 105 RedactNotificationPreferenceController.KEY_LOCKSCREEN_WORK_PROFILE_REDACT); 106 mWorkPreference = new Preference(mContext); 107 mWorkPreference.setKey(mWorkController.getPreferenceKey()); 108 when(mScreen.findPreference( 109 mWorkController.getPreferenceKey())).thenReturn(mWorkPreference); 110 } 111 112 @Test getAvailabilityStatus_noSecureLockscreen()113 public void getAvailabilityStatus_noSecureLockscreen() { 114 when(mLockPatternUtils.isSecure(anyInt())).thenReturn(false); 115 Settings.Secure.putIntForUser(mContext.getContentResolver(), 116 LOCK_SCREEN_SHOW_NOTIFICATIONS, 117 1, 0); 118 Settings.Secure.putIntForUser(mContext.getContentResolver(), 119 LOCK_SCREEN_SHOW_NOTIFICATIONS, 120 1, 10); 121 122 assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE); 123 assertThat(mWorkController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE); 124 } 125 126 @Test getAvailabilityStatus_noWorkProfile()127 public void getAvailabilityStatus_noWorkProfile() { 128 // reset controllers with no work profile 129 when(mUm.getProfileIdsWithDisabled(anyInt())).thenReturn(new int[] {UserHandle.USER_NULL}); 130 mWorkController = new RedactNotificationPreferenceController(mMockContext, 131 RedactNotificationPreferenceController.KEY_LOCKSCREEN_WORK_PROFILE_REDACT); 132 mController = new RedactNotificationPreferenceController(mMockContext, 133 RedactNotificationPreferenceController.KEY_LOCKSCREEN_REDACT); 134 135 // should otherwise show 136 when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true); 137 Settings.Secure.putIntForUser(mContext.getContentResolver(), 138 LOCK_SCREEN_SHOW_NOTIFICATIONS, 139 1, 0); 140 141 assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); 142 assertThat(mWorkController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE); 143 } 144 145 @Test getAvailabilityStatus_adminSaysNoRedaction()146 public void getAvailabilityStatus_adminSaysNoRedaction() { 147 when(mDpm.getKeyguardDisabledFeatures(eq(null), anyInt())).thenReturn( 148 KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS); 149 150 // should otherwise show 151 when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true); 152 Settings.Secure.putIntForUser(mContext.getContentResolver(), 153 LOCK_SCREEN_SHOW_NOTIFICATIONS, 154 1, 0); 155 Settings.Secure.putIntForUser(mContext.getContentResolver(), 156 LOCK_SCREEN_SHOW_NOTIFICATIONS, 157 1, 10); 158 159 assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_DEPENDENT_SETTING); 160 assertThat(mWorkController.getAvailabilityStatus()).isEqualTo(DISABLED_DEPENDENT_SETTING); 161 } 162 163 @Test getAvailabilityStatus_adminSaysNoNotifications()164 public void getAvailabilityStatus_adminSaysNoNotifications() { 165 when(mDpm.getKeyguardDisabledFeatures(eq(null), anyInt())).thenReturn( 166 KEYGUARD_DISABLE_SECURE_NOTIFICATIONS); 167 168 // should otherwise show 169 when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true); 170 Settings.Secure.putIntForUser(mContext.getContentResolver(), 171 LOCK_SCREEN_SHOW_NOTIFICATIONS, 172 1, 0); 173 Settings.Secure.putIntForUser(mContext.getContentResolver(), 174 LOCK_SCREEN_SHOW_NOTIFICATIONS, 175 1, 10); 176 177 assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_DEPENDENT_SETTING); 178 assertThat(mWorkController.getAvailabilityStatus()).isEqualTo(DISABLED_DEPENDENT_SETTING); 179 } 180 181 @Test getAvailabilityStatus_noNotifications()182 public void getAvailabilityStatus_noNotifications() { 183 when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true); 184 185 Settings.Secure.putIntForUser(mContext.getContentResolver(), 186 LOCK_SCREEN_SHOW_NOTIFICATIONS, 187 0, 0); 188 Settings.Secure.putIntForUser(mContext.getContentResolver(), 189 LOCK_SCREEN_SHOW_NOTIFICATIONS, 190 0, 10); 191 192 assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_DEPENDENT_SETTING); 193 assertThat(mWorkController.getAvailabilityStatus()).isEqualTo(DISABLED_DEPENDENT_SETTING); 194 } 195 196 @Test getAvailabilityStatus_workProfileLocked()197 public void getAvailabilityStatus_workProfileLocked() { 198 // should otherwise show 199 when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true); 200 Settings.Secure.putIntForUser(mContext.getContentResolver(), 201 LOCK_SCREEN_SHOW_NOTIFICATIONS, 202 1, 0); 203 Settings.Secure.putIntForUser(mContext.getContentResolver(), 204 LOCK_SCREEN_SHOW_NOTIFICATIONS, 205 1, 10); 206 207 when(mKm.isDeviceLocked(10)).thenReturn(true); 208 209 assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); 210 assertThat(mWorkController.getAvailabilityStatus()).isEqualTo(DISABLED_DEPENDENT_SETTING); 211 } 212 213 @Test getAvailabilityStatus_show()214 public void getAvailabilityStatus_show() { 215 // should otherwise show 216 when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true); 217 Settings.Secure.putIntForUser(mContext.getContentResolver(), 218 LOCK_SCREEN_SHOW_NOTIFICATIONS, 219 1, 0); 220 Settings.Secure.putIntForUser(mContext.getContentResolver(), 221 LOCK_SCREEN_SHOW_NOTIFICATIONS, 222 1, 10); 223 224 assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); 225 assertThat(mWorkController.getAvailabilityStatus()).isEqualTo(AVAILABLE); 226 } 227 228 @Test isChecked()229 public void isChecked() { 230 Settings.Secure.putIntForUser(mContext.getContentResolver(), 231 LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 232 1, 0); 233 234 assertThat(mController.isChecked()).isTrue(); 235 236 Settings.Secure.putIntForUser(mContext.getContentResolver(), 237 LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 238 0, 0); 239 240 assertThat(mController.isChecked()).isFalse(); 241 } 242 243 @Test isChecked_work()244 public void isChecked_work() { 245 Settings.Secure.putIntForUser(mContext.getContentResolver(), 246 LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 247 1, 10); 248 249 assertThat(mWorkController.isChecked()).isTrue(); 250 251 Settings.Secure.putIntForUser(mContext.getContentResolver(), 252 LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 253 0, 10); 254 255 assertThat(mWorkController.isChecked()).isFalse(); 256 } 257 258 @Test setChecked_false()259 public void setChecked_false() throws Exception { 260 Settings.Secure.putIntForUser(mContext.getContentResolver(), 261 LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 262 1, 0); 263 264 mController.setChecked(false); 265 assertThat(Settings.Secure.getIntForUser( 266 mContext.getContentResolver(), LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 0)) 267 .isEqualTo(0); 268 } 269 270 @Test setChecked_workProfile_false()271 public void setChecked_workProfile_false() throws Exception { 272 Settings.Secure.putIntForUser(mContext.getContentResolver(), 273 LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 274 1, 10); 275 276 mWorkController.setChecked(false); 277 assertThat(Settings.Secure.getIntForUser( 278 mContext.getContentResolver(), LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 10)) 279 .isEqualTo(0); 280 } 281 282 @Test setChecked_true()283 public void setChecked_true() throws Exception { 284 Settings.Secure.putIntForUser(mContext.getContentResolver(), 285 LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 286 0, 0); 287 Settings.Secure.putIntForUser(mContext.getContentResolver(), 288 LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 289 0, 10); 290 291 mController.setChecked(true); 292 mWorkController.setChecked(true); 293 assertThat(Settings.Secure.getIntForUser( 294 mContext.getContentResolver(), LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 10)) 295 .isEqualTo(1); 296 assertThat(Settings.Secure.getIntForUser( 297 mContext.getContentResolver(), LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 0)) 298 .isEqualTo(1); 299 } 300 } 301 302