[go: nahoru, domu]

blob: ad436724a37e8492944595e50dfb9b0fc4164b1f [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 Reynolds0c299d42016-11-15 14:37:04 -050018import static junit.framework.Assert.assertFalse;
19import static junit.framework.Assert.assertNull;
20import static junit.framework.Assert.assertTrue;
21
Julia Reynoldsb5e44b72016-08-16 15:00:25 -040022import com.android.server.lights.Light;
23import com.android.server.statusbar.StatusBarManagerInternal;
24
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040025import org.junit.Before;
26import org.junit.Test;
27import org.junit.runner.RunWith;
Chris Wren93bb8b82016-03-29 14:35:05 -040028
29import android.app.ActivityManager;
30import android.app.Notification;
31import android.app.Notification.Builder;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -040032import android.app.NotificationManager;
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040033import android.content.Context;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -040034import android.app.NotificationChannel;
35import android.graphics.Color;
Chris Wren93bb8b82016-03-29 14:35:05 -040036import android.media.AudioAttributes;
37import android.media.AudioManager;
38import android.net.Uri;
39import android.os.Handler;
40import android.os.RemoteException;
41import android.os.UserHandle;
42import android.os.Vibrator;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -040043import android.provider.Settings;
Chris Wren93bb8b82016-03-29 14:35:05 -040044import android.service.notification.StatusBarNotification;
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040045import android.support.test.InstrumentationRegistry;
46import android.support.test.runner.AndroidJUnit4;
Chris Wren93bb8b82016-03-29 14:35:05 -040047import android.test.suitebuilder.annotation.SmallTest;
48
49import org.mockito.Mock;
50import org.mockito.Mockito;
51import org.mockito.MockitoAnnotations;
52
53import static org.mockito.Matchers.anyBoolean;
54import static org.mockito.Matchers.anyInt;
55import static org.mockito.Matchers.anyObject;
56import static org.mockito.Matchers.anyString;
57import static org.mockito.Matchers.eq;
58import static org.mockito.Mockito.never;
59import static org.mockito.Mockito.times;
60import static org.mockito.Mockito.verify;
61import static org.mockito.Mockito.when;
62
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040063@SmallTest
64@RunWith(AndroidJUnit4.class)
65public class BuzzBeepBlinkTest {
Chris Wren93bb8b82016-03-29 14:35:05 -040066
67 @Mock AudioManager mAudioManager;
68 @Mock Vibrator mVibrator;
69 @Mock android.media.IRingtonePlayer mRingtonePlayer;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -040070 @Mock StatusBarManagerInternal mStatusBar;
71 @Mock Light mLight;
Chris Wren93bb8b82016-03-29 14:35:05 -040072 @Mock Handler mHandler;
73
74 private NotificationManagerService mService;
75 private String mPkg = "com.android.server.notification";
76 private int mId = 1001;
77 private int mOtherId = 1002;
78 private String mTag = null;
79 private int mUid = 1000;
80 private int mPid = 2000;
Chris Wren93bb8b82016-03-29 14:35:05 -040081 private android.os.UserHandle mUser = UserHandle.of(ActivityManager.getCurrentUser());
82
Julia Reynoldsb5e44b72016-08-16 15:00:25 -040083 private static final long[] CUSTOM_VIBRATION = new long[] {
84 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400,
85 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400,
86 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400 };
87 private static final Uri CUSTOM_SOUND = Settings.System.DEFAULT_ALARM_ALERT_URI;
88 private static final int CUSTOM_LIGHT_COLOR = Color.BLACK;
89 private static final int CUSTOM_LIGHT_ON = 10000;
90 private static final int CUSTOM_LIGHT_OFF = 10000;
Julia Reynolds0c299d42016-11-15 14:37:04 -050091 private static final long[] FALLBACK_VIBRATION = new long[] {100, 100, 100};
Julia Reynoldsb5e44b72016-08-16 15:00:25 -040092
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040093 @Before
Chris Wren93bb8b82016-03-29 14:35:05 -040094 public void setUp() {
95 MockitoAnnotations.initMocks(this);
96
97 when(mAudioManager.isAudioFocusExclusive()).thenReturn(false);
98 when(mAudioManager.getRingtonePlayer()).thenReturn(mRingtonePlayer);
99 when(mAudioManager.getStreamVolume(anyInt())).thenReturn(10);
100 when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_NORMAL);
101
102 mService = new NotificationManagerService(getContext());
103 mService.setAudioManager(mAudioManager);
104 mService.setVibrator(mVibrator);
105 mService.setSystemReady(true);
106 mService.setHandler(mHandler);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400107 mService.setStatusBarManager(mStatusBar);
108 mService.setLights(mLight);
109 mService.setScreenOn(false);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500110 mService.setFallbackVibrationPattern(FALLBACK_VIBRATION);
Chris Wren93bb8b82016-03-29 14:35:05 -0400111 }
112
113 //
114 // Convenience functions for creating notification records
115 //
116
117 private NotificationRecord getNoisyOtherNotification() {
118 return getNotificationRecord(mOtherId, false /* insistent */, false /* once */,
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400119 true /* noisy */, true /* buzzy*/, false /* lights */);
Chris Wren93bb8b82016-03-29 14:35:05 -0400120 }
121
122 private NotificationRecord getBeepyNotification() {
123 return getNotificationRecord(mId, false /* insistent */, false /* once */,
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400124 true /* noisy */, false /* buzzy*/, false /* lights */);
Chris Wren93bb8b82016-03-29 14:35:05 -0400125 }
126
127 private NotificationRecord getBeepyOnceNotification() {
128 return getNotificationRecord(mId, false /* insistent */, true /* once */,
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400129 true /* noisy */, false /* buzzy*/, false /* lights */);
Chris Wren93bb8b82016-03-29 14:35:05 -0400130 }
131
132 private NotificationRecord getQuietNotification() {
133 return getNotificationRecord(mId, false /* insistent */, false /* once */,
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400134 false /* noisy */, false /* buzzy*/, false /* lights */);
Chris Wren93bb8b82016-03-29 14:35:05 -0400135 }
136
137 private NotificationRecord getQuietOtherNotification() {
138 return getNotificationRecord(mOtherId, false /* insistent */, false /* once */,
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400139 false /* noisy */, false /* buzzy*/, false /* lights */);
Chris Wren93bb8b82016-03-29 14:35:05 -0400140 }
141
142 private NotificationRecord getQuietOnceNotification() {
143 return getNotificationRecord(mId, false /* insistent */, true /* once */,
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400144 false /* noisy */, false /* buzzy*/, false /* lights */);
Chris Wren93bb8b82016-03-29 14:35:05 -0400145 }
146
147 private NotificationRecord getInsistentBeepyNotification() {
148 return getNotificationRecord(mId, true /* insistent */, false /* once */,
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400149 true /* noisy */, false /* buzzy*/, false /* lights */);
Chris Wren93bb8b82016-03-29 14:35:05 -0400150 }
151
152 private NotificationRecord getBuzzyNotification() {
153 return getNotificationRecord(mId, false /* insistent */, false /* once */,
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400154 false /* noisy */, true /* buzzy*/, false /* lights */);
Chris Wren93bb8b82016-03-29 14:35:05 -0400155 }
156
157 private NotificationRecord getBuzzyOnceNotification() {
158 return getNotificationRecord(mId, false /* insistent */, true /* once */,
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400159 false /* noisy */, true /* buzzy*/, false /* lights */);
Chris Wren93bb8b82016-03-29 14:35:05 -0400160 }
161
162 private NotificationRecord getInsistentBuzzyNotification() {
163 return getNotificationRecord(mId, true /* insistent */, false /* once */,
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400164 false /* noisy */, true /* buzzy*/, false /* lights */);
165 }
166
Julia Reynolds0c299d42016-11-15 14:37:04 -0500167 private NotificationRecord getBuzzyBeepyNotification() {
168 return getNotificationRecord(mId, false /* insistent */, false /* once */,
169 true /* noisy */, true /* buzzy*/, false /* lights */);
170 }
171
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400172 private NotificationRecord getLightsNotification() {
173 return getNotificationRecord(mId, false /* insistent */, true /* once */,
174 false /* noisy */, true /* buzzy*/, true /* lights */);
175 }
176
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400177 private NotificationRecord getCustomLightsNotification() {
178 return getNotificationRecord(mId, false /* insistent */, true /* once */,
179 false /* noisy */, true /* buzzy*/, true /* lights */,
180 true /* defaultVibration */, true /* defaultSound */, false /* defaultLights */);
Chris Wren93bb8b82016-03-29 14:35:05 -0400181 }
182
183 private NotificationRecord getNotificationRecord(int id, boolean insistent, boolean once,
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400184 boolean noisy, boolean buzzy, boolean lights) {
185 return getNotificationRecord(id, insistent, once, noisy, buzzy, lights, true, true, true);
186 }
187
188 private NotificationRecord getNotificationRecord(int id, boolean insistent, boolean once,
189 boolean noisy, boolean buzzy, boolean lights, boolean defaultVibration,
190 boolean defaultSound, boolean defaultLights) {
Julia Reynolds0c299d42016-11-15 14:37:04 -0500191 NotificationChannel channel =
192 new NotificationChannel("test", "test", NotificationManager.IMPORTANCE_HIGH);
Chris Wren93bb8b82016-03-29 14:35:05 -0400193 final Builder builder = new Builder(getContext())
194 .setContentTitle("foo")
195 .setSmallIcon(android.R.drawable.sym_def_app_icon)
196 .setPriority(Notification.PRIORITY_HIGH)
197 .setOnlyAlertOnce(once);
198
199 int defaults = 0;
200 if (noisy) {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400201 if (defaultSound) {
202 defaults |= Notification.DEFAULT_SOUND;
Julia Reynolds0c299d42016-11-15 14:37:04 -0500203 channel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400204 } else {
205 builder.setSound(CUSTOM_SOUND);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500206 channel.setSound(CUSTOM_SOUND);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400207 }
Chris Wren93bb8b82016-03-29 14:35:05 -0400208 }
209 if (buzzy) {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400210 if (defaultVibration) {
211 defaults |= Notification.DEFAULT_VIBRATE;
212 } else {
213 builder.setVibrate(CUSTOM_VIBRATION);
Julia Reynoldsf57de462016-11-23 11:31:46 -0500214 channel.setVibrationPattern(CUSTOM_VIBRATION);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400215 }
Julia Reynoldsf57de462016-11-23 11:31:46 -0500216 channel.enableVibration(true);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400217 }
218 if (lights) {
219 if (defaultLights) {
220 defaults |= Notification.DEFAULT_LIGHTS;
221 } else {
222 builder.setLights(CUSTOM_LIGHT_COLOR, CUSTOM_LIGHT_ON, CUSTOM_LIGHT_OFF);
223 }
Julia Reynolds0c299d42016-11-15 14:37:04 -0500224 channel.setLights(true);
Chris Wren93bb8b82016-03-29 14:35:05 -0400225 }
226 builder.setDefaults(defaults);
227
228 Notification n = builder.build();
229 if (insistent) {
230 n.flags |= Notification.FLAG_INSISTENT;
231 }
Julia Reynolds0c299d42016-11-15 14:37:04 -0500232
Julia Reynolds423b9fc2016-11-09 09:51:08 -0500233 StatusBarNotification sbn = new StatusBarNotification(mPkg, mPkg, channel, id, mTag, mUid,
234 mPid, n, mUser, null, System.currentTimeMillis());
235 NotificationRecord r = new NotificationRecord(getContext(), sbn);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400236 mService.addNotification(r);
237 return r;
Chris Wren93bb8b82016-03-29 14:35:05 -0400238 }
239
240 //
241 // Convenience functions for interacting with mocks
242 //
243
244 private void verifyNeverBeep() throws RemoteException {
245 verify(mRingtonePlayer, never()).playAsync((Uri) anyObject(), (UserHandle) anyObject(),
246 anyBoolean(), (AudioAttributes) anyObject());
247 }
248
249 private void verifyBeep() throws RemoteException {
250 verify(mRingtonePlayer, times(1)).playAsync((Uri) anyObject(), (UserHandle) anyObject(),
251 eq(true), (AudioAttributes) anyObject());
252 }
253
254 private void verifyBeepLooped() throws RemoteException {
255 verify(mRingtonePlayer, times(1)).playAsync((Uri) anyObject(), (UserHandle) anyObject(),
256 eq(false), (AudioAttributes) anyObject());
257 }
258
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400259 private void verifyCustomBeep() throws RemoteException {
260 verify(mRingtonePlayer, times(1)).playAsync(eq(CUSTOM_SOUND), (UserHandle) anyObject(),
261 eq(false), (AudioAttributes) anyObject());
262 }
263
Chris Wren93bb8b82016-03-29 14:35:05 -0400264 private void verifyNeverStopAudio() throws RemoteException {
265 verify(mRingtonePlayer, never()).stopAsync();
266 }
267
268 private void verifyStopAudio() throws RemoteException {
269 verify(mRingtonePlayer, times(1)).stopAsync();
270 }
271
272 private void verifyNeverVibrate() {
273 verify(mVibrator, never()).vibrate(anyInt(), anyString(), (long[]) anyObject(),
274 anyInt(), (AudioAttributes) anyObject());
275 }
276
277 private void verifyVibrate() {
278 verify(mVibrator, times(1)).vibrate(anyInt(), anyString(), (long[]) anyObject(),
279 eq(-1), (AudioAttributes) anyObject());
280 }
281
282 private void verifyVibrateLooped() {
283 verify(mVibrator, times(1)).vibrate(anyInt(), anyString(), (long[]) anyObject(),
284 eq(0), (AudioAttributes) anyObject());
285 }
286
287 private void verifyStopVibrate() {
288 verify(mVibrator, times(1)).cancel();
289 }
290
291 private void verifyNeverStopVibrate() throws RemoteException {
292 verify(mVibrator, never()).cancel();
293 }
294
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400295 private void verifyLights() {
296 verify(mStatusBar, times(1)).notificationLightPulse(anyInt(), anyInt(), anyInt());
297 }
298
299 private void verifyCustomLights() {
300 verify(mStatusBar, times(1)).notificationLightPulse(
301 eq(CUSTOM_LIGHT_COLOR), eq(CUSTOM_LIGHT_ON), eq(CUSTOM_LIGHT_OFF));
302 }
303
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400304 private Context getContext() {
305 return InstrumentationRegistry.getTargetContext();
306 }
307
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400308 //
309 // Tests
310 //
311
312 @Test
313 public void testLights() throws Exception {
314 NotificationRecord r = getLightsNotification();
315 r.setImportance(NotificationManager.IMPORTANCE_DEFAULT, "for testing");
316
317 mService.buzzBeepBlinkLocked(r);
318
319 verifyLights();
320 }
321
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400322 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400323 public void testBeep() throws Exception {
324 NotificationRecord r = getBeepyNotification();
325
326 mService.buzzBeepBlinkLocked(r);
327
328 verifyBeepLooped();
329 verifyNeverVibrate();
330 }
331
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400332 @Test
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400333 public void testLightsFromChannel() throws Exception {
334 NotificationRecord r = getQuietNotification();
335 r.setImportance(NotificationManager.IMPORTANCE_DEFAULT, "for testing");
336 r.getChannel().setLights(true);
337
338 mService.buzzBeepBlinkLocked(r);
339
340 verifyLights();
341 }
Chris Wren93bb8b82016-03-29 14:35:05 -0400342
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400343 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400344 public void testBeepInsistently() throws Exception {
345 NotificationRecord r = getInsistentBeepyNotification();
346
347 mService.buzzBeepBlinkLocked(r);
348
349 verifyBeep();
350 }
351
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400352 @Test
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400353 public void testChannelNoOverwriteCustomLights() throws Exception {
354 NotificationRecord r = getCustomLightsNotification();
355 r.getChannel().setLights(true);
356
357 mService.buzzBeepBlinkLocked(r);
358
359 verifyCustomLights();
360 }
361
362 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400363 public void testNoInterruptionForMin() throws Exception {
364 NotificationRecord r = getBeepyNotification();
Julia Reynolds85769912016-10-25 09:08:57 -0400365 r.setImportance(NotificationManager.IMPORTANCE_MIN, "foo");
Chris Wren93bb8b82016-03-29 14:35:05 -0400366
367 mService.buzzBeepBlinkLocked(r);
368
369 verifyNeverBeep();
370 verifyNeverVibrate();
371 }
372
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400373 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400374 public void testNoInterruptionForIntercepted() throws Exception {
375 NotificationRecord r = getBeepyNotification();
376 r.setIntercepted(true);
377
378 mService.buzzBeepBlinkLocked(r);
379
380 verifyNeverBeep();
381 verifyNeverVibrate();
382 }
383
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400384 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400385 public void testBeepTwice() throws Exception {
386 NotificationRecord r = getBeepyNotification();
387
388 // set up internal state
389 mService.buzzBeepBlinkLocked(r);
390 Mockito.reset(mRingtonePlayer);
391
392 // update should beep
393 r.isUpdate = true;
394 mService.buzzBeepBlinkLocked(r);
395 verifyBeepLooped();
396 }
397
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400398 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400399 public void testHonorAlertOnlyOnceForBeep() throws Exception {
400 NotificationRecord r = getBeepyNotification();
401 NotificationRecord s = getBeepyOnceNotification();
402 s.isUpdate = true;
403
404 // set up internal state
405 mService.buzzBeepBlinkLocked(r);
406 Mockito.reset(mRingtonePlayer);
407
408 // update should not beep
409 mService.buzzBeepBlinkLocked(s);
410 verifyNeverBeep();
411 }
412
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400413 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400414 public void testNoisyUpdateDoesNotCancelAudio() throws Exception {
415 NotificationRecord r = getBeepyNotification();
416
417 mService.buzzBeepBlinkLocked(r);
418 r.isUpdate = true;
419 mService.buzzBeepBlinkLocked(r);
420
421 verifyNeverStopAudio();
422 }
423
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400424 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400425 public void testNoisyOnceUpdateDoesNotCancelAudio() throws Exception {
426 NotificationRecord r = getBeepyNotification();
427 NotificationRecord s = getBeepyOnceNotification();
428 s.isUpdate = true;
429
430 mService.buzzBeepBlinkLocked(r);
431 mService.buzzBeepBlinkLocked(s);
432
433 verifyNeverStopAudio();
434 }
435
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400436 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400437 public void testQuietUpdateDoesNotCancelAudioFromOther() throws Exception {
438 NotificationRecord r = getBeepyNotification();
439 NotificationRecord s = getQuietNotification();
440 s.isUpdate = true;
441 NotificationRecord other = getNoisyOtherNotification();
442
443 // set up internal state
444 mService.buzzBeepBlinkLocked(r);
445 mService.buzzBeepBlinkLocked(other); // this takes the audio stream
446 Mockito.reset(mRingtonePlayer);
447
448 // should not stop noise, since we no longer own it
449 mService.buzzBeepBlinkLocked(s); // this no longer owns the stream
450 verifyNeverStopAudio();
451 }
452
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400453 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400454 public void testQuietInterloperDoesNotCancelAudio() throws Exception {
455 NotificationRecord r = getBeepyNotification();
456 NotificationRecord other = getQuietOtherNotification();
457
458 // set up internal state
459 mService.buzzBeepBlinkLocked(r);
460 Mockito.reset(mRingtonePlayer);
461
462 // should not stop noise, since it does not own it
463 mService.buzzBeepBlinkLocked(other);
464 verifyNeverStopAudio();
465 }
466
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400467 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400468 public void testQuietUpdateCancelsAudio() throws Exception {
469 NotificationRecord r = getBeepyNotification();
470 NotificationRecord s = getQuietNotification();
471 s.isUpdate = true;
472
473 // set up internal state
474 mService.buzzBeepBlinkLocked(r);
475 Mockito.reset(mRingtonePlayer);
476
477 // quiet update should stop making noise
478 mService.buzzBeepBlinkLocked(s);
479 verifyStopAudio();
480 }
481
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400482 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400483 public void testQuietOnceUpdateCancelsAudio() throws Exception {
484 NotificationRecord r = getBeepyNotification();
485 NotificationRecord s = getQuietOnceNotification();
486 s.isUpdate = true;
487
488 // set up internal state
489 mService.buzzBeepBlinkLocked(r);
490 Mockito.reset(mRingtonePlayer);
491
492 // stop making noise - this is a weird corner case, but quiet should override once
493 mService.buzzBeepBlinkLocked(s);
494 verifyStopAudio();
495 }
496
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400497 @Test
Julia Reynolds0c299d42016-11-15 14:37:04 -0500498 public void testNoDemoteSoundToVibrateIfVibrateGiven() throws Exception {
499 NotificationRecord r = getBuzzyBeepyNotification();
500 assertTrue(r.getSound() != null);
Chris Wren93bb8b82016-03-29 14:35:05 -0400501
502 // the phone is quiet
Chris Wren93bb8b82016-03-29 14:35:05 -0400503 when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_VIBRATE);
504
505 mService.buzzBeepBlinkLocked(r);
506
Julia Reynolds0c299d42016-11-15 14:37:04 -0500507 verify(mVibrator, times(1)).vibrate(anyInt(), anyString(), eq(r.getVibration()),
508 eq(-1), (AudioAttributes) anyObject());
Chris Wren93bb8b82016-03-29 14:35:05 -0400509 }
510
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400511 @Test
Julia Reynolds0c299d42016-11-15 14:37:04 -0500512 public void testDemoteSoundToVibrate() throws Exception {
513 NotificationRecord r = getBeepyNotification();
514 assertTrue(r.getSound() != null);
515 assertNull(r.getVibration());
516
517 // the phone is quiet
518 when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_VIBRATE);
519
520 mService.buzzBeepBlinkLocked(r);
521
522 verify(mVibrator, times(1)).vibrate(anyInt(), anyString(), eq(FALLBACK_VIBRATION),
523 eq(-1), (AudioAttributes) anyObject());
524 }
525
526 @Test
527 public void testDemoteInsistentSoundToVibrate() throws Exception {
Chris Wren93bb8b82016-03-29 14:35:05 -0400528 NotificationRecord r = getInsistentBeepyNotification();
Julia Reynolds0c299d42016-11-15 14:37:04 -0500529 assertTrue(r.getSound() != null);
530 assertNull(r.getVibration());
Chris Wren93bb8b82016-03-29 14:35:05 -0400531
532 // the phone is quiet
533 when(mAudioManager.getStreamVolume(anyInt())).thenReturn(0);
534 when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_VIBRATE);
535
536 mService.buzzBeepBlinkLocked(r);
537
538 verifyVibrateLooped();
539 }
540
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400541 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400542 public void testVibrate() throws Exception {
543 NotificationRecord r = getBuzzyNotification();
544
545 mService.buzzBeepBlinkLocked(r);
546
547 verifyNeverBeep();
548 verifyVibrate();
549 }
550
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400551 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400552 public void testInsistenteVibrate() throws Exception {
553 NotificationRecord r = getInsistentBuzzyNotification();
554
555 mService.buzzBeepBlinkLocked(r);
556 verifyVibrateLooped();
557 }
558
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400559 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400560 public void testVibratTwice() throws Exception {
561 NotificationRecord r = getBuzzyNotification();
562
563 // set up internal state
564 mService.buzzBeepBlinkLocked(r);
565 Mockito.reset(mVibrator);
566
567 // update should vibrate
568 r.isUpdate = true;
569 mService.buzzBeepBlinkLocked(r);
570 verifyVibrate();
571 }
572
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400573 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400574 public void testHonorAlertOnlyOnceForBuzz() throws Exception {
575 NotificationRecord r = getBuzzyNotification();
576 NotificationRecord s = getBuzzyOnceNotification();
577 s.isUpdate = true;
578
579 // set up internal state
580 mService.buzzBeepBlinkLocked(r);
581 Mockito.reset(mVibrator);
582
583 // update should not beep
584 mService.buzzBeepBlinkLocked(s);
585 verifyNeverVibrate();
586 }
587
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400588 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400589 public void testNoisyUpdateDoesNotCancelVibrate() throws Exception {
590 NotificationRecord r = getBuzzyNotification();
591
592 mService.buzzBeepBlinkLocked(r);
593 r.isUpdate = true;
594 mService.buzzBeepBlinkLocked(r);
595
596 verifyNeverStopVibrate();
597 }
598
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400599 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400600 public void testNoisyOnceUpdateDoesNotCancelVibrate() throws Exception {
601 NotificationRecord r = getBuzzyNotification();
602 NotificationRecord s = getBuzzyOnceNotification();
603 s.isUpdate = true;
604
605 mService.buzzBeepBlinkLocked(r);
606 mService.buzzBeepBlinkLocked(s);
607
608 verifyNeverStopVibrate();
609 }
610
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400611 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400612 public void testQuietUpdateDoesNotCancelVibrateFromOther() throws Exception {
613 NotificationRecord r = getBuzzyNotification();
614 NotificationRecord s = getQuietNotification();
615 s.isUpdate = true;
616 NotificationRecord other = getNoisyOtherNotification();
617
618 // set up internal state
619 mService.buzzBeepBlinkLocked(r);
620 mService.buzzBeepBlinkLocked(other); // this takes the vibrate stream
621 Mockito.reset(mVibrator);
622
623 // should not stop vibrate, since we no longer own it
624 mService.buzzBeepBlinkLocked(s); // this no longer owns the stream
625 verifyNeverStopVibrate();
626 }
627
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400628 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400629 public void testQuietInterloperDoesNotCancelVibrate() throws Exception {
630 NotificationRecord r = getBuzzyNotification();
631 NotificationRecord other = getQuietOtherNotification();
632
633 // set up internal state
634 mService.buzzBeepBlinkLocked(r);
635 Mockito.reset(mVibrator);
636
637 // should not stop noise, since it does not own it
638 mService.buzzBeepBlinkLocked(other);
639 verifyNeverStopVibrate();
640 }
641
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400642 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400643 public void testQuietUpdateCancelsVibrate() throws Exception {
644 NotificationRecord r = getBuzzyNotification();
645 NotificationRecord s = getQuietNotification();
646 s.isUpdate = true;
647
648 // set up internal state
649 mService.buzzBeepBlinkLocked(r);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500650 verifyVibrate();
Chris Wren93bb8b82016-03-29 14:35:05 -0400651
652 // quiet update should stop making noise
653 mService.buzzBeepBlinkLocked(s);
654 verifyStopVibrate();
655 }
656
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400657 @Test
Julia Reynolds0c299d42016-11-15 14:37:04 -0500658 public void testQuietOnceUpdateCancelVibrate() throws Exception {
Chris Wren93bb8b82016-03-29 14:35:05 -0400659 NotificationRecord r = getBuzzyNotification();
660 NotificationRecord s = getQuietOnceNotification();
661 s.isUpdate = true;
662
663 // set up internal state
664 mService.buzzBeepBlinkLocked(r);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500665 verifyVibrate();
Chris Wren93bb8b82016-03-29 14:35:05 -0400666
667 // stop making noise - this is a weird corner case, but quiet should override once
668 mService.buzzBeepBlinkLocked(s);
669 verifyStopVibrate();
670 }
671
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400672 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400673 public void testQuietUpdateCancelsDemotedVibrate() throws Exception {
674 NotificationRecord r = getBeepyNotification();
675 NotificationRecord s = getQuietNotification();
676
677 // the phone is quiet
678 when(mAudioManager.getStreamVolume(anyInt())).thenReturn(0);
679 when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_VIBRATE);
680
681 mService.buzzBeepBlinkLocked(r);
682
683 // quiet update should stop making noise
684 mService.buzzBeepBlinkLocked(s);
685 verifyStopVibrate();
686 }
687}