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