Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.android.server.notification; |
| 17 | |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 18 | import static android.app.Notification.GROUP_ALERT_ALL; |
| 19 | import static android.app.Notification.GROUP_ALERT_CHILDREN; |
| 20 | import static android.app.Notification.GROUP_ALERT_SUMMARY; |
Julia Reynolds | ca80d16 | 2017-04-23 12:39:32 -0400 | [diff] [blame] | 21 | import static android.app.NotificationManager.IMPORTANCE_HIGH; |
Julia Reynolds | 5436923 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 22 | import static android.app.NotificationManager.IMPORTANCE_LOW; |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 23 | import static android.app.NotificationManager.IMPORTANCE_MIN; |
Julia Reynolds | 5436923 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 24 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_LIGHTS; |
Julia Reynolds | ca80d16 | 2017-04-23 12:39:32 -0400 | [diff] [blame] | 25 | |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 26 | import static junit.framework.Assert.assertFalse; |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 27 | import static junit.framework.Assert.assertNull; |
| 28 | import static junit.framework.Assert.assertTrue; |
| 29 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 30 | import static org.mockito.Matchers.any; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 31 | import static org.mockito.Matchers.anyBoolean; |
| 32 | import static org.mockito.Matchers.anyInt; |
| 33 | import static org.mockito.Matchers.anyObject; |
| 34 | import static org.mockito.Matchers.anyString; |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 35 | import static org.mockito.Matchers.argThat; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 36 | import static org.mockito.Matchers.eq; |
| 37 | import static org.mockito.Mockito.never; |
Tony Mantler | ab55f0f | 2017-06-16 10:50:00 -0700 | [diff] [blame] | 38 | import static org.mockito.Mockito.spy; |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 39 | import static org.mockito.Mockito.timeout; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 40 | import static org.mockito.Mockito.times; |
| 41 | import static org.mockito.Mockito.verify; |
| 42 | import static org.mockito.Mockito.when; |
| 43 | |
Tony Mantler | ab55f0f | 2017-06-16 10:50:00 -0700 | [diff] [blame] | 44 | import android.app.ActivityManager; |
| 45 | import android.app.Notification; |
| 46 | import android.app.Notification.Builder; |
| 47 | import android.app.NotificationChannel; |
| 48 | import android.app.NotificationManager; |
| 49 | import android.content.Context; |
| 50 | import android.content.pm.PackageManager; |
| 51 | import android.graphics.Color; |
| 52 | import android.media.AudioAttributes; |
| 53 | import android.media.AudioManager; |
| 54 | import android.net.Uri; |
| 55 | import android.os.Handler; |
| 56 | import android.os.RemoteException; |
| 57 | import android.os.UserHandle; |
| 58 | import android.os.VibrationEffect; |
| 59 | import android.os.Vibrator; |
| 60 | import android.provider.Settings; |
Julia Reynolds | 9aee284 | 2019-01-03 14:35:38 -0500 | [diff] [blame] | 61 | import android.service.notification.NotificationListenerService; |
Tony Mantler | ab55f0f | 2017-06-16 10:50:00 -0700 | [diff] [blame] | 62 | import android.service.notification.StatusBarNotification; |
| 63 | import android.support.test.runner.AndroidJUnit4; |
| 64 | import android.test.suitebuilder.annotation.SmallTest; |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 65 | import android.view.accessibility.AccessibilityEvent; |
| 66 | import android.view.accessibility.AccessibilityManager; |
| 67 | import android.view.accessibility.IAccessibilityManager; |
| 68 | import android.view.accessibility.IAccessibilityManagerClient; |
Tony Mantler | ab55f0f | 2017-06-16 10:50:00 -0700 | [diff] [blame] | 69 | |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 70 | import com.android.internal.util.IntPair; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 71 | import com.android.server.UiServiceTestCase; |
Tony Mantler | ab55f0f | 2017-06-16 10:50:00 -0700 | [diff] [blame] | 72 | import com.android.server.lights.Light; |
| 73 | |
| 74 | import org.junit.Before; |
| 75 | import org.junit.Test; |
| 76 | import org.junit.runner.RunWith; |
| 77 | import org.mockito.ArgumentMatcher; |
| 78 | import org.mockito.Mock; |
| 79 | import org.mockito.Mockito; |
| 80 | import org.mockito.MockitoAnnotations; |
| 81 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 82 | @SmallTest |
| 83 | @RunWith(AndroidJUnit4.class) |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 84 | public class BuzzBeepBlinkTest extends UiServiceTestCase { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 85 | |
| 86 | @Mock AudioManager mAudioManager; |
| 87 | @Mock Vibrator mVibrator; |
| 88 | @Mock android.media.IRingtonePlayer mRingtonePlayer; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 89 | @Mock Light mLight; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 90 | @Mock |
| 91 | NotificationManagerService.WorkerHandler mHandler; |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 92 | @Mock |
| 93 | NotificationUsageStats mUsageStats; |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 94 | @Mock |
| 95 | IAccessibilityManager mAccessibilityService; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 96 | |
| 97 | private NotificationManagerService mService; |
| 98 | private String mPkg = "com.android.server.notification"; |
| 99 | private int mId = 1001; |
| 100 | private int mOtherId = 1002; |
| 101 | private String mTag = null; |
| 102 | private int mUid = 1000; |
| 103 | private int mPid = 2000; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 104 | private android.os.UserHandle mUser = UserHandle.of(ActivityManager.getCurrentUser()); |
| 105 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 106 | private VibrateRepeatMatcher mVibrateOnceMatcher = new VibrateRepeatMatcher(-1); |
| 107 | private VibrateRepeatMatcher mVibrateLoopMatcher = new VibrateRepeatMatcher(0); |
| 108 | |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 109 | private static final long[] CUSTOM_VIBRATION = new long[] { |
| 110 | 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, |
| 111 | 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, |
| 112 | 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400 }; |
| 113 | private static final Uri CUSTOM_SOUND = Settings.System.DEFAULT_ALARM_ALERT_URI; |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 114 | private static final AudioAttributes CUSTOM_ATTRIBUTES = new AudioAttributes.Builder() |
| 115 | .setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN) |
Julia Reynolds | 9aee284 | 2019-01-03 14:35:38 -0500 | [diff] [blame] | 116 | .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION) |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 117 | .build(); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 118 | private static final int CUSTOM_LIGHT_COLOR = Color.BLACK; |
| 119 | private static final int CUSTOM_LIGHT_ON = 10000; |
| 120 | private static final int CUSTOM_LIGHT_OFF = 10000; |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 121 | private static final long[] FALLBACK_VIBRATION_PATTERN = new long[] {100, 100, 100}; |
| 122 | private static final VibrationEffect FALLBACK_VIBRATION = |
| 123 | VibrationEffect.createWaveform(FALLBACK_VIBRATION_PATTERN, -1); |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 124 | private static final int MAX_VIBRATION_DELAY = 1000; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 125 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 126 | @Before |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 127 | public void setUp() throws Exception { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 128 | MockitoAnnotations.initMocks(this); |
| 129 | |
| 130 | when(mAudioManager.isAudioFocusExclusive()).thenReturn(false); |
| 131 | when(mAudioManager.getRingtonePlayer()).thenReturn(mRingtonePlayer); |
| 132 | when(mAudioManager.getStreamVolume(anyInt())).thenReturn(10); |
| 133 | when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_NORMAL); |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 134 | when(mUsageStats.isAlertRateLimited(any())).thenReturn(false); |
| 135 | |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 136 | long serviceReturnValue = IntPair.of( |
| 137 | AccessibilityManager.STATE_FLAG_ACCESSIBILITY_ENABLED, |
| 138 | AccessibilityEvent.TYPES_ALL_MASK); |
| 139 | when(mAccessibilityService.addClient(any(), anyInt())).thenReturn(serviceReturnValue); |
| 140 | AccessibilityManager accessibilityManager = |
| 141 | new AccessibilityManager(Handler.getMain(), mAccessibilityService, 0); |
| 142 | verify(mAccessibilityService).addClient(any(IAccessibilityManagerClient.class), anyInt()); |
| 143 | assertTrue(accessibilityManager.isEnabled()); |
| 144 | |
| 145 | mService = spy(new NotificationManagerService(getContext())); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 146 | mService.setAudioManager(mAudioManager); |
| 147 | mService.setVibrator(mVibrator); |
| 148 | mService.setSystemReady(true); |
| 149 | mService.setHandler(mHandler); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 150 | mService.setLights(mLight); |
| 151 | mService.setScreenOn(false); |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 152 | mService.setFallbackVibrationPattern(FALLBACK_VIBRATION_PATTERN); |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 153 | mService.setUsageStats(mUsageStats); |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 154 | mService.setAccessibilityManager(accessibilityManager); |
Julia Reynolds | 5436923 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 155 | mService.mScreenOn = false; |
| 156 | mService.mInCall = false; |
| 157 | mService.mNotificationPulseEnabled = true; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // |
| 161 | // Convenience functions for creating notification records |
| 162 | // |
| 163 | |
| 164 | private NotificationRecord getNoisyOtherNotification() { |
| 165 | return getNotificationRecord(mOtherId, false /* insistent */, false /* once */, |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 166 | true /* noisy */, true /* buzzy*/, false /* lights */); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | private NotificationRecord getBeepyNotification() { |
| 170 | return getNotificationRecord(mId, false /* insistent */, false /* once */, |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 171 | true /* noisy */, false /* buzzy*/, false /* lights */); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | private NotificationRecord getBeepyOnceNotification() { |
| 175 | return getNotificationRecord(mId, false /* insistent */, true /* once */, |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 176 | true /* noisy */, false /* buzzy*/, false /* lights */); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | private NotificationRecord getQuietNotification() { |
| 180 | return getNotificationRecord(mId, false /* insistent */, false /* once */, |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 181 | false /* noisy */, false /* buzzy*/, false /* lights */); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | private NotificationRecord getQuietOtherNotification() { |
| 185 | return getNotificationRecord(mOtherId, false /* insistent */, false /* once */, |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 186 | false /* noisy */, false /* buzzy*/, false /* lights */); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | private NotificationRecord getQuietOnceNotification() { |
| 190 | return getNotificationRecord(mId, false /* insistent */, true /* once */, |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 191 | false /* noisy */, false /* buzzy*/, false /* lights */); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | private NotificationRecord getInsistentBeepyNotification() { |
| 195 | return getNotificationRecord(mId, true /* insistent */, false /* once */, |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 196 | true /* noisy */, false /* buzzy*/, false /* lights */); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 197 | } |
| 198 | |
Tony Mantler | ab55f0f | 2017-06-16 10:50:00 -0700 | [diff] [blame] | 199 | private NotificationRecord getInsistentBeepyLeanbackNotification() { |
| 200 | return getLeanbackNotificationRecord(mId, true /* insistent */, false /* once */, |
| 201 | true /* noisy */, false /* buzzy*/, false /* lights */); |
| 202 | } |
| 203 | |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 204 | private NotificationRecord getBuzzyNotification() { |
| 205 | return getNotificationRecord(mId, false /* insistent */, false /* once */, |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 206 | false /* noisy */, true /* buzzy*/, false /* lights */); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | private NotificationRecord getBuzzyOnceNotification() { |
| 210 | return getNotificationRecord(mId, false /* insistent */, true /* once */, |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 211 | false /* noisy */, true /* buzzy*/, false /* lights */); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | private NotificationRecord getInsistentBuzzyNotification() { |
| 215 | return getNotificationRecord(mId, true /* insistent */, false /* once */, |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 216 | false /* noisy */, true /* buzzy*/, false /* lights */); |
| 217 | } |
| 218 | |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 219 | private NotificationRecord getBuzzyBeepyNotification() { |
| 220 | return getNotificationRecord(mId, false /* insistent */, false /* once */, |
| 221 | true /* noisy */, true /* buzzy*/, false /* lights */); |
| 222 | } |
| 223 | |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 224 | private NotificationRecord getLightsNotification() { |
Julia Reynolds | 5436923 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 225 | return getNotificationRecord(mId, false /* insistent */, false /* once */, |
| 226 | false /* noisy */, false /* buzzy*/, true /* lights */); |
| 227 | } |
| 228 | |
| 229 | private NotificationRecord getLightsOnceNotification() { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 230 | return getNotificationRecord(mId, false /* insistent */, true /* once */, |
Julia Reynolds | 5436923 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 231 | false /* noisy */, false /* buzzy*/, true /* lights */); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 232 | } |
| 233 | |
Julia Reynolds | 9aee284 | 2019-01-03 14:35:38 -0500 | [diff] [blame] | 234 | private NotificationRecord getCallRecord(int id, boolean insistent) { |
| 235 | return getNotificationRecord(id, false, false /* once */, true /* noisy */, |
| 236 | false /* buzzy */, false /* lights */, false /* default vib */, |
| 237 | false /* default sound */, false /* default lights */, "", |
| 238 | Notification.GROUP_ALERT_ALL, false); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | private NotificationRecord getNotificationRecord(int id, boolean insistent, boolean once, |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 242 | boolean noisy, boolean buzzy, boolean lights) { |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 243 | return getNotificationRecord(id, insistent, once, noisy, buzzy, lights, true, true, true, |
Tony Mantler | ab55f0f | 2017-06-16 10:50:00 -0700 | [diff] [blame] | 244 | null, Notification.GROUP_ALERT_ALL, false); |
| 245 | } |
| 246 | |
| 247 | private NotificationRecord getLeanbackNotificationRecord(int id, boolean insistent, boolean once, |
| 248 | boolean noisy, boolean buzzy, boolean lights) { |
| 249 | return getNotificationRecord(id, insistent, once, noisy, buzzy, lights, true, true, true, |
| 250 | null, Notification.GROUP_ALERT_ALL, true); |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | private NotificationRecord getBeepyNotificationRecord(String groupKey, int groupAlertBehavior) { |
| 254 | return getNotificationRecord(mId, false, false, true, false, false, true, true, true, |
Tony Mantler | ab55f0f | 2017-06-16 10:50:00 -0700 | [diff] [blame] | 255 | groupKey, groupAlertBehavior, false); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 256 | } |
| 257 | |
Julia Reynolds | 5436923 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 258 | private NotificationRecord getLightsNotificationRecord(String groupKey, |
| 259 | int groupAlertBehavior) { |
| 260 | return getNotificationRecord(mId, false, false, false, false, true /*lights*/, true, true, |
| 261 | true, groupKey, groupAlertBehavior, false); |
| 262 | } |
| 263 | |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 264 | private NotificationRecord getNotificationRecord(int id, boolean insistent, boolean once, |
| 265 | boolean noisy, boolean buzzy, boolean lights, boolean defaultVibration, |
Tony Mantler | ab55f0f | 2017-06-16 10:50:00 -0700 | [diff] [blame] | 266 | boolean defaultSound, boolean defaultLights, String groupKey, int groupAlertBehavior, |
| 267 | boolean isLeanback) { |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 268 | NotificationChannel channel = |
Julia Reynolds | ca80d16 | 2017-04-23 12:39:32 -0400 | [diff] [blame] | 269 | new NotificationChannel("test", "test", IMPORTANCE_HIGH); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 270 | final Builder builder = new Builder(getContext()) |
| 271 | .setContentTitle("foo") |
| 272 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 273 | .setPriority(Notification.PRIORITY_HIGH) |
| 274 | .setOnlyAlertOnce(once); |
| 275 | |
| 276 | int defaults = 0; |
| 277 | if (noisy) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 278 | if (defaultSound) { |
| 279 | defaults |= Notification.DEFAULT_SOUND; |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 280 | channel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI, |
| 281 | Notification.AUDIO_ATTRIBUTES_DEFAULT); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 282 | } else { |
| 283 | builder.setSound(CUSTOM_SOUND); |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 284 | channel.setSound(CUSTOM_SOUND, CUSTOM_ATTRIBUTES); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 285 | } |
Julia Reynolds | 184b86d | 2017-04-12 13:27:58 -0400 | [diff] [blame] | 286 | } else { |
| 287 | channel.setSound(null, null); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 288 | } |
| 289 | if (buzzy) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 290 | if (defaultVibration) { |
| 291 | defaults |= Notification.DEFAULT_VIBRATE; |
| 292 | } else { |
| 293 | builder.setVibrate(CUSTOM_VIBRATION); |
Julia Reynolds | f57de46 | 2016-11-23 11:31:46 -0500 | [diff] [blame] | 294 | channel.setVibrationPattern(CUSTOM_VIBRATION); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 295 | } |
Julia Reynolds | f57de46 | 2016-11-23 11:31:46 -0500 | [diff] [blame] | 296 | channel.enableVibration(true); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 297 | } |
| 298 | if (lights) { |
| 299 | if (defaultLights) { |
| 300 | defaults |= Notification.DEFAULT_LIGHTS; |
| 301 | } else { |
| 302 | builder.setLights(CUSTOM_LIGHT_COLOR, CUSTOM_LIGHT_ON, CUSTOM_LIGHT_OFF); |
| 303 | } |
Julia Reynolds | 529e332 | 2017-02-06 08:33:01 -0500 | [diff] [blame] | 304 | channel.enableLights(true); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 305 | } |
| 306 | builder.setDefaults(defaults); |
| 307 | |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 308 | builder.setGroup(groupKey); |
| 309 | builder.setGroupAlertBehavior(groupAlertBehavior); |
| 310 | |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 311 | Notification n = builder.build(); |
| 312 | if (insistent) { |
| 313 | n.flags |= Notification.FLAG_INSISTENT; |
| 314 | } |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 315 | |
Tony Mantler | ab55f0f | 2017-06-16 10:50:00 -0700 | [diff] [blame] | 316 | Context context = spy(getContext()); |
| 317 | PackageManager packageManager = spy(context.getPackageManager()); |
| 318 | when(context.getPackageManager()).thenReturn(packageManager); |
| 319 | when(packageManager.hasSystemFeature(PackageManager.FEATURE_LEANBACK)) |
| 320 | .thenReturn(isLeanback); |
| 321 | |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 322 | StatusBarNotification sbn = new StatusBarNotification(mPkg, mPkg, id, mTag, mUid, |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 323 | mPid, n, mUser, null, System.currentTimeMillis()); |
Tony Mantler | ab55f0f | 2017-06-16 10:50:00 -0700 | [diff] [blame] | 324 | NotificationRecord r = new NotificationRecord(context, sbn, channel); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 325 | mService.addNotification(r); |
| 326 | return r; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | // |
| 330 | // Convenience functions for interacting with mocks |
| 331 | // |
| 332 | |
| 333 | private void verifyNeverBeep() throws RemoteException { |
| 334 | verify(mRingtonePlayer, never()).playAsync((Uri) anyObject(), (UserHandle) anyObject(), |
| 335 | anyBoolean(), (AudioAttributes) anyObject()); |
| 336 | } |
| 337 | |
| 338 | private void verifyBeep() throws RemoteException { |
| 339 | verify(mRingtonePlayer, times(1)).playAsync((Uri) anyObject(), (UserHandle) anyObject(), |
| 340 | eq(true), (AudioAttributes) anyObject()); |
| 341 | } |
| 342 | |
| 343 | private void verifyBeepLooped() throws RemoteException { |
| 344 | verify(mRingtonePlayer, times(1)).playAsync((Uri) anyObject(), (UserHandle) anyObject(), |
| 345 | eq(false), (AudioAttributes) anyObject()); |
| 346 | } |
| 347 | |
| 348 | private void verifyNeverStopAudio() throws RemoteException { |
| 349 | verify(mRingtonePlayer, never()).stopAsync(); |
| 350 | } |
| 351 | |
| 352 | private void verifyStopAudio() throws RemoteException { |
| 353 | verify(mRingtonePlayer, times(1)).stopAsync(); |
| 354 | } |
| 355 | |
| 356 | private void verifyNeverVibrate() { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 357 | verify(mVibrator, never()).vibrate(anyInt(), anyString(), (VibrationEffect) anyObject(), |
| 358 | (AudioAttributes) anyObject()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | private void verifyVibrate() { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 362 | verify(mVibrator, times(1)).vibrate(anyInt(), anyString(), argThat(mVibrateOnceMatcher), |
| 363 | (AudioAttributes) anyObject()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | private void verifyVibrateLooped() { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 367 | verify(mVibrator, times(1)).vibrate(anyInt(), anyString(), argThat(mVibrateLoopMatcher), |
| 368 | (AudioAttributes) anyObject()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 369 | } |
| 370 | |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 371 | private void verifyDelayedVibrateLooped() { |
| 372 | verify(mVibrator, timeout(MAX_VIBRATION_DELAY).times(1)).vibrate(anyInt(), anyString(), |
| 373 | argThat(mVibrateLoopMatcher), (AudioAttributes) anyObject()); |
| 374 | } |
| 375 | |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 376 | private void verifyStopVibrate() { |
| 377 | verify(mVibrator, times(1)).cancel(); |
| 378 | } |
| 379 | |
| 380 | private void verifyNeverStopVibrate() throws RemoteException { |
| 381 | verify(mVibrator, never()).cancel(); |
| 382 | } |
| 383 | |
Julia Reynolds | 5436923 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 384 | private void verifyNeverLights() { |
| 385 | verify(mLight, never()).setFlashing(anyInt(), anyInt(), anyInt(), anyInt()); |
| 386 | } |
| 387 | |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 388 | private void verifyLights() { |
Julia Reynolds | 033a412 | 2017-01-31 16:50:38 -0500 | [diff] [blame] | 389 | verify(mLight, times(1)).setFlashing(anyInt(), anyInt(), anyInt(), anyInt()); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | private void verifyCustomLights() { |
Julia Reynolds | 033a412 | 2017-01-31 16:50:38 -0500 | [diff] [blame] | 393 | verify(mLight, times(1)).setFlashing( |
| 394 | eq(CUSTOM_LIGHT_COLOR), anyInt(), eq(CUSTOM_LIGHT_ON), eq(CUSTOM_LIGHT_OFF)); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 395 | } |
| 396 | |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 397 | // |
| 398 | // Tests |
| 399 | // |
| 400 | |
| 401 | @Test |
| 402 | public void testLights() throws Exception { |
| 403 | NotificationRecord r = getLightsNotification(); |
| 404 | r.setImportance(NotificationManager.IMPORTANCE_DEFAULT, "for testing"); |
| 405 | |
| 406 | mService.buzzBeepBlinkLocked(r); |
| 407 | |
| 408 | verifyLights(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 409 | assertTrue(r.isInterruptive()); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 410 | } |
| 411 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 412 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 413 | public void testBeep() throws Exception { |
| 414 | NotificationRecord r = getBeepyNotification(); |
| 415 | |
| 416 | mService.buzzBeepBlinkLocked(r); |
| 417 | |
| 418 | verifyBeepLooped(); |
| 419 | verifyNeverVibrate(); |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 420 | verify(mAccessibilityService, times(1)).sendAccessibilityEvent(any(), anyInt()); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 421 | assertTrue(r.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 422 | } |
| 423 | |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 424 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 425 | public void testBeepInsistently() throws Exception { |
| 426 | NotificationRecord r = getInsistentBeepyNotification(); |
| 427 | |
| 428 | mService.buzzBeepBlinkLocked(r); |
| 429 | |
| 430 | verifyBeep(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 431 | assertTrue(r.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 432 | } |
| 433 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 434 | @Test |
Tony Mantler | ab55f0f | 2017-06-16 10:50:00 -0700 | [diff] [blame] | 435 | public void testNoLeanbackBeep() throws Exception { |
| 436 | NotificationRecord r = getInsistentBeepyLeanbackNotification(); |
| 437 | |
| 438 | mService.buzzBeepBlinkLocked(r); |
| 439 | |
| 440 | verifyNeverBeep(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 441 | assertFalse(r.isInterruptive()); |
Tony Mantler | ab55f0f | 2017-06-16 10:50:00 -0700 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 445 | public void testNoInterruptionForMin() throws Exception { |
| 446 | NotificationRecord r = getBeepyNotification(); |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 447 | r.setImportance(NotificationManager.IMPORTANCE_MIN, "foo"); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 448 | |
| 449 | mService.buzzBeepBlinkLocked(r); |
| 450 | |
| 451 | verifyNeverBeep(); |
| 452 | verifyNeverVibrate(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 453 | assertFalse(r.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 454 | } |
| 455 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 456 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 457 | public void testNoInterruptionForIntercepted() throws Exception { |
| 458 | NotificationRecord r = getBeepyNotification(); |
| 459 | r.setIntercepted(true); |
| 460 | |
| 461 | mService.buzzBeepBlinkLocked(r); |
| 462 | |
| 463 | verifyNeverBeep(); |
| 464 | verifyNeverVibrate(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 465 | assertFalse(r.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 466 | } |
| 467 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 468 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 469 | public void testBeepTwice() throws Exception { |
| 470 | NotificationRecord r = getBeepyNotification(); |
| 471 | |
| 472 | // set up internal state |
| 473 | mService.buzzBeepBlinkLocked(r); |
| 474 | Mockito.reset(mRingtonePlayer); |
| 475 | |
| 476 | // update should beep |
| 477 | r.isUpdate = true; |
| 478 | mService.buzzBeepBlinkLocked(r); |
| 479 | verifyBeepLooped(); |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 480 | verify(mAccessibilityService, times(2)).sendAccessibilityEvent(any(), anyInt()); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 481 | assertTrue(r.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 482 | } |
| 483 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 484 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 485 | public void testHonorAlertOnlyOnceForBeep() throws Exception { |
| 486 | NotificationRecord r = getBeepyNotification(); |
| 487 | NotificationRecord s = getBeepyOnceNotification(); |
| 488 | s.isUpdate = true; |
| 489 | |
| 490 | // set up internal state |
| 491 | mService.buzzBeepBlinkLocked(r); |
| 492 | Mockito.reset(mRingtonePlayer); |
| 493 | |
| 494 | // update should not beep |
| 495 | mService.buzzBeepBlinkLocked(s); |
| 496 | verifyNeverBeep(); |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 497 | verify(mAccessibilityService, times(1)).sendAccessibilityEvent(any(), anyInt()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 498 | } |
| 499 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 500 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 501 | public void testNoisyUpdateDoesNotCancelAudio() throws Exception { |
| 502 | NotificationRecord r = getBeepyNotification(); |
| 503 | |
| 504 | mService.buzzBeepBlinkLocked(r); |
| 505 | r.isUpdate = true; |
| 506 | mService.buzzBeepBlinkLocked(r); |
| 507 | |
| 508 | verifyNeverStopAudio(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 509 | assertTrue(r.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 510 | } |
| 511 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 512 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 513 | public void testNoisyOnceUpdateDoesNotCancelAudio() throws Exception { |
| 514 | NotificationRecord r = getBeepyNotification(); |
| 515 | NotificationRecord s = getBeepyOnceNotification(); |
| 516 | s.isUpdate = true; |
| 517 | |
| 518 | mService.buzzBeepBlinkLocked(r); |
| 519 | mService.buzzBeepBlinkLocked(s); |
| 520 | |
| 521 | verifyNeverStopAudio(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 522 | assertTrue(r.isInterruptive()); |
| 523 | assertFalse(s.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 524 | } |
| 525 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 526 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 527 | public void testQuietUpdateDoesNotCancelAudioFromOther() throws Exception { |
| 528 | NotificationRecord r = getBeepyNotification(); |
| 529 | NotificationRecord s = getQuietNotification(); |
| 530 | s.isUpdate = true; |
| 531 | NotificationRecord other = getNoisyOtherNotification(); |
| 532 | |
| 533 | // set up internal state |
| 534 | mService.buzzBeepBlinkLocked(r); |
| 535 | mService.buzzBeepBlinkLocked(other); // this takes the audio stream |
| 536 | Mockito.reset(mRingtonePlayer); |
| 537 | |
| 538 | // should not stop noise, since we no longer own it |
| 539 | mService.buzzBeepBlinkLocked(s); // this no longer owns the stream |
| 540 | verifyNeverStopAudio(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 541 | assertTrue(other.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 542 | } |
| 543 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 544 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 545 | public void testQuietInterloperDoesNotCancelAudio() throws Exception { |
| 546 | NotificationRecord r = getBeepyNotification(); |
| 547 | NotificationRecord other = getQuietOtherNotification(); |
| 548 | |
| 549 | // set up internal state |
| 550 | mService.buzzBeepBlinkLocked(r); |
| 551 | Mockito.reset(mRingtonePlayer); |
| 552 | |
| 553 | // should not stop noise, since it does not own it |
| 554 | mService.buzzBeepBlinkLocked(other); |
| 555 | verifyNeverStopAudio(); |
| 556 | } |
| 557 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 558 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 559 | public void testQuietUpdateCancelsAudio() throws Exception { |
| 560 | NotificationRecord r = getBeepyNotification(); |
| 561 | NotificationRecord s = getQuietNotification(); |
| 562 | s.isUpdate = true; |
| 563 | |
| 564 | // set up internal state |
| 565 | mService.buzzBeepBlinkLocked(r); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 566 | assertTrue(r.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 567 | Mockito.reset(mRingtonePlayer); |
| 568 | |
| 569 | // quiet update should stop making noise |
| 570 | mService.buzzBeepBlinkLocked(s); |
| 571 | verifyStopAudio(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 572 | assertFalse(s.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 573 | } |
| 574 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 575 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 576 | public void testQuietOnceUpdateCancelsAudio() throws Exception { |
| 577 | NotificationRecord r = getBeepyNotification(); |
| 578 | NotificationRecord s = getQuietOnceNotification(); |
| 579 | s.isUpdate = true; |
| 580 | |
| 581 | // set up internal state |
| 582 | mService.buzzBeepBlinkLocked(r); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 583 | assertTrue(r.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 584 | Mockito.reset(mRingtonePlayer); |
| 585 | |
| 586 | // stop making noise - this is a weird corner case, but quiet should override once |
| 587 | mService.buzzBeepBlinkLocked(s); |
| 588 | verifyStopAudio(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 589 | assertFalse(s.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 590 | } |
| 591 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 592 | @Test |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 593 | public void testInCallNotification() throws Exception { |
| 594 | NotificationRecord r = getBeepyNotification(); |
| 595 | |
| 596 | // set up internal state |
| 597 | mService.buzzBeepBlinkLocked(r); |
| 598 | Mockito.reset(mRingtonePlayer); |
| 599 | |
| 600 | mService.mInCall = true; |
| 601 | mService.buzzBeepBlinkLocked(r); |
| 602 | |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 603 | verify(mService, times(1)).playInCallNotification(); |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 604 | verifyNeverBeep(); // doesn't play normal beep |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 605 | assertTrue(r.isInterruptive()); |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | @Test |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 609 | public void testNoDemoteSoundToVibrateIfVibrateGiven() throws Exception { |
| 610 | NotificationRecord r = getBuzzyBeepyNotification(); |
| 611 | assertTrue(r.getSound() != null); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 612 | |
| 613 | // the phone is quiet |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 614 | when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_VIBRATE); |
Julia Reynolds | 8589657 | 2017-09-20 12:54:52 -0400 | [diff] [blame] | 615 | when(mAudioManager.getStreamVolume(anyInt())).thenReturn(0); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 616 | |
| 617 | mService.buzzBeepBlinkLocked(r); |
| 618 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 619 | VibrationEffect effect = VibrationEffect.createWaveform(r.getVibration(), -1); |
| 620 | |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 621 | verify(mVibrator, timeout(MAX_VIBRATION_DELAY).times(1)).vibrate(anyInt(), anyString(), |
| 622 | eq(effect), (AudioAttributes) anyObject()); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 623 | assertTrue(r.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 624 | } |
| 625 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 626 | @Test |
Julia Reynolds | 8589657 | 2017-09-20 12:54:52 -0400 | [diff] [blame] | 627 | public void testNoDemoteSoundToVibrateIfNonNotificationStream() throws Exception { |
| 628 | NotificationRecord r = getBeepyNotification(); |
| 629 | assertTrue(r.getSound() != null); |
| 630 | assertNull(r.getVibration()); |
| 631 | |
| 632 | // the phone is quiet |
| 633 | when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_VIBRATE); |
| 634 | when(mAudioManager.getStreamVolume(anyInt())).thenReturn(1); |
| 635 | |
| 636 | mService.buzzBeepBlinkLocked(r); |
| 637 | |
| 638 | verifyNeverVibrate(); |
| 639 | verifyBeepLooped(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 640 | assertTrue(r.isInterruptive()); |
Julia Reynolds | 8589657 | 2017-09-20 12:54:52 -0400 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | @Test |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 644 | public void testDemoteSoundToVibrate() throws Exception { |
| 645 | NotificationRecord r = getBeepyNotification(); |
| 646 | assertTrue(r.getSound() != null); |
| 647 | assertNull(r.getVibration()); |
| 648 | |
| 649 | // the phone is quiet |
| 650 | when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_VIBRATE); |
Julia Reynolds | 8589657 | 2017-09-20 12:54:52 -0400 | [diff] [blame] | 651 | when(mAudioManager.getStreamVolume(anyInt())).thenReturn(0); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 652 | |
| 653 | mService.buzzBeepBlinkLocked(r); |
| 654 | |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 655 | verify(mVibrator, timeout(MAX_VIBRATION_DELAY).times(1)).vibrate(anyInt(), anyString(), |
| 656 | eq(FALLBACK_VIBRATION), (AudioAttributes) anyObject()); |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 657 | verify(mRingtonePlayer, never()).playAsync |
| 658 | (anyObject(), anyObject(), anyBoolean(), anyObject()); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 659 | assertTrue(r.isInterruptive()); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | @Test |
| 663 | public void testDemoteInsistentSoundToVibrate() throws Exception { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 664 | NotificationRecord r = getInsistentBeepyNotification(); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 665 | assertTrue(r.getSound() != null); |
| 666 | assertNull(r.getVibration()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 667 | |
| 668 | // the phone is quiet |
| 669 | when(mAudioManager.getStreamVolume(anyInt())).thenReturn(0); |
| 670 | when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_VIBRATE); |
| 671 | |
| 672 | mService.buzzBeepBlinkLocked(r); |
| 673 | |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 674 | verifyDelayedVibrateLooped(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 675 | assertTrue(r.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 676 | } |
| 677 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 678 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 679 | public void testVibrate() throws Exception { |
| 680 | NotificationRecord r = getBuzzyNotification(); |
| 681 | |
| 682 | mService.buzzBeepBlinkLocked(r); |
| 683 | |
| 684 | verifyNeverBeep(); |
| 685 | verifyVibrate(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 686 | assertTrue(r.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 687 | } |
| 688 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 689 | @Test |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 690 | public void testInsistentVibrate() { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 691 | NotificationRecord r = getInsistentBuzzyNotification(); |
| 692 | |
| 693 | mService.buzzBeepBlinkLocked(r); |
| 694 | verifyVibrateLooped(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 695 | assertTrue(r.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 696 | } |
| 697 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 698 | @Test |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 699 | public void testVibrateTwice() { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 700 | NotificationRecord r = getBuzzyNotification(); |
| 701 | |
| 702 | // set up internal state |
| 703 | mService.buzzBeepBlinkLocked(r); |
| 704 | Mockito.reset(mVibrator); |
| 705 | |
| 706 | // update should vibrate |
| 707 | r.isUpdate = true; |
| 708 | mService.buzzBeepBlinkLocked(r); |
| 709 | verifyVibrate(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 710 | assertTrue(r.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 711 | } |
| 712 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 713 | @Test |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 714 | public void testGroupAlertSummarySilenceChild() throws Exception { |
| 715 | NotificationRecord child = getBeepyNotificationRecord("a", GROUP_ALERT_SUMMARY); |
| 716 | |
| 717 | mService.buzzBeepBlinkLocked(child); |
| 718 | |
| 719 | verifyNeverBeep(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 720 | assertFalse(child.isInterruptive()); |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | @Test |
| 724 | public void testGroupAlertSummaryNoSilenceSummary() throws Exception { |
| 725 | NotificationRecord summary = getBeepyNotificationRecord("a", GROUP_ALERT_SUMMARY); |
| 726 | summary.getNotification().flags |= Notification.FLAG_GROUP_SUMMARY; |
| 727 | |
| 728 | mService.buzzBeepBlinkLocked(summary); |
| 729 | |
| 730 | verifyBeepLooped(); |
Julia Reynolds | 5436923 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 731 | // summaries are never interruptive for notification counts |
| 732 | assertFalse(summary.isInterruptive()); |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | @Test |
| 736 | public void testGroupAlertSummaryNoSilenceNonGroupChild() throws Exception { |
| 737 | NotificationRecord nonGroup = getBeepyNotificationRecord(null, GROUP_ALERT_SUMMARY); |
| 738 | |
| 739 | mService.buzzBeepBlinkLocked(nonGroup); |
| 740 | |
| 741 | verifyBeepLooped(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 742 | assertTrue(nonGroup.isInterruptive()); |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | @Test |
| 746 | public void testGroupAlertChildSilenceSummary() throws Exception { |
| 747 | NotificationRecord summary = getBeepyNotificationRecord("a", GROUP_ALERT_CHILDREN); |
| 748 | summary.getNotification().flags |= Notification.FLAG_GROUP_SUMMARY; |
| 749 | |
| 750 | mService.buzzBeepBlinkLocked(summary); |
| 751 | |
| 752 | verifyNeverBeep(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 753 | assertFalse(summary.isInterruptive()); |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | @Test |
| 757 | public void testGroupAlertChildNoSilenceChild() throws Exception { |
| 758 | NotificationRecord child = getBeepyNotificationRecord("a", GROUP_ALERT_CHILDREN); |
| 759 | |
| 760 | mService.buzzBeepBlinkLocked(child); |
| 761 | |
| 762 | verifyBeepLooped(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 763 | assertTrue(child.isInterruptive()); |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | @Test |
| 767 | public void testGroupAlertChildNoSilenceNonGroupSummary() throws Exception { |
| 768 | NotificationRecord nonGroup = getBeepyNotificationRecord(null, GROUP_ALERT_CHILDREN); |
| 769 | |
| 770 | mService.buzzBeepBlinkLocked(nonGroup); |
| 771 | |
| 772 | verifyBeepLooped(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 773 | assertTrue(nonGroup.isInterruptive()); |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | @Test |
| 777 | public void testGroupAlertAllNoSilenceGroup() throws Exception { |
| 778 | NotificationRecord group = getBeepyNotificationRecord("a", GROUP_ALERT_ALL); |
| 779 | |
| 780 | mService.buzzBeepBlinkLocked(group); |
| 781 | |
| 782 | verifyBeepLooped(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 783 | assertTrue(group.isInterruptive()); |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 787 | public void testHonorAlertOnlyOnceForBuzz() throws Exception { |
| 788 | NotificationRecord r = getBuzzyNotification(); |
| 789 | NotificationRecord s = getBuzzyOnceNotification(); |
| 790 | s.isUpdate = true; |
| 791 | |
| 792 | // set up internal state |
| 793 | mService.buzzBeepBlinkLocked(r); |
| 794 | Mockito.reset(mVibrator); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 795 | assertTrue(r.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 796 | |
| 797 | // update should not beep |
| 798 | mService.buzzBeepBlinkLocked(s); |
| 799 | verifyNeverVibrate(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 800 | assertFalse(s.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 801 | } |
| 802 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 803 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 804 | public void testNoisyUpdateDoesNotCancelVibrate() throws Exception { |
| 805 | NotificationRecord r = getBuzzyNotification(); |
| 806 | |
| 807 | mService.buzzBeepBlinkLocked(r); |
| 808 | r.isUpdate = true; |
| 809 | mService.buzzBeepBlinkLocked(r); |
| 810 | |
| 811 | verifyNeverStopVibrate(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 812 | assertTrue(r.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 813 | } |
| 814 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 815 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 816 | public void testNoisyOnceUpdateDoesNotCancelVibrate() throws Exception { |
| 817 | NotificationRecord r = getBuzzyNotification(); |
| 818 | NotificationRecord s = getBuzzyOnceNotification(); |
| 819 | s.isUpdate = true; |
| 820 | |
| 821 | mService.buzzBeepBlinkLocked(r); |
| 822 | mService.buzzBeepBlinkLocked(s); |
| 823 | |
| 824 | verifyNeverStopVibrate(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 825 | assertTrue(r.isInterruptive()); |
| 826 | assertFalse(s.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 827 | } |
| 828 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 829 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 830 | public void testQuietUpdateDoesNotCancelVibrateFromOther() throws Exception { |
| 831 | NotificationRecord r = getBuzzyNotification(); |
| 832 | NotificationRecord s = getQuietNotification(); |
| 833 | s.isUpdate = true; |
| 834 | NotificationRecord other = getNoisyOtherNotification(); |
| 835 | |
| 836 | // set up internal state |
| 837 | mService.buzzBeepBlinkLocked(r); |
| 838 | mService.buzzBeepBlinkLocked(other); // this takes the vibrate stream |
| 839 | Mockito.reset(mVibrator); |
| 840 | |
| 841 | // should not stop vibrate, since we no longer own it |
| 842 | mService.buzzBeepBlinkLocked(s); // this no longer owns the stream |
| 843 | verifyNeverStopVibrate(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 844 | assertTrue(r.isInterruptive()); |
| 845 | assertTrue(other.isInterruptive()); |
| 846 | assertFalse(s.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 847 | } |
| 848 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 849 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 850 | public void testQuietInterloperDoesNotCancelVibrate() throws Exception { |
| 851 | NotificationRecord r = getBuzzyNotification(); |
| 852 | NotificationRecord other = getQuietOtherNotification(); |
| 853 | |
| 854 | // set up internal state |
| 855 | mService.buzzBeepBlinkLocked(r); |
| 856 | Mockito.reset(mVibrator); |
| 857 | |
| 858 | // should not stop noise, since it does not own it |
| 859 | mService.buzzBeepBlinkLocked(other); |
| 860 | verifyNeverStopVibrate(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 861 | assertFalse(other.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 862 | } |
| 863 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 864 | @Test |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 865 | public void testQuietUpdateCancelsVibrate() { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 866 | NotificationRecord r = getBuzzyNotification(); |
| 867 | NotificationRecord s = getQuietNotification(); |
| 868 | s.isUpdate = true; |
| 869 | |
| 870 | // set up internal state |
| 871 | mService.buzzBeepBlinkLocked(r); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 872 | verifyVibrate(); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 873 | |
| 874 | // quiet update should stop making noise |
| 875 | mService.buzzBeepBlinkLocked(s); |
| 876 | verifyStopVibrate(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 877 | assertTrue(r.isInterruptive()); |
| 878 | assertFalse(s.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 879 | } |
| 880 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 881 | @Test |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 882 | public void testQuietOnceUpdateCancelVibrate() throws Exception { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 883 | NotificationRecord r = getBuzzyNotification(); |
| 884 | NotificationRecord s = getQuietOnceNotification(); |
| 885 | s.isUpdate = true; |
| 886 | |
| 887 | // set up internal state |
| 888 | mService.buzzBeepBlinkLocked(r); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 889 | verifyVibrate(); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 890 | |
| 891 | // stop making noise - this is a weird corner case, but quiet should override once |
| 892 | mService.buzzBeepBlinkLocked(s); |
| 893 | verifyStopVibrate(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 894 | assertTrue(r.isInterruptive()); |
| 895 | assertFalse(s.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 896 | } |
| 897 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 898 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 899 | public void testQuietUpdateCancelsDemotedVibrate() throws Exception { |
| 900 | NotificationRecord r = getBeepyNotification(); |
| 901 | NotificationRecord s = getQuietNotification(); |
| 902 | |
| 903 | // the phone is quiet |
| 904 | when(mAudioManager.getStreamVolume(anyInt())).thenReturn(0); |
| 905 | when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_VIBRATE); |
| 906 | |
| 907 | mService.buzzBeepBlinkLocked(r); |
| 908 | |
| 909 | // quiet update should stop making noise |
| 910 | mService.buzzBeepBlinkLocked(s); |
| 911 | verifyStopVibrate(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 912 | assertTrue(r.isInterruptive()); |
| 913 | assertFalse(s.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 914 | } |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 915 | |
Julia Reynolds | ca80d16 | 2017-04-23 12:39:32 -0400 | [diff] [blame] | 916 | @Test |
Seungho Lee | 0e97ae6 | 2018-10-31 21:49:09 +0900 | [diff] [blame] | 917 | public void testCanceledNoisyNeverVibrate() throws Exception { |
| 918 | NotificationRecord r = getBuzzyBeepyNotification(); |
| 919 | |
| 920 | final int waitMs = mAudioManager.getFocusRampTimeMs( |
| 921 | AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK, |
| 922 | r.getAudioAttributes()); |
| 923 | |
| 924 | mService.buzzBeepBlinkLocked(r); |
| 925 | mService.clearNotifications(); |
| 926 | |
| 927 | verifyNeverVibrate(); |
| 928 | Thread.sleep(waitMs); |
| 929 | verifyNeverVibrate(); |
| 930 | } |
| 931 | |
| 932 | @Test |
Julia Reynolds | ca80d16 | 2017-04-23 12:39:32 -0400 | [diff] [blame] | 933 | public void testEmptyUriSoundTreatedAsNoSound() throws Exception { |
| 934 | NotificationChannel channel = new NotificationChannel("test", "test", IMPORTANCE_HIGH); |
| 935 | channel.setSound(Uri.EMPTY, null); |
| 936 | final Notification n = new Builder(getContext(), "test") |
| 937 | .setSmallIcon(android.R.drawable.sym_def_app_icon).build(); |
| 938 | |
| 939 | StatusBarNotification sbn = new StatusBarNotification(mPkg, mPkg, 0, mTag, mUid, |
| 940 | mPid, n, mUser, null, System.currentTimeMillis()); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 941 | NotificationRecord r = new NotificationRecord(getContext(), sbn, channel); |
Julia Reynolds | ca80d16 | 2017-04-23 12:39:32 -0400 | [diff] [blame] | 942 | mService.addNotification(r); |
| 943 | |
| 944 | mService.buzzBeepBlinkLocked(r); |
Julia Reynolds | ca80d16 | 2017-04-23 12:39:32 -0400 | [diff] [blame] | 945 | verifyNeverBeep(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 946 | assertFalse(r.isInterruptive()); |
Julia Reynolds | ca80d16 | 2017-04-23 12:39:32 -0400 | [diff] [blame] | 947 | } |
| 948 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 949 | @Test |
| 950 | public void testRepeatedSoundOverLimitMuted() throws Exception { |
| 951 | when(mUsageStats.isAlertRateLimited(any())).thenReturn(true); |
| 952 | |
| 953 | NotificationRecord r = getBeepyNotification(); |
| 954 | |
| 955 | mService.buzzBeepBlinkLocked(r); |
| 956 | verifyNeverBeep(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 957 | assertFalse(r.isInterruptive()); |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | @Test |
| 961 | public void testPostingSilentNotificationDoesNotAffectRateLimiting() throws Exception { |
| 962 | NotificationRecord r = getQuietNotification(); |
| 963 | mService.buzzBeepBlinkLocked(r); |
| 964 | |
| 965 | verify(mUsageStats, never()).isAlertRateLimited(any()); |
| 966 | } |
| 967 | |
| 968 | @Test |
Julia Reynolds | 65b85cf | 2017-07-20 09:19:20 -0400 | [diff] [blame] | 969 | public void testPostingGroupSuppressedDoesNotAffectRateLimiting() throws Exception { |
| 970 | NotificationRecord summary = getBeepyNotificationRecord("a", GROUP_ALERT_CHILDREN); |
| 971 | summary.getNotification().flags |= Notification.FLAG_GROUP_SUMMARY; |
| 972 | |
| 973 | mService.buzzBeepBlinkLocked(summary); |
Julia Reynolds | 65b85cf | 2017-07-20 09:19:20 -0400 | [diff] [blame] | 974 | verify(mUsageStats, never()).isAlertRateLimited(any()); |
| 975 | } |
| 976 | |
| 977 | @Test |
Julia Reynolds | 79dfdd6 | 2018-04-17 15:36:33 -0400 | [diff] [blame] | 978 | public void testGroupSuppressionFailureDoesNotAffectRateLimiting() { |
| 979 | NotificationRecord summary = getBeepyNotificationRecord("a", GROUP_ALERT_SUMMARY); |
| 980 | summary.getNotification().flags |= Notification.FLAG_GROUP_SUMMARY; |
| 981 | |
| 982 | mService.buzzBeepBlinkLocked(summary); |
| 983 | verify(mUsageStats, times(1)).isAlertRateLimited(any()); |
| 984 | } |
| 985 | |
| 986 | @Test |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 987 | public void testCrossUserSoundMuted() throws Exception { |
| 988 | final Notification n = new Builder(getContext(), "test") |
| 989 | .setSmallIcon(android.R.drawable.sym_def_app_icon).build(); |
| 990 | |
| 991 | int userId = mUser.getIdentifier() + 1; |
| 992 | StatusBarNotification sbn = new StatusBarNotification(mPkg, mPkg, 0, mTag, mUid, |
| 993 | mPid, n, UserHandle.of(userId), null, System.currentTimeMillis()); |
| 994 | NotificationRecord r = new NotificationRecord(getContext(), sbn, |
| 995 | new NotificationChannel("test", "test", IMPORTANCE_HIGH)); |
| 996 | |
| 997 | mService.buzzBeepBlinkLocked(r); |
| 998 | verifyNeverBeep(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 999 | assertFalse(r.isInterruptive()); |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 1000 | } |
| 1001 | |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 1002 | @Test |
| 1003 | public void testA11yMinInitialPost() throws Exception { |
| 1004 | NotificationRecord r = getQuietNotification(); |
| 1005 | r.setImportance(IMPORTANCE_MIN, ""); |
| 1006 | mService.buzzBeepBlinkLocked(r); |
| 1007 | verify(mAccessibilityService, never()).sendAccessibilityEvent(any(), anyInt()); |
| 1008 | } |
| 1009 | |
| 1010 | @Test |
| 1011 | public void testA11yQuietInitialPost() throws Exception { |
| 1012 | NotificationRecord r = getQuietNotification(); |
| 1013 | mService.buzzBeepBlinkLocked(r); |
| 1014 | verify(mAccessibilityService, times(1)).sendAccessibilityEvent(any(), anyInt()); |
| 1015 | } |
| 1016 | |
| 1017 | @Test |
| 1018 | public void testA11yQuietUpdate() throws Exception { |
| 1019 | NotificationRecord r = getQuietNotification(); |
| 1020 | mService.buzzBeepBlinkLocked(r); |
| 1021 | r.isUpdate = true; |
| 1022 | mService.buzzBeepBlinkLocked(r); |
| 1023 | verify(mAccessibilityService, times(1)).sendAccessibilityEvent(any(), anyInt()); |
| 1024 | } |
| 1025 | |
Julia Reynolds | 5436923 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 1026 | @Test |
| 1027 | public void testLightsScreenOn() { |
| 1028 | mService.mScreenOn = true; |
| 1029 | NotificationRecord r = getLightsNotification(); |
| 1030 | mService.buzzBeepBlinkLocked(r); |
| 1031 | verifyNeverLights(); |
| 1032 | assertFalse(r.isInterruptive()); |
| 1033 | } |
| 1034 | |
| 1035 | @Test |
| 1036 | public void testLightsInCall() { |
| 1037 | mService.mInCall = true; |
| 1038 | NotificationRecord r = getLightsNotification(); |
| 1039 | mService.buzzBeepBlinkLocked(r); |
| 1040 | verifyNeverLights(); |
| 1041 | assertFalse(r.isInterruptive()); |
| 1042 | } |
| 1043 | |
| 1044 | @Test |
| 1045 | public void testLightsSilentUpdate() { |
| 1046 | NotificationRecord r = getLightsOnceNotification(); |
| 1047 | mService.buzzBeepBlinkLocked(r); |
| 1048 | verifyLights(); |
| 1049 | assertTrue(r.isInterruptive()); |
| 1050 | |
| 1051 | r = getLightsOnceNotification(); |
| 1052 | r.isUpdate = true; |
| 1053 | mService.buzzBeepBlinkLocked(r); |
| 1054 | // checks that lights happened once, i.e. this new call didn't trigger them again |
| 1055 | verifyLights(); |
| 1056 | assertFalse(r.isInterruptive()); |
| 1057 | } |
| 1058 | |
| 1059 | @Test |
| 1060 | public void testLightsUnimportant() { |
| 1061 | NotificationRecord r = getLightsNotification(); |
| 1062 | r.setImportance(IMPORTANCE_LOW, "testing"); |
| 1063 | mService.buzzBeepBlinkLocked(r); |
| 1064 | verifyNeverLights(); |
| 1065 | assertFalse(r.isInterruptive()); |
| 1066 | } |
| 1067 | |
| 1068 | @Test |
| 1069 | public void testLightsNoLights() { |
| 1070 | NotificationRecord r = getQuietNotification(); |
| 1071 | mService.buzzBeepBlinkLocked(r); |
| 1072 | verifyNeverLights(); |
| 1073 | assertFalse(r.isInterruptive()); |
| 1074 | } |
| 1075 | |
| 1076 | @Test |
| 1077 | public void testLightsNoLightOnDevice() { |
| 1078 | mService.mHasLight = false; |
| 1079 | NotificationRecord r = getLightsNotification(); |
| 1080 | mService.buzzBeepBlinkLocked(r); |
| 1081 | verifyNeverLights(); |
| 1082 | assertFalse(r.isInterruptive()); |
| 1083 | } |
| 1084 | |
| 1085 | @Test |
| 1086 | public void testLightsLightsOffGlobally() { |
| 1087 | mService.mNotificationPulseEnabled = false; |
| 1088 | NotificationRecord r = getLightsNotification(); |
| 1089 | mService.buzzBeepBlinkLocked(r); |
| 1090 | verifyNeverLights(); |
| 1091 | assertFalse(r.isInterruptive()); |
| 1092 | } |
| 1093 | |
| 1094 | @Test |
| 1095 | public void testLightsDndIntercepted() { |
| 1096 | NotificationRecord r = getLightsNotification(); |
| 1097 | r.setSuppressedVisualEffects(SUPPRESSED_EFFECT_LIGHTS); |
| 1098 | mService.buzzBeepBlinkLocked(r); |
| 1099 | verifyNeverLights(); |
| 1100 | assertFalse(r.isInterruptive()); |
| 1101 | } |
| 1102 | |
| 1103 | @Test |
| 1104 | public void testGroupAlertSummaryNoLightsChild() { |
| 1105 | NotificationRecord child = getLightsNotificationRecord("a", GROUP_ALERT_SUMMARY); |
| 1106 | |
| 1107 | mService.buzzBeepBlinkLocked(child); |
| 1108 | |
| 1109 | verifyNeverLights(); |
| 1110 | assertFalse(child.isInterruptive()); |
| 1111 | } |
| 1112 | |
| 1113 | @Test |
| 1114 | public void testGroupAlertSummaryLightsSummary() { |
| 1115 | NotificationRecord summary = getLightsNotificationRecord("a", GROUP_ALERT_SUMMARY); |
| 1116 | summary.getNotification().flags |= Notification.FLAG_GROUP_SUMMARY; |
| 1117 | |
| 1118 | mService.buzzBeepBlinkLocked(summary); |
| 1119 | |
| 1120 | verifyLights(); |
| 1121 | // summaries should never count for interruptiveness counts |
| 1122 | assertFalse(summary.isInterruptive()); |
| 1123 | } |
| 1124 | |
| 1125 | @Test |
| 1126 | public void testGroupAlertSummaryLightsNonGroupChild() { |
| 1127 | NotificationRecord nonGroup = getLightsNotificationRecord(null, GROUP_ALERT_SUMMARY); |
| 1128 | |
| 1129 | mService.buzzBeepBlinkLocked(nonGroup); |
| 1130 | |
| 1131 | verifyLights(); |
| 1132 | assertTrue(nonGroup.isInterruptive()); |
| 1133 | } |
| 1134 | |
| 1135 | @Test |
| 1136 | public void testGroupAlertChildNoLightsSummary() { |
| 1137 | NotificationRecord summary = getLightsNotificationRecord("a", GROUP_ALERT_CHILDREN); |
| 1138 | summary.getNotification().flags |= Notification.FLAG_GROUP_SUMMARY; |
| 1139 | |
| 1140 | mService.buzzBeepBlinkLocked(summary); |
| 1141 | |
| 1142 | verifyNeverLights(); |
| 1143 | assertFalse(summary.isInterruptive()); |
| 1144 | } |
| 1145 | |
| 1146 | @Test |
| 1147 | public void testGroupAlertChildLightsChild() { |
| 1148 | NotificationRecord child = getLightsNotificationRecord("a", GROUP_ALERT_CHILDREN); |
| 1149 | |
| 1150 | mService.buzzBeepBlinkLocked(child); |
| 1151 | |
| 1152 | verifyLights(); |
| 1153 | assertTrue(child.isInterruptive()); |
| 1154 | } |
| 1155 | |
| 1156 | @Test |
| 1157 | public void testGroupAlertChildLightsNonGroupSummary() { |
| 1158 | NotificationRecord nonGroup = getLightsNotificationRecord(null, GROUP_ALERT_CHILDREN); |
| 1159 | |
| 1160 | mService.buzzBeepBlinkLocked(nonGroup); |
| 1161 | |
| 1162 | verifyLights(); |
| 1163 | assertTrue(nonGroup.isInterruptive()); |
| 1164 | } |
| 1165 | |
| 1166 | @Test |
| 1167 | public void testGroupAlertAllLightsGroup() { |
| 1168 | NotificationRecord group = getLightsNotificationRecord("a", GROUP_ALERT_ALL); |
| 1169 | |
| 1170 | mService.buzzBeepBlinkLocked(group); |
| 1171 | |
| 1172 | verifyLights(); |
| 1173 | assertTrue(group.isInterruptive()); |
| 1174 | } |
| 1175 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 1176 | static class VibrateRepeatMatcher implements ArgumentMatcher<VibrationEffect> { |
| 1177 | private final int mRepeatIndex; |
| 1178 | |
| 1179 | VibrateRepeatMatcher(int repeatIndex) { |
| 1180 | mRepeatIndex = repeatIndex; |
| 1181 | } |
| 1182 | |
| 1183 | @Override |
| 1184 | public boolean matches(VibrationEffect actual) { |
| 1185 | if (actual instanceof VibrationEffect.Waveform && |
| 1186 | ((VibrationEffect.Waveform) actual).getRepeatIndex() == mRepeatIndex) { |
| 1187 | return true; |
| 1188 | } |
| 1189 | // All non-waveform effects are essentially one shots. |
| 1190 | return mRepeatIndex == -1; |
| 1191 | } |
| 1192 | |
| 1193 | @Override |
| 1194 | public String toString() { |
| 1195 | return "repeatIndex=" + mRepeatIndex; |
| 1196 | } |
| 1197 | } |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 1198 | } |