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; |
| 61 | import android.service.notification.StatusBarNotification; |
Tony Mantler | ab55f0f | 2017-06-16 10:50:00 -0700 | [diff] [blame] | 62 | import android.test.suitebuilder.annotation.SmallTest; |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 63 | import android.view.accessibility.AccessibilityEvent; |
| 64 | import android.view.accessibility.AccessibilityManager; |
| 65 | import android.view.accessibility.IAccessibilityManager; |
| 66 | import android.view.accessibility.IAccessibilityManagerClient; |
Tony Mantler | ab55f0f | 2017-06-16 10:50:00 -0700 | [diff] [blame] | 67 | |
Brett Chabot | 8091d9e | 2019-02-26 14:52:33 -0800 | [diff] [blame] | 68 | import androidx.test.runner.AndroidJUnit4; |
| 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) |
| 116 | .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE) |
| 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 | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 234 | private NotificationRecord getCustomLightsNotification() { |
| 235 | return getNotificationRecord(mId, false /* insistent */, true /* once */, |
| 236 | false /* noisy */, true /* buzzy*/, true /* lights */, |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 237 | true /* defaultVibration */, true /* defaultSound */, false /* defaultLights */, |
Tony Mantler | ab55f0f | 2017-06-16 10:50:00 -0700 | [diff] [blame] | 238 | null, 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 | |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 348 | private void verifyCustomBeep() throws RemoteException { |
| 349 | verify(mRingtonePlayer, times(1)).playAsync(eq(CUSTOM_SOUND), (UserHandle) anyObject(), |
| 350 | eq(false), (AudioAttributes) anyObject()); |
| 351 | } |
| 352 | |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 353 | private void verifyNeverStopAudio() throws RemoteException { |
| 354 | verify(mRingtonePlayer, never()).stopAsync(); |
| 355 | } |
| 356 | |
| 357 | private void verifyStopAudio() throws RemoteException { |
| 358 | verify(mRingtonePlayer, times(1)).stopAsync(); |
| 359 | } |
| 360 | |
| 361 | private void verifyNeverVibrate() { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 362 | verify(mVibrator, never()).vibrate(anyInt(), anyString(), (VibrationEffect) anyObject(), |
| 363 | (AudioAttributes) anyObject()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | private void verifyVibrate() { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 367 | verify(mVibrator, times(1)).vibrate(anyInt(), anyString(), argThat(mVibrateOnceMatcher), |
| 368 | (AudioAttributes) anyObject()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | private void verifyVibrateLooped() { |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 372 | verify(mVibrator, times(1)).vibrate(anyInt(), anyString(), argThat(mVibrateLoopMatcher), |
| 373 | (AudioAttributes) anyObject()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 374 | } |
| 375 | |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 376 | private void verifyDelayedVibrateLooped() { |
| 377 | verify(mVibrator, timeout(MAX_VIBRATION_DELAY).times(1)).vibrate(anyInt(), anyString(), |
| 378 | argThat(mVibrateLoopMatcher), (AudioAttributes) anyObject()); |
| 379 | } |
| 380 | |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 381 | private void verifyStopVibrate() { |
| 382 | verify(mVibrator, times(1)).cancel(); |
| 383 | } |
| 384 | |
| 385 | private void verifyNeverStopVibrate() throws RemoteException { |
| 386 | verify(mVibrator, never()).cancel(); |
| 387 | } |
| 388 | |
Julia Reynolds | 5436923 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 389 | private void verifyNeverLights() { |
| 390 | verify(mLight, never()).setFlashing(anyInt(), anyInt(), anyInt(), anyInt()); |
| 391 | } |
| 392 | |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 393 | private void verifyLights() { |
Julia Reynolds | 033a412 | 2017-01-31 16:50:38 -0500 | [diff] [blame] | 394 | verify(mLight, times(1)).setFlashing(anyInt(), anyInt(), anyInt(), anyInt()); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | private void verifyCustomLights() { |
Julia Reynolds | 033a412 | 2017-01-31 16:50:38 -0500 | [diff] [blame] | 398 | verify(mLight, times(1)).setFlashing( |
| 399 | 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] | 400 | } |
| 401 | |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 402 | // |
| 403 | // Tests |
| 404 | // |
| 405 | |
| 406 | @Test |
| 407 | public void testLights() throws Exception { |
| 408 | NotificationRecord r = getLightsNotification(); |
| 409 | r.setImportance(NotificationManager.IMPORTANCE_DEFAULT, "for testing"); |
| 410 | |
| 411 | mService.buzzBeepBlinkLocked(r); |
| 412 | |
| 413 | verifyLights(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 414 | assertTrue(r.isInterruptive()); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 415 | } |
| 416 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 417 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 418 | public void testBeep() throws Exception { |
| 419 | NotificationRecord r = getBeepyNotification(); |
| 420 | |
| 421 | mService.buzzBeepBlinkLocked(r); |
| 422 | |
| 423 | verifyBeepLooped(); |
| 424 | verifyNeverVibrate(); |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 425 | verify(mAccessibilityService, times(1)).sendAccessibilityEvent(any(), anyInt()); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 426 | assertTrue(r.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 427 | } |
| 428 | |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 429 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 430 | public void testBeepInsistently() throws Exception { |
| 431 | NotificationRecord r = getInsistentBeepyNotification(); |
| 432 | |
| 433 | mService.buzzBeepBlinkLocked(r); |
| 434 | |
| 435 | verifyBeep(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 436 | assertTrue(r.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 437 | } |
| 438 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 439 | @Test |
Tony Mantler | ab55f0f | 2017-06-16 10:50:00 -0700 | [diff] [blame] | 440 | public void testNoLeanbackBeep() throws Exception { |
| 441 | NotificationRecord r = getInsistentBeepyLeanbackNotification(); |
| 442 | |
| 443 | mService.buzzBeepBlinkLocked(r); |
| 444 | |
| 445 | verifyNeverBeep(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 446 | assertFalse(r.isInterruptive()); |
Tony Mantler | ab55f0f | 2017-06-16 10:50:00 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 450 | public void testNoInterruptionForMin() throws Exception { |
| 451 | NotificationRecord r = getBeepyNotification(); |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 452 | r.setImportance(NotificationManager.IMPORTANCE_MIN, "foo"); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 453 | |
| 454 | mService.buzzBeepBlinkLocked(r); |
| 455 | |
| 456 | verifyNeverBeep(); |
| 457 | verifyNeverVibrate(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 458 | assertFalse(r.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 459 | } |
| 460 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 461 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 462 | public void testNoInterruptionForIntercepted() throws Exception { |
| 463 | NotificationRecord r = getBeepyNotification(); |
| 464 | r.setIntercepted(true); |
| 465 | |
| 466 | mService.buzzBeepBlinkLocked(r); |
| 467 | |
| 468 | verifyNeverBeep(); |
| 469 | verifyNeverVibrate(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 470 | assertFalse(r.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 471 | } |
| 472 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 473 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 474 | public void testBeepTwice() throws Exception { |
| 475 | NotificationRecord r = getBeepyNotification(); |
| 476 | |
| 477 | // set up internal state |
| 478 | mService.buzzBeepBlinkLocked(r); |
| 479 | Mockito.reset(mRingtonePlayer); |
| 480 | |
| 481 | // update should beep |
| 482 | r.isUpdate = true; |
| 483 | mService.buzzBeepBlinkLocked(r); |
| 484 | verifyBeepLooped(); |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 485 | verify(mAccessibilityService, times(2)).sendAccessibilityEvent(any(), anyInt()); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 486 | assertTrue(r.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 487 | } |
| 488 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 489 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 490 | public void testHonorAlertOnlyOnceForBeep() throws Exception { |
| 491 | NotificationRecord r = getBeepyNotification(); |
| 492 | NotificationRecord s = getBeepyOnceNotification(); |
| 493 | s.isUpdate = true; |
| 494 | |
| 495 | // set up internal state |
| 496 | mService.buzzBeepBlinkLocked(r); |
| 497 | Mockito.reset(mRingtonePlayer); |
| 498 | |
| 499 | // update should not beep |
| 500 | mService.buzzBeepBlinkLocked(s); |
| 501 | verifyNeverBeep(); |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 502 | verify(mAccessibilityService, times(1)).sendAccessibilityEvent(any(), anyInt()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 503 | } |
| 504 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 505 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 506 | public void testNoisyUpdateDoesNotCancelAudio() throws Exception { |
| 507 | NotificationRecord r = getBeepyNotification(); |
| 508 | |
| 509 | mService.buzzBeepBlinkLocked(r); |
| 510 | r.isUpdate = true; |
| 511 | mService.buzzBeepBlinkLocked(r); |
| 512 | |
| 513 | verifyNeverStopAudio(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 514 | assertTrue(r.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 515 | } |
| 516 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 517 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 518 | public void testNoisyOnceUpdateDoesNotCancelAudio() throws Exception { |
| 519 | NotificationRecord r = getBeepyNotification(); |
| 520 | NotificationRecord s = getBeepyOnceNotification(); |
| 521 | s.isUpdate = true; |
| 522 | |
| 523 | mService.buzzBeepBlinkLocked(r); |
| 524 | mService.buzzBeepBlinkLocked(s); |
| 525 | |
| 526 | verifyNeverStopAudio(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 527 | assertTrue(r.isInterruptive()); |
| 528 | assertFalse(s.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 529 | } |
| 530 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 531 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 532 | public void testQuietUpdateDoesNotCancelAudioFromOther() throws Exception { |
| 533 | NotificationRecord r = getBeepyNotification(); |
| 534 | NotificationRecord s = getQuietNotification(); |
| 535 | s.isUpdate = true; |
| 536 | NotificationRecord other = getNoisyOtherNotification(); |
| 537 | |
| 538 | // set up internal state |
| 539 | mService.buzzBeepBlinkLocked(r); |
| 540 | mService.buzzBeepBlinkLocked(other); // this takes the audio stream |
| 541 | Mockito.reset(mRingtonePlayer); |
| 542 | |
| 543 | // should not stop noise, since we no longer own it |
| 544 | mService.buzzBeepBlinkLocked(s); // this no longer owns the stream |
| 545 | verifyNeverStopAudio(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 546 | assertTrue(other.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 547 | } |
| 548 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 549 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 550 | public void testQuietInterloperDoesNotCancelAudio() throws Exception { |
| 551 | NotificationRecord r = getBeepyNotification(); |
| 552 | NotificationRecord other = getQuietOtherNotification(); |
| 553 | |
| 554 | // set up internal state |
| 555 | mService.buzzBeepBlinkLocked(r); |
| 556 | Mockito.reset(mRingtonePlayer); |
| 557 | |
| 558 | // should not stop noise, since it does not own it |
| 559 | mService.buzzBeepBlinkLocked(other); |
| 560 | verifyNeverStopAudio(); |
| 561 | } |
| 562 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 563 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 564 | public void testQuietUpdateCancelsAudio() throws Exception { |
| 565 | NotificationRecord r = getBeepyNotification(); |
| 566 | NotificationRecord s = getQuietNotification(); |
| 567 | s.isUpdate = true; |
| 568 | |
| 569 | // set up internal state |
| 570 | mService.buzzBeepBlinkLocked(r); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 571 | assertTrue(r.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 572 | Mockito.reset(mRingtonePlayer); |
| 573 | |
| 574 | // quiet update should stop making noise |
| 575 | mService.buzzBeepBlinkLocked(s); |
| 576 | verifyStopAudio(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 577 | assertFalse(s.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 578 | } |
| 579 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 580 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 581 | public void testQuietOnceUpdateCancelsAudio() throws Exception { |
| 582 | NotificationRecord r = getBeepyNotification(); |
| 583 | NotificationRecord s = getQuietOnceNotification(); |
| 584 | s.isUpdate = true; |
| 585 | |
| 586 | // set up internal state |
| 587 | mService.buzzBeepBlinkLocked(r); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 588 | assertTrue(r.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 589 | Mockito.reset(mRingtonePlayer); |
| 590 | |
| 591 | // stop making noise - this is a weird corner case, but quiet should override once |
| 592 | mService.buzzBeepBlinkLocked(s); |
| 593 | verifyStopAudio(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 594 | assertFalse(s.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 595 | } |
| 596 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 597 | @Test |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 598 | public void testInCallNotification() throws Exception { |
| 599 | NotificationRecord r = getBeepyNotification(); |
| 600 | |
| 601 | // set up internal state |
| 602 | mService.buzzBeepBlinkLocked(r); |
| 603 | Mockito.reset(mRingtonePlayer); |
| 604 | |
| 605 | mService.mInCall = true; |
| 606 | mService.buzzBeepBlinkLocked(r); |
| 607 | |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 608 | verify(mService, times(1)).playInCallNotification(); |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 609 | verifyNeverBeep(); // doesn't play normal beep |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 610 | assertTrue(r.isInterruptive()); |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | @Test |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 614 | public void testNoDemoteSoundToVibrateIfVibrateGiven() throws Exception { |
| 615 | NotificationRecord r = getBuzzyBeepyNotification(); |
| 616 | assertTrue(r.getSound() != null); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 617 | |
| 618 | // the phone is quiet |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 619 | when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_VIBRATE); |
Julia Reynolds | 8589657 | 2017-09-20 12:54:52 -0400 | [diff] [blame] | 620 | when(mAudioManager.getStreamVolume(anyInt())).thenReturn(0); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 621 | |
| 622 | mService.buzzBeepBlinkLocked(r); |
| 623 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 624 | VibrationEffect effect = VibrationEffect.createWaveform(r.getVibration(), -1); |
| 625 | |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 626 | verify(mVibrator, timeout(MAX_VIBRATION_DELAY).times(1)).vibrate(anyInt(), anyString(), |
| 627 | eq(effect), (AudioAttributes) anyObject()); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 628 | assertTrue(r.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 629 | } |
| 630 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 631 | @Test |
Julia Reynolds | 8589657 | 2017-09-20 12:54:52 -0400 | [diff] [blame] | 632 | public void testNoDemoteSoundToVibrateIfNonNotificationStream() throws Exception { |
| 633 | NotificationRecord r = getBeepyNotification(); |
| 634 | assertTrue(r.getSound() != null); |
| 635 | assertNull(r.getVibration()); |
| 636 | |
| 637 | // the phone is quiet |
| 638 | when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_VIBRATE); |
| 639 | when(mAudioManager.getStreamVolume(anyInt())).thenReturn(1); |
| 640 | |
| 641 | mService.buzzBeepBlinkLocked(r); |
| 642 | |
| 643 | verifyNeverVibrate(); |
| 644 | verifyBeepLooped(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 645 | assertTrue(r.isInterruptive()); |
Julia Reynolds | 8589657 | 2017-09-20 12:54:52 -0400 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | @Test |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 649 | public void testDemoteSoundToVibrate() throws Exception { |
| 650 | NotificationRecord r = getBeepyNotification(); |
| 651 | assertTrue(r.getSound() != null); |
| 652 | assertNull(r.getVibration()); |
| 653 | |
| 654 | // the phone is quiet |
| 655 | when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_VIBRATE); |
Julia Reynolds | 8589657 | 2017-09-20 12:54:52 -0400 | [diff] [blame] | 656 | when(mAudioManager.getStreamVolume(anyInt())).thenReturn(0); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 657 | |
| 658 | mService.buzzBeepBlinkLocked(r); |
| 659 | |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 660 | verify(mVibrator, timeout(MAX_VIBRATION_DELAY).times(1)).vibrate(anyInt(), anyString(), |
| 661 | eq(FALLBACK_VIBRATION), (AudioAttributes) anyObject()); |
Julia Reynolds | 619a69f | 2017-01-27 15:11:38 -0500 | [diff] [blame] | 662 | verify(mRingtonePlayer, never()).playAsync |
| 663 | (anyObject(), anyObject(), anyBoolean(), anyObject()); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 664 | assertTrue(r.isInterruptive()); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | @Test |
| 668 | public void testDemoteInsistentSoundToVibrate() throws Exception { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 669 | NotificationRecord r = getInsistentBeepyNotification(); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 670 | assertTrue(r.getSound() != null); |
| 671 | assertNull(r.getVibration()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 672 | |
| 673 | // the phone is quiet |
| 674 | when(mAudioManager.getStreamVolume(anyInt())).thenReturn(0); |
| 675 | when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_VIBRATE); |
| 676 | |
| 677 | mService.buzzBeepBlinkLocked(r); |
| 678 | |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 679 | verifyDelayedVibrateLooped(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 680 | assertTrue(r.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 681 | } |
| 682 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 683 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 684 | public void testVibrate() throws Exception { |
| 685 | NotificationRecord r = getBuzzyNotification(); |
| 686 | |
| 687 | mService.buzzBeepBlinkLocked(r); |
| 688 | |
| 689 | verifyNeverBeep(); |
| 690 | verifyVibrate(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 691 | assertTrue(r.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 692 | } |
| 693 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 694 | @Test |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 695 | public void testInsistentVibrate() { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 696 | NotificationRecord r = getInsistentBuzzyNotification(); |
| 697 | |
| 698 | mService.buzzBeepBlinkLocked(r); |
| 699 | verifyVibrateLooped(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 700 | assertTrue(r.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 701 | } |
| 702 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 703 | @Test |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 704 | public void testVibrateTwice() { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 705 | NotificationRecord r = getBuzzyNotification(); |
| 706 | |
| 707 | // set up internal state |
| 708 | mService.buzzBeepBlinkLocked(r); |
| 709 | Mockito.reset(mVibrator); |
| 710 | |
| 711 | // update should vibrate |
| 712 | r.isUpdate = true; |
| 713 | mService.buzzBeepBlinkLocked(r); |
| 714 | verifyVibrate(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 715 | assertTrue(r.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 716 | } |
| 717 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 718 | @Test |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 719 | public void testGroupAlertSummarySilenceChild() throws Exception { |
| 720 | NotificationRecord child = getBeepyNotificationRecord("a", GROUP_ALERT_SUMMARY); |
| 721 | |
| 722 | mService.buzzBeepBlinkLocked(child); |
| 723 | |
| 724 | verifyNeverBeep(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 725 | assertFalse(child.isInterruptive()); |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | @Test |
| 729 | public void testGroupAlertSummaryNoSilenceSummary() throws Exception { |
| 730 | NotificationRecord summary = getBeepyNotificationRecord("a", GROUP_ALERT_SUMMARY); |
| 731 | summary.getNotification().flags |= Notification.FLAG_GROUP_SUMMARY; |
| 732 | |
| 733 | mService.buzzBeepBlinkLocked(summary); |
| 734 | |
| 735 | verifyBeepLooped(); |
Julia Reynolds | 5436923 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 736 | // summaries are never interruptive for notification counts |
| 737 | assertFalse(summary.isInterruptive()); |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | @Test |
| 741 | public void testGroupAlertSummaryNoSilenceNonGroupChild() throws Exception { |
| 742 | NotificationRecord nonGroup = getBeepyNotificationRecord(null, GROUP_ALERT_SUMMARY); |
| 743 | |
| 744 | mService.buzzBeepBlinkLocked(nonGroup); |
| 745 | |
| 746 | verifyBeepLooped(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 747 | assertTrue(nonGroup.isInterruptive()); |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | @Test |
| 751 | public void testGroupAlertChildSilenceSummary() throws Exception { |
| 752 | NotificationRecord summary = getBeepyNotificationRecord("a", GROUP_ALERT_CHILDREN); |
| 753 | summary.getNotification().flags |= Notification.FLAG_GROUP_SUMMARY; |
| 754 | |
| 755 | mService.buzzBeepBlinkLocked(summary); |
| 756 | |
| 757 | verifyNeverBeep(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 758 | assertFalse(summary.isInterruptive()); |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | @Test |
| 762 | public void testGroupAlertChildNoSilenceChild() throws Exception { |
| 763 | NotificationRecord child = getBeepyNotificationRecord("a", GROUP_ALERT_CHILDREN); |
| 764 | |
| 765 | mService.buzzBeepBlinkLocked(child); |
| 766 | |
| 767 | verifyBeepLooped(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 768 | assertTrue(child.isInterruptive()); |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | @Test |
| 772 | public void testGroupAlertChildNoSilenceNonGroupSummary() throws Exception { |
| 773 | NotificationRecord nonGroup = getBeepyNotificationRecord(null, GROUP_ALERT_CHILDREN); |
| 774 | |
| 775 | mService.buzzBeepBlinkLocked(nonGroup); |
| 776 | |
| 777 | verifyBeepLooped(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 778 | assertTrue(nonGroup.isInterruptive()); |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | @Test |
| 782 | public void testGroupAlertAllNoSilenceGroup() throws Exception { |
| 783 | NotificationRecord group = getBeepyNotificationRecord("a", GROUP_ALERT_ALL); |
| 784 | |
| 785 | mService.buzzBeepBlinkLocked(group); |
| 786 | |
| 787 | verifyBeepLooped(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 788 | assertTrue(group.isInterruptive()); |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 792 | public void testHonorAlertOnlyOnceForBuzz() throws Exception { |
| 793 | NotificationRecord r = getBuzzyNotification(); |
| 794 | NotificationRecord s = getBuzzyOnceNotification(); |
| 795 | s.isUpdate = true; |
| 796 | |
| 797 | // set up internal state |
| 798 | mService.buzzBeepBlinkLocked(r); |
| 799 | Mockito.reset(mVibrator); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 800 | assertTrue(r.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 801 | |
| 802 | // update should not beep |
| 803 | mService.buzzBeepBlinkLocked(s); |
| 804 | verifyNeverVibrate(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 805 | assertFalse(s.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 806 | } |
| 807 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 808 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 809 | public void testNoisyUpdateDoesNotCancelVibrate() throws Exception { |
| 810 | NotificationRecord r = getBuzzyNotification(); |
| 811 | |
| 812 | mService.buzzBeepBlinkLocked(r); |
| 813 | r.isUpdate = true; |
| 814 | mService.buzzBeepBlinkLocked(r); |
| 815 | |
| 816 | verifyNeverStopVibrate(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 817 | assertTrue(r.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 818 | } |
| 819 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 820 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 821 | public void testNoisyOnceUpdateDoesNotCancelVibrate() throws Exception { |
| 822 | NotificationRecord r = getBuzzyNotification(); |
| 823 | NotificationRecord s = getBuzzyOnceNotification(); |
| 824 | s.isUpdate = true; |
| 825 | |
| 826 | mService.buzzBeepBlinkLocked(r); |
| 827 | mService.buzzBeepBlinkLocked(s); |
| 828 | |
| 829 | verifyNeverStopVibrate(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 830 | assertTrue(r.isInterruptive()); |
| 831 | assertFalse(s.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 832 | } |
| 833 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 834 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 835 | public void testQuietUpdateDoesNotCancelVibrateFromOther() throws Exception { |
| 836 | NotificationRecord r = getBuzzyNotification(); |
| 837 | NotificationRecord s = getQuietNotification(); |
| 838 | s.isUpdate = true; |
| 839 | NotificationRecord other = getNoisyOtherNotification(); |
| 840 | |
| 841 | // set up internal state |
| 842 | mService.buzzBeepBlinkLocked(r); |
| 843 | mService.buzzBeepBlinkLocked(other); // this takes the vibrate stream |
| 844 | Mockito.reset(mVibrator); |
| 845 | |
| 846 | // should not stop vibrate, since we no longer own it |
| 847 | mService.buzzBeepBlinkLocked(s); // this no longer owns the stream |
| 848 | verifyNeverStopVibrate(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 849 | assertTrue(r.isInterruptive()); |
| 850 | assertTrue(other.isInterruptive()); |
| 851 | assertFalse(s.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 852 | } |
| 853 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 854 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 855 | public void testQuietInterloperDoesNotCancelVibrate() throws Exception { |
| 856 | NotificationRecord r = getBuzzyNotification(); |
| 857 | NotificationRecord other = getQuietOtherNotification(); |
| 858 | |
| 859 | // set up internal state |
| 860 | mService.buzzBeepBlinkLocked(r); |
| 861 | Mockito.reset(mVibrator); |
| 862 | |
| 863 | // should not stop noise, since it does not own it |
| 864 | mService.buzzBeepBlinkLocked(other); |
| 865 | verifyNeverStopVibrate(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 866 | assertFalse(other.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 867 | } |
| 868 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 869 | @Test |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 870 | public void testQuietUpdateCancelsVibrate() { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 871 | NotificationRecord r = getBuzzyNotification(); |
| 872 | NotificationRecord s = getQuietNotification(); |
| 873 | s.isUpdate = true; |
| 874 | |
| 875 | // set up internal state |
| 876 | mService.buzzBeepBlinkLocked(r); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 877 | verifyVibrate(); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 878 | |
| 879 | // quiet update should stop making noise |
| 880 | mService.buzzBeepBlinkLocked(s); |
| 881 | verifyStopVibrate(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 882 | assertTrue(r.isInterruptive()); |
| 883 | assertFalse(s.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 884 | } |
| 885 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 886 | @Test |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 887 | public void testQuietOnceUpdateCancelVibrate() throws Exception { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 888 | NotificationRecord r = getBuzzyNotification(); |
| 889 | NotificationRecord s = getQuietOnceNotification(); |
| 890 | s.isUpdate = true; |
| 891 | |
| 892 | // set up internal state |
| 893 | mService.buzzBeepBlinkLocked(r); |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 894 | verifyVibrate(); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 895 | |
| 896 | // stop making noise - this is a weird corner case, but quiet should override once |
| 897 | mService.buzzBeepBlinkLocked(s); |
| 898 | verifyStopVibrate(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 899 | assertTrue(r.isInterruptive()); |
| 900 | assertFalse(s.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 901 | } |
| 902 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 903 | @Test |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 904 | public void testQuietUpdateCancelsDemotedVibrate() throws Exception { |
| 905 | NotificationRecord r = getBeepyNotification(); |
| 906 | NotificationRecord s = getQuietNotification(); |
| 907 | |
| 908 | // the phone is quiet |
| 909 | when(mAudioManager.getStreamVolume(anyInt())).thenReturn(0); |
| 910 | when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_VIBRATE); |
| 911 | |
| 912 | mService.buzzBeepBlinkLocked(r); |
| 913 | |
| 914 | // quiet update should stop making noise |
| 915 | mService.buzzBeepBlinkLocked(s); |
| 916 | verifyStopVibrate(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 917 | assertTrue(r.isInterruptive()); |
| 918 | assertFalse(s.isInterruptive()); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 919 | } |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 920 | |
Julia Reynolds | ca80d16 | 2017-04-23 12:39:32 -0400 | [diff] [blame] | 921 | @Test |
Seungho Lee | 0e97ae6 | 2018-10-31 21:49:09 +0900 | [diff] [blame] | 922 | public void testCanceledNoisyNeverVibrate() throws Exception { |
| 923 | NotificationRecord r = getBuzzyBeepyNotification(); |
| 924 | |
| 925 | final int waitMs = mAudioManager.getFocusRampTimeMs( |
| 926 | AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK, |
| 927 | r.getAudioAttributes()); |
| 928 | |
| 929 | mService.buzzBeepBlinkLocked(r); |
| 930 | mService.clearNotifications(); |
| 931 | |
| 932 | verifyNeverVibrate(); |
| 933 | Thread.sleep(waitMs); |
| 934 | verifyNeverVibrate(); |
| 935 | } |
| 936 | |
| 937 | @Test |
Julia Reynolds | ca80d16 | 2017-04-23 12:39:32 -0400 | [diff] [blame] | 938 | public void testEmptyUriSoundTreatedAsNoSound() throws Exception { |
| 939 | NotificationChannel channel = new NotificationChannel("test", "test", IMPORTANCE_HIGH); |
| 940 | channel.setSound(Uri.EMPTY, null); |
| 941 | final Notification n = new Builder(getContext(), "test") |
| 942 | .setSmallIcon(android.R.drawable.sym_def_app_icon).build(); |
| 943 | |
| 944 | StatusBarNotification sbn = new StatusBarNotification(mPkg, mPkg, 0, mTag, mUid, |
| 945 | mPid, n, mUser, null, System.currentTimeMillis()); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 946 | NotificationRecord r = new NotificationRecord(getContext(), sbn, channel); |
Julia Reynolds | ca80d16 | 2017-04-23 12:39:32 -0400 | [diff] [blame] | 947 | mService.addNotification(r); |
| 948 | |
| 949 | mService.buzzBeepBlinkLocked(r); |
Julia Reynolds | ca80d16 | 2017-04-23 12:39:32 -0400 | [diff] [blame] | 950 | verifyNeverBeep(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 951 | assertFalse(r.isInterruptive()); |
Julia Reynolds | ca80d16 | 2017-04-23 12:39:32 -0400 | [diff] [blame] | 952 | } |
| 953 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 954 | @Test |
| 955 | public void testRepeatedSoundOverLimitMuted() throws Exception { |
| 956 | when(mUsageStats.isAlertRateLimited(any())).thenReturn(true); |
| 957 | |
| 958 | NotificationRecord r = getBeepyNotification(); |
| 959 | |
| 960 | mService.buzzBeepBlinkLocked(r); |
| 961 | verifyNeverBeep(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 962 | assertFalse(r.isInterruptive()); |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | @Test |
| 966 | public void testPostingSilentNotificationDoesNotAffectRateLimiting() throws Exception { |
| 967 | NotificationRecord r = getQuietNotification(); |
| 968 | mService.buzzBeepBlinkLocked(r); |
| 969 | |
| 970 | verify(mUsageStats, never()).isAlertRateLimited(any()); |
| 971 | } |
| 972 | |
| 973 | @Test |
Julia Reynolds | 65b85cf | 2017-07-20 09:19:20 -0400 | [diff] [blame] | 974 | public void testPostingGroupSuppressedDoesNotAffectRateLimiting() throws Exception { |
| 975 | NotificationRecord summary = getBeepyNotificationRecord("a", GROUP_ALERT_CHILDREN); |
| 976 | summary.getNotification().flags |= Notification.FLAG_GROUP_SUMMARY; |
| 977 | |
| 978 | mService.buzzBeepBlinkLocked(summary); |
Julia Reynolds | 65b85cf | 2017-07-20 09:19:20 -0400 | [diff] [blame] | 979 | verify(mUsageStats, never()).isAlertRateLimited(any()); |
| 980 | } |
| 981 | |
| 982 | @Test |
Julia Reynolds | 79dfdd6 | 2018-04-17 15:36:33 -0400 | [diff] [blame] | 983 | public void testGroupSuppressionFailureDoesNotAffectRateLimiting() { |
| 984 | NotificationRecord summary = getBeepyNotificationRecord("a", GROUP_ALERT_SUMMARY); |
| 985 | summary.getNotification().flags |= Notification.FLAG_GROUP_SUMMARY; |
| 986 | |
| 987 | mService.buzzBeepBlinkLocked(summary); |
| 988 | verify(mUsageStats, times(1)).isAlertRateLimited(any()); |
| 989 | } |
| 990 | |
| 991 | @Test |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 992 | public void testCrossUserSoundMuted() throws Exception { |
| 993 | final Notification n = new Builder(getContext(), "test") |
| 994 | .setSmallIcon(android.R.drawable.sym_def_app_icon).build(); |
| 995 | |
| 996 | int userId = mUser.getIdentifier() + 1; |
| 997 | StatusBarNotification sbn = new StatusBarNotification(mPkg, mPkg, 0, mTag, mUid, |
| 998 | mPid, n, UserHandle.of(userId), null, System.currentTimeMillis()); |
| 999 | NotificationRecord r = new NotificationRecord(getContext(), sbn, |
| 1000 | new NotificationChannel("test", "test", IMPORTANCE_HIGH)); |
| 1001 | |
| 1002 | mService.buzzBeepBlinkLocked(r); |
| 1003 | verifyNeverBeep(); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 1004 | assertFalse(r.isInterruptive()); |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 1005 | } |
| 1006 | |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 1007 | @Test |
| 1008 | public void testA11yMinInitialPost() throws Exception { |
| 1009 | NotificationRecord r = getQuietNotification(); |
| 1010 | r.setImportance(IMPORTANCE_MIN, ""); |
| 1011 | mService.buzzBeepBlinkLocked(r); |
| 1012 | verify(mAccessibilityService, never()).sendAccessibilityEvent(any(), anyInt()); |
| 1013 | } |
| 1014 | |
| 1015 | @Test |
| 1016 | public void testA11yQuietInitialPost() throws Exception { |
| 1017 | NotificationRecord r = getQuietNotification(); |
| 1018 | mService.buzzBeepBlinkLocked(r); |
| 1019 | verify(mAccessibilityService, times(1)).sendAccessibilityEvent(any(), anyInt()); |
| 1020 | } |
| 1021 | |
| 1022 | @Test |
| 1023 | public void testA11yQuietUpdate() throws Exception { |
| 1024 | NotificationRecord r = getQuietNotification(); |
| 1025 | mService.buzzBeepBlinkLocked(r); |
| 1026 | r.isUpdate = true; |
| 1027 | mService.buzzBeepBlinkLocked(r); |
| 1028 | verify(mAccessibilityService, times(1)).sendAccessibilityEvent(any(), anyInt()); |
| 1029 | } |
| 1030 | |
Julia Reynolds | 5436923 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 1031 | @Test |
| 1032 | public void testLightsScreenOn() { |
| 1033 | mService.mScreenOn = true; |
| 1034 | NotificationRecord r = getLightsNotification(); |
| 1035 | mService.buzzBeepBlinkLocked(r); |
| 1036 | verifyNeverLights(); |
| 1037 | assertFalse(r.isInterruptive()); |
| 1038 | } |
| 1039 | |
| 1040 | @Test |
| 1041 | public void testLightsInCall() { |
| 1042 | mService.mInCall = true; |
| 1043 | NotificationRecord r = getLightsNotification(); |
| 1044 | mService.buzzBeepBlinkLocked(r); |
| 1045 | verifyNeverLights(); |
| 1046 | assertFalse(r.isInterruptive()); |
| 1047 | } |
| 1048 | |
| 1049 | @Test |
| 1050 | public void testLightsSilentUpdate() { |
| 1051 | NotificationRecord r = getLightsOnceNotification(); |
| 1052 | mService.buzzBeepBlinkLocked(r); |
| 1053 | verifyLights(); |
| 1054 | assertTrue(r.isInterruptive()); |
| 1055 | |
| 1056 | r = getLightsOnceNotification(); |
| 1057 | r.isUpdate = true; |
| 1058 | mService.buzzBeepBlinkLocked(r); |
| 1059 | // checks that lights happened once, i.e. this new call didn't trigger them again |
| 1060 | verifyLights(); |
| 1061 | assertFalse(r.isInterruptive()); |
| 1062 | } |
| 1063 | |
| 1064 | @Test |
| 1065 | public void testLightsUnimportant() { |
| 1066 | NotificationRecord r = getLightsNotification(); |
| 1067 | r.setImportance(IMPORTANCE_LOW, "testing"); |
| 1068 | mService.buzzBeepBlinkLocked(r); |
| 1069 | verifyNeverLights(); |
| 1070 | assertFalse(r.isInterruptive()); |
| 1071 | } |
| 1072 | |
| 1073 | @Test |
| 1074 | public void testLightsNoLights() { |
| 1075 | NotificationRecord r = getQuietNotification(); |
| 1076 | mService.buzzBeepBlinkLocked(r); |
| 1077 | verifyNeverLights(); |
| 1078 | assertFalse(r.isInterruptive()); |
| 1079 | } |
| 1080 | |
| 1081 | @Test |
| 1082 | public void testLightsNoLightOnDevice() { |
| 1083 | mService.mHasLight = false; |
| 1084 | NotificationRecord r = getLightsNotification(); |
| 1085 | mService.buzzBeepBlinkLocked(r); |
| 1086 | verifyNeverLights(); |
| 1087 | assertFalse(r.isInterruptive()); |
| 1088 | } |
| 1089 | |
| 1090 | @Test |
| 1091 | public void testLightsLightsOffGlobally() { |
| 1092 | mService.mNotificationPulseEnabled = false; |
| 1093 | NotificationRecord r = getLightsNotification(); |
| 1094 | mService.buzzBeepBlinkLocked(r); |
| 1095 | verifyNeverLights(); |
| 1096 | assertFalse(r.isInterruptive()); |
| 1097 | } |
| 1098 | |
| 1099 | @Test |
| 1100 | public void testLightsDndIntercepted() { |
| 1101 | NotificationRecord r = getLightsNotification(); |
| 1102 | r.setSuppressedVisualEffects(SUPPRESSED_EFFECT_LIGHTS); |
| 1103 | mService.buzzBeepBlinkLocked(r); |
| 1104 | verifyNeverLights(); |
| 1105 | assertFalse(r.isInterruptive()); |
| 1106 | } |
| 1107 | |
| 1108 | @Test |
| 1109 | public void testGroupAlertSummaryNoLightsChild() { |
| 1110 | NotificationRecord child = getLightsNotificationRecord("a", GROUP_ALERT_SUMMARY); |
| 1111 | |
| 1112 | mService.buzzBeepBlinkLocked(child); |
| 1113 | |
| 1114 | verifyNeverLights(); |
| 1115 | assertFalse(child.isInterruptive()); |
| 1116 | } |
| 1117 | |
| 1118 | @Test |
| 1119 | public void testGroupAlertSummaryLightsSummary() { |
| 1120 | NotificationRecord summary = getLightsNotificationRecord("a", GROUP_ALERT_SUMMARY); |
| 1121 | summary.getNotification().flags |= Notification.FLAG_GROUP_SUMMARY; |
| 1122 | |
| 1123 | mService.buzzBeepBlinkLocked(summary); |
| 1124 | |
| 1125 | verifyLights(); |
| 1126 | // summaries should never count for interruptiveness counts |
| 1127 | assertFalse(summary.isInterruptive()); |
| 1128 | } |
| 1129 | |
| 1130 | @Test |
| 1131 | public void testGroupAlertSummaryLightsNonGroupChild() { |
| 1132 | NotificationRecord nonGroup = getLightsNotificationRecord(null, GROUP_ALERT_SUMMARY); |
| 1133 | |
| 1134 | mService.buzzBeepBlinkLocked(nonGroup); |
| 1135 | |
| 1136 | verifyLights(); |
| 1137 | assertTrue(nonGroup.isInterruptive()); |
| 1138 | } |
| 1139 | |
| 1140 | @Test |
| 1141 | public void testGroupAlertChildNoLightsSummary() { |
| 1142 | NotificationRecord summary = getLightsNotificationRecord("a", GROUP_ALERT_CHILDREN); |
| 1143 | summary.getNotification().flags |= Notification.FLAG_GROUP_SUMMARY; |
| 1144 | |
| 1145 | mService.buzzBeepBlinkLocked(summary); |
| 1146 | |
| 1147 | verifyNeverLights(); |
| 1148 | assertFalse(summary.isInterruptive()); |
| 1149 | } |
| 1150 | |
| 1151 | @Test |
| 1152 | public void testGroupAlertChildLightsChild() { |
| 1153 | NotificationRecord child = getLightsNotificationRecord("a", GROUP_ALERT_CHILDREN); |
| 1154 | |
| 1155 | mService.buzzBeepBlinkLocked(child); |
| 1156 | |
| 1157 | verifyLights(); |
| 1158 | assertTrue(child.isInterruptive()); |
| 1159 | } |
| 1160 | |
| 1161 | @Test |
| 1162 | public void testGroupAlertChildLightsNonGroupSummary() { |
| 1163 | NotificationRecord nonGroup = getLightsNotificationRecord(null, GROUP_ALERT_CHILDREN); |
| 1164 | |
| 1165 | mService.buzzBeepBlinkLocked(nonGroup); |
| 1166 | |
| 1167 | verifyLights(); |
| 1168 | assertTrue(nonGroup.isInterruptive()); |
| 1169 | } |
| 1170 | |
| 1171 | @Test |
| 1172 | public void testGroupAlertAllLightsGroup() { |
| 1173 | NotificationRecord group = getLightsNotificationRecord("a", GROUP_ALERT_ALL); |
| 1174 | |
| 1175 | mService.buzzBeepBlinkLocked(group); |
| 1176 | |
| 1177 | verifyLights(); |
| 1178 | assertTrue(group.isInterruptive()); |
| 1179 | } |
| 1180 | |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 1181 | static class VibrateRepeatMatcher implements ArgumentMatcher<VibrationEffect> { |
| 1182 | private final int mRepeatIndex; |
| 1183 | |
| 1184 | VibrateRepeatMatcher(int repeatIndex) { |
| 1185 | mRepeatIndex = repeatIndex; |
| 1186 | } |
| 1187 | |
| 1188 | @Override |
| 1189 | public boolean matches(VibrationEffect actual) { |
| 1190 | if (actual instanceof VibrationEffect.Waveform && |
| 1191 | ((VibrationEffect.Waveform) actual).getRepeatIndex() == mRepeatIndex) { |
| 1192 | return true; |
| 1193 | } |
| 1194 | // All non-waveform effects are essentially one shots. |
| 1195 | return mRepeatIndex == -1; |
| 1196 | } |
| 1197 | |
| 1198 | @Override |
| 1199 | public String toString() { |
| 1200 | return "repeatIndex=" + mRepeatIndex; |
| 1201 | } |
| 1202 | } |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 1203 | } |