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