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