[go: nahoru, domu]

blob: 15f7557c92efe2d0d4dd53ff18597757f436df82 [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;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -040023
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040024import org.junit.Before;
25import org.junit.Test;
26import org.junit.runner.RunWith;
Chris Wren93bb8b82016-03-29 14:35:05 -040027
28import android.app.ActivityManager;
29import android.app.Notification;
30import android.app.Notification.Builder;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -040031import android.app.NotificationManager;
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040032import android.content.Context;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -040033import android.app.NotificationChannel;
34import android.graphics.Color;
Chris Wren93bb8b82016-03-29 14:35:05 -040035import android.media.AudioAttributes;
36import android.media.AudioManager;
37import android.net.Uri;
38import android.os.Handler;
39import android.os.RemoteException;
40import android.os.UserHandle;
41import android.os.Vibrator;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -040042import android.provider.Settings;
Chris Wren93bb8b82016-03-29 14:35:05 -040043import android.service.notification.StatusBarNotification;
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040044import android.support.test.InstrumentationRegistry;
45import android.support.test.runner.AndroidJUnit4;
Chris Wren93bb8b82016-03-29 14:35:05 -040046import android.test.suitebuilder.annotation.SmallTest;
47
48import org.mockito.Mock;
49import org.mockito.Mockito;
50import org.mockito.MockitoAnnotations;
51
52import static org.mockito.Matchers.anyBoolean;
53import static org.mockito.Matchers.anyInt;
54import static org.mockito.Matchers.anyObject;
55import static org.mockito.Matchers.anyString;
56import static org.mockito.Matchers.eq;
57import static org.mockito.Mockito.never;
58import static org.mockito.Mockito.times;
59import static org.mockito.Mockito.verify;
60import static org.mockito.Mockito.when;
61
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040062@SmallTest
63@RunWith(AndroidJUnit4.class)
64public class BuzzBeepBlinkTest {
Chris Wren93bb8b82016-03-29 14:35:05 -040065
66 @Mock AudioManager mAudioManager;
67 @Mock Vibrator mVibrator;
68 @Mock android.media.IRingtonePlayer mRingtonePlayer;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -040069 @Mock Light mLight;
Chris Wren93bb8b82016-03-29 14:35:05 -040070 @Mock Handler mHandler;
71
72 private NotificationManagerService mService;
73 private String mPkg = "com.android.server.notification";
74 private int mId = 1001;
75 private int mOtherId = 1002;
76 private String mTag = null;
77 private int mUid = 1000;
78 private int mPid = 2000;
Chris Wren93bb8b82016-03-29 14:35:05 -040079 private android.os.UserHandle mUser = UserHandle.of(ActivityManager.getCurrentUser());
80
Julia Reynoldsb5e44b72016-08-16 15:00:25 -040081 private static final long[] CUSTOM_VIBRATION = new long[] {
82 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400,
83 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400,
84 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400 };
85 private static final Uri CUSTOM_SOUND = Settings.System.DEFAULT_ALARM_ALERT_URI;
Julia Reynolds619a69f2017-01-27 15:11:38 -050086 private static final AudioAttributes CUSTOM_ATTRIBUTES = new AudioAttributes.Builder()
87 .setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
88 .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
89 .build();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -040090 private static final int CUSTOM_LIGHT_COLOR = Color.BLACK;
91 private static final int CUSTOM_LIGHT_ON = 10000;
92 private static final int CUSTOM_LIGHT_OFF = 10000;
Julia Reynolds0c299d42016-11-15 14:37:04 -050093 private static final long[] FALLBACK_VIBRATION = new long[] {100, 100, 100};
Julia Reynoldsb5e44b72016-08-16 15:00:25 -040094
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040095 @Before
Chris Wren93bb8b82016-03-29 14:35:05 -040096 public void setUp() {
97 MockitoAnnotations.initMocks(this);
98
99 when(mAudioManager.isAudioFocusExclusive()).thenReturn(false);
100 when(mAudioManager.getRingtonePlayer()).thenReturn(mRingtonePlayer);
101 when(mAudioManager.getStreamVolume(anyInt())).thenReturn(10);
102 when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_NORMAL);
103
104 mService = new NotificationManagerService(getContext());
105 mService.setAudioManager(mAudioManager);
106 mService.setVibrator(mVibrator);
107 mService.setSystemReady(true);
108 mService.setHandler(mHandler);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400109 mService.setLights(mLight);
110 mService.setScreenOn(false);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500111 mService.setFallbackVibrationPattern(FALLBACK_VIBRATION);
Chris Wren93bb8b82016-03-29 14:35:05 -0400112 }
113
114 //
115 // Convenience functions for creating notification records
116 //
117
118 private NotificationRecord getNoisyOtherNotification() {
119 return getNotificationRecord(mOtherId, false /* insistent */, false /* once */,
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400120 true /* noisy */, true /* buzzy*/, false /* lights */);
Chris Wren93bb8b82016-03-29 14:35:05 -0400121 }
122
123 private NotificationRecord getBeepyNotification() {
124 return getNotificationRecord(mId, false /* insistent */, false /* once */,
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400125 true /* noisy */, false /* buzzy*/, false /* lights */);
Chris Wren93bb8b82016-03-29 14:35:05 -0400126 }
127
128 private NotificationRecord getBeepyOnceNotification() {
129 return getNotificationRecord(mId, false /* insistent */, true /* once */,
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400130 true /* noisy */, false /* buzzy*/, false /* lights */);
Chris Wren93bb8b82016-03-29 14:35:05 -0400131 }
132
133 private NotificationRecord getQuietNotification() {
134 return getNotificationRecord(mId, false /* insistent */, false /* once */,
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400135 false /* noisy */, false /* buzzy*/, false /* lights */);
Chris Wren93bb8b82016-03-29 14:35:05 -0400136 }
137
138 private NotificationRecord getQuietOtherNotification() {
139 return getNotificationRecord(mOtherId, false /* insistent */, false /* once */,
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400140 false /* noisy */, false /* buzzy*/, false /* lights */);
Chris Wren93bb8b82016-03-29 14:35:05 -0400141 }
142
143 private NotificationRecord getQuietOnceNotification() {
144 return getNotificationRecord(mId, false /* insistent */, true /* once */,
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400145 false /* noisy */, false /* buzzy*/, false /* lights */);
Chris Wren93bb8b82016-03-29 14:35:05 -0400146 }
147
148 private NotificationRecord getInsistentBeepyNotification() {
149 return getNotificationRecord(mId, true /* insistent */, false /* once */,
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400150 true /* noisy */, false /* buzzy*/, false /* lights */);
Chris Wren93bb8b82016-03-29 14:35:05 -0400151 }
152
153 private NotificationRecord getBuzzyNotification() {
154 return getNotificationRecord(mId, false /* insistent */, false /* once */,
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400155 false /* noisy */, true /* buzzy*/, false /* lights */);
Chris Wren93bb8b82016-03-29 14:35:05 -0400156 }
157
158 private NotificationRecord getBuzzyOnceNotification() {
159 return getNotificationRecord(mId, false /* insistent */, true /* once */,
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400160 false /* noisy */, true /* buzzy*/, false /* lights */);
Chris Wren93bb8b82016-03-29 14:35:05 -0400161 }
162
163 private NotificationRecord getInsistentBuzzyNotification() {
164 return getNotificationRecord(mId, true /* insistent */, false /* once */,
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400165 false /* noisy */, true /* buzzy*/, false /* lights */);
166 }
167
Julia Reynolds0c299d42016-11-15 14:37:04 -0500168 private NotificationRecord getBuzzyBeepyNotification() {
169 return getNotificationRecord(mId, false /* insistent */, false /* once */,
170 true /* noisy */, true /* buzzy*/, false /* lights */);
171 }
172
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400173 private NotificationRecord getLightsNotification() {
174 return getNotificationRecord(mId, false /* insistent */, true /* once */,
175 false /* noisy */, true /* buzzy*/, true /* lights */);
176 }
177
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400178 private NotificationRecord getCustomLightsNotification() {
179 return getNotificationRecord(mId, false /* insistent */, true /* once */,
180 false /* noisy */, true /* buzzy*/, true /* lights */,
181 true /* defaultVibration */, true /* defaultSound */, false /* defaultLights */);
Chris Wren93bb8b82016-03-29 14:35:05 -0400182 }
183
184 private NotificationRecord getNotificationRecord(int id, boolean insistent, boolean once,
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400185 boolean noisy, boolean buzzy, boolean lights) {
186 return getNotificationRecord(id, insistent, once, noisy, buzzy, lights, true, true, true);
187 }
188
189 private NotificationRecord getNotificationRecord(int id, boolean insistent, boolean once,
190 boolean noisy, boolean buzzy, boolean lights, boolean defaultVibration,
191 boolean defaultSound, boolean defaultLights) {
Julia Reynolds0c299d42016-11-15 14:37:04 -0500192 NotificationChannel channel =
193 new NotificationChannel("test", "test", NotificationManager.IMPORTANCE_HIGH);
Chris Wren93bb8b82016-03-29 14:35:05 -0400194 final Builder builder = new Builder(getContext())
195 .setContentTitle("foo")
196 .setSmallIcon(android.R.drawable.sym_def_app_icon)
197 .setPriority(Notification.PRIORITY_HIGH)
198 .setOnlyAlertOnce(once);
199
200 int defaults = 0;
201 if (noisy) {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400202 if (defaultSound) {
203 defaults |= Notification.DEFAULT_SOUND;
Julia Reynolds619a69f2017-01-27 15:11:38 -0500204 channel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI,
205 Notification.AUDIO_ATTRIBUTES_DEFAULT);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400206 } else {
207 builder.setSound(CUSTOM_SOUND);
Julia Reynolds619a69f2017-01-27 15:11:38 -0500208 channel.setSound(CUSTOM_SOUND, CUSTOM_ATTRIBUTES);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400209 }
Chris Wren93bb8b82016-03-29 14:35:05 -0400210 }
211 if (buzzy) {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400212 if (defaultVibration) {
213 defaults |= Notification.DEFAULT_VIBRATE;
214 } else {
215 builder.setVibrate(CUSTOM_VIBRATION);
Julia Reynoldsf57de462016-11-23 11:31:46 -0500216 channel.setVibrationPattern(CUSTOM_VIBRATION);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400217 }
Julia Reynoldsf57de462016-11-23 11:31:46 -0500218 channel.enableVibration(true);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400219 }
220 if (lights) {
221 if (defaultLights) {
222 defaults |= Notification.DEFAULT_LIGHTS;
223 } else {
224 builder.setLights(CUSTOM_LIGHT_COLOR, CUSTOM_LIGHT_ON, CUSTOM_LIGHT_OFF);
225 }
Julia Reynolds529e3322017-02-06 08:33:01 -0500226 channel.enableLights(true);
Chris Wren93bb8b82016-03-29 14:35:05 -0400227 }
228 builder.setDefaults(defaults);
229
230 Notification n = builder.build();
231 if (insistent) {
232 n.flags |= Notification.FLAG_INSISTENT;
233 }
Julia Reynolds0c299d42016-11-15 14:37:04 -0500234
Julia Reynolds924eed12017-01-19 09:52:07 -0500235 StatusBarNotification sbn = new StatusBarNotification(mPkg, mPkg, id, mTag, mUid,
Julia Reynolds423b9fc2016-11-09 09:51:08 -0500236 mPid, n, mUser, null, System.currentTimeMillis());
Julia Reynolds924eed12017-01-19 09:52:07 -0500237 NotificationRecord r = new NotificationRecord(getContext(), sbn, channel);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400238 mService.addNotification(r);
239 return r;
Chris Wren93bb8b82016-03-29 14:35:05 -0400240 }
241
242 //
243 // Convenience functions for interacting with mocks
244 //
245
246 private void verifyNeverBeep() throws RemoteException {
247 verify(mRingtonePlayer, never()).playAsync((Uri) anyObject(), (UserHandle) anyObject(),
248 anyBoolean(), (AudioAttributes) anyObject());
249 }
250
251 private void verifyBeep() throws RemoteException {
252 verify(mRingtonePlayer, times(1)).playAsync((Uri) anyObject(), (UserHandle) anyObject(),
253 eq(true), (AudioAttributes) anyObject());
254 }
255
256 private void verifyBeepLooped() throws RemoteException {
257 verify(mRingtonePlayer, times(1)).playAsync((Uri) anyObject(), (UserHandle) anyObject(),
258 eq(false), (AudioAttributes) anyObject());
259 }
260
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400261 private void verifyCustomBeep() throws RemoteException {
262 verify(mRingtonePlayer, times(1)).playAsync(eq(CUSTOM_SOUND), (UserHandle) anyObject(),
263 eq(false), (AudioAttributes) anyObject());
264 }
265
Chris Wren93bb8b82016-03-29 14:35:05 -0400266 private void verifyNeverStopAudio() throws RemoteException {
267 verify(mRingtonePlayer, never()).stopAsync();
268 }
269
270 private void verifyStopAudio() throws RemoteException {
271 verify(mRingtonePlayer, times(1)).stopAsync();
272 }
273
274 private void verifyNeverVibrate() {
275 verify(mVibrator, never()).vibrate(anyInt(), anyString(), (long[]) anyObject(),
276 anyInt(), (AudioAttributes) anyObject());
277 }
278
279 private void verifyVibrate() {
280 verify(mVibrator, times(1)).vibrate(anyInt(), anyString(), (long[]) anyObject(),
281 eq(-1), (AudioAttributes) anyObject());
282 }
283
284 private void verifyVibrateLooped() {
285 verify(mVibrator, times(1)).vibrate(anyInt(), anyString(), (long[]) anyObject(),
286 eq(0), (AudioAttributes) anyObject());
287 }
288
289 private void verifyStopVibrate() {
290 verify(mVibrator, times(1)).cancel();
291 }
292
293 private void verifyNeverStopVibrate() throws RemoteException {
294 verify(mVibrator, never()).cancel();
295 }
296
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400297 private void verifyLights() {
Julia Reynolds033a4122017-01-31 16:50:38 -0500298 verify(mLight, times(1)).setFlashing(anyInt(), anyInt(), anyInt(), anyInt());
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400299 }
300
301 private void verifyCustomLights() {
Julia Reynolds033a4122017-01-31 16:50:38 -0500302 verify(mLight, times(1)).setFlashing(
303 eq(CUSTOM_LIGHT_COLOR), anyInt(), eq(CUSTOM_LIGHT_ON), eq(CUSTOM_LIGHT_OFF));
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400304 }
305
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400306 private Context getContext() {
307 return InstrumentationRegistry.getTargetContext();
308 }
309
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400310 //
311 // Tests
312 //
313
314 @Test
315 public void testLights() throws Exception {
316 NotificationRecord r = getLightsNotification();
317 r.setImportance(NotificationManager.IMPORTANCE_DEFAULT, "for testing");
318
319 mService.buzzBeepBlinkLocked(r);
320
321 verifyLights();
322 }
323
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400324 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400325 public void testBeep() throws Exception {
326 NotificationRecord r = getBeepyNotification();
327
328 mService.buzzBeepBlinkLocked(r);
329
330 verifyBeepLooped();
331 verifyNeverVibrate();
332 }
333
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400334 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400335 public void testBeepInsistently() throws Exception {
336 NotificationRecord r = getInsistentBeepyNotification();
337
338 mService.buzzBeepBlinkLocked(r);
339
340 verifyBeep();
341 }
342
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400343 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400344 public void testNoInterruptionForMin() throws Exception {
345 NotificationRecord r = getBeepyNotification();
Julia Reynolds85769912016-10-25 09:08:57 -0400346 r.setImportance(NotificationManager.IMPORTANCE_MIN, "foo");
Chris Wren93bb8b82016-03-29 14:35:05 -0400347
348 mService.buzzBeepBlinkLocked(r);
349
350 verifyNeverBeep();
351 verifyNeverVibrate();
352 }
353
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400354 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400355 public void testNoInterruptionForIntercepted() throws Exception {
356 NotificationRecord r = getBeepyNotification();
357 r.setIntercepted(true);
358
359 mService.buzzBeepBlinkLocked(r);
360
361 verifyNeverBeep();
362 verifyNeverVibrate();
363 }
364
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400365 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400366 public void testBeepTwice() throws Exception {
367 NotificationRecord r = getBeepyNotification();
368
369 // set up internal state
370 mService.buzzBeepBlinkLocked(r);
371 Mockito.reset(mRingtonePlayer);
372
373 // update should beep
374 r.isUpdate = true;
375 mService.buzzBeepBlinkLocked(r);
376 verifyBeepLooped();
377 }
378
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400379 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400380 public void testHonorAlertOnlyOnceForBeep() throws Exception {
381 NotificationRecord r = getBeepyNotification();
382 NotificationRecord s = getBeepyOnceNotification();
383 s.isUpdate = true;
384
385 // set up internal state
386 mService.buzzBeepBlinkLocked(r);
387 Mockito.reset(mRingtonePlayer);
388
389 // update should not beep
390 mService.buzzBeepBlinkLocked(s);
391 verifyNeverBeep();
392 }
393
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400394 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400395 public void testNoisyUpdateDoesNotCancelAudio() throws Exception {
396 NotificationRecord r = getBeepyNotification();
397
398 mService.buzzBeepBlinkLocked(r);
399 r.isUpdate = true;
400 mService.buzzBeepBlinkLocked(r);
401
402 verifyNeverStopAudio();
403 }
404
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400405 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400406 public void testNoisyOnceUpdateDoesNotCancelAudio() throws Exception {
407 NotificationRecord r = getBeepyNotification();
408 NotificationRecord s = getBeepyOnceNotification();
409 s.isUpdate = true;
410
411 mService.buzzBeepBlinkLocked(r);
412 mService.buzzBeepBlinkLocked(s);
413
414 verifyNeverStopAudio();
415 }
416
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400417 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400418 public void testQuietUpdateDoesNotCancelAudioFromOther() throws Exception {
419 NotificationRecord r = getBeepyNotification();
420 NotificationRecord s = getQuietNotification();
421 s.isUpdate = true;
422 NotificationRecord other = getNoisyOtherNotification();
423
424 // set up internal state
425 mService.buzzBeepBlinkLocked(r);
426 mService.buzzBeepBlinkLocked(other); // this takes the audio stream
427 Mockito.reset(mRingtonePlayer);
428
429 // should not stop noise, since we no longer own it
430 mService.buzzBeepBlinkLocked(s); // this no longer owns the stream
431 verifyNeverStopAudio();
432 }
433
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400434 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400435 public void testQuietInterloperDoesNotCancelAudio() throws Exception {
436 NotificationRecord r = getBeepyNotification();
437 NotificationRecord other = getQuietOtherNotification();
438
439 // set up internal state
440 mService.buzzBeepBlinkLocked(r);
441 Mockito.reset(mRingtonePlayer);
442
443 // should not stop noise, since it does not own it
444 mService.buzzBeepBlinkLocked(other);
445 verifyNeverStopAudio();
446 }
447
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400448 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400449 public void testQuietUpdateCancelsAudio() throws Exception {
450 NotificationRecord r = getBeepyNotification();
451 NotificationRecord s = getQuietNotification();
452 s.isUpdate = true;
453
454 // set up internal state
455 mService.buzzBeepBlinkLocked(r);
456 Mockito.reset(mRingtonePlayer);
457
458 // quiet update should stop making noise
459 mService.buzzBeepBlinkLocked(s);
460 verifyStopAudio();
461 }
462
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400463 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400464 public void testQuietOnceUpdateCancelsAudio() throws Exception {
465 NotificationRecord r = getBeepyNotification();
466 NotificationRecord s = getQuietOnceNotification();
467 s.isUpdate = true;
468
469 // set up internal state
470 mService.buzzBeepBlinkLocked(r);
471 Mockito.reset(mRingtonePlayer);
472
473 // stop making noise - this is a weird corner case, but quiet should override once
474 mService.buzzBeepBlinkLocked(s);
475 verifyStopAudio();
476 }
477
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400478 @Test
Julia Reynolds0c299d42016-11-15 14:37:04 -0500479 public void testNoDemoteSoundToVibrateIfVibrateGiven() throws Exception {
480 NotificationRecord r = getBuzzyBeepyNotification();
481 assertTrue(r.getSound() != null);
Chris Wren93bb8b82016-03-29 14:35:05 -0400482
483 // the phone is quiet
Chris Wren93bb8b82016-03-29 14:35:05 -0400484 when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_VIBRATE);
485
486 mService.buzzBeepBlinkLocked(r);
487
Julia Reynolds0c299d42016-11-15 14:37:04 -0500488 verify(mVibrator, times(1)).vibrate(anyInt(), anyString(), eq(r.getVibration()),
489 eq(-1), (AudioAttributes) anyObject());
Chris Wren93bb8b82016-03-29 14:35:05 -0400490 }
491
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400492 @Test
Julia Reynolds0c299d42016-11-15 14:37:04 -0500493 public void testDemoteSoundToVibrate() throws Exception {
494 NotificationRecord r = getBeepyNotification();
495 assertTrue(r.getSound() != null);
496 assertNull(r.getVibration());
497
498 // the phone is quiet
499 when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_VIBRATE);
500
501 mService.buzzBeepBlinkLocked(r);
502
503 verify(mVibrator, times(1)).vibrate(anyInt(), anyString(), eq(FALLBACK_VIBRATION),
504 eq(-1), (AudioAttributes) anyObject());
Julia Reynolds619a69f2017-01-27 15:11:38 -0500505 verify(mRingtonePlayer, never()).playAsync
506 (anyObject(), anyObject(), anyBoolean(), anyObject());
Julia Reynolds0c299d42016-11-15 14:37:04 -0500507 }
508
509 @Test
510 public void testDemoteInsistentSoundToVibrate() throws Exception {
Chris Wren93bb8b82016-03-29 14:35:05 -0400511 NotificationRecord r = getInsistentBeepyNotification();
Julia Reynolds0c299d42016-11-15 14:37:04 -0500512 assertTrue(r.getSound() != null);
513 assertNull(r.getVibration());
Chris Wren93bb8b82016-03-29 14:35:05 -0400514
515 // the phone is quiet
516 when(mAudioManager.getStreamVolume(anyInt())).thenReturn(0);
517 when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_VIBRATE);
518
519 mService.buzzBeepBlinkLocked(r);
520
521 verifyVibrateLooped();
522 }
523
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400524 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400525 public void testVibrate() throws Exception {
526 NotificationRecord r = getBuzzyNotification();
527
528 mService.buzzBeepBlinkLocked(r);
529
530 verifyNeverBeep();
531 verifyVibrate();
532 }
533
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400534 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400535 public void testInsistenteVibrate() throws Exception {
536 NotificationRecord r = getInsistentBuzzyNotification();
537
538 mService.buzzBeepBlinkLocked(r);
539 verifyVibrateLooped();
540 }
541
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400542 @Test
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500543 public void testVibrateTwice() throws Exception {
Chris Wren93bb8b82016-03-29 14:35:05 -0400544 NotificationRecord r = getBuzzyNotification();
545
546 // set up internal state
547 mService.buzzBeepBlinkLocked(r);
548 Mockito.reset(mVibrator);
549
550 // update should vibrate
551 r.isUpdate = true;
552 mService.buzzBeepBlinkLocked(r);
553 verifyVibrate();
554 }
555
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400556 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400557 public void testHonorAlertOnlyOnceForBuzz() throws Exception {
558 NotificationRecord r = getBuzzyNotification();
559 NotificationRecord s = getBuzzyOnceNotification();
560 s.isUpdate = true;
561
562 // set up internal state
563 mService.buzzBeepBlinkLocked(r);
564 Mockito.reset(mVibrator);
565
566 // update should not beep
567 mService.buzzBeepBlinkLocked(s);
568 verifyNeverVibrate();
569 }
570
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400571 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400572 public void testNoisyUpdateDoesNotCancelVibrate() throws Exception {
573 NotificationRecord r = getBuzzyNotification();
574
575 mService.buzzBeepBlinkLocked(r);
576 r.isUpdate = true;
577 mService.buzzBeepBlinkLocked(r);
578
579 verifyNeverStopVibrate();
580 }
581
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400582 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400583 public void testNoisyOnceUpdateDoesNotCancelVibrate() throws Exception {
584 NotificationRecord r = getBuzzyNotification();
585 NotificationRecord s = getBuzzyOnceNotification();
586 s.isUpdate = true;
587
588 mService.buzzBeepBlinkLocked(r);
589 mService.buzzBeepBlinkLocked(s);
590
591 verifyNeverStopVibrate();
592 }
593
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400594 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400595 public void testQuietUpdateDoesNotCancelVibrateFromOther() throws Exception {
596 NotificationRecord r = getBuzzyNotification();
597 NotificationRecord s = getQuietNotification();
598 s.isUpdate = true;
599 NotificationRecord other = getNoisyOtherNotification();
600
601 // set up internal state
602 mService.buzzBeepBlinkLocked(r);
603 mService.buzzBeepBlinkLocked(other); // this takes the vibrate stream
604 Mockito.reset(mVibrator);
605
606 // should not stop vibrate, since we no longer own it
607 mService.buzzBeepBlinkLocked(s); // this no longer owns the stream
608 verifyNeverStopVibrate();
609 }
610
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400611 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400612 public void testQuietInterloperDoesNotCancelVibrate() throws Exception {
613 NotificationRecord r = getBuzzyNotification();
614 NotificationRecord other = getQuietOtherNotification();
615
616 // set up internal state
617 mService.buzzBeepBlinkLocked(r);
618 Mockito.reset(mVibrator);
619
620 // should not stop noise, since it does not own it
621 mService.buzzBeepBlinkLocked(other);
622 verifyNeverStopVibrate();
623 }
624
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400625 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400626 public void testQuietUpdateCancelsVibrate() throws Exception {
627 NotificationRecord r = getBuzzyNotification();
628 NotificationRecord s = getQuietNotification();
629 s.isUpdate = true;
630
631 // set up internal state
632 mService.buzzBeepBlinkLocked(r);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500633 verifyVibrate();
Chris Wren93bb8b82016-03-29 14:35:05 -0400634
635 // quiet update should stop making noise
636 mService.buzzBeepBlinkLocked(s);
637 verifyStopVibrate();
638 }
639
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400640 @Test
Julia Reynolds0c299d42016-11-15 14:37:04 -0500641 public void testQuietOnceUpdateCancelVibrate() throws Exception {
Chris Wren93bb8b82016-03-29 14:35:05 -0400642 NotificationRecord r = getBuzzyNotification();
643 NotificationRecord s = getQuietOnceNotification();
644 s.isUpdate = true;
645
646 // set up internal state
647 mService.buzzBeepBlinkLocked(r);
Julia Reynolds0c299d42016-11-15 14:37:04 -0500648 verifyVibrate();
Chris Wren93bb8b82016-03-29 14:35:05 -0400649
650 // stop making noise - this is a weird corner case, but quiet should override once
651 mService.buzzBeepBlinkLocked(s);
652 verifyStopVibrate();
653 }
654
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400655 @Test
Chris Wren93bb8b82016-03-29 14:35:05 -0400656 public void testQuietUpdateCancelsDemotedVibrate() throws Exception {
657 NotificationRecord r = getBeepyNotification();
658 NotificationRecord s = getQuietNotification();
659
660 // the phone is quiet
661 when(mAudioManager.getStreamVolume(anyInt())).thenReturn(0);
662 when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_VIBRATE);
663
664 mService.buzzBeepBlinkLocked(r);
665
666 // quiet update should stop making noise
667 mService.buzzBeepBlinkLocked(s);
668 verifyStopVibrate();
669 }
670}