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.provider.Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS; 21 import static android.provider.Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS; 22 23 import static com.google.common.truth.Truth.assertThat; 24 25 import static org.mockito.Mockito.when; 26 27 import android.app.admin.DevicePolicyManager; 28 import android.content.Context; 29 import android.provider.Settings; 30 31 import androidx.preference.PreferenceScreen; 32 33 import com.android.settings.R; 34 import com.android.settings.RestrictedListPreference; 35 import com.android.settings.testutils.shadow.ShadowRestrictedLockUtilsInternal; 36 import com.android.settings.testutils.shadow.ShadowUserManager; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.mockito.Answers; 42 import org.mockito.Mock; 43 import org.mockito.MockitoAnnotations; 44 import org.robolectric.Robolectric; 45 import org.robolectric.RobolectricTestRunner; 46 import org.robolectric.RuntimeEnvironment; 47 import org.robolectric.annotation.Config; 48 49 @RunWith(RobolectricTestRunner.class) 50 @Config(shadows = {ShadowUserManager.class, ShadowRestrictedLockUtilsInternal.class}) 51 public class ShowOnLockscreenNotificationPreferenceControllerTest { 52 53 private Context mContext; 54 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 55 private PreferenceScreen mScreen; 56 @Mock 57 DevicePolicyManager mDpm; 58 59 private ShowOnLockScreenNotificationPreferenceController mController; 60 private RestrictedListPreference mPreference; 61 62 private static final String KEY = "key"; 63 64 @Before setUp()65 public void setUp() { 66 MockitoAnnotations.initMocks(this); 67 mContext = RuntimeEnvironment.application; 68 mController = new ShowOnLockScreenNotificationPreferenceController(mContext, KEY); 69 mPreference = new RestrictedListPreference( 70 mContext, Robolectric.buildAttributeSet().build()); 71 mPreference.setKey(mController.getPreferenceKey()); 72 when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference); 73 mController.setDpm(mDpm); 74 } 75 76 @Test display_shouldDisplay()77 public void display_shouldDisplay() { 78 assertThat(mPreference.isVisible()).isTrue(); 79 } 80 81 @Test updateState_noNotifsOnLockscreen()82 public void updateState_noNotifsOnLockscreen() { 83 Settings.Secure.putInt(mContext.getContentResolver(), 84 LOCK_SCREEN_SHOW_NOTIFICATIONS, 85 0); 86 // should be ignored 87 Settings.Secure.putInt(mContext.getContentResolver(), 88 LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 89 1); 90 91 mController.displayPreference(mScreen); 92 93 assertThat(mPreference.getValue()).isEqualTo( 94 String.valueOf(R.string.lock_screen_notifs_show_none)); 95 96 assertThat(mPreference.getSummary().toString()) 97 .isEqualTo(mContext.getString(R.string.lock_screen_notifs_show_none)); 98 } 99 100 @Test updateState_alertingNotifsOnLockscreen()101 public void updateState_alertingNotifsOnLockscreen() { 102 Settings.Secure.putInt(mContext.getContentResolver(), 103 LOCK_SCREEN_SHOW_NOTIFICATIONS, 104 1); 105 Settings.Secure.putInt(mContext.getContentResolver(), 106 LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 107 0); 108 109 mController.displayPreference(mScreen); 110 111 assertThat(mPreference.getValue()).isEqualTo( 112 String.valueOf(R.string.lock_screen_notifs_show_alerting)); 113 assertThat(mPreference.getSummary().toString()) 114 .isEqualTo(mContext.getString(R.string.lock_screen_notifs_show_alerting)); 115 } 116 117 @Test updateState_allNotifsOnLockscreen()118 public void updateState_allNotifsOnLockscreen() { 119 Settings.Secure.putInt(mContext.getContentResolver(), 120 LOCK_SCREEN_SHOW_NOTIFICATIONS, 121 1); 122 Settings.Secure.putInt(mContext.getContentResolver(), 123 LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 124 1); 125 126 mController.displayPreference(mScreen); 127 128 assertThat(mPreference.getValue()).isEqualTo( 129 String.valueOf(R.string.lock_screen_notifs_show_all)); 130 assertThat(mPreference.getSummary().toString()) 131 .isEqualTo(mContext.getString(R.string.lock_screen_notifs_show_all)); 132 } 133 134 @Test updateState_alertingNotifsOnLockscreen_isDefault()135 public void updateState_alertingNotifsOnLockscreen_isDefault() { 136 // settings don't exist 137 138 mController.displayPreference(mScreen); 139 140 assertThat(mPreference.getValue()).isEqualTo( 141 String.valueOf(R.string.lock_screen_notifs_show_alerting)); 142 assertThat(mPreference.getSummary().toString()) 143 .isEqualTo(mContext.getString(R.string.lock_screen_notifs_show_alerting)); 144 } 145 146 @Test updateState_dpmSaysNo()147 public void updateState_dpmSaysNo() { 148 Settings.Secure.putInt(mContext.getContentResolver(), 149 LOCK_SCREEN_SHOW_NOTIFICATIONS, 150 1); 151 Settings.Secure.putInt(mContext.getContentResolver(), 152 LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 153 1); 154 155 when(mDpm.getKeyguardDisabledFeatures(null)) 156 .thenReturn(KEYGUARD_DISABLE_SECURE_NOTIFICATIONS); 157 ShadowRestrictedLockUtilsInternal.setKeyguardDisabledFeatures( 158 KEYGUARD_DISABLE_SECURE_NOTIFICATIONS); 159 160 mController.displayPreference(mScreen); 161 162 assertThat(mPreference.getValue()).isEqualTo( 163 String.valueOf(R.string.lock_screen_notifs_show_none)); 164 assertThat(mPreference.getSummary()) 165 .isEqualTo(mContext.getString(R.string.lock_screen_notifs_show_none)); 166 167 assertThat(mPreference.isRestrictedForEntry( 168 mContext.getString(R.string.lock_screen_notifs_show_all))).isTrue(); 169 assertThat(mPreference.isRestrictedForEntry( 170 mContext.getString(R.string.lock_screen_notifs_show_alerting))).isTrue(); 171 } 172 173 @Test onPreferenceChange_allToAlerting()174 public void onPreferenceChange_allToAlerting() { 175 Settings.Secure.putInt(mContext.getContentResolver(), 176 LOCK_SCREEN_SHOW_NOTIFICATIONS, 177 1); 178 Settings.Secure.putInt(mContext.getContentResolver(), 179 LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 180 1); 181 182 mController.onPreferenceChange(mPreference, 183 Integer.toString(R.string.lock_screen_notifs_show_alerting)); 184 185 assertThat(Settings.Secure.getInt(mContext.getContentResolver(), 186 LOCK_SCREEN_SHOW_NOTIFICATIONS, 1)).isEqualTo(1); 187 assertThat(Settings.Secure.getInt(mContext.getContentResolver(), 188 LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 1)).isEqualTo(0); 189 190 assertThat(mPreference.getSummary()) 191 .isEqualTo(mContext.getString(R.string.lock_screen_notifs_show_alerting)); 192 } 193 194 @Test onPreferenceChange_noneToAll()195 public void onPreferenceChange_noneToAll() { 196 Settings.Secure.putInt(mContext.getContentResolver(), 197 LOCK_SCREEN_SHOW_NOTIFICATIONS, 198 0); 199 Settings.Secure.putInt(mContext.getContentResolver(), 200 LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 201 0); 202 203 mController.onPreferenceChange(mPreference, 204 Integer.toString(R.string.lock_screen_notifs_show_all)); 205 206 assertThat(Settings.Secure.getInt(mContext.getContentResolver(), 207 LOCK_SCREEN_SHOW_NOTIFICATIONS, 1)).isEqualTo(1); 208 assertThat(Settings.Secure.getInt(mContext.getContentResolver(), 209 LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 1)).isEqualTo(1); 210 211 assertThat(mPreference.getSummary()) 212 .isEqualTo(mContext.getString(R.string.lock_screen_notifs_show_all)); 213 } 214 215 @Test onPreferenceChange_alertingToNone()216 public void onPreferenceChange_alertingToNone() { 217 Settings.Secure.putInt(mContext.getContentResolver(), 218 LOCK_SCREEN_SHOW_NOTIFICATIONS, 219 1); 220 Settings.Secure.putInt(mContext.getContentResolver(), 221 LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 222 0); 223 224 mController.onPreferenceChange(mPreference, 225 Integer.toString(R.string.lock_screen_notifs_show_none)); 226 227 assertThat(Settings.Secure.getInt(mContext.getContentResolver(), 228 LOCK_SCREEN_SHOW_NOTIFICATIONS, 1)).isEqualTo(0); 229 assertThat(Settings.Secure.getInt(mContext.getContentResolver(), 230 LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 1)).isEqualTo(0); 231 232 assertThat(mPreference.getSummary()) 233 .isEqualTo(mContext.getString(R.string.lock_screen_notifs_show_none)); 234 } 235 } 236