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