[go: nahoru, domu]

blob: 6061d51f3d7922b389b5db052ae3d27014d276ef [file] [log] [blame]
Chris Wren93bb8b82016-03-29 14:35:05 -04001/*
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 */
16package com.android.server.notification;
17
Julia Reynoldsa79c3712017-04-21 10:29:57 -040018import static android.app.Notification.GROUP_ALERT_ALL;
19import static android.app.Notification.GROUP_ALERT_CHILDREN;
20import static android.app.Notification.GROUP_ALERT_SUMMARY;
Julia Reynoldsca80d162017-04-23 12:39:32 -040021import static android.app.NotificationManager.IMPORTANCE_HIGH;
Julia Reynolds28149f62018-07-03 10:43:35 -040022import static android.app.NotificationManager.IMPORTANCE_LOW;
Julia Reynolds94187562017-10-10 13:58:49 -040023import static android.app.NotificationManager.IMPORTANCE_MIN;
Julia Reynolds28149f62018-07-03 10:43:35 -040024import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_LIGHTS;
Julia Reynoldsca80d162017-04-23 12:39:32 -040025
Julia Reynolds1fac86e2018-03-07 08:30:37 -050026import static junit.framework.Assert.assertFalse;
Julia Reynolds0c299d42016-11-15 14:37:04 -050027import static junit.framework.Assert.assertNull;
28import static junit.framework.Assert.assertTrue;
29
Gus Prevas7306b902018-12-11 10:57:06 -050030import static org.junit.Assert.assertEquals;
31import static org.junit.Assert.assertNotEquals;
Julia Reynolds76c096d2017-06-19 08:16:04 -040032import static org.mockito.Matchers.any;
Chris Wren93bb8b82016-03-29 14:35:05 -040033import static org.mockito.Matchers.anyBoolean;
34import static org.mockito.Matchers.anyInt;
35import static org.mockito.Matchers.anyObject;
36import static org.mockito.Matchers.anyString;
Michael Wright71216972017-01-31 18:33:54 +000037import static org.mockito.Matchers.argThat;
Chris Wren93bb8b82016-03-29 14:35:05 -040038import static org.mockito.Matchers.eq;
39import static org.mockito.Mockito.never;
Tony Mantlerab55f0f2017-06-16 10:50:00 -070040import static org.mockito.Mockito.spy;
Jean-Michel Triviea0eb5f2017-05-25 18:32:40 -070041import static org.mockito.Mockito.timeout;
Chris Wren93bb8b82016-03-29 14:35:05 -040042import static org.mockito.Mockito.times;
43import static org.mockito.Mockito.verify;
44import static org.mockito.Mockito.when;
45
Tony Mantlerab55f0f2017-06-16 10:50:00 -070046import android.app.ActivityManager;
47import android.app.Notification;
48import android.app.Notification.Builder;
49import android.app.NotificationChannel;
50import android.app.NotificationManager;
Qiao (Adora) Zhanga9d0bff2019-04-15 17:45:43 +000051import android.content.Context;
Tony Mantlerab55f0f2017-06-16 10:50:00 -070052import android.content.pm.PackageManager;
53import android.graphics.Color;
54import android.media.AudioAttributes;
55import android.media.AudioManager;
56import android.net.Uri;
57import android.os.Handler;
58import android.os.RemoteException;
59import android.os.UserHandle;
60import android.os.VibrationEffect;
61import android.os.Vibrator;
62import android.provider.Settings;
Julia Reynoldsdb7081e2019-01-03 14:35:38 -050063import android.service.notification.NotificationListenerService;
Tony Mantlerab55f0f2017-06-16 10:50:00 -070064import android.service.notification.StatusBarNotification;
Tony Mantlerab55f0f2017-06-16 10:50:00 -070065import android.test.suitebuilder.annotation.SmallTest;
Julia Reynolds94187562017-10-10 13:58:49 -040066import android.view.accessibility.AccessibilityEvent;
67import android.view.accessibility.AccessibilityManager;
68import android.view.accessibility.IAccessibilityManager;
69import android.view.accessibility.IAccessibilityManagerClient;
Tony Mantlerab55f0f2017-06-16 10:50:00 -070070
Brett Chabot84151d92019-02-27 15:37:59 -080071import androidx.test.runner.AndroidJUnit4;
72
Julia Reynolds94187562017-10-10 13:58:49 -040073import com.android.internal.util.IntPair;
Jason Monk74f5e362017-12-06 08:56:33 -050074import com.android.server.UiServiceTestCase;
Tony Mantlerab55f0f2017-06-16 10:50:00 -070075import com.android.server.lights.Light;
76
77import org.junit.Before;
78import org.junit.Test;
79import org.junit.runner.RunWith;
80import org.mockito.ArgumentMatcher;
81import org.mockito.Mock;
82import org.mockito.Mockito;
83import org.mockito.MockitoAnnotations;
84
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040085@SmallTest
86@RunWith(AndroidJUnit4.class)
Jason Monk74f5e362017-12-06 08:56:33 -050087public class BuzzBeepBlinkTest extends UiServiceTestCase {
Chris Wren93bb8b82016-03-29 14:35:05 -040088
89 @Mock AudioManager mAudioManager;
90 @Mock Vibrator mVibrator;
91 @Mock android.media.IRingtonePlayer mRingtonePlayer;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -040092 @Mock Light mLight;
Julia Reynoldseb3dca72017-07-11 10:39:58 -040093 @Mock
94 NotificationManagerService.WorkerHandler mHandler;
Julia Reynolds76c096d2017-06-19 08:16:04 -040095 @Mock
96 NotificationUsageStats mUsageStats;
Julia Reynolds94187562017-10-10 13:58:49 -040097 @Mock
98 IAccessibilityManager mAccessibilityService;
Chris Wren93bb8b82016-03-29 14:35:05 -040099
100 private NotificationManagerService mService;
101 private String mPkg = "com.android.server.notification";
102 private int mId = 1001;
103 private int mOtherId = 1002;
104 private String mTag = null;
105 private int mUid = 1000;
106 private int mPid = 2000;
Chris Wren93bb8b82016-03-29 14:35:05 -0400107 private android.os.UserHandle mUser = UserHandle.of(ActivityManager.getCurrentUser());
108
Michael Wright71216972017-01-31 18:33:54 +0000109 private VibrateRepeatMatcher mVibrateOnceMatcher = new VibrateRepeatMatcher(-1);
110 private VibrateRepeatMatcher mVibrateLoopMatcher = new VibrateRepeatMatcher(0);
111
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400112 private static final long[] CUSTOM_VIBRATION = new long[] {
113 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400,
114 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400,
115 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400 };
116 private static final Uri CUSTOM_SOUND = Settings.System.DEFAULT_ALARM_ALERT_URI;
Julia Reynolds619a69f2017-01-27 15:11:38 -0500117 private static final AudioAttributes CUSTOM_ATTRIBUTES = new AudioAttributes.Builder()
118 .setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
Julia Reynoldsdb7081e2019-01-03 14:35:38 -0500119 .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
Julia Reynolds619a69f2017-01-27 15:11:38 -0500120 .build();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400121 private static final int CUSTOM_LIGHT_COLOR = Color.BLACK;
122 private static final int CUSTOM_LIGHT_ON = 10000;
123 private static final int CUSTOM_LIGHT_OFF = 10000;
Michael Wright71216972017-01-31 18:33:54 +0000124 private static final long[] FALLBACK_VIBRATION_PATTERN = new long[] {100, 100, 100};
125 private static final VibrationEffect FALLBACK_VIBRATION =
126 VibrationEffect.createWaveform(FALLBACK_VIBRATION_PATTERN, -1);
Jean-Michel Triviea0eb5f2017-05-25 18:32:40 -0700127 private static final int MAX_VIBRATION_DELAY = 1000;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400128
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400129 @Before
Julia Reynolds94187562017-10-10 13:58:49 -0400130 public void setUp() throws Exception {
Chris Wren93bb8b82016-03-29 14:35:05 -0400131 MockitoAnnotations.initMocks(this);
132
133 when(mAudioManager.isAudioFocusExclusive()).thenReturn(false);
134 when(mAudioManager.getRingtonePlayer()).thenReturn(mRingtonePlayer);
135 when(mAudioManager.getStreamVolume(anyInt())).thenReturn(10);
136 when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_NORMAL);
Julia Reynolds76c096d2017-06-19 08:16:04 -0400137 when(mUsageStats.isAlertRateLimited(any())).thenReturn(false);
138
Julia Reynolds94187562017-10-10 13:58:49 -0400139 long serviceReturnValue = IntPair.of(
140 AccessibilityManager.STATE_FLAG_ACCESSIBILITY_ENABLED,
141 AccessibilityEvent.TYPES_ALL_MASK);
142 when(mAccessibilityService.addClient(any(), anyInt())).thenReturn(serviceReturnValue);
143 AccessibilityManager accessibilityManager =
144 new AccessibilityManager(Handler.getMain(), mAccessibilityService, 0);
145 verify(mAccessibilityService).addClient(any(IAccessibilityManagerClient.class), anyInt());
146 assertTrue(accessibilityManager.isEnabled());
147
Qiao (Adora) Zhanga9d0bff2019-04-15 17:45:43 +0000148 mService = spy(new NotificationManagerService(getContext()));
Chris Wren93bb8b82016-03-29 14:35:05 -0400149 mService.setAudioManager(mAudioManager);
150 mService.setVibrator(mVibrator);
151 mService.setSystemReady(true);
152 mService.setHandler(mHandler);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400153 mService.setLights(mLight);
154 mService.setScreenOn(false);
Michael Wright71216972017-01-31 18:33:54 +0000155 mService.setFallbackVibrationPattern(FALLBACK_VIBRATION_PATTERN);
Julia Reynolds76c096d2017-06-19 08:16:04 -0400156 mService.setUsageStats(mUsageStats);
Julia Reynolds94187562017-10-10 13:58:49 -0400157 mService.setAccessibilityManager(accessibilityManager);
Julia Reynolds28149f62018-07-03 10:43:35 -0400158 mService.mScreenOn = false;
159 mService.mInCall = false;
160 mService.mNotificationPulseEnabled = true;
Chris Wren93bb8b82016-03-29 14:35:05 -0400161 }
162
163 //
164 // Convenience functions for creating notification records
165 //
166
167 private NotificationRecord getNoisyOtherNotification() {
168 return getNotificationRecord(mOtherId, false /* insistent */, false /* once */,
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400169 true /* noisy */, true /* buzzy*/, false /* lights */);
Chris Wren93bb8b82016-03-29 14:35:05 -0400170 }
171
172 private NotificationRecord getBeepyNotification() {
173 return getNotificationRecord(mId, false /* insistent */, false /* once */,
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400174 true /* noisy */, false /* buzzy*/, false /* lights */);
Chris Wren93bb8b82016-03-29 14:35:05 -0400175 }
176
177 private NotificationRecord getBeepyOnceNotification() {
178 return getNotificationRecord(mId, false /* insistent */, true /* once */,
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400179 true /* noisy */, false /* buzzy*/, false /* lights */);
Chris Wren93bb8b82016-03-29 14:35:05 -0400180 }
181
182 private NotificationRecord getQuietNotification() {
183 return getNotificationRecord(mId, false /* insistent */, false /* once */,
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400184 false /* noisy */, false /* buzzy*/, false /* lights */);
Chris Wren93bb8b82016-03-29 14:35:05 -0400185 }
186
187 private NotificationRecord getQuietOtherNotification() {
188 return getNotificationRecord(mOtherId, false /* insistent */, false /* once */,
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400189 false /* noisy */, false /* buzzy*/, false /* lights */);
Chris Wren93bb8b82016-03-29 14:35:05 -0400190 }
191
192 private NotificationRecord getQuietOnceNotification() {
193 return getNotificationRecord(mId, false /* insistent */, true /* once */,
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400194 false /* noisy */, false /* buzzy*/, false /* lights */);
Chris Wren93bb8b82016-03-29 14:35:05 -0400195 }
196
197 private NotificationRecord getInsistentBeepyNotification() {
198 return getNotificationRecord(mId, true /* insistent */, false /* once */,
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400199 true /* noisy */, false /* buzzy*/, false /* lights */);
Chris Wren93bb8b82016-03-29 14:35:05 -0400200 }
201
Tyler Gunn48f86272018-07-03 12:38:49 -0700202 private NotificationRecord getInsistentBeepyOnceNotification() {
203 return getNotificationRecord(mId, true /* insistent */, true /* once */,
204 true /* noisy */, false /* buzzy*/, false /* lights */);
205 }
206
Tony Mantlerab55f0f2017-06-16 10:50:00 -0700207 private NotificationRecord getInsistentBeepyLeanbackNotification() {
208 return getLeanbackNotificationRecord(mId, true /* insistent */, false /* once */,
209 true /* noisy */, false /* buzzy*/, false /* lights */);
210 }
211
Chris Wren93bb8b82016-03-29 14:35:05 -0400212 private NotificationRecord getBuzzyNotification() {
213 return getNotificationRecord(mId, false /* insistent */, false /* once */,
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400214 false /* noisy */, true /* buzzy*/, false /* lights */);
Chris Wren93bb8b82016-03-29 14:35:05 -0400215 }
216
217 private NotificationRecord getBuzzyOnceNotification() {
218 return getNotificationRecord(mId, false /* insistent */, true /* once */,
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400219 false /* noisy */, true /* buzzy*/, false /* lights */);
Chris Wren93bb8b82016-03-29 14:35:05 -0400220 }
221
222 private NotificationRecord getInsistentBuzzyNotification() {
223 return getNotificationRecord(mId, true /* insistent */, false /* once */,
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400224 false /* noisy */, true /* buzzy*/, false /* lights */);
225 }
226
Julia Reynolds0c299d42016-11-15 14:37:04 -0500227 private NotificationRecord getBuzzyBeepyNotification() {
228 return getNotificationRecord(mId, false /* insistent */, false /* once */,
229 true /* noisy */, true /* buzzy*/, false /* lights */);
230 }
231
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400232 private NotificationRecord getLightsNotification() {
Julia Reynolds28149f62018-07-03 10:43:35 -0400233 return getNotificationRecord(mId, false /* insistent */, false /* once */,
234 false /* noisy */, false /* buzzy*/, true /* lights */);
235 }
236
237 private NotificationRecord getLightsOnceNotification() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400238 return getNotificationRecord(mId, false /* insistent */, true /* once */,
Julia Reynolds28149f62018-07-03 10:43:35 -0400239 false /* noisy */, false /* buzzy*/, true /* lights */);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400240 }
241
Julia Reynoldsdb7081e2019-01-03 14:35:38 -0500242 private NotificationRecord getCallRecord(int id, boolean insistent) {
243 return getNotificationRecord(id, false, false /* once */, true /* noisy */,
244 false /* buzzy */, false /* lights */, false /* default vib */,
245 false /* default sound */, false /* default lights */, "",
246 Notification.GROUP_ALERT_ALL, false);
Chris Wren93bb8b82016-03-29 14:35:05 -0400247 }
248
249 private NotificationRecord getNotificationRecord(int id, boolean insistent, boolean once,
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400250 boolean noisy, boolean buzzy, boolean lights) {
Julia Reynoldsa79c3712017-04-21 10:29:57 -0400251 return getNotificationRecord(id, insistent, once, noisy, buzzy, lights, true, true, true,
Tony Mantlerab55f0f2017-06-16 10:50:00 -0700252 null, Notification.GROUP_ALERT_ALL, false);
253 }
254
255 private NotificationRecord getLeanbackNotificationRecord(int id, boolean insistent, boolean once,
256 boolean noisy, boolean buzzy, boolean lights) {
257 return getNotificationRecord(id, insistent, once, noisy, buzzy, lights, true, true, true,
258 null, Notification.GROUP_ALERT_ALL, true);
Julia Reynoldsa79c3712017-04-21 10:29:57 -0400259 }
260
261 private NotificationRecord getBeepyNotificationRecord(String groupKey, int groupAlertBehavior) {
262 return getNotificationRecord(mId, false, false, true, false, false, true, true, true,
Tony Mantlerab55f0f2017-06-16 10:50:00 -0700263 groupKey, groupAlertBehavior, false);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400264 }
265
Julia Reynolds28149f62018-07-03 10:43:35 -0400266 private NotificationRecord getLightsNotificationRecord(String groupKey,
267 int groupAlertBehavior) {
268 return getNotificationRecord(mId, false, false, false, false, true /*lights*/, true, true,
269 true, groupKey, groupAlertBehavior, false);
270 }
271
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400272 private NotificationRecord getNotificationRecord(int id, boolean insistent, boolean once,
273 boolean noisy, boolean buzzy, boolean lights, boolean defaultVibration,
Tony Mantlerab55f0f2017-06-16 10:50:00 -0700274 boolean defaultSound, boolean defaultLights, String groupKey, int groupAlertBehavior,
275 boolean isLeanback) {
Julia Reynolds0c299d42016-11-15 14:37:04 -0500276 NotificationChannel channel =
Julia Reynoldsca80d162017-04-23 12:39:32 -0400277 new NotificationChannel("test", "test", IMPORTANCE_HIGH);
Qiao (Adora) Zhanga9d0bff2019-04-15 17:45:43 +0000278 final Builder builder = new Builder(getContext())
Chris Wren93bb8b82016-03-29 14:35:05 -0400279 .setContentTitle("foo")
280 .setSmallIcon(android.R.drawable.sym_def_app_icon)
281 .setPriority(Notification.PRIORITY_HIGH)
282 .setOnlyAlertOnce(once);
283
284 int defaults = 0;
285 if (noisy) {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400286 if (defaultSound) {
287 defaults |= Notification.DEFAULT_SOUND;
Julia Reynolds619a69f2017-01-27 15:11:38 -0500288 channel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI,
289 Notification.AUDIO_ATTRIBUTES_DEFAULT);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400290 } else {
291 builder.setSound(CUSTOM_SOUND);
Julia Reynolds619a69f2017-01-27 15:11:38 -0500292 channel.setSound(CUSTOM_SOUND, CUSTOM_ATTRIBUTES);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400293 }
Julia Reynolds184b86d2017-04-12 13:27:58 -0400294 } else {
295 channel.setSound(null, null);
Chris Wren93bb8b82016-03-29 14:35:05 -0400296 }
297 if (buzzy) {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400298 if (defaultVibration) {
299 defaults |= Notification.DEFAULT_VIBRATE;
300 } else {
301 builder.setVibrate(CUSTOM_VIBRATION);
Julia Reynoldsf57de462016-11-23 11:31:46 -0500302 channel.setVibrationPattern(CUSTOM_VIBRATION);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400303 }
Julia Reynoldsf57de462016-11-23 11:31:46 -0500304 channel.enableVibration(true);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400305 }
306 if (lights) {
307 if (defaultLights) {
308 defaults |= Notification.DEFAULT_LIGHTS;
309 } else {
310 builder.setLights(CUSTOM_LIGHT_COLOR, CUSTOM_LIGHT_ON, CUSTOM_LIGHT_OFF);
311 }
Julia Reynolds529e3322017-02-06 08:33:01 -0500312 channel.enableLights(true);
Chris Wren93bb8b82016-03-29 14:35:05 -0400313 }
314 builder.setDefaults(defaults);
315
Julia Reynoldsa79c3712017-04-21 10:29:57 -0400316 builder.setGroup(groupKey);
317 builder.setGroupAlertBehavior(groupAlertBehavior);
318
Chris Wren93bb8b82016-03-29 14:35:05 -0400319 Notification n = builder.build();
320 if (insistent) {
321 n.flags |= Notification.FLAG_INSISTENT;
322 }
Julia Reynolds0c299d42016-11-15 14:37:04 -0500323
Qiao (Adora) Zhanga9d0bff2019-04-15 17:45:43 +0000324 Context context = spy(getContext());
325 PackageManager packageManager = spy(context.getPackageManager());
326 when(context.getPackageManager()).thenReturn(packageManager);
Tony Mantlerab55f0f2017-06-16 10:50:00 -0700327 when(packageManager.hasSystemFeature(PackageManager.FEATURE_LEANBACK))
328 .thenReturn(isLeanback);
329
Julia Reynolds924eed12017-01-19 09:52:07 -0500330 StatusBarNotification sbn = new StatusBarNotification(mPkg, mPkg, id, mTag, mUid,
Julia Reynolds423b9fc2016-11-09 09:51:08 -0500331 mPid, n, mUser, null, System.currentTimeMillis());
Qiao (Adora) Zhanga9d0bff2019-04-15 17:45:43 +0000332 NotificationRecord r = new NotificationRecord(context, sbn, channel);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400333 mService.addNotification(r);
334 return r;
Chris Wren93bb8b82016-03-29 14:35:05 -0400335 }
336
337 //
338 // Convenience functions for interacting with mocks
339 //
340
341 private void verifyNeverBeep() throws RemoteException {
342 verify(mRingtonePlayer, never()).playAsync((Uri) anyObject(), (UserHandle) anyObject(),
343 anyBoolean(), (AudioAttributes) anyObject());
344 }
345
346 private void verifyBeep() throws RemoteException {
347 verify(mRingtonePlayer, times(1)).playAsync((Uri) anyObject(), (UserHandle) anyObject(),
348 eq(true), (AudioAttributes) anyObject());
349 }
350
351 private void verifyBeepLooped() throws RemoteException {
352 verify(mRingtonePlayer, times(1)).playAsync((Uri) anyObject(), (UserHandle) anyObject(),
353 eq(false), (AudioAttributes) anyObject());
354 }
355
356 private void verifyNeverStopAudio() throws RemoteException {
357 verify(mRingtonePlayer, never()).stopAsync();
358 }
359
360 private void verifyStopAudio() throws RemoteException {
361 verify(mRingtonePlayer, times(1)).stopAsync();
362 }
363
364 private void verifyNeverVibrate() {
Michael Wright71216972017-01-31 18:33:54 +0000365 verify(mVibrator, never()).vibrate(anyInt(), anyString(), (VibrationEffect) anyObject(),
Alexey Kuzmine1f06b82018-06-20 17:48:43 +0100366 anyString(), (AudioAttributes) anyObject());
Chris Wren93bb8b82016-03-29 14:35:05 -0400367 }
368
369 private void verifyVibrate() {
Michael Wright71216972017-01-31 18:33:54 +0000370 verify(mVibrator, times(1)).vibrate(anyInt(), anyString(), argThat(mVibrateOnceMatcher),
Alexey Kuzmine1f06b82018-06-20 17:48:43 +0100371 anyString(), (AudioAttributes) anyObject());
Chris Wren93bb8b82016-03-29 14:35:05 -0400372 }
373
374 private void verifyVibrateLooped() {
Michael Wright71216972017-01-31 18:33:54 +0000375 verify(mVibrator, times(1)).vibrate(anyInt(), anyString(), argThat(mVibrateLoopMatcher),
Alexey Kuzmine1f06b82018-06-20 17:48:43 +0100376 anyString(), (AudioAttributes) anyObject());
Chris Wren93bb8b82016-03-29 14:35:05 -0400377 }
378
Jean-Michel Triviea0eb5f2017-05-25 18:32:40 -0700379 private void verifyDelayedVibrateLooped() {
380 verify(mVibrator, timeout(MAX_VIBRATION_DELAY).times(1)).vibrate(anyInt(), anyString(),
Alexey Kuzmine1f06b82018-06-20 17:48:43 +0100381 argThat(mVibrateLoopMatcher), anyString(),
382 (AudioAttributes) anyObject());
Jean-Michel Triviea0eb5f2017-05-25 18:32:40 -0700383 }
384
Chris Wren93bb8b82016-03-29 14:35:05 -0400385 private void verifyStopVibrate() {
386 verify(mVibrator, times(1)).cancel();
387 }
388
389 private void verifyNeverStopVibrate() throws RemoteException {
390 verify(mVibrator, never()).cancel();
391 }
392
Julia Reynolds28149f62018-07-03 10:43:35 -0400393 private void verifyNeverLights() {
394 verify(mLight, never()).setFlashing(anyInt(), anyInt(), anyInt(), anyInt());
395 }
396
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400397 private void verifyLights() {
Julia Reynolds033a4122017-01-31 16:50:38 -0500398 verify(mLight, times(1)).setFlashing(anyInt(), anyInt(), anyInt(), anyInt());
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400399 }
400
401 private void verifyCustomLights() {
Julia Reynolds033a4122017-01-31 16:50:38 -0500402 verify(mLight, times(1)).setFlashing(
403 eq(CUSTOM_LIGHT_COLOR), anyInt(), eq(CUSTOM_LIGHT_ON), eq(CUSTOM_LIGHT_OFF));
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400404 }
405
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400406 //
407 // Tests
408 //
409
410 @Test
411 public void testLights() throws Exception {
412 NotificationRecord r = getLightsNotification();
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400413 r.setSystemImportance(NotificationManager.IMPORTANCE_DEFAULT);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400414
415 mService.buzzBeepBlinkLocked(r);
416
417 verifyLights();
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500418 assertTrue(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500419 assertEquals(-1, r.getLastAudiblyAlertedMs());
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400420 }
421
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400422 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400423 public void testBeep() throws Exception {
424 NotificationRecord r = getBeepyNotification();
425
426 mService.buzzBeepBlinkLocked(r);
427
428 verifyBeepLooped();
429 verifyNeverVibrate();
Julia Reynolds94187562017-10-10 13:58:49 -0400430 verify(mAccessibilityService, times(1)).sendAccessibilityEvent(any(), anyInt());
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500431 assertTrue(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500432 assertNotEquals(-1, r.getLastAudiblyAlertedMs());
Chris Wren93bb8b82016-03-29 14:35:05 -0400433 }
434
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400435 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400436 public void testBeepInsistently() throws Exception {
437 NotificationRecord r = getInsistentBeepyNotification();
438
439 mService.buzzBeepBlinkLocked(r);
440
441 verifyBeep();
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500442 assertTrue(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500443 assertNotEquals(-1, r.getLastAudiblyAlertedMs());
Chris Wren93bb8b82016-03-29 14:35:05 -0400444 }
445
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400446 @Test
Tony Mantlerab55f0f2017-06-16 10:50:00 -0700447 public void testNoLeanbackBeep() throws Exception {
448 NotificationRecord r = getInsistentBeepyLeanbackNotification();
449
450 mService.buzzBeepBlinkLocked(r);
451
452 verifyNeverBeep();
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500453 assertFalse(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500454 assertEquals(-1, r.getLastAudiblyAlertedMs());
Tony Mantlerab55f0f2017-06-16 10:50:00 -0700455 }
456
457 @Test
Qiao (Adora) Zhang47b553c2019-04-15 20:42:57 +0000458 public void testNoBeepForAutomotiveIfEffectsDisabled() throws Exception {
Adora Zhang963328f2018-11-15 18:17:19 -0800459 mService.setIsAutomotive(true);
Qiao (Adora) Zhang47b553c2019-04-15 20:42:57 +0000460 mService.setNotificationEffectsEnabledForAutomotive(false);
461
462 NotificationRecord r = getBeepyNotification();
463 r.setSystemImportance(NotificationManager.IMPORTANCE_HIGH);
464
465 mService.buzzBeepBlinkLocked(r);
466
467 verifyNeverBeep();
468 assertFalse(r.isInterruptive());
469 }
470
471 @Test
472 public void testNoBeepForImportanceDefaultInAutomotiveIfEffectsEnabled() throws Exception {
473 mService.setIsAutomotive(true);
474 mService.setNotificationEffectsEnabledForAutomotive(true);
Adora Zhang963328f2018-11-15 18:17:19 -0800475
476 NotificationRecord r = getBeepyNotification();
477 r.setSystemImportance(NotificationManager.IMPORTANCE_DEFAULT);
478
479 mService.buzzBeepBlinkLocked(r);
480
481 verifyNeverBeep();
482 assertFalse(r.isInterruptive());
483 }
484
485 @Test
Qiao (Adora) Zhang47b553c2019-04-15 20:42:57 +0000486 public void testBeepForImportanceHighInAutomotiveIfEffectsEnabled() throws Exception {
Adora Zhang963328f2018-11-15 18:17:19 -0800487 mService.setIsAutomotive(true);
Qiao (Adora) Zhang47b553c2019-04-15 20:42:57 +0000488 mService.setNotificationEffectsEnabledForAutomotive(true);
Adora Zhang963328f2018-11-15 18:17:19 -0800489
490 NotificationRecord r = getBeepyNotification();
Qiao (Adora) Zhang47b553c2019-04-15 20:42:57 +0000491 r.setSystemImportance(NotificationManager.IMPORTANCE_HIGH);
Adora Zhang963328f2018-11-15 18:17:19 -0800492
493 mService.buzzBeepBlinkLocked(r);
494
495 verifyBeepLooped();
496 assertTrue(r.isInterruptive());
497 }
498
499 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400500 public void testNoInterruptionForMin() throws Exception {
501 NotificationRecord r = getBeepyNotification();
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400502 r.setSystemImportance(NotificationManager.IMPORTANCE_MIN);
Chris Wren93bb8b82016-03-29 14:35:05 -0400503
504 mService.buzzBeepBlinkLocked(r);
505
506 verifyNeverBeep();
507 verifyNeverVibrate();
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500508 assertFalse(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500509 assertEquals(-1, r.getLastAudiblyAlertedMs());
Chris Wren93bb8b82016-03-29 14:35:05 -0400510 }
511
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400512 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400513 public void testNoInterruptionForIntercepted() throws Exception {
514 NotificationRecord r = getBeepyNotification();
515 r.setIntercepted(true);
516
517 mService.buzzBeepBlinkLocked(r);
518
519 verifyNeverBeep();
520 verifyNeverVibrate();
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500521 assertFalse(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500522 assertEquals(-1, r.getLastAudiblyAlertedMs());
Chris Wren93bb8b82016-03-29 14:35:05 -0400523 }
524
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400525 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400526 public void testBeepTwice() throws Exception {
527 NotificationRecord r = getBeepyNotification();
528
529 // set up internal state
530 mService.buzzBeepBlinkLocked(r);
531 Mockito.reset(mRingtonePlayer);
532
533 // update should beep
534 r.isUpdate = true;
535 mService.buzzBeepBlinkLocked(r);
536 verifyBeepLooped();
Julia Reynolds94187562017-10-10 13:58:49 -0400537 verify(mAccessibilityService, times(2)).sendAccessibilityEvent(any(), anyInt());
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500538 assertTrue(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500539 assertNotEquals(-1, r.getLastAudiblyAlertedMs());
Chris Wren93bb8b82016-03-29 14:35:05 -0400540 }
541
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400542 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400543 public void testHonorAlertOnlyOnceForBeep() throws Exception {
544 NotificationRecord r = getBeepyNotification();
545 NotificationRecord s = getBeepyOnceNotification();
546 s.isUpdate = true;
547
548 // set up internal state
549 mService.buzzBeepBlinkLocked(r);
550 Mockito.reset(mRingtonePlayer);
551
552 // update should not beep
553 mService.buzzBeepBlinkLocked(s);
554 verifyNeverBeep();
Julia Reynolds94187562017-10-10 13:58:49 -0400555 verify(mAccessibilityService, times(1)).sendAccessibilityEvent(any(), anyInt());
Chris Wren93bb8b82016-03-29 14:35:05 -0400556 }
557
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400558 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400559 public void testNoisyUpdateDoesNotCancelAudio() throws Exception {
560 NotificationRecord r = getBeepyNotification();
561
562 mService.buzzBeepBlinkLocked(r);
563 r.isUpdate = true;
564 mService.buzzBeepBlinkLocked(r);
565
566 verifyNeverStopAudio();
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500567 assertTrue(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500568 assertNotEquals(-1, r.getLastAudiblyAlertedMs());
Chris Wren93bb8b82016-03-29 14:35:05 -0400569 }
570
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400571 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400572 public void testNoisyOnceUpdateDoesNotCancelAudio() throws Exception {
573 NotificationRecord r = getBeepyNotification();
574 NotificationRecord s = getBeepyOnceNotification();
575 s.isUpdate = true;
576
577 mService.buzzBeepBlinkLocked(r);
578 mService.buzzBeepBlinkLocked(s);
579
580 verifyNeverStopAudio();
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500581 assertTrue(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500582 assertNotEquals(-1, r.getLastAudiblyAlertedMs());
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500583 assertFalse(s.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500584 assertEquals(-1, s.getLastAudiblyAlertedMs());
Chris Wren93bb8b82016-03-29 14:35:05 -0400585 }
586
Tyler Gunn48f86272018-07-03 12:38:49 -0700587 /**
588 * Tests the case where the user re-posts a {@link Notification} with looping sound where
589 * {@link Notification.Builder#setOnlyAlertOnce(true)} has been called. This should silence
590 * the sound associated with the notification.
591 * @throws Exception
592 */
593 @Test
594 public void testNoisyOnceUpdateDoesCancelAudio() throws Exception {
595 NotificationRecord r = getInsistentBeepyNotification();
596 NotificationRecord s = getInsistentBeepyOnceNotification();
597 s.isUpdate = true;
598
599 mService.buzzBeepBlinkLocked(r);
600 mService.buzzBeepBlinkLocked(s);
601
602 verifyStopAudio();
603 }
604
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400605 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400606 public void testQuietUpdateDoesNotCancelAudioFromOther() throws Exception {
607 NotificationRecord r = getBeepyNotification();
608 NotificationRecord s = getQuietNotification();
609 s.isUpdate = true;
610 NotificationRecord other = getNoisyOtherNotification();
611
612 // set up internal state
613 mService.buzzBeepBlinkLocked(r);
614 mService.buzzBeepBlinkLocked(other); // this takes the audio stream
615 Mockito.reset(mRingtonePlayer);
616
617 // should not stop noise, since we no longer own it
618 mService.buzzBeepBlinkLocked(s); // this no longer owns the stream
619 verifyNeverStopAudio();
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500620 assertTrue(other.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500621 assertNotEquals(-1, other.getLastAudiblyAlertedMs());
Chris Wren93bb8b82016-03-29 14:35:05 -0400622 }
623
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400624 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400625 public void testQuietInterloperDoesNotCancelAudio() throws Exception {
626 NotificationRecord r = getBeepyNotification();
627 NotificationRecord other = getQuietOtherNotification();
628
629 // set up internal state
630 mService.buzzBeepBlinkLocked(r);
631 Mockito.reset(mRingtonePlayer);
632
633 // should not stop noise, since it does not own it
634 mService.buzzBeepBlinkLocked(other);
635 verifyNeverStopAudio();
636 }
637
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400638 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400639 public void testQuietUpdateCancelsAudio() throws Exception {
640 NotificationRecord r = getBeepyNotification();
641 NotificationRecord s = getQuietNotification();
642 s.isUpdate = true;
643
644 // set up internal state
645 mService.buzzBeepBlinkLocked(r);
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500646 assertTrue(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500647 assertNotEquals(-1, r.getLastAudiblyAlertedMs());
Chris Wren93bb8b82016-03-29 14:35:05 -0400648 Mockito.reset(mRingtonePlayer);
649
650 // quiet update should stop making noise
651 mService.buzzBeepBlinkLocked(s);
652 verifyStopAudio();
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500653 assertFalse(s.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500654 assertEquals(-1, s.getLastAudiblyAlertedMs());
Chris Wren93bb8b82016-03-29 14:35:05 -0400655 }
656
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400657 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400658 public void testQuietOnceUpdateCancelsAudio() throws Exception {
659 NotificationRecord r = getBeepyNotification();
660 NotificationRecord s = getQuietOnceNotification();
661 s.isUpdate = true;
662
663 // set up internal state
664 mService.buzzBeepBlinkLocked(r);
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500665 assertTrue(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500666 assertNotEquals(-1, r.getLastAudiblyAlertedMs());
Chris Wren93bb8b82016-03-29 14:35:05 -0400667 Mockito.reset(mRingtonePlayer);
668
669 // stop making noise - this is a weird corner case, but quiet should override once
670 mService.buzzBeepBlinkLocked(s);
671 verifyStopAudio();
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500672 assertFalse(s.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500673 assertEquals(-1, s.getLastAudiblyAlertedMs());
Chris Wren93bb8b82016-03-29 14:35:05 -0400674 }
675
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400676 @Test
Beverly5d463b62017-07-26 14:13:40 -0400677 public void testInCallNotification() throws Exception {
678 NotificationRecord r = getBeepyNotification();
679
680 // set up internal state
681 mService.buzzBeepBlinkLocked(r);
682 Mockito.reset(mRingtonePlayer);
683
684 mService.mInCall = true;
685 mService.buzzBeepBlinkLocked(r);
686
Julia Reynolds94187562017-10-10 13:58:49 -0400687 verify(mService, times(1)).playInCallNotification();
Beverly5d463b62017-07-26 14:13:40 -0400688 verifyNeverBeep(); // doesn't play normal beep
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500689 assertTrue(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500690 assertNotEquals(-1, r.getLastAudiblyAlertedMs());
Beverly5d463b62017-07-26 14:13:40 -0400691 }
692
693 @Test
Julia Reynolds0c299d42016-11-15 14:37:04 -0500694 public void testNoDemoteSoundToVibrateIfVibrateGiven() throws Exception {
695 NotificationRecord r = getBuzzyBeepyNotification();
696 assertTrue(r.getSound() != null);
Chris Wren93bb8b82016-03-29 14:35:05 -0400697
698 // the phone is quiet
Chris Wren93bb8b82016-03-29 14:35:05 -0400699 when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_VIBRATE);
Julia Reynolds85896572017-09-20 12:54:52 -0400700 when(mAudioManager.getStreamVolume(anyInt())).thenReturn(0);
Chris Wren93bb8b82016-03-29 14:35:05 -0400701
702 mService.buzzBeepBlinkLocked(r);
703
Michael Wright71216972017-01-31 18:33:54 +0000704 VibrationEffect effect = VibrationEffect.createWaveform(r.getVibration(), -1);
705
Jean-Michel Triviea0eb5f2017-05-25 18:32:40 -0700706 verify(mVibrator, timeout(MAX_VIBRATION_DELAY).times(1)).vibrate(anyInt(), anyString(),
Alexey Kuzmine1f06b82018-06-20 17:48:43 +0100707 eq(effect), anyString(),
708 (AudioAttributes) anyObject());
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500709 assertTrue(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500710 assertNotEquals(-1, r.getLastAudiblyAlertedMs());
Chris Wren93bb8b82016-03-29 14:35:05 -0400711 }
712
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400713 @Test
Julia Reynolds85896572017-09-20 12:54:52 -0400714 public void testNoDemoteSoundToVibrateIfNonNotificationStream() throws Exception {
715 NotificationRecord r = getBeepyNotification();
716 assertTrue(r.getSound() != null);
717 assertNull(r.getVibration());
718
719 // the phone is quiet
720 when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_VIBRATE);
721 when(mAudioManager.getStreamVolume(anyInt())).thenReturn(1);
722
723 mService.buzzBeepBlinkLocked(r);
724
725 verifyNeverVibrate();
726 verifyBeepLooped();
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500727 assertTrue(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500728 assertNotEquals(-1, r.getLastAudiblyAlertedMs());
Julia Reynolds85896572017-09-20 12:54:52 -0400729 }
730
731 @Test
Julia Reynolds0c299d42016-11-15 14:37:04 -0500732 public void testDemoteSoundToVibrate() throws Exception {
733 NotificationRecord r = getBeepyNotification();
734 assertTrue(r.getSound() != null);
735 assertNull(r.getVibration());
736
737 // the phone is quiet
738 when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_VIBRATE);
Julia Reynolds85896572017-09-20 12:54:52 -0400739 when(mAudioManager.getStreamVolume(anyInt())).thenReturn(0);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500740
741 mService.buzzBeepBlinkLocked(r);
742
Jean-Michel Triviea0eb5f2017-05-25 18:32:40 -0700743 verify(mVibrator, timeout(MAX_VIBRATION_DELAY).times(1)).vibrate(anyInt(), anyString(),
Alexey Kuzmine1f06b82018-06-20 17:48:43 +0100744 eq(FALLBACK_VIBRATION), anyString(), (AudioAttributes) anyObject());
Julia Reynolds619a69f2017-01-27 15:11:38 -0500745 verify(mRingtonePlayer, never()).playAsync
746 (anyObject(), anyObject(), anyBoolean(), anyObject());
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500747 assertTrue(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500748 assertNotEquals(-1, r.getLastAudiblyAlertedMs());
Julia Reynolds0c299d42016-11-15 14:37:04 -0500749 }
750
751 @Test
752 public void testDemoteInsistentSoundToVibrate() throws Exception {
Chris Wren93bb8b82016-03-29 14:35:05 -0400753 NotificationRecord r = getInsistentBeepyNotification();
Julia Reynolds0c299d42016-11-15 14:37:04 -0500754 assertTrue(r.getSound() != null);
755 assertNull(r.getVibration());
Chris Wren93bb8b82016-03-29 14:35:05 -0400756
757 // the phone is quiet
758 when(mAudioManager.getStreamVolume(anyInt())).thenReturn(0);
759 when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_VIBRATE);
760
761 mService.buzzBeepBlinkLocked(r);
762
Jean-Michel Triviea0eb5f2017-05-25 18:32:40 -0700763 verifyDelayedVibrateLooped();
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500764 assertTrue(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500765 assertNotEquals(-1, r.getLastAudiblyAlertedMs());
Chris Wren93bb8b82016-03-29 14:35:05 -0400766 }
767
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400768 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400769 public void testVibrate() throws Exception {
770 NotificationRecord r = getBuzzyNotification();
771
772 mService.buzzBeepBlinkLocked(r);
773
774 verifyNeverBeep();
775 verifyVibrate();
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500776 assertTrue(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500777 assertNotEquals(-1, r.getLastAudiblyAlertedMs());
Chris Wren93bb8b82016-03-29 14:35:05 -0400778 }
779
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400780 @Test
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500781 public void testInsistentVibrate() {
Chris Wren93bb8b82016-03-29 14:35:05 -0400782 NotificationRecord r = getInsistentBuzzyNotification();
783
784 mService.buzzBeepBlinkLocked(r);
785 verifyVibrateLooped();
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500786 assertTrue(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500787 assertNotEquals(-1, r.getLastAudiblyAlertedMs());
Chris Wren93bb8b82016-03-29 14:35:05 -0400788 }
789
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400790 @Test
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500791 public void testVibrateTwice() {
Chris Wren93bb8b82016-03-29 14:35:05 -0400792 NotificationRecord r = getBuzzyNotification();
793
794 // set up internal state
795 mService.buzzBeepBlinkLocked(r);
796 Mockito.reset(mVibrator);
797
798 // update should vibrate
799 r.isUpdate = true;
800 mService.buzzBeepBlinkLocked(r);
801 verifyVibrate();
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500802 assertTrue(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500803 assertNotEquals(-1, r.getLastAudiblyAlertedMs());
Chris Wren93bb8b82016-03-29 14:35:05 -0400804 }
805
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400806 @Test
Julia Reynoldsa79c3712017-04-21 10:29:57 -0400807 public void testGroupAlertSummarySilenceChild() throws Exception {
808 NotificationRecord child = getBeepyNotificationRecord("a", GROUP_ALERT_SUMMARY);
809
810 mService.buzzBeepBlinkLocked(child);
811
812 verifyNeverBeep();
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500813 assertFalse(child.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500814 assertEquals(-1, child.getLastAudiblyAlertedMs());
Julia Reynoldsa79c3712017-04-21 10:29:57 -0400815 }
816
817 @Test
818 public void testGroupAlertSummaryNoSilenceSummary() throws Exception {
819 NotificationRecord summary = getBeepyNotificationRecord("a", GROUP_ALERT_SUMMARY);
820 summary.getNotification().flags |= Notification.FLAG_GROUP_SUMMARY;
821
822 mService.buzzBeepBlinkLocked(summary);
823
824 verifyBeepLooped();
Julia Reynolds28149f62018-07-03 10:43:35 -0400825 // summaries are never interruptive for notification counts
826 assertFalse(summary.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500827 assertNotEquals(-1, summary.getLastAudiblyAlertedMs());
Julia Reynoldsa79c3712017-04-21 10:29:57 -0400828 }
829
830 @Test
831 public void testGroupAlertSummaryNoSilenceNonGroupChild() throws Exception {
832 NotificationRecord nonGroup = getBeepyNotificationRecord(null, GROUP_ALERT_SUMMARY);
833
834 mService.buzzBeepBlinkLocked(nonGroup);
835
836 verifyBeepLooped();
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500837 assertTrue(nonGroup.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500838 assertNotEquals(-1, nonGroup.getLastAudiblyAlertedMs());
Julia Reynoldsa79c3712017-04-21 10:29:57 -0400839 }
840
841 @Test
842 public void testGroupAlertChildSilenceSummary() throws Exception {
843 NotificationRecord summary = getBeepyNotificationRecord("a", GROUP_ALERT_CHILDREN);
844 summary.getNotification().flags |= Notification.FLAG_GROUP_SUMMARY;
845
846 mService.buzzBeepBlinkLocked(summary);
847
848 verifyNeverBeep();
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500849 assertFalse(summary.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500850 assertEquals(-1, summary.getLastAudiblyAlertedMs());
Julia Reynoldsa79c3712017-04-21 10:29:57 -0400851 }
852
853 @Test
854 public void testGroupAlertChildNoSilenceChild() throws Exception {
855 NotificationRecord child = getBeepyNotificationRecord("a", GROUP_ALERT_CHILDREN);
856
857 mService.buzzBeepBlinkLocked(child);
858
859 verifyBeepLooped();
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500860 assertTrue(child.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500861 assertNotEquals(-1, child.getLastAudiblyAlertedMs());
Julia Reynoldsa79c3712017-04-21 10:29:57 -0400862 }
863
864 @Test
865 public void testGroupAlertChildNoSilenceNonGroupSummary() throws Exception {
866 NotificationRecord nonGroup = getBeepyNotificationRecord(null, GROUP_ALERT_CHILDREN);
867
868 mService.buzzBeepBlinkLocked(nonGroup);
869
870 verifyBeepLooped();
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500871 assertTrue(nonGroup.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500872 assertNotEquals(-1, nonGroup.getLastAudiblyAlertedMs());
Julia Reynoldsa79c3712017-04-21 10:29:57 -0400873 }
874
875 @Test
876 public void testGroupAlertAllNoSilenceGroup() throws Exception {
877 NotificationRecord group = getBeepyNotificationRecord("a", GROUP_ALERT_ALL);
878
879 mService.buzzBeepBlinkLocked(group);
880
881 verifyBeepLooped();
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500882 assertTrue(group.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500883 assertNotEquals(-1, group.getLastAudiblyAlertedMs());
Julia Reynoldsa79c3712017-04-21 10:29:57 -0400884 }
885
886 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400887 public void testHonorAlertOnlyOnceForBuzz() throws Exception {
888 NotificationRecord r = getBuzzyNotification();
889 NotificationRecord s = getBuzzyOnceNotification();
890 s.isUpdate = true;
891
892 // set up internal state
893 mService.buzzBeepBlinkLocked(r);
894 Mockito.reset(mVibrator);
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500895 assertTrue(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500896 assertNotEquals(-1, r.getLastAudiblyAlertedMs());
Chris Wren93bb8b82016-03-29 14:35:05 -0400897
898 // update should not beep
899 mService.buzzBeepBlinkLocked(s);
900 verifyNeverVibrate();
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500901 assertFalse(s.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500902 assertEquals(-1, s.getLastAudiblyAlertedMs());
Chris Wren93bb8b82016-03-29 14:35:05 -0400903 }
904
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400905 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400906 public void testNoisyUpdateDoesNotCancelVibrate() throws Exception {
907 NotificationRecord r = getBuzzyNotification();
908
909 mService.buzzBeepBlinkLocked(r);
910 r.isUpdate = true;
911 mService.buzzBeepBlinkLocked(r);
912
913 verifyNeverStopVibrate();
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500914 assertTrue(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500915 assertNotEquals(-1, r.getLastAudiblyAlertedMs());
Chris Wren93bb8b82016-03-29 14:35:05 -0400916 }
917
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400918 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400919 public void testNoisyOnceUpdateDoesNotCancelVibrate() throws Exception {
920 NotificationRecord r = getBuzzyNotification();
921 NotificationRecord s = getBuzzyOnceNotification();
922 s.isUpdate = true;
923
924 mService.buzzBeepBlinkLocked(r);
925 mService.buzzBeepBlinkLocked(s);
926
927 verifyNeverStopVibrate();
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500928 assertTrue(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500929 assertNotEquals(-1, r.getLastAudiblyAlertedMs());
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500930 assertFalse(s.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500931 assertEquals(-1, s.getLastAudiblyAlertedMs());
Chris Wren93bb8b82016-03-29 14:35:05 -0400932 }
933
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400934 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400935 public void testQuietUpdateDoesNotCancelVibrateFromOther() throws Exception {
936 NotificationRecord r = getBuzzyNotification();
937 NotificationRecord s = getQuietNotification();
938 s.isUpdate = true;
939 NotificationRecord other = getNoisyOtherNotification();
940
941 // set up internal state
942 mService.buzzBeepBlinkLocked(r);
943 mService.buzzBeepBlinkLocked(other); // this takes the vibrate stream
944 Mockito.reset(mVibrator);
945
946 // should not stop vibrate, since we no longer own it
947 mService.buzzBeepBlinkLocked(s); // this no longer owns the stream
948 verifyNeverStopVibrate();
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500949 assertTrue(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500950 assertNotEquals(-1, r.getLastAudiblyAlertedMs());
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500951 assertTrue(other.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500952 assertNotEquals(-1, other.getLastAudiblyAlertedMs());
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500953 assertFalse(s.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500954 assertEquals(-1, s.getLastAudiblyAlertedMs());
Chris Wren93bb8b82016-03-29 14:35:05 -0400955 }
956
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400957 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400958 public void testQuietInterloperDoesNotCancelVibrate() throws Exception {
959 NotificationRecord r = getBuzzyNotification();
960 NotificationRecord other = getQuietOtherNotification();
961
962 // set up internal state
963 mService.buzzBeepBlinkLocked(r);
964 Mockito.reset(mVibrator);
965
966 // should not stop noise, since it does not own it
967 mService.buzzBeepBlinkLocked(other);
968 verifyNeverStopVibrate();
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500969 assertFalse(other.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500970 assertEquals(-1, other.getLastAudiblyAlertedMs());
Chris Wren93bb8b82016-03-29 14:35:05 -0400971 }
972
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400973 @Test
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500974 public void testQuietUpdateCancelsVibrate() {
Chris Wren93bb8b82016-03-29 14:35:05 -0400975 NotificationRecord r = getBuzzyNotification();
976 NotificationRecord s = getQuietNotification();
977 s.isUpdate = true;
978
979 // set up internal state
980 mService.buzzBeepBlinkLocked(r);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500981 verifyVibrate();
Chris Wren93bb8b82016-03-29 14:35:05 -0400982
983 // quiet update should stop making noise
984 mService.buzzBeepBlinkLocked(s);
985 verifyStopVibrate();
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500986 assertTrue(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500987 assertNotEquals(-1, r.getLastAudiblyAlertedMs());
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500988 assertFalse(s.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -0500989 assertEquals(-1, s.getLastAudiblyAlertedMs());
Chris Wren93bb8b82016-03-29 14:35:05 -0400990 }
991
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400992 @Test
Julia Reynolds0c299d42016-11-15 14:37:04 -0500993 public void testQuietOnceUpdateCancelVibrate() throws Exception {
Chris Wren93bb8b82016-03-29 14:35:05 -0400994 NotificationRecord r = getBuzzyNotification();
995 NotificationRecord s = getQuietOnceNotification();
996 s.isUpdate = true;
997
998 // set up internal state
999 mService.buzzBeepBlinkLocked(r);
Julia Reynolds0c299d42016-11-15 14:37:04 -05001000 verifyVibrate();
Chris Wren93bb8b82016-03-29 14:35:05 -04001001
1002 // stop making noise - this is a weird corner case, but quiet should override once
1003 mService.buzzBeepBlinkLocked(s);
1004 verifyStopVibrate();
Julia Reynolds1fac86e2018-03-07 08:30:37 -05001005 assertTrue(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -05001006 assertNotEquals(-1, r.getLastAudiblyAlertedMs());
Julia Reynolds1fac86e2018-03-07 08:30:37 -05001007 assertFalse(s.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -05001008 assertEquals(-1, s.getLastAudiblyAlertedMs());
Chris Wren93bb8b82016-03-29 14:35:05 -04001009 }
1010
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -04001011 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -04001012 public void testQuietUpdateCancelsDemotedVibrate() throws Exception {
1013 NotificationRecord r = getBeepyNotification();
1014 NotificationRecord s = getQuietNotification();
1015
1016 // the phone is quiet
1017 when(mAudioManager.getStreamVolume(anyInt())).thenReturn(0);
1018 when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_VIBRATE);
1019
1020 mService.buzzBeepBlinkLocked(r);
1021
1022 // quiet update should stop making noise
1023 mService.buzzBeepBlinkLocked(s);
1024 verifyStopVibrate();
Julia Reynolds1fac86e2018-03-07 08:30:37 -05001025 assertTrue(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -05001026 assertNotEquals(-1, r.getLastAudiblyAlertedMs());
Julia Reynolds1fac86e2018-03-07 08:30:37 -05001027 assertFalse(s.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -05001028 assertEquals(-1, s.getLastAudiblyAlertedMs());
Chris Wren93bb8b82016-03-29 14:35:05 -04001029 }
Michael Wright71216972017-01-31 18:33:54 +00001030
Julia Reynoldsca80d162017-04-23 12:39:32 -04001031 @Test
1032 public void testEmptyUriSoundTreatedAsNoSound() throws Exception {
1033 NotificationChannel channel = new NotificationChannel("test", "test", IMPORTANCE_HIGH);
1034 channel.setSound(Uri.EMPTY, null);
Qiao (Adora) Zhanga9d0bff2019-04-15 17:45:43 +00001035 final Notification n = new Builder(getContext(), "test")
Julia Reynoldsca80d162017-04-23 12:39:32 -04001036 .setSmallIcon(android.R.drawable.sym_def_app_icon).build();
1037
1038 StatusBarNotification sbn = new StatusBarNotification(mPkg, mPkg, 0, mTag, mUid,
1039 mPid, n, mUser, null, System.currentTimeMillis());
Qiao (Adora) Zhanga9d0bff2019-04-15 17:45:43 +00001040 NotificationRecord r = new NotificationRecord(getContext(), sbn, channel);
Julia Reynoldsca80d162017-04-23 12:39:32 -04001041 mService.addNotification(r);
1042
1043 mService.buzzBeepBlinkLocked(r);
Julia Reynoldsca80d162017-04-23 12:39:32 -04001044 verifyNeverBeep();
Julia Reynolds1fac86e2018-03-07 08:30:37 -05001045 assertFalse(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -05001046 assertEquals(-1, r.getLastAudiblyAlertedMs());
Julia Reynoldsca80d162017-04-23 12:39:32 -04001047 }
1048
Julia Reynolds76c096d2017-06-19 08:16:04 -04001049 @Test
1050 public void testRepeatedSoundOverLimitMuted() throws Exception {
1051 when(mUsageStats.isAlertRateLimited(any())).thenReturn(true);
1052
1053 NotificationRecord r = getBeepyNotification();
1054
1055 mService.buzzBeepBlinkLocked(r);
1056 verifyNeverBeep();
Julia Reynolds1fac86e2018-03-07 08:30:37 -05001057 assertFalse(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -05001058 assertEquals(-1, r.getLastAudiblyAlertedMs());
Julia Reynolds76c096d2017-06-19 08:16:04 -04001059 }
1060
1061 @Test
1062 public void testPostingSilentNotificationDoesNotAffectRateLimiting() throws Exception {
1063 NotificationRecord r = getQuietNotification();
1064 mService.buzzBeepBlinkLocked(r);
1065
1066 verify(mUsageStats, never()).isAlertRateLimited(any());
1067 }
1068
1069 @Test
Julia Reynolds65b85cf2017-07-20 09:19:20 -04001070 public void testPostingGroupSuppressedDoesNotAffectRateLimiting() throws Exception {
1071 NotificationRecord summary = getBeepyNotificationRecord("a", GROUP_ALERT_CHILDREN);
1072 summary.getNotification().flags |= Notification.FLAG_GROUP_SUMMARY;
1073
1074 mService.buzzBeepBlinkLocked(summary);
Julia Reynolds65b85cf2017-07-20 09:19:20 -04001075 verify(mUsageStats, never()).isAlertRateLimited(any());
1076 }
1077
1078 @Test
Julia Reynolds79dfdd62018-04-17 15:36:33 -04001079 public void testGroupSuppressionFailureDoesNotAffectRateLimiting() {
1080 NotificationRecord summary = getBeepyNotificationRecord("a", GROUP_ALERT_SUMMARY);
1081 summary.getNotification().flags |= Notification.FLAG_GROUP_SUMMARY;
1082
1083 mService.buzzBeepBlinkLocked(summary);
1084 verify(mUsageStats, times(1)).isAlertRateLimited(any());
1085 }
1086
1087 @Test
Julia Reynolds76c096d2017-06-19 08:16:04 -04001088 public void testCrossUserSoundMuted() throws Exception {
Qiao (Adora) Zhanga9d0bff2019-04-15 17:45:43 +00001089 final Notification n = new Builder(getContext(), "test")
Julia Reynolds76c096d2017-06-19 08:16:04 -04001090 .setSmallIcon(android.R.drawable.sym_def_app_icon).build();
1091
1092 int userId = mUser.getIdentifier() + 1;
1093 StatusBarNotification sbn = new StatusBarNotification(mPkg, mPkg, 0, mTag, mUid,
1094 mPid, n, UserHandle.of(userId), null, System.currentTimeMillis());
Qiao (Adora) Zhanga9d0bff2019-04-15 17:45:43 +00001095 NotificationRecord r = new NotificationRecord(getContext(), sbn,
Julia Reynolds76c096d2017-06-19 08:16:04 -04001096 new NotificationChannel("test", "test", IMPORTANCE_HIGH));
1097
1098 mService.buzzBeepBlinkLocked(r);
1099 verifyNeverBeep();
Julia Reynolds1fac86e2018-03-07 08:30:37 -05001100 assertFalse(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -05001101 assertEquals(-1, r.getLastAudiblyAlertedMs());
Julia Reynolds76c096d2017-06-19 08:16:04 -04001102 }
1103
Julia Reynolds94187562017-10-10 13:58:49 -04001104 @Test
1105 public void testA11yMinInitialPost() throws Exception {
1106 NotificationRecord r = getQuietNotification();
Julia Reynoldsefcdff42018-08-09 09:42:56 -04001107 r.setSystemImportance(IMPORTANCE_MIN);
Julia Reynolds94187562017-10-10 13:58:49 -04001108 mService.buzzBeepBlinkLocked(r);
1109 verify(mAccessibilityService, never()).sendAccessibilityEvent(any(), anyInt());
1110 }
1111
1112 @Test
1113 public void testA11yQuietInitialPost() throws Exception {
1114 NotificationRecord r = getQuietNotification();
1115 mService.buzzBeepBlinkLocked(r);
1116 verify(mAccessibilityService, times(1)).sendAccessibilityEvent(any(), anyInt());
1117 }
1118
1119 @Test
1120 public void testA11yQuietUpdate() throws Exception {
1121 NotificationRecord r = getQuietNotification();
1122 mService.buzzBeepBlinkLocked(r);
1123 r.isUpdate = true;
1124 mService.buzzBeepBlinkLocked(r);
1125 verify(mAccessibilityService, times(1)).sendAccessibilityEvent(any(), anyInt());
1126 }
1127
Julia Reynolds28149f62018-07-03 10:43:35 -04001128 @Test
1129 public void testLightsScreenOn() {
1130 mService.mScreenOn = true;
1131 NotificationRecord r = getLightsNotification();
1132 mService.buzzBeepBlinkLocked(r);
1133 verifyNeverLights();
1134 assertFalse(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -05001135 assertEquals(-1, r.getLastAudiblyAlertedMs());
Julia Reynolds28149f62018-07-03 10:43:35 -04001136 }
1137
1138 @Test
1139 public void testLightsInCall() {
1140 mService.mInCall = true;
1141 NotificationRecord r = getLightsNotification();
1142 mService.buzzBeepBlinkLocked(r);
1143 verifyNeverLights();
1144 assertFalse(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -05001145 assertEquals(-1, r.getLastAudiblyAlertedMs());
Julia Reynolds28149f62018-07-03 10:43:35 -04001146 }
1147
1148 @Test
1149 public void testLightsSilentUpdate() {
1150 NotificationRecord r = getLightsOnceNotification();
1151 mService.buzzBeepBlinkLocked(r);
1152 verifyLights();
1153 assertTrue(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -05001154 assertEquals(-1, r.getLastAudiblyAlertedMs());
Julia Reynolds28149f62018-07-03 10:43:35 -04001155
1156 r = getLightsOnceNotification();
1157 r.isUpdate = true;
1158 mService.buzzBeepBlinkLocked(r);
1159 // checks that lights happened once, i.e. this new call didn't trigger them again
1160 verifyLights();
1161 assertFalse(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -05001162 assertEquals(-1, r.getLastAudiblyAlertedMs());
Julia Reynolds28149f62018-07-03 10:43:35 -04001163 }
1164
1165 @Test
1166 public void testLightsUnimportant() {
1167 NotificationRecord r = getLightsNotification();
Julia Reynoldsefcdff42018-08-09 09:42:56 -04001168 r.setSystemImportance(IMPORTANCE_LOW);
Julia Reynolds28149f62018-07-03 10:43:35 -04001169 mService.buzzBeepBlinkLocked(r);
1170 verifyNeverLights();
1171 assertFalse(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -05001172 assertEquals(-1, r.getLastAudiblyAlertedMs());
Julia Reynolds28149f62018-07-03 10:43:35 -04001173 }
1174
1175 @Test
1176 public void testLightsNoLights() {
1177 NotificationRecord r = getQuietNotification();
1178 mService.buzzBeepBlinkLocked(r);
1179 verifyNeverLights();
1180 assertFalse(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -05001181 assertEquals(-1, r.getLastAudiblyAlertedMs());
Julia Reynolds28149f62018-07-03 10:43:35 -04001182 }
1183
1184 @Test
1185 public void testLightsNoLightOnDevice() {
1186 mService.mHasLight = false;
1187 NotificationRecord r = getLightsNotification();
1188 mService.buzzBeepBlinkLocked(r);
1189 verifyNeverLights();
1190 assertFalse(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -05001191 assertEquals(-1, r.getLastAudiblyAlertedMs());
Julia Reynolds28149f62018-07-03 10:43:35 -04001192 }
1193
1194 @Test
1195 public void testLightsLightsOffGlobally() {
1196 mService.mNotificationPulseEnabled = false;
1197 NotificationRecord r = getLightsNotification();
1198 mService.buzzBeepBlinkLocked(r);
1199 verifyNeverLights();
1200 assertFalse(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -05001201 assertEquals(-1, r.getLastAudiblyAlertedMs());
Julia Reynolds28149f62018-07-03 10:43:35 -04001202 }
1203
1204 @Test
1205 public void testLightsDndIntercepted() {
1206 NotificationRecord r = getLightsNotification();
1207 r.setSuppressedVisualEffects(SUPPRESSED_EFFECT_LIGHTS);
1208 mService.buzzBeepBlinkLocked(r);
1209 verifyNeverLights();
1210 assertFalse(r.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -05001211 assertEquals(-1, r.getLastAudiblyAlertedMs());
Julia Reynolds28149f62018-07-03 10:43:35 -04001212 }
1213
1214 @Test
1215 public void testGroupAlertSummaryNoLightsChild() {
1216 NotificationRecord child = getLightsNotificationRecord("a", GROUP_ALERT_SUMMARY);
1217
1218 mService.buzzBeepBlinkLocked(child);
1219
1220 verifyNeverLights();
1221 assertFalse(child.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -05001222 assertEquals(-1, child.getLastAudiblyAlertedMs());
Julia Reynolds28149f62018-07-03 10:43:35 -04001223 }
1224
1225 @Test
1226 public void testGroupAlertSummaryLightsSummary() {
1227 NotificationRecord summary = getLightsNotificationRecord("a", GROUP_ALERT_SUMMARY);
1228 summary.getNotification().flags |= Notification.FLAG_GROUP_SUMMARY;
1229
1230 mService.buzzBeepBlinkLocked(summary);
1231
1232 verifyLights();
1233 // summaries should never count for interruptiveness counts
1234 assertFalse(summary.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -05001235 assertEquals(-1, summary.getLastAudiblyAlertedMs());
Julia Reynolds28149f62018-07-03 10:43:35 -04001236 }
1237
1238 @Test
1239 public void testGroupAlertSummaryLightsNonGroupChild() {
1240 NotificationRecord nonGroup = getLightsNotificationRecord(null, GROUP_ALERT_SUMMARY);
1241
1242 mService.buzzBeepBlinkLocked(nonGroup);
1243
1244 verifyLights();
1245 assertTrue(nonGroup.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -05001246 assertEquals(-1, nonGroup.getLastAudiblyAlertedMs());
Julia Reynolds28149f62018-07-03 10:43:35 -04001247 }
1248
1249 @Test
1250 public void testGroupAlertChildNoLightsSummary() {
1251 NotificationRecord summary = getLightsNotificationRecord("a", GROUP_ALERT_CHILDREN);
1252 summary.getNotification().flags |= Notification.FLAG_GROUP_SUMMARY;
1253
1254 mService.buzzBeepBlinkLocked(summary);
1255
1256 verifyNeverLights();
1257 assertFalse(summary.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -05001258 assertEquals(-1, summary.getLastAudiblyAlertedMs());
Julia Reynolds28149f62018-07-03 10:43:35 -04001259 }
1260
1261 @Test
1262 public void testGroupAlertChildLightsChild() {
1263 NotificationRecord child = getLightsNotificationRecord("a", GROUP_ALERT_CHILDREN);
1264
1265 mService.buzzBeepBlinkLocked(child);
1266
1267 verifyLights();
1268 assertTrue(child.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -05001269 assertEquals(-1, child.getLastAudiblyAlertedMs());
Julia Reynolds28149f62018-07-03 10:43:35 -04001270 }
1271
1272 @Test
1273 public void testGroupAlertChildLightsNonGroupSummary() {
1274 NotificationRecord nonGroup = getLightsNotificationRecord(null, GROUP_ALERT_CHILDREN);
1275
1276 mService.buzzBeepBlinkLocked(nonGroup);
1277
1278 verifyLights();
1279 assertTrue(nonGroup.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -05001280 assertEquals(-1, nonGroup.getLastAudiblyAlertedMs());
Julia Reynolds28149f62018-07-03 10:43:35 -04001281 }
1282
1283 @Test
1284 public void testGroupAlertAllLightsGroup() {
1285 NotificationRecord group = getLightsNotificationRecord("a", GROUP_ALERT_ALL);
1286
1287 mService.buzzBeepBlinkLocked(group);
1288
1289 verifyLights();
1290 assertTrue(group.isInterruptive());
Gus Prevas7306b902018-12-11 10:57:06 -05001291 assertEquals(-1, group.getLastAudiblyAlertedMs());
Julia Reynolds28149f62018-07-03 10:43:35 -04001292 }
1293
Julia Reynoldsdb7081e2019-01-03 14:35:38 -05001294 @Test
1295 public void testListenerHintCall() throws Exception {
1296 NotificationRecord r = getCallRecord(1, true);
1297
1298 mService.setHints(NotificationListenerService.HINT_HOST_DISABLE_CALL_EFFECTS);
1299
1300 mService.buzzBeepBlinkLocked(r);
1301
1302 verifyNeverBeep();
1303 }
1304
1305 @Test
1306 public void testListenerHintCall_notificationSound() throws Exception {
1307 NotificationRecord r = getBeepyNotification();
1308
1309 mService.setHints(NotificationListenerService.HINT_HOST_DISABLE_CALL_EFFECTS);
1310
1311 mService.buzzBeepBlinkLocked(r);
1312
1313 verifyBeepLooped();
1314 }
1315
1316 @Test
1317 public void testListenerHintNotification() throws Exception {
1318 NotificationRecord r = getBeepyNotification();
1319
1320 mService.setHints(NotificationListenerService.HINT_HOST_DISABLE_NOTIFICATION_EFFECTS);
1321
1322 mService.buzzBeepBlinkLocked(r);
1323
1324 verifyNeverBeep();
1325 }
1326
1327 @Test
1328 public void testListenerHintBoth() throws Exception {
1329 NotificationRecord r = getCallRecord(1, true);
1330 NotificationRecord s = getBeepyNotification();
1331
1332 mService.setHints(NotificationListenerService.HINT_HOST_DISABLE_NOTIFICATION_EFFECTS
1333 | NotificationListenerService.HINT_HOST_DISABLE_CALL_EFFECTS);
1334
1335 mService.buzzBeepBlinkLocked(r);
1336 mService.buzzBeepBlinkLocked(s);
1337
1338 verifyNeverBeep();
1339 }
1340
1341 @Test
1342 public void testListenerHintNotification_callSound() throws Exception {
1343 NotificationRecord r = getCallRecord(1, true);
1344
1345 mService.setHints(NotificationListenerService.HINT_HOST_DISABLE_NOTIFICATION_EFFECTS);
1346
1347 mService.buzzBeepBlinkLocked(r);
1348
1349 verifyBeepLooped();
1350 }
1351
Michael Wright71216972017-01-31 18:33:54 +00001352 static class VibrateRepeatMatcher implements ArgumentMatcher<VibrationEffect> {
1353 private final int mRepeatIndex;
1354
1355 VibrateRepeatMatcher(int repeatIndex) {
1356 mRepeatIndex = repeatIndex;
1357 }
1358
1359 @Override
1360 public boolean matches(VibrationEffect actual) {
1361 if (actual instanceof VibrationEffect.Waveform &&
1362 ((VibrationEffect.Waveform) actual).getRepeatIndex() == mRepeatIndex) {
1363 return true;
1364 }
1365 // All non-waveform effects are essentially one shots.
1366 return mRepeatIndex == -1;
1367 }
1368
1369 @Override
1370 public String toString() {
1371 return "repeatIndex=" + mRepeatIndex;
1372 }
1373 }
Chris Wren93bb8b82016-03-29 14:35:05 -04001374}