1 /* 2 * Copyright (C) 2017 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.NotificationChannel.DEFAULT_CHANNEL_ID; 20 import static android.app.NotificationManager.IMPORTANCE_HIGH; 21 import static android.app.NotificationManager.IMPORTANCE_LOW; 22 import static android.app.NotificationManager.IMPORTANCE_NONE; 23 import static android.provider.Settings.Secure.NOTIFICATION_BADGING; 24 import static junit.framework.Assert.assertFalse; 25 import static junit.framework.Assert.assertTrue; 26 import static org.mockito.ArgumentMatchers.any; 27 import static org.mockito.ArgumentMatchers.anyInt; 28 import static org.mockito.ArgumentMatchers.eq; 29 import static org.mockito.Mockito.mock; 30 import static org.mockito.Mockito.spy; 31 import static org.mockito.Mockito.times; 32 import static org.mockito.Mockito.verify; 33 import static org.mockito.Mockito.when; 34 35 import android.app.NotificationChannel; 36 import android.app.NotificationManager; 37 import android.content.Context; 38 import android.os.UserManager; 39 import android.provider.Settings; 40 import android.support.v7.preference.Preference; 41 import android.support.v7.preference.PreferenceScreen; 42 43 import com.android.settings.testutils.SettingsRobolectricTestRunner; 44 import com.android.settingslib.RestrictedLockUtils; 45 import com.android.settingslib.RestrictedSwitchPreference; 46 47 import org.junit.Before; 48 import org.junit.Test; 49 import org.junit.runner.RunWith; 50 import org.mockito.Answers; 51 import org.mockito.Mock; 52 import org.mockito.MockitoAnnotations; 53 import org.robolectric.RuntimeEnvironment; 54 import org.robolectric.shadows.ShadowApplication; 55 56 @RunWith(SettingsRobolectricTestRunner.class) 57 public class BadgePreferenceControllerTest { 58 59 private Context mContext; 60 @Mock 61 private NotificationBackend mBackend; 62 @Mock 63 private NotificationManager mNm; 64 @Mock 65 private UserManager mUm; 66 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 67 private PreferenceScreen mScreen; 68 69 private BadgePreferenceController mController; 70 71 @Before setUp()72 public void setUp() { 73 MockitoAnnotations.initMocks(this); 74 ShadowApplication shadowApplication = ShadowApplication.getInstance(); 75 shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNm); 76 shadowApplication.setSystemService(Context.USER_SERVICE, mUm); 77 mContext = RuntimeEnvironment.application; 78 mController = spy(new BadgePreferenceController(mContext, mBackend)); 79 } 80 81 @Test testNoCrashIfNoOnResume()82 public void testNoCrashIfNoOnResume() { 83 mController.isAvailable(); 84 mController.updateState(mock(RestrictedSwitchPreference.class)); 85 mController.onPreferenceChange(mock(RestrictedSwitchPreference.class), true); 86 } 87 88 @Test testIsAvailable_notIfAppBlocked()89 public void testIsAvailable_notIfAppBlocked() { 90 NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); 91 appRow.banned = true; 92 mController.onResume(appRow, mock(NotificationChannel.class), null, null); 93 assertFalse(mController.isAvailable()); 94 } 95 96 @Test testIsAvailable_notIfChannelBlocked()97 public void testIsAvailable_notIfChannelBlocked() { 98 NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); 99 NotificationChannel channel = mock(NotificationChannel.class); 100 when(channel.getImportance()).thenReturn(IMPORTANCE_NONE); 101 mController.onResume(appRow, channel, null, null); 102 assertFalse(mController.isAvailable()); 103 } 104 105 @Test testIsAvailable_channel_notIfAppOff()106 public void testIsAvailable_channel_notIfAppOff() { 107 NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); 108 appRow.showBadge = false; 109 NotificationChannel channel = mock(NotificationChannel.class); 110 when(channel.getImportance()).thenReturn(IMPORTANCE_HIGH); 111 mController.onResume(appRow, channel, null, null); 112 113 assertFalse(mController.isAvailable()); 114 } 115 116 @Test testIsAvailable_notIfOffGlobally()117 public void testIsAvailable_notIfOffGlobally() { 118 NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); 119 NotificationChannel channel = mock(NotificationChannel.class); 120 when(channel.getImportance()).thenReturn(IMPORTANCE_HIGH); 121 mController.onResume(appRow, channel, null, null); 122 Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BADGING, 0); 123 124 assertFalse(mController.isAvailable()); 125 } 126 127 @Test testIsAvailable_app()128 public void testIsAvailable_app() { 129 NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); 130 mController.onResume(appRow, null, null, null); 131 Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BADGING, 1); 132 133 assertTrue(mController.isAvailable()); 134 } 135 136 @Test testIsAvailable_defaultChannel()137 public void testIsAvailable_defaultChannel() { 138 NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); 139 appRow.showBadge = true; 140 NotificationChannel channel = mock(NotificationChannel.class); 141 when(channel.getImportance()).thenReturn(IMPORTANCE_HIGH); 142 when(channel.getId()).thenReturn(DEFAULT_CHANNEL_ID); 143 mController.onResume(appRow, channel, null, null); 144 Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BADGING, 1); 145 146 assertTrue(mController.isAvailable()); 147 } 148 149 @Test testIsAvailable_channel()150 public void testIsAvailable_channel() { 151 NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); 152 appRow.showBadge = true; 153 NotificationChannel channel = mock(NotificationChannel.class); 154 when(channel.getImportance()).thenReturn(IMPORTANCE_HIGH); 155 mController.onResume(appRow, channel, null, null); 156 Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BADGING, 1); 157 158 assertTrue(mController.isAvailable()); 159 } 160 161 @Test testIsAvailable_channelAppOff()162 public void testIsAvailable_channelAppOff() { 163 NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); 164 appRow.showBadge = false; 165 NotificationChannel channel = mock(NotificationChannel.class); 166 when(channel.getImportance()).thenReturn(IMPORTANCE_HIGH); 167 mController.onResume(appRow, channel, null, null); 168 Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BADGING, 1); 169 170 assertFalse(mController.isAvailable()); 171 } 172 173 @Test testUpdateState_disabledByAdmin()174 public void testUpdateState_disabledByAdmin() { 175 NotificationChannel channel = mock(NotificationChannel.class); 176 when(channel.getId()).thenReturn("something"); 177 mController.onResume(new NotificationBackend.AppRow(), channel, null, 178 mock(RestrictedLockUtils.EnforcedAdmin.class)); 179 180 Preference pref = new RestrictedSwitchPreference(mContext); 181 mController.updateState(pref); 182 183 assertFalse(pref.isEnabled()); 184 } 185 186 @Test testUpdateState_channelNotConfigurable()187 public void testUpdateState_channelNotConfigurable() { 188 String lockedId = "locked"; 189 NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); 190 appRow.lockedChannelId = lockedId; 191 NotificationChannel channel = mock(NotificationChannel.class); 192 when(channel.getId()).thenReturn(lockedId); 193 mController.onResume(appRow, channel, null, null); 194 195 Preference pref = new RestrictedSwitchPreference(mContext); 196 mController.updateState(pref); 197 198 assertFalse(pref.isEnabled()); 199 } 200 201 @Test testUpdateState_channel()202 public void testUpdateState_channel() { 203 NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); 204 appRow.lockedChannelId = "a"; 205 NotificationChannel channel = mock(NotificationChannel.class); 206 when(channel.canShowBadge()).thenReturn(true); 207 mController.onResume(appRow, channel, null, null); 208 209 RestrictedSwitchPreference pref = new RestrictedSwitchPreference(mContext); 210 mController.updateState(pref); 211 212 assertTrue(pref.isChecked()); 213 214 when(channel.canShowBadge()).thenReturn(false); 215 mController.onResume(appRow, channel, null, null); 216 mController.updateState(pref); 217 218 assertFalse(pref.isChecked()); 219 } 220 221 @Test testUpdateState_app()222 public void testUpdateState_app() { 223 NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); 224 appRow.showBadge = true; 225 mController.onResume(appRow, null, null, null); 226 227 RestrictedSwitchPreference pref = new RestrictedSwitchPreference(mContext); 228 mController.updateState(pref); 229 assertTrue(pref.isChecked()); 230 231 appRow.showBadge = false; 232 mController.onResume(appRow, null, null, null); 233 234 mController.updateState(pref); 235 assertFalse(pref.isChecked()); 236 } 237 238 @Test testOnPreferenceChange_on_channel()239 public void testOnPreferenceChange_on_channel() { 240 NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); 241 appRow.showBadge = true; 242 NotificationChannel channel = 243 new NotificationChannel(DEFAULT_CHANNEL_ID, "a", IMPORTANCE_LOW); 244 channel.setShowBadge(false); 245 mController.onResume(appRow, channel, null, null); 246 247 RestrictedSwitchPreference pref = new RestrictedSwitchPreference(mContext); 248 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(pref); 249 mController.displayPreference(mScreen); 250 mController.updateState(pref); 251 252 mController.onPreferenceChange(pref, true); 253 assertTrue(channel.canShowBadge()); 254 verify(mBackend, times(1)).updateChannel(any(), anyInt(), any()); 255 } 256 257 @Test testOnPreferenceChange_off_channel()258 public void testOnPreferenceChange_off_channel() { 259 NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); 260 appRow.showBadge = true; 261 NotificationChannel channel = 262 new NotificationChannel(DEFAULT_CHANNEL_ID, "a", IMPORTANCE_HIGH); 263 channel.setShowBadge(true); 264 mController.onResume(appRow, channel, null, null); 265 266 RestrictedSwitchPreference pref = new RestrictedSwitchPreference(mContext); 267 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(pref); 268 mController.displayPreference(mScreen); 269 mController.updateState(pref); 270 271 mController.onPreferenceChange(pref, false); 272 verify(mBackend, times(1)).updateChannel(any(), anyInt(), any()); 273 assertFalse(channel.canShowBadge()); 274 } 275 276 @Test testOnPreferenceChange_on_app()277 public void testOnPreferenceChange_on_app() { 278 NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); 279 appRow.showBadge = false; 280 mController.onResume(appRow, null, null, null); 281 282 RestrictedSwitchPreference pref = new RestrictedSwitchPreference(mContext); 283 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(pref); 284 mController.displayPreference(mScreen); 285 mController.updateState(pref); 286 287 mController.onPreferenceChange(pref, true); 288 289 assertTrue(appRow.showBadge); 290 verify(mBackend, times(1)).setShowBadge(any(), anyInt(), eq(true)); 291 } 292 293 @Test testOnPreferenceChange_off_app()294 public void testOnPreferenceChange_off_app() { 295 NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); 296 appRow.showBadge = true; 297 mController.onResume(appRow, null, null, null); 298 299 RestrictedSwitchPreference pref = new RestrictedSwitchPreference(mContext); 300 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(pref); 301 mController.displayPreference(mScreen); 302 mController.updateState(pref); 303 304 mController.onPreferenceChange(pref, false); 305 306 assertFalse(appRow.showBadge); 307 verify(mBackend, times(1)).setShowBadge(any(), anyInt(), eq(false)); 308 } 309 } 310