Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | package com.android.server.notification; |
| 17 | |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 18 | import static junit.framework.Assert.assertFalse; |
| 19 | import static junit.framework.Assert.assertNull; |
| 20 | import static junit.framework.Assert.assertTrue; |
| 21 | |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 22 | import com.android.server.lights.Light; |
| 23 | import com.android.server.statusbar.StatusBarManagerInternal; |
| 24 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 25 | import org.junit.Before; |
| 26 | import org.junit.Test; |
| 27 | import org.junit.runner.RunWith; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 28 | |
| 29 | import android.app.ActivityManager; |
| 30 | import android.app.Notification; |
| 31 | import android.app.Notification.Builder; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 32 | import android.app.NotificationManager; |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 33 | import android.content.Context; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 34 | import android.app.NotificationChannel; |
| 35 | import android.graphics.Color; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 36 | import android.media.AudioAttributes; |
| 37 | import android.media.AudioManager; |
| 38 | import android.net.Uri; |
| 39 | import android.os.Handler; |
| 40 | import android.os.RemoteException; |
| 41 | import android.os.UserHandle; |
| 42 | import android.os.Vibrator; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 43 | import android.provider.Settings; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 44 | import android.service.notification.StatusBarNotification; |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 45 | import android.support.test.InstrumentationRegistry; |
| 46 | import android.support.test.runner.AndroidJUnit4; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 47 | import android.test.suitebuilder.annotation.SmallTest; |
| 48 | |
| 49 | import org.mockito.Mock; |
| 50 | import org.mockito.Mockito; |
| 51 | import org.mockito.MockitoAnnotations; |
| 52 | |
| 53 | import static org.mockito.Matchers.anyBoolean; |
| 54 | import static org.mockito.Matchers.anyInt; |
| 55 | import static org.mockito.Matchers.anyObject; |
| 56 | import static org.mockito.Matchers.anyString; |
| 57 | import static org.mockito.Matchers.eq; |
| 58 | import static org.mockito.Mockito.never; |
| 59 | import static org.mockito.Mockito.times; |
| 60 | import static org.mockito.Mockito.verify; |
| 61 | import static org.mockito.Mockito.when; |
| 62 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 63 | @SmallTest |
| 64 | @RunWith(AndroidJUnit4.class) |
| 65 | public class BuzzBeepBlinkTest { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 66 | |
| 67 | @Mock AudioManager mAudioManager; |
| 68 | @Mock Vibrator mVibrator; |
| 69 | @Mock android.media.IRingtonePlayer mRingtonePlayer; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 70 | @Mock StatusBarManagerInternal mStatusBar; |
| 71 | @Mock Light mLight; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 72 | @Mock Handler mHandler; |
| 73 | |
| 74 | private NotificationManagerService mService; |
| 75 | private String mPkg = "com.android.server.notification"; |
| 76 | private int mId = 1001; |
| 77 | private int mOtherId = 1002; |
| 78 | private String mTag = null; |
| 79 | private int mUid = 1000; |
| 80 | private int mPid = 2000; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 81 | private android.os.UserHandle mUser = UserHandle.of(ActivityManager.getCurrentUser()); |
| 82 | |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 83 | private static final long[] CUSTOM_VIBRATION = new long[] { |
| 84 | 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, |
| 85 | 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, |
| 86 | 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400 }; |
| 87 | private static final Uri CUSTOM_SOUND = Settings.System.DEFAULT_ALARM_ALERT_URI; |
| 88 | private static final int CUSTOM_LIGHT_COLOR = Color.BLACK; |
| 89 | private static final int CUSTOM_LIGHT_ON = 10000; |
| 90 | private static final int CUSTOM_LIGHT_OFF = 10000; |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 91 | private static final long[] FALLBACK_VIBRATION = new long[] {100, 100, 100}; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 92 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 93 | @Before |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 94 | public void setUp() { |
| 95 | MockitoAnnotations.initMocks(this); |
| 96 | |
| 97 | when(mAudioManager.isAudioFocusExclusive()).thenReturn(false); |
| 98 | when(mAudioManager.getRingtonePlayer()).thenReturn(mRingtonePlayer); |
| 99 | when(mAudioManager.getStreamVolume(anyInt())).thenReturn(10); |
| 100 | when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_NORMAL); |
| 101 | |
| 102 | mService = new NotificationManagerService(getContext()); |
| 103 | mService.setAudioManager(mAudioManager); |
| 104 | mService.setVibrator(mVibrator); |
| 105 | mService.setSystemReady(true); |
| 106 | mService.setHandler(mHandler); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 107 | mService.setStatusBarManager(mStatusBar); |
| 108 | mService.setLights(mLight); |
| 109 | mService.setScreenOn(false); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 110 | mService.setFallbackVibrationPattern(FALLBACK_VIBRATION); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | // |
| 114 | // Convenience functions for creating notification records |
| 115 | // |
| 116 | |
| 117 | private NotificationRecord getNoisyOtherNotification() { |
| 118 | return getNotificationRecord(mOtherId, false /* insistent */, false /* once */, |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 119 | true /* noisy */, true /* buzzy*/, false /* lights */); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | private NotificationRecord getBeepyNotification() { |
| 123 | return getNotificationRecord(mId, false /* insistent */, false /* once */, |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 124 | true /* noisy */, false /* buzzy*/, false /* lights */); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | private NotificationRecord getBeepyOnceNotification() { |
| 128 | return getNotificationRecord(mId, false /* insistent */, true /* once */, |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 129 | true /* noisy */, false /* buzzy*/, false /* lights */); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | private NotificationRecord getQuietNotification() { |
| 133 | return getNotificationRecord(mId, false /* insistent */, false /* once */, |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 134 | false /* noisy */, false /* buzzy*/, false /* lights */); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | private NotificationRecord getQuietOtherNotification() { |
| 138 | return getNotificationRecord(mOtherId, false /* insistent */, false /* once */, |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 139 | false /* noisy */, false /* buzzy*/, false /* lights */); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | private NotificationRecord getQuietOnceNotification() { |
| 143 | return getNotificationRecord(mId, false /* insistent */, true /* once */, |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 144 | false /* noisy */, false /* buzzy*/, false /* lights */); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | private NotificationRecord getInsistentBeepyNotification() { |
| 148 | return getNotificationRecord(mId, true /* insistent */, false /* once */, |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 149 | true /* noisy */, false /* buzzy*/, false /* lights */); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | private NotificationRecord getBuzzyNotification() { |
| 153 | return getNotificationRecord(mId, false /* insistent */, false /* once */, |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 154 | false /* noisy */, true /* buzzy*/, false /* lights */); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | private NotificationRecord getBuzzyOnceNotification() { |
| 158 | return getNotificationRecord(mId, false /* insistent */, true /* once */, |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 159 | false /* noisy */, true /* buzzy*/, false /* lights */); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | private NotificationRecord getInsistentBuzzyNotification() { |
| 163 | return getNotificationRecord(mId, true /* insistent */, false /* once */, |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 164 | false /* noisy */, true /* buzzy*/, false /* lights */); |
| 165 | } |
| 166 | |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 167 | private NotificationRecord getBuzzyBeepyNotification() { |
| 168 | return getNotificationRecord(mId, false /* insistent */, false /* once */, |
| 169 | true /* noisy */, true /* buzzy*/, false /* lights */); |
| 170 | } |
| 171 | |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 172 | private NotificationRecord getLightsNotification() { |
| 173 | return getNotificationRecord(mId, false /* insistent */, true /* once */, |
| 174 | false /* noisy */, true /* buzzy*/, true /* lights */); |
| 175 | } |
| 176 | |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 177 | private NotificationRecord getCustomLightsNotification() { |
| 178 | return getNotificationRecord(mId, false /* insistent */, true /* once */, |
| 179 | false /* noisy */, true /* buzzy*/, true /* lights */, |
| 180 | true /* defaultVibration */, true /* defaultSound */, false /* defaultLights */); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | private NotificationRecord getNotificationRecord(int id, boolean insistent, boolean once, |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 184 | boolean noisy, boolean buzzy, boolean lights) { |
| 185 | return getNotificationRecord(id, insistent, once, noisy, buzzy, lights, true, true, true); |
| 186 | } |
| 187 | |
| 188 | private NotificationRecord getNotificationRecord(int id, boolean insistent, boolean once, |
| 189 | boolean noisy, boolean buzzy, boolean lights, boolean defaultVibration, |
| 190 | boolean defaultSound, boolean defaultLights) { |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 191 | NotificationChannel channel = |
| 192 | new NotificationChannel("test", "test", NotificationManager.IMPORTANCE_HIGH); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 193 | final Builder builder = new Builder(getContext()) |
| 194 | .setContentTitle("foo") |
| 195 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 196 | .setPriority(Notification.PRIORITY_HIGH) |
| 197 | .setOnlyAlertOnce(once); |
| 198 | |
| 199 | int defaults = 0; |
| 200 | if (noisy) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 201 | if (defaultSound) { |
| 202 | defaults |= Notification.DEFAULT_SOUND; |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 203 | channel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 204 | } else { |
| 205 | builder.setSound(CUSTOM_SOUND); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 206 | channel.setSound(CUSTOM_SOUND); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 207 | } |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 208 | } |
| 209 | if (buzzy) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 210 | if (defaultVibration) { |
| 211 | defaults |= Notification.DEFAULT_VIBRATE; |
| 212 | } else { |
| 213 | builder.setVibrate(CUSTOM_VIBRATION); |
Julia Reynolds | f57de46 | 2016-11-23 11:31:46 -0500 | [diff] [blame^] | 214 | channel.setVibrationPattern(CUSTOM_VIBRATION); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 215 | } |
Julia Reynolds | f57de46 | 2016-11-23 11:31:46 -0500 | [diff] [blame^] | 216 | channel.enableVibration(true); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 217 | } |
| 218 | if (lights) { |
| 219 | if (defaultLights) { |
| 220 | defaults |= Notification.DEFAULT_LIGHTS; |
| 221 | } else { |
| 222 | builder.setLights(CUSTOM_LIGHT_COLOR, CUSTOM_LIGHT_ON, CUSTOM_LIGHT_OFF); |
| 223 | } |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 224 | channel.setLights(true); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 225 | } |
| 226 | builder.setDefaults(defaults); |
| 227 | |
| 228 | Notification n = builder.build(); |
| 229 | if (insistent) { |
| 230 | n.flags |= Notification.FLAG_INSISTENT; |
| 231 | } |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 232 | |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 233 | StatusBarNotification sbn = new StatusBarNotification(mPkg, mPkg, channel, id, mTag, mUid, |
| 234 | mPid, n, mUser, null, System.currentTimeMillis()); |
| 235 | NotificationRecord r = new NotificationRecord(getContext(), sbn); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 236 | mService.addNotification(r); |
| 237 | return r; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | // |
| 241 | // Convenience functions for interacting with mocks |
| 242 | // |
| 243 | |
| 244 | private void verifyNeverBeep() throws RemoteException { |
| 245 | verify(mRingtonePlayer, never()).playAsync((Uri) anyObject(), (UserHandle) anyObject(), |
| 246 | anyBoolean(), (AudioAttributes) anyObject()); |
| 247 | } |
| 248 | |
| 249 | private void verifyBeep() throws RemoteException { |
| 250 | verify(mRingtonePlayer, times(1)).playAsync((Uri) anyObject(), (UserHandle) anyObject(), |
| 251 | eq(true), (AudioAttributes) anyObject()); |
| 252 | } |
| 253 | |
| 254 | private void verifyBeepLooped() throws RemoteException { |
| 255 | verify(mRingtonePlayer, times(1)).playAsync((Uri) anyObject(), (UserHandle) anyObject(), |
| 256 | eq(false), (AudioAttributes) anyObject()); |
| 257 | } |
| 258 | |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 259 | private void verifyCustomBeep() throws RemoteException { |
| 260 | verify(mRingtonePlayer, times(1)).playAsync(eq(CUSTOM_SOUND), (UserHandle) anyObject(), |
| 261 | eq(false), (AudioAttributes) anyObject()); |
| 262 | } |
| 263 | |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 264 | private void verifyNeverStopAudio() throws RemoteException { |
| 265 | verify(mRingtonePlayer, never()).stopAsync(); |
| 266 | } |
| 267 | |
| 268 | private void verifyStopAudio() throws RemoteException { |
| 269 | verify(mRingtonePlayer, times(1)).stopAsync(); |
| 270 | } |
| 271 | |
| 272 | private void verifyNeverVibrate() { |
| 273 | verify(mVibrator, never()).vibrate(anyInt(), anyString(), (long[]) anyObject(), |
| 274 | anyInt(), (AudioAttributes) anyObject()); |
| 275 | } |
| 276 | |
| 277 | private void verifyVibrate() { |
| 278 | verify(mVibrator, times(1)).vibrate(anyInt(), anyString(), (long[]) anyObject(), |
| 279 | eq(-1), (AudioAttributes) anyObject()); |
| 280 | } |
| 281 | |
| 282 | private void verifyVibrateLooped() { |
| 283 | verify(mVibrator, times(1)).vibrate(anyInt(), anyString(), (long[]) anyObject(), |
| 284 | eq(0), (AudioAttributes) anyObject()); |
| 285 | } |
| 286 | |
| 287 | private void verifyStopVibrate() { |
| 288 | verify(mVibrator, times(1)).cancel(); |
| 289 | } |
| 290 | |
| 291 | private void verifyNeverStopVibrate() throws RemoteException { |
| 292 | verify(mVibrator, never()).cancel(); |
| 293 | } |
| 294 | |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 295 | private void verifyLights() { |
| 296 | verify(mStatusBar, times(1)).notificationLightPulse(anyInt(), anyInt(), anyInt()); |
| 297 | } |
| 298 | |
| 299 | private void verifyCustomLights() { |
| 300 | verify(mStatusBar, times(1)).notificationLightPulse( |
| 301 | eq(CUSTOM_LIGHT_COLOR), eq(CUSTOM_LIGHT_ON), eq(CUSTOM_LIGHT_OFF)); |
| 302 | } |
| 303 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 304 | private Context getContext() { |
| 305 | return InstrumentationRegistry.getTargetContext(); |
| 306 | } |
| 307 | |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 308 | // |
| 309 | // Tests |
| 310 | // |
| 311 | |
| 312 | @Test |
| 313 | public void testLights() throws Exception { |
| 314 | NotificationRecord r = getLightsNotification(); |
| 315 | r.setImportance(NotificationManager.IMPORTANCE_DEFAULT, "for testing"); |
| 316 | |
| 317 | mService.buzzBeepBlinkLocked(r); |
| 318 | |
| 319 | verifyLights(); |
| 320 | } |
| 321 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 322 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 323 | public void testBeep() throws Exception { |
| 324 | NotificationRecord r = getBeepyNotification(); |
| 325 | |
| 326 | mService.buzzBeepBlinkLocked(r); |
| 327 | |
| 328 | verifyBeepLooped(); |
| 329 | verifyNeverVibrate(); |
| 330 | } |
| 331 | |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 332 | @Test |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 333 | public void testLightsFromChannel() throws Exception { |
| 334 | NotificationRecord r = getQuietNotification(); |
| 335 | r.setImportance(NotificationManager.IMPORTANCE_DEFAULT, "for testing"); |
| 336 | r.getChannel().setLights(true); |
| 337 | |
| 338 | mService.buzzBeepBlinkLocked(r); |
| 339 | |
| 340 | verifyLights(); |
| 341 | } |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 342 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 343 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 344 | public void testBeepInsistently() throws Exception { |
| 345 | NotificationRecord r = getInsistentBeepyNotification(); |
| 346 | |
| 347 | mService.buzzBeepBlinkLocked(r); |
| 348 | |
| 349 | verifyBeep(); |
| 350 | } |
| 351 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 352 | @Test |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 353 | public void testChannelNoOverwriteCustomLights() throws Exception { |
| 354 | NotificationRecord r = getCustomLightsNotification(); |
| 355 | r.getChannel().setLights(true); |
| 356 | |
| 357 | mService.buzzBeepBlinkLocked(r); |
| 358 | |
| 359 | verifyCustomLights(); |
| 360 | } |
| 361 | |
| 362 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 363 | public void testNoInterruptionForMin() throws Exception { |
| 364 | NotificationRecord r = getBeepyNotification(); |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 365 | r.setImportance(NotificationManager.IMPORTANCE_MIN, "foo"); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 366 | |
| 367 | mService.buzzBeepBlinkLocked(r); |
| 368 | |
| 369 | verifyNeverBeep(); |
| 370 | verifyNeverVibrate(); |
| 371 | } |
| 372 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 373 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 374 | public void testNoInterruptionForIntercepted() throws Exception { |
| 375 | NotificationRecord r = getBeepyNotification(); |
| 376 | r.setIntercepted(true); |
| 377 | |
| 378 | mService.buzzBeepBlinkLocked(r); |
| 379 | |
| 380 | verifyNeverBeep(); |
| 381 | verifyNeverVibrate(); |
| 382 | } |
| 383 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 384 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 385 | public void testBeepTwice() throws Exception { |
| 386 | NotificationRecord r = getBeepyNotification(); |
| 387 | |
| 388 | // set up internal state |
| 389 | mService.buzzBeepBlinkLocked(r); |
| 390 | Mockito.reset(mRingtonePlayer); |
| 391 | |
| 392 | // update should beep |
| 393 | r.isUpdate = true; |
| 394 | mService.buzzBeepBlinkLocked(r); |
| 395 | verifyBeepLooped(); |
| 396 | } |
| 397 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 398 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 399 | public void testHonorAlertOnlyOnceForBeep() throws Exception { |
| 400 | NotificationRecord r = getBeepyNotification(); |
| 401 | NotificationRecord s = getBeepyOnceNotification(); |
| 402 | s.isUpdate = true; |
| 403 | |
| 404 | // set up internal state |
| 405 | mService.buzzBeepBlinkLocked(r); |
| 406 | Mockito.reset(mRingtonePlayer); |
| 407 | |
| 408 | // update should not beep |
| 409 | mService.buzzBeepBlinkLocked(s); |
| 410 | verifyNeverBeep(); |
| 411 | } |
| 412 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 413 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 414 | public void testNoisyUpdateDoesNotCancelAudio() throws Exception { |
| 415 | NotificationRecord r = getBeepyNotification(); |
| 416 | |
| 417 | mService.buzzBeepBlinkLocked(r); |
| 418 | r.isUpdate = true; |
| 419 | mService.buzzBeepBlinkLocked(r); |
| 420 | |
| 421 | verifyNeverStopAudio(); |
| 422 | } |
| 423 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 424 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 425 | public void testNoisyOnceUpdateDoesNotCancelAudio() throws Exception { |
| 426 | NotificationRecord r = getBeepyNotification(); |
| 427 | NotificationRecord s = getBeepyOnceNotification(); |
| 428 | s.isUpdate = true; |
| 429 | |
| 430 | mService.buzzBeepBlinkLocked(r); |
| 431 | mService.buzzBeepBlinkLocked(s); |
| 432 | |
| 433 | verifyNeverStopAudio(); |
| 434 | } |
| 435 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 436 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 437 | public void testQuietUpdateDoesNotCancelAudioFromOther() throws Exception { |
| 438 | NotificationRecord r = getBeepyNotification(); |
| 439 | NotificationRecord s = getQuietNotification(); |
| 440 | s.isUpdate = true; |
| 441 | NotificationRecord other = getNoisyOtherNotification(); |
| 442 | |
| 443 | // set up internal state |
| 444 | mService.buzzBeepBlinkLocked(r); |
| 445 | mService.buzzBeepBlinkLocked(other); // this takes the audio stream |
| 446 | Mockito.reset(mRingtonePlayer); |
| 447 | |
| 448 | // should not stop noise, since we no longer own it |
| 449 | mService.buzzBeepBlinkLocked(s); // this no longer owns the stream |
| 450 | verifyNeverStopAudio(); |
| 451 | } |
| 452 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 453 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 454 | public void testQuietInterloperDoesNotCancelAudio() throws Exception { |
| 455 | NotificationRecord r = getBeepyNotification(); |
| 456 | NotificationRecord other = getQuietOtherNotification(); |
| 457 | |
| 458 | // set up internal state |
| 459 | mService.buzzBeepBlinkLocked(r); |
| 460 | Mockito.reset(mRingtonePlayer); |
| 461 | |
| 462 | // should not stop noise, since it does not own it |
| 463 | mService.buzzBeepBlinkLocked(other); |
| 464 | verifyNeverStopAudio(); |
| 465 | } |
| 466 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 467 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 468 | public void testQuietUpdateCancelsAudio() throws Exception { |
| 469 | NotificationRecord r = getBeepyNotification(); |
| 470 | NotificationRecord s = getQuietNotification(); |
| 471 | s.isUpdate = true; |
| 472 | |
| 473 | // set up internal state |
| 474 | mService.buzzBeepBlinkLocked(r); |
| 475 | Mockito.reset(mRingtonePlayer); |
| 476 | |
| 477 | // quiet update should stop making noise |
| 478 | mService.buzzBeepBlinkLocked(s); |
| 479 | verifyStopAudio(); |
| 480 | } |
| 481 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 482 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 483 | public void testQuietOnceUpdateCancelsAudio() throws Exception { |
| 484 | NotificationRecord r = getBeepyNotification(); |
| 485 | NotificationRecord s = getQuietOnceNotification(); |
| 486 | s.isUpdate = true; |
| 487 | |
| 488 | // set up internal state |
| 489 | mService.buzzBeepBlinkLocked(r); |
| 490 | Mockito.reset(mRingtonePlayer); |
| 491 | |
| 492 | // stop making noise - this is a weird corner case, but quiet should override once |
| 493 | mService.buzzBeepBlinkLocked(s); |
| 494 | verifyStopAudio(); |
| 495 | } |
| 496 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 497 | @Test |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 498 | public void testNoDemoteSoundToVibrateIfVibrateGiven() throws Exception { |
| 499 | NotificationRecord r = getBuzzyBeepyNotification(); |
| 500 | assertTrue(r.getSound() != null); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 501 | |
| 502 | // the phone is quiet |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 503 | when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_VIBRATE); |
| 504 | |
| 505 | mService.buzzBeepBlinkLocked(r); |
| 506 | |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 507 | verify(mVibrator, times(1)).vibrate(anyInt(), anyString(), eq(r.getVibration()), |
| 508 | eq(-1), (AudioAttributes) anyObject()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 509 | } |
| 510 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 511 | @Test |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 512 | public void testDemoteSoundToVibrate() throws Exception { |
| 513 | NotificationRecord r = getBeepyNotification(); |
| 514 | assertTrue(r.getSound() != null); |
| 515 | assertNull(r.getVibration()); |
| 516 | |
| 517 | // the phone is quiet |
| 518 | when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_VIBRATE); |
| 519 | |
| 520 | mService.buzzBeepBlinkLocked(r); |
| 521 | |
| 522 | verify(mVibrator, times(1)).vibrate(anyInt(), anyString(), eq(FALLBACK_VIBRATION), |
| 523 | eq(-1), (AudioAttributes) anyObject()); |
| 524 | } |
| 525 | |
| 526 | @Test |
| 527 | public void testDemoteInsistentSoundToVibrate() throws Exception { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 528 | NotificationRecord r = getInsistentBeepyNotification(); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 529 | assertTrue(r.getSound() != null); |
| 530 | assertNull(r.getVibration()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 531 | |
| 532 | // the phone is quiet |
| 533 | when(mAudioManager.getStreamVolume(anyInt())).thenReturn(0); |
| 534 | when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_VIBRATE); |
| 535 | |
| 536 | mService.buzzBeepBlinkLocked(r); |
| 537 | |
| 538 | verifyVibrateLooped(); |
| 539 | } |
| 540 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 541 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 542 | public void testVibrate() throws Exception { |
| 543 | NotificationRecord r = getBuzzyNotification(); |
| 544 | |
| 545 | mService.buzzBeepBlinkLocked(r); |
| 546 | |
| 547 | verifyNeverBeep(); |
| 548 | verifyVibrate(); |
| 549 | } |
| 550 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 551 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 552 | public void testInsistenteVibrate() throws Exception { |
| 553 | NotificationRecord r = getInsistentBuzzyNotification(); |
| 554 | |
| 555 | mService.buzzBeepBlinkLocked(r); |
| 556 | verifyVibrateLooped(); |
| 557 | } |
| 558 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 559 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 560 | public void testVibratTwice() throws Exception { |
| 561 | NotificationRecord r = getBuzzyNotification(); |
| 562 | |
| 563 | // set up internal state |
| 564 | mService.buzzBeepBlinkLocked(r); |
| 565 | Mockito.reset(mVibrator); |
| 566 | |
| 567 | // update should vibrate |
| 568 | r.isUpdate = true; |
| 569 | mService.buzzBeepBlinkLocked(r); |
| 570 | verifyVibrate(); |
| 571 | } |
| 572 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 573 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 574 | public void testHonorAlertOnlyOnceForBuzz() throws Exception { |
| 575 | NotificationRecord r = getBuzzyNotification(); |
| 576 | NotificationRecord s = getBuzzyOnceNotification(); |
| 577 | s.isUpdate = true; |
| 578 | |
| 579 | // set up internal state |
| 580 | mService.buzzBeepBlinkLocked(r); |
| 581 | Mockito.reset(mVibrator); |
| 582 | |
| 583 | // update should not beep |
| 584 | mService.buzzBeepBlinkLocked(s); |
| 585 | verifyNeverVibrate(); |
| 586 | } |
| 587 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 588 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 589 | public void testNoisyUpdateDoesNotCancelVibrate() throws Exception { |
| 590 | NotificationRecord r = getBuzzyNotification(); |
| 591 | |
| 592 | mService.buzzBeepBlinkLocked(r); |
| 593 | r.isUpdate = true; |
| 594 | mService.buzzBeepBlinkLocked(r); |
| 595 | |
| 596 | verifyNeverStopVibrate(); |
| 597 | } |
| 598 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 599 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 600 | public void testNoisyOnceUpdateDoesNotCancelVibrate() throws Exception { |
| 601 | NotificationRecord r = getBuzzyNotification(); |
| 602 | NotificationRecord s = getBuzzyOnceNotification(); |
| 603 | s.isUpdate = true; |
| 604 | |
| 605 | mService.buzzBeepBlinkLocked(r); |
| 606 | mService.buzzBeepBlinkLocked(s); |
| 607 | |
| 608 | verifyNeverStopVibrate(); |
| 609 | } |
| 610 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 611 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 612 | public void testQuietUpdateDoesNotCancelVibrateFromOther() throws Exception { |
| 613 | NotificationRecord r = getBuzzyNotification(); |
| 614 | NotificationRecord s = getQuietNotification(); |
| 615 | s.isUpdate = true; |
| 616 | NotificationRecord other = getNoisyOtherNotification(); |
| 617 | |
| 618 | // set up internal state |
| 619 | mService.buzzBeepBlinkLocked(r); |
| 620 | mService.buzzBeepBlinkLocked(other); // this takes the vibrate stream |
| 621 | Mockito.reset(mVibrator); |
| 622 | |
| 623 | // should not stop vibrate, since we no longer own it |
| 624 | mService.buzzBeepBlinkLocked(s); // this no longer owns the stream |
| 625 | verifyNeverStopVibrate(); |
| 626 | } |
| 627 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 628 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 629 | public void testQuietInterloperDoesNotCancelVibrate() throws Exception { |
| 630 | NotificationRecord r = getBuzzyNotification(); |
| 631 | NotificationRecord other = getQuietOtherNotification(); |
| 632 | |
| 633 | // set up internal state |
| 634 | mService.buzzBeepBlinkLocked(r); |
| 635 | Mockito.reset(mVibrator); |
| 636 | |
| 637 | // should not stop noise, since it does not own it |
| 638 | mService.buzzBeepBlinkLocked(other); |
| 639 | verifyNeverStopVibrate(); |
| 640 | } |
| 641 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 642 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 643 | public void testQuietUpdateCancelsVibrate() throws Exception { |
| 644 | NotificationRecord r = getBuzzyNotification(); |
| 645 | NotificationRecord s = getQuietNotification(); |
| 646 | s.isUpdate = true; |
| 647 | |
| 648 | // set up internal state |
| 649 | mService.buzzBeepBlinkLocked(r); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 650 | verifyVibrate(); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 651 | |
| 652 | // quiet update should stop making noise |
| 653 | mService.buzzBeepBlinkLocked(s); |
| 654 | verifyStopVibrate(); |
| 655 | } |
| 656 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 657 | @Test |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 658 | public void testQuietOnceUpdateCancelVibrate() throws Exception { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 659 | NotificationRecord r = getBuzzyNotification(); |
| 660 | NotificationRecord s = getQuietOnceNotification(); |
| 661 | s.isUpdate = true; |
| 662 | |
| 663 | // set up internal state |
| 664 | mService.buzzBeepBlinkLocked(r); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 665 | verifyVibrate(); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 666 | |
| 667 | // stop making noise - this is a weird corner case, but quiet should override once |
| 668 | mService.buzzBeepBlinkLocked(s); |
| 669 | verifyStopVibrate(); |
| 670 | } |
| 671 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 672 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 673 | public void testQuietUpdateCancelsDemotedVibrate() throws Exception { |
| 674 | NotificationRecord r = getBeepyNotification(); |
| 675 | NotificationRecord s = getQuietNotification(); |
| 676 | |
| 677 | // the phone is quiet |
| 678 | when(mAudioManager.getStreamVolume(anyInt())).thenReturn(0); |
| 679 | when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_VIBRATE); |
| 680 | |
| 681 | mService.buzzBeepBlinkLocked(r); |
| 682 | |
| 683 | // quiet update should stop making noise |
| 684 | mService.buzzBeepBlinkLocked(s); |
| 685 | verifyStopVibrate(); |
| 686 | } |
| 687 | } |