Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [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 | |
| 17 | package com.android.server.notification; |
| 18 | |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 19 | import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND; |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 20 | import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE; |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 21 | import static android.app.Notification.FLAG_AUTO_CANCEL; |
Mady Mellor | 49b1bf1 | 2019-03-29 12:00:02 -0700 | [diff] [blame] | 22 | import static android.app.Notification.FLAG_BUBBLE; |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 23 | import static android.app.Notification.FLAG_FOREGROUND_SERVICE; |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 24 | import static android.app.NotificationChannel.USER_LOCKED_ALLOW_BUBBLE; |
| 25 | import static android.app.NotificationManager.BUBBLE_PREFERENCE_ALL; |
| 26 | import static android.app.NotificationManager.BUBBLE_PREFERENCE_NONE; |
| 27 | import static android.app.NotificationManager.BUBBLE_PREFERENCE_SELECTED; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 28 | import static android.app.NotificationManager.EXTRA_BLOCKED_STATE; |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 29 | import static android.app.NotificationManager.IMPORTANCE_DEFAULT; |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 30 | import static android.app.NotificationManager.IMPORTANCE_HIGH; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 31 | import static android.app.NotificationManager.IMPORTANCE_LOW; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 32 | import static android.app.NotificationManager.IMPORTANCE_MAX; |
Julia Reynolds | 4da7970 | 2017-06-01 11:06:10 -0400 | [diff] [blame] | 33 | import static android.app.NotificationManager.IMPORTANCE_NONE; |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 34 | import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED; |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 35 | import static android.app.NotificationManager.Policy.PRIORITY_CATEGORY_CALLS; |
| 36 | import static android.app.NotificationManager.Policy.PRIORITY_CATEGORY_CONVERSATIONS; |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 37 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_AMBIENT; |
| 38 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_BADGE; |
| 39 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_FULL_SCREEN_INTENT; |
| 40 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_LIGHTS; |
| 41 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_NOTIFICATION_LIST; |
| 42 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK; |
| 43 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_OFF; |
| 44 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_ON; |
| 45 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_STATUS_BAR; |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 46 | import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE; |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 47 | import static android.content.pm.PackageManager.FEATURE_WATCH; |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 48 | import static android.content.pm.PackageManager.PERMISSION_DENIED; |
Julia Reynolds | 7a6d07a | 2019-03-18 11:31:56 -0400 | [diff] [blame] | 49 | import static android.content.pm.PackageManager.PERMISSION_GRANTED; |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 50 | import static android.os.Build.VERSION_CODES.O_MR1; |
| 51 | import static android.os.Build.VERSION_CODES.P; |
Julia Reynolds | 57a974b | 2019-10-07 11:51:47 -0400 | [diff] [blame] | 52 | import static android.os.UserHandle.USER_SYSTEM; |
Julia Reynolds | c7dcdc2 | 2019-03-25 10:26:14 -0400 | [diff] [blame] | 53 | import static android.service.notification.Adjustment.KEY_IMPORTANCE; |
| 54 | import static android.service.notification.Adjustment.KEY_USER_SENTIMENT; |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 55 | import static android.service.notification.NotificationListenerService.Ranking.USER_SENTIMENT_NEGATIVE; |
| 56 | import static android.service.notification.NotificationListenerService.Ranking.USER_SENTIMENT_NEUTRAL; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 57 | |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 58 | import static junit.framework.Assert.assertEquals; |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 59 | import static junit.framework.Assert.assertFalse; |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 60 | import static junit.framework.Assert.assertNotNull; |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 61 | import static junit.framework.Assert.assertNull; |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 62 | import static junit.framework.Assert.assertTrue; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 63 | import static junit.framework.Assert.fail; |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 64 | |
Jay Aliomer | fea8025 | 2019-11-20 18:14:24 -0500 | [diff] [blame] | 65 | import static org.mockito.ArgumentMatchers.isNull; |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 66 | import static org.mockito.Matchers.anyBoolean; |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 67 | import static org.mockito.Matchers.anyLong; |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 68 | import static org.mockito.Matchers.anyString; |
| 69 | import static org.mockito.Matchers.eq; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 70 | import static org.mockito.Mockito.any; |
| 71 | import static org.mockito.Mockito.anyInt; |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 72 | import static org.mockito.Mockito.clearInvocations; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 73 | import static org.mockito.Mockito.doAnswer; |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 74 | import static org.mockito.Mockito.doNothing; |
Julia Reynolds | ea58c20 | 2020-02-03 12:52:49 -0500 | [diff] [blame] | 75 | import static org.mockito.Mockito.doThrow; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 76 | import static org.mockito.Mockito.mock; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 77 | import static org.mockito.Mockito.never; |
| 78 | import static org.mockito.Mockito.reset; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 79 | import static org.mockito.Mockito.spy; |
| 80 | import static org.mockito.Mockito.timeout; |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 81 | import static org.mockito.Mockito.times; |
| 82 | import static org.mockito.Mockito.verify; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 83 | import static org.mockito.Mockito.when; |
| 84 | |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 85 | import android.app.ActivityManager; |
Mady Mellor | 13f9bc8 | 2020-03-24 19:09:28 -0700 | [diff] [blame] | 86 | import android.app.ActivityManagerInternal; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 87 | import android.app.AppOpsManager; |
Julia Reynolds | a94365d | 2019-04-09 10:48:43 -0400 | [diff] [blame] | 88 | import android.app.AutomaticZenRule; |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 89 | import android.app.IActivityManager; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 90 | import android.app.INotificationManager; |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 91 | import android.app.ITransientNotification; |
| 92 | import android.app.IUriGrantsManager; |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 93 | import android.app.Notification; |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 94 | import android.app.Notification.MessagingStyle.Message; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 95 | import android.app.NotificationChannel; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 96 | import android.app.NotificationChannelGroup; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 97 | import android.app.NotificationManager; |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 98 | import android.app.PendingIntent; |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 99 | import android.app.Person; |
Mady Mellor | a10448e | 2019-04-26 13:50:58 -0700 | [diff] [blame] | 100 | import android.app.RemoteInput; |
Yotam Aron | 7429997 | 2020-01-16 16:20:58 +0200 | [diff] [blame] | 101 | import android.app.StatsManager; |
Jason Parks | 50322ff | 2018-03-27 10:23:33 -0500 | [diff] [blame] | 102 | import android.app.admin.DevicePolicyManagerInternal; |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 103 | import android.app.usage.UsageStatsManagerInternal; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 104 | import android.companion.ICompanionDeviceManager; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 105 | import android.content.ComponentName; |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 106 | import android.content.ContentUris; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 107 | import android.content.Context; |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 108 | import android.content.Intent; |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 109 | import android.content.pm.ActivityInfo; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 110 | import android.content.pm.ApplicationInfo; |
| 111 | import android.content.pm.IPackageManager; |
Mady Mellor | 2ac2d3a | 2020-01-08 17:18:54 -0800 | [diff] [blame] | 112 | import android.content.pm.LauncherApps; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 113 | import android.content.pm.PackageManager; |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 114 | import android.content.pm.ParceledListSlice; |
Mady Mellor | 2ac2d3a | 2020-01-08 17:18:54 -0800 | [diff] [blame] | 115 | import android.content.pm.ShortcutInfo; |
Julia Reynolds | fa27307 | 2020-04-14 15:31:21 -0400 | [diff] [blame] | 116 | import android.content.pm.ShortcutServiceInternal; |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 117 | import android.content.pm.UserInfo; |
Kristian Monsen | 05f3479 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 118 | import android.content.res.Resources; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 119 | import android.graphics.Color; |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 120 | import android.graphics.drawable.Icon; |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 121 | import android.media.AudioManager; |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 122 | import android.net.Uri; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 123 | import android.os.Binder; |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 124 | import android.os.Build; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 125 | import android.os.Bundle; |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 126 | import android.os.IBinder; |
Julia Reynolds | 0f76734 | 2019-12-18 09:11:55 -0500 | [diff] [blame] | 127 | import android.os.Parcel; |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 128 | import android.os.Process; |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 129 | import android.os.RemoteException; |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 130 | import android.os.SystemClock; |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 131 | import android.os.UserHandle; |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 132 | import android.os.UserManager; |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 133 | import android.provider.DeviceConfig; |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 134 | import android.provider.MediaStore; |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 135 | import android.provider.Settings; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 136 | import android.service.notification.Adjustment; |
Julia Reynolds | 882f206 | 2020-02-05 12:11:38 -0500 | [diff] [blame] | 137 | import android.service.notification.ConversationChannelWrapper; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 138 | import android.service.notification.NotificationListenerService; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 139 | import android.service.notification.NotificationStats; |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 140 | import android.service.notification.StatusBarNotification; |
Julia Reynolds | a94365d | 2019-04-09 10:48:43 -0400 | [diff] [blame] | 141 | import android.service.notification.ZenPolicy; |
Will Brockman | af25fbd | 2020-03-26 15:49:47 -0400 | [diff] [blame] | 142 | import android.telephony.TelephonyManager; |
Geoffrey Pitsch | 8185d38 | 2017-05-19 18:41:32 -0400 | [diff] [blame] | 143 | import android.test.suitebuilder.annotation.SmallTest; |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 144 | import android.testing.AndroidTestingRunner; |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 145 | import android.testing.TestableContext; |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 146 | import android.testing.TestableLooper; |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 147 | import android.testing.TestableLooper.RunWithLooper; |
Julia Reynolds | 7a6d07a | 2019-03-18 11:31:56 -0400 | [diff] [blame] | 148 | import android.testing.TestablePermissions; |
Julia Reynolds | 57a974b | 2019-10-07 11:51:47 -0400 | [diff] [blame] | 149 | import android.testing.TestableResources; |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 150 | import android.text.Html; |
Julia Reynolds | 0f76734 | 2019-12-18 09:11:55 -0500 | [diff] [blame] | 151 | import android.text.TextUtils; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 152 | import android.util.ArrayMap; |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 153 | import android.util.ArraySet; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 154 | import android.util.AtomicFile; |
Julia Reynolds | 469144c | 2019-06-21 14:30:28 -0400 | [diff] [blame] | 155 | import android.util.Xml; |
Julia Reynolds | b681ffe | 2019-06-19 13:40:46 -0400 | [diff] [blame] | 156 | import android.widget.RemoteViews; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 157 | |
Mady Mellor | 49b1bf1 | 2019-03-29 12:00:02 -0700 | [diff] [blame] | 158 | import androidx.annotation.Nullable; |
| 159 | import androidx.test.InstrumentationRegistry; |
| 160 | |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 161 | import com.android.internal.config.sysui.SystemUiDeviceConfigFlags; |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 162 | import com.android.internal.logging.InstanceIdSequence; |
| 163 | import com.android.internal.logging.InstanceIdSequenceFake; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 164 | import com.android.internal.statusbar.NotificationVisibility; |
Julia Reynolds | 469144c | 2019-06-21 14:30:28 -0400 | [diff] [blame] | 165 | import com.android.internal.util.FastXmlSerializer; |
Mady Mellor | 13f9bc8 | 2020-03-24 19:09:28 -0700 | [diff] [blame] | 166 | import com.android.server.DeviceIdleInternal; |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 167 | import com.android.server.LocalServices; |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 168 | import com.android.server.SystemService; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 169 | import com.android.server.UiServiceTestCase; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 170 | import com.android.server.lights.LightsManager; |
Ivailo Karamanolev | f773e10 | 2020-01-16 16:10:42 +0100 | [diff] [blame] | 171 | import com.android.server.lights.LogicalLight; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 172 | import com.android.server.notification.NotificationManagerService.NotificationAssistants; |
| 173 | import com.android.server.notification.NotificationManagerService.NotificationListeners; |
Bernardo Rufino | 18725b6 | 2020-01-21 14:12:43 +0000 | [diff] [blame] | 174 | import com.android.server.statusbar.StatusBarManagerInternal; |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 175 | import com.android.server.uri.UriGrantsManagerInternal; |
Bernardo Rufino | e6cb310 | 2020-03-06 20:33:11 +0000 | [diff] [blame] | 176 | import com.android.server.wm.ActivityTaskManagerInternal; |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 177 | import com.android.server.wm.WindowManagerInternal; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 178 | |
| 179 | import org.junit.After; |
| 180 | import org.junit.Before; |
Chris Wren | 0f0e877 | 2020-06-10 10:02:19 -0400 | [diff] [blame] | 181 | import org.junit.Ignore; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 182 | import org.junit.Test; |
| 183 | import org.junit.runner.RunWith; |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 184 | import org.mockito.ArgumentCaptor; |
| 185 | import org.mockito.Mock; |
| 186 | import org.mockito.MockitoAnnotations; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 187 | import org.mockito.stubbing.Answer; |
Julia Reynolds | 469144c | 2019-06-21 14:30:28 -0400 | [diff] [blame] | 188 | import org.xmlpull.v1.XmlPullParser; |
| 189 | import org.xmlpull.v1.XmlSerializer; |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 190 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 191 | import java.io.BufferedInputStream; |
Julia Reynolds | 469144c | 2019-06-21 14:30:28 -0400 | [diff] [blame] | 192 | import java.io.BufferedOutputStream; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 193 | import java.io.ByteArrayInputStream; |
Julia Reynolds | 469144c | 2019-06-21 14:30:28 -0400 | [diff] [blame] | 194 | import java.io.ByteArrayOutputStream; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 195 | import java.io.File; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 196 | import java.io.FileOutputStream; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 197 | import java.util.ArrayList; |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 198 | import java.util.Arrays; |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 199 | import java.util.Collections; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 200 | import java.util.List; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 201 | import java.util.Map; |
Robin Lee | d107af6 | 2018-04-27 13:55:56 +0200 | [diff] [blame] | 202 | import java.util.function.Consumer; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 203 | |
Julia Reynolds | b681ffe | 2019-06-19 13:40:46 -0400 | [diff] [blame] | 204 | |
Geoffrey Pitsch | 8185d38 | 2017-05-19 18:41:32 -0400 | [diff] [blame] | 205 | @SmallTest |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 206 | @RunWith(AndroidTestingRunner.class) |
| 207 | @RunWithLooper |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 208 | public class NotificationManagerServiceTest extends UiServiceTestCase { |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 209 | private static final String TEST_CHANNEL_ID = "NotificationManagerServiceTestChannelId"; |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 210 | |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 211 | private final int mUid = Binder.getCallingUid(); |
Julia Reynolds | d78263d | 2018-01-30 10:40:41 -0500 | [diff] [blame] | 212 | private TestableNotificationManagerService mService; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 213 | private INotificationManager mBinderService; |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 214 | private NotificationManagerInternal mInternalService; |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 215 | private ShortcutHelper mShortcutHelper; |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 216 | @Mock |
| 217 | private IPackageManager mPackageManager; |
| 218 | @Mock |
| 219 | private PackageManager mPackageManagerClient; |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 220 | @Mock |
| 221 | private WindowManagerInternal mWindowManagerInternal; |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 222 | private TestableContext mContext = spy(getContext()); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 223 | private final String PKG = mContext.getPackageName(); |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 224 | private TestableLooper mTestableLooper; |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 225 | @Mock |
| 226 | private RankingHelper mRankingHelper; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 227 | @Mock private PreferencesHelper mPreferencesHelper; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 228 | AtomicFile mPolicyFile; |
| 229 | File mFile; |
| 230 | @Mock |
Geoffrey Pitsch | d5bcf21 | 2017-06-01 15:45:35 -0400 | [diff] [blame] | 231 | private NotificationUsageStats mUsageStats; |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 232 | @Mock |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 233 | private UsageStatsManagerInternal mAppUsageStats; |
| 234 | @Mock |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 235 | private AudioManager mAudioManager; |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 236 | @Mock |
Mady Mellor | 2ac2d3a | 2020-01-08 17:18:54 -0800 | [diff] [blame] | 237 | private LauncherApps mLauncherApps; |
| 238 | @Mock |
Julia Reynolds | fa27307 | 2020-04-14 15:31:21 -0400 | [diff] [blame] | 239 | private ShortcutServiceInternal mShortcutServiceInternal; |
| 240 | @Mock |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 241 | ActivityManager mActivityManager; |
Kristian Monsen | 05f3479 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 242 | @Mock |
| 243 | Resources mResources; |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 244 | @Mock |
| 245 | RankingHandler mRankingHandler; |
Julia Reynolds | 3ff26d2 | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 246 | |
Julia Reynolds | b317ff7 | 2019-11-26 14:20:51 -0500 | [diff] [blame] | 247 | private static final int MAX_POST_DELAY = 1000; |
| 248 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 249 | private NotificationChannel mTestNotificationChannel = new NotificationChannel( |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 250 | TEST_CHANNEL_ID, TEST_CHANNEL_ID, IMPORTANCE_DEFAULT); |
Gustav Sennton | a8e38aa | 2019-01-22 14:55:39 +0000 | [diff] [blame] | 251 | |
| 252 | private static final int NOTIFICATION_LOCATION_UNKNOWN = 0; |
| 253 | |
Julia Reynolds | c76888d | 2020-05-04 15:36:09 -0400 | [diff] [blame] | 254 | private static final String VALID_CONVO_SHORTCUT_ID = "shortcut"; |
| 255 | |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 256 | @Mock |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 257 | private NotificationListeners mListeners; |
| 258 | @Mock private NotificationAssistants mAssistants; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 259 | @Mock private ConditionProviders mConditionProviders; |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 260 | private ManagedServices.ManagedServiceInfo mListener; |
| 261 | @Mock private ICompanionDeviceManager mCompanionMgr; |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 262 | @Mock SnoozeHelper mSnoozeHelper; |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 263 | @Mock GroupHelper mGroupHelper; |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 264 | @Mock |
| 265 | IBinder mPermOwner; |
| 266 | @Mock |
| 267 | IActivityManager mAm; |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 268 | @Mock |
Bernardo Rufino | e6cb310 | 2020-03-06 20:33:11 +0000 | [diff] [blame] | 269 | ActivityTaskManagerInternal mAtm; |
| 270 | @Mock |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 271 | IUriGrantsManager mUgm; |
| 272 | @Mock |
| 273 | UriGrantsManagerInternal mUgmInternal; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 274 | @Mock |
| 275 | AppOpsManager mAppOpsManager; |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 276 | @Mock |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 277 | private TestableNotificationManagerService.NotificationAssistantAccessGrantedCallback |
| 278 | mNotificationAssistantAccessGrantedCallback; |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 279 | @Mock |
| 280 | UserManager mUm; |
Julia Reynolds | b317ff7 | 2019-11-26 14:20:51 -0500 | [diff] [blame] | 281 | @Mock |
| 282 | NotificationHistoryManager mHistoryManager; |
Yotam Aron | 7429997 | 2020-01-16 16:20:58 +0200 | [diff] [blame] | 283 | @Mock |
| 284 | StatsManager mStatsManager; |
Will Brockman | 2b6959e | 2020-01-22 09:59:50 -0500 | [diff] [blame] | 285 | NotificationRecordLoggerFake mNotificationRecordLogger = new NotificationRecordLoggerFake(); |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 286 | private InstanceIdSequence mNotificationInstanceIdSequence = new InstanceIdSequenceFake( |
| 287 | 1 << 30); |
Bernardo Rufino | 18725b6 | 2020-01-21 14:12:43 +0000 | [diff] [blame] | 288 | @Mock |
| 289 | StatusBarManagerInternal mStatusBar; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 290 | |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 291 | // Use a Testable subclass so we can simulate calls from the system without failing. |
| 292 | private static class TestableNotificationManagerService extends NotificationManagerService { |
Julia Reynolds | d78263d | 2018-01-30 10:40:41 -0500 | [diff] [blame] | 293 | int countSystemChecks = 0; |
Brad Stenning | 8c991ea | 2018-07-31 13:33:01 -0700 | [diff] [blame] | 294 | boolean isSystemUid = true; |
Gustav Sennton | 44dc588 | 2018-12-13 14:38:50 +0000 | [diff] [blame] | 295 | int countLogSmartSuggestionsVisible = 0; |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 296 | @Nullable |
| 297 | NotificationAssistantAccessGrantedCallback mNotificationAssistantAccessGrantedCallback; |
Julia Reynolds | d78263d | 2018-01-30 10:40:41 -0500 | [diff] [blame] | 298 | |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 299 | TestableNotificationManagerService(Context context, NotificationRecordLogger logger, |
| 300 | InstanceIdSequence notificationInstanceIdSequence) { |
| 301 | super(context, logger, notificationInstanceIdSequence); |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 302 | } |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 303 | |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 304 | RankingHelper getRankingHelper() { |
| 305 | return mRankingHelper; |
| 306 | } |
| 307 | |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 308 | @Override |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 309 | protected boolean isCallingUidSystem() { |
Julia Reynolds | d78263d | 2018-01-30 10:40:41 -0500 | [diff] [blame] | 310 | countSystemChecks++; |
Brad Stenning | 8c991ea | 2018-07-31 13:33:01 -0700 | [diff] [blame] | 311 | return isSystemUid; |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | @Override |
| 315 | protected boolean isCallerSystemOrPhone() { |
Julia Reynolds | d78263d | 2018-01-30 10:40:41 -0500 | [diff] [blame] | 316 | countSystemChecks++; |
Brad Stenning | 8c991ea | 2018-07-31 13:33:01 -0700 | [diff] [blame] | 317 | return isSystemUid; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 318 | } |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 319 | |
| 320 | @Override |
| 321 | protected ICompanionDeviceManager getCompanionManager() { |
| 322 | return null; |
| 323 | } |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 324 | |
| 325 | @Override |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 326 | protected void reportUserInteraction(NotificationRecord r) { |
| 327 | return; |
| 328 | } |
Julia Reynolds | b62dad4 | 2018-11-26 16:33:02 -0500 | [diff] [blame] | 329 | |
| 330 | @Override |
| 331 | protected void handleSavePolicyFile() { |
| 332 | return; |
| 333 | } |
Gustav Sennton | 44dc588 | 2018-12-13 14:38:50 +0000 | [diff] [blame] | 334 | |
| 335 | @Override |
Gustav Sennton | c7d0d32 | 2019-01-07 15:36:41 +0000 | [diff] [blame] | 336 | void logSmartSuggestionsVisible(NotificationRecord r, int notificationLocation) { |
| 337 | super.logSmartSuggestionsVisible(r, notificationLocation); |
Gustav Sennton | 44dc588 | 2018-12-13 14:38:50 +0000 | [diff] [blame] | 338 | countLogSmartSuggestionsVisible++; |
| 339 | } |
| 340 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 341 | @Override |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 342 | protected void setNotificationAssistantAccessGrantedForUserInternal( |
| 343 | ComponentName assistant, int userId, boolean granted) { |
| 344 | if (mNotificationAssistantAccessGrantedCallback != null) { |
| 345 | mNotificationAssistantAccessGrantedCallback.onGranted(assistant, userId, granted); |
| 346 | return; |
| 347 | } |
| 348 | super.setNotificationAssistantAccessGrantedForUserInternal(assistant, userId, granted); |
| 349 | } |
| 350 | |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 351 | @Override |
| 352 | protected String[] getStringArrayResource(int key) { |
| 353 | return new String[] {PKG_O}; |
| 354 | } |
| 355 | |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 356 | private void setNotificationAssistantAccessGrantedCallback( |
| 357 | @Nullable NotificationAssistantAccessGrantedCallback callback) { |
| 358 | this.mNotificationAssistantAccessGrantedCallback = callback; |
| 359 | } |
| 360 | |
| 361 | interface NotificationAssistantAccessGrantedCallback { |
| 362 | void onGranted(ComponentName assistant, int userId, boolean granted); |
| 363 | } |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 364 | } |
| 365 | |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 366 | private class TestableToastCallback extends ITransientNotification.Stub { |
| 367 | @Override |
| 368 | public void show(IBinder windowToken) { |
| 369 | } |
| 370 | |
| 371 | @Override |
| 372 | public void hide() { |
| 373 | } |
| 374 | } |
| 375 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 376 | @Before |
| 377 | public void setUp() throws Exception { |
Beverly | 24d103b | 2020-02-18 17:40:57 -0500 | [diff] [blame] | 378 | // Shell permisssions will override permissions of our app, so add all necessary permissions |
Beverly | 080782f | 2020-02-24 16:42:57 -0500 | [diff] [blame] | 379 | // for this test here: |
Stanislav Zholnin | 872afd4 | 2019-03-12 15:57:25 +0000 | [diff] [blame] | 380 | InstrumentationRegistry.getInstrumentation().getUiAutomation().adoptShellPermissionIdentity( |
Beverly | 24d103b | 2020-02-18 17:40:57 -0500 | [diff] [blame] | 381 | "android.permission.WRITE_DEVICE_CONFIG", |
| 382 | "android.permission.READ_DEVICE_CONFIG", |
| 383 | "android.permission.READ_CONTACTS"); |
Stanislav Zholnin | 872afd4 | 2019-03-12 15:57:25 +0000 | [diff] [blame] | 384 | |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 385 | MockitoAnnotations.initMocks(this); |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 386 | |
Mady Mellor | 13f9bc8 | 2020-03-24 19:09:28 -0700 | [diff] [blame] | 387 | DeviceIdleInternal deviceIdleInternal = mock(DeviceIdleInternal.class); |
| 388 | when(deviceIdleInternal.getNotificationWhitelistDuration()).thenReturn(3000L); |
| 389 | ActivityManagerInternal activityManagerInternal = mock(ActivityManagerInternal.class); |
| 390 | |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 391 | LocalServices.removeServiceForTest(UriGrantsManagerInternal.class); |
| 392 | LocalServices.addService(UriGrantsManagerInternal.class, mUgmInternal); |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 393 | LocalServices.removeServiceForTest(WindowManagerInternal.class); |
| 394 | LocalServices.addService(WindowManagerInternal.class, mWindowManagerInternal); |
Bernardo Rufino | 18725b6 | 2020-01-21 14:12:43 +0000 | [diff] [blame] | 395 | LocalServices.removeServiceForTest(StatusBarManagerInternal.class); |
| 396 | LocalServices.addService(StatusBarManagerInternal.class, mStatusBar); |
Mady Mellor | 13f9bc8 | 2020-03-24 19:09:28 -0700 | [diff] [blame] | 397 | LocalServices.removeServiceForTest(DeviceIdleInternal.class); |
| 398 | LocalServices.addService(DeviceIdleInternal.class, deviceIdleInternal); |
| 399 | LocalServices.removeServiceForTest(ActivityManagerInternal.class); |
| 400 | LocalServices.addService(ActivityManagerInternal.class, activityManagerInternal); |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 401 | |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 402 | doNothing().when(mContext).sendBroadcastAsUser(any(), any(), any()); |
| 403 | |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 404 | mService = new TestableNotificationManagerService(mContext, mNotificationRecordLogger, |
| 405 | mNotificationInstanceIdSequence); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 406 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 407 | // Use this testable looper. |
| 408 | mTestableLooper = TestableLooper.get(this); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 409 | // MockPackageManager - default returns ApplicationInfo with matching calling UID |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 410 | mContext.setMockPackageManager(mPackageManagerClient); |
Julia Reynolds | b681ffe | 2019-06-19 13:40:46 -0400 | [diff] [blame] | 411 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 412 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), anyInt())) |
Julia Reynolds | b681ffe | 2019-06-19 13:40:46 -0400 | [diff] [blame] | 413 | .thenAnswer((Answer<ApplicationInfo>) invocation -> { |
| 414 | Object[] args = invocation.getArguments(); |
| 415 | return getApplicationInfo((String) args[0], mUid); |
| 416 | }); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 417 | when(mPackageManagerClient.getApplicationInfoAsUser(anyString(), anyInt(), anyInt())) |
Julia Reynolds | b681ffe | 2019-06-19 13:40:46 -0400 | [diff] [blame] | 418 | .thenAnswer((Answer<ApplicationInfo>) invocation -> { |
| 419 | Object[] args = invocation.getArguments(); |
| 420 | return getApplicationInfo((String) args[0], mUid); |
| 421 | }); |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 422 | when(mPackageManagerClient.getPackageUidAsUser(any(), anyInt())).thenReturn(mUid); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 423 | final LightsManager mockLightsManager = mock(LightsManager.class); |
Ivailo Karamanolev | f773e10 | 2020-01-16 16:10:42 +0100 | [diff] [blame] | 424 | when(mockLightsManager.getLight(anyInt())).thenReturn(mock(LogicalLight.class)); |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 425 | when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_NORMAL); |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 426 | when(mPackageManagerClient.hasSystemFeature(FEATURE_WATCH)).thenReturn(false); |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 427 | when(mUgmInternal.newUriPermissionOwner(anyString())).thenReturn(mPermOwner); |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 428 | when(mPackageManager.getPackagesForUid(mUid)).thenReturn(new String[]{PKG}); |
Julia Reynolds | 4214da9 | 2019-04-10 15:04:06 -0400 | [diff] [blame] | 429 | when(mPackageManagerClient.getPackagesForUid(anyInt())).thenReturn(new String[]{PKG}); |
Julia Reynolds | e99db5a | 2019-04-16 12:50:04 -0400 | [diff] [blame] | 430 | mContext.addMockSystemService(AppOpsManager.class, mock(AppOpsManager.class)); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 431 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 432 | // write to a test file; the system file isn't readable from tests |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 433 | mFile = new File(mContext.getCacheDir(), "test.xml"); |
| 434 | mFile.createNewFile(); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 435 | final String preupgradeXml = "<notification-policy></notification-policy>"; |
| 436 | mPolicyFile = new AtomicFile(mFile); |
| 437 | FileOutputStream fos = mPolicyFile.startWrite(); |
| 438 | fos.write(preupgradeXml.getBytes()); |
| 439 | mPolicyFile.finishWrite(fos); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 440 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 441 | // Setup managed services |
| 442 | mListener = mListeners.new ManagedServiceInfo( |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 443 | null, new ComponentName(PKG, "test_class"), |
| 444 | UserHandle.getUserId(mUid), true, null, 0); |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 445 | ComponentName defaultComponent = ComponentName.unflattenFromString("config/device"); |
| 446 | ArraySet<ComponentName> components = new ArraySet<>(); |
| 447 | components.add(defaultComponent); |
| 448 | when(mListeners.getDefaultComponents()).thenReturn(components); |
| 449 | when(mConditionProviders.getDefaultPackages()) |
| 450 | .thenReturn(new ArraySet<>(Arrays.asList("config"))); |
| 451 | when(mAssistants.getDefaultComponents()).thenReturn(components); |
| 452 | when(mAssistants.queryPackageForServices( |
| 453 | anyString(), anyInt(), anyInt())).thenReturn(components); |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 454 | when(mListeners.checkServiceTokenLocked(null)).thenReturn(mListener); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 455 | ManagedServices.Config listenerConfig = new ManagedServices.Config(); |
| 456 | listenerConfig.xmlTag = NotificationListeners.TAG_ENABLED_NOTIFICATION_LISTENERS; |
| 457 | when(mListeners.getConfig()).thenReturn(listenerConfig); |
| 458 | ManagedServices.Config assistantConfig = new ManagedServices.Config(); |
| 459 | assistantConfig.xmlTag = NotificationAssistants.TAG_ENABLED_NOTIFICATION_ASSISTANTS; |
| 460 | when(mAssistants.getConfig()).thenReturn(assistantConfig); |
| 461 | ManagedServices.Config dndConfig = new ManagedServices.Config(); |
| 462 | dndConfig.xmlTag = ConditionProviders.TAG_ENABLED_DND_APPS; |
| 463 | when(mConditionProviders.getConfig()).thenReturn(dndConfig); |
| 464 | |
Julia Reynolds | 418a8ff | 2019-03-21 10:45:10 -0400 | [diff] [blame] | 465 | when(mAssistants.isAdjustmentAllowed(anyString())).thenReturn(true); |
| 466 | |
Julia Reynolds | b317ff7 | 2019-11-26 14:20:51 -0500 | [diff] [blame] | 467 | mService.init(mService.new WorkerHandler(mTestableLooper.getLooper()), |
| 468 | mRankingHandler, mPackageManager, mPackageManagerClient, mockLightsManager, |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 469 | mListeners, mAssistants, mConditionProviders, |
| 470 | mCompanionMgr, mSnoozeHelper, mUsageStats, mPolicyFile, mActivityManager, |
Bernardo Rufino | e6cb310 | 2020-03-06 20:33:11 +0000 | [diff] [blame] | 471 | mGroupHelper, mAm, mAtm, mAppUsageStats, |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 472 | mock(DevicePolicyManagerInternal.class), mUgm, mUgmInternal, |
Will Brockman | af25fbd | 2020-03-26 15:49:47 -0400 | [diff] [blame] | 473 | mAppOpsManager, mUm, mHistoryManager, mStatsManager, |
| 474 | mock(TelephonyManager.class)); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 475 | mService.onBootPhase(SystemService.PHASE_SYSTEM_SERVICES_READY); |
| 476 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 477 | mService.setAudioManager(mAudioManager); |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 478 | |
| 479 | mShortcutHelper = mService.getShortcutHelper(); |
| 480 | mShortcutHelper.setLauncherApps(mLauncherApps); |
Julia Reynolds | fa27307 | 2020-04-14 15:31:21 -0400 | [diff] [blame] | 481 | mShortcutHelper.setShortcutServiceInternal(mShortcutServiceInternal); |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 482 | |
Julia Reynolds | c76888d | 2020-05-04 15:36:09 -0400 | [diff] [blame] | 483 | // Pretend the shortcut exists |
| 484 | List<ShortcutInfo> shortcutInfos = new ArrayList<>(); |
| 485 | ShortcutInfo info = mock(ShortcutInfo.class); |
| 486 | when(info.getPackage()).thenReturn(PKG); |
| 487 | when(info.getId()).thenReturn(VALID_CONVO_SHORTCUT_ID); |
| 488 | when(info.getUserId()).thenReturn(USER_SYSTEM); |
| 489 | when(info.isLongLived()).thenReturn(true); |
| 490 | when(info.isEnabled()).thenReturn(true); |
| 491 | shortcutInfos.add(info); |
| 492 | when(mLauncherApps.getShortcuts(any(), any())).thenReturn(shortcutInfos); |
| 493 | when(mShortcutServiceInternal.isSharingShortcut(anyInt(), anyString(), anyString(), |
| 494 | anyString(), anyInt(), any())).thenReturn(true); |
| 495 | |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 496 | // Set the testable bubble extractor |
| 497 | RankingHelper rankingHelper = mService.getRankingHelper(); |
| 498 | BubbleExtractor extractor = rankingHelper.findExtractor(BubbleExtractor.class); |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 499 | extractor.setActivityManager(mActivityManager); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 500 | |
| 501 | // Tests call directly into the Binder. |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 502 | mBinderService = mService.getBinderService(); |
| 503 | mInternalService = mService.getInternalService(); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 504 | |
| 505 | mBinderService.createNotificationChannels( |
| 506 | PKG, new ParceledListSlice(Arrays.asList(mTestNotificationChannel))); |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 507 | mBinderService.createNotificationChannels( |
| 508 | PKG_P, new ParceledListSlice(Arrays.asList(mTestNotificationChannel))); |
| 509 | mBinderService.createNotificationChannels( |
| 510 | PKG_O, new ParceledListSlice(Arrays.asList(mTestNotificationChannel))); |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 511 | assertNotNull(mBinderService.getNotificationChannel( |
| 512 | PKG, mContext.getUserId(), PKG, TEST_CHANNEL_ID)); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 513 | clearInvocations(mRankingHandler); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 514 | } |
| 515 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 516 | @After |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 517 | public void assertNotificationRecordLoggerCallsValid() { |
| 518 | for (NotificationRecordLoggerFake.CallRecord call : mNotificationRecordLogger.getCalls()) { |
| 519 | if (call.wasLogged) { |
| 520 | assertNotNull(call.event); |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | @After |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 526 | public void tearDown() throws Exception { |
Julia Reynolds | b681ffe | 2019-06-19 13:40:46 -0400 | [diff] [blame] | 527 | if (mFile != null) mFile.delete(); |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 528 | clearDeviceConfig(); |
Julia Reynolds | b317ff7 | 2019-11-26 14:20:51 -0500 | [diff] [blame] | 529 | mService.unregisterDeviceConfigChange(); |
Stanislav Zholnin | 872afd4 | 2019-03-12 15:57:25 +0000 | [diff] [blame] | 530 | InstrumentationRegistry.getInstrumentation() |
| 531 | .getUiAutomation().dropShellPermissionIdentity(); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 532 | } |
| 533 | |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 534 | private ArrayMap<Boolean, ArrayList<ComponentName>> generateResetComponentValues() { |
| 535 | ArrayMap<Boolean, ArrayList<ComponentName>> changed = new ArrayMap<>(); |
| 536 | changed.put(true, new ArrayList<>()); |
| 537 | changed.put(false, new ArrayList<>()); |
| 538 | return changed; |
| 539 | } |
Julia Reynolds | b681ffe | 2019-06-19 13:40:46 -0400 | [diff] [blame] | 540 | private ApplicationInfo getApplicationInfo(String pkg, int uid) { |
| 541 | final ApplicationInfo applicationInfo = new ApplicationInfo(); |
| 542 | applicationInfo.uid = uid; |
| 543 | switch (pkg) { |
| 544 | case PKG_N_MR1: |
| 545 | applicationInfo.targetSdkVersion = Build.VERSION_CODES.N_MR1; |
| 546 | break; |
| 547 | case PKG_O: |
| 548 | applicationInfo.targetSdkVersion = Build.VERSION_CODES.O; |
| 549 | break; |
| 550 | case PKG_P: |
| 551 | applicationInfo.targetSdkVersion = Build.VERSION_CODES.P; |
| 552 | break; |
| 553 | default: |
| 554 | applicationInfo.targetSdkVersion = Build.VERSION_CODES.CUR_DEVELOPMENT; |
| 555 | break; |
| 556 | } |
| 557 | return applicationInfo; |
| 558 | } |
| 559 | |
Julia Reynolds | 7bcb57b | 2018-01-22 10:37:58 -0500 | [diff] [blame] | 560 | public void waitForIdle() { |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 561 | mTestableLooper.processAllMessages(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 562 | } |
| 563 | |
Julia Reynolds | 12ba4cf | 2020-01-10 16:01:38 -0500 | [diff] [blame] | 564 | private void setUpPrefsForBubbles(String pkg, int uid, boolean globalEnabled, |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 565 | int pkgPref, boolean channelEnabled) { |
Julia Reynolds | 12ba4cf | 2020-01-10 16:01:38 -0500 | [diff] [blame] | 566 | Settings.Global.putInt(mContext.getContentResolver(), |
| 567 | Settings.Global.NOTIFICATION_BUBBLES, globalEnabled ? 1 : 0); |
| 568 | mService.mPreferencesHelper.updateBubblesEnabled(); |
| 569 | assertEquals(globalEnabled, mService.mPreferencesHelper.bubblesEnabled()); |
| 570 | try { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 571 | mBinderService.setBubblesAllowed(pkg, uid, pkgPref); |
Julia Reynolds | 12ba4cf | 2020-01-10 16:01:38 -0500 | [diff] [blame] | 572 | } catch (RemoteException e) { |
| 573 | e.printStackTrace(); |
| 574 | } |
Mady Mellor | c682034 | 2019-05-20 12:04:36 -0700 | [diff] [blame] | 575 | mTestNotificationChannel.setAllowBubbles(channelEnabled); |
| 576 | } |
| 577 | |
Julia Reynolds | 7bcb57b | 2018-01-22 10:37:58 -0500 | [diff] [blame] | 578 | private StatusBarNotification generateSbn(String pkg, int uid, long postTime, int userId) { |
| 579 | Notification.Builder nb = new Notification.Builder(mContext, "a") |
| 580 | .setContentTitle("foo") |
| 581 | .setSmallIcon(android.R.drawable.sym_def_app_icon); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 582 | StatusBarNotification sbn = new StatusBarNotification(pkg, pkg, uid, |
| 583 | "tag" + System.currentTimeMillis(), uid, 0, |
Julia Reynolds | 7bcb57b | 2018-01-22 10:37:58 -0500 | [diff] [blame] | 584 | nb.build(), new UserHandle(userId), null, postTime); |
| 585 | return sbn; |
| 586 | } |
| 587 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 588 | private NotificationRecord generateNotificationRecord(NotificationChannel channel, int id, |
| 589 | String groupKey, boolean isSummary) { |
| 590 | Notification.Builder nb = new Notification.Builder(mContext, channel.getId()) |
| 591 | .setContentTitle("foo") |
| 592 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 593 | .setGroup(groupKey) |
| 594 | .setGroupSummary(isSummary); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 595 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, id, |
| 596 | "tag" + System.currentTimeMillis(), mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 597 | nb.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 598 | return new NotificationRecord(mContext, sbn, channel); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 599 | } |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 600 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 601 | private NotificationRecord generateNotificationRecord(NotificationChannel channel) { |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 602 | return generateNotificationRecord(channel, null); |
| 603 | } |
| 604 | |
| 605 | private NotificationRecord generateNotificationRecord(NotificationChannel channel, |
| 606 | Notification.TvExtender extender) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 607 | if (channel == null) { |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 608 | channel = mTestNotificationChannel; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 609 | } |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 610 | Notification.Builder nb = new Notification.Builder(mContext, channel.getId()) |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 611 | .setContentTitle("foo") |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 612 | .setSmallIcon(android.R.drawable.sym_def_app_icon); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 613 | if (extender != null) { |
| 614 | nb.extend(extender); |
| 615 | } |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 616 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 8, "tag", mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 617 | nb.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 618 | return new NotificationRecord(mContext, sbn, channel); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 619 | } |
| 620 | |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 621 | private NotificationRecord generateNotificationRecord(NotificationChannel channel, int userId) { |
| 622 | if (channel == null) { |
| 623 | channel = mTestNotificationChannel; |
| 624 | } |
| 625 | Notification.Builder nb = new Notification.Builder(mContext, channel.getId()) |
| 626 | .setContentTitle("foo") |
| 627 | .setSmallIcon(android.R.drawable.sym_def_app_icon); |
| 628 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, "tag", mUid, 0, |
| 629 | nb.build(), new UserHandle(userId), null, 0); |
| 630 | return new NotificationRecord(mContext, sbn, channel); |
| 631 | } |
| 632 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 633 | private NotificationRecord generateMessageBubbleNotifRecord(NotificationChannel channel, |
| 634 | String tag) { |
| 635 | return generateMessageBubbleNotifRecord(true, channel, 1, tag, null, false); |
| 636 | } |
| 637 | |
| 638 | private NotificationRecord generateMessageBubbleNotifRecord(boolean addMetadata, |
| 639 | NotificationChannel channel, int id, String tag, String groupKey, boolean isSummary) { |
| 640 | if (channel == null) { |
| 641 | channel = mTestNotificationChannel; |
| 642 | } |
| 643 | if (tag == null) { |
| 644 | tag = "tag"; |
| 645 | } |
| 646 | Notification.Builder nb = getMessageStyleNotifBuilder(addMetadata, groupKey, isSummary); |
| 647 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, id, |
| 648 | tag, mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 649 | nb.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 650 | return new NotificationRecord(mContext, sbn, channel); |
| 651 | } |
| 652 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 653 | private Map<String, Answer> getSignalExtractorSideEffects() { |
| 654 | Map<String, Answer> answers = new ArrayMap<>(); |
| 655 | |
| 656 | answers.put("override group key", invocationOnMock -> { |
| 657 | ((NotificationRecord) invocationOnMock.getArguments()[0]) |
| 658 | .setOverrideGroupKey("bananas"); |
| 659 | return null; |
| 660 | }); |
| 661 | answers.put("override people", invocationOnMock -> { |
| 662 | ((NotificationRecord) invocationOnMock.getArguments()[0]) |
| 663 | .setPeopleOverride(new ArrayList<>()); |
| 664 | return null; |
| 665 | }); |
| 666 | answers.put("snooze criteria", invocationOnMock -> { |
| 667 | ((NotificationRecord) invocationOnMock.getArguments()[0]) |
| 668 | .setSnoozeCriteria(new ArrayList<>()); |
| 669 | return null; |
| 670 | }); |
| 671 | answers.put("notification channel", invocationOnMock -> { |
| 672 | ((NotificationRecord) invocationOnMock.getArguments()[0]) |
| 673 | .updateNotificationChannel(new NotificationChannel("a", "", IMPORTANCE_LOW)); |
| 674 | return null; |
| 675 | }); |
| 676 | answers.put("badging", invocationOnMock -> { |
| 677 | NotificationRecord r = (NotificationRecord) invocationOnMock.getArguments()[0]; |
| 678 | r.setShowBadge(!r.canShowBadge()); |
| 679 | return null; |
| 680 | }); |
Julia Reynolds | 4509ce7 | 2019-01-31 13:12:43 -0500 | [diff] [blame] | 681 | answers.put("bubbles", invocationOnMock -> { |
| 682 | NotificationRecord r = (NotificationRecord) invocationOnMock.getArguments()[0]; |
| 683 | r.setAllowBubble(!r.canBubble()); |
| 684 | return null; |
| 685 | }); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 686 | answers.put("package visibility", invocationOnMock -> { |
| 687 | ((NotificationRecord) invocationOnMock.getArguments()[0]).setPackageVisibilityOverride( |
| 688 | Notification.VISIBILITY_SECRET); |
| 689 | return null; |
| 690 | }); |
| 691 | |
| 692 | return answers; |
| 693 | } |
| 694 | |
Julia Reynolds | c7dcdc2 | 2019-03-25 10:26:14 -0400 | [diff] [blame] | 695 | private void clearDeviceConfig() { |
| 696 | DeviceConfig.resetToDefaults( |
| 697 | Settings.RESET_MODE_PACKAGE_DEFAULTS, DeviceConfig.NAMESPACE_SYSTEMUI); |
| 698 | } |
| 699 | |
| 700 | private void setDefaultAssistantInDeviceConfig(String componentName) { |
| 701 | DeviceConfig.setProperty( |
| 702 | DeviceConfig.NAMESPACE_SYSTEMUI, |
| 703 | SystemUiDeviceConfigFlags.NAS_DEFAULT_SERVICE, |
| 704 | componentName, |
| 705 | false); |
| 706 | } |
| 707 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 708 | private Notification.Builder getMessageStyleNotifBuilder(boolean addBubbleMetadata, |
| 709 | String groupKey, boolean isSummary) { |
| 710 | // Give it a person |
| 711 | Person person = new Person.Builder() |
| 712 | .setName("bubblebot") |
| 713 | .build(); |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 714 | RemoteInput remoteInput = new RemoteInput.Builder("reply_key").setLabel("reply").build(); |
| 715 | PendingIntent inputIntent = PendingIntent.getActivity(mContext, 0, new Intent(), 0); |
| 716 | Icon icon = Icon.createWithResource(mContext, android.R.drawable.sym_def_app_icon); |
| 717 | Notification.Action replyAction = new Notification.Action.Builder(icon, "Reply", |
| 718 | inputIntent).addRemoteInput(remoteInput) |
| 719 | .build(); |
| 720 | // Make it messaging style |
| 721 | Notification.Builder nb = new Notification.Builder(mContext, |
| 722 | mTestNotificationChannel.getId()) |
| 723 | .setContentTitle("foo") |
| 724 | .setStyle(new Notification.MessagingStyle(person) |
| 725 | .setConversationTitle("Bubble Chat") |
| 726 | .addMessage("Hello?", |
| 727 | SystemClock.currentThreadTimeMillis() - 300000, person) |
| 728 | .addMessage("Is it me you're looking for?", |
| 729 | SystemClock.currentThreadTimeMillis(), person) |
| 730 | ) |
| 731 | .setActions(replyAction) |
| 732 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
Julia Reynolds | c76888d | 2020-05-04 15:36:09 -0400 | [diff] [blame] | 733 | .setShortcutId(VALID_CONVO_SHORTCUT_ID) |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 734 | .setGroupSummary(isSummary); |
| 735 | if (groupKey != null) { |
| 736 | nb.setGroup(groupKey); |
| 737 | } |
| 738 | if (addBubbleMetadata) { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 739 | nb.setBubbleMetadata(getBubbleMetadata()); |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 740 | } |
| 741 | return nb; |
| 742 | } |
| 743 | |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 744 | private Notification.BubbleMetadata getBubbleMetadata() { |
| 745 | PendingIntent pendingIntent = mock(PendingIntent.class); |
| 746 | Intent intent = mock(Intent.class); |
| 747 | when(pendingIntent.getIntent()).thenReturn(intent); |
| 748 | |
| 749 | ActivityInfo info = new ActivityInfo(); |
| 750 | info.resizeMode = RESIZE_MODE_RESIZEABLE; |
| 751 | when(intent.resolveActivityInfo(any(), anyInt())).thenReturn(info); |
| 752 | |
| 753 | return new Notification.BubbleMetadata.Builder( |
| 754 | pendingIntent, |
| 755 | Icon.createWithResource(mContext, android.R.drawable.sym_def_app_icon)) |
| 756 | .build(); |
| 757 | } |
| 758 | |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 759 | private NotificationRecord addGroupWithBubblesAndValidateAdded(boolean summaryAutoCancel) |
| 760 | throws RemoteException { |
| 761 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 762 | String groupKey = "BUBBLE_GROUP"; |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 763 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 764 | // Notification that has bubble metadata |
| 765 | NotificationRecord nrBubble = generateMessageBubbleNotifRecord(true /* addMetadata */, |
| 766 | mTestNotificationChannel, 1 /* id */, "tag", groupKey, false /* isSummary */); |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 767 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 768 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nrBubble.getSbn().getTag(), |
| 769 | nrBubble.getSbn().getId(), nrBubble.getSbn().getNotification(), |
| 770 | nrBubble.getSbn().getUserId()); |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 771 | waitForIdle(); |
| 772 | |
| 773 | // Make sure we are a bubble |
| 774 | StatusBarNotification[] notifsAfter = mBinderService.getActiveNotifications(PKG); |
| 775 | assertEquals(1, notifsAfter.length); |
| 776 | assertTrue((notifsAfter[0].getNotification().flags & FLAG_BUBBLE) != 0); |
| 777 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 778 | // Notification without bubble metadata |
| 779 | NotificationRecord nrPlain = generateMessageBubbleNotifRecord(false /* addMetadata */, |
| 780 | mTestNotificationChannel, 2 /* id */, "tag", groupKey, false /* isSummary */); |
| 781 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 782 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nrPlain.getSbn().getTag(), |
| 783 | nrPlain.getSbn().getId(), nrPlain.getSbn().getNotification(), |
| 784 | nrPlain.getSbn().getUserId()); |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 785 | waitForIdle(); |
| 786 | |
| 787 | notifsAfter = mBinderService.getActiveNotifications(PKG); |
| 788 | assertEquals(2, notifsAfter.length); |
| 789 | |
| 790 | // Summary notification for both of those |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 791 | NotificationRecord nrSummary = generateMessageBubbleNotifRecord(false /* addMetadata */, |
| 792 | mTestNotificationChannel, 3 /* id */, "tag", groupKey, true /* isSummary */); |
| 793 | |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 794 | if (summaryAutoCancel) { |
| 795 | nrSummary.getNotification().flags |= FLAG_AUTO_CANCEL; |
| 796 | } |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 797 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nrSummary.getSbn().getTag(), |
| 798 | nrSummary.getSbn().getId(), nrSummary.getSbn().getNotification(), |
| 799 | nrSummary.getSbn().getUserId()); |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 800 | waitForIdle(); |
| 801 | |
| 802 | notifsAfter = mBinderService.getActiveNotifications(PKG); |
| 803 | assertEquals(3, notifsAfter.length); |
| 804 | |
| 805 | return nrSummary; |
| 806 | } |
| 807 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 808 | @Test |
Jay Aliomer | fea8025 | 2019-11-20 18:14:24 -0500 | [diff] [blame] | 809 | public void testDefaultAssistant_overrideDefault() { |
| 810 | final int userId = 0; |
| 811 | final String testComponent = "package/class"; |
| 812 | final List<UserInfo> userInfos = new ArrayList<>(); |
| 813 | userInfos.add(new UserInfo(0, "", 0)); |
| 814 | final ArraySet<ComponentName> validAssistants = new ArraySet<>(); |
| 815 | validAssistants.add(ComponentName.unflattenFromString(testComponent)); |
Jay Aliomer | fea8025 | 2019-11-20 18:14:24 -0500 | [diff] [blame] | 816 | when(mActivityManager.isLowRamDevice()).thenReturn(false); |
| 817 | when(mAssistants.queryPackageForServices(isNull(), anyInt(), anyInt())) |
| 818 | .thenReturn(validAssistants); |
Jay Aliomer | 76e1f272 | 2020-03-05 12:36:38 -0500 | [diff] [blame] | 819 | when(mAssistants.getDefaultComponents()).thenReturn(validAssistants); |
Jay Aliomer | fea8025 | 2019-11-20 18:14:24 -0500 | [diff] [blame] | 820 | when(mUm.getEnabledProfiles(anyInt())).thenReturn(userInfos); |
| 821 | |
| 822 | mService.setDefaultAssistantForUser(userId); |
| 823 | |
| 824 | verify(mAssistants).setPackageOrComponentEnabled( |
| 825 | eq(testComponent), eq(userId), eq(true), eq(true)); |
Jay Aliomer | fea8025 | 2019-11-20 18:14:24 -0500 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | @Test |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 829 | public void testCreateNotificationChannels_SingleChannel() throws Exception { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 830 | final NotificationChannel channel = |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 831 | new NotificationChannel("id", "name", IMPORTANCE_DEFAULT); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 832 | mBinderService.createNotificationChannels(PKG, |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 833 | new ParceledListSlice(Arrays.asList(channel))); |
| 834 | final NotificationChannel createdChannel = |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 835 | mBinderService.getNotificationChannel(PKG, mContext.getUserId(), PKG, "id"); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 836 | assertTrue(createdChannel != null); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 837 | } |
| 838 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 839 | @Test |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 840 | public void testCreateNotificationChannels_NullChannelThrowsException() throws Exception { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 841 | try { |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 842 | mBinderService.createNotificationChannels(PKG, |
Kristian Monsen | 05f3479 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 843 | new ParceledListSlice(Arrays.asList((Object[])null))); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 844 | fail("Exception should be thrown immediately."); |
| 845 | } catch (NullPointerException e) { |
| 846 | // pass |
| 847 | } |
| 848 | } |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 849 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 850 | @Test |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 851 | public void testCreateNotificationChannels_TwoChannels() throws Exception { |
| 852 | final NotificationChannel channel1 = |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 853 | new NotificationChannel("id1", "name", IMPORTANCE_DEFAULT); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 854 | final NotificationChannel channel2 = |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 855 | new NotificationChannel("id2", "name", IMPORTANCE_DEFAULT); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 856 | mBinderService.createNotificationChannels(PKG, |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 857 | new ParceledListSlice(Arrays.asList(channel1, channel2))); |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 858 | assertTrue(mBinderService.getNotificationChannel( |
| 859 | PKG, mContext.getUserId(), PKG, "id1") != null); |
| 860 | assertTrue(mBinderService.getNotificationChannel( |
| 861 | PKG, mContext.getUserId(), PKG, "id2") != null); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 862 | } |
| 863 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 864 | @Test |
Geoffrey Pitsch | 76a3aa0 | 2017-07-26 15:07:34 -0400 | [diff] [blame] | 865 | public void testCreateNotificationChannels_SecondCreateDoesNotChangeImportance() |
| 866 | throws Exception { |
| 867 | final NotificationChannel channel = |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 868 | new NotificationChannel("id", "name", IMPORTANCE_DEFAULT); |
Geoffrey Pitsch | 76a3aa0 | 2017-07-26 15:07:34 -0400 | [diff] [blame] | 869 | mBinderService.createNotificationChannels(PKG, |
| 870 | new ParceledListSlice(Arrays.asList(channel))); |
| 871 | |
| 872 | // Recreating the channel doesn't throw, but ignores importance. |
| 873 | final NotificationChannel dupeChannel = |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 874 | new NotificationChannel("id", "name", IMPORTANCE_HIGH); |
Geoffrey Pitsch | 76a3aa0 | 2017-07-26 15:07:34 -0400 | [diff] [blame] | 875 | mBinderService.createNotificationChannels(PKG, |
| 876 | new ParceledListSlice(Arrays.asList(dupeChannel))); |
| 877 | final NotificationChannel createdChannel = |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 878 | mBinderService.getNotificationChannel(PKG, mContext.getUserId(), PKG, "id"); |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 879 | assertEquals(IMPORTANCE_DEFAULT, createdChannel.getImportance()); |
Geoffrey Pitsch | 76a3aa0 | 2017-07-26 15:07:34 -0400 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | @Test |
| 883 | public void testCreateNotificationChannels_SecondCreateAllowedToDowngradeImportance() |
| 884 | throws Exception { |
| 885 | final NotificationChannel channel = |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 886 | new NotificationChannel("id", "name", IMPORTANCE_DEFAULT); |
Geoffrey Pitsch | 76a3aa0 | 2017-07-26 15:07:34 -0400 | [diff] [blame] | 887 | mBinderService.createNotificationChannels(PKG, |
| 888 | new ParceledListSlice(Arrays.asList(channel))); |
| 889 | |
| 890 | // Recreating with a lower importance is allowed to modify the channel. |
| 891 | final NotificationChannel dupeChannel = |
| 892 | new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_LOW); |
| 893 | mBinderService.createNotificationChannels(PKG, |
| 894 | new ParceledListSlice(Arrays.asList(dupeChannel))); |
| 895 | final NotificationChannel createdChannel = |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 896 | mBinderService.getNotificationChannel(PKG, mContext.getUserId(), PKG, "id"); |
Geoffrey Pitsch | 76a3aa0 | 2017-07-26 15:07:34 -0400 | [diff] [blame] | 897 | assertEquals(NotificationManager.IMPORTANCE_LOW, createdChannel.getImportance()); |
| 898 | } |
| 899 | |
| 900 | @Test |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 901 | public void testCreateNotificationChannels_CannotDowngradeImportanceIfAlreadyUpdated() |
| 902 | throws Exception { |
| 903 | final NotificationChannel channel = |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 904 | new NotificationChannel("id", "name", IMPORTANCE_DEFAULT); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 905 | mBinderService.createNotificationChannels(PKG, |
| 906 | new ParceledListSlice(Arrays.asList(channel))); |
| 907 | |
| 908 | // The user modifies importance directly, can no longer be changed by the app. |
| 909 | final NotificationChannel updatedChannel = |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 910 | new NotificationChannel("id", "name", IMPORTANCE_HIGH); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 911 | mBinderService.updateNotificationChannelForPackage(PKG, mUid, updatedChannel); |
| 912 | |
| 913 | // Recreating with a lower importance leaves channel unchanged. |
| 914 | final NotificationChannel dupeChannel = |
| 915 | new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_LOW); |
| 916 | mBinderService.createNotificationChannels(PKG, |
| 917 | new ParceledListSlice(Arrays.asList(dupeChannel))); |
| 918 | final NotificationChannel createdChannel = |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 919 | mBinderService.getNotificationChannel(PKG, mContext.getUserId(), PKG, "id"); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 920 | assertEquals(IMPORTANCE_HIGH, createdChannel.getImportance()); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | @Test |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 924 | public void testCreateNotificationChannels_IdenticalChannelsInListIgnoresSecond() |
| 925 | throws Exception { |
| 926 | final NotificationChannel channel1 = |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 927 | new NotificationChannel("id", "name", IMPORTANCE_DEFAULT); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 928 | final NotificationChannel channel2 = |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 929 | new NotificationChannel("id", "name", IMPORTANCE_HIGH); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 930 | mBinderService.createNotificationChannels(PKG, |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 931 | new ParceledListSlice(Arrays.asList(channel1, channel2))); |
| 932 | final NotificationChannel createdChannel = |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 933 | mBinderService.getNotificationChannel(PKG, mContext.getUserId(), PKG, "id"); |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 934 | assertEquals(IMPORTANCE_DEFAULT, createdChannel.getImportance()); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 935 | } |
| 936 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 937 | @Test |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 938 | public void testBlockedNotifications_suspended() throws Exception { |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 939 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(true); |
| 940 | |
| 941 | NotificationChannel channel = new NotificationChannel("id", "name", |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 942 | IMPORTANCE_HIGH); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 943 | NotificationRecord r = generateNotificationRecord(channel); |
Beverly | 3c707b4 | 2018-09-14 09:49:07 -0400 | [diff] [blame] | 944 | |
| 945 | // isBlocked is only used for user blocking, not app suspension |
| 946 | assertFalse(mService.isBlocked(r, mUsageStats)); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 947 | } |
| 948 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 949 | @Test |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 950 | public void testBlockedNotifications_blockedChannel() throws Exception { |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 951 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false); |
| 952 | |
| 953 | NotificationChannel channel = new NotificationChannel("id", "name", |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 954 | NotificationManager.IMPORTANCE_NONE); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 955 | NotificationRecord r = generateNotificationRecord(channel); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 956 | assertTrue(mService.isBlocked(r, mUsageStats)); |
Geoffrey Pitsch | d5bcf21 | 2017-06-01 15:45:35 -0400 | [diff] [blame] | 957 | verify(mUsageStats, times(1)).registerBlocked(eq(r)); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 958 | |
| 959 | mBinderService.createNotificationChannels( |
| 960 | PKG, new ParceledListSlice(Arrays.asList(channel))); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 961 | final StatusBarNotification sbn = generateNotificationRecord(channel).getSbn(); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 962 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 963 | "testBlockedNotifications_blockedChannel", |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 964 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 965 | waitForIdle(); |
| 966 | assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length); |
| 967 | } |
| 968 | |
| 969 | @Test |
| 970 | public void testEnqueuedBlockedNotifications_appBlockedChannelForegroundService() |
| 971 | throws Exception { |
| 972 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false); |
| 973 | |
| 974 | NotificationChannel channel = new NotificationChannel("blocked", "name", |
| 975 | NotificationManager.IMPORTANCE_NONE); |
| 976 | mBinderService.createNotificationChannels( |
| 977 | PKG, new ParceledListSlice(Arrays.asList(channel))); |
| 978 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 979 | final StatusBarNotification sbn = generateNotificationRecord(channel).getSbn(); |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 980 | sbn.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 981 | mBinderService.enqueueNotificationWithTag(PKG, PKG, sbn.getTag(), |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 982 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 983 | waitForIdle(); |
| 984 | assertEquals(1, mBinderService.getActiveNotifications(sbn.getPackageName()).length); |
| 985 | assertEquals(IMPORTANCE_LOW, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 986 | mService.getNotificationRecord(sbn.getKey()).getImportance()); |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 987 | assertEquals(IMPORTANCE_LOW, mBinderService.getNotificationChannel( |
| 988 | PKG, mContext.getUserId(), PKG, channel.getId()).getImportance()); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | @Test |
| 992 | public void testEnqueuedBlockedNotifications_userBlockedChannelForegroundService() |
| 993 | throws Exception { |
| 994 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false); |
| 995 | |
| 996 | NotificationChannel channel = |
| 997 | new NotificationChannel("blockedbyuser", "name", IMPORTANCE_HIGH); |
| 998 | mBinderService.createNotificationChannels( |
| 999 | PKG, new ParceledListSlice(Arrays.asList(channel))); |
| 1000 | |
| 1001 | NotificationChannel update = |
| 1002 | new NotificationChannel("blockedbyuser", "name", IMPORTANCE_NONE); |
| 1003 | mBinderService.updateNotificationChannelForPackage(PKG, mUid, update); |
| 1004 | waitForIdle(); |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 1005 | assertEquals(IMPORTANCE_NONE, mBinderService.getNotificationChannel( |
| 1006 | PKG, mContext.getUserId(), PKG, channel.getId()).getImportance()); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 1007 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1008 | StatusBarNotification sbn = generateNotificationRecord(channel).getSbn(); |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 1009 | sbn.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1010 | mBinderService.enqueueNotificationWithTag(PKG, PKG, sbn.getTag(), |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 1011 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 1012 | waitForIdle(); |
Dianne Hackborn | 025d4a5 | 2018-04-30 16:23:26 -0700 | [diff] [blame] | 1013 | // The first time a foreground service notification is shown, we allow the channel |
| 1014 | // to be updated to allow it to be seen. |
| 1015 | assertEquals(1, mBinderService.getActiveNotifications(sbn.getPackageName()).length); |
| 1016 | assertEquals(IMPORTANCE_LOW, |
| 1017 | mService.getNotificationRecord(sbn.getKey()).getImportance()); |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 1018 | assertEquals(IMPORTANCE_LOW, mBinderService.getNotificationChannel( |
| 1019 | PKG, mContext.getUserId(), PKG, channel.getId()).getImportance()); |
Julia Reynolds | e4a47dd | 2019-06-07 13:40:59 -0400 | [diff] [blame] | 1020 | mBinderService.cancelNotificationWithTag(PKG, PKG, "tag", sbn.getId(), sbn.getUserId()); |
Dianne Hackborn | 025d4a5 | 2018-04-30 16:23:26 -0700 | [diff] [blame] | 1021 | waitForIdle(); |
| 1022 | |
| 1023 | update = new NotificationChannel("blockedbyuser", "name", IMPORTANCE_NONE); |
| 1024 | update.setFgServiceShown(true); |
| 1025 | mBinderService.updateNotificationChannelForPackage(PKG, mUid, update); |
| 1026 | waitForIdle(); |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 1027 | assertEquals(IMPORTANCE_NONE, mBinderService.getNotificationChannel( |
| 1028 | PKG, mContext.getUserId(), PKG, channel.getId()).getImportance()); |
Dianne Hackborn | 025d4a5 | 2018-04-30 16:23:26 -0700 | [diff] [blame] | 1029 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1030 | sbn = generateNotificationRecord(channel).getSbn(); |
Dianne Hackborn | 025d4a5 | 2018-04-30 16:23:26 -0700 | [diff] [blame] | 1031 | sbn.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1032 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1033 | "testEnqueuedBlockedNotifications_userBlockedChannelForegroundService", |
Dianne Hackborn | 025d4a5 | 2018-04-30 16:23:26 -0700 | [diff] [blame] | 1034 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 1035 | waitForIdle(); |
| 1036 | // The second time it is shown, we keep the user's preference. |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 1037 | assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1038 | assertNull(mService.getNotificationRecord(sbn.getKey())); |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 1039 | assertEquals(IMPORTANCE_NONE, mBinderService.getNotificationChannel( |
| 1040 | PKG, mContext.getUserId(), PKG, channel.getId()).getImportance()); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 1041 | } |
| 1042 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 1043 | @Test |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 1044 | public void testBlockedNotifications_blockedChannelGroup() throws Exception { |
| 1045 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1046 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1047 | when(mPreferencesHelper.isGroupBlocked(anyString(), anyInt(), anyString())). |
| 1048 | thenReturn(true); |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 1049 | |
| 1050 | NotificationChannel channel = new NotificationChannel("id", "name", |
| 1051 | NotificationManager.IMPORTANCE_HIGH); |
| 1052 | channel.setGroup("something"); |
| 1053 | NotificationRecord r = generateNotificationRecord(channel); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1054 | assertTrue(mService.isBlocked(r, mUsageStats)); |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 1055 | verify(mUsageStats, times(1)).registerBlocked(eq(r)); |
| 1056 | } |
| 1057 | |
| 1058 | @Test |
Julia Reynolds | 4da7970 | 2017-06-01 11:06:10 -0400 | [diff] [blame] | 1059 | public void testEnqueuedBlockedNotifications_blockedApp() throws Exception { |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 1060 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false); |
| 1061 | |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 1062 | mBinderService.setNotificationsEnabledForPackage(PKG, mUid, false); |
Julia Reynolds | 4da7970 | 2017-06-01 11:06:10 -0400 | [diff] [blame] | 1063 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1064 | final StatusBarNotification sbn = generateNotificationRecord(null).getSbn(); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1065 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1066 | "testEnqueuedBlockedNotifications_blockedApp", |
Julia Reynolds | 4da7970 | 2017-06-01 11:06:10 -0400 | [diff] [blame] | 1067 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 1068 | waitForIdle(); |
| 1069 | assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 1070 | } |
| 1071 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 1072 | @Test |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 1073 | public void testEnqueuedBlockedNotifications_blockedAppForegroundService() throws Exception { |
| 1074 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false); |
| 1075 | |
| 1076 | mBinderService.setNotificationsEnabledForPackage(PKG, mUid, false); |
| 1077 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1078 | final StatusBarNotification sbn = generateNotificationRecord(null).getSbn(); |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 1079 | sbn.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1080 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1081 | "testEnqueuedBlockedNotifications_blockedAppForegroundService", |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 1082 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 1083 | waitForIdle(); |
| 1084 | assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1085 | assertNull(mService.getNotificationRecord(sbn.getKey())); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 1086 | } |
| 1087 | |
Brad Stenning | 8c991ea | 2018-07-31 13:33:01 -0700 | [diff] [blame] | 1088 | /** |
| 1089 | * Confirm the system user on automotive devices can use car categories |
| 1090 | */ |
| 1091 | @Test |
| 1092 | public void testEnqueuedRestrictedNotifications_asSystem() throws Exception { |
| 1093 | when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE, 0)) |
| 1094 | .thenReturn(true); |
| 1095 | List<String> categories = Arrays.asList(Notification.CATEGORY_CAR_EMERGENCY, |
| 1096 | Notification.CATEGORY_CAR_WARNING, |
| 1097 | Notification.CATEGORY_CAR_INFORMATION); |
| 1098 | int id = 0; |
| 1099 | for (String category: categories) { |
| 1100 | final StatusBarNotification sbn = |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1101 | generateNotificationRecord(mTestNotificationChannel, ++id, "", false).getSbn(); |
Brad Stenning | 8c991ea | 2018-07-31 13:33:01 -0700 | [diff] [blame] | 1102 | sbn.getNotification().category = category; |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1103 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1104 | "testEnqueuedRestrictedNotifications_asSystem", |
Brad Stenning | 8c991ea | 2018-07-31 13:33:01 -0700 | [diff] [blame] | 1105 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 1106 | } |
| 1107 | waitForIdle(); |
| 1108 | assertEquals(categories.size(), mBinderService.getActiveNotifications(PKG).length); |
| 1109 | } |
| 1110 | |
| 1111 | |
| 1112 | /** |
| 1113 | * Confirm restricted notification categories only apply to automotive. |
| 1114 | */ |
| 1115 | @Test |
| 1116 | public void testEnqueuedRestrictedNotifications_notAutomotive() throws Exception { |
| 1117 | mService.isSystemUid = false; |
| 1118 | when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE, 0)) |
| 1119 | .thenReturn(false); |
| 1120 | List<String> categories = Arrays.asList(Notification.CATEGORY_CAR_EMERGENCY, |
| 1121 | Notification.CATEGORY_CAR_WARNING, |
| 1122 | Notification.CATEGORY_CAR_INFORMATION); |
| 1123 | int id = 0; |
| 1124 | for (String category: categories) { |
| 1125 | final StatusBarNotification sbn = |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1126 | generateNotificationRecord(mTestNotificationChannel, ++id, "", false).getSbn(); |
Brad Stenning | 8c991ea | 2018-07-31 13:33:01 -0700 | [diff] [blame] | 1127 | sbn.getNotification().category = category; |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1128 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1129 | "testEnqueuedRestrictedNotifications_notAutomotive", |
Brad Stenning | 8c991ea | 2018-07-31 13:33:01 -0700 | [diff] [blame] | 1130 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 1131 | } |
| 1132 | waitForIdle(); |
| 1133 | assertEquals(categories.size(), mBinderService.getActiveNotifications(PKG).length); |
| 1134 | } |
| 1135 | |
| 1136 | /** |
| 1137 | * Confirm if a non-system user tries to use the car categories on a automotive device that |
| 1138 | * they will get a security exception |
| 1139 | */ |
| 1140 | @Test |
| 1141 | public void testEnqueuedRestrictedNotifications_badUser() throws Exception { |
| 1142 | mService.isSystemUid = false; |
| 1143 | when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE, 0)) |
| 1144 | .thenReturn(true); |
| 1145 | List<String> categories = Arrays.asList(Notification.CATEGORY_CAR_EMERGENCY, |
| 1146 | Notification.CATEGORY_CAR_WARNING, |
| 1147 | Notification.CATEGORY_CAR_INFORMATION); |
| 1148 | for (String category: categories) { |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1149 | final StatusBarNotification sbn = generateNotificationRecord(null).getSbn(); |
Brad Stenning | 8c991ea | 2018-07-31 13:33:01 -0700 | [diff] [blame] | 1150 | sbn.getNotification().category = category; |
| 1151 | try { |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1152 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1153 | "testEnqueuedRestrictedNotifications_badUser", |
Brad Stenning | 8c991ea | 2018-07-31 13:33:01 -0700 | [diff] [blame] | 1154 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 1155 | fail("Calls from non system apps should not allow use of restricted categories"); |
| 1156 | } catch (SecurityException e) { |
| 1157 | // pass |
| 1158 | } |
| 1159 | } |
| 1160 | waitForIdle(); |
| 1161 | assertEquals(0, mBinderService.getActiveNotifications(PKG).length); |
| 1162 | } |
| 1163 | |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 1164 | @Test |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 1165 | public void testBlockedNotifications_blockedByAssistant() throws Exception { |
| 1166 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false); |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 1167 | when(mAssistants.isSameUser(any(), anyInt())).thenReturn(true); |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 1168 | |
| 1169 | NotificationChannel channel = new NotificationChannel("id", "name", |
| 1170 | NotificationManager.IMPORTANCE_HIGH); |
| 1171 | NotificationRecord r = generateNotificationRecord(channel); |
| 1172 | mService.addEnqueuedNotification(r); |
| 1173 | |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 1174 | Bundle bundle = new Bundle(); |
Julia Reynolds | c7dcdc2 | 2019-03-25 10:26:14 -0400 | [diff] [blame] | 1175 | bundle.putInt(KEY_IMPORTANCE, IMPORTANCE_NONE); |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 1176 | Adjustment adjustment = new Adjustment( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1177 | r.getSbn().getPackageName(), r.getKey(), bundle, "", r.getUser().getIdentifier()); |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 1178 | mBinderService.applyEnqueuedAdjustmentFromAssistant(null, adjustment); |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 1179 | |
| 1180 | NotificationManagerService.PostNotificationRunnable runnable = |
| 1181 | mService.new PostNotificationRunnable(r.getKey()); |
| 1182 | runnable.run(); |
| 1183 | waitForIdle(); |
| 1184 | |
| 1185 | verify(mUsageStats, never()).registerPostedByApp(any()); |
| 1186 | } |
| 1187 | |
| 1188 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1189 | public void testEnqueueNotificationWithTag_PopulatesGetActiveNotifications() throws Exception { |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1190 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1191 | "testEnqueueNotificationWithTag_PopulatesGetActiveNotifications", 0, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1192 | generateNotificationRecord(null).getNotification(), 0); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1193 | waitForIdle(); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1194 | StatusBarNotification[] notifs = mBinderService.getActiveNotifications(PKG); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1195 | assertEquals(1, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1196 | assertEquals(1, mService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1197 | } |
| 1198 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 1199 | @Test |
Will Brockman | d3d4933 | 2020-02-10 19:43:03 -0500 | [diff] [blame] | 1200 | public void testEnqueueNotificationWithTag_WritesExpectedLogs() throws Exception { |
Will Brockman | 2b6959e | 2020-01-22 09:59:50 -0500 | [diff] [blame] | 1201 | final String tag = "testEnqueueNotificationWithTag_WritesExpectedLog"; |
| 1202 | mBinderService.enqueueNotificationWithTag(PKG, PKG, tag, 0, |
| 1203 | generateNotificationRecord(null).getNotification(), 0); |
| 1204 | waitForIdle(); |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 1205 | assertEquals(1, mNotificationRecordLogger.numCalls()); |
Will Brockman | d3d4933 | 2020-02-10 19:43:03 -0500 | [diff] [blame] | 1206 | |
Will Brockman | 2b6959e | 2020-01-22 09:59:50 -0500 | [diff] [blame] | 1207 | NotificationRecordLoggerFake.CallRecord call = mNotificationRecordLogger.get(0); |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 1208 | assertTrue(call.wasLogged); |
Will Brockman | 75c6057 | 2020-01-31 10:30:27 -0500 | [diff] [blame] | 1209 | assertEquals(NotificationRecordLogger.NotificationReportedEvent.NOTIFICATION_POSTED, |
| 1210 | call.event); |
Will Brockman | 2b6959e | 2020-01-22 09:59:50 -0500 | [diff] [blame] | 1211 | assertNotNull(call.r); |
| 1212 | assertNull(call.old); |
| 1213 | assertEquals(0, call.position); |
| 1214 | assertEquals(0, call.buzzBeepBlink); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1215 | assertEquals(PKG, call.r.getSbn().getPackageName()); |
| 1216 | assertEquals(0, call.r.getSbn().getId()); |
| 1217 | assertEquals(tag, call.r.getSbn().getTag()); |
Will Brockman | 4f0f9d2 | 2020-02-23 21:18:11 -0500 | [diff] [blame] | 1218 | assertEquals(1, call.getInstanceId()); // Fake instance IDs are assigned in order |
Will Brockman | 2b6959e | 2020-01-22 09:59:50 -0500 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | @Test |
| 1222 | public void testEnqueueNotificationWithTag_LogsOnMajorUpdates() throws Exception { |
| 1223 | final String tag = "testEnqueueNotificationWithTag_LogsOnMajorUpdates"; |
| 1224 | Notification original = new Notification.Builder(mContext, |
| 1225 | mTestNotificationChannel.getId()) |
| 1226 | .setSmallIcon(android.R.drawable.sym_def_app_icon).build(); |
| 1227 | mBinderService.enqueueNotificationWithTag(PKG, PKG, tag, 0, original, 0); |
| 1228 | Notification update = new Notification.Builder(mContext, |
| 1229 | mTestNotificationChannel.getId()) |
| 1230 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 1231 | .setCategory(Notification.CATEGORY_ALARM).build(); |
| 1232 | mBinderService.enqueueNotificationWithTag(PKG, PKG, tag, 0, update, 0); |
| 1233 | waitForIdle(); |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 1234 | assertEquals(2, mNotificationRecordLogger.numCalls()); |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 1235 | |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 1236 | assertTrue(mNotificationRecordLogger.get(0).wasLogged); |
Will Brockman | 2b6959e | 2020-01-22 09:59:50 -0500 | [diff] [blame] | 1237 | assertEquals( |
Will Brockman | 75c6057 | 2020-01-31 10:30:27 -0500 | [diff] [blame] | 1238 | NotificationRecordLogger.NotificationReportedEvent.NOTIFICATION_POSTED, |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 1239 | mNotificationRecordLogger.event(0)); |
Will Brockman | 4f0f9d2 | 2020-02-23 21:18:11 -0500 | [diff] [blame] | 1240 | assertEquals(1, mNotificationRecordLogger.get(0).getInstanceId()); |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 1241 | |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 1242 | assertTrue(mNotificationRecordLogger.get(1).wasLogged); |
Will Brockman | 2b6959e | 2020-01-22 09:59:50 -0500 | [diff] [blame] | 1243 | assertEquals( |
Will Brockman | 75c6057 | 2020-01-31 10:30:27 -0500 | [diff] [blame] | 1244 | NotificationRecordLogger.NotificationReportedEvent.NOTIFICATION_UPDATED, |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 1245 | mNotificationRecordLogger.event(1)); |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 1246 | // Instance ID doesn't change on update of an active notification |
Will Brockman | 4f0f9d2 | 2020-02-23 21:18:11 -0500 | [diff] [blame] | 1247 | assertEquals(1, mNotificationRecordLogger.get(1).getInstanceId()); |
Will Brockman | 2b6959e | 2020-01-22 09:59:50 -0500 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | @Test |
Will Brockman | d3d4933 | 2020-02-10 19:43:03 -0500 | [diff] [blame] | 1251 | public void testEnqueueNotificationWithTag_DoesNotLogOnMinorUpdate() throws Exception { |
| 1252 | final String tag = "testEnqueueNotificationWithTag_DoesNotLogOnMinorUpdate"; |
Will Brockman | 2b6959e | 2020-01-22 09:59:50 -0500 | [diff] [blame] | 1253 | mBinderService.enqueueNotificationWithTag(PKG, PKG, tag, 0, |
| 1254 | generateNotificationRecord(null).getNotification(), 0); |
| 1255 | mBinderService.enqueueNotificationWithTag(PKG, PKG, tag, 0, |
| 1256 | generateNotificationRecord(null).getNotification(), 0); |
| 1257 | waitForIdle(); |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 1258 | assertEquals(2, mNotificationRecordLogger.numCalls()); |
| 1259 | assertTrue(mNotificationRecordLogger.get(0).wasLogged); |
Will Brockman | d3d4933 | 2020-02-10 19:43:03 -0500 | [diff] [blame] | 1260 | assertEquals( |
| 1261 | NotificationRecordLogger.NotificationReportedEvent.NOTIFICATION_POSTED, |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 1262 | mNotificationRecordLogger.event(0)); |
| 1263 | assertFalse(mNotificationRecordLogger.get(1).wasLogged); |
| 1264 | assertNull(mNotificationRecordLogger.event(1)); |
Will Brockman | d3d4933 | 2020-02-10 19:43:03 -0500 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | @Test |
| 1268 | public void testEnqueueNotificationWithTag_DoesNotLogOnTitleUpdate() throws Exception { |
| 1269 | final String tag = "testEnqueueNotificationWithTag_DoesNotLogOnTitleUpdate"; |
| 1270 | mBinderService.enqueueNotificationWithTag(PKG, PKG, tag, 0, |
| 1271 | generateNotificationRecord(null).getNotification(), |
| 1272 | 0); |
| 1273 | final Notification notif = generateNotificationRecord(null).getNotification(); |
| 1274 | notif.extras.putString(Notification.EXTRA_TITLE, "Changed title"); |
| 1275 | mBinderService.enqueueNotificationWithTag(PKG, PKG, tag, 0, notif, 0); |
| 1276 | waitForIdle(); |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 1277 | assertEquals(2, mNotificationRecordLogger.numCalls()); |
Will Brockman | d3d4933 | 2020-02-10 19:43:03 -0500 | [diff] [blame] | 1278 | assertEquals( |
| 1279 | NotificationRecordLogger.NotificationReportedEvent.NOTIFICATION_POSTED, |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 1280 | mNotificationRecordLogger.event(0)); |
| 1281 | assertNull(mNotificationRecordLogger.event(1)); |
Will Brockman | 2b6959e | 2020-01-22 09:59:50 -0500 | [diff] [blame] | 1282 | } |
| 1283 | |
| 1284 | @Test |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 1285 | public void testEnqueueNotificationWithTag_LogsAgainAfterCancel() throws Exception { |
| 1286 | final String tag = "testEnqueueNotificationWithTag_LogsAgainAfterCancel"; |
| 1287 | Notification notification = new Notification.Builder(mContext, |
| 1288 | mTestNotificationChannel.getId()) |
| 1289 | .setSmallIcon(android.R.drawable.sym_def_app_icon).build(); |
| 1290 | mBinderService.enqueueNotificationWithTag(PKG, PKG, tag, 0, notification, 0); |
| 1291 | waitForIdle(); |
| 1292 | mBinderService.cancelNotificationWithTag(PKG, PKG, tag, 0, 0); |
| 1293 | waitForIdle(); |
| 1294 | mBinderService.enqueueNotificationWithTag(PKG, PKG, tag, 0, notification, 0); |
| 1295 | waitForIdle(); |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 1296 | assertEquals(3, mNotificationRecordLogger.numCalls()); |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 1297 | |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 1298 | assertEquals( |
Will Brockman | 75c6057 | 2020-01-31 10:30:27 -0500 | [diff] [blame] | 1299 | NotificationRecordLogger.NotificationReportedEvent.NOTIFICATION_POSTED, |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 1300 | mNotificationRecordLogger.event(0)); |
| 1301 | assertTrue(mNotificationRecordLogger.get(0).wasLogged); |
Will Brockman | 4f0f9d2 | 2020-02-23 21:18:11 -0500 | [diff] [blame] | 1302 | assertEquals(1, mNotificationRecordLogger.get(0).getInstanceId()); |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 1303 | |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 1304 | assertEquals( |
Will Brockman | 75c6057 | 2020-01-31 10:30:27 -0500 | [diff] [blame] | 1305 | NotificationRecordLogger.NotificationCancelledEvent.NOTIFICATION_CANCEL_APP_CANCEL, |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 1306 | mNotificationRecordLogger.event(1)); |
Will Brockman | 4f0f9d2 | 2020-02-23 21:18:11 -0500 | [diff] [blame] | 1307 | assertEquals(1, mNotificationRecordLogger.get(1).getInstanceId()); |
Will Brockman | 75c6057 | 2020-01-31 10:30:27 -0500 | [diff] [blame] | 1308 | |
| 1309 | assertEquals( |
| 1310 | NotificationRecordLogger.NotificationReportedEvent.NOTIFICATION_POSTED, |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 1311 | mNotificationRecordLogger.event(2)); |
| 1312 | assertTrue(mNotificationRecordLogger.get(2).wasLogged); |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 1313 | // New instance ID because notification was canceled before re-post |
Will Brockman | 4f0f9d2 | 2020-02-23 21:18:11 -0500 | [diff] [blame] | 1314 | assertEquals(2, mNotificationRecordLogger.get(2).getInstanceId()); |
Will Brockman | 75c6057 | 2020-01-31 10:30:27 -0500 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | @Test |
| 1318 | public void testCancelNonexistentNotification() throws Exception { |
| 1319 | mBinderService.cancelNotificationWithTag(PKG, PKG, |
| 1320 | "testCancelNonexistentNotification", 0, 0); |
| 1321 | waitForIdle(); |
| 1322 | // The notification record logger doesn't even get called when a nonexistent notification |
| 1323 | // is cancelled, because that happens very frequently and is not interesting. |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 1324 | assertEquals(0, mNotificationRecordLogger.numCalls()); |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1328 | public void testCancelNotificationImmediatelyAfterEnqueue() throws Exception { |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1329 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1330 | "testCancelNotificationImmediatelyAfterEnqueue", 0, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1331 | generateNotificationRecord(null).getNotification(), 0); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1332 | mBinderService.cancelNotificationWithTag(PKG, PKG, |
| 1333 | "testCancelNotificationImmediatelyAfterEnqueue", 0, 0); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1334 | waitForIdle(); |
| 1335 | StatusBarNotification[] notifs = |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1336 | mBinderService.getActiveNotifications(PKG); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1337 | assertEquals(0, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1338 | assertEquals(0, mService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1339 | } |
| 1340 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 1341 | @Test |
Evan Laird | d04af51 | 2020-01-09 11:18:09 -0500 | [diff] [blame] | 1342 | public void testPostCancelPostNotifiesListeners() throws Exception { |
| 1343 | // WHEN a notification is posted |
| 1344 | final StatusBarNotification sbn = generateNotificationRecord(null).getSbn(); |
| 1345 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", sbn.getId(), |
| 1346 | sbn.getNotification(), sbn.getUserId()); |
Evan Laird | 131d38b | 2020-06-24 23:40:30 -0400 | [diff] [blame] | 1347 | Thread.sleep(1); // make sure the system clock advances before the next step |
Evan Laird | d04af51 | 2020-01-09 11:18:09 -0500 | [diff] [blame] | 1348 | // THEN it is canceled |
| 1349 | mBinderService.cancelNotificationWithTag(PKG, PKG, "tag", sbn.getId(), sbn.getUserId()); |
Evan Laird | 131d38b | 2020-06-24 23:40:30 -0400 | [diff] [blame] | 1350 | Thread.sleep(1); // here too |
Evan Laird | d04af51 | 2020-01-09 11:18:09 -0500 | [diff] [blame] | 1351 | // THEN it is posted again (before the cancel has a chance to finish) |
| 1352 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", sbn.getId(), |
| 1353 | sbn.getNotification(), sbn.getUserId()); |
| 1354 | // THEN the later enqueue isn't swallowed by the cancel. I.e., ordering is respected |
| 1355 | waitForIdle(); |
| 1356 | |
| 1357 | // The final enqueue made it to the listener instead of being canceled |
| 1358 | StatusBarNotification[] notifs = |
| 1359 | mBinderService.getActiveNotifications(PKG); |
| 1360 | assertEquals(1, notifs.length); |
| 1361 | assertEquals(1, mService.getNotificationRecordCount()); |
| 1362 | } |
| 1363 | |
| 1364 | @Test |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 1365 | public void testCancelNotificationWhilePostedAndEnqueued() throws Exception { |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1366 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1367 | "testCancelNotificationWhilePostedAndEnqueued", 0, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1368 | generateNotificationRecord(null).getNotification(), 0); |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 1369 | waitForIdle(); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1370 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1371 | "testCancelNotificationWhilePostedAndEnqueued", 0, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1372 | generateNotificationRecord(null).getNotification(), 0); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1373 | mBinderService.cancelNotificationWithTag(PKG, PKG, |
| 1374 | "testCancelNotificationWhilePostedAndEnqueued", 0, 0); |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 1375 | waitForIdle(); |
| 1376 | StatusBarNotification[] notifs = |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1377 | mBinderService.getActiveNotifications(PKG); |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 1378 | assertEquals(0, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1379 | assertEquals(0, mService.getNotificationRecordCount()); |
| 1380 | ArgumentCaptor<NotificationStats> captor = ArgumentCaptor.forClass(NotificationStats.class); |
| 1381 | verify(mListeners, times(1)).notifyRemovedLocked(any(), anyInt(), captor.capture()); |
| 1382 | assertEquals(NotificationStats.DISMISSAL_OTHER, captor.getValue().getDismissalSurface()); |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1386 | public void testCancelNotificationsFromListenerImmediatelyAfterEnqueue() throws Exception { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1387 | NotificationRecord r = generateNotificationRecord(null); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1388 | final StatusBarNotification sbn = r.getSbn(); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1389 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1390 | "testCancelNotificationsFromListenerImmediatelyAfterEnqueue", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1391 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1392 | mBinderService.cancelNotificationsFromListener(null, null); |
| 1393 | waitForIdle(); |
| 1394 | StatusBarNotification[] notifs = |
| 1395 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 1396 | assertEquals(0, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1397 | assertEquals(0, mService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1398 | } |
| 1399 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 1400 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1401 | public void testCancelAllNotificationsImmediatelyAfterEnqueue() throws Exception { |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1402 | final StatusBarNotification sbn = generateNotificationRecord(null).getSbn(); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1403 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1404 | "testCancelAllNotificationsImmediatelyAfterEnqueue", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1405 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1406 | mBinderService.cancelAllNotifications(PKG, sbn.getUserId()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1407 | waitForIdle(); |
| 1408 | StatusBarNotification[] notifs = |
| 1409 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 1410 | assertEquals(0, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1411 | assertEquals(0, mService.getNotificationRecordCount()); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1412 | } |
| 1413 | |
| 1414 | @Test |
Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 1415 | public void testCancelImmediatelyAfterEnqueueNotifiesListeners_ForegroundServiceFlag() |
| 1416 | throws Exception { |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1417 | final StatusBarNotification sbn = generateNotificationRecord(null).getSbn(); |
Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 1418 | sbn.getNotification().flags = |
| 1419 | Notification.FLAG_ONGOING_EVENT | FLAG_FOREGROUND_SERVICE; |
| 1420 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
| 1421 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 1422 | mBinderService.cancelNotificationWithTag(PKG, PKG, "tag", sbn.getId(), sbn.getUserId()); |
| 1423 | waitForIdle(); |
| 1424 | verify(mListeners, times(1)).notifyPostedLocked(any(), any()); |
| 1425 | verify(mListeners, times(1)).notifyRemovedLocked(any(), anyInt(), any()); |
| 1426 | } |
| 1427 | |
| 1428 | @Test |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1429 | public void testUserInitiatedClearAll_noLeak() throws Exception { |
| 1430 | final NotificationRecord n = generateNotificationRecord( |
| 1431 | mTestNotificationChannel, 1, "group", true); |
| 1432 | |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1433 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1434 | "testUserInitiatedClearAll_noLeak", |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1435 | n.getSbn().getId(), n.getSbn().getNotification(), n.getSbn().getUserId()); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1436 | waitForIdle(); |
| 1437 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1438 | mService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(), |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1439 | n.getUserId()); |
| 1440 | waitForIdle(); |
| 1441 | StatusBarNotification[] notifs = |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1442 | mBinderService.getActiveNotifications(n.getSbn().getPackageName()); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1443 | assertEquals(0, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1444 | assertEquals(0, mService.getNotificationRecordCount()); |
| 1445 | ArgumentCaptor<NotificationStats> captor = ArgumentCaptor.forClass(NotificationStats.class); |
| 1446 | verify(mListeners, times(1)).notifyRemovedLocked(any(), anyInt(), captor.capture()); |
| 1447 | assertEquals(NotificationStats.DISMISSAL_OTHER, captor.getValue().getDismissalSurface()); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1448 | } |
| 1449 | |
| 1450 | @Test |
| 1451 | public void testCancelAllNotificationsCancelsChildren() throws Exception { |
| 1452 | final NotificationRecord parent = generateNotificationRecord( |
| 1453 | mTestNotificationChannel, 1, "group1", true); |
| 1454 | final NotificationRecord child = generateNotificationRecord( |
| 1455 | mTestNotificationChannel, 2, "group1", false); |
| 1456 | |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1457 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1458 | "testCancelAllNotificationsCancelsChildren", |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1459 | parent.getSbn().getId(), parent.getSbn().getNotification(), |
| 1460 | parent.getSbn().getUserId()); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1461 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1462 | "testCancelAllNotificationsCancelsChildren", |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1463 | child.getSbn().getId(), child.getSbn().getNotification(), |
| 1464 | child.getSbn().getUserId()); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1465 | waitForIdle(); |
| 1466 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1467 | mBinderService.cancelAllNotifications(PKG, parent.getSbn().getUserId()); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1468 | waitForIdle(); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1469 | assertEquals(0, mService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1470 | } |
| 1471 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 1472 | @Test |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 1473 | public void testCancelAllNotificationsMultipleEnqueuedDoesNotCrash() throws Exception { |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1474 | final StatusBarNotification sbn = generateNotificationRecord(null).getSbn(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 1475 | for (int i = 0; i < 10; i++) { |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1476 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1477 | "testCancelAllNotificationsMultipleEnqueuedDoesNotCrash", |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 1478 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 1479 | } |
| 1480 | mBinderService.cancelAllNotifications(PKG, sbn.getUserId()); |
| 1481 | waitForIdle(); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1482 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1483 | assertEquals(0, mService.getNotificationRecordCount()); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 1484 | } |
| 1485 | |
| 1486 | @Test |
| 1487 | public void testCancelGroupSummaryMultipleEnqueuedChildrenDoesNotCrash() throws Exception { |
| 1488 | final NotificationRecord parent = generateNotificationRecord( |
| 1489 | mTestNotificationChannel, 1, "group1", true); |
| 1490 | final NotificationRecord parentAsChild = generateNotificationRecord( |
| 1491 | mTestNotificationChannel, 1, "group1", false); |
| 1492 | final NotificationRecord child = generateNotificationRecord( |
| 1493 | mTestNotificationChannel, 2, "group1", false); |
| 1494 | |
| 1495 | // fully post parent notification |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1496 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1497 | "testCancelGroupSummaryMultipleEnqueuedChildrenDoesNotCrash", |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1498 | parent.getSbn().getId(), parent.getSbn().getNotification(), |
| 1499 | parent.getSbn().getUserId()); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 1500 | waitForIdle(); |
| 1501 | |
| 1502 | // enqueue the child several times |
| 1503 | for (int i = 0; i < 10; i++) { |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1504 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1505 | "testCancelGroupSummaryMultipleEnqueuedChildrenDoesNotCrash", |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1506 | child.getSbn().getId(), child.getSbn().getNotification(), |
| 1507 | child.getSbn().getUserId()); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 1508 | } |
| 1509 | // make the parent a child, which will cancel the child notification |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1510 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1511 | "testCancelGroupSummaryMultipleEnqueuedChildrenDoesNotCrash", |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1512 | parentAsChild.getSbn().getId(), parentAsChild.getSbn().getNotification(), |
| 1513 | parentAsChild.getSbn().getUserId()); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 1514 | waitForIdle(); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1515 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1516 | assertEquals(0, mService.getNotificationRecordCount()); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 1517 | } |
| 1518 | |
| 1519 | @Test |
Jay Aliomer | efe1c92 | 2019-09-19 16:42:16 -0400 | [diff] [blame] | 1520 | public void testAutobundledSummary_notificationAdded() { |
| 1521 | NotificationRecord summary = |
| 1522 | generateNotificationRecord(mTestNotificationChannel, 0, "pkg", true); |
| 1523 | summary.getNotification().flags |= Notification.FLAG_AUTOGROUP_SUMMARY; |
| 1524 | mService.addNotification(summary); |
| 1525 | mService.mSummaryByGroupKey.put("pkg", summary); |
| 1526 | mService.mAutobundledSummaries.put(0, new ArrayMap<>()); |
| 1527 | mService.mAutobundledSummaries.get(0).put("pkg", summary.getKey()); |
Mady Mellor | 6d775f8 | 2019-11-12 16:12:19 -0800 | [diff] [blame] | 1528 | mService.updateAutobundledSummaryFlags(0, "pkg", true, false); |
Jay Aliomer | efe1c92 | 2019-09-19 16:42:16 -0400 | [diff] [blame] | 1529 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1530 | assertTrue(summary.getSbn().isOngoing()); |
Jay Aliomer | efe1c92 | 2019-09-19 16:42:16 -0400 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | @Test |
| 1534 | public void testAutobundledSummary_notificationRemoved() { |
| 1535 | NotificationRecord summary = |
| 1536 | generateNotificationRecord(mTestNotificationChannel, 0, "pkg", true); |
| 1537 | summary.getNotification().flags |= Notification.FLAG_AUTOGROUP_SUMMARY; |
| 1538 | summary.getNotification().flags |= Notification.FLAG_ONGOING_EVENT; |
| 1539 | mService.addNotification(summary); |
| 1540 | mService.mAutobundledSummaries.put(0, new ArrayMap<>()); |
| 1541 | mService.mAutobundledSummaries.get(0).put("pkg", summary.getKey()); |
| 1542 | mService.mSummaryByGroupKey.put("pkg", summary); |
| 1543 | |
Mady Mellor | 6d775f8 | 2019-11-12 16:12:19 -0800 | [diff] [blame] | 1544 | mService.updateAutobundledSummaryFlags(0, "pkg", false, false); |
Jay Aliomer | efe1c92 | 2019-09-19 16:42:16 -0400 | [diff] [blame] | 1545 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1546 | assertFalse(summary.getSbn().isOngoing()); |
Jay Aliomer | efe1c92 | 2019-09-19 16:42:16 -0400 | [diff] [blame] | 1547 | } |
| 1548 | |
| 1549 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1550 | public void testCancelAllNotifications_IgnoreForegroundService() throws Exception { |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1551 | final StatusBarNotification sbn = generateNotificationRecord(null).getSbn(); |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 1552 | sbn.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1553 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1554 | "testCancelAllNotifications_IgnoreForegroundService", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1555 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1556 | mBinderService.cancelAllNotifications(PKG, sbn.getUserId()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1557 | waitForIdle(); |
| 1558 | StatusBarNotification[] notifs = |
| 1559 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 1560 | assertEquals(1, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1561 | assertEquals(1, mService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1562 | } |
| 1563 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 1564 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1565 | public void testCancelAllNotifications_IgnoreOtherPackages() throws Exception { |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1566 | final StatusBarNotification sbn = generateNotificationRecord(null).getSbn(); |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 1567 | sbn.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1568 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1569 | "testCancelAllNotifications_IgnoreOtherPackages", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1570 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1571 | mBinderService.cancelAllNotifications("other_pkg_name", sbn.getUserId()); |
| 1572 | waitForIdle(); |
| 1573 | StatusBarNotification[] notifs = |
| 1574 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 1575 | assertEquals(1, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1576 | assertEquals(1, mService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1577 | } |
| 1578 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 1579 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1580 | public void testCancelAllNotifications_NullPkgRemovesAll() throws Exception { |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1581 | final StatusBarNotification sbn = generateNotificationRecord(null).getSbn(); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1582 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1583 | "testCancelAllNotifications_NullPkgRemovesAll", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1584 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1585 | mBinderService.cancelAllNotifications(null, sbn.getUserId()); |
| 1586 | waitForIdle(); |
| 1587 | StatusBarNotification[] notifs = |
| 1588 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 1589 | assertEquals(0, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1590 | assertEquals(0, mService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1591 | } |
| 1592 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 1593 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1594 | public void testCancelAllNotifications_NullPkgIgnoresUserAllNotifications() throws Exception { |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1595 | final StatusBarNotification sbn = generateNotificationRecord(null).getSbn(); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1596 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1597 | "testCancelAllNotifications_NullPkgIgnoresUserAllNotifications", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1598 | sbn.getId(), sbn.getNotification(), UserHandle.USER_ALL); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1599 | // Null pkg is how we signal a user switch. |
| 1600 | mBinderService.cancelAllNotifications(null, sbn.getUserId()); |
| 1601 | waitForIdle(); |
| 1602 | StatusBarNotification[] notifs = |
| 1603 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 1604 | assertEquals(1, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1605 | assertEquals(1, mService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1606 | } |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1607 | |
| 1608 | @Test |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 1609 | public void testAppInitiatedCancelAllNotifications_CancelsNoClearFlag() throws Exception { |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1610 | final StatusBarNotification sbn = generateNotificationRecord(null).getSbn(); |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 1611 | sbn.getNotification().flags |= Notification.FLAG_NO_CLEAR; |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1612 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1613 | "testAppInitiatedCancelAllNotifications_CancelsNoClearFlag", |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 1614 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 1615 | mBinderService.cancelAllNotifications(PKG, sbn.getUserId()); |
| 1616 | waitForIdle(); |
| 1617 | StatusBarNotification[] notifs = |
| 1618 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 1619 | assertEquals(0, notifs.length); |
| 1620 | } |
| 1621 | |
| 1622 | @Test |
| 1623 | public void testCancelAllNotifications_CancelsNoClearFlag() throws Exception { |
| 1624 | final NotificationRecord notif = generateNotificationRecord( |
| 1625 | mTestNotificationChannel, 1, "group", true); |
| 1626 | notif.getNotification().flags |= Notification.FLAG_NO_CLEAR; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1627 | mService.addNotification(notif); |
| 1628 | mService.cancelAllNotificationsInt(mUid, 0, PKG, null, 0, 0, true, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 1629 | notif.getUserId(), 0, null); |
| 1630 | waitForIdle(); |
| 1631 | StatusBarNotification[] notifs = |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1632 | mBinderService.getActiveNotifications(notif.getSbn().getPackageName()); |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 1633 | assertEquals(0, notifs.length); |
| 1634 | } |
| 1635 | |
| 1636 | @Test |
| 1637 | public void testUserInitiatedCancelAllOnClearAll_NoClearFlag() throws Exception { |
| 1638 | final NotificationRecord notif = generateNotificationRecord( |
| 1639 | mTestNotificationChannel, 1, "group", true); |
| 1640 | notif.getNotification().flags |= Notification.FLAG_NO_CLEAR; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1641 | mService.addNotification(notif); |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 1642 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1643 | mService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(), |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 1644 | notif.getUserId()); |
| 1645 | waitForIdle(); |
| 1646 | StatusBarNotification[] notifs = |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1647 | mBinderService.getActiveNotifications(notif.getSbn().getPackageName()); |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 1648 | assertEquals(1, notifs.length); |
| 1649 | } |
| 1650 | |
| 1651 | @Test |
| 1652 | public void testCancelAllCancelNotificationsFromListener_NoClearFlag() throws Exception { |
| 1653 | final NotificationRecord parent = generateNotificationRecord( |
| 1654 | mTestNotificationChannel, 1, "group", true); |
| 1655 | final NotificationRecord child = generateNotificationRecord( |
| 1656 | mTestNotificationChannel, 2, "group", false); |
| 1657 | final NotificationRecord child2 = generateNotificationRecord( |
| 1658 | mTestNotificationChannel, 3, "group", false); |
| 1659 | child2.getNotification().flags |= Notification.FLAG_NO_CLEAR; |
| 1660 | final NotificationRecord newGroup = generateNotificationRecord( |
| 1661 | mTestNotificationChannel, 4, "group2", false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1662 | mService.addNotification(parent); |
| 1663 | mService.addNotification(child); |
| 1664 | mService.addNotification(child2); |
| 1665 | mService.addNotification(newGroup); |
| 1666 | mService.getBinderService().cancelNotificationsFromListener(null, null); |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 1667 | waitForIdle(); |
| 1668 | StatusBarNotification[] notifs = |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1669 | mBinderService.getActiveNotifications(parent.getSbn().getPackageName()); |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 1670 | assertEquals(1, notifs.length); |
| 1671 | } |
| 1672 | |
| 1673 | @Test |
| 1674 | public void testUserInitiatedCancelAllWithGroup_NoClearFlag() throws Exception { |
| 1675 | final NotificationRecord parent = generateNotificationRecord( |
| 1676 | mTestNotificationChannel, 1, "group", true); |
| 1677 | final NotificationRecord child = generateNotificationRecord( |
| 1678 | mTestNotificationChannel, 2, "group", false); |
| 1679 | final NotificationRecord child2 = generateNotificationRecord( |
| 1680 | mTestNotificationChannel, 3, "group", false); |
| 1681 | child2.getNotification().flags |= Notification.FLAG_NO_CLEAR; |
| 1682 | final NotificationRecord newGroup = generateNotificationRecord( |
| 1683 | mTestNotificationChannel, 4, "group2", false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1684 | mService.addNotification(parent); |
| 1685 | mService.addNotification(child); |
| 1686 | mService.addNotification(child2); |
| 1687 | mService.addNotification(newGroup); |
| 1688 | mService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(), |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 1689 | parent.getUserId()); |
| 1690 | waitForIdle(); |
| 1691 | StatusBarNotification[] notifs = |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1692 | mBinderService.getActiveNotifications(parent.getSbn().getPackageName()); |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 1693 | assertEquals(1, notifs.length); |
| 1694 | } |
| 1695 | |
| 1696 | @Test |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 1697 | public void testRemoveForegroundServiceFlag_ImmediatelyAfterEnqueue() throws Exception { |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1698 | Notification n = |
| 1699 | new Notification.Builder(mContext, mTestNotificationChannel.getId()) |
| 1700 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 1701 | .build(); |
| 1702 | StatusBarNotification sbn = new StatusBarNotification("a", "a", 0, null, mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 1703 | n, UserHandle.getUserHandleForUid(mUid), null, 0); |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 1704 | sbn.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1705 | mBinderService.enqueueNotificationWithTag(PKG, PKG, null, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1706 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 1707 | mInternalService.removeForegroundServiceFlagFromNotification(PKG, sbn.getId(), |
| 1708 | sbn.getUserId()); |
| 1709 | waitForIdle(); |
| 1710 | StatusBarNotification[] notifs = |
| 1711 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 1712 | assertEquals(0, notifs[0].getNotification().flags & FLAG_FOREGROUND_SERVICE); |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 1713 | } |
| 1714 | |
| 1715 | @Test |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 1716 | public void testCancelAfterSecondEnqueueDoesNotSpecifyForegroundFlag() throws Exception { |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1717 | final StatusBarNotification sbn = generateNotificationRecord(null).getSbn(); |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 1718 | sbn.getNotification().flags = |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 1719 | Notification.FLAG_ONGOING_EVENT | FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1720 | mBinderService.enqueueNotificationWithTag(PKG, PKG, sbn.getTag(), |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 1721 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 1722 | sbn.getNotification().flags = Notification.FLAG_ONGOING_EVENT; |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1723 | mBinderService.enqueueNotificationWithTag(PKG, PKG, sbn.getTag(), |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 1724 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1725 | mBinderService.cancelNotificationWithTag(PKG, PKG, sbn.getTag(), sbn.getId(), |
| 1726 | sbn.getUserId()); |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 1727 | waitForIdle(); |
| 1728 | assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1729 | assertEquals(0, mService.getNotificationRecordCount()); |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 1730 | } |
| 1731 | |
| 1732 | @Test |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1733 | public void testCancelAllCancelNotificationsFromListener_ForegroundServiceFlag() |
| 1734 | throws Exception { |
| 1735 | final NotificationRecord parent = generateNotificationRecord( |
| 1736 | mTestNotificationChannel, 1, "group", true); |
| 1737 | final NotificationRecord child = generateNotificationRecord( |
| 1738 | mTestNotificationChannel, 2, "group", false); |
| 1739 | final NotificationRecord child2 = generateNotificationRecord( |
| 1740 | mTestNotificationChannel, 3, "group", false); |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 1741 | child2.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1742 | final NotificationRecord newGroup = generateNotificationRecord( |
| 1743 | mTestNotificationChannel, 4, "group2", false); |
| 1744 | mService.addNotification(parent); |
| 1745 | mService.addNotification(child); |
| 1746 | mService.addNotification(child2); |
| 1747 | mService.addNotification(newGroup); |
| 1748 | mService.getBinderService().cancelNotificationsFromListener(null, null); |
| 1749 | waitForIdle(); |
| 1750 | StatusBarNotification[] notifs = |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1751 | mBinderService.getActiveNotifications(parent.getSbn().getPackageName()); |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1752 | assertEquals(0, notifs.length); |
| 1753 | } |
| 1754 | |
| 1755 | @Test |
| 1756 | public void testCancelAllCancelNotificationsFromListener_ForegroundServiceFlagWithParameter() |
| 1757 | throws Exception { |
| 1758 | final NotificationRecord parent = generateNotificationRecord( |
| 1759 | mTestNotificationChannel, 1, "group", true); |
| 1760 | final NotificationRecord child = generateNotificationRecord( |
| 1761 | mTestNotificationChannel, 2, "group", false); |
| 1762 | final NotificationRecord child2 = generateNotificationRecord( |
| 1763 | mTestNotificationChannel, 3, "group", false); |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 1764 | child2.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1765 | final NotificationRecord newGroup = generateNotificationRecord( |
| 1766 | mTestNotificationChannel, 4, "group2", false); |
| 1767 | mService.addNotification(parent); |
| 1768 | mService.addNotification(child); |
| 1769 | mService.addNotification(child2); |
| 1770 | mService.addNotification(newGroup); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1771 | String[] keys = {parent.getSbn().getKey(), child.getSbn().getKey(), |
| 1772 | child2.getSbn().getKey(), newGroup.getSbn().getKey()}; |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1773 | mService.getBinderService().cancelNotificationsFromListener(null, keys); |
| 1774 | waitForIdle(); |
| 1775 | StatusBarNotification[] notifs = |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1776 | mBinderService.getActiveNotifications(parent.getSbn().getPackageName()); |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1777 | assertEquals(1, notifs.length); |
| 1778 | } |
| 1779 | |
| 1780 | @Test |
| 1781 | public void testUserInitiatedCancelAllWithGroup_ForegroundServiceFlag() throws Exception { |
| 1782 | final NotificationRecord parent = generateNotificationRecord( |
| 1783 | mTestNotificationChannel, 1, "group", true); |
| 1784 | final NotificationRecord child = generateNotificationRecord( |
| 1785 | mTestNotificationChannel, 2, "group", false); |
| 1786 | final NotificationRecord child2 = generateNotificationRecord( |
| 1787 | mTestNotificationChannel, 3, "group", false); |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 1788 | child2.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1789 | final NotificationRecord newGroup = generateNotificationRecord( |
| 1790 | mTestNotificationChannel, 4, "group2", false); |
| 1791 | mService.addNotification(parent); |
| 1792 | mService.addNotification(child); |
| 1793 | mService.addNotification(child2); |
| 1794 | mService.addNotification(newGroup); |
| 1795 | mService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(), |
| 1796 | parent.getUserId()); |
| 1797 | waitForIdle(); |
| 1798 | StatusBarNotification[] notifs = |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1799 | mBinderService.getActiveNotifications(parent.getSbn().getPackageName()); |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1800 | assertEquals(0, notifs.length); |
| 1801 | } |
| 1802 | |
| 1803 | @Test |
Will Brockman | 8da8c8e | 2020-04-30 12:46:15 -0400 | [diff] [blame] | 1804 | public void testGroupInstanceIds() throws Exception { |
| 1805 | final NotificationRecord group1 = generateNotificationRecord( |
| 1806 | mTestNotificationChannel, 1, "group1", true); |
| 1807 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "testFindGroupNotificationsLocked", |
| 1808 | group1.getSbn().getId(), group1.getSbn().getNotification(), |
| 1809 | group1.getSbn().getUserId()); |
| 1810 | waitForIdle(); |
| 1811 | |
| 1812 | // same group, child, should be returned |
| 1813 | final NotificationRecord group1Child = generateNotificationRecord( |
| 1814 | mTestNotificationChannel, 2, "group1", false); |
| 1815 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "testFindGroupNotificationsLocked", |
| 1816 | group1Child.getSbn().getId(), |
| 1817 | group1Child.getSbn().getNotification(), group1Child.getSbn().getUserId()); |
| 1818 | waitForIdle(); |
| 1819 | |
| 1820 | assertEquals(2, mNotificationRecordLogger.numCalls()); |
| 1821 | assertEquals(mNotificationRecordLogger.get(0).getInstanceId(), |
| 1822 | mNotificationRecordLogger.get(1).groupInstanceId.getId()); |
| 1823 | } |
| 1824 | |
| 1825 | @Test |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1826 | public void testFindGroupNotificationsLocked() throws Exception { |
| 1827 | // make sure the same notification can be found in both lists and returned |
| 1828 | final NotificationRecord group1 = generateNotificationRecord( |
| 1829 | mTestNotificationChannel, 1, "group1", true); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1830 | mService.addEnqueuedNotification(group1); |
| 1831 | mService.addNotification(group1); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1832 | |
| 1833 | // should not be returned |
| 1834 | final NotificationRecord group2 = generateNotificationRecord( |
| 1835 | mTestNotificationChannel, 2, "group2", true); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1836 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "testFindGroupNotificationsLocked", |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1837 | group2.getSbn().getId(), group2.getSbn().getNotification(), |
| 1838 | group2.getSbn().getUserId()); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1839 | waitForIdle(); |
| 1840 | |
| 1841 | // should not be returned |
| 1842 | final NotificationRecord nonGroup = generateNotificationRecord( |
| 1843 | mTestNotificationChannel, 3, null, false); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1844 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "testFindGroupNotificationsLocked", |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1845 | nonGroup.getSbn().getId(), nonGroup.getSbn().getNotification(), |
| 1846 | nonGroup.getSbn().getUserId()); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1847 | waitForIdle(); |
| 1848 | |
| 1849 | // same group, child, should be returned |
| 1850 | final NotificationRecord group1Child = generateNotificationRecord( |
| 1851 | mTestNotificationChannel, 4, "group1", false); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1852 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "testFindGroupNotificationsLocked", |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1853 | group1Child.getSbn().getId(), |
| 1854 | group1Child.getSbn().getNotification(), group1Child.getSbn().getUserId()); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1855 | waitForIdle(); |
| 1856 | |
| 1857 | List<NotificationRecord> inGroup1 = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1858 | mService.findGroupNotificationsLocked(PKG, group1.getGroupKey(), |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1859 | group1.getSbn().getUserId()); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1860 | assertEquals(3, inGroup1.size()); |
| 1861 | for (NotificationRecord record : inGroup1) { |
| 1862 | assertTrue(record.getGroupKey().equals(group1.getGroupKey())); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1863 | assertTrue(record.getSbn().getId() == 1 || record.getSbn().getId() == 4); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1864 | } |
| 1865 | } |
| 1866 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1867 | @Test |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1868 | public void testCancelAllNotifications_CancelsNoClearFlagOnGoing() throws Exception { |
| 1869 | final NotificationRecord notif = generateNotificationRecord( |
| 1870 | mTestNotificationChannel, 1, "group", true); |
| 1871 | notif.getNotification().flags |= Notification.FLAG_NO_CLEAR; |
| 1872 | mService.addNotification(notif); |
| 1873 | mService.cancelAllNotificationsInt(mUid, 0, PKG, null, 0, |
| 1874 | Notification.FLAG_ONGOING_EVENT, true, notif.getUserId(), 0, null); |
| 1875 | waitForIdle(); |
| 1876 | StatusBarNotification[] notifs = |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1877 | mBinderService.getActiveNotifications(notif.getSbn().getPackageName()); |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1878 | assertEquals(0, notifs.length); |
| 1879 | } |
| 1880 | |
| 1881 | @Test |
| 1882 | public void testCancelAllCancelNotificationsFromListener_NoClearFlagWithParameter() |
| 1883 | throws Exception { |
| 1884 | final NotificationRecord parent = generateNotificationRecord( |
| 1885 | mTestNotificationChannel, 1, "group", true); |
| 1886 | final NotificationRecord child = generateNotificationRecord( |
| 1887 | mTestNotificationChannel, 2, "group", false); |
| 1888 | final NotificationRecord child2 = generateNotificationRecord( |
| 1889 | mTestNotificationChannel, 3, "group", false); |
| 1890 | child2.getNotification().flags |= Notification.FLAG_NO_CLEAR; |
| 1891 | final NotificationRecord newGroup = generateNotificationRecord( |
| 1892 | mTestNotificationChannel, 4, "group2", false); |
| 1893 | mService.addNotification(parent); |
| 1894 | mService.addNotification(child); |
| 1895 | mService.addNotification(child2); |
| 1896 | mService.addNotification(newGroup); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1897 | String[] keys = {parent.getSbn().getKey(), child.getSbn().getKey(), |
| 1898 | child2.getSbn().getKey(), newGroup.getSbn().getKey()}; |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1899 | mService.getBinderService().cancelNotificationsFromListener(null, keys); |
| 1900 | waitForIdle(); |
| 1901 | StatusBarNotification[] notifs = |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1902 | mBinderService.getActiveNotifications(parent.getSbn().getPackageName()); |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1903 | assertEquals(0, notifs.length); |
| 1904 | } |
| 1905 | |
| 1906 | @Test |
| 1907 | public void testAppInitiatedCancelAllNotifications_CancelsOnGoingFlag() throws Exception { |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1908 | final StatusBarNotification sbn = generateNotificationRecord(null).getSbn(); |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1909 | sbn.getNotification().flags |= Notification.FLAG_ONGOING_EVENT; |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1910 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1911 | "testAppInitiatedCancelAllNotifications_CancelsOnGoingFlag", |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1912 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 1913 | mBinderService.cancelAllNotifications(PKG, sbn.getUserId()); |
| 1914 | waitForIdle(); |
| 1915 | StatusBarNotification[] notifs = |
| 1916 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 1917 | assertEquals(0, notifs.length); |
| 1918 | } |
| 1919 | |
| 1920 | @Test |
| 1921 | public void testCancelAllNotifications_CancelsOnGoingFlag() throws Exception { |
| 1922 | final NotificationRecord notif = generateNotificationRecord( |
| 1923 | mTestNotificationChannel, 1, "group", true); |
| 1924 | notif.getNotification().flags |= Notification.FLAG_ONGOING_EVENT; |
| 1925 | mService.addNotification(notif); |
| 1926 | mService.cancelAllNotificationsInt(mUid, 0, PKG, null, 0, 0, true, |
| 1927 | notif.getUserId(), 0, null); |
| 1928 | waitForIdle(); |
| 1929 | StatusBarNotification[] notifs = |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1930 | mBinderService.getActiveNotifications(notif.getSbn().getPackageName()); |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1931 | assertEquals(0, notifs.length); |
| 1932 | } |
| 1933 | |
| 1934 | @Test |
| 1935 | public void testUserInitiatedCancelAllOnClearAll_OnGoingFlag() throws Exception { |
| 1936 | final NotificationRecord notif = generateNotificationRecord( |
| 1937 | mTestNotificationChannel, 1, "group", true); |
| 1938 | notif.getNotification().flags |= Notification.FLAG_ONGOING_EVENT; |
| 1939 | mService.addNotification(notif); |
| 1940 | |
| 1941 | mService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(), |
| 1942 | notif.getUserId()); |
| 1943 | waitForIdle(); |
| 1944 | StatusBarNotification[] notifs = |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1945 | mBinderService.getActiveNotifications(notif.getSbn().getPackageName()); |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1946 | assertEquals(1, notifs.length); |
| 1947 | } |
| 1948 | |
| 1949 | @Test |
| 1950 | public void testCancelAllCancelNotificationsFromListener_OnGoingFlag() throws Exception { |
| 1951 | final NotificationRecord parent = generateNotificationRecord( |
| 1952 | mTestNotificationChannel, 1, "group", true); |
| 1953 | final NotificationRecord child = generateNotificationRecord( |
| 1954 | mTestNotificationChannel, 2, "group", false); |
| 1955 | final NotificationRecord child2 = generateNotificationRecord( |
| 1956 | mTestNotificationChannel, 3, "group", false); |
| 1957 | child2.getNotification().flags |= Notification.FLAG_ONGOING_EVENT; |
| 1958 | final NotificationRecord newGroup = generateNotificationRecord( |
| 1959 | mTestNotificationChannel, 4, "group2", false); |
| 1960 | mService.addNotification(parent); |
| 1961 | mService.addNotification(child); |
| 1962 | mService.addNotification(child2); |
| 1963 | mService.addNotification(newGroup); |
| 1964 | mService.getBinderService().cancelNotificationsFromListener(null, null); |
| 1965 | waitForIdle(); |
| 1966 | StatusBarNotification[] notifs = |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1967 | mBinderService.getActiveNotifications(parent.getSbn().getPackageName()); |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1968 | assertEquals(1, notifs.length); |
| 1969 | } |
| 1970 | |
| 1971 | @Test |
| 1972 | public void testCancelAllCancelNotificationsFromListener_OnGoingFlagWithParameter() |
| 1973 | throws Exception { |
| 1974 | final NotificationRecord parent = generateNotificationRecord( |
| 1975 | mTestNotificationChannel, 1, "group", true); |
| 1976 | final NotificationRecord child = generateNotificationRecord( |
| 1977 | mTestNotificationChannel, 2, "group", false); |
| 1978 | final NotificationRecord child2 = generateNotificationRecord( |
| 1979 | mTestNotificationChannel, 3, "group", false); |
| 1980 | child2.getNotification().flags |= Notification.FLAG_ONGOING_EVENT; |
| 1981 | final NotificationRecord newGroup = generateNotificationRecord( |
| 1982 | mTestNotificationChannel, 4, "group2", false); |
| 1983 | mService.addNotification(parent); |
| 1984 | mService.addNotification(child); |
| 1985 | mService.addNotification(child2); |
| 1986 | mService.addNotification(newGroup); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1987 | String[] keys = {parent.getSbn().getKey(), child.getSbn().getKey(), |
| 1988 | child2.getSbn().getKey(), newGroup.getSbn().getKey()}; |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1989 | mService.getBinderService().cancelNotificationsFromListener(null, keys); |
| 1990 | waitForIdle(); |
| 1991 | StatusBarNotification[] notifs = |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1992 | mBinderService.getActiveNotifications(parent.getSbn().getPackageName()); |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1993 | assertEquals(0, notifs.length); |
| 1994 | } |
| 1995 | |
| 1996 | @Test |
| 1997 | public void testUserInitiatedCancelAllWithGroup_OnGoingFlag() throws Exception { |
| 1998 | final NotificationRecord parent = generateNotificationRecord( |
| 1999 | mTestNotificationChannel, 1, "group", true); |
| 2000 | final NotificationRecord child = generateNotificationRecord( |
| 2001 | mTestNotificationChannel, 2, "group", false); |
| 2002 | final NotificationRecord child2 = generateNotificationRecord( |
| 2003 | mTestNotificationChannel, 3, "group", false); |
| 2004 | child2.getNotification().flags |= Notification.FLAG_ONGOING_EVENT; |
| 2005 | final NotificationRecord newGroup = generateNotificationRecord( |
| 2006 | mTestNotificationChannel, 4, "group2", false); |
| 2007 | mService.addNotification(parent); |
| 2008 | mService.addNotification(child); |
| 2009 | mService.addNotification(child2); |
| 2010 | mService.addNotification(newGroup); |
| 2011 | mService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(), |
| 2012 | parent.getUserId()); |
| 2013 | waitForIdle(); |
| 2014 | StatusBarNotification[] notifs = |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 2015 | mBinderService.getActiveNotifications(parent.getSbn().getPackageName()); |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 2016 | assertEquals(1, notifs.length); |
| 2017 | } |
| 2018 | |
| 2019 | @Test |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 2020 | public void testTvExtenderChannelOverride_onTv() throws Exception { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2021 | mService.setIsTelevision(true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2022 | mService.setPreferencesHelper(mPreferencesHelper); |
| 2023 | when(mPreferencesHelper.getNotificationChannel( |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 2024 | anyString(), anyInt(), eq("foo"), anyBoolean())).thenReturn( |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 2025 | new NotificationChannel("foo", "foo", IMPORTANCE_HIGH)); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 2026 | |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 2027 | Notification.TvExtender tv = new Notification.TvExtender().setChannelId("foo"); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 2028 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "testTvExtenderChannelOverride_onTv", 0, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 2029 | generateNotificationRecord(null, tv).getNotification(), 0); |
Julia Reynolds | 12ba4cf | 2020-01-10 16:01:38 -0500 | [diff] [blame] | 2030 | verify(mPreferencesHelper, times(1)).getConversationNotificationChannel( |
| 2031 | anyString(), anyInt(), eq("foo"), eq(null), anyBoolean(), anyBoolean()); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 2032 | } |
| 2033 | |
| 2034 | @Test |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 2035 | public void testTvExtenderChannelOverride_notOnTv() throws Exception { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2036 | mService.setIsTelevision(false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2037 | mService.setPreferencesHelper(mPreferencesHelper); |
| 2038 | when(mPreferencesHelper.getNotificationChannel( |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 2039 | anyString(), anyInt(), anyString(), anyBoolean())).thenReturn( |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 2040 | mTestNotificationChannel); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 2041 | |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 2042 | Notification.TvExtender tv = new Notification.TvExtender().setChannelId("foo"); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 2043 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "testTvExtenderChannelOverride_notOnTv", |
| 2044 | 0, generateNotificationRecord(null, tv).getNotification(), 0); |
Julia Reynolds | 12ba4cf | 2020-01-10 16:01:38 -0500 | [diff] [blame] | 2045 | verify(mPreferencesHelper, times(1)).getConversationNotificationChannel( |
Julia Reynolds | 0f76734 | 2019-12-18 09:11:55 -0500 | [diff] [blame] | 2046 | anyString(), anyInt(), eq(mTestNotificationChannel.getId()), eq(null), |
Julia Reynolds | 12ba4cf | 2020-01-10 16:01:38 -0500 | [diff] [blame] | 2047 | anyBoolean(), anyBoolean()); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 2048 | } |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2049 | |
| 2050 | @Test |
Julia Reynolds | fc9767b | 2018-01-22 17:45:16 -0500 | [diff] [blame] | 2051 | public void testUpdateAppNotifyCreatorBlock() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2052 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | fc9767b | 2018-01-22 17:45:16 -0500 | [diff] [blame] | 2053 | |
Julia Reynolds | c4c6e9f | 2019-06-03 12:48:49 -0400 | [diff] [blame] | 2054 | mBinderService.setNotificationsEnabledForPackage(PKG, 0, true); |
Julia Reynolds | fc9767b | 2018-01-22 17:45:16 -0500 | [diff] [blame] | 2055 | ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class); |
| 2056 | verify(mContext, times(1)).sendBroadcastAsUser(captor.capture(), any(), eq(null)); |
| 2057 | |
| 2058 | assertEquals(NotificationManager.ACTION_APP_BLOCK_STATE_CHANGED, |
| 2059 | captor.getValue().getAction()); |
| 2060 | assertEquals(PKG, captor.getValue().getPackage()); |
Julia Reynolds | c4c6e9f | 2019-06-03 12:48:49 -0400 | [diff] [blame] | 2061 | assertFalse(captor.getValue().getBooleanExtra(EXTRA_BLOCKED_STATE, true)); |
| 2062 | } |
| 2063 | |
| 2064 | @Test |
| 2065 | public void testUpdateAppNotifyCreatorBlock_notIfMatchesExistingSetting() throws Exception { |
| 2066 | mService.setPreferencesHelper(mPreferencesHelper); |
| 2067 | |
| 2068 | mBinderService.setNotificationsEnabledForPackage(PKG, 0, false); |
| 2069 | verify(mContext, never()).sendBroadcastAsUser(any(), any(), eq(null)); |
Julia Reynolds | fc9767b | 2018-01-22 17:45:16 -0500 | [diff] [blame] | 2070 | } |
| 2071 | |
| 2072 | @Test |
| 2073 | public void testUpdateAppNotifyCreatorUnblock() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2074 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | fc9767b | 2018-01-22 17:45:16 -0500 | [diff] [blame] | 2075 | |
| 2076 | mBinderService.setNotificationsEnabledForPackage(PKG, 0, true); |
| 2077 | ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class); |
| 2078 | verify(mContext, times(1)).sendBroadcastAsUser(captor.capture(), any(), eq(null)); |
| 2079 | |
| 2080 | assertEquals(NotificationManager.ACTION_APP_BLOCK_STATE_CHANGED, |
| 2081 | captor.getValue().getAction()); |
| 2082 | assertEquals(PKG, captor.getValue().getPackage()); |
| 2083 | assertFalse(captor.getValue().getBooleanExtra(EXTRA_BLOCKED_STATE, true)); |
| 2084 | } |
| 2085 | |
| 2086 | @Test |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2087 | public void testUpdateChannelNotifyCreatorBlock() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2088 | mService.setPreferencesHelper(mPreferencesHelper); |
| 2089 | when(mPreferencesHelper.getNotificationChannel(eq(PKG), anyInt(), |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2090 | eq(mTestNotificationChannel.getId()), anyBoolean())) |
| 2091 | .thenReturn(mTestNotificationChannel); |
| 2092 | |
| 2093 | NotificationChannel updatedChannel = |
| 2094 | new NotificationChannel(mTestNotificationChannel.getId(), |
| 2095 | mTestNotificationChannel.getName(), IMPORTANCE_NONE); |
| 2096 | |
| 2097 | mBinderService.updateNotificationChannelForPackage(PKG, 0, updatedChannel); |
| 2098 | ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class); |
| 2099 | verify(mContext, times(1)).sendBroadcastAsUser(captor.capture(), any(), eq(null)); |
| 2100 | |
| 2101 | assertEquals(NotificationManager.ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED, |
| 2102 | captor.getValue().getAction()); |
| 2103 | assertEquals(PKG, captor.getValue().getPackage()); |
| 2104 | assertEquals(mTestNotificationChannel.getId(), captor.getValue().getStringExtra( |
Julia Reynolds | 44ff7c9 | 2018-02-05 10:02:30 -0500 | [diff] [blame] | 2105 | NotificationManager.EXTRA_NOTIFICATION_CHANNEL_ID)); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2106 | assertTrue(captor.getValue().getBooleanExtra(EXTRA_BLOCKED_STATE, false)); |
| 2107 | } |
| 2108 | |
| 2109 | @Test |
| 2110 | public void testUpdateChannelNotifyCreatorUnblock() throws Exception { |
| 2111 | NotificationChannel existingChannel = |
| 2112 | new NotificationChannel(mTestNotificationChannel.getId(), |
| 2113 | mTestNotificationChannel.getName(), IMPORTANCE_NONE); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2114 | mService.setPreferencesHelper(mPreferencesHelper); |
| 2115 | when(mPreferencesHelper.getNotificationChannel(eq(PKG), anyInt(), |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2116 | eq(mTestNotificationChannel.getId()), anyBoolean())) |
| 2117 | .thenReturn(existingChannel); |
| 2118 | |
| 2119 | mBinderService.updateNotificationChannelForPackage(PKG, 0, mTestNotificationChannel); |
| 2120 | ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class); |
| 2121 | verify(mContext, times(1)).sendBroadcastAsUser(captor.capture(), any(), eq(null)); |
| 2122 | |
| 2123 | assertEquals(NotificationManager.ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED, |
| 2124 | captor.getValue().getAction()); |
| 2125 | assertEquals(PKG, captor.getValue().getPackage()); |
| 2126 | assertEquals(mTestNotificationChannel.getId(), captor.getValue().getStringExtra( |
Julia Reynolds | 44ff7c9 | 2018-02-05 10:02:30 -0500 | [diff] [blame] | 2127 | NotificationManager.EXTRA_NOTIFICATION_CHANNEL_ID)); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2128 | assertFalse(captor.getValue().getBooleanExtra(EXTRA_BLOCKED_STATE, false)); |
| 2129 | } |
| 2130 | |
| 2131 | @Test |
| 2132 | public void testUpdateChannelNoNotifyCreatorOtherChanges() throws Exception { |
| 2133 | NotificationChannel existingChannel = |
| 2134 | new NotificationChannel(mTestNotificationChannel.getId(), |
| 2135 | mTestNotificationChannel.getName(), IMPORTANCE_MAX); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2136 | mService.setPreferencesHelper(mPreferencesHelper); |
| 2137 | when(mPreferencesHelper.getNotificationChannel(eq(PKG), anyInt(), |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2138 | eq(mTestNotificationChannel.getId()), anyBoolean())) |
| 2139 | .thenReturn(existingChannel); |
| 2140 | |
| 2141 | mBinderService.updateNotificationChannelForPackage(PKG, 0, mTestNotificationChannel); |
| 2142 | verify(mContext, never()).sendBroadcastAsUser(any(), any(), eq(null)); |
| 2143 | } |
| 2144 | |
| 2145 | @Test |
| 2146 | public void testUpdateGroupNotifyCreatorBlock() throws Exception { |
| 2147 | NotificationChannelGroup existing = new NotificationChannelGroup("id", "name"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2148 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 2149 | when(mPreferencesHelper.getNotificationChannelGroup(eq(existing.getId()), |
| 2150 | eq(PKG), anyInt())) |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2151 | .thenReturn(existing); |
| 2152 | |
| 2153 | NotificationChannelGroup updated = new NotificationChannelGroup("id", "name"); |
| 2154 | updated.setBlocked(true); |
| 2155 | |
| 2156 | mBinderService.updateNotificationChannelGroupForPackage(PKG, 0, updated); |
| 2157 | ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class); |
| 2158 | verify(mContext, times(1)).sendBroadcastAsUser(captor.capture(), any(), eq(null)); |
| 2159 | |
| 2160 | assertEquals(NotificationManager.ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED, |
| 2161 | captor.getValue().getAction()); |
| 2162 | assertEquals(PKG, captor.getValue().getPackage()); |
| 2163 | assertEquals(existing.getId(), captor.getValue().getStringExtra( |
Julia Reynolds | 44ff7c9 | 2018-02-05 10:02:30 -0500 | [diff] [blame] | 2164 | NotificationManager.EXTRA_NOTIFICATION_CHANNEL_GROUP_ID)); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2165 | assertTrue(captor.getValue().getBooleanExtra(EXTRA_BLOCKED_STATE, false)); |
| 2166 | } |
| 2167 | |
| 2168 | @Test |
| 2169 | public void testUpdateGroupNotifyCreatorUnblock() throws Exception { |
| 2170 | NotificationChannelGroup existing = new NotificationChannelGroup("id", "name"); |
| 2171 | existing.setBlocked(true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2172 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 2173 | when(mPreferencesHelper.getNotificationChannelGroup(eq(existing.getId()), |
| 2174 | eq(PKG), anyInt())) |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2175 | .thenReturn(existing); |
| 2176 | |
| 2177 | mBinderService.updateNotificationChannelGroupForPackage( |
| 2178 | PKG, 0, new NotificationChannelGroup("id", "name")); |
| 2179 | ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class); |
| 2180 | verify(mContext, times(1)).sendBroadcastAsUser(captor.capture(), any(), eq(null)); |
| 2181 | |
| 2182 | assertEquals(NotificationManager.ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED, |
| 2183 | captor.getValue().getAction()); |
| 2184 | assertEquals(PKG, captor.getValue().getPackage()); |
| 2185 | assertEquals(existing.getId(), captor.getValue().getStringExtra( |
Julia Reynolds | 44ff7c9 | 2018-02-05 10:02:30 -0500 | [diff] [blame] | 2186 | NotificationManager.EXTRA_NOTIFICATION_CHANNEL_GROUP_ID)); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2187 | assertFalse(captor.getValue().getBooleanExtra(EXTRA_BLOCKED_STATE, false)); |
| 2188 | } |
| 2189 | |
| 2190 | @Test |
| 2191 | public void testUpdateGroupNoNotifyCreatorOtherChanges() throws Exception { |
| 2192 | NotificationChannelGroup existing = new NotificationChannelGroup("id", "name"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2193 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 2194 | when(mPreferencesHelper.getNotificationChannelGroup( |
| 2195 | eq(existing.getId()), eq(PKG), anyInt())) |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2196 | .thenReturn(existing); |
| 2197 | |
| 2198 | mBinderService.updateNotificationChannelGroupForPackage( |
| 2199 | PKG, 0, new NotificationChannelGroup("id", "new name")); |
| 2200 | verify(mContext, never()).sendBroadcastAsUser(any(), any(), eq(null)); |
| 2201 | } |
| 2202 | |
| 2203 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2204 | public void testCreateChannelNotifyListener() throws Exception { |
| 2205 | List<String> associations = new ArrayList<>(); |
| 2206 | associations.add("a"); |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 2207 | when(mCompanionMgr.getAssociations(PKG, UserHandle.getUserId(mUid))) |
| 2208 | .thenReturn(associations); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2209 | mService.setPreferencesHelper(mPreferencesHelper); |
| 2210 | when(mPreferencesHelper.getNotificationChannel(eq(PKG), anyInt(), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2211 | eq(mTestNotificationChannel.getId()), anyBoolean())) |
| 2212 | .thenReturn(mTestNotificationChannel); |
| 2213 | NotificationChannel channel2 = new NotificationChannel("a", "b", IMPORTANCE_LOW); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2214 | when(mPreferencesHelper.getNotificationChannel(eq(PKG), anyInt(), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2215 | eq(channel2.getId()), anyBoolean())) |
| 2216 | .thenReturn(channel2); |
Julia Reynolds | dafd3a4 | 2019-05-24 13:33:28 -0400 | [diff] [blame] | 2217 | when(mPreferencesHelper.createNotificationChannel(eq(PKG), anyInt(), |
| 2218 | eq(channel2), anyBoolean(), anyBoolean())) |
| 2219 | .thenReturn(true); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2220 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2221 | reset(mListeners); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2222 | mBinderService.createNotificationChannels(PKG, |
| 2223 | new ParceledListSlice(Arrays.asList(mTestNotificationChannel, channel2))); |
Julia Reynolds | dafd3a4 | 2019-05-24 13:33:28 -0400 | [diff] [blame] | 2224 | verify(mListeners, never()).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2225 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2226 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED)); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2227 | verify(mListeners, times(1)).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2228 | eq(Process.myUserHandle()), eq(channel2), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2229 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED)); |
| 2230 | } |
| 2231 | |
| 2232 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2233 | public void testCreateChannelGroupNotifyListener() throws Exception { |
| 2234 | List<String> associations = new ArrayList<>(); |
| 2235 | associations.add("a"); |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 2236 | when(mCompanionMgr.getAssociations(PKG, UserHandle.getUserId(mUid))) |
| 2237 | .thenReturn(associations); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2238 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2239 | NotificationChannelGroup group1 = new NotificationChannelGroup("a", "b"); |
| 2240 | NotificationChannelGroup group2 = new NotificationChannelGroup("n", "m"); |
| 2241 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2242 | reset(mListeners); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2243 | mBinderService.createNotificationChannelGroups(PKG, |
| 2244 | new ParceledListSlice(Arrays.asList(group1, group2))); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2245 | verify(mListeners, times(1)).notifyNotificationChannelGroupChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2246 | eq(Process.myUserHandle()), eq(group1), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2247 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED)); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2248 | verify(mListeners, times(1)).notifyNotificationChannelGroupChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2249 | eq(Process.myUserHandle()), eq(group2), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2250 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED)); |
| 2251 | } |
| 2252 | |
| 2253 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2254 | public void testUpdateChannelNotifyListener() throws Exception { |
| 2255 | List<String> associations = new ArrayList<>(); |
| 2256 | associations.add("a"); |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 2257 | when(mCompanionMgr.getAssociations(PKG, UserHandle.getUserId(mUid))) |
| 2258 | .thenReturn(associations); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2259 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2260 | mTestNotificationChannel.setLightColor(Color.CYAN); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2261 | when(mPreferencesHelper.getNotificationChannel(eq(PKG), anyInt(), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2262 | eq(mTestNotificationChannel.getId()), anyBoolean())) |
| 2263 | .thenReturn(mTestNotificationChannel); |
| 2264 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2265 | reset(mListeners); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2266 | mBinderService.updateNotificationChannelForPackage(PKG, 0, mTestNotificationChannel); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2267 | verify(mListeners, times(1)).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2268 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2269 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED)); |
| 2270 | } |
| 2271 | |
| 2272 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2273 | public void testDeleteChannelNotifyListener() throws Exception { |
| 2274 | List<String> associations = new ArrayList<>(); |
| 2275 | associations.add("a"); |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 2276 | when(mCompanionMgr.getAssociations(PKG, UserHandle.getUserId(mUid))) |
| 2277 | .thenReturn(associations); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2278 | mService.setPreferencesHelper(mPreferencesHelper); |
| 2279 | when(mPreferencesHelper.getNotificationChannel(eq(PKG), anyInt(), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2280 | eq(mTestNotificationChannel.getId()), anyBoolean())) |
| 2281 | .thenReturn(mTestNotificationChannel); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2282 | reset(mListeners); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2283 | mBinderService.deleteNotificationChannel(PKG, mTestNotificationChannel.getId()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2284 | verify(mListeners, times(1)).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2285 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2286 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_DELETED)); |
| 2287 | } |
| 2288 | |
| 2289 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2290 | public void testDeleteChannelGroupNotifyListener() throws Exception { |
| 2291 | List<String> associations = new ArrayList<>(); |
| 2292 | associations.add("a"); |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 2293 | when(mCompanionMgr.getAssociations(PKG, UserHandle.getUserId(mUid))) |
| 2294 | .thenReturn(associations); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2295 | NotificationChannelGroup ncg = new NotificationChannelGroup("a", "b/c"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2296 | mService.setPreferencesHelper(mPreferencesHelper); |
| 2297 | when(mPreferencesHelper.getNotificationChannelGroup(eq(ncg.getId()), eq(PKG), anyInt())) |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2298 | .thenReturn(ncg); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2299 | reset(mListeners); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2300 | mBinderService.deleteNotificationChannelGroup(PKG, ncg.getId()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2301 | verify(mListeners, times(1)).notifyNotificationChannelGroupChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2302 | eq(Process.myUserHandle()), eq(ncg), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2303 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_DELETED)); |
| 2304 | } |
| 2305 | |
| 2306 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2307 | public void testUpdateNotificationChannelFromPrivilegedListener_success() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2308 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2309 | List<String> associations = new ArrayList<>(); |
| 2310 | associations.add("a"); |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 2311 | when(mCompanionMgr.getAssociations(PKG, UserHandle.getUserId(mUid))) |
| 2312 | .thenReturn(associations); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2313 | when(mPreferencesHelper.getNotificationChannel(eq(PKG), anyInt(), |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2314 | eq(mTestNotificationChannel.getId()), anyBoolean())) |
| 2315 | .thenReturn(mTestNotificationChannel); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2316 | |
| 2317 | mBinderService.updateNotificationChannelFromPrivilegedListener( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2318 | null, PKG, Process.myUserHandle(), mTestNotificationChannel); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2319 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2320 | verify(mPreferencesHelper, times(1)).updateNotificationChannel( |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 2321 | anyString(), anyInt(), any(), anyBoolean()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2322 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2323 | verify(mListeners, never()).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2324 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2325 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED)); |
| 2326 | } |
| 2327 | |
| 2328 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2329 | public void testUpdateNotificationChannelFromPrivilegedListener_noAccess() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2330 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2331 | List<String> associations = new ArrayList<>(); |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 2332 | when(mCompanionMgr.getAssociations(PKG, UserHandle.getUserId(mUid))) |
| 2333 | .thenReturn(associations); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2334 | |
| 2335 | try { |
| 2336 | mBinderService.updateNotificationChannelFromPrivilegedListener( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2337 | null, PKG, Process.myUserHandle(), mTestNotificationChannel); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2338 | fail("listeners that don't have a companion device shouldn't be able to call this"); |
| 2339 | } catch (SecurityException e) { |
| 2340 | // pass |
| 2341 | } |
| 2342 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2343 | verify(mPreferencesHelper, never()).updateNotificationChannel( |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 2344 | anyString(), anyInt(), any(), anyBoolean()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2345 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2346 | verify(mListeners, never()).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2347 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
| 2348 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED)); |
| 2349 | } |
| 2350 | |
| 2351 | @Test |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2352 | public void testUpdateNotificationChannelFromPrivilegedListener_badUser() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2353 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2354 | List<String> associations = new ArrayList<>(); |
| 2355 | associations.add("a"); |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 2356 | when(mCompanionMgr.getAssociations(PKG, UserHandle.getUserId(mUid))) |
| 2357 | .thenReturn(associations); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2358 | mListener = mock(ManagedServices.ManagedServiceInfo.class); |
Julia Reynolds | 4da7970 | 2017-06-01 11:06:10 -0400 | [diff] [blame] | 2359 | mListener.component = new ComponentName(PKG, PKG); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2360 | when(mListener.enabledAndUserMatches(anyInt())).thenReturn(false); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2361 | when(mListeners.checkServiceTokenLocked(any())).thenReturn(mListener); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2362 | |
| 2363 | try { |
| 2364 | mBinderService.updateNotificationChannelFromPrivilegedListener( |
| 2365 | null, PKG, UserHandle.ALL, mTestNotificationChannel); |
| 2366 | fail("incorrectly allowed a change to a user listener cannot see"); |
| 2367 | } catch (SecurityException e) { |
| 2368 | // pass |
| 2369 | } |
| 2370 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2371 | verify(mPreferencesHelper, never()).updateNotificationChannel( |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 2372 | anyString(), anyInt(), any(), anyBoolean()); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2373 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2374 | verify(mListeners, never()).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2375 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2376 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED)); |
| 2377 | } |
| 2378 | |
| 2379 | @Test |
Julia Reynolds | 48a6ed9 | 2018-10-22 12:52:03 -0400 | [diff] [blame] | 2380 | public void testGetNotificationChannelFromPrivilegedListener_cdm_success() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2381 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2382 | List<String> associations = new ArrayList<>(); |
| 2383 | associations.add("a"); |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 2384 | when(mCompanionMgr.getAssociations(PKG, UserHandle.getUserId(mUid))) |
| 2385 | .thenReturn(associations); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2386 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2387 | mBinderService.getNotificationChannelsFromPrivilegedListener( |
| 2388 | null, PKG, Process.myUserHandle()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2389 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2390 | verify(mPreferencesHelper, times(1)).getNotificationChannels( |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2391 | anyString(), anyInt(), anyBoolean()); |
| 2392 | } |
| 2393 | |
| 2394 | @Test |
Julia Reynolds | 48a6ed9 | 2018-10-22 12:52:03 -0400 | [diff] [blame] | 2395 | public void testGetNotificationChannelFromPrivilegedListener_cdm_noAccess() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2396 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2397 | List<String> associations = new ArrayList<>(); |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 2398 | when(mCompanionMgr.getAssociations(PKG, UserHandle.getUserId(mUid))) |
| 2399 | .thenReturn(associations); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2400 | |
| 2401 | try { |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2402 | mBinderService.getNotificationChannelsFromPrivilegedListener( |
| 2403 | null, PKG, Process.myUserHandle()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2404 | fail("listeners that don't have a companion device shouldn't be able to call this"); |
| 2405 | } catch (SecurityException e) { |
| 2406 | // pass |
| 2407 | } |
| 2408 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2409 | verify(mPreferencesHelper, never()).getNotificationChannels( |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2410 | anyString(), anyInt(), anyBoolean()); |
| 2411 | } |
| 2412 | |
| 2413 | @Test |
Julia Reynolds | 48a6ed9 | 2018-10-22 12:52:03 -0400 | [diff] [blame] | 2414 | public void testGetNotificationChannelFromPrivilegedListener_assistant_success() |
| 2415 | throws Exception { |
| 2416 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 2417 | when(mCompanionMgr.getAssociations(PKG, UserHandle.getUserId(mUid))) |
| 2418 | .thenReturn(new ArrayList<>()); |
Julia Reynolds | 48a6ed9 | 2018-10-22 12:52:03 -0400 | [diff] [blame] | 2419 | when(mAssistants.isServiceTokenValidLocked(any())).thenReturn(true); |
| 2420 | |
| 2421 | mBinderService.getNotificationChannelsFromPrivilegedListener( |
| 2422 | null, PKG, Process.myUserHandle()); |
| 2423 | |
| 2424 | verify(mPreferencesHelper, times(1)).getNotificationChannels( |
| 2425 | anyString(), anyInt(), anyBoolean()); |
| 2426 | } |
| 2427 | |
| 2428 | @Test |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 2429 | public void testGetNotificationChannelFromPrivilegedListener_assistant_noAccess() |
| 2430 | throws Exception { |
Julia Reynolds | 48a6ed9 | 2018-10-22 12:52:03 -0400 | [diff] [blame] | 2431 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 2432 | when(mCompanionMgr.getAssociations(PKG, UserHandle.getUserId(mUid))) |
| 2433 | .thenReturn(new ArrayList<>()); |
Julia Reynolds | 48a6ed9 | 2018-10-22 12:52:03 -0400 | [diff] [blame] | 2434 | when(mAssistants.isServiceTokenValidLocked(any())).thenReturn(false); |
| 2435 | |
| 2436 | try { |
| 2437 | mBinderService.getNotificationChannelsFromPrivilegedListener( |
| 2438 | null, PKG, Process.myUserHandle()); |
| 2439 | fail("listeners that don't have a companion device shouldn't be able to call this"); |
| 2440 | } catch (SecurityException e) { |
| 2441 | // pass |
| 2442 | } |
| 2443 | |
| 2444 | verify(mPreferencesHelper, never()).getNotificationChannels( |
| 2445 | anyString(), anyInt(), anyBoolean()); |
| 2446 | } |
| 2447 | |
| 2448 | @Test |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2449 | public void testGetNotificationChannelFromPrivilegedListener_badUser() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2450 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2451 | List<String> associations = new ArrayList<>(); |
| 2452 | associations.add("a"); |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 2453 | when(mCompanionMgr.getAssociations(PKG, UserHandle.getUserId(mUid))) |
| 2454 | .thenReturn(associations); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2455 | mListener = mock(ManagedServices.ManagedServiceInfo.class); |
| 2456 | when(mListener.enabledAndUserMatches(anyInt())).thenReturn(false); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2457 | when(mListeners.checkServiceTokenLocked(any())).thenReturn(mListener); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2458 | |
| 2459 | try { |
| 2460 | mBinderService.getNotificationChannelsFromPrivilegedListener( |
| 2461 | null, PKG, Process.myUserHandle()); |
| 2462 | fail("listener getting channels from a user they cannot see"); |
| 2463 | } catch (SecurityException e) { |
| 2464 | // pass |
| 2465 | } |
| 2466 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2467 | verify(mPreferencesHelper, never()).getNotificationChannels( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2468 | anyString(), anyInt(), anyBoolean()); |
| 2469 | } |
| 2470 | |
| 2471 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2472 | public void testGetNotificationChannelGroupsFromPrivilegedListener_success() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2473 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2474 | List<String> associations = new ArrayList<>(); |
| 2475 | associations.add("a"); |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 2476 | when(mCompanionMgr.getAssociations(PKG, UserHandle.getUserId(mUid))) |
| 2477 | .thenReturn(associations); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2478 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2479 | mBinderService.getNotificationChannelGroupsFromPrivilegedListener( |
| 2480 | null, PKG, Process.myUserHandle()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2481 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2482 | verify(mPreferencesHelper, times(1)).getNotificationChannelGroups(anyString(), anyInt()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2483 | } |
| 2484 | |
| 2485 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2486 | public void testGetNotificationChannelGroupsFromPrivilegedListener_noAccess() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2487 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2488 | List<String> associations = new ArrayList<>(); |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 2489 | when(mCompanionMgr.getAssociations(PKG, UserHandle.getUserId(mUid))) |
| 2490 | .thenReturn(associations); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2491 | |
| 2492 | try { |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2493 | mBinderService.getNotificationChannelGroupsFromPrivilegedListener( |
| 2494 | null, PKG, Process.myUserHandle()); |
| 2495 | fail("listeners that don't have a companion device shouldn't be able to call this"); |
| 2496 | } catch (SecurityException e) { |
| 2497 | // pass |
| 2498 | } |
| 2499 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2500 | verify(mPreferencesHelper, never()).getNotificationChannelGroups(anyString(), anyInt()); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2501 | } |
| 2502 | |
| 2503 | @Test |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2504 | public void testGetNotificationChannelGroupsFromPrivilegedListener_badUser() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2505 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2506 | List<String> associations = new ArrayList<>(); |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 2507 | when(mCompanionMgr.getAssociations(PKG, UserHandle.getUserId(mUid))) |
| 2508 | .thenReturn(associations); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2509 | mListener = mock(ManagedServices.ManagedServiceInfo.class); |
| 2510 | when(mListener.enabledAndUserMatches(anyInt())).thenReturn(false); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2511 | when(mListeners.checkServiceTokenLocked(any())).thenReturn(mListener); |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 2512 | try { |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2513 | mBinderService.getNotificationChannelGroupsFromPrivilegedListener( |
| 2514 | null, PKG, Process.myUserHandle()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2515 | fail("listeners that don't have a companion device shouldn't be able to call this"); |
| 2516 | } catch (SecurityException e) { |
| 2517 | // pass |
| 2518 | } |
| 2519 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2520 | verify(mPreferencesHelper, never()).getNotificationChannelGroups(anyString(), anyInt()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2521 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 2522 | |
| 2523 | @Test |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 2524 | public void testHasCompanionDevice_failure() throws Exception { |
| 2525 | when(mCompanionMgr.getAssociations(anyString(), anyInt())).thenThrow( |
| 2526 | new IllegalArgumentException()); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2527 | mService.hasCompanionDevice(mListener); |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 2528 | } |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 2529 | |
| 2530 | @Test |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 2531 | public void testHasCompanionDevice_noService() { |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 2532 | mService = new TestableNotificationManagerService(mContext, mNotificationRecordLogger, |
| 2533 | mNotificationInstanceIdSequence); |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 2534 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2535 | assertFalse(mService.hasCompanionDevice(mListener)); |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 2536 | } |
| 2537 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2538 | @Test |
Jay Aliomer | 24642da | 2019-07-30 09:57:41 -0400 | [diff] [blame] | 2539 | public void testSnoozeRunnable_reSnoozeASingleSnoozedNotification() throws Exception { |
| 2540 | final NotificationRecord notification = generateNotificationRecord( |
| 2541 | mTestNotificationChannel, 1, null, true); |
| 2542 | mService.addNotification(notification); |
| 2543 | when(mSnoozeHelper.getNotification(any())).thenReturn(notification); |
| 2544 | |
| 2545 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
| 2546 | mService.new SnoozeNotificationRunnable( |
| 2547 | notification.getKey(), 100, null); |
| 2548 | snoozeNotificationRunnable.run(); |
| 2549 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable2 = |
| 2550 | mService.new SnoozeNotificationRunnable( |
| 2551 | notification.getKey(), 100, null); |
| 2552 | snoozeNotificationRunnable.run(); |
| 2553 | |
| 2554 | // snooze twice |
| 2555 | verify(mSnoozeHelper, times(2)).snooze(any(NotificationRecord.class), anyLong()); |
| 2556 | } |
| 2557 | |
| 2558 | @Test |
| 2559 | public void testSnoozeRunnable_reSnoozeASnoozedNotificationWithGroupKey() throws Exception { |
| 2560 | final NotificationRecord notification = generateNotificationRecord( |
| 2561 | mTestNotificationChannel, 1, "group", true); |
| 2562 | mService.addNotification(notification); |
| 2563 | when(mSnoozeHelper.getNotification(any())).thenReturn(notification); |
| 2564 | |
| 2565 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
| 2566 | mService.new SnoozeNotificationRunnable( |
| 2567 | notification.getKey(), 100, null); |
| 2568 | snoozeNotificationRunnable.run(); |
| 2569 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable2 = |
| 2570 | mService.new SnoozeNotificationRunnable( |
| 2571 | notification.getKey(), 100, null); |
| 2572 | snoozeNotificationRunnable.run(); |
| 2573 | |
| 2574 | // snooze twice |
| 2575 | verify(mSnoozeHelper, times(2)).snooze(any(NotificationRecord.class), anyLong()); |
| 2576 | } |
| 2577 | |
| 2578 | @Test |
| 2579 | public void testSnoozeRunnable_reSnoozeMultipleNotificationsWithGroupKey() throws Exception { |
| 2580 | final NotificationRecord notification = generateNotificationRecord( |
| 2581 | mTestNotificationChannel, 1, "group", true); |
| 2582 | final NotificationRecord notification2 = generateNotificationRecord( |
| 2583 | mTestNotificationChannel, 2, "group", true); |
| 2584 | mService.addNotification(notification); |
| 2585 | mService.addNotification(notification2); |
| 2586 | when(mSnoozeHelper.getNotification(any())).thenReturn(notification); |
| 2587 | when(mSnoozeHelper.getNotifications( |
| 2588 | anyString(), anyString(), anyInt())).thenReturn(new ArrayList<>()); |
| 2589 | |
| 2590 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
| 2591 | mService.new SnoozeNotificationRunnable( |
| 2592 | notification.getKey(), 100, null); |
| 2593 | snoozeNotificationRunnable.run(); |
| 2594 | when(mSnoozeHelper.getNotifications(anyString(), anyString(), anyInt())) |
| 2595 | .thenReturn(new ArrayList<>(Arrays.asList(notification, notification2))); |
| 2596 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable2 = |
| 2597 | mService.new SnoozeNotificationRunnable( |
| 2598 | notification.getKey(), 100, null); |
| 2599 | snoozeNotificationRunnable.run(); |
| 2600 | |
| 2601 | // snooze twice |
| 2602 | verify(mSnoozeHelper, times(4)).snooze(any(NotificationRecord.class), anyLong()); |
| 2603 | } |
| 2604 | |
| 2605 | @Test |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2606 | public void testSnoozeRunnable_snoozeNonGrouped() throws Exception { |
| 2607 | final NotificationRecord nonGrouped = generateNotificationRecord( |
| 2608 | mTestNotificationChannel, 1, null, false); |
| 2609 | final NotificationRecord grouped = generateNotificationRecord( |
| 2610 | mTestNotificationChannel, 2, "group", false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2611 | mService.addNotification(grouped); |
| 2612 | mService.addNotification(nonGrouped); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2613 | |
| 2614 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2615 | mService.new SnoozeNotificationRunnable( |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2616 | nonGrouped.getKey(), 100, null); |
| 2617 | snoozeNotificationRunnable.run(); |
| 2618 | |
| 2619 | // only snooze the one notification |
| 2620 | verify(mSnoozeHelper, times(1)).snooze(any(NotificationRecord.class), anyLong()); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2621 | assertTrue(nonGrouped.getStats().hasSnoozed()); |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 2622 | |
| 2623 | assertEquals(2, mNotificationRecordLogger.numCalls()); |
| 2624 | assertEquals(NotificationRecordLogger.NotificationEvent.NOTIFICATION_SNOOZED, |
| 2625 | mNotificationRecordLogger.event(0)); |
| 2626 | assertEquals( |
| 2627 | NotificationRecordLogger.NotificationCancelledEvent.NOTIFICATION_CANCEL_SNOOZED, |
| 2628 | mNotificationRecordLogger.event(1)); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2629 | } |
| 2630 | |
| 2631 | @Test |
| 2632 | public void testSnoozeRunnable_snoozeSummary_withChildren() throws Exception { |
| 2633 | final NotificationRecord parent = generateNotificationRecord( |
| 2634 | mTestNotificationChannel, 1, "group", true); |
| 2635 | final NotificationRecord child = generateNotificationRecord( |
| 2636 | mTestNotificationChannel, 2, "group", false); |
| 2637 | final NotificationRecord child2 = generateNotificationRecord( |
| 2638 | mTestNotificationChannel, 3, "group", false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2639 | mService.addNotification(parent); |
| 2640 | mService.addNotification(child); |
| 2641 | mService.addNotification(child2); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2642 | |
| 2643 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2644 | mService.new SnoozeNotificationRunnable( |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2645 | parent.getKey(), 100, null); |
| 2646 | snoozeNotificationRunnable.run(); |
| 2647 | |
| 2648 | // snooze parent and children |
| 2649 | verify(mSnoozeHelper, times(3)).snooze(any(NotificationRecord.class), anyLong()); |
| 2650 | } |
| 2651 | |
| 2652 | @Test |
| 2653 | public void testSnoozeRunnable_snoozeGroupChild_fellowChildren() throws Exception { |
| 2654 | final NotificationRecord parent = generateNotificationRecord( |
| 2655 | mTestNotificationChannel, 1, "group", true); |
| 2656 | final NotificationRecord child = generateNotificationRecord( |
| 2657 | mTestNotificationChannel, 2, "group", false); |
| 2658 | final NotificationRecord child2 = generateNotificationRecord( |
| 2659 | mTestNotificationChannel, 3, "group", false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2660 | mService.addNotification(parent); |
| 2661 | mService.addNotification(child); |
| 2662 | mService.addNotification(child2); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2663 | |
| 2664 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2665 | mService.new SnoozeNotificationRunnable( |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2666 | child2.getKey(), 100, null); |
| 2667 | snoozeNotificationRunnable.run(); |
| 2668 | |
| 2669 | // only snooze the one child |
| 2670 | verify(mSnoozeHelper, times(1)).snooze(any(NotificationRecord.class), anyLong()); |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 2671 | |
| 2672 | assertEquals(2, mNotificationRecordLogger.numCalls()); |
| 2673 | assertEquals(NotificationRecordLogger.NotificationEvent.NOTIFICATION_SNOOZED, |
| 2674 | mNotificationRecordLogger.event(0)); |
| 2675 | assertEquals(NotificationRecordLogger.NotificationCancelledEvent |
| 2676 | .NOTIFICATION_CANCEL_SNOOZED, mNotificationRecordLogger.event(1)); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2677 | } |
| 2678 | |
| 2679 | @Test |
| 2680 | public void testSnoozeRunnable_snoozeGroupChild_onlyChildOfSummary() throws Exception { |
| 2681 | final NotificationRecord parent = generateNotificationRecord( |
| 2682 | mTestNotificationChannel, 1, "group", true); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 2683 | assertTrue(parent.getSbn().getNotification().isGroupSummary()); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2684 | final NotificationRecord child = generateNotificationRecord( |
| 2685 | mTestNotificationChannel, 2, "group", false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2686 | mService.addNotification(parent); |
| 2687 | mService.addNotification(child); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2688 | |
| 2689 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2690 | mService.new SnoozeNotificationRunnable( |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2691 | child.getKey(), 100, null); |
| 2692 | snoozeNotificationRunnable.run(); |
| 2693 | |
| 2694 | // snooze child and summary |
| 2695 | verify(mSnoozeHelper, times(2)).snooze(any(NotificationRecord.class), anyLong()); |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 2696 | |
| 2697 | assertEquals(4, mNotificationRecordLogger.numCalls()); |
| 2698 | assertEquals(NotificationRecordLogger.NotificationEvent.NOTIFICATION_SNOOZED, |
| 2699 | mNotificationRecordLogger.event(0)); |
| 2700 | assertEquals( |
| 2701 | NotificationRecordLogger.NotificationCancelledEvent.NOTIFICATION_CANCEL_SNOOZED, |
| 2702 | mNotificationRecordLogger.event(1)); |
| 2703 | assertEquals(NotificationRecordLogger.NotificationEvent.NOTIFICATION_SNOOZED, |
| 2704 | mNotificationRecordLogger.event(2)); |
| 2705 | assertEquals( |
| 2706 | NotificationRecordLogger.NotificationCancelledEvent.NOTIFICATION_CANCEL_SNOOZED, |
| 2707 | mNotificationRecordLogger.event(3)); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2708 | } |
| 2709 | |
| 2710 | @Test |
| 2711 | public void testSnoozeRunnable_snoozeGroupChild_noOthersInGroup() throws Exception { |
| 2712 | final NotificationRecord child = generateNotificationRecord( |
| 2713 | mTestNotificationChannel, 2, "group", false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2714 | mService.addNotification(child); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2715 | |
| 2716 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2717 | mService.new SnoozeNotificationRunnable( |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2718 | child.getKey(), 100, null); |
| 2719 | snoozeNotificationRunnable.run(); |
| 2720 | |
| 2721 | // snooze child only |
| 2722 | verify(mSnoozeHelper, times(1)).snooze(any(NotificationRecord.class), anyLong()); |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 2723 | |
| 2724 | assertEquals(2, mNotificationRecordLogger.numCalls()); |
| 2725 | assertEquals(NotificationRecordLogger.NotificationEvent.NOTIFICATION_SNOOZED, |
| 2726 | mNotificationRecordLogger.event(0)); |
| 2727 | assertEquals( |
| 2728 | NotificationRecordLogger.NotificationCancelledEvent.NOTIFICATION_CANCEL_SNOOZED, |
| 2729 | mNotificationRecordLogger.event(1)); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2730 | } |
| 2731 | |
| 2732 | @Test |
| 2733 | public void testPostGroupChild_unsnoozeParent() throws Exception { |
| 2734 | final NotificationRecord child = generateNotificationRecord( |
| 2735 | mTestNotificationChannel, 2, "group", false); |
| 2736 | |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 2737 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "testPostNonGroup_noUnsnoozing", |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 2738 | child.getSbn().getId(), child.getSbn().getNotification(), |
| 2739 | child.getSbn().getUserId()); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2740 | waitForIdle(); |
| 2741 | |
| 2742 | verify(mSnoozeHelper, times(1)).repostGroupSummary( |
| 2743 | anyString(), anyInt(), eq(child.getGroupKey())); |
| 2744 | } |
| 2745 | |
| 2746 | @Test |
| 2747 | public void testPostNonGroup_noUnsnoozing() throws Exception { |
| 2748 | final NotificationRecord record = generateNotificationRecord( |
| 2749 | mTestNotificationChannel, 2, null, false); |
| 2750 | |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 2751 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "testPostNonGroup_noUnsnoozing", |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 2752 | record.getSbn().getId(), record.getSbn().getNotification(), |
| 2753 | record.getSbn().getUserId()); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2754 | waitForIdle(); |
| 2755 | |
| 2756 | verify(mSnoozeHelper, never()).repostGroupSummary(anyString(), anyInt(), anyString()); |
| 2757 | } |
| 2758 | |
| 2759 | @Test |
| 2760 | public void testPostGroupSummary_noUnsnoozing() throws Exception { |
| 2761 | final NotificationRecord parent = generateNotificationRecord( |
| 2762 | mTestNotificationChannel, 2, "group", true); |
| 2763 | |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 2764 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "testPostGroupSummary_noUnsnoozing", |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 2765 | parent.getSbn().getId(), parent.getSbn().getNotification(), |
| 2766 | parent.getSbn().getUserId()); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2767 | waitForIdle(); |
| 2768 | |
| 2769 | verify(mSnoozeHelper, never()).repostGroupSummary(anyString(), anyInt(), anyString()); |
| 2770 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2771 | |
| 2772 | @Test |
Julia Reynolds | 0d07378 | 2020-02-13 16:07:52 -0500 | [diff] [blame] | 2773 | public void testSystemNotificationListenerCanUnsnooze() throws Exception { |
| 2774 | final NotificationRecord nr = generateNotificationRecord( |
| 2775 | mTestNotificationChannel, 2, "group", false); |
| 2776 | |
| 2777 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 2778 | "testSystemNotificationListenerCanUnsnooze", |
| 2779 | nr.getSbn().getId(), nr.getSbn().getNotification(), |
| 2780 | nr.getSbn().getUserId()); |
| 2781 | waitForIdle(); |
| 2782 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
| 2783 | mService.new SnoozeNotificationRunnable( |
| 2784 | nr.getKey(), 100, null); |
| 2785 | snoozeNotificationRunnable.run(); |
| 2786 | |
| 2787 | ManagedServices.ManagedServiceInfo listener = mListeners.new ManagedServiceInfo( |
| 2788 | null, new ComponentName(PKG, "test_class"), mUid, true, null, 0); |
| 2789 | listener.isSystem = true; |
| 2790 | when(mListeners.checkServiceTokenLocked(any())).thenReturn(listener); |
| 2791 | |
| 2792 | mBinderService.unsnoozeNotificationFromSystemListener(null, nr.getKey()); |
| 2793 | waitForIdle(); |
| 2794 | StatusBarNotification[] notifs = mBinderService.getActiveNotifications(PKG); |
| 2795 | assertEquals(1, notifs.length); |
| 2796 | assertNotNull(notifs[0].getKey());//mService.getNotificationRecord(nr.getSbn().getKey())); |
| 2797 | } |
| 2798 | |
| 2799 | @Test |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 2800 | public void testSetListenerAccessForUser() throws Exception { |
| 2801 | UserHandle user = UserHandle.of(10); |
| 2802 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2803 | mBinderService.setNotificationListenerAccessGrantedForUser(c, user.getIdentifier(), true); |
| 2804 | |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 2805 | |
| 2806 | verify(mContext, times(1)).sendBroadcastAsUser(any(), eq(user), any()); |
| 2807 | verify(mListeners, times(1)).setPackageOrComponentEnabled( |
| 2808 | c.flattenToString(), user.getIdentifier(), true, true); |
| 2809 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 2810 | c.flattenToString(), user.getIdentifier(), false, true); |
| 2811 | verify(mAssistants, never()).setPackageOrComponentEnabled( |
| 2812 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2813 | } |
| 2814 | |
| 2815 | @Test |
| 2816 | public void testSetAssistantAccessForUser() throws Exception { |
| 2817 | UserHandle user = UserHandle.of(10); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2818 | List<UserInfo> uis = new ArrayList<>(); |
| 2819 | UserInfo ui = new UserInfo(); |
| 2820 | ui.id = 10; |
| 2821 | uis.add(ui); |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 2822 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2823 | when(mUm.getEnabledProfiles(10)).thenReturn(uis); |
| 2824 | |
| 2825 | mBinderService.setNotificationAssistantAccessGrantedForUser(c, user.getIdentifier(), true); |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 2826 | |
| 2827 | verify(mContext, times(1)).sendBroadcastAsUser(any(), eq(user), any()); |
| 2828 | verify(mAssistants, times(1)).setPackageOrComponentEnabled( |
| 2829 | c.flattenToString(), user.getIdentifier(), true, true); |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 2830 | verify(mAssistants).setUserSet(10, true); |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 2831 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 2832 | c.flattenToString(), user.getIdentifier(), false, true); |
| 2833 | verify(mListeners, never()).setPackageOrComponentEnabled( |
| 2834 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2835 | } |
| 2836 | |
| 2837 | @Test |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 2838 | public void testGetAssistantAllowedForUser() throws Exception { |
| 2839 | UserHandle user = UserHandle.of(10); |
| 2840 | try { |
| 2841 | mBinderService.getAllowedNotificationAssistantForUser(user.getIdentifier()); |
| 2842 | } catch (IllegalStateException e) { |
| 2843 | if (!e.getMessage().contains("At most one NotificationAssistant")) { |
| 2844 | throw e; |
| 2845 | } |
| 2846 | } |
| 2847 | verify(mAssistants, times(1)).getAllowedComponents(user.getIdentifier()); |
| 2848 | } |
| 2849 | |
| 2850 | @Test |
| 2851 | public void testGetAssistantAllowed() throws Exception { |
| 2852 | try { |
| 2853 | mBinderService.getAllowedNotificationAssistant(); |
| 2854 | } catch (IllegalStateException e) { |
| 2855 | if (!e.getMessage().contains("At most one NotificationAssistant")) { |
| 2856 | throw e; |
| 2857 | } |
| 2858 | } |
| 2859 | verify(mAssistants, times(1)).getAllowedComponents(0); |
| 2860 | } |
| 2861 | |
| 2862 | @Test |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 2863 | public void testSetDndAccessForUser() throws Exception { |
| 2864 | UserHandle user = UserHandle.of(10); |
| 2865 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2866 | mBinderService.setNotificationPolicyAccessGrantedForUser( |
| 2867 | c.getPackageName(), user.getIdentifier(), true); |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 2868 | |
| 2869 | verify(mContext, times(1)).sendBroadcastAsUser(any(), eq(user), any()); |
| 2870 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 2871 | c.getPackageName(), user.getIdentifier(), true, true); |
| 2872 | verify(mAssistants, never()).setPackageOrComponentEnabled( |
| 2873 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2874 | verify(mListeners, never()).setPackageOrComponentEnabled( |
| 2875 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2876 | } |
| 2877 | |
| 2878 | @Test |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2879 | public void testSetListenerAccess() throws Exception { |
| 2880 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2881 | mBinderService.setNotificationListenerAccessGranted(c, true); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2882 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2883 | verify(mListeners, times(1)).setPackageOrComponentEnabled( |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2884 | c.flattenToString(), 0, true, true); |
| 2885 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 2886 | c.flattenToString(), 0, false, true); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2887 | verify(mAssistants, never()).setPackageOrComponentEnabled( |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2888 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2889 | } |
| 2890 | |
| 2891 | @Test |
| 2892 | public void testSetAssistantAccess() throws Exception { |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2893 | List<UserInfo> uis = new ArrayList<>(); |
| 2894 | UserInfo ui = new UserInfo(); |
| 2895 | ui.id = 0; |
| 2896 | uis.add(ui); |
| 2897 | when(mUm.getEnabledProfiles(ui.id)).thenReturn(uis); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2898 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2899 | |
| 2900 | mBinderService.setNotificationAssistantAccessGranted(c, true); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2901 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2902 | verify(mAssistants, times(1)).setPackageOrComponentEnabled( |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2903 | c.flattenToString(), 0, true, true); |
| 2904 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 2905 | c.flattenToString(), 0, false, true); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2906 | verify(mListeners, never()).setPackageOrComponentEnabled( |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2907 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2908 | } |
| 2909 | |
| 2910 | @Test |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2911 | public void testSetAssistantAccess_multiProfile() throws Exception { |
| 2912 | List<UserInfo> uis = new ArrayList<>(); |
| 2913 | UserInfo ui = new UserInfo(); |
| 2914 | ui.id = 0; |
| 2915 | uis.add(ui); |
| 2916 | UserInfo ui10 = new UserInfo(); |
| 2917 | ui10.id = 10; |
| 2918 | uis.add(ui10); |
| 2919 | when(mUm.getEnabledProfiles(ui.id)).thenReturn(uis); |
| 2920 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 2921 | |
| 2922 | mBinderService.setNotificationAssistantAccessGranted(c, true); |
| 2923 | |
| 2924 | verify(mAssistants, times(1)).setPackageOrComponentEnabled( |
| 2925 | c.flattenToString(), 0, true, true); |
| 2926 | verify(mAssistants, times(1)).setPackageOrComponentEnabled( |
| 2927 | c.flattenToString(), 10, true, true); |
| 2928 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 2929 | c.flattenToString(), 0, false, true); |
| 2930 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 2931 | c.flattenToString(), 10, false, true); |
| 2932 | verify(mListeners, never()).setPackageOrComponentEnabled( |
| 2933 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2934 | } |
| 2935 | |
| 2936 | @Test |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 2937 | public void testSetAssistantAccess_nullWithAllowedAssistant() throws Exception { |
| 2938 | ArrayList<ComponentName> componentList = new ArrayList<>(); |
| 2939 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 2940 | componentList.add(c); |
| 2941 | when(mAssistants.getAllowedComponents(anyInt())).thenReturn(componentList); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2942 | List<UserInfo> uis = new ArrayList<>(); |
| 2943 | UserInfo ui = new UserInfo(); |
| 2944 | ui.id = 0; |
| 2945 | uis.add(ui); |
| 2946 | when(mUm.getEnabledProfiles(ui.id)).thenReturn(uis); |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 2947 | |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2948 | mBinderService.setNotificationAssistantAccessGranted(null, true); |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 2949 | |
| 2950 | verify(mAssistants, times(1)).setPackageOrComponentEnabled( |
| 2951 | c.flattenToString(), 0, true, false); |
| 2952 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 2953 | c.flattenToString(), 0, false, false); |
| 2954 | verify(mListeners, never()).setPackageOrComponentEnabled( |
| 2955 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2956 | } |
| 2957 | |
| 2958 | @Test |
| 2959 | public void testSetAssistantAccessForUser_nullWithAllowedAssistant() throws Exception { |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2960 | List<UserInfo> uis = new ArrayList<>(); |
| 2961 | UserInfo ui = new UserInfo(); |
| 2962 | ui.id = 10; |
| 2963 | uis.add(ui); |
| 2964 | UserHandle user = ui.getUserHandle(); |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 2965 | ArrayList<ComponentName> componentList = new ArrayList<>(); |
| 2966 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 2967 | componentList.add(c); |
| 2968 | when(mAssistants.getAllowedComponents(anyInt())).thenReturn(componentList); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2969 | when(mUm.getEnabledProfiles(10)).thenReturn(uis); |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 2970 | |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2971 | mBinderService.setNotificationAssistantAccessGrantedForUser( |
| 2972 | null, user.getIdentifier(), true); |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 2973 | |
| 2974 | verify(mAssistants, times(1)).setPackageOrComponentEnabled( |
| 2975 | c.flattenToString(), user.getIdentifier(), true, false); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2976 | verify(mAssistants).setUserSet(10, true); |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 2977 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 2978 | c.flattenToString(), user.getIdentifier(), false, false); |
| 2979 | verify(mListeners, never()).setPackageOrComponentEnabled( |
| 2980 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2981 | } |
| 2982 | |
| 2983 | @Test |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2984 | public void testSetAssistantAccessForUser_workProfile_nullWithAllowedAssistant() |
| 2985 | throws Exception { |
| 2986 | List<UserInfo> uis = new ArrayList<>(); |
| 2987 | UserInfo ui = new UserInfo(); |
| 2988 | ui.id = 0; |
| 2989 | uis.add(ui); |
| 2990 | UserInfo ui10 = new UserInfo(); |
| 2991 | ui10.id = 10; |
| 2992 | uis.add(ui10); |
| 2993 | UserHandle user = ui.getUserHandle(); |
| 2994 | ArrayList<ComponentName> componentList = new ArrayList<>(); |
| 2995 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 2996 | componentList.add(c); |
| 2997 | when(mAssistants.getAllowedComponents(anyInt())).thenReturn(componentList); |
| 2998 | when(mUm.getEnabledProfiles(ui.id)).thenReturn(uis); |
| 2999 | |
| 3000 | mBinderService.setNotificationAssistantAccessGrantedForUser( |
| 3001 | null, user.getIdentifier(), true); |
| 3002 | |
| 3003 | verify(mAssistants, times(1)).setPackageOrComponentEnabled( |
| 3004 | c.flattenToString(), user.getIdentifier(), true, false); |
| 3005 | verify(mAssistants, times(1)).setPackageOrComponentEnabled( |
| 3006 | c.flattenToString(), ui10.id, true, false); |
| 3007 | verify(mAssistants).setUserSet(0, true); |
| 3008 | verify(mAssistants).setUserSet(10, true); |
| 3009 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 3010 | c.flattenToString(), user.getIdentifier(), false, false); |
| 3011 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 3012 | c.flattenToString(), ui10.id, false, false); |
| 3013 | verify(mListeners, never()).setPackageOrComponentEnabled( |
| 3014 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 3015 | } |
| 3016 | |
| 3017 | @Test |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3018 | public void testSetDndAccess() throws Exception { |
| 3019 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 3020 | |
| 3021 | mBinderService.setNotificationPolicyAccessGranted(c.getPackageName(), true); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3022 | |
| 3023 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 3024 | c.getPackageName(), 0, true, true); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3025 | verify(mAssistants, never()).setPackageOrComponentEnabled( |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3026 | any(), anyInt(), anyBoolean(), anyBoolean()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3027 | verify(mListeners, never()).setPackageOrComponentEnabled( |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3028 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 3029 | } |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 3030 | |
| 3031 | @Test |
Rajeev Kumar | bc79dcd | 2020-02-04 19:54:30 -0800 | [diff] [blame] | 3032 | public void testSetListenerAccess_onLowRam() throws Exception { |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 3033 | when(mActivityManager.isLowRamDevice()).thenReturn(true); |
| 3034 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 3035 | mBinderService.setNotificationListenerAccessGranted(c, true); |
| 3036 | |
Rajeev Kumar | bc79dcd | 2020-02-04 19:54:30 -0800 | [diff] [blame] | 3037 | verify(mListeners).setPackageOrComponentEnabled( |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 3038 | anyString(), anyInt(), anyBoolean(), anyBoolean()); |
Rajeev Kumar | bc79dcd | 2020-02-04 19:54:30 -0800 | [diff] [blame] | 3039 | verify(mConditionProviders).setPackageOrComponentEnabled( |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 3040 | anyString(), anyInt(), anyBoolean(), anyBoolean()); |
Rajeev Kumar | bc79dcd | 2020-02-04 19:54:30 -0800 | [diff] [blame] | 3041 | verify(mAssistants).migrateToXml(); |
| 3042 | verify(mAssistants).resetDefaultAssistantsIfNecessary(); |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 3043 | } |
| 3044 | |
| 3045 | @Test |
Rajeev Kumar | bc79dcd | 2020-02-04 19:54:30 -0800 | [diff] [blame] | 3046 | public void testSetAssistantAccess_onLowRam() throws Exception { |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 3047 | when(mActivityManager.isLowRamDevice()).thenReturn(true); |
| 3048 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 3049 | List<UserInfo> uis = new ArrayList<>(); |
| 3050 | UserInfo ui = new UserInfo(); |
| 3051 | ui.id = 0; |
| 3052 | uis.add(ui); |
| 3053 | when(mUm.getEnabledProfiles(ui.id)).thenReturn(uis); |
| 3054 | |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 3055 | mBinderService.setNotificationAssistantAccessGranted(c, true); |
| 3056 | |
Rajeev Kumar | bc79dcd | 2020-02-04 19:54:30 -0800 | [diff] [blame] | 3057 | verify(mListeners).migrateToXml(); |
Rajeev Kumar | bc79dcd | 2020-02-04 19:54:30 -0800 | [diff] [blame] | 3058 | verify(mConditionProviders).setPackageOrComponentEnabled( |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 3059 | anyString(), anyInt(), anyBoolean(), anyBoolean()); |
Rajeev Kumar | bc79dcd | 2020-02-04 19:54:30 -0800 | [diff] [blame] | 3060 | verify(mAssistants).migrateToXml(); |
| 3061 | verify(mAssistants).resetDefaultAssistantsIfNecessary(); |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 3062 | } |
| 3063 | |
| 3064 | @Test |
Rajeev Kumar | bc79dcd | 2020-02-04 19:54:30 -0800 | [diff] [blame] | 3065 | public void testSetDndAccess_onLowRam() throws Exception { |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 3066 | when(mActivityManager.isLowRamDevice()).thenReturn(true); |
| 3067 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 3068 | mBinderService.setNotificationPolicyAccessGranted(c.getPackageName(), true); |
| 3069 | |
Rajeev Kumar | bc79dcd | 2020-02-04 19:54:30 -0800 | [diff] [blame] | 3070 | verify(mListeners).migrateToXml(); |
Rajeev Kumar | bc79dcd | 2020-02-04 19:54:30 -0800 | [diff] [blame] | 3071 | verify(mConditionProviders).setPackageOrComponentEnabled( |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 3072 | anyString(), anyInt(), anyBoolean(), anyBoolean()); |
Rajeev Kumar | bc79dcd | 2020-02-04 19:54:30 -0800 | [diff] [blame] | 3073 | verify(mAssistants).migrateToXml(); |
| 3074 | verify(mAssistants).resetDefaultAssistantsIfNecessary(); |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 3075 | } |
| 3076 | |
| 3077 | @Test |
| 3078 | public void testSetListenerAccess_doesNothingOnLowRam_exceptWatch() throws Exception { |
| 3079 | when(mPackageManagerClient.hasSystemFeature(FEATURE_WATCH)).thenReturn(true); |
| 3080 | when(mActivityManager.isLowRamDevice()).thenReturn(true); |
| 3081 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 3082 | |
| 3083 | mBinderService.setNotificationListenerAccessGranted(c, true); |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 3084 | |
| 3085 | verify(mListeners, times(1)).setPackageOrComponentEnabled( |
| 3086 | c.flattenToString(), 0, true, true); |
| 3087 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 3088 | c.flattenToString(), 0, false, true); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3089 | verify(mAssistants, never()).setPackageOrComponentEnabled( |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 3090 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 3091 | } |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 3092 | |
| 3093 | @Test |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 3094 | public void testSetAssistantAccess_doesNothingOnLowRam_exceptWatch() throws Exception { |
| 3095 | when(mPackageManagerClient.hasSystemFeature(FEATURE_WATCH)).thenReturn(true); |
| 3096 | when(mActivityManager.isLowRamDevice()).thenReturn(true); |
| 3097 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 3098 | List<UserInfo> uis = new ArrayList<>(); |
| 3099 | UserInfo ui = new UserInfo(); |
| 3100 | ui.id = 0; |
| 3101 | uis.add(ui); |
| 3102 | when(mUm.getEnabledProfiles(ui.id)).thenReturn(uis); |
| 3103 | |
| 3104 | mBinderService.setNotificationAssistantAccessGranted(c, true); |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 3105 | |
| 3106 | verify(mListeners, never()).setPackageOrComponentEnabled( |
| 3107 | anyString(), anyInt(), anyBoolean(), anyBoolean()); |
| 3108 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 3109 | c.flattenToString(), 0, false, true); |
| 3110 | verify(mAssistants, times(1)).setPackageOrComponentEnabled( |
| 3111 | c.flattenToString(), 0, true, true); |
| 3112 | } |
| 3113 | |
| 3114 | @Test |
| 3115 | public void testSetDndAccess_doesNothingOnLowRam_exceptWatch() throws Exception { |
| 3116 | when(mPackageManagerClient.hasSystemFeature(FEATURE_WATCH)).thenReturn(true); |
| 3117 | when(mActivityManager.isLowRamDevice()).thenReturn(true); |
| 3118 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 3119 | |
| 3120 | mBinderService.setNotificationPolicyAccessGranted(c.getPackageName(), true); |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 3121 | |
| 3122 | verify(mListeners, never()).setPackageOrComponentEnabled( |
| 3123 | anyString(), anyInt(), anyBoolean(), anyBoolean()); |
| 3124 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 3125 | c.getPackageName(), 0, true, true); |
| 3126 | verify(mAssistants, never()).setPackageOrComponentEnabled( |
| 3127 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 3128 | } |
| 3129 | |
| 3130 | @Test |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 3131 | public void testOnlyAutogroupIfGroupChanged_noPriorNoti_autogroups() throws Exception { |
| 3132 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel, 0, null, false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3133 | mService.addEnqueuedNotification(r); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 3134 | NotificationManagerService.PostNotificationRunnable runnable = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3135 | mService.new PostNotificationRunnable(r.getKey()); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 3136 | runnable.run(); |
| 3137 | waitForIdle(); |
| 3138 | |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 3139 | verify(mGroupHelper, times(1)).onNotificationPosted(any(), anyBoolean()); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 3140 | } |
| 3141 | |
| 3142 | @Test |
| 3143 | public void testOnlyAutogroupIfGroupChanged_groupChanged_autogroups() |
| 3144 | throws Exception { |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3145 | NotificationRecord r = |
| 3146 | generateNotificationRecord(mTestNotificationChannel, 0, "group", false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3147 | mService.addNotification(r); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 3148 | |
| 3149 | r = generateNotificationRecord(mTestNotificationChannel, 0, null, false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3150 | mService.addEnqueuedNotification(r); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 3151 | NotificationManagerService.PostNotificationRunnable runnable = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3152 | mService.new PostNotificationRunnable(r.getKey()); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 3153 | runnable.run(); |
| 3154 | waitForIdle(); |
| 3155 | |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 3156 | verify(mGroupHelper, times(1)).onNotificationPosted(any(), anyBoolean()); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 3157 | } |
| 3158 | |
| 3159 | @Test |
| 3160 | public void testOnlyAutogroupIfGroupChanged_noGroupChanged_autogroups() |
| 3161 | throws Exception { |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 3162 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel, 0, "group", |
| 3163 | false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3164 | mService.addNotification(r); |
| 3165 | mService.addEnqueuedNotification(r); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 3166 | |
| 3167 | NotificationManagerService.PostNotificationRunnable runnable = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3168 | mService.new PostNotificationRunnable(r.getKey()); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 3169 | runnable.run(); |
| 3170 | waitForIdle(); |
| 3171 | |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 3172 | verify(mGroupHelper, never()).onNotificationPosted(any(), anyBoolean()); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 3173 | } |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 3174 | |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 3175 | @Test |
Brad Stenning | d2e7a97 | 2018-10-01 09:08:42 -0700 | [diff] [blame] | 3176 | public void testDontAutogroupIfCritical() throws Exception { |
| 3177 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel, 0, null, false); |
| 3178 | r.setCriticality(CriticalNotificationExtractor.CRITICAL_LOW); |
| 3179 | mService.addEnqueuedNotification(r); |
| 3180 | NotificationManagerService.PostNotificationRunnable runnable = |
| 3181 | mService.new PostNotificationRunnable(r.getKey()); |
| 3182 | runnable.run(); |
| 3183 | |
| 3184 | r = generateNotificationRecord(mTestNotificationChannel, 1, null, false); |
| 3185 | r.setCriticality(CriticalNotificationExtractor.CRITICAL); |
| 3186 | runnable = mService.new PostNotificationRunnable(r.getKey()); |
| 3187 | mService.addEnqueuedNotification(r); |
| 3188 | |
| 3189 | runnable.run(); |
| 3190 | waitForIdle(); |
| 3191 | |
| 3192 | verify(mGroupHelper, never()).onNotificationPosted(any(), anyBoolean()); |
| 3193 | } |
| 3194 | |
| 3195 | @Test |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 3196 | public void testNoFakeColorizedPermission() throws Exception { |
| 3197 | when(mPackageManagerClient.checkPermission(any(), any())).thenReturn(PERMISSION_DENIED); |
| 3198 | Notification.Builder nb = new Notification.Builder(mContext, |
| 3199 | mTestNotificationChannel.getId()) |
| 3200 | .setContentTitle("foo") |
| 3201 | .setColorized(true) |
| 3202 | .setFlag(Notification.FLAG_CAN_COLORIZE, true) |
| 3203 | .setSmallIcon(android.R.drawable.sym_def_app_icon); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 3204 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, |
| 3205 | "testNoFakeColorizedPermission", mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 3206 | nb.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 3207 | NotificationRecord nr = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 3208 | |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 3209 | mBinderService.enqueueNotificationWithTag(PKG, PKG, sbn.getTag(), |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3210 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 3211 | waitForIdle(); |
| 3212 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3213 | NotificationRecord posted = mService.findNotificationLocked( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3214 | PKG, nr.getSbn().getTag(), nr.getSbn().getId(), nr.getSbn().getUserId()); |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 3215 | |
| 3216 | assertFalse(posted.getNotification().isColorized()); |
| 3217 | } |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 3218 | |
| 3219 | @Test |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 3220 | public void testGetNotificationCountLocked() { |
| 3221 | String sampleTagToExclude = null; |
| 3222 | int sampleIdToExclude = 0; |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 3223 | for (int i = 0; i < 20; i++) { |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 3224 | NotificationRecord r = |
| 3225 | generateNotificationRecord(mTestNotificationChannel, i, null, false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3226 | mService.addEnqueuedNotification(r); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 3227 | |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 3228 | } |
| 3229 | for (int i = 0; i < 20; i++) { |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 3230 | NotificationRecord r = |
| 3231 | generateNotificationRecord(mTestNotificationChannel, i, null, false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3232 | mService.addNotification(r); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3233 | sampleTagToExclude = r.getSbn().getTag(); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 3234 | sampleIdToExclude = i; |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 3235 | } |
| 3236 | |
| 3237 | // another package |
| 3238 | Notification n = |
| 3239 | new Notification.Builder(mContext, mTestNotificationChannel.getId()) |
| 3240 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 3241 | .build(); |
| 3242 | |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 3243 | StatusBarNotification sbn = new StatusBarNotification("a", "a", 0, "tag", mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 3244 | n, UserHandle.getUserHandleForUid(mUid), null, 0); |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 3245 | NotificationRecord otherPackage = |
| 3246 | new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3247 | mService.addEnqueuedNotification(otherPackage); |
| 3248 | mService.addNotification(otherPackage); |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 3249 | |
| 3250 | // Same notifications are enqueued as posted, everything counts b/c id and tag don't match |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 3251 | // anything that's currently enqueued or posted |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 3252 | int userId = UserHandle.getUserId(mUid); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3253 | assertEquals(40, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3254 | mService.getNotificationCountLocked(PKG, userId, 0, null)); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3255 | assertEquals(40, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3256 | mService.getNotificationCountLocked(PKG, userId, 0, "tag2")); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 3257 | |
| 3258 | // return all for package "a" - "banana" tag isn't used |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3259 | assertEquals(2, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3260 | mService.getNotificationCountLocked("a", userId, 0, "banana")); |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 3261 | |
| 3262 | // exclude a known notification - it's excluded from only the posted list, not enqueued |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 3263 | assertEquals(39, mService.getNotificationCountLocked( |
| 3264 | PKG, userId, sampleIdToExclude, sampleTagToExclude)); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3265 | } |
| 3266 | |
| 3267 | @Test |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 3268 | public void testAddAutogroup_requestsSort() throws Exception { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3269 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3270 | mService.addNotification(r); |
| 3271 | mService.addAutogroupKeyLocked(r.getKey()); |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 3272 | |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 3273 | verify(mRankingHandler, times(1)).requestSort(); |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 3274 | } |
| 3275 | |
| 3276 | @Test |
| 3277 | public void testRemoveAutogroup_requestsSort() throws Exception { |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 3278 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3279 | r.setOverrideGroupKey("TEST"); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3280 | mService.addNotification(r); |
| 3281 | mService.removeAutogroupKeyLocked(r.getKey()); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3282 | |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 3283 | verify(mRankingHandler, times(1)).requestSort(); |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 3284 | } |
| 3285 | |
| 3286 | @Test |
| 3287 | public void testReaddAutogroup_noSort() throws Exception { |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 3288 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3289 | r.setOverrideGroupKey("TEST"); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3290 | mService.addNotification(r); |
| 3291 | mService.addAutogroupKeyLocked(r.getKey()); |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 3292 | |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 3293 | verify(mRankingHandler, never()).requestSort(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3294 | } |
| 3295 | |
| 3296 | @Test |
| 3297 | public void testHandleRankingSort_sendsUpdateOnSignalExtractorChange() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 3298 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3299 | NotificationManagerService.WorkerHandler handler = mock( |
| 3300 | NotificationManagerService.WorkerHandler.class); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3301 | mService.setHandler(handler); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3302 | |
| 3303 | Map<String, Answer> answers = getSignalExtractorSideEffects(); |
| 3304 | for (String message : answers.keySet()) { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3305 | mService.clearNotifications(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3306 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3307 | mService.addNotification(r); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3308 | |
| 3309 | doAnswer(answers.get(message)).when(mRankingHelper).extractSignals(r); |
| 3310 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3311 | mService.handleRankingSort(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3312 | } |
| 3313 | verify(handler, times(answers.size())).scheduleSendRankingUpdate(); |
| 3314 | } |
| 3315 | |
| 3316 | @Test |
| 3317 | public void testHandleRankingSort_noUpdateWhenNoSignalChange() throws Exception { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3318 | mService.setRankingHelper(mRankingHelper); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3319 | NotificationManagerService.WorkerHandler handler = mock( |
| 3320 | NotificationManagerService.WorkerHandler.class); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3321 | mService.setHandler(handler); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3322 | |
| 3323 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3324 | mService.addNotification(r); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3325 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3326 | mService.handleRankingSort(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3327 | verify(handler, never()).scheduleSendRankingUpdate(); |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 3328 | } |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3329 | |
| 3330 | @Test |
| 3331 | public void testReadPolicyXml_readApprovedServicesFromXml() throws Exception { |
Julia Reynolds | d6d5a59 | 2018-04-02 11:03:32 -0400 | [diff] [blame] | 3332 | final String upgradeXml = "<notification-policy version=\"1\">" |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3333 | + "<ranking></ranking>" |
| 3334 | + "<enabled_listeners>" |
| 3335 | + "<service_listing approved=\"test\" user=\"0\" primary=\"true\" />" |
| 3336 | + "</enabled_listeners>" |
| 3337 | + "<enabled_assistants>" |
| 3338 | + "<service_listing approved=\"test\" user=\"0\" primary=\"true\" />" |
| 3339 | + "</enabled_assistants>" |
| 3340 | + "<dnd_apps>" |
| 3341 | + "<service_listing approved=\"test\" user=\"0\" primary=\"true\" />" |
| 3342 | + "</dnd_apps>" |
| 3343 | + "</notification-policy>"; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3344 | mService.readPolicyXml( |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 3345 | new BufferedInputStream(new ByteArrayInputStream(upgradeXml.getBytes())), |
| 3346 | false, |
| 3347 | UserHandle.USER_ALL); |
| 3348 | verify(mListeners, times(1)).readXml(any(), any(), anyBoolean(), anyInt()); |
| 3349 | verify(mConditionProviders, times(1)).readXml(any(), any(), anyBoolean(), anyInt()); |
| 3350 | verify(mAssistants, times(1)).readXml(any(), any(), anyBoolean(), anyInt()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3351 | |
| 3352 | // numbers are inflated for setup |
| 3353 | verify(mListeners, times(1)).migrateToXml(); |
| 3354 | verify(mConditionProviders, times(1)).migrateToXml(); |
| 3355 | verify(mAssistants, times(1)).migrateToXml(); |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 3356 | verify(mAssistants, times(2)).resetDefaultAssistantsIfNecessary(); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3357 | } |
| 3358 | |
| 3359 | @Test |
Jay Aliomer | 4dc508d | 2019-08-06 17:36:31 -0400 | [diff] [blame] | 3360 | public void testReadPolicyXml_readSnoozedNotificationsFromXml() throws Exception { |
| 3361 | final String upgradeXml = "<notification-policy version=\"1\">" |
| 3362 | + "<snoozed-notifications>></snoozed-notifications>" |
| 3363 | + "</notification-policy>"; |
| 3364 | mService.readPolicyXml( |
| 3365 | new BufferedInputStream(new ByteArrayInputStream(upgradeXml.getBytes())), |
| 3366 | false, |
| 3367 | UserHandle.USER_ALL); |
Julia Reynolds | 9050d9a | 2020-02-20 09:38:05 -0500 | [diff] [blame] | 3368 | verify(mSnoozeHelper, times(1)).readXml(any(XmlPullParser.class), anyLong()); |
Jay Aliomer | 4dc508d | 2019-08-06 17:36:31 -0400 | [diff] [blame] | 3369 | } |
| 3370 | |
| 3371 | @Test |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3372 | public void testReadPolicyXml_readApprovedServicesFromSettings() throws Exception { |
| 3373 | final String preupgradeXml = "<notification-policy version=\"1\">" |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3374 | + "<ranking></ranking>" |
| 3375 | + "</notification-policy>"; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3376 | mService.readPolicyXml( |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 3377 | new BufferedInputStream(new ByteArrayInputStream(preupgradeXml.getBytes())), |
| 3378 | false, |
| 3379 | UserHandle.USER_ALL); |
| 3380 | verify(mListeners, never()).readXml(any(), any(), anyBoolean(), anyInt()); |
| 3381 | verify(mConditionProviders, never()).readXml(any(), any(), anyBoolean(), anyInt()); |
| 3382 | verify(mAssistants, never()).readXml(any(), any(), anyBoolean(), anyInt()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3383 | |
| 3384 | // numbers are inflated for setup |
| 3385 | verify(mListeners, times(2)).migrateToXml(); |
| 3386 | verify(mConditionProviders, times(2)).migrateToXml(); |
| 3387 | verify(mAssistants, times(2)).migrateToXml(); |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 3388 | verify(mAssistants, times(2)).resetDefaultAssistantsIfNecessary(); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3389 | } |
| 3390 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 3391 | @Test |
| 3392 | public void testReadPolicyXml_doesNotRestoreManagedServicesForManagedUser() throws Exception { |
| 3393 | final String policyXml = "<notification-policy version=\"1\">" |
| 3394 | + "<ranking></ranking>" |
| 3395 | + "<enabled_listeners>" |
| 3396 | + "<service_listing approved=\"test\" user=\"10\" primary=\"true\" />" |
| 3397 | + "</enabled_listeners>" |
| 3398 | + "<enabled_assistants>" |
| 3399 | + "<service_listing approved=\"test\" user=\"10\" primary=\"true\" />" |
| 3400 | + "</enabled_assistants>" |
| 3401 | + "<dnd_apps>" |
| 3402 | + "<service_listing approved=\"test\" user=\"10\" primary=\"true\" />" |
| 3403 | + "</dnd_apps>" |
| 3404 | + "</notification-policy>"; |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 3405 | when(mUm.isManagedProfile(10)).thenReturn(true); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 3406 | mService.readPolicyXml( |
| 3407 | new BufferedInputStream(new ByteArrayInputStream(policyXml.getBytes())), |
| 3408 | true, |
| 3409 | 10); |
| 3410 | verify(mListeners, never()).readXml(any(), any(), eq(true), eq(10)); |
| 3411 | verify(mConditionProviders, never()).readXml(any(), any(), eq(true), eq(10)); |
| 3412 | verify(mAssistants, never()).readXml(any(), any(), eq(true), eq(10)); |
| 3413 | } |
| 3414 | |
| 3415 | @Test |
| 3416 | public void testReadPolicyXml_restoresManagedServicesForNonManagedUser() throws Exception { |
| 3417 | final String policyXml = "<notification-policy version=\"1\">" |
| 3418 | + "<ranking></ranking>" |
| 3419 | + "<enabled_listeners>" |
| 3420 | + "<service_listing approved=\"test\" user=\"10\" primary=\"true\" />" |
| 3421 | + "</enabled_listeners>" |
| 3422 | + "<enabled_assistants>" |
| 3423 | + "<service_listing approved=\"test\" user=\"10\" primary=\"true\" />" |
| 3424 | + "</enabled_assistants>" |
| 3425 | + "<dnd_apps>" |
| 3426 | + "<service_listing approved=\"test\" user=\"10\" primary=\"true\" />" |
| 3427 | + "</dnd_apps>" |
| 3428 | + "</notification-policy>"; |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 3429 | when(mUm.isManagedProfile(10)).thenReturn(false); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 3430 | mService.readPolicyXml( |
| 3431 | new BufferedInputStream(new ByteArrayInputStream(policyXml.getBytes())), |
| 3432 | true, |
| 3433 | 10); |
| 3434 | verify(mListeners, times(1)).readXml(any(), any(), eq(true), eq(10)); |
| 3435 | verify(mConditionProviders, times(1)).readXml(any(), any(), eq(true), eq(10)); |
| 3436 | verify(mAssistants, times(1)).readXml(any(), any(), eq(true), eq(10)); |
| 3437 | } |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 3438 | |
| 3439 | @Test |
| 3440 | public void testLocaleChangedCallsUpdateDefaultZenModeRules() throws Exception { |
| 3441 | ZenModeHelper mZenModeHelper = mock(ZenModeHelper.class); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3442 | mService.mZenModeHelper = mZenModeHelper; |
| 3443 | mService.mLocaleChangeReceiver.onReceive(mContext, |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 3444 | new Intent(Intent.ACTION_LOCALE_CHANGED)); |
| 3445 | |
| 3446 | verify(mZenModeHelper, times(1)).updateDefaultZenRules(); |
| 3447 | } |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3448 | |
| 3449 | @Test |
| 3450 | public void testBumpFGImportance_noChannelChangePreOApp() throws Exception { |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3451 | String preOPkg = PKG_N_MR1; |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3452 | final ApplicationInfo legacy = new ApplicationInfo(); |
| 3453 | legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1; |
| 3454 | when(mPackageManagerClient.getApplicationInfoAsUser(eq(preOPkg), anyInt(), anyInt())) |
| 3455 | .thenReturn(legacy); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 3456 | when(mPackageManagerClient.getPackageUidAsUser(eq(preOPkg), anyInt())) |
| 3457 | .thenReturn(Binder.getCallingUid()); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3458 | getContext().setMockPackageManager(mPackageManagerClient); |
| 3459 | |
| 3460 | Notification.Builder nb = new Notification.Builder(mContext, |
| 3461 | NotificationChannel.DEFAULT_CHANNEL_ID) |
| 3462 | .setContentTitle("foo") |
| 3463 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 3464 | .setFlag(FLAG_FOREGROUND_SERVICE, true) |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3465 | .setPriority(Notification.PRIORITY_MIN); |
| 3466 | |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 3467 | StatusBarNotification sbn = new StatusBarNotification(preOPkg, preOPkg, 9, |
| 3468 | "testBumpFGImportance_noChannelChangePreOApp", |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 3469 | Binder.getCallingUid(), 0, nb.build(), |
| 3470 | UserHandle.getUserHandleForUid(Binder.getCallingUid()), null, 0); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3471 | |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 3472 | mBinderService.enqueueNotificationWithTag(sbn.getPackageName(), sbn.getOpPkg(), |
| 3473 | sbn.getTag(), sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3474 | waitForIdle(); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 3475 | |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3476 | assertEquals(IMPORTANCE_LOW, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3477 | mService.getNotificationRecord(sbn.getKey()).getImportance()); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3478 | |
| 3479 | nb = new Notification.Builder(mContext) |
| 3480 | .setContentTitle("foo") |
| 3481 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 3482 | .setFlag(FLAG_FOREGROUND_SERVICE, true) |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3483 | .setPriority(Notification.PRIORITY_MIN); |
| 3484 | |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 3485 | sbn = new StatusBarNotification(preOPkg, preOPkg, 9, |
| 3486 | "testBumpFGImportance_noChannelChangePreOApp", Binder.getCallingUid(), |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 3487 | 0, nb.build(), UserHandle.getUserHandleForUid(Binder.getCallingUid()), null, 0); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3488 | |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 3489 | mBinderService.enqueueNotificationWithTag(preOPkg, preOPkg, |
| 3490 | "testBumpFGImportance_noChannelChangePreOApp", |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3491 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 3492 | waitForIdle(); |
| 3493 | assertEquals(IMPORTANCE_LOW, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3494 | mService.getNotificationRecord(sbn.getKey()).getImportance()); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3495 | |
| 3496 | NotificationChannel defaultChannel = mBinderService.getNotificationChannel( |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 3497 | preOPkg, mContext.getUserId(), preOPkg, NotificationChannel.DEFAULT_CHANNEL_ID); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3498 | assertEquals(IMPORTANCE_UNSPECIFIED, defaultChannel.getImportance()); |
| 3499 | } |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3500 | |
| 3501 | @Test |
| 3502 | public void testStats_updatedOnDirectReply() throws Exception { |
| 3503 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3504 | mService.addNotification(r); |
| 3505 | |
| 3506 | mService.mNotificationDelegate.onNotificationDirectReplied(r.getKey()); |
| 3507 | assertTrue(mService.getNotificationRecord(r.getKey()).getStats().hasDirectReplied()); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3508 | verify(mAssistants).notifyAssistantNotificationDirectReplyLocked(eq(r.getSbn())); |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 3509 | |
| 3510 | assertEquals(1, mNotificationRecordLogger.numCalls()); |
| 3511 | assertEquals(NotificationRecordLogger.NotificationEvent.NOTIFICATION_DIRECT_REPLIED, |
| 3512 | mNotificationRecordLogger.event(0)); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3513 | } |
| 3514 | |
| 3515 | @Test |
Julia Reynolds | 84dc96b | 2017-11-14 09:51:01 -0500 | [diff] [blame] | 3516 | public void testStats_updatedOnUserExpansion() throws Exception { |
| 3517 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3518 | mService.addNotification(r); |
| 3519 | |
Gustav Sennton | a8e38aa | 2019-01-22 14:55:39 +0000 | [diff] [blame] | 3520 | mService.mNotificationDelegate.onNotificationExpansionChanged(r.getKey(), true, true, |
| 3521 | NOTIFICATION_LOCATION_UNKNOWN); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3522 | verify(mAssistants).notifyAssistantExpansionChangedLocked(eq(r.getSbn()), eq(true), |
| 3523 | eq((true))); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3524 | assertTrue(mService.getNotificationRecord(r.getKey()).getStats().hasExpanded()); |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 3525 | |
Gustav Sennton | a8e38aa | 2019-01-22 14:55:39 +0000 | [diff] [blame] | 3526 | mService.mNotificationDelegate.onNotificationExpansionChanged(r.getKey(), true, false, |
| 3527 | NOTIFICATION_LOCATION_UNKNOWN); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3528 | verify(mAssistants).notifyAssistantExpansionChangedLocked(eq(r.getSbn()), eq(true), |
| 3529 | eq((false))); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3530 | assertTrue(mService.getNotificationRecord(r.getKey()).getStats().hasExpanded()); |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 3531 | |
| 3532 | assertEquals(2, mNotificationRecordLogger.numCalls()); |
| 3533 | assertEquals(NotificationRecordLogger.NotificationEvent.NOTIFICATION_DETAIL_OPEN_USER, |
| 3534 | mNotificationRecordLogger.event(0)); |
| 3535 | assertEquals(NotificationRecordLogger.NotificationEvent.NOTIFICATION_DETAIL_CLOSE_USER, |
| 3536 | mNotificationRecordLogger.event(1)); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3537 | } |
| 3538 | |
| 3539 | @Test |
Julia Reynolds | 84dc96b | 2017-11-14 09:51:01 -0500 | [diff] [blame] | 3540 | public void testStats_notUpdatedOnAutoExpansion() throws Exception { |
| 3541 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3542 | mService.addNotification(r); |
| 3543 | |
Gustav Sennton | a8e38aa | 2019-01-22 14:55:39 +0000 | [diff] [blame] | 3544 | mService.mNotificationDelegate.onNotificationExpansionChanged(r.getKey(), false, true, |
| 3545 | NOTIFICATION_LOCATION_UNKNOWN); |
Julia Reynolds | 84dc96b | 2017-11-14 09:51:01 -0500 | [diff] [blame] | 3546 | assertFalse(mService.getNotificationRecord(r.getKey()).getStats().hasExpanded()); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3547 | verify(mAssistants).notifyAssistantExpansionChangedLocked(eq(r.getSbn()), eq(false), |
| 3548 | eq((true))); |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 3549 | |
Gustav Sennton | a8e38aa | 2019-01-22 14:55:39 +0000 | [diff] [blame] | 3550 | mService.mNotificationDelegate.onNotificationExpansionChanged(r.getKey(), false, false, |
| 3551 | NOTIFICATION_LOCATION_UNKNOWN); |
Julia Reynolds | 84dc96b | 2017-11-14 09:51:01 -0500 | [diff] [blame] | 3552 | assertFalse(mService.getNotificationRecord(r.getKey()).getStats().hasExpanded()); |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 3553 | verify(mAssistants).notifyAssistantExpansionChangedLocked( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3554 | eq(r.getSbn()), eq(false), eq((false))); |
Julia Reynolds | 84dc96b | 2017-11-14 09:51:01 -0500 | [diff] [blame] | 3555 | } |
| 3556 | |
| 3557 | @Test |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3558 | public void testStats_updatedOnViewSettings() throws Exception { |
| 3559 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3560 | mService.addNotification(r); |
| 3561 | |
| 3562 | mService.mNotificationDelegate.onNotificationSettingsViewed(r.getKey()); |
| 3563 | assertTrue(mService.getNotificationRecord(r.getKey()).getStats().hasViewedSettings()); |
| 3564 | } |
| 3565 | |
| 3566 | @Test |
| 3567 | public void testStats_updatedOnVisibilityChanged() throws Exception { |
| 3568 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3569 | mService.addNotification(r); |
| 3570 | |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 3571 | final NotificationVisibility nv = NotificationVisibility.obtain(r.getKey(), 1, 2, true); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3572 | mService.mNotificationDelegate.onNotificationVisibilityChanged( |
| 3573 | new NotificationVisibility[] {nv}, new NotificationVisibility[]{}); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3574 | verify(mAssistants).notifyAssistantVisibilityChangedLocked(eq(r.getSbn()), eq(true)); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3575 | assertTrue(mService.getNotificationRecord(r.getKey()).getStats().hasSeen()); |
| 3576 | mService.mNotificationDelegate.onNotificationVisibilityChanged( |
| 3577 | new NotificationVisibility[] {}, new NotificationVisibility[]{nv}); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3578 | verify(mAssistants).notifyAssistantVisibilityChangedLocked(eq(r.getSbn()), eq(false)); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3579 | assertTrue(mService.getNotificationRecord(r.getKey()).getStats().hasSeen()); |
| 3580 | } |
| 3581 | |
| 3582 | @Test |
| 3583 | public void testStats_dismissalSurface() throws Exception { |
| 3584 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
Will Brockman | 4f0f9d2 | 2020-02-23 21:18:11 -0500 | [diff] [blame] | 3585 | r.getSbn().setInstanceId(mNotificationInstanceIdSequence.newInstanceId()); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3586 | mService.addNotification(r); |
| 3587 | |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 3588 | final NotificationVisibility nv = NotificationVisibility.obtain(r.getKey(), 0, 1, true); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3589 | mService.mNotificationDelegate.onNotificationClear(mUid, 0, PKG, r.getSbn().getTag(), |
| 3590 | r.getSbn().getId(), r.getUserId(), r.getKey(), NotificationStats.DISMISSAL_AOD, |
Julia Reynolds | fd4099d | 2018-08-21 11:06:06 -0400 | [diff] [blame] | 3591 | NotificationStats.DISMISS_SENTIMENT_POSITIVE, nv); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3592 | waitForIdle(); |
| 3593 | |
| 3594 | assertEquals(NotificationStats.DISMISSAL_AOD, r.getStats().getDismissalSurface()); |
Will Brockman | 75c6057 | 2020-01-31 10:30:27 -0500 | [diff] [blame] | 3595 | |
| 3596 | // Using mService.addNotification() does not generate a NotificationRecordLogger log, |
| 3597 | // so we only get the cancel notification. |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 3598 | assertEquals(1, mNotificationRecordLogger.numCalls()); |
Will Brockman | 75c6057 | 2020-01-31 10:30:27 -0500 | [diff] [blame] | 3599 | |
Will Brockman | 75c6057 | 2020-01-31 10:30:27 -0500 | [diff] [blame] | 3600 | assertEquals( |
| 3601 | NotificationRecordLogger.NotificationCancelledEvent.NOTIFICATION_CANCEL_USER_AOD, |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 3602 | mNotificationRecordLogger.event(0)); |
Will Brockman | 4f0f9d2 | 2020-02-23 21:18:11 -0500 | [diff] [blame] | 3603 | assertEquals(1, mNotificationRecordLogger.get(0).getInstanceId()); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3604 | } |
| 3605 | |
| 3606 | @Test |
Julia Reynolds | fd4099d | 2018-08-21 11:06:06 -0400 | [diff] [blame] | 3607 | public void testStats_dismissalSentiment() throws Exception { |
| 3608 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3609 | mService.addNotification(r); |
| 3610 | |
| 3611 | final NotificationVisibility nv = NotificationVisibility.obtain(r.getKey(), 0, 1, true); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3612 | mService.mNotificationDelegate.onNotificationClear(mUid, 0, PKG, r.getSbn().getTag(), |
| 3613 | r.getSbn().getId(), r.getUserId(), r.getKey(), NotificationStats.DISMISSAL_AOD, |
Julia Reynolds | fd4099d | 2018-08-21 11:06:06 -0400 | [diff] [blame] | 3614 | NotificationStats.DISMISS_SENTIMENT_NEGATIVE, nv); |
| 3615 | waitForIdle(); |
| 3616 | |
| 3617 | assertEquals(NotificationStats.DISMISS_SENTIMENT_NEGATIVE, |
| 3618 | r.getStats().getDismissalSentiment()); |
| 3619 | } |
| 3620 | |
| 3621 | @Test |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3622 | public void testApplyAdjustmentMultiUser() throws Exception { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3623 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3624 | mService.addNotification(r); |
| 3625 | NotificationManagerService.WorkerHandler handler = mock( |
| 3626 | NotificationManagerService.WorkerHandler.class); |
| 3627 | mService.setHandler(handler); |
| 3628 | |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3629 | when(mAssistants.isSameUser(eq(null), anyInt())).thenReturn(false); |
| 3630 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3631 | Bundle signals = new Bundle(); |
| 3632 | signals.putInt(Adjustment.KEY_USER_SENTIMENT, |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3633 | USER_SENTIMENT_NEGATIVE); |
| 3634 | Adjustment adjustment = new Adjustment( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3635 | r.getSbn().getPackageName(), r.getKey(), signals, "", r.getUser().getIdentifier()); |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3636 | mBinderService.applyAdjustmentFromAssistant(null, adjustment); |
| 3637 | |
| 3638 | waitForIdle(); |
| 3639 | |
| 3640 | verify(handler, timeout(300).times(0)).scheduleSendRankingUpdate(); |
| 3641 | } |
| 3642 | |
| 3643 | @Test |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 3644 | public void testAssistantBlockingTriggersCancel() throws Exception { |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 3645 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3646 | mService.addNotification(r); |
| 3647 | NotificationManagerService.WorkerHandler handler = mock( |
| 3648 | NotificationManagerService.WorkerHandler.class); |
| 3649 | mService.setHandler(handler); |
| 3650 | |
| 3651 | Bundle signals = new Bundle(); |
Julia Reynolds | c7dcdc2 | 2019-03-25 10:26:14 -0400 | [diff] [blame] | 3652 | signals.putInt(KEY_IMPORTANCE, IMPORTANCE_NONE); |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 3653 | Adjustment adjustment = new Adjustment( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3654 | r.getSbn().getPackageName(), r.getKey(), signals, "", r.getUser().getIdentifier()); |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 3655 | when(mAssistants.isSameUser(any(), anyInt())).thenReturn(true); |
| 3656 | mBinderService.applyAdjustmentFromAssistant(null, adjustment); |
| 3657 | |
| 3658 | waitForIdle(); |
| 3659 | |
| 3660 | verify(handler, timeout(300).times(0)).scheduleSendRankingUpdate(); |
| 3661 | verify(handler, times(1)).scheduleCancelNotification(any()); |
| 3662 | } |
| 3663 | |
| 3664 | @Test |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3665 | public void testApplyEnqueuedAdjustmentFromAssistant_singleUser() throws Exception { |
| 3666 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3667 | mService.addEnqueuedNotification(r); |
| 3668 | NotificationManagerService.WorkerHandler handler = mock( |
| 3669 | NotificationManagerService.WorkerHandler.class); |
| 3670 | mService.setHandler(handler); |
| 3671 | when(mAssistants.isSameUser(eq(null), anyInt())).thenReturn(true); |
| 3672 | |
| 3673 | Bundle signals = new Bundle(); |
| 3674 | signals.putInt(Adjustment.KEY_USER_SENTIMENT, |
| 3675 | USER_SENTIMENT_NEGATIVE); |
| 3676 | Adjustment adjustment = new Adjustment( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3677 | r.getSbn().getPackageName(), r.getKey(), signals, "", r.getUser().getIdentifier()); |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3678 | mBinderService.applyEnqueuedAdjustmentFromAssistant(null, adjustment); |
| 3679 | |
| 3680 | assertEquals(USER_SENTIMENT_NEGATIVE, r.getUserSentiment()); |
| 3681 | } |
| 3682 | |
| 3683 | @Test |
Julia Reynolds | dc6adc6 | 2019-04-08 10:35:40 -0400 | [diff] [blame] | 3684 | public void testApplyEnqueuedAdjustmentFromAssistant_importance() throws Exception { |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 3685 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3686 | mService.addEnqueuedNotification(r); |
| 3687 | NotificationManagerService.WorkerHandler handler = mock( |
| 3688 | NotificationManagerService.WorkerHandler.class); |
| 3689 | mService.setHandler(handler); |
| 3690 | when(mAssistants.isSameUser(eq(null), anyInt())).thenReturn(true); |
| 3691 | |
| 3692 | Bundle signals = new Bundle(); |
Julia Reynolds | c7dcdc2 | 2019-03-25 10:26:14 -0400 | [diff] [blame] | 3693 | signals.putInt(KEY_IMPORTANCE, IMPORTANCE_LOW); |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 3694 | Adjustment adjustment = new Adjustment( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3695 | r.getSbn().getPackageName(), r.getKey(), signals, "", r.getUser().getIdentifier()); |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 3696 | mBinderService.applyEnqueuedAdjustmentFromAssistant(null, adjustment); |
| 3697 | |
| 3698 | assertEquals(IMPORTANCE_LOW, r.getImportance()); |
| 3699 | } |
| 3700 | |
| 3701 | @Test |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3702 | public void testApplyEnqueuedAdjustmentFromAssistant_crossUser() throws Exception { |
| 3703 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3704 | mService.addEnqueuedNotification(r); |
| 3705 | NotificationManagerService.WorkerHandler handler = mock( |
| 3706 | NotificationManagerService.WorkerHandler.class); |
| 3707 | mService.setHandler(handler); |
| 3708 | when(mAssistants.isSameUser(eq(null), anyInt())).thenReturn(false); |
| 3709 | |
| 3710 | Bundle signals = new Bundle(); |
| 3711 | signals.putInt(Adjustment.KEY_USER_SENTIMENT, |
| 3712 | USER_SENTIMENT_NEGATIVE); |
| 3713 | Adjustment adjustment = new Adjustment( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3714 | r.getSbn().getPackageName(), r.getKey(), signals, "", r.getUser().getIdentifier()); |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3715 | mBinderService.applyEnqueuedAdjustmentFromAssistant(null, adjustment); |
| 3716 | |
| 3717 | assertEquals(USER_SENTIMENT_NEUTRAL, r.getUserSentiment()); |
| 3718 | |
| 3719 | waitForIdle(); |
| 3720 | |
| 3721 | verify(handler, timeout(300).times(0)).scheduleSendRankingUpdate(); |
| 3722 | } |
| 3723 | |
| 3724 | @Test |
| 3725 | public void testUserSentimentChangeTriggersUpdate() throws Exception { |
| 3726 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3727 | mService.addNotification(r); |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3728 | when(mAssistants.isSameUser(eq(null), anyInt())).thenReturn(true); |
| 3729 | |
| 3730 | Bundle signals = new Bundle(); |
| 3731 | signals.putInt(Adjustment.KEY_USER_SENTIMENT, |
| 3732 | USER_SENTIMENT_NEGATIVE); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3733 | Adjustment adjustment = new Adjustment( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3734 | r.getSbn().getPackageName(), r.getKey(), signals, "", r.getUser().getIdentifier()); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3735 | mBinderService.applyAdjustmentFromAssistant(null, adjustment); |
| 3736 | |
| 3737 | waitForIdle(); |
| 3738 | |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 3739 | verify(mRankingHandler, timeout(300).times(1)).requestSort(); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3740 | } |
Julia Reynolds | 7bcb57b | 2018-01-22 10:37:58 -0500 | [diff] [blame] | 3741 | |
| 3742 | @Test |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3743 | public void testTooLateAdjustmentTriggersUpdate() throws Exception { |
| 3744 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3745 | mService.addNotification(r); |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3746 | when(mAssistants.isSameUser(eq(null), anyInt())).thenReturn(true); |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3747 | |
| 3748 | Bundle signals = new Bundle(); |
| 3749 | signals.putInt(Adjustment.KEY_USER_SENTIMENT, |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3750 | USER_SENTIMENT_NEGATIVE); |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3751 | Adjustment adjustment = new Adjustment( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3752 | r.getSbn().getPackageName(), r.getKey(), signals, "", r.getUser().getIdentifier()); |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3753 | mBinderService.applyEnqueuedAdjustmentFromAssistant(null, adjustment); |
| 3754 | |
| 3755 | waitForIdle(); |
| 3756 | |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 3757 | verify(mRankingHandler, times(1)).requestSort(); |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3758 | } |
| 3759 | |
| 3760 | @Test |
| 3761 | public void testEnqueuedAdjustmentAppliesAdjustments() throws Exception { |
| 3762 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3763 | mService.addEnqueuedNotification(r); |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3764 | when(mAssistants.isSameUser(eq(null), anyInt())).thenReturn(true); |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3765 | |
| 3766 | Bundle signals = new Bundle(); |
| 3767 | signals.putInt(Adjustment.KEY_USER_SENTIMENT, |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3768 | USER_SENTIMENT_NEGATIVE); |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3769 | Adjustment adjustment = new Adjustment( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3770 | r.getSbn().getPackageName(), r.getKey(), signals, "", r.getUser().getIdentifier()); |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3771 | mBinderService.applyEnqueuedAdjustmentFromAssistant(null, adjustment); |
| 3772 | |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 3773 | assertEquals(USER_SENTIMENT_NEGATIVE, r.getUserSentiment()); |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3774 | } |
| 3775 | |
| 3776 | @Test |
Julia Reynolds | d78263d | 2018-01-30 10:40:41 -0500 | [diff] [blame] | 3777 | public void testRestore() throws Exception { |
| 3778 | int systemChecks = mService.countSystemChecks; |
Julia Reynolds | 57a974b | 2019-10-07 11:51:47 -0400 | [diff] [blame] | 3779 | mBinderService.applyRestore(null, USER_SYSTEM); |
Julia Reynolds | d78263d | 2018-01-30 10:40:41 -0500 | [diff] [blame] | 3780 | assertEquals(1, mService.countSystemChecks - systemChecks); |
| 3781 | } |
| 3782 | |
| 3783 | @Test |
Julia Reynolds | 469144c | 2019-06-21 14:30:28 -0400 | [diff] [blame] | 3784 | public void testBackupEmptySound() throws Exception { |
| 3785 | NotificationChannel channel = new NotificationChannel("a", "ab", IMPORTANCE_DEFAULT); |
| 3786 | channel.setSound(Uri.EMPTY, null); |
| 3787 | |
| 3788 | XmlSerializer serializer = new FastXmlSerializer(); |
| 3789 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 3790 | serializer.setOutput(new BufferedOutputStream(baos), "utf-8"); |
| 3791 | channel.writeXmlForBackup(serializer, getContext()); |
| 3792 | |
| 3793 | XmlPullParser parser = Xml.newPullParser(); |
| 3794 | parser.setInput(new BufferedInputStream( |
| 3795 | new ByteArrayInputStream(baos.toByteArray())), null); |
| 3796 | NotificationChannel restored = new NotificationChannel("a", "ab", IMPORTANCE_DEFAULT); |
| 3797 | restored.populateFromXmlForRestore(parser, getContext()); |
| 3798 | |
| 3799 | assertNull(restored.getSound()); |
| 3800 | } |
| 3801 | |
| 3802 | @Test |
Julia Reynolds | d78263d | 2018-01-30 10:40:41 -0500 | [diff] [blame] | 3803 | public void testBackup() throws Exception { |
| 3804 | int systemChecks = mService.countSystemChecks; |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 3805 | when(mListeners.queryPackageForServices(anyString(), anyInt(), anyInt())) |
| 3806 | .thenReturn(new ArraySet<>()); |
Julia Reynolds | d78263d | 2018-01-30 10:40:41 -0500 | [diff] [blame] | 3807 | mBinderService.getBackupPayload(1); |
| 3808 | assertEquals(1, mService.countSystemChecks - systemChecks); |
| 3809 | } |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3810 | |
| 3811 | @Test |
Julia Reynolds | 469144c | 2019-06-21 14:30:28 -0400 | [diff] [blame] | 3812 | public void testEmptyVibration_noException() throws Exception { |
| 3813 | NotificationChannel channel = new NotificationChannel("a", "ab", IMPORTANCE_DEFAULT); |
| 3814 | channel.setVibrationPattern(new long[0]); |
| 3815 | |
| 3816 | XmlSerializer serializer = new FastXmlSerializer(); |
| 3817 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 3818 | serializer.setOutput(new BufferedOutputStream(baos), "utf-8"); |
| 3819 | channel.writeXml(serializer); |
| 3820 | } |
| 3821 | |
| 3822 | @Test |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3823 | public void updateUriPermissions_update() throws Exception { |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3824 | NotificationChannel c = new NotificationChannel( |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 3825 | TEST_CHANNEL_ID, TEST_CHANNEL_ID, IMPORTANCE_DEFAULT); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3826 | c.setSound(null, Notification.AUDIO_ATTRIBUTES_DEFAULT); |
| 3827 | Message message1 = new Message("", 0, ""); |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3828 | message1.setData("", |
| 3829 | ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 1)); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3830 | Message message2 = new Message("", 1, ""); |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3831 | message2.setData("", |
| 3832 | ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 2)); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3833 | |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3834 | Notification.Builder nbA = new Notification.Builder(mContext, c.getId()) |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3835 | .setContentTitle("foo") |
| 3836 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 3837 | .setStyle(new Notification.MessagingStyle("") |
| 3838 | .addMessage(message1) |
| 3839 | .addMessage(message2)); |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3840 | NotificationRecord recordA = new NotificationRecord(mContext, new StatusBarNotification( |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 3841 | PKG, PKG, 0, "tag", mUid, 0, nbA.build(), UserHandle.getUserHandleForUid(mUid), |
| 3842 | null, 0), c); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3843 | |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3844 | // First post means we grant access to both |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 3845 | reset(mUgm); |
| 3846 | reset(mUgmInternal); |
| 3847 | when(mUgmInternal.newUriPermissionOwner(any())).thenReturn(new Binder()); |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3848 | mService.updateUriPermissions(recordA, null, mContext.getPackageName(), |
Julia Reynolds | 57a974b | 2019-10-07 11:51:47 -0400 | [diff] [blame] | 3849 | USER_SYSTEM); |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 3850 | verify(mUgm, times(1)).grantUriPermissionFromOwner(any(), anyInt(), any(), |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3851 | eq(message1.getDataUri()), anyInt(), anyInt(), anyInt()); |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 3852 | verify(mUgm, times(1)).grantUriPermissionFromOwner(any(), anyInt(), any(), |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3853 | eq(message2.getDataUri()), anyInt(), anyInt(), anyInt()); |
| 3854 | |
| 3855 | Notification.Builder nbB = new Notification.Builder(mContext, c.getId()) |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3856 | .setContentTitle("foo") |
| 3857 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 3858 | .setStyle(new Notification.MessagingStyle("").addMessage(message2)); |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3859 | NotificationRecord recordB = new NotificationRecord(mContext, new StatusBarNotification(PKG, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 3860 | PKG, 0, "tag", mUid, 0, nbB.build(), UserHandle.getUserHandleForUid(mUid), null, 0), |
| 3861 | c); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3862 | |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3863 | // Update means we drop access to first |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 3864 | reset(mUgmInternal); |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3865 | mService.updateUriPermissions(recordB, recordA, mContext.getPackageName(), |
Julia Reynolds | 57a974b | 2019-10-07 11:51:47 -0400 | [diff] [blame] | 3866 | USER_SYSTEM); |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 3867 | verify(mUgmInternal, times(1)).revokeUriPermissionFromOwner(any(), |
| 3868 | eq(message1.getDataUri()), anyInt(), anyInt()); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3869 | |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3870 | // Update back means we grant access to first again |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 3871 | reset(mUgm); |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3872 | mService.updateUriPermissions(recordA, recordB, mContext.getPackageName(), |
Julia Reynolds | 57a974b | 2019-10-07 11:51:47 -0400 | [diff] [blame] | 3873 | USER_SYSTEM); |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 3874 | verify(mUgm, times(1)).grantUriPermissionFromOwner(any(), anyInt(), any(), |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3875 | eq(message1.getDataUri()), anyInt(), anyInt(), anyInt()); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3876 | |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3877 | // And update to empty means we drop everything |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 3878 | reset(mUgmInternal); |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3879 | mService.updateUriPermissions(null, recordB, mContext.getPackageName(), |
Julia Reynolds | 57a974b | 2019-10-07 11:51:47 -0400 | [diff] [blame] | 3880 | USER_SYSTEM); |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 3881 | verify(mUgmInternal, times(1)).revokeUriPermissionFromOwner(any(), eq(null), |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3882 | anyInt(), anyInt()); |
| 3883 | } |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 3884 | |
| 3885 | @Test |
Julia Reynolds | ea58c20 | 2020-02-03 12:52:49 -0500 | [diff] [blame] | 3886 | public void updateUriPermissions_posterDoesNotOwnUri() throws Exception { |
| 3887 | NotificationChannel c = new NotificationChannel( |
| 3888 | TEST_CHANNEL_ID, TEST_CHANNEL_ID, IMPORTANCE_DEFAULT); |
| 3889 | c.setSound(null, Notification.AUDIO_ATTRIBUTES_DEFAULT); |
| 3890 | Message message1 = new Message("", 0, ""); |
| 3891 | message1.setData("", |
| 3892 | ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 1)); |
| 3893 | |
| 3894 | Notification.Builder nbA = new Notification.Builder(mContext, c.getId()) |
| 3895 | .setContentTitle("foo") |
| 3896 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 3897 | .setStyle(new Notification.MessagingStyle("") |
| 3898 | .addMessage(message1)); |
| 3899 | NotificationRecord recordA = new NotificationRecord(mContext, new StatusBarNotification( |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 3900 | PKG, PKG, 0, "tag", mUid, 0, nbA.build(), UserHandle.getUserHandleForUid(mUid), |
| 3901 | null, 0), c); |
Julia Reynolds | ea58c20 | 2020-02-03 12:52:49 -0500 | [diff] [blame] | 3902 | |
| 3903 | doThrow(new SecurityException("no access")).when(mUgm) |
| 3904 | .grantUriPermissionFromOwner( |
| 3905 | any(), anyInt(), any(), any(), anyInt(), anyInt(), anyInt()); |
| 3906 | |
| 3907 | when(mUgmInternal.newUriPermissionOwner(any())).thenReturn(new Binder()); |
| 3908 | mService.updateUriPermissions(recordA, null, mContext.getPackageName(), USER_SYSTEM); |
| 3909 | |
| 3910 | // yay, no crash |
| 3911 | } |
| 3912 | |
| 3913 | @Test |
Robin Lee | d107af6 | 2018-04-27 13:55:56 +0200 | [diff] [blame] | 3914 | public void testVisitUris() throws Exception { |
| 3915 | final Uri audioContents = Uri.parse("content://com.example/audio"); |
| 3916 | final Uri backgroundImage = Uri.parse("content://com.example/background"); |
| 3917 | |
| 3918 | Bundle extras = new Bundle(); |
| 3919 | extras.putParcelable(Notification.EXTRA_AUDIO_CONTENTS_URI, audioContents); |
| 3920 | extras.putString(Notification.EXTRA_BACKGROUND_IMAGE_URI, backgroundImage.toString()); |
| 3921 | |
| 3922 | Notification n = new Notification.Builder(mContext, "a") |
| 3923 | .setContentTitle("notification with uris") |
| 3924 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 3925 | .addExtras(extras) |
| 3926 | .build(); |
| 3927 | |
| 3928 | Consumer<Uri> visitor = (Consumer<Uri>) spy(Consumer.class); |
| 3929 | n.visitUris(visitor); |
| 3930 | verify(visitor, times(1)).accept(eq(audioContents)); |
| 3931 | verify(visitor, times(1)).accept(eq(backgroundImage)); |
| 3932 | } |
| 3933 | |
| 3934 | @Test |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 3935 | public void testSetNotificationPolicy_preP_setOldFields() { |
| 3936 | ZenModeHelper mZenModeHelper = mock(ZenModeHelper.class); |
| 3937 | mService.mZenModeHelper = mZenModeHelper; |
| 3938 | NotificationManager.Policy userPolicy = |
| 3939 | new NotificationManager.Policy(0, 0, 0, SUPPRESSED_EFFECT_BADGE); |
| 3940 | when(mZenModeHelper.getNotificationPolicy()).thenReturn(userPolicy); |
| 3941 | |
| 3942 | NotificationManager.Policy appPolicy = new NotificationManager.Policy(0, 0, 0, |
| 3943 | SUPPRESSED_EFFECT_SCREEN_ON | SUPPRESSED_EFFECT_SCREEN_OFF); |
| 3944 | |
| 3945 | int expected = SUPPRESSED_EFFECT_BADGE |
| 3946 | | SUPPRESSED_EFFECT_SCREEN_ON | SUPPRESSED_EFFECT_SCREEN_OFF |
Julia Reynolds | eac2da2 | 2018-04-12 10:48:46 -0400 | [diff] [blame] | 3947 | | SUPPRESSED_EFFECT_PEEK | SUPPRESSED_EFFECT_LIGHTS |
| 3948 | | SUPPRESSED_EFFECT_FULL_SCREEN_INTENT; |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 3949 | int actual = mService.calculateSuppressedVisualEffects(appPolicy, userPolicy, O_MR1); |
| 3950 | |
| 3951 | assertEquals(expected, actual); |
| 3952 | } |
| 3953 | |
| 3954 | @Test |
| 3955 | public void testSetNotificationPolicy_preP_setNewFields() { |
| 3956 | ZenModeHelper mZenModeHelper = mock(ZenModeHelper.class); |
| 3957 | mService.mZenModeHelper = mZenModeHelper; |
| 3958 | NotificationManager.Policy userPolicy = |
| 3959 | new NotificationManager.Policy(0, 0, 0, SUPPRESSED_EFFECT_BADGE); |
| 3960 | when(mZenModeHelper.getNotificationPolicy()).thenReturn(userPolicy); |
| 3961 | |
| 3962 | NotificationManager.Policy appPolicy = new NotificationManager.Policy(0, 0, 0, |
| 3963 | SUPPRESSED_EFFECT_NOTIFICATION_LIST); |
| 3964 | |
| 3965 | int expected = SUPPRESSED_EFFECT_BADGE; |
| 3966 | int actual = mService.calculateSuppressedVisualEffects(appPolicy, userPolicy, O_MR1); |
| 3967 | |
| 3968 | assertEquals(expected, actual); |
| 3969 | } |
| 3970 | |
| 3971 | @Test |
| 3972 | public void testSetNotificationPolicy_preP_setOldNewFields() { |
| 3973 | ZenModeHelper mZenModeHelper = mock(ZenModeHelper.class); |
| 3974 | mService.mZenModeHelper = mZenModeHelper; |
| 3975 | NotificationManager.Policy userPolicy = |
| 3976 | new NotificationManager.Policy(0, 0, 0, SUPPRESSED_EFFECT_BADGE); |
| 3977 | when(mZenModeHelper.getNotificationPolicy()).thenReturn(userPolicy); |
| 3978 | |
| 3979 | NotificationManager.Policy appPolicy = new NotificationManager.Policy(0, 0, 0, |
| 3980 | SUPPRESSED_EFFECT_SCREEN_ON | SUPPRESSED_EFFECT_STATUS_BAR); |
| 3981 | |
| 3982 | int expected = |
| 3983 | SUPPRESSED_EFFECT_BADGE | SUPPRESSED_EFFECT_SCREEN_ON | SUPPRESSED_EFFECT_PEEK; |
| 3984 | int actual = mService.calculateSuppressedVisualEffects(appPolicy, userPolicy, O_MR1); |
| 3985 | |
| 3986 | assertEquals(expected, actual); |
| 3987 | } |
| 3988 | |
| 3989 | @Test |
| 3990 | public void testSetNotificationPolicy_P_setOldFields() { |
| 3991 | ZenModeHelper mZenModeHelper = mock(ZenModeHelper.class); |
| 3992 | mService.mZenModeHelper = mZenModeHelper; |
| 3993 | NotificationManager.Policy userPolicy = |
| 3994 | new NotificationManager.Policy(0, 0, 0, SUPPRESSED_EFFECT_BADGE); |
| 3995 | when(mZenModeHelper.getNotificationPolicy()).thenReturn(userPolicy); |
| 3996 | |
| 3997 | NotificationManager.Policy appPolicy = new NotificationManager.Policy(0, 0, 0, |
| 3998 | SUPPRESSED_EFFECT_SCREEN_ON | SUPPRESSED_EFFECT_SCREEN_OFF); |
| 3999 | |
| 4000 | int expected = SUPPRESSED_EFFECT_SCREEN_ON | SUPPRESSED_EFFECT_SCREEN_OFF |
| 4001 | | SUPPRESSED_EFFECT_PEEK | SUPPRESSED_EFFECT_AMBIENT |
| 4002 | | SUPPRESSED_EFFECT_LIGHTS | SUPPRESSED_EFFECT_FULL_SCREEN_INTENT; |
| 4003 | int actual = mService.calculateSuppressedVisualEffects(appPolicy, userPolicy, P); |
| 4004 | |
| 4005 | assertEquals(expected, actual); |
| 4006 | } |
| 4007 | |
| 4008 | @Test |
| 4009 | public void testSetNotificationPolicy_P_setNewFields() { |
| 4010 | ZenModeHelper mZenModeHelper = mock(ZenModeHelper.class); |
| 4011 | mService.mZenModeHelper = mZenModeHelper; |
| 4012 | NotificationManager.Policy userPolicy = |
| 4013 | new NotificationManager.Policy(0, 0, 0, SUPPRESSED_EFFECT_BADGE); |
| 4014 | when(mZenModeHelper.getNotificationPolicy()).thenReturn(userPolicy); |
| 4015 | |
| 4016 | NotificationManager.Policy appPolicy = new NotificationManager.Policy(0, 0, 0, |
| 4017 | SUPPRESSED_EFFECT_NOTIFICATION_LIST | SUPPRESSED_EFFECT_AMBIENT |
| 4018 | | SUPPRESSED_EFFECT_LIGHTS | SUPPRESSED_EFFECT_FULL_SCREEN_INTENT); |
| 4019 | |
| 4020 | int expected = SUPPRESSED_EFFECT_NOTIFICATION_LIST | SUPPRESSED_EFFECT_SCREEN_OFF |
| 4021 | | SUPPRESSED_EFFECT_AMBIENT | SUPPRESSED_EFFECT_LIGHTS |
| 4022 | | SUPPRESSED_EFFECT_FULL_SCREEN_INTENT; |
| 4023 | int actual = mService.calculateSuppressedVisualEffects(appPolicy, userPolicy, P); |
| 4024 | |
| 4025 | assertEquals(expected, actual); |
| 4026 | } |
| 4027 | |
| 4028 | @Test |
| 4029 | public void testSetNotificationPolicy_P_setOldNewFields() { |
| 4030 | ZenModeHelper mZenModeHelper = mock(ZenModeHelper.class); |
| 4031 | mService.mZenModeHelper = mZenModeHelper; |
| 4032 | NotificationManager.Policy userPolicy = |
| 4033 | new NotificationManager.Policy(0, 0, 0, SUPPRESSED_EFFECT_BADGE); |
| 4034 | when(mZenModeHelper.getNotificationPolicy()).thenReturn(userPolicy); |
| 4035 | |
| 4036 | NotificationManager.Policy appPolicy = new NotificationManager.Policy(0, 0, 0, |
| 4037 | SUPPRESSED_EFFECT_SCREEN_ON | SUPPRESSED_EFFECT_STATUS_BAR); |
| 4038 | |
| 4039 | int expected = SUPPRESSED_EFFECT_STATUS_BAR; |
| 4040 | int actual = mService.calculateSuppressedVisualEffects(appPolicy, userPolicy, P); |
| 4041 | |
| 4042 | assertEquals(expected, actual); |
| 4043 | |
| 4044 | appPolicy = new NotificationManager.Policy(0, 0, 0, |
| 4045 | SUPPRESSED_EFFECT_SCREEN_ON | SUPPRESSED_EFFECT_AMBIENT |
| 4046 | | SUPPRESSED_EFFECT_LIGHTS | SUPPRESSED_EFFECT_FULL_SCREEN_INTENT); |
| 4047 | |
| 4048 | expected = SUPPRESSED_EFFECT_SCREEN_OFF | SUPPRESSED_EFFECT_AMBIENT |
| 4049 | | SUPPRESSED_EFFECT_LIGHTS | SUPPRESSED_EFFECT_FULL_SCREEN_INTENT; |
| 4050 | actual = mService.calculateSuppressedVisualEffects(appPolicy, userPolicy, P); |
| 4051 | |
| 4052 | assertEquals(expected, actual); |
| 4053 | } |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4054 | |
| 4055 | @Test |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 4056 | public void testVisualDifference_foreground() { |
| 4057 | Notification.Builder nb1 = new Notification.Builder(mContext, "") |
| 4058 | .setContentTitle("foo"); |
| 4059 | StatusBarNotification sbn1 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 4060 | nb1.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 4061 | NotificationRecord r1 = |
| 4062 | new NotificationRecord(mContext, sbn1, mock(NotificationChannel.class)); |
| 4063 | |
| 4064 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 4065 | .setFlag(FLAG_FOREGROUND_SERVICE, true) |
| 4066 | .setContentTitle("bar"); |
| 4067 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 4068 | nb2.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 4069 | NotificationRecord r2 = |
| 4070 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 4071 | |
| 4072 | assertFalse(mService.isVisuallyInterruptive(r1, r2)); |
| 4073 | } |
| 4074 | |
| 4075 | @Test |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4076 | public void testVisualDifference_diffTitle() { |
| 4077 | Notification.Builder nb1 = new Notification.Builder(mContext, "") |
| 4078 | .setContentTitle("foo"); |
| 4079 | StatusBarNotification sbn1 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 4080 | nb1.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4081 | NotificationRecord r1 = |
| 4082 | new NotificationRecord(mContext, sbn1, mock(NotificationChannel.class)); |
| 4083 | |
| 4084 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 4085 | .setContentTitle("bar"); |
| 4086 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 4087 | nb2.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4088 | NotificationRecord r2 = |
| 4089 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 4090 | |
| 4091 | assertTrue(mService.isVisuallyInterruptive(r1, r2)); |
| 4092 | } |
| 4093 | |
| 4094 | @Test |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4095 | public void testVisualDifference_inboxStyle() { |
| 4096 | Notification.Builder nb1 = new Notification.Builder(mContext, "") |
| 4097 | .setStyle(new Notification.InboxStyle() |
| 4098 | .addLine("line1").addLine("line2")); |
| 4099 | StatusBarNotification sbn1 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 4100 | nb1.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4101 | NotificationRecord r1 = |
| 4102 | new NotificationRecord(mContext, sbn1, mock(NotificationChannel.class)); |
| 4103 | |
| 4104 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 4105 | .setStyle(new Notification.InboxStyle() |
| 4106 | .addLine("line1").addLine("line2_changed")); |
| 4107 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 4108 | nb2.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4109 | NotificationRecord r2 = |
| 4110 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 4111 | |
| 4112 | assertTrue(mService.isVisuallyInterruptive(r1, r2)); // line 2 changed unnoticed |
| 4113 | |
| 4114 | Notification.Builder nb3 = new Notification.Builder(mContext, "") |
| 4115 | .setStyle(new Notification.InboxStyle() |
| 4116 | .addLine("line1")); |
| 4117 | StatusBarNotification sbn3 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 4118 | nb3.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4119 | NotificationRecord r3 = |
| 4120 | new NotificationRecord(mContext, sbn3, mock(NotificationChannel.class)); |
| 4121 | |
| 4122 | assertTrue(mService.isVisuallyInterruptive(r1, r3)); // line 2 removed unnoticed |
| 4123 | |
| 4124 | Notification.Builder nb4 = new Notification.Builder(mContext, "") |
| 4125 | .setStyle(new Notification.InboxStyle() |
| 4126 | .addLine("line1").addLine("line2").addLine("line3")); |
| 4127 | StatusBarNotification sbn4 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 4128 | nb4.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4129 | NotificationRecord r4 = |
| 4130 | new NotificationRecord(mContext, sbn4, mock(NotificationChannel.class)); |
| 4131 | |
| 4132 | assertTrue(mService.isVisuallyInterruptive(r1, r4)); // line 3 added unnoticed |
| 4133 | |
| 4134 | Notification.Builder nb5 = new Notification.Builder(mContext, "") |
| 4135 | .setContentText("not an inbox"); |
| 4136 | StatusBarNotification sbn5 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 4137 | nb5.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4138 | NotificationRecord r5 = |
| 4139 | new NotificationRecord(mContext, sbn5, mock(NotificationChannel.class)); |
| 4140 | |
| 4141 | assertTrue(mService.isVisuallyInterruptive(r1, r5)); // changed Styles, went unnoticed |
| 4142 | } |
| 4143 | |
| 4144 | @Test |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4145 | public void testVisualDifference_diffText() { |
| 4146 | Notification.Builder nb1 = new Notification.Builder(mContext, "") |
| 4147 | .setContentText("foo"); |
| 4148 | StatusBarNotification sbn1 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 4149 | nb1.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4150 | NotificationRecord r1 = |
| 4151 | new NotificationRecord(mContext, sbn1, mock(NotificationChannel.class)); |
| 4152 | |
| 4153 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 4154 | .setContentText("bar"); |
| 4155 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 4156 | nb2.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4157 | NotificationRecord r2 = |
| 4158 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 4159 | |
| 4160 | assertTrue(mService.isVisuallyInterruptive(r1, r2)); |
| 4161 | } |
| 4162 | |
| 4163 | @Test |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4164 | public void testVisualDifference_sameText() { |
| 4165 | Notification.Builder nb1 = new Notification.Builder(mContext, "") |
| 4166 | .setContentText("foo"); |
| 4167 | StatusBarNotification sbn1 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 4168 | nb1.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4169 | NotificationRecord r1 = |
| 4170 | new NotificationRecord(mContext, sbn1, mock(NotificationChannel.class)); |
| 4171 | |
| 4172 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 4173 | .setContentText("foo"); |
| 4174 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 4175 | nb2.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4176 | NotificationRecord r2 = |
| 4177 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 4178 | |
| 4179 | assertFalse(mService.isVisuallyInterruptive(r1, r2)); |
| 4180 | } |
| 4181 | |
| 4182 | @Test |
| 4183 | public void testVisualDifference_sameTextButStyled() { |
| 4184 | Notification.Builder nb1 = new Notification.Builder(mContext, "") |
| 4185 | .setContentText(Html.fromHtml("<b>foo</b>")); |
| 4186 | StatusBarNotification sbn1 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 4187 | nb1.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4188 | NotificationRecord r1 = |
| 4189 | new NotificationRecord(mContext, sbn1, mock(NotificationChannel.class)); |
| 4190 | |
| 4191 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 4192 | .setContentText(Html.fromHtml("<b>foo</b>")); |
| 4193 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 4194 | nb2.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4195 | NotificationRecord r2 = |
| 4196 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 4197 | |
| 4198 | assertFalse(mService.isVisuallyInterruptive(r1, r2)); |
| 4199 | } |
| 4200 | |
| 4201 | @Test |
| 4202 | public void testVisualDifference_diffTextButStyled() { |
| 4203 | Notification.Builder nb1 = new Notification.Builder(mContext, "") |
| 4204 | .setContentText(Html.fromHtml("<b>foo</b>")); |
| 4205 | StatusBarNotification sbn1 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 4206 | nb1.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4207 | NotificationRecord r1 = |
| 4208 | new NotificationRecord(mContext, sbn1, mock(NotificationChannel.class)); |
| 4209 | |
| 4210 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 4211 | .setContentText(Html.fromHtml("<b>bar</b>")); |
| 4212 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 4213 | nb2.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4214 | NotificationRecord r2 = |
| 4215 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 4216 | |
| 4217 | assertTrue(mService.isVisuallyInterruptive(r1, r2)); |
| 4218 | } |
| 4219 | |
| 4220 | @Test |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4221 | public void testVisualDifference_diffProgress() { |
| 4222 | Notification.Builder nb1 = new Notification.Builder(mContext, "") |
| 4223 | .setProgress(100, 90, false); |
| 4224 | StatusBarNotification sbn1 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 4225 | nb1.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4226 | NotificationRecord r1 = |
| 4227 | new NotificationRecord(mContext, sbn1, mock(NotificationChannel.class)); |
| 4228 | |
| 4229 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 4230 | .setProgress(100, 100, false); |
| 4231 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 4232 | nb2.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4233 | NotificationRecord r2 = |
| 4234 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 4235 | |
| 4236 | assertTrue(mService.isVisuallyInterruptive(r1, r2)); |
| 4237 | } |
| 4238 | |
| 4239 | @Test |
| 4240 | public void testVisualDifference_diffProgressNotDone() { |
| 4241 | Notification.Builder nb1 = new Notification.Builder(mContext, "") |
| 4242 | .setProgress(100, 90, false); |
| 4243 | StatusBarNotification sbn1 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 4244 | nb1.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4245 | NotificationRecord r1 = |
| 4246 | new NotificationRecord(mContext, sbn1, mock(NotificationChannel.class)); |
| 4247 | |
| 4248 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 4249 | .setProgress(100, 91, false); |
| 4250 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 4251 | nb2.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4252 | NotificationRecord r2 = |
| 4253 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 4254 | |
| 4255 | assertFalse(mService.isVisuallyInterruptive(r1, r2)); |
| 4256 | } |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 4257 | |
| 4258 | @Test |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4259 | public void testVisualDifference_sameProgressStillDone() { |
| 4260 | Notification.Builder nb1 = new Notification.Builder(mContext, "") |
| 4261 | .setProgress(100, 100, false); |
| 4262 | StatusBarNotification sbn1 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 4263 | nb1.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4264 | NotificationRecord r1 = |
| 4265 | new NotificationRecord(mContext, sbn1, mock(NotificationChannel.class)); |
| 4266 | |
| 4267 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 4268 | .setProgress(100, 100, false); |
| 4269 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 4270 | nb2.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4271 | NotificationRecord r2 = |
| 4272 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 4273 | |
| 4274 | assertFalse(mService.isVisuallyInterruptive(r1, r2)); |
| 4275 | } |
| 4276 | |
| 4277 | @Test |
Julia Reynolds | a4fb9da | 2018-06-04 12:27:58 -0400 | [diff] [blame] | 4278 | public void testVisualDifference_summary() { |
| 4279 | Notification.Builder nb1 = new Notification.Builder(mContext, "") |
| 4280 | .setGroup("bananas") |
| 4281 | .setFlag(Notification.FLAG_GROUP_SUMMARY, true) |
| 4282 | .setContentText("foo"); |
| 4283 | StatusBarNotification sbn1 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 4284 | nb1.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Julia Reynolds | a4fb9da | 2018-06-04 12:27:58 -0400 | [diff] [blame] | 4285 | NotificationRecord r1 = |
| 4286 | new NotificationRecord(mContext, sbn1, mock(NotificationChannel.class)); |
| 4287 | |
| 4288 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 4289 | .setGroup("bananas") |
| 4290 | .setFlag(Notification.FLAG_GROUP_SUMMARY, true) |
| 4291 | .setContentText("bar"); |
| 4292 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 4293 | nb2.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Julia Reynolds | a4fb9da | 2018-06-04 12:27:58 -0400 | [diff] [blame] | 4294 | NotificationRecord r2 = |
| 4295 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 4296 | |
| 4297 | assertFalse(mService.isVisuallyInterruptive(r1, r2)); |
| 4298 | } |
| 4299 | |
| 4300 | @Test |
Julia Reynolds | 760fa76 | 2018-06-19 15:39:23 -0400 | [diff] [blame] | 4301 | public void testVisualDifference_summaryNewNotification() { |
| 4302 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 4303 | .setGroup("bananas") |
| 4304 | .setFlag(Notification.FLAG_GROUP_SUMMARY, true) |
| 4305 | .setContentText("bar"); |
| 4306 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 4307 | nb2.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Julia Reynolds | 760fa76 | 2018-06-19 15:39:23 -0400 | [diff] [blame] | 4308 | NotificationRecord r2 = |
| 4309 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 4310 | |
| 4311 | assertFalse(mService.isVisuallyInterruptive(null, r2)); |
| 4312 | } |
| 4313 | |
| 4314 | @Test |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 4315 | public void testHideAndUnhideNotificationsOnSuspendedPackageBroadcast() { |
| 4316 | // post 2 notification from this package |
| 4317 | final NotificationRecord notif1 = generateNotificationRecord( |
| 4318 | mTestNotificationChannel, 1, null, true); |
| 4319 | final NotificationRecord notif2 = generateNotificationRecord( |
| 4320 | mTestNotificationChannel, 2, null, false); |
| 4321 | mService.addNotification(notif1); |
| 4322 | mService.addNotification(notif2); |
| 4323 | |
| 4324 | // on broadcast, hide the 2 notifications |
| 4325 | mService.simulatePackageSuspendBroadcast(true, PKG); |
| 4326 | ArgumentCaptor<List> captorHide = ArgumentCaptor.forClass(List.class); |
| 4327 | verify(mListeners, times(1)).notifyHiddenLocked(captorHide.capture()); |
| 4328 | assertEquals(2, captorHide.getValue().size()); |
| 4329 | |
| 4330 | // on broadcast, unhide the 2 notifications |
| 4331 | mService.simulatePackageSuspendBroadcast(false, PKG); |
| 4332 | ArgumentCaptor<List> captorUnhide = ArgumentCaptor.forClass(List.class); |
| 4333 | verify(mListeners, times(1)).notifyUnhiddenLocked(captorUnhide.capture()); |
| 4334 | assertEquals(2, captorUnhide.getValue().size()); |
| 4335 | } |
| 4336 | |
| 4337 | @Test |
| 4338 | public void testNoNotificationsHiddenOnSuspendedPackageBroadcast() { |
| 4339 | // post 2 notification from this package |
| 4340 | final NotificationRecord notif1 = generateNotificationRecord( |
| 4341 | mTestNotificationChannel, 1, null, true); |
| 4342 | final NotificationRecord notif2 = generateNotificationRecord( |
| 4343 | mTestNotificationChannel, 2, null, false); |
| 4344 | mService.addNotification(notif1); |
| 4345 | mService.addNotification(notif2); |
| 4346 | |
| 4347 | // on broadcast, nothing is hidden since no notifications are of package "test_package" |
| 4348 | mService.simulatePackageSuspendBroadcast(true, "test_package"); |
| 4349 | ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class); |
| 4350 | verify(mListeners, times(1)).notifyHiddenLocked(captor.capture()); |
| 4351 | assertEquals(0, captor.getValue().size()); |
| 4352 | } |
Kristian Monsen | 05f3479 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 4353 | |
| 4354 | @Test |
Julia Reynolds | 0e5a343 | 2019-01-17 09:40:46 -0500 | [diff] [blame] | 4355 | public void testHideAndUnhideNotificationsOnDistractingPackageBroadcast() { |
| 4356 | // Post 2 notifications from 2 packages |
| 4357 | NotificationRecord pkgA = new NotificationRecord(mContext, |
| 4358 | generateSbn("a", 1000, 9, 0), mTestNotificationChannel); |
| 4359 | mService.addNotification(pkgA); |
| 4360 | NotificationRecord pkgB = new NotificationRecord(mContext, |
| 4361 | generateSbn("b", 1001, 9, 0), mTestNotificationChannel); |
| 4362 | mService.addNotification(pkgB); |
| 4363 | |
| 4364 | // on broadcast, hide one of the packages |
| 4365 | mService.simulatePackageDistractionBroadcast( |
| 4366 | PackageManager.RESTRICTION_HIDE_NOTIFICATIONS, new String[] {"a"}); |
| 4367 | ArgumentCaptor<List<NotificationRecord>> captorHide = ArgumentCaptor.forClass(List.class); |
| 4368 | verify(mListeners, times(1)).notifyHiddenLocked(captorHide.capture()); |
| 4369 | assertEquals(1, captorHide.getValue().size()); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 4370 | assertEquals("a", captorHide.getValue().get(0).getSbn().getPackageName()); |
Julia Reynolds | 0e5a343 | 2019-01-17 09:40:46 -0500 | [diff] [blame] | 4371 | |
| 4372 | // on broadcast, unhide the package |
| 4373 | mService.simulatePackageDistractionBroadcast( |
| 4374 | PackageManager.RESTRICTION_HIDE_FROM_SUGGESTIONS, new String[] {"a"}); |
| 4375 | ArgumentCaptor<List<NotificationRecord>> captorUnhide = ArgumentCaptor.forClass(List.class); |
| 4376 | verify(mListeners, times(1)).notifyUnhiddenLocked(captorUnhide.capture()); |
| 4377 | assertEquals(1, captorUnhide.getValue().size()); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 4378 | assertEquals("a", captorUnhide.getValue().get(0).getSbn().getPackageName()); |
Julia Reynolds | 0e5a343 | 2019-01-17 09:40:46 -0500 | [diff] [blame] | 4379 | } |
| 4380 | |
| 4381 | @Test |
| 4382 | public void testHideAndUnhideNotificationsOnDistractingPackageBroadcast_multiPkg() { |
| 4383 | // Post 2 notifications from 2 packages |
| 4384 | NotificationRecord pkgA = new NotificationRecord(mContext, |
| 4385 | generateSbn("a", 1000, 9, 0), mTestNotificationChannel); |
| 4386 | mService.addNotification(pkgA); |
| 4387 | NotificationRecord pkgB = new NotificationRecord(mContext, |
| 4388 | generateSbn("b", 1001, 9, 0), mTestNotificationChannel); |
| 4389 | mService.addNotification(pkgB); |
| 4390 | |
| 4391 | // on broadcast, hide one of the packages |
| 4392 | mService.simulatePackageDistractionBroadcast( |
| 4393 | PackageManager.RESTRICTION_HIDE_NOTIFICATIONS, new String[] {"a", "b"}); |
| 4394 | ArgumentCaptor<List<NotificationRecord>> captorHide = ArgumentCaptor.forClass(List.class); |
woongki min | bbc9728 | 2019-09-25 11:50:48 +0900 | [diff] [blame] | 4395 | |
| 4396 | // should be called only once. |
| 4397 | verify(mListeners, times(1)).notifyHiddenLocked(captorHide.capture()); |
Julia Reynolds | 0e5a343 | 2019-01-17 09:40:46 -0500 | [diff] [blame] | 4398 | assertEquals(2, captorHide.getValue().size()); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 4399 | assertEquals("a", captorHide.getValue().get(0).getSbn().getPackageName()); |
| 4400 | assertEquals("b", captorHide.getValue().get(1).getSbn().getPackageName()); |
Julia Reynolds | 0e5a343 | 2019-01-17 09:40:46 -0500 | [diff] [blame] | 4401 | |
| 4402 | // on broadcast, unhide the package |
| 4403 | mService.simulatePackageDistractionBroadcast( |
| 4404 | PackageManager.RESTRICTION_HIDE_FROM_SUGGESTIONS, new String[] {"a", "b"}); |
| 4405 | ArgumentCaptor<List<NotificationRecord>> captorUnhide = ArgumentCaptor.forClass(List.class); |
woongki min | bbc9728 | 2019-09-25 11:50:48 +0900 | [diff] [blame] | 4406 | |
| 4407 | // should be called only once. |
| 4408 | verify(mListeners, times(1)).notifyUnhiddenLocked(captorUnhide.capture()); |
Julia Reynolds | 0e5a343 | 2019-01-17 09:40:46 -0500 | [diff] [blame] | 4409 | assertEquals(2, captorUnhide.getValue().size()); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 4410 | assertEquals("a", captorUnhide.getValue().get(0).getSbn().getPackageName()); |
| 4411 | assertEquals("b", captorUnhide.getValue().get(1).getSbn().getPackageName()); |
Julia Reynolds | 0e5a343 | 2019-01-17 09:40:46 -0500 | [diff] [blame] | 4412 | } |
| 4413 | |
| 4414 | @Test |
| 4415 | public void testNoNotificationsHiddenOnDistractingPackageBroadcast() { |
| 4416 | // post notification from this package |
| 4417 | final NotificationRecord notif1 = generateNotificationRecord( |
| 4418 | mTestNotificationChannel, 1, null, true); |
| 4419 | mService.addNotification(notif1); |
| 4420 | |
| 4421 | // on broadcast, nothing is hidden since no notifications are of package "test_package" |
| 4422 | mService.simulatePackageDistractionBroadcast( |
| 4423 | PackageManager.RESTRICTION_HIDE_NOTIFICATIONS, new String[] {"test_package"}); |
| 4424 | ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class); |
| 4425 | verify(mListeners, times(1)).notifyHiddenLocked(captor.capture()); |
| 4426 | assertEquals(0, captor.getValue().size()); |
| 4427 | } |
| 4428 | |
| 4429 | @Test |
Rajeev Kumar | bc79dcd | 2020-02-04 19:54:30 -0800 | [diff] [blame] | 4430 | public void testCanUseManagedServicesNullPkg() { |
| 4431 | assertEquals(true, mService.canUseManagedServices(null, 0, null)); |
Kristian Monsen | 05f3479 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 4432 | } |
| 4433 | |
Kristian Monsen | 05f3479 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 4434 | |
| 4435 | @Test |
Rajeev Kumar | bc79dcd | 2020-02-04 19:54:30 -0800 | [diff] [blame] | 4436 | public void testCanUseManagedServicesNoValidPkg() { |
Julia Reynolds | d0ceefa | 2019-03-03 16:10:52 -0500 | [diff] [blame] | 4437 | assertEquals(true, mService.canUseManagedServices("d", 0, null)); |
| 4438 | } |
| 4439 | |
| 4440 | @Test |
| 4441 | public void testCanUseManagedServices_hasPermission() throws Exception { |
| 4442 | when(mPackageManager.checkPermission("perm", "pkg", 0)) |
| 4443 | .thenReturn(PackageManager.PERMISSION_GRANTED); |
| 4444 | |
| 4445 | assertEquals(true, mService.canUseManagedServices("pkg", 0, "perm")); |
| 4446 | } |
| 4447 | |
| 4448 | @Test |
| 4449 | public void testCanUseManagedServices_noPermission() throws Exception { |
| 4450 | when(mPackageManager.checkPermission("perm", "pkg", 0)) |
| 4451 | .thenReturn(PackageManager.PERMISSION_DENIED); |
| 4452 | |
| 4453 | assertEquals(false, mService.canUseManagedServices("pkg", 0, "perm")); |
| 4454 | } |
| 4455 | |
| 4456 | @Test |
| 4457 | public void testCanUseManagedServices_permDoesNotMatter() { |
| 4458 | assertEquals(true, mService.canUseManagedServices("pkg", 0, null)); |
Kristian Monsen | 05f3479 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 4459 | } |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 4460 | |
| 4461 | @Test |
| 4462 | public void testOnNotificationVisibilityChanged_triggersInterruptionUsageStat() { |
| 4463 | final NotificationRecord r = generateNotificationRecord( |
| 4464 | mTestNotificationChannel, 1, null, true); |
| 4465 | r.setTextChanged(true); |
| 4466 | mService.addNotification(r); |
| 4467 | |
| 4468 | mService.mNotificationDelegate.onNotificationVisibilityChanged(new NotificationVisibility[] |
| 4469 | {NotificationVisibility.obtain(r.getKey(), 1, 1, true)}, |
| 4470 | new NotificationVisibility[]{}); |
| 4471 | |
| 4472 | verify(mAppUsageStats).reportInterruptiveNotification(anyString(), anyString(), anyInt()); |
| 4473 | } |
| 4474 | |
| 4475 | @Test |
Will Brockman | d3d4933 | 2020-02-10 19:43:03 -0500 | [diff] [blame] | 4476 | public void testOnNotificationVisibilityChanged_triggersVisibilityLog() { |
| 4477 | final NotificationRecord r = generateNotificationRecord( |
| 4478 | mTestNotificationChannel, 1, null, true); |
| 4479 | r.setTextChanged(true); |
Will Brockman | 4f0f9d2 | 2020-02-23 21:18:11 -0500 | [diff] [blame] | 4480 | r.getSbn().setInstanceId(mNotificationInstanceIdSequence.newInstanceId()); |
Will Brockman | d3d4933 | 2020-02-10 19:43:03 -0500 | [diff] [blame] | 4481 | mService.addNotification(r); |
| 4482 | |
| 4483 | mService.mNotificationDelegate.onNotificationVisibilityChanged(new NotificationVisibility[] |
| 4484 | {NotificationVisibility.obtain(r.getKey(), 1, 1, true)}, |
| 4485 | new NotificationVisibility[]{}); |
| 4486 | |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 4487 | assertEquals(1, mNotificationRecordLogger.numCalls()); |
Will Brockman | d3d4933 | 2020-02-10 19:43:03 -0500 | [diff] [blame] | 4488 | assertEquals(NotificationRecordLogger.NotificationEvent.NOTIFICATION_OPEN, |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 4489 | mNotificationRecordLogger.event(0)); |
Will Brockman | 4f0f9d2 | 2020-02-23 21:18:11 -0500 | [diff] [blame] | 4490 | assertEquals(1, mNotificationRecordLogger.get(0).getInstanceId()); |
Will Brockman | d3d4933 | 2020-02-10 19:43:03 -0500 | [diff] [blame] | 4491 | |
| 4492 | mService.mNotificationDelegate.onNotificationVisibilityChanged( |
| 4493 | new NotificationVisibility[]{}, |
| 4494 | new NotificationVisibility[] |
| 4495 | {NotificationVisibility.obtain(r.getKey(), 1, 1, true)} |
| 4496 | ); |
| 4497 | |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 4498 | assertEquals(2, mNotificationRecordLogger.numCalls()); |
Will Brockman | d3d4933 | 2020-02-10 19:43:03 -0500 | [diff] [blame] | 4499 | assertEquals(NotificationRecordLogger.NotificationEvent.NOTIFICATION_CLOSE, |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 4500 | mNotificationRecordLogger.event(1)); |
Will Brockman | 4f0f9d2 | 2020-02-23 21:18:11 -0500 | [diff] [blame] | 4501 | assertEquals(1, mNotificationRecordLogger.get(1).getInstanceId()); |
Will Brockman | d3d4933 | 2020-02-10 19:43:03 -0500 | [diff] [blame] | 4502 | } |
| 4503 | |
| 4504 | @Test |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 4505 | public void testSetNotificationsShownFromListener_triggersInterruptionUsageStat() |
| 4506 | throws RemoteException { |
| 4507 | final NotificationRecord r = generateNotificationRecord( |
| 4508 | mTestNotificationChannel, 1, null, true); |
| 4509 | r.setTextChanged(true); |
| 4510 | mService.addNotification(r); |
| 4511 | |
| 4512 | mBinderService.setNotificationsShownFromListener(null, new String[] {r.getKey()}); |
| 4513 | |
| 4514 | verify(mAppUsageStats).reportInterruptiveNotification(anyString(), anyString(), anyInt()); |
| 4515 | } |
| 4516 | |
| 4517 | @Test |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 4518 | public void testMaybeRecordInterruptionLocked_doesNotRecordTwice() |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 4519 | throws RemoteException { |
| 4520 | final NotificationRecord r = generateNotificationRecord( |
| 4521 | mTestNotificationChannel, 1, null, true); |
| 4522 | r.setInterruptive(true); |
| 4523 | mService.addNotification(r); |
| 4524 | |
| 4525 | mService.maybeRecordInterruptionLocked(r); |
| 4526 | mService.maybeRecordInterruptionLocked(r); |
| 4527 | |
| 4528 | verify(mAppUsageStats, times(1)).reportInterruptiveNotification( |
| 4529 | anyString(), anyString(), anyInt()); |
Julia Reynolds | fd9f834 | 2020-03-06 09:36:00 -0500 | [diff] [blame] | 4530 | verify(mHistoryManager, times(1)).addNotification(any()); |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 4531 | } |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 4532 | |
| 4533 | @Test |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 4534 | public void testBubble() throws Exception { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 4535 | mBinderService.setBubblesAllowed(PKG, mUid, BUBBLE_PREFERENCE_NONE); |
| 4536 | assertFalse(mBinderService.areBubblesAllowed(PKG)); |
| 4537 | assertEquals(mBinderService.getBubblePreferenceForPackage(PKG, mUid), |
| 4538 | BUBBLE_PREFERENCE_NONE); |
Julia Reynolds | 33ab8a0 | 2018-12-17 16:19:52 -0500 | [diff] [blame] | 4539 | } |
| 4540 | |
| 4541 | @Test |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 4542 | public void testUserApprovedBubblesForPackageSelected() throws Exception { |
| 4543 | mBinderService.setBubblesAllowed(PKG, mUid, BUBBLE_PREFERENCE_SELECTED); |
| 4544 | assertEquals(mBinderService.getBubblePreferenceForPackage(PKG, mUid), |
| 4545 | BUBBLE_PREFERENCE_SELECTED); |
| 4546 | } |
| 4547 | |
| 4548 | @Test |
| 4549 | public void testUserApprovedBubblesForPackageAll() throws Exception { |
| 4550 | mBinderService.setBubblesAllowed(PKG, mUid, BUBBLE_PREFERENCE_ALL); |
| 4551 | assertTrue(mBinderService.areBubblesAllowed(PKG)); |
| 4552 | assertEquals(mBinderService.getBubblePreferenceForPackage(PKG, mUid), |
| 4553 | BUBBLE_PREFERENCE_ALL); |
Mady Mellor | 9db685a | 2019-01-23 13:23:37 -0800 | [diff] [blame] | 4554 | } |
| 4555 | |
| 4556 | @Test |
| 4557 | public void testUserRejectsBubblesForPackage() throws Exception { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 4558 | mBinderService.setBubblesAllowed(PKG, mUid, BUBBLE_PREFERENCE_NONE); |
| 4559 | assertFalse(mBinderService.areBubblesAllowed(PKG)); |
Mady Mellor | 9db685a | 2019-01-23 13:23:37 -0800 | [diff] [blame] | 4560 | } |
| 4561 | |
| 4562 | @Test |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 4563 | public void testIsCallerInstantApp_primaryUser() throws Exception { |
| 4564 | ApplicationInfo info = new ApplicationInfo(); |
| 4565 | info.privateFlags = ApplicationInfo.PRIVATE_FLAG_INSTANT; |
| 4566 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), eq(0))).thenReturn(info); |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 4567 | when(mPackageManager.getPackagesForUid(anyInt())).thenReturn(new String[]{"any"}); |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 4568 | |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 4569 | assertTrue(mService.isCallerInstantApp(45770, 0)); |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 4570 | |
| 4571 | info.privateFlags = 0; |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 4572 | assertFalse(mService.isCallerInstantApp(575370, 0)); |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 4573 | } |
| 4574 | |
| 4575 | @Test |
| 4576 | public void testIsCallerInstantApp_secondaryUser() throws Exception { |
| 4577 | ApplicationInfo info = new ApplicationInfo(); |
| 4578 | info.privateFlags = ApplicationInfo.PRIVATE_FLAG_INSTANT; |
| 4579 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), eq(10))).thenReturn(info); |
| 4580 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), eq(0))).thenReturn(null); |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 4581 | when(mPackageManager.getPackagesForUid(anyInt())).thenReturn(new String[]{"any"}); |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 4582 | |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 4583 | assertTrue(mService.isCallerInstantApp(68638450, 10)); |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 4584 | } |
| 4585 | |
| 4586 | @Test |
Julia Reynolds | 86869c9 | 2019-06-21 10:45:06 -0400 | [diff] [blame] | 4587 | public void testIsCallerInstantApp_userAllNotification() throws Exception { |
| 4588 | ApplicationInfo info = new ApplicationInfo(); |
| 4589 | info.privateFlags = ApplicationInfo.PRIVATE_FLAG_INSTANT; |
Julia Reynolds | 57a974b | 2019-10-07 11:51:47 -0400 | [diff] [blame] | 4590 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), eq(USER_SYSTEM))) |
Julia Reynolds | 86869c9 | 2019-06-21 10:45:06 -0400 | [diff] [blame] | 4591 | .thenReturn(info); |
| 4592 | when(mPackageManager.getPackagesForUid(anyInt())).thenReturn(new String[]{"any"}); |
| 4593 | |
| 4594 | assertTrue(mService.isCallerInstantApp(45770, UserHandle.USER_ALL)); |
| 4595 | |
| 4596 | info.privateFlags = 0; |
| 4597 | assertFalse(mService.isCallerInstantApp(575370, UserHandle.USER_ALL )); |
| 4598 | } |
| 4599 | |
| 4600 | @Test |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 4601 | public void testResolveNotificationUid_sameApp_nonSystemUser() throws Exception { |
| 4602 | ApplicationInfo info = new ApplicationInfo(); |
| 4603 | info.uid = Binder.getCallingUid(); |
| 4604 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), eq(10))).thenReturn(info); |
| 4605 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), eq(0))).thenReturn(null); |
| 4606 | |
| 4607 | int actualUid = mService.resolveNotificationUid("caller", "caller", info.uid, 10); |
| 4608 | |
| 4609 | assertEquals(info.uid, actualUid); |
| 4610 | } |
| 4611 | |
| 4612 | @Test |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 4613 | public void testResolveNotificationUid_sameApp() throws Exception { |
| 4614 | ApplicationInfo info = new ApplicationInfo(); |
| 4615 | info.uid = Binder.getCallingUid(); |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 4616 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), eq(0))).thenReturn(info); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 4617 | |
| 4618 | int actualUid = mService.resolveNotificationUid("caller", "caller", info.uid, 0); |
| 4619 | |
| 4620 | assertEquals(info.uid, actualUid); |
| 4621 | } |
| 4622 | |
| 4623 | @Test |
Julia Reynolds | ecc1b57 | 2018-10-01 16:19:24 -0400 | [diff] [blame] | 4624 | public void testResolveNotificationUid_sameAppDiffPackage() throws Exception { |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 4625 | ApplicationInfo info = new ApplicationInfo(); |
| 4626 | info.uid = Binder.getCallingUid(); |
Julia Reynolds | ecc1b57 | 2018-10-01 16:19:24 -0400 | [diff] [blame] | 4627 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), eq(0))).thenReturn(info); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 4628 | |
Julia Reynolds | ecc1b57 | 2018-10-01 16:19:24 -0400 | [diff] [blame] | 4629 | int actualUid = mService.resolveNotificationUid("caller", "callerAlso", info.uid, 0); |
| 4630 | |
| 4631 | assertEquals(info.uid, actualUid); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 4632 | } |
| 4633 | |
| 4634 | @Test |
| 4635 | public void testResolveNotificationUid_sameAppWrongUid() throws Exception { |
| 4636 | ApplicationInfo info = new ApplicationInfo(); |
| 4637 | info.uid = 1356347; |
| 4638 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), anyInt())).thenReturn(info); |
| 4639 | |
| 4640 | try { |
| 4641 | mService.resolveNotificationUid("caller", "caller", 9, 0); |
| 4642 | fail("Incorrect uid didn't throw security exception"); |
| 4643 | } catch (SecurityException e) { |
| 4644 | // yay |
| 4645 | } |
| 4646 | } |
| 4647 | |
| 4648 | @Test |
| 4649 | public void testResolveNotificationUid_delegateAllowed() throws Exception { |
| 4650 | int expectedUid = 123; |
| 4651 | |
| 4652 | when(mPackageManagerClient.getPackageUidAsUser("target", 0)).thenReturn(expectedUid); |
| 4653 | mService.setPreferencesHelper(mPreferencesHelper); |
| 4654 | when(mPreferencesHelper.isDelegateAllowed(anyString(), anyInt(), anyString(), anyInt())) |
| 4655 | .thenReturn(true); |
| 4656 | |
| 4657 | assertEquals(expectedUid, mService.resolveNotificationUid("caller", "target", 9, 0)); |
| 4658 | } |
| 4659 | |
| 4660 | @Test |
| 4661 | public void testResolveNotificationUid_androidAllowed() throws Exception { |
| 4662 | int expectedUid = 123; |
| 4663 | |
| 4664 | when(mPackageManagerClient.getPackageUidAsUser("target", 0)).thenReturn(expectedUid); |
| 4665 | // no delegate |
| 4666 | |
| 4667 | assertEquals(expectedUid, mService.resolveNotificationUid("android", "target", 0, 0)); |
| 4668 | } |
| 4669 | |
| 4670 | @Test |
Julia Reynolds | a46a769 | 2019-08-15 14:35:46 -0400 | [diff] [blame] | 4671 | public void testPostFromAndroidForNonExistentPackage() throws Exception { |
| 4672 | final String notReal = "NOT REAL"; |
| 4673 | when(mPackageManagerClient.getPackageUidAsUser(anyString(), anyInt())).thenThrow( |
| 4674 | PackageManager.NameNotFoundException.class); |
| 4675 | ApplicationInfo ai = new ApplicationInfo(); |
| 4676 | ai.uid = -1; |
| 4677 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), anyInt())).thenReturn(ai); |
| 4678 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 4679 | final StatusBarNotification sbn = generateNotificationRecord(null).getSbn(); |
Julia Reynolds | a46a769 | 2019-08-15 14:35:46 -0400 | [diff] [blame] | 4680 | try { |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 4681 | mInternalService.enqueueNotification(notReal, "android", 0, 0, |
| 4682 | "testPostFromAndroidForNonExistentPackage", |
Julia Reynolds | a46a769 | 2019-08-15 14:35:46 -0400 | [diff] [blame] | 4683 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 4684 | fail("can't post notifications for nonexistent packages, even if you exist"); |
| 4685 | } catch (SecurityException e) { |
| 4686 | // yay |
| 4687 | } |
| 4688 | } |
| 4689 | |
| 4690 | @Test |
| 4691 | public void testCancelFromAndroidForNonExistentPackage() throws Exception { |
| 4692 | final String notReal = "NOT REAL"; |
| 4693 | when(mPackageManagerClient.getPackageUidAsUser(eq(notReal), anyInt())).thenThrow( |
| 4694 | PackageManager.NameNotFoundException.class); |
| 4695 | ApplicationInfo ai = new ApplicationInfo(); |
| 4696 | ai.uid = -1; |
| 4697 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), anyInt())).thenReturn(ai); |
| 4698 | |
| 4699 | // unlike the post case, ignore instead of throwing |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 4700 | final StatusBarNotification sbn = generateNotificationRecord(null).getSbn(); |
Julia Reynolds | a46a769 | 2019-08-15 14:35:46 -0400 | [diff] [blame] | 4701 | |
| 4702 | mInternalService.cancelNotification(notReal, "android", 0, 0, "tag", |
| 4703 | sbn.getId(), sbn.getUserId()); |
| 4704 | } |
| 4705 | |
| 4706 | @Test |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 4707 | public void testResolveNotificationUid_delegateNotAllowed() throws Exception { |
| 4708 | when(mPackageManagerClient.getPackageUidAsUser("target", 0)).thenReturn(123); |
| 4709 | // no delegate |
| 4710 | |
| 4711 | try { |
| 4712 | mService.resolveNotificationUid("caller", "target", 9, 0); |
| 4713 | fail("Incorrect uid didn't throw security exception"); |
| 4714 | } catch (SecurityException e) { |
| 4715 | // yay |
| 4716 | } |
| 4717 | } |
Julia Reynolds | 564273f | 2018-09-13 15:53:11 -0400 | [diff] [blame] | 4718 | |
| 4719 | @Test |
| 4720 | public void testRemoveForegroundServiceFlagFromNotification_enqueued() { |
| 4721 | Notification n = new Notification.Builder(mContext, "").build(); |
| 4722 | n.flags |= FLAG_FOREGROUND_SERVICE; |
| 4723 | |
| 4724 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 9, null, mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 4725 | n, UserHandle.getUserHandleForUid(mUid), null, 0); |
Julia Reynolds | 564273f | 2018-09-13 15:53:11 -0400 | [diff] [blame] | 4726 | NotificationRecord r = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 4727 | |
| 4728 | mService.addEnqueuedNotification(r); |
| 4729 | |
| 4730 | mInternalService.removeForegroundServiceFlagFromNotification( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 4731 | PKG, r.getSbn().getId(), r.getSbn().getUserId()); |
Julia Reynolds | 564273f | 2018-09-13 15:53:11 -0400 | [diff] [blame] | 4732 | |
| 4733 | waitForIdle(); |
| 4734 | |
| 4735 | verify(mListeners, timeout(200).times(0)).notifyPostedLocked(any(), any()); |
| 4736 | } |
| 4737 | |
| 4738 | @Test |
| 4739 | public void testRemoveForegroundServiceFlagFromNotification_posted() { |
| 4740 | Notification n = new Notification.Builder(mContext, "").build(); |
| 4741 | n.flags |= FLAG_FOREGROUND_SERVICE; |
| 4742 | |
| 4743 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 9, null, mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 4744 | n, UserHandle.getUserHandleForUid(mUid), null, 0); |
Julia Reynolds | 564273f | 2018-09-13 15:53:11 -0400 | [diff] [blame] | 4745 | NotificationRecord r = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 4746 | |
| 4747 | mService.addNotification(r); |
| 4748 | |
| 4749 | mInternalService.removeForegroundServiceFlagFromNotification( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 4750 | PKG, r.getSbn().getId(), r.getSbn().getUserId()); |
Julia Reynolds | 564273f | 2018-09-13 15:53:11 -0400 | [diff] [blame] | 4751 | |
| 4752 | waitForIdle(); |
| 4753 | |
| 4754 | ArgumentCaptor<NotificationRecord> captor = |
| 4755 | ArgumentCaptor.forClass(NotificationRecord.class); |
| 4756 | verify(mListeners, times(1)).notifyPostedLocked(captor.capture(), any()); |
| 4757 | |
| 4758 | assertEquals(0, captor.getValue().getNotification().flags); |
| 4759 | } |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 4760 | |
| 4761 | @Test |
Bernardo Rufino | 18725b6 | 2020-01-21 14:12:43 +0000 | [diff] [blame] | 4762 | public void testAllowForegroundCustomToasts() throws Exception { |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 4763 | final String testPackage = "testPackageName"; |
| 4764 | assertEquals(0, mService.mToastQueue.size()); |
| 4765 | mService.isSystemUid = false; |
| 4766 | |
| 4767 | // package is not suspended |
| 4768 | when(mPackageManager.isPackageSuspendedForUser(testPackage, UserHandle.getUserId(mUid))) |
| 4769 | .thenReturn(false); |
| 4770 | |
| 4771 | // notifications from this package are blocked by the user |
| 4772 | mService.setPreferencesHelper(mPreferencesHelper); |
| 4773 | when(mPreferencesHelper.getImportance(testPackage, mUid)).thenReturn(IMPORTANCE_NONE); |
| 4774 | |
Bernardo Rufino | e6cb310 | 2020-03-06 20:33:11 +0000 | [diff] [blame] | 4775 | setAppInForegroundForToasts(mUid, true); |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 4776 | |
| 4777 | // enqueue toast -> toast should still enqueue |
Bernardo Rufino | 52af6db | 2019-12-20 18:26:07 +0000 | [diff] [blame] | 4778 | ((INotificationManager) mService.mService).enqueueToast(testPackage, new Binder(), |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 4779 | new TestableToastCallback(), 2000, 0); |
| 4780 | assertEquals(1, mService.mToastQueue.size()); |
| 4781 | } |
| 4782 | |
| 4783 | @Test |
Bernardo Rufino | 18725b6 | 2020-01-21 14:12:43 +0000 | [diff] [blame] | 4784 | public void testDisallowBackgroundCustomToasts() throws Exception { |
| 4785 | final String testPackage = "testPackageName"; |
| 4786 | assertEquals(0, mService.mToastQueue.size()); |
| 4787 | mService.isSystemUid = false; |
| 4788 | |
| 4789 | // package is not suspended |
| 4790 | when(mPackageManager.isPackageSuspendedForUser(testPackage, UserHandle.getUserId(mUid))) |
| 4791 | .thenReturn(false); |
| 4792 | |
Bernardo Rufino | e6cb310 | 2020-03-06 20:33:11 +0000 | [diff] [blame] | 4793 | setAppInForegroundForToasts(mUid, false); |
Bernardo Rufino | 18725b6 | 2020-01-21 14:12:43 +0000 | [diff] [blame] | 4794 | |
| 4795 | // enqueue toast -> no toasts enqueued |
| 4796 | ((INotificationManager) mService.mService).enqueueToast(testPackage, new Binder(), |
| 4797 | new TestableToastCallback(), 2000, 0); |
| 4798 | assertEquals(0, mService.mToastQueue.size()); |
| 4799 | } |
| 4800 | |
| 4801 | @Test |
| 4802 | public void testAllowForegroundTextToasts() throws Exception { |
| 4803 | final String testPackage = "testPackageName"; |
| 4804 | assertEquals(0, mService.mToastQueue.size()); |
| 4805 | mService.isSystemUid = false; |
| 4806 | |
| 4807 | // package is not suspended |
| 4808 | when(mPackageManager.isPackageSuspendedForUser(testPackage, UserHandle.getUserId(mUid))) |
| 4809 | .thenReturn(false); |
| 4810 | |
Bernardo Rufino | e6cb310 | 2020-03-06 20:33:11 +0000 | [diff] [blame] | 4811 | setAppInForegroundForToasts(mUid, true); |
Bernardo Rufino | 18725b6 | 2020-01-21 14:12:43 +0000 | [diff] [blame] | 4812 | |
| 4813 | // enqueue toast -> toast should still enqueue |
| 4814 | ((INotificationManager) mService.mService).enqueueTextToast(testPackage, new Binder(), |
| 4815 | "Text", 2000, 0, null); |
| 4816 | assertEquals(1, mService.mToastQueue.size()); |
| 4817 | } |
| 4818 | |
| 4819 | @Test |
| 4820 | public void testAllowBackgroundTextToasts() throws Exception { |
| 4821 | final String testPackage = "testPackageName"; |
| 4822 | assertEquals(0, mService.mToastQueue.size()); |
| 4823 | mService.isSystemUid = false; |
| 4824 | |
| 4825 | // package is not suspended |
| 4826 | when(mPackageManager.isPackageSuspendedForUser(testPackage, UserHandle.getUserId(mUid))) |
| 4827 | .thenReturn(false); |
| 4828 | |
Bernardo Rufino | e6cb310 | 2020-03-06 20:33:11 +0000 | [diff] [blame] | 4829 | setAppInForegroundForToasts(mUid, false); |
Bernardo Rufino | 18725b6 | 2020-01-21 14:12:43 +0000 | [diff] [blame] | 4830 | |
| 4831 | // enqueue toast -> toast should still enqueue |
| 4832 | ((INotificationManager) mService.mService).enqueueTextToast(testPackage, new Binder(), |
| 4833 | "Text", 2000, 0, null); |
| 4834 | assertEquals(1, mService.mToastQueue.size()); |
| 4835 | } |
| 4836 | |
| 4837 | @Test |
| 4838 | public void testTextToastsCallStatusBar() throws Exception { |
| 4839 | final String testPackage = "testPackageName"; |
| 4840 | assertEquals(0, mService.mToastQueue.size()); |
| 4841 | mService.isSystemUid = false; |
| 4842 | |
| 4843 | // package is not suspended |
| 4844 | when(mPackageManager.isPackageSuspendedForUser(testPackage, UserHandle.getUserId(mUid))) |
| 4845 | .thenReturn(false); |
| 4846 | |
| 4847 | // enqueue toast -> no toasts enqueued |
| 4848 | ((INotificationManager) mService.mService).enqueueTextToast(testPackage, new Binder(), |
| 4849 | "Text", 2000, 0, null); |
Bernardo Rufino | 78f5d83 | 2020-04-03 11:50:23 +0100 | [diff] [blame] | 4850 | verify(mStatusBar).showToast(anyInt(), any(), any(), any(), any(), anyInt(), any()); |
Bernardo Rufino | 18725b6 | 2020-01-21 14:12:43 +0000 | [diff] [blame] | 4851 | } |
| 4852 | |
| 4853 | @Test |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 4854 | public void testDisallowToastsFromSuspendedPackages() throws Exception { |
| 4855 | final String testPackage = "testPackageName"; |
| 4856 | assertEquals(0, mService.mToastQueue.size()); |
| 4857 | mService.isSystemUid = false; |
| 4858 | |
| 4859 | // package is suspended |
| 4860 | when(mPackageManager.isPackageSuspendedForUser(testPackage, UserHandle.getUserId(mUid))) |
| 4861 | .thenReturn(true); |
| 4862 | |
| 4863 | // notifications from this package are NOT blocked by the user |
| 4864 | mService.setPreferencesHelper(mPreferencesHelper); |
| 4865 | when(mPreferencesHelper.getImportance(testPackage, mUid)).thenReturn(IMPORTANCE_LOW); |
| 4866 | |
| 4867 | // enqueue toast -> no toasts enqueued |
Bernardo Rufino | 52af6db | 2019-12-20 18:26:07 +0000 | [diff] [blame] | 4868 | ((INotificationManager) mService.mService).enqueueToast(testPackage, new Binder(), |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 4869 | new TestableToastCallback(), 2000, 0); |
| 4870 | assertEquals(0, mService.mToastQueue.size()); |
| 4871 | } |
| 4872 | |
| 4873 | @Test |
| 4874 | public void testDisallowToastsFromBlockedApps() throws Exception { |
| 4875 | final String testPackage = "testPackageName"; |
| 4876 | assertEquals(0, mService.mToastQueue.size()); |
| 4877 | mService.isSystemUid = false; |
| 4878 | |
| 4879 | // package is not suspended |
| 4880 | when(mPackageManager.isPackageSuspendedForUser(testPackage, UserHandle.getUserId(mUid))) |
| 4881 | .thenReturn(false); |
| 4882 | |
| 4883 | // notifications from this package are blocked by the user |
| 4884 | mService.setPreferencesHelper(mPreferencesHelper); |
| 4885 | when(mPreferencesHelper.getImportance(testPackage, mUid)).thenReturn(IMPORTANCE_NONE); |
| 4886 | |
Bernardo Rufino | e6cb310 | 2020-03-06 20:33:11 +0000 | [diff] [blame] | 4887 | setAppInForegroundForToasts(mUid, false); |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 4888 | |
| 4889 | // enqueue toast -> no toasts enqueued |
Bernardo Rufino | 52af6db | 2019-12-20 18:26:07 +0000 | [diff] [blame] | 4890 | ((INotificationManager) mService.mService).enqueueToast(testPackage, new Binder(), |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 4891 | new TestableToastCallback(), 2000, 0); |
| 4892 | assertEquals(0, mService.mToastQueue.size()); |
| 4893 | } |
| 4894 | |
| 4895 | @Test |
| 4896 | public void testAlwaysAllowSystemToasts() throws Exception { |
| 4897 | final String testPackage = "testPackageName"; |
| 4898 | assertEquals(0, mService.mToastQueue.size()); |
| 4899 | mService.isSystemUid = true; |
| 4900 | |
| 4901 | // package is suspended |
| 4902 | when(mPackageManager.isPackageSuspendedForUser(testPackage, UserHandle.getUserId(mUid))) |
| 4903 | .thenReturn(true); |
| 4904 | |
| 4905 | // notifications from this package ARE blocked by the user |
| 4906 | mService.setPreferencesHelper(mPreferencesHelper); |
| 4907 | when(mPreferencesHelper.getImportance(testPackage, mUid)).thenReturn(IMPORTANCE_NONE); |
| 4908 | |
Bernardo Rufino | e6cb310 | 2020-03-06 20:33:11 +0000 | [diff] [blame] | 4909 | setAppInForegroundForToasts(mUid, false); |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 4910 | |
| 4911 | // enqueue toast -> system toast can still be enqueued |
Bernardo Rufino | 52af6db | 2019-12-20 18:26:07 +0000 | [diff] [blame] | 4912 | ((INotificationManager) mService.mService).enqueueToast(testPackage, new Binder(), |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 4913 | new TestableToastCallback(), 2000, 0); |
| 4914 | assertEquals(1, mService.mToastQueue.size()); |
| 4915 | } |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 4916 | |
Bernardo Rufino | e6cb310 | 2020-03-06 20:33:11 +0000 | [diff] [blame] | 4917 | private void setAppInForegroundForToasts(int uid, boolean inForeground) { |
| 4918 | int importance = (inForeground) ? IMPORTANCE_FOREGROUND : IMPORTANCE_NONE; |
| 4919 | when(mActivityManager.getUidImportance(mUid)).thenReturn(importance); |
| 4920 | when(mAtm.hasResumedActivity(uid)).thenReturn(inForeground); |
| 4921 | } |
| 4922 | |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 4923 | @Test |
Alex Mang | 509e554 | 2020-01-03 13:06:40 -0800 | [diff] [blame] | 4924 | public void testOnPanelRevealedAndHidden() { |
| 4925 | int items = 5; |
| 4926 | mService.mNotificationDelegate.onPanelRevealed(false, items); |
| 4927 | verify(mAssistants, times(1)).onPanelRevealed(eq(items)); |
| 4928 | |
| 4929 | mService.mNotificationDelegate.onPanelHidden(); |
| 4930 | verify(mAssistants, times(1)).onPanelHidden(); |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 4931 | |
| 4932 | assertEquals(2, mNotificationRecordLogger.numCalls()); |
| 4933 | assertEquals(NotificationRecordLogger.NotificationPanelEvent.NOTIFICATION_PANEL_OPEN, |
| 4934 | mNotificationRecordLogger.event(0)); |
| 4935 | assertEquals(NotificationRecordLogger.NotificationPanelEvent.NOTIFICATION_PANEL_CLOSE, |
| 4936 | mNotificationRecordLogger.event(1)); |
Alex Mang | 509e554 | 2020-01-03 13:06:40 -0800 | [diff] [blame] | 4937 | } |
| 4938 | |
| 4939 | @Test |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 4940 | public void testOnNotificationSmartReplySent() { |
| 4941 | final int replyIndex = 2; |
| 4942 | final String reply = "Hello"; |
Milo Sredkov | 13d8811 | 2019-02-01 12:23:24 +0000 | [diff] [blame] | 4943 | final boolean modifiedBeforeSending = true; |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 4944 | final boolean generatedByAssistant = true; |
| 4945 | |
| 4946 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
Milo Sredkov | 13d8811 | 2019-02-01 12:23:24 +0000 | [diff] [blame] | 4947 | r.setSuggestionsGeneratedByAssistant(generatedByAssistant); |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 4948 | mService.addNotification(r); |
| 4949 | |
| 4950 | mService.mNotificationDelegate.onNotificationSmartReplySent( |
Milo Sredkov | 13d8811 | 2019-02-01 12:23:24 +0000 | [diff] [blame] | 4951 | r.getKey(), replyIndex, reply, NOTIFICATION_LOCATION_UNKNOWN, |
| 4952 | modifiedBeforeSending); |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 4953 | verify(mAssistants).notifyAssistantSuggestedReplySent( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 4954 | eq(r.getSbn()), eq(reply), eq(generatedByAssistant)); |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 4955 | assertEquals(1, mNotificationRecordLogger.numCalls()); |
| 4956 | assertEquals(NotificationRecordLogger.NotificationEvent.NOTIFICATION_SMART_REPLIED, |
| 4957 | mNotificationRecordLogger.event(0)); |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 4958 | } |
Tony Mak | 7d4b3a5 | 2018-11-27 17:29:36 +0000 | [diff] [blame] | 4959 | |
| 4960 | @Test |
| 4961 | public void testOnNotificationActionClick() { |
| 4962 | final int actionIndex = 2; |
| 4963 | final Notification.Action action = |
| 4964 | new Notification.Action.Builder(null, "text", null).build(); |
| 4965 | final boolean generatedByAssistant = false; |
| 4966 | |
| 4967 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 4968 | mService.addNotification(r); |
| 4969 | |
| 4970 | NotificationVisibility notificationVisibility = |
| 4971 | NotificationVisibility.obtain(r.getKey(), 1, 2, true); |
| 4972 | mService.mNotificationDelegate.onNotificationActionClick( |
| 4973 | 10, 10, r.getKey(), actionIndex, action, notificationVisibility, |
| 4974 | generatedByAssistant); |
| 4975 | verify(mAssistants).notifyAssistantActionClicked( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 4976 | eq(r.getSbn()), eq(actionIndex), eq(action), eq(generatedByAssistant)); |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 4977 | |
| 4978 | assertEquals(1, mNotificationRecordLogger.numCalls()); |
Will Brockman | 2a2c0c9 | 2020-05-04 13:58:28 -0400 | [diff] [blame] | 4979 | assertEquals( |
| 4980 | NotificationRecordLogger.NotificationEvent.NOTIFICATION_ACTION_CLICKED_2, |
| 4981 | mNotificationRecordLogger.event(0)); |
| 4982 | } |
| 4983 | |
| 4984 | @Test |
| 4985 | public void testOnAssistantNotificationActionClick() { |
| 4986 | final int actionIndex = 1; |
| 4987 | final Notification.Action action = |
| 4988 | new Notification.Action.Builder(null, "text", null).build(); |
| 4989 | final boolean generatedByAssistant = true; |
| 4990 | |
| 4991 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 4992 | mService.addNotification(r); |
| 4993 | |
| 4994 | NotificationVisibility notificationVisibility = |
| 4995 | NotificationVisibility.obtain(r.getKey(), 1, 2, true); |
| 4996 | mService.mNotificationDelegate.onNotificationActionClick( |
| 4997 | 10, 10, r.getKey(), actionIndex, action, notificationVisibility, |
| 4998 | generatedByAssistant); |
| 4999 | verify(mAssistants).notifyAssistantActionClicked( |
| 5000 | eq(r.getSbn()), eq(actionIndex), eq(action), eq(generatedByAssistant)); |
| 5001 | |
| 5002 | assertEquals(1, mNotificationRecordLogger.numCalls()); |
| 5003 | assertEquals( |
| 5004 | NotificationRecordLogger.NotificationEvent.NOTIFICATION_ASSIST_ACTION_CLICKED_1, |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 5005 | mNotificationRecordLogger.event(0)); |
Tony Mak | 7d4b3a5 | 2018-11-27 17:29:36 +0000 | [diff] [blame] | 5006 | } |
Gustav Sennton | 44dc588 | 2018-12-13 14:38:50 +0000 | [diff] [blame] | 5007 | |
| 5008 | @Test |
| 5009 | public void testLogSmartSuggestionsVisible_triggerOnExpandAndVisible() { |
| 5010 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 5011 | mService.addNotification(r); |
| 5012 | |
Gustav Sennton | a8e38aa | 2019-01-22 14:55:39 +0000 | [diff] [blame] | 5013 | mService.mNotificationDelegate.onNotificationExpansionChanged(r.getKey(), false, true, |
| 5014 | NOTIFICATION_LOCATION_UNKNOWN); |
Gustav Sennton | 44dc588 | 2018-12-13 14:38:50 +0000 | [diff] [blame] | 5015 | NotificationVisibility[] notificationVisibility = new NotificationVisibility[] { |
| 5016 | NotificationVisibility.obtain(r.getKey(), 0, 0, true) |
| 5017 | }; |
| 5018 | mService.mNotificationDelegate.onNotificationVisibilityChanged(notificationVisibility, |
| 5019 | new NotificationVisibility[0]); |
| 5020 | |
| 5021 | assertEquals(1, mService.countLogSmartSuggestionsVisible); |
| 5022 | } |
| 5023 | |
| 5024 | @Test |
| 5025 | public void testLogSmartSuggestionsVisible_noTriggerOnExpand() { |
| 5026 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 5027 | mService.addNotification(r); |
| 5028 | |
Gustav Sennton | a8e38aa | 2019-01-22 14:55:39 +0000 | [diff] [blame] | 5029 | mService.mNotificationDelegate.onNotificationExpansionChanged(r.getKey(), false, true, |
| 5030 | NOTIFICATION_LOCATION_UNKNOWN); |
Gustav Sennton | 44dc588 | 2018-12-13 14:38:50 +0000 | [diff] [blame] | 5031 | |
| 5032 | assertEquals(0, mService.countLogSmartSuggestionsVisible); |
| 5033 | } |
| 5034 | |
| 5035 | @Test |
| 5036 | public void testLogSmartSuggestionsVisible_noTriggerOnVisible() { |
| 5037 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 5038 | mService.addNotification(r); |
| 5039 | |
Julia Reynolds | 9533413 | 2018-12-19 11:15:35 -0500 | [diff] [blame] | 5040 | NotificationVisibility[] notificationVisibility = new NotificationVisibility[]{ |
Gustav Sennton | 44dc588 | 2018-12-13 14:38:50 +0000 | [diff] [blame] | 5041 | NotificationVisibility.obtain(r.getKey(), 0, 0, true) |
| 5042 | }; |
| 5043 | mService.mNotificationDelegate.onNotificationVisibilityChanged(notificationVisibility, |
| 5044 | new NotificationVisibility[0]); |
| 5045 | |
| 5046 | assertEquals(0, mService.countLogSmartSuggestionsVisible); |
| 5047 | } |
Julia Reynolds | 9533413 | 2018-12-19 11:15:35 -0500 | [diff] [blame] | 5048 | |
Colin Cross | 5ea7fb7 | 2019-12-18 17:16:36 -0800 | [diff] [blame] | 5049 | @Test |
Julia Reynolds | 9533413 | 2018-12-19 11:15:35 -0500 | [diff] [blame] | 5050 | public void testReportSeen_delegated() { |
| 5051 | Notification.Builder nb = |
| 5052 | new Notification.Builder(mContext, mTestNotificationChannel.getId()) |
| 5053 | .setContentTitle("foo") |
| 5054 | .setSmallIcon(android.R.drawable.sym_def_app_icon); |
| 5055 | |
| 5056 | StatusBarNotification sbn = new StatusBarNotification(PKG, "opPkg", 0, "tag", mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 5057 | nb.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Julia Reynolds | 9533413 | 2018-12-19 11:15:35 -0500 | [diff] [blame] | 5058 | NotificationRecord r = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 5059 | |
| 5060 | mService.reportSeen(r); |
| 5061 | verify(mAppUsageStats, never()).reportEvent(anyString(), anyInt(), anyInt()); |
| 5062 | |
| 5063 | } |
| 5064 | |
| 5065 | @Test |
| 5066 | public void testReportSeen_notDelegated() { |
| 5067 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 5068 | |
| 5069 | mService.reportSeen(r); |
| 5070 | verify(mAppUsageStats, times(1)).reportEvent(anyString(), anyInt(), anyInt()); |
| 5071 | } |
Julia Reynolds | 3207e2f | 2018-12-20 09:39:53 -0500 | [diff] [blame] | 5072 | |
| 5073 | @Test |
| 5074 | public void testNotificationStats_notificationError() { |
| 5075 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 5076 | mService.addNotification(r); |
| 5077 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5078 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, r.getSbn().getId(), |
| 5079 | r.getSbn().getTag(), mUid, 0, |
Julia Reynolds | 3207e2f | 2018-12-20 09:39:53 -0500 | [diff] [blame] | 5080 | new Notification.Builder(mContext, mTestNotificationChannel.getId()).build(), |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 5081 | UserHandle.getUserHandleForUid(mUid), null, 0); |
Julia Reynolds | 3207e2f | 2018-12-20 09:39:53 -0500 | [diff] [blame] | 5082 | NotificationRecord update = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 5083 | mService.addEnqueuedNotification(update); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5084 | assertNull(update.getSbn().getNotification().getSmallIcon()); |
Julia Reynolds | 3207e2f | 2018-12-20 09:39:53 -0500 | [diff] [blame] | 5085 | |
| 5086 | NotificationManagerService.PostNotificationRunnable runnable = |
| 5087 | mService.new PostNotificationRunnable(update.getKey()); |
| 5088 | runnable.run(); |
| 5089 | waitForIdle(); |
| 5090 | |
| 5091 | ArgumentCaptor<NotificationStats> captor = ArgumentCaptor.forClass(NotificationStats.class); |
| 5092 | verify(mListeners).notifyRemovedLocked(any(), anyInt(), captor.capture()); |
| 5093 | assertNotNull(captor.getValue()); |
| 5094 | } |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 5095 | |
| 5096 | @Test |
Julia Reynolds | 7a6d07a | 2019-03-18 11:31:56 -0400 | [diff] [blame] | 5097 | public void testCanNotifyAsUser_crossUser() throws Exception { |
| 5098 | // same user no problem |
| 5099 | mBinderService.canNotifyAsPackage("src", "target", mContext.getUserId()); |
| 5100 | |
| 5101 | // cross user, no permission, problem |
| 5102 | try { |
| 5103 | mBinderService.canNotifyAsPackage("src", "target", mContext.getUserId() + 1); |
| 5104 | fail("Should not be callable cross user without cross user permission"); |
| 5105 | } catch (SecurityException e) { |
| 5106 | // good |
| 5107 | } |
| 5108 | |
| 5109 | // cross user, with permission, no problem |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 5110 | enableInteractAcrossUsers(); |
Julia Reynolds | 7a6d07a | 2019-03-18 11:31:56 -0400 | [diff] [blame] | 5111 | mBinderService.canNotifyAsPackage("src", "target", mContext.getUserId() + 1); |
| 5112 | } |
| 5113 | |
| 5114 | @Test |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 5115 | public void testGetNotificationChannels_crossUser() throws Exception { |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 5116 | // same user no problem |
| 5117 | mBinderService.getNotificationChannels("src", "target", mContext.getUserId()); |
| 5118 | |
| 5119 | // cross user, no permission, problem |
| 5120 | try { |
| 5121 | mBinderService.getNotificationChannels("src", "target", mContext.getUserId() + 1); |
| 5122 | fail("Should not be callable cross user without cross user permission"); |
| 5123 | } catch (SecurityException e) { |
| 5124 | // good |
| 5125 | } |
| 5126 | |
| 5127 | // cross user, with permission, no problem |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 5128 | enableInteractAcrossUsers(); |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 5129 | mBinderService.getNotificationChannels("src", "target", mContext.getUserId() + 1); |
| 5130 | } |
| 5131 | |
| 5132 | @Test |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 5133 | public void setDefaultAssistantForUser_fromConfigXml() { |
| 5134 | clearDeviceConfig(); |
| 5135 | ComponentName xmlConfig = new ComponentName("config", "xml"); |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 5136 | ArraySet<ComponentName> components = new ArraySet<>(Arrays.asList(xmlConfig)); |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 5137 | when(mResources |
| 5138 | .getString( |
| 5139 | com.android.internal.R.string.config_defaultAssistantAccessComponent)) |
| 5140 | .thenReturn(xmlConfig.flattenToString()); |
| 5141 | when(mContext.getResources()).thenReturn(mResources); |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 5142 | when(mAssistants.queryPackageForServices(eq(null), anyInt(), anyInt())) |
| 5143 | .thenReturn(components); |
| 5144 | when(mAssistants.getDefaultComponents()) |
| 5145 | .thenReturn(components); |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 5146 | mService.setNotificationAssistantAccessGrantedCallback( |
| 5147 | mNotificationAssistantAccessGrantedCallback); |
| 5148 | |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 5149 | |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 5150 | mService.setDefaultAssistantForUser(0); |
| 5151 | |
| 5152 | verify(mNotificationAssistantAccessGrantedCallback) |
| 5153 | .onGranted(eq(xmlConfig), eq(0), eq(true)); |
| 5154 | } |
| 5155 | |
| 5156 | @Test |
| 5157 | public void setDefaultAssistantForUser_fromDeviceConfig() { |
| 5158 | ComponentName xmlConfig = new ComponentName("xml", "config"); |
| 5159 | ComponentName deviceConfig = new ComponentName("device", "config"); |
| 5160 | setDefaultAssistantInDeviceConfig(deviceConfig.flattenToString()); |
| 5161 | when(mResources |
| 5162 | .getString(com.android.internal.R.string.config_defaultAssistantAccessComponent)) |
| 5163 | .thenReturn(xmlConfig.flattenToString()); |
| 5164 | when(mContext.getResources()).thenReturn(mResources); |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 5165 | when(mAssistants.queryPackageForServices(eq(null), anyInt(), anyInt())) |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 5166 | .thenReturn(new ArraySet<>(Arrays.asList(xmlConfig, deviceConfig))); |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 5167 | when(mAssistants.getDefaultComponents()) |
| 5168 | .thenReturn(new ArraySet<>(Arrays.asList(deviceConfig))); |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 5169 | mService.setNotificationAssistantAccessGrantedCallback( |
| 5170 | mNotificationAssistantAccessGrantedCallback); |
| 5171 | |
| 5172 | mService.setDefaultAssistantForUser(0); |
| 5173 | |
| 5174 | verify(mNotificationAssistantAccessGrantedCallback) |
| 5175 | .onGranted(eq(deviceConfig), eq(0), eq(true)); |
| 5176 | } |
| 5177 | |
| 5178 | @Test |
| 5179 | public void setDefaultAssistantForUser_deviceConfigInvalid() { |
| 5180 | ComponentName xmlConfig = new ComponentName("xml", "config"); |
| 5181 | ComponentName deviceConfig = new ComponentName("device", "config"); |
| 5182 | setDefaultAssistantInDeviceConfig(deviceConfig.flattenToString()); |
| 5183 | when(mResources |
| 5184 | .getString(com.android.internal.R.string.config_defaultAssistantAccessComponent)) |
| 5185 | .thenReturn(xmlConfig.flattenToString()); |
| 5186 | when(mContext.getResources()).thenReturn(mResources); |
| 5187 | // Only xmlConfig is valid, deviceConfig is not. |
| 5188 | when(mAssistants.queryPackageForServices(eq(null), anyInt(), eq(0))) |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 5189 | .thenReturn(new ArraySet<>(Collections.singleton(xmlConfig))); |
| 5190 | when(mAssistants.getDefaultComponents()) |
| 5191 | .thenReturn(new ArraySet<>(Arrays.asList(xmlConfig, deviceConfig))); |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 5192 | mService.setNotificationAssistantAccessGrantedCallback( |
| 5193 | mNotificationAssistantAccessGrantedCallback); |
| 5194 | |
| 5195 | mService.setDefaultAssistantForUser(0); |
| 5196 | |
| 5197 | verify(mNotificationAssistantAccessGrantedCallback) |
| 5198 | .onGranted(eq(xmlConfig), eq(0), eq(true)); |
| 5199 | } |
| 5200 | |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 5201 | @Test |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 5202 | public void clearMultipleDefaultAssistantPackagesShouldEnableOnlyOne() throws RemoteException { |
| 5203 | ArrayMap<Boolean, ArrayList<ComponentName>> changedListeners = |
| 5204 | generateResetComponentValues(); |
| 5205 | when(mListeners.resetComponents(anyString(), anyInt())).thenReturn(changedListeners); |
| 5206 | ArrayMap<Boolean, ArrayList<ComponentName>> changes = new ArrayMap<>(); |
| 5207 | ComponentName deviceConfig1 = new ComponentName("device", "config1"); |
| 5208 | ComponentName deviceConfig2 = new ComponentName("device", "config2"); |
| 5209 | changes.put(true, new ArrayList(Arrays.asList(deviceConfig1, deviceConfig2))); |
| 5210 | changes.put(false, new ArrayList()); |
| 5211 | when(mAssistants.resetComponents(anyString(), anyInt())).thenReturn(changes); |
| 5212 | mService.getBinderService().clearData("device", 0, false); |
| 5213 | verify(mAssistants, times(1)) |
| 5214 | .setPackageOrComponentEnabled( |
| 5215 | eq("device/config2"), |
| 5216 | eq(0), eq(true), eq(false)); |
| 5217 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 5218 | eq("device"), eq(0), eq(false), eq(true)); |
| 5219 | } |
| 5220 | |
| 5221 | @Test |
| 5222 | public void clearDefaultListenersPackageShouldEnableIt() throws RemoteException { |
| 5223 | ArrayMap<Boolean, ArrayList<ComponentName>> changedAssistants = |
| 5224 | generateResetComponentValues(); |
| 5225 | when(mAssistants.resetComponents(anyString(), anyInt())).thenReturn(changedAssistants); |
| 5226 | ComponentName deviceConfig = new ComponentName("device", "config"); |
| 5227 | ArrayMap<Boolean, ArrayList<ComponentName>> changes = new ArrayMap<>(); |
| 5228 | changes.put(true, new ArrayList(Arrays.asList(deviceConfig))); |
| 5229 | changes.put(false, new ArrayList()); |
| 5230 | when(mListeners.resetComponents(anyString(), anyInt())) |
| 5231 | .thenReturn(changes); |
| 5232 | mService.getBinderService().clearData("device", 0, false); |
| 5233 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 5234 | eq("device"), eq(0), eq(false), eq(true)); |
| 5235 | } |
| 5236 | |
| 5237 | @Test |
| 5238 | public void clearDefaultDnDPackageShouldEnableIt() throws RemoteException { |
| 5239 | ComponentName deviceConfig = new ComponentName("device", "config"); |
| 5240 | ArrayMap<Boolean, ArrayList<ComponentName>> changed = generateResetComponentValues(); |
| 5241 | when(mAssistants.resetComponents(anyString(), anyInt())).thenReturn(changed); |
| 5242 | when(mListeners.resetComponents(anyString(), anyInt())).thenReturn(changed); |
| 5243 | mService.getBinderService().clearData("device", 0, false); |
| 5244 | verify(mConditionProviders, times(1)).resetPackage( |
| 5245 | eq("device"), eq(0)); |
| 5246 | } |
| 5247 | |
| 5248 | @Test |
Mady Mellor | 65dcaa9 | 2019-04-03 12:21:44 -0700 | [diff] [blame] | 5249 | public void testFlagBubble() throws RemoteException { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 5250 | setUpPrefsForBubbles(PKG, mUid, |
| 5251 | true /* global */, |
| 5252 | BUBBLE_PREFERENCE_ALL /* app */, |
| 5253 | true /* channel */); |
Mady Mellor | 65dcaa9 | 2019-04-03 12:21:44 -0700 | [diff] [blame] | 5254 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5255 | NotificationRecord nr = |
| 5256 | generateMessageBubbleNotifRecord(mTestNotificationChannel, "testFlagBubble"); |
Mady Mellor | 65dcaa9 | 2019-04-03 12:21:44 -0700 | [diff] [blame] | 5257 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5258 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 5259 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Mady Mellor | 65dcaa9 | 2019-04-03 12:21:44 -0700 | [diff] [blame] | 5260 | waitForIdle(); |
| 5261 | |
| 5262 | StatusBarNotification[] notifs = mBinderService.getActiveNotifications(PKG); |
| 5263 | assertEquals(1, notifs.length); |
| 5264 | assertTrue((notifs[0].getNotification().flags & FLAG_BUBBLE) != 0); |
| 5265 | assertTrue(mService.getNotificationRecord( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5266 | nr.getSbn().getKey()).getNotification().isBubbleNotification()); |
Mady Mellor | 65dcaa9 | 2019-04-03 12:21:44 -0700 | [diff] [blame] | 5267 | } |
| 5268 | |
| 5269 | @Test |
Mady Mellor | c682034 | 2019-05-20 12:04:36 -0700 | [diff] [blame] | 5270 | public void testFlagBubble_noFlag_appNotAllowed() throws RemoteException { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 5271 | setUpPrefsForBubbles(PKG, mUid, |
| 5272 | true /* global */, |
| 5273 | BUBBLE_PREFERENCE_NONE /* app */, |
| 5274 | true /* channel */); |
Mady Mellor | c682034 | 2019-05-20 12:04:36 -0700 | [diff] [blame] | 5275 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5276 | NotificationRecord nr = generateMessageBubbleNotifRecord(mTestNotificationChannel, |
| 5277 | "testFlagBubble_noFlag_appNotAllowed"); |
Mady Mellor | c682034 | 2019-05-20 12:04:36 -0700 | [diff] [blame] | 5278 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5279 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 5280 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Mady Mellor | c682034 | 2019-05-20 12:04:36 -0700 | [diff] [blame] | 5281 | waitForIdle(); |
| 5282 | |
| 5283 | StatusBarNotification[] notifs = mBinderService.getActiveNotifications(PKG); |
| 5284 | assertEquals(1, notifs.length); |
| 5285 | assertEquals((notifs[0].getNotification().flags & FLAG_BUBBLE), 0); |
| 5286 | assertFalse(mService.getNotificationRecord( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5287 | nr.getSbn().getKey()).getNotification().isBubbleNotification()); |
Mady Mellor | c682034 | 2019-05-20 12:04:36 -0700 | [diff] [blame] | 5288 | } |
| 5289 | |
| 5290 | @Test |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5291 | public void testFlagBubbleNotifs_noFlag_whenAppForeground() throws RemoteException { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 5292 | setUpPrefsForBubbles(PKG, mUid, |
| 5293 | true /* global */, |
| 5294 | BUBBLE_PREFERENCE_ALL /* app */, |
| 5295 | true /* channel */); |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 5296 | |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 5297 | // Notif with bubble metadata but not our other misc requirements |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5298 | Notification.Builder nb = new Notification.Builder(mContext, |
| 5299 | mTestNotificationChannel.getId()) |
| 5300 | .setContentTitle("foo") |
| 5301 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 5302 | .setBubbleMetadata(getBubbleMetadata()); |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5303 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, "tag", mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 5304 | nb.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5305 | NotificationRecord nr = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 5306 | |
| 5307 | // Say we're foreground |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5308 | when(mActivityManager.getPackageImportance(nr.getSbn().getPackageName())).thenReturn( |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 5309 | IMPORTANCE_FOREGROUND); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5310 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 5311 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 5312 | waitForIdle(); |
| 5313 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5314 | // if notif isn't configured properly it doesn't get to bubble just because app is |
| 5315 | // foreground. |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 5316 | assertFalse(mService.getNotificationRecord( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5317 | nr.getSbn().getKey()).getNotification().isBubbleNotification()); |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 5318 | } |
| 5319 | |
| 5320 | @Test |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 5321 | public void testFlagBubbleNotifs_flag_messaging() throws RemoteException { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 5322 | setUpPrefsForBubbles(PKG, mUid, |
| 5323 | true /* global */, |
| 5324 | BUBBLE_PREFERENCE_ALL /* app */, |
| 5325 | true /* channel */); |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 5326 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5327 | NotificationRecord nr = generateMessageBubbleNotifRecord(mTestNotificationChannel, |
| 5328 | "testFlagBubbleNotifs_flag_messaging"); |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 5329 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5330 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 5331 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 5332 | waitForIdle(); |
| 5333 | |
| 5334 | // yes allowed, yes messaging, yes bubble |
| 5335 | assertTrue(mService.getNotificationRecord( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5336 | nr.getSbn().getKey()).getNotification().isBubbleNotification()); |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 5337 | } |
| 5338 | |
| 5339 | @Test |
Mady Mellor | 6ce11e7 | 2020-05-29 14:07:24 -0700 | [diff] [blame] | 5340 | public void testFlagBubbleNotifs_noFlag_noShortcut() throws RemoteException { |
| 5341 | setUpPrefsForBubbles(PKG, mUid, |
| 5342 | true /* global */, |
| 5343 | BUBBLE_PREFERENCE_ALL /* app */, |
| 5344 | true /* channel */); |
| 5345 | |
| 5346 | Notification.Builder nb = getMessageStyleNotifBuilder(true, null, false); |
| 5347 | nb.setShortcutId(null); |
| 5348 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, |
| 5349 | null, mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 5350 | nb.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Mady Mellor | 6ce11e7 | 2020-05-29 14:07:24 -0700 | [diff] [blame] | 5351 | |
| 5352 | mBinderService.enqueueNotificationWithTag(PKG, PKG, sbn.getTag(), |
| 5353 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 5354 | waitForIdle(); |
| 5355 | |
| 5356 | // no shortcut no bubble |
| 5357 | assertFalse(mService.getNotificationRecord( |
| 5358 | sbn.getKey()).getNotification().isBubbleNotification()); |
| 5359 | } |
| 5360 | |
| 5361 | @Test |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 5362 | public void testFlagBubbleNotifs_noFlag_messaging_appNotAllowed() throws RemoteException { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 5363 | setUpPrefsForBubbles(PKG, mUid, |
| 5364 | true /* global */, |
| 5365 | BUBBLE_PREFERENCE_NONE /* app */, |
| 5366 | true /* channel */); |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 5367 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5368 | NotificationRecord nr = generateMessageBubbleNotifRecord(mTestNotificationChannel, |
| 5369 | "testFlagBubbleNotifs_noFlag_messaging_appNotAllowed"); |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 5370 | |
| 5371 | // Post the notification |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5372 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 5373 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 5374 | waitForIdle(); |
| 5375 | |
| 5376 | // not allowed, no bubble |
| 5377 | assertFalse(mService.getNotificationRecord( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5378 | nr.getSbn().getKey()).getNotification().isBubbleNotification()); |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 5379 | } |
| 5380 | |
| 5381 | @Test |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 5382 | public void testFlagBubbleNotifs_noFlag_notBubble() throws RemoteException { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 5383 | setUpPrefsForBubbles(PKG, mUid, |
| 5384 | true /* global */, |
| 5385 | BUBBLE_PREFERENCE_ALL /* app */, |
| 5386 | true /* channel */); |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 5387 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5388 | // Messaging notif WITHOUT bubble metadata |
| 5389 | Notification.Builder nb = getMessageStyleNotifBuilder(false /* addBubbleMetadata */, |
| 5390 | null /* groupKey */, false /* isSummary */); |
| 5391 | |
| 5392 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, |
| 5393 | "testFlagBubbleNotifs_noFlag_notBubble", mUid, 0, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 5394 | nb.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5395 | NotificationRecord nr = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 5396 | |
| 5397 | // Post the notification |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5398 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 5399 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 5400 | waitForIdle(); |
| 5401 | |
| 5402 | // no bubble metadata, no bubble |
| 5403 | assertFalse(mService.getNotificationRecord( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5404 | nr.getSbn().getKey()).getNotification().isBubbleNotification()); |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 5405 | } |
| 5406 | |
| 5407 | @Test |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 5408 | public void testFlagBubbleNotifs_noFlag_messaging_channelNotAllowed() throws RemoteException { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 5409 | setUpPrefsForBubbles(PKG, mUid, |
| 5410 | true /* global */, |
| 5411 | BUBBLE_PREFERENCE_ALL /* app */, |
| 5412 | false /* channel */); |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 5413 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5414 | NotificationRecord nr = generateMessageBubbleNotifRecord(mTestNotificationChannel, |
| 5415 | "testFlagBubbleNotifs_noFlag_messaging_channelNotAllowed"); |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 5416 | nr.getChannel().lockFields(USER_LOCKED_ALLOW_BUBBLE); |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 5417 | |
| 5418 | // Post the notification |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5419 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 5420 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 5421 | waitForIdle(); |
| 5422 | |
| 5423 | // channel not allowed, no bubble |
| 5424 | assertFalse(mService.getNotificationRecord( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5425 | nr.getSbn().getKey()).getNotification().isBubbleNotification()); |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 5426 | } |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 5427 | |
Mady Mellor | 49b1bf1 | 2019-03-29 12:00:02 -0700 | [diff] [blame] | 5428 | @Test |
Beverly | 9e6a764 | 2020-02-13 10:11:29 -0500 | [diff] [blame] | 5429 | public void testCancelNotificationsFromApp_cancelsBubbles() throws Exception { |
Mady Mellor | 49b1bf1 | 2019-03-29 12:00:02 -0700 | [diff] [blame] | 5430 | final NotificationRecord nrBubble = generateNotificationRecord(mTestNotificationChannel); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5431 | nrBubble.getSbn().getNotification().flags |= FLAG_BUBBLE; |
Mady Mellor | 49b1bf1 | 2019-03-29 12:00:02 -0700 | [diff] [blame] | 5432 | |
| 5433 | // Post the notification |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 5434 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 5435 | "testAppCancelNotifications_cancelsBubbles", |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5436 | nrBubble.getSbn().getId(), nrBubble.getSbn().getNotification(), |
| 5437 | nrBubble.getSbn().getUserId()); |
Mady Mellor | 49b1bf1 | 2019-03-29 12:00:02 -0700 | [diff] [blame] | 5438 | waitForIdle(); |
| 5439 | |
| 5440 | StatusBarNotification[] notifs = mBinderService.getActiveNotifications(PKG); |
| 5441 | assertEquals(1, notifs.length); |
| 5442 | assertEquals(1, mService.getNotificationRecordCount()); |
| 5443 | |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 5444 | mBinderService.cancelNotificationWithTag(PKG, PKG, |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5445 | "testAppCancelNotifications_cancelsBubbles", nrBubble.getSbn().getId(), |
| 5446 | nrBubble.getSbn().getUserId()); |
Mady Mellor | 49b1bf1 | 2019-03-29 12:00:02 -0700 | [diff] [blame] | 5447 | waitForIdle(); |
| 5448 | |
| 5449 | StatusBarNotification[] notifs2 = mBinderService.getActiveNotifications(PKG); |
| 5450 | assertEquals(0, notifs2.length); |
| 5451 | assertEquals(0, mService.getNotificationRecordCount()); |
| 5452 | } |
| 5453 | |
| 5454 | @Test |
Beverly | 9e6a764 | 2020-02-13 10:11:29 -0500 | [diff] [blame] | 5455 | public void testCancelAllNotificationsFromApp_cancelsBubble() throws Exception { |
| 5456 | final NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel); |
| 5457 | nr.getSbn().getNotification().flags |= FLAG_BUBBLE; |
| 5458 | mService.addNotification(nr); |
| 5459 | |
| 5460 | mBinderService.cancelAllNotifications(PKG, nr.getSbn().getUserId()); |
| 5461 | waitForIdle(); |
| 5462 | |
| 5463 | StatusBarNotification[] notifs = mBinderService.getActiveNotifications(PKG); |
| 5464 | assertEquals(0, notifs.length); |
| 5465 | assertEquals(0, mService.getNotificationRecordCount()); |
| 5466 | } |
| 5467 | |
| 5468 | @Test |
Mady Mellor | 49b1bf1 | 2019-03-29 12:00:02 -0700 | [diff] [blame] | 5469 | public void testCancelAllNotificationsFromListener_ignoresBubbles() throws Exception { |
| 5470 | final NotificationRecord nrNormal = generateNotificationRecord(mTestNotificationChannel); |
| 5471 | final NotificationRecord nrBubble = generateNotificationRecord(mTestNotificationChannel); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5472 | nrBubble.getSbn().getNotification().flags |= FLAG_BUBBLE; |
Mady Mellor | 49b1bf1 | 2019-03-29 12:00:02 -0700 | [diff] [blame] | 5473 | |
| 5474 | mService.addNotification(nrNormal); |
| 5475 | mService.addNotification(nrBubble); |
| 5476 | |
| 5477 | mService.getBinderService().cancelNotificationsFromListener(null, null); |
| 5478 | waitForIdle(); |
| 5479 | |
| 5480 | StatusBarNotification[] notifs = mBinderService.getActiveNotifications(PKG); |
| 5481 | assertEquals(1, notifs.length); |
| 5482 | assertEquals(1, mService.getNotificationRecordCount()); |
| 5483 | } |
| 5484 | |
| 5485 | @Test |
Lyn Han | 675570a | 2020-06-05 23:31:22 -0700 | [diff] [blame] | 5486 | public void testCancelNotificationsFromListener_cancelsNonBubble() throws Exception { |
| 5487 | // Add non-bubble notif |
| 5488 | final NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel); |
| 5489 | mService.addNotification(nr); |
Mady Mellor | 49b1bf1 | 2019-03-29 12:00:02 -0700 | [diff] [blame] | 5490 | |
Lyn Han | 675570a | 2020-06-05 23:31:22 -0700 | [diff] [blame] | 5491 | // Cancel via listener |
| 5492 | String[] keys = {nr.getSbn().getKey()}; |
Mady Mellor | 49b1bf1 | 2019-03-29 12:00:02 -0700 | [diff] [blame] | 5493 | mService.getBinderService().cancelNotificationsFromListener(null, keys); |
| 5494 | waitForIdle(); |
| 5495 | |
Lyn Han | 675570a | 2020-06-05 23:31:22 -0700 | [diff] [blame] | 5496 | // Notif not active anymore |
| 5497 | StatusBarNotification[] notifs = mBinderService.getActiveNotifications(PKG); |
| 5498 | assertEquals(0, notifs.length); |
| 5499 | assertEquals(0, mService.getNotificationRecordCount()); |
| 5500 | // Cancel event is logged |
| 5501 | assertEquals(1, mNotificationRecordLogger.numCalls()); |
| 5502 | assertEquals(NotificationRecordLogger.NotificationCancelledEvent |
| 5503 | .NOTIFICATION_CANCEL_LISTENER_CANCEL, mNotificationRecordLogger.event(0)); |
| 5504 | } |
| 5505 | |
| 5506 | @Test |
| 5507 | public void testCancelNotificationsFromListener_suppressesBubble() throws Exception { |
| 5508 | // Add bubble notif |
| 5509 | setUpPrefsForBubbles(PKG, mUid, |
| 5510 | true /* global */, |
| 5511 | BUBBLE_PREFERENCE_ALL /* app */, |
| 5512 | true /* channel */); |
| 5513 | NotificationRecord nr = generateMessageBubbleNotifRecord(mTestNotificationChannel, "tag"); |
| 5514 | |
| 5515 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 5516 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
| 5517 | waitForIdle(); |
| 5518 | |
| 5519 | // Cancel via listener |
| 5520 | String[] keys = {nr.getSbn().getKey()}; |
| 5521 | mService.getBinderService().cancelNotificationsFromListener(null, keys); |
| 5522 | waitForIdle(); |
| 5523 | |
| 5524 | // Bubble notif active and suppressed |
Mady Mellor | 49b1bf1 | 2019-03-29 12:00:02 -0700 | [diff] [blame] | 5525 | StatusBarNotification[] notifs = mBinderService.getActiveNotifications(PKG); |
| 5526 | assertEquals(1, notifs.length); |
| 5527 | assertEquals(1, mService.getNotificationRecordCount()); |
Lyn Han | 675570a | 2020-06-05 23:31:22 -0700 | [diff] [blame] | 5528 | assertTrue(notifs[0].getNotification().getBubbleMetadata().isNotificationSuppressed()); |
Mady Mellor | 49b1bf1 | 2019-03-29 12:00:02 -0700 | [diff] [blame] | 5529 | } |
Julia Reynolds | ad6dd35 | 2019-03-07 16:46:22 -0500 | [diff] [blame] | 5530 | |
Julia Reynolds | 4214da9 | 2019-04-10 15:04:06 -0400 | [diff] [blame] | 5531 | @Test |
Beverly | 9e6a764 | 2020-02-13 10:11:29 -0500 | [diff] [blame] | 5532 | public void testCancelAllNotificationsFromStatusBar_ignoresBubble() throws Exception { |
| 5533 | // GIVEN a notification bubble |
| 5534 | final NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel); |
| 5535 | nr.getSbn().getNotification().flags |= FLAG_BUBBLE; |
| 5536 | mService.addNotification(nr); |
| 5537 | |
| 5538 | // WHEN the status bar clears all notifications |
| 5539 | mService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(), |
| 5540 | nr.getSbn().getUserId()); |
| 5541 | waitForIdle(); |
| 5542 | |
| 5543 | // THEN the bubble notification does not get removed |
| 5544 | StatusBarNotification[] notifs = mBinderService.getActiveNotifications(PKG); |
| 5545 | assertEquals(1, notifs.length); |
| 5546 | assertEquals(1, mService.getNotificationRecordCount()); |
| 5547 | } |
| 5548 | |
| 5549 | |
| 5550 | @Test |
Julia Reynolds | 088c448 | 2019-04-10 12:43:27 -0400 | [diff] [blame] | 5551 | public void testGetAllowedAssistantAdjustments() throws Exception { |
| 5552 | List<String> capabilities = mBinderService.getAllowedAssistantAdjustments(null); |
Julia Reynolds | ad6dd35 | 2019-03-07 16:46:22 -0500 | [diff] [blame] | 5553 | assertNotNull(capabilities); |
| 5554 | |
| 5555 | for (int i = capabilities.size() - 1; i >= 0; i--) { |
| 5556 | String capability = capabilities.get(i); |
Julia Reynolds | 088c448 | 2019-04-10 12:43:27 -0400 | [diff] [blame] | 5557 | mBinderService.disallowAssistantAdjustment(capability); |
| 5558 | assertEquals(i + 1, mBinderService.getAllowedAssistantAdjustments(null).size()); |
| 5559 | List<String> currentCapabilities = mBinderService.getAllowedAssistantAdjustments(null); |
Julia Reynolds | ad6dd35 | 2019-03-07 16:46:22 -0500 | [diff] [blame] | 5560 | assertNotNull(currentCapabilities); |
| 5561 | assertFalse(currentCapabilities.contains(capability)); |
| 5562 | } |
| 5563 | } |
Julia Reynolds | c7dcdc2 | 2019-03-25 10:26:14 -0400 | [diff] [blame] | 5564 | |
Julia Reynolds | 4214da9 | 2019-04-10 15:04:06 -0400 | [diff] [blame] | 5565 | @Test |
Julia Reynolds | c7dcdc2 | 2019-03-25 10:26:14 -0400 | [diff] [blame] | 5566 | public void testAdjustRestrictedKey() throws Exception { |
| 5567 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
Julia Reynolds | 4214da9 | 2019-04-10 15:04:06 -0400 | [diff] [blame] | 5568 | mService.addNotification(r); |
| 5569 | when(mAssistants.isSameUser(any(), anyInt())).thenReturn(true); |
Julia Reynolds | c7dcdc2 | 2019-03-25 10:26:14 -0400 | [diff] [blame] | 5570 | |
| 5571 | when(mAssistants.isAdjustmentAllowed(KEY_IMPORTANCE)).thenReturn(true); |
| 5572 | when(mAssistants.isAdjustmentAllowed(KEY_USER_SENTIMENT)).thenReturn(false); |
| 5573 | |
| 5574 | Bundle signals = new Bundle(); |
| 5575 | signals.putInt(KEY_IMPORTANCE, IMPORTANCE_LOW); |
| 5576 | signals.putInt(KEY_USER_SENTIMENT, USER_SENTIMENT_NEGATIVE); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5577 | Adjustment adjustment = new Adjustment(r.getSbn().getPackageName(), r.getKey(), signals, |
Julia Reynolds | c7dcdc2 | 2019-03-25 10:26:14 -0400 | [diff] [blame] | 5578 | "", r.getUser().getIdentifier()); |
| 5579 | |
| 5580 | mBinderService.applyAdjustmentFromAssistant(null, adjustment); |
| 5581 | r.applyAdjustments(); |
| 5582 | |
| 5583 | assertEquals(IMPORTANCE_LOW, r.getAssistantImportance()); |
| 5584 | assertEquals(USER_SENTIMENT_NEUTRAL, r.getUserSentiment()); |
| 5585 | } |
Julia Reynolds | 657d164 | 2019-03-27 12:15:57 -0400 | [diff] [blame] | 5586 | |
Julia Reynolds | 4214da9 | 2019-04-10 15:04:06 -0400 | [diff] [blame] | 5587 | @Test |
Julia Reynolds | a94365d | 2019-04-09 10:48:43 -0400 | [diff] [blame] | 5588 | public void testAutomaticZenRuleValidation_policyFilterAgreement() throws Exception { |
Julia Reynolds | 4214da9 | 2019-04-10 15:04:06 -0400 | [diff] [blame] | 5589 | when(mConditionProviders.isPackageOrComponentAllowed(anyString(), anyInt())) |
| 5590 | .thenReturn(true); |
| 5591 | mService.setZenHelper(mock(ZenModeHelper.class)); |
| 5592 | ComponentName owner = new ComponentName(mContext, this.getClass()); |
Julia Reynolds | a94365d | 2019-04-09 10:48:43 -0400 | [diff] [blame] | 5593 | ZenPolicy zenPolicy = new ZenPolicy.Builder().allowAlarms(true).build(); |
| 5594 | boolean isEnabled = true; |
| 5595 | AutomaticZenRule rule = new AutomaticZenRule("test", owner, owner, mock(Uri.class), |
| 5596 | zenPolicy, NotificationManager.INTERRUPTION_FILTER_NONE, isEnabled); |
| 5597 | |
| 5598 | try { |
| 5599 | mBinderService.addAutomaticZenRule(rule); |
Julia Reynolds | 4214da9 | 2019-04-10 15:04:06 -0400 | [diff] [blame] | 5600 | fail("Zen policy only applies to priority only mode"); |
Julia Reynolds | a94365d | 2019-04-09 10:48:43 -0400 | [diff] [blame] | 5601 | } catch (IllegalArgumentException e) { |
| 5602 | // yay |
| 5603 | } |
| 5604 | |
| 5605 | rule = new AutomaticZenRule("test", owner, owner, mock(Uri.class), |
| 5606 | zenPolicy, NotificationManager.INTERRUPTION_FILTER_PRIORITY, isEnabled); |
| 5607 | mBinderService.addAutomaticZenRule(rule); |
| 5608 | |
| 5609 | rule = new AutomaticZenRule("test", owner, owner, mock(Uri.class), |
| 5610 | null, NotificationManager.INTERRUPTION_FILTER_NONE, isEnabled); |
| 5611 | mBinderService.addAutomaticZenRule(rule); |
| 5612 | } |
| 5613 | |
Julia Reynolds | 4214da9 | 2019-04-10 15:04:06 -0400 | [diff] [blame] | 5614 | @Test |
Julia Reynolds | 657d164 | 2019-03-27 12:15:57 -0400 | [diff] [blame] | 5615 | public void testAreNotificationsEnabledForPackage_crossUser() throws Exception { |
| 5616 | try { |
| 5617 | mBinderService.areNotificationsEnabledForPackage(mContext.getPackageName(), |
| 5618 | mUid + UserHandle.PER_USER_RANGE); |
| 5619 | fail("Cannot call cross user without permission"); |
| 5620 | } catch (SecurityException e) { |
| 5621 | // pass |
| 5622 | } |
| 5623 | |
| 5624 | // cross user, with permission, no problem |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 5625 | enableInteractAcrossUsers(); |
Julia Reynolds | 657d164 | 2019-03-27 12:15:57 -0400 | [diff] [blame] | 5626 | mBinderService.areNotificationsEnabledForPackage(mContext.getPackageName(), |
| 5627 | mUid + UserHandle.PER_USER_RANGE); |
| 5628 | } |
Julia Reynolds | 2f7592d | 2019-03-27 12:17:23 -0400 | [diff] [blame] | 5629 | |
Julia Reynolds | 4214da9 | 2019-04-10 15:04:06 -0400 | [diff] [blame] | 5630 | @Test |
Julia Reynolds | 2f7592d | 2019-03-27 12:17:23 -0400 | [diff] [blame] | 5631 | public void testAreBubblesAllowedForPackage_crossUser() throws Exception { |
| 5632 | try { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 5633 | mBinderService.getBubblePreferenceForPackage(mContext.getPackageName(), |
Julia Reynolds | 2f7592d | 2019-03-27 12:17:23 -0400 | [diff] [blame] | 5634 | mUid + UserHandle.PER_USER_RANGE); |
| 5635 | fail("Cannot call cross user without permission"); |
| 5636 | } catch (SecurityException e) { |
| 5637 | // pass |
| 5638 | } |
| 5639 | |
| 5640 | // cross user, with permission, no problem |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 5641 | enableInteractAcrossUsers(); |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 5642 | mBinderService.getBubblePreferenceForPackage(mContext.getPackageName(), |
Julia Reynolds | 2f7592d | 2019-03-27 12:17:23 -0400 | [diff] [blame] | 5643 | mUid + UserHandle.PER_USER_RANGE); |
| 5644 | } |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5645 | |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 5646 | private void enableInteractAcrossUsers() { |
| 5647 | TestablePermissions perms = mContext.getTestablePermissions(); |
| 5648 | perms.setPermission(android.Manifest.permission.INTERACT_ACROSS_USERS, PERMISSION_GRANTED); |
| 5649 | } |
| 5650 | |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5651 | @Test |
| 5652 | public void testNotificationBubbleChanged_false() throws Exception { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 5653 | setUpPrefsForBubbles(PKG, mUid, |
| 5654 | true /* global */, |
| 5655 | BUBBLE_PREFERENCE_ALL /* app */, |
| 5656 | true /* channel */); |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5657 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5658 | // Notif with bubble metadata |
| 5659 | NotificationRecord nr = generateMessageBubbleNotifRecord(mTestNotificationChannel, |
| 5660 | "testNotificationBubbleChanged_false"); |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5661 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5662 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 5663 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5664 | waitForIdle(); |
| 5665 | |
Mady Mellor | 66efd5e | 2019-05-15 13:38:11 -0700 | [diff] [blame] | 5666 | // Reset as this is called when the notif is first sent |
| 5667 | reset(mListeners); |
| 5668 | |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5669 | // First we were a bubble |
| 5670 | StatusBarNotification[] notifsBefore = mBinderService.getActiveNotifications(PKG); |
| 5671 | assertEquals(1, notifsBefore.length); |
| 5672 | assertTrue((notifsBefore[0].getNotification().flags & FLAG_BUBBLE) != 0); |
| 5673 | |
| 5674 | // Notify we're not a bubble |
Mady Mellor | 9adfe6a | 2020-03-30 17:23:26 -0700 | [diff] [blame] | 5675 | mService.mNotificationDelegate.onNotificationBubbleChanged(nr.getKey(), false, 0); |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5676 | waitForIdle(); |
| 5677 | |
Mady Mellor | 3a0a1b4 | 2019-05-23 06:40:21 -0700 | [diff] [blame] | 5678 | // Make sure we are not a bubble |
| 5679 | StatusBarNotification[] notifsAfter = mBinderService.getActiveNotifications(PKG); |
| 5680 | assertEquals(1, notifsAfter.length); |
| 5681 | assertEquals((notifsAfter[0].getNotification().flags & FLAG_BUBBLE), 0); |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5682 | } |
| 5683 | |
| 5684 | @Test |
| 5685 | public void testNotificationBubbleChanged_true() throws Exception { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 5686 | setUpPrefsForBubbles(PKG, mUid, |
| 5687 | true /* global */, |
| 5688 | BUBBLE_PREFERENCE_ALL /* app */, |
| 5689 | true /* channel */); |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5690 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5691 | // Notif that is not a bubble |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5692 | NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel, |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5693 | 1, null, false); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5694 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 5695 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5696 | waitForIdle(); |
| 5697 | |
| 5698 | // Would be a normal notification because wouldn't have met requirements to bubble |
| 5699 | StatusBarNotification[] notifsBefore = mBinderService.getActiveNotifications(PKG); |
| 5700 | assertEquals(1, notifsBefore.length); |
| 5701 | assertEquals((notifsBefore[0].getNotification().flags & FLAG_BUBBLE), 0); |
| 5702 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5703 | // Update the notification to be message style / meet bubble requirements |
| 5704 | NotificationRecord nr2 = generateMessageBubbleNotifRecord(mTestNotificationChannel, |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5705 | nr.getSbn().getTag()); |
| 5706 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr2.getSbn().getTag(), |
| 5707 | nr2.getSbn().getId(), nr2.getSbn().getNotification(), nr2.getSbn().getUserId()); |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5708 | waitForIdle(); |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5709 | |
Mady Mellor | 66efd5e | 2019-05-15 13:38:11 -0700 | [diff] [blame] | 5710 | // Reset as this is called when the notif is first sent |
| 5711 | reset(mListeners); |
| 5712 | |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5713 | // Notify we are now a bubble |
Mady Mellor | 9adfe6a | 2020-03-30 17:23:26 -0700 | [diff] [blame] | 5714 | mService.mNotificationDelegate.onNotificationBubbleChanged(nr.getKey(), true, 0); |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5715 | waitForIdle(); |
| 5716 | |
Mady Mellor | 3a0a1b4 | 2019-05-23 06:40:21 -0700 | [diff] [blame] | 5717 | // Make sure we are a bubble |
| 5718 | StatusBarNotification[] notifsAfter = mBinderService.getActiveNotifications(PKG); |
| 5719 | assertEquals(1, notifsAfter.length); |
| 5720 | assertTrue((notifsAfter[0].getNotification().flags & FLAG_BUBBLE) != 0); |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5721 | } |
| 5722 | |
| 5723 | @Test |
| 5724 | public void testNotificationBubbleChanged_true_notAllowed() throws Exception { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 5725 | setUpPrefsForBubbles(PKG, mUid, |
| 5726 | true /* global */, |
| 5727 | BUBBLE_PREFERENCE_ALL /* app */, |
| 5728 | true /* channel */); |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5729 | |
| 5730 | // Notif that is not a bubble |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5731 | NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5732 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 5733 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5734 | waitForIdle(); |
| 5735 | |
Mady Mellor | 66efd5e | 2019-05-15 13:38:11 -0700 | [diff] [blame] | 5736 | // Reset as this is called when the notif is first sent |
| 5737 | reset(mListeners); |
| 5738 | |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5739 | // Would be a normal notification because wouldn't have met requirements to bubble |
| 5740 | StatusBarNotification[] notifsBefore = mBinderService.getActiveNotifications(PKG); |
| 5741 | assertEquals(1, notifsBefore.length); |
| 5742 | assertEquals((notifsBefore[0].getNotification().flags & FLAG_BUBBLE), 0); |
| 5743 | |
| 5744 | // Notify we are now a bubble |
Mady Mellor | 9adfe6a | 2020-03-30 17:23:26 -0700 | [diff] [blame] | 5745 | mService.mNotificationDelegate.onNotificationBubbleChanged(nr.getKey(), true, 0); |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5746 | waitForIdle(); |
| 5747 | |
| 5748 | // We still wouldn't be a bubble because the notification didn't meet requirements |
| 5749 | StatusBarNotification[] notifsAfter = mBinderService.getActiveNotifications(PKG); |
| 5750 | assertEquals(1, notifsAfter.length); |
| 5751 | assertEquals((notifsAfter[0].getNotification().flags & FLAG_BUBBLE), 0); |
| 5752 | } |
Mady Mellor | 5c11a2e | 2019-04-25 17:26:15 -0700 | [diff] [blame] | 5753 | |
| 5754 | @Test |
Mady Mellor | 13f9bc8 | 2020-03-24 19:09:28 -0700 | [diff] [blame] | 5755 | public void testNotificationBubbleIsFlagRemoved_resetOnUpdate() throws Exception { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 5756 | setUpPrefsForBubbles(PKG, mUid, |
| 5757 | true /* global */, |
| 5758 | BUBBLE_PREFERENCE_ALL /* app */, |
| 5759 | true /* channel */); |
| 5760 | |
Mady Mellor | 13f9bc8 | 2020-03-24 19:09:28 -0700 | [diff] [blame] | 5761 | // Notif with bubble metadata |
| 5762 | NotificationRecord nr = generateMessageBubbleNotifRecord(mTestNotificationChannel, |
| 5763 | "testNotificationBubbleIsFlagRemoved_resetOnUpdate"); |
| 5764 | |
| 5765 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 5766 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
| 5767 | waitForIdle(); |
| 5768 | // Flag shouldn't be modified |
| 5769 | NotificationRecord recordToCheck = mService.getNotificationRecord(nr.getSbn().getKey()); |
| 5770 | assertFalse(recordToCheck.isFlagBubbleRemoved()); |
| 5771 | |
| 5772 | // Notify we're not a bubble |
Mady Mellor | 9adfe6a | 2020-03-30 17:23:26 -0700 | [diff] [blame] | 5773 | mService.mNotificationDelegate.onNotificationBubbleChanged(nr.getKey(), false, 0); |
Mady Mellor | 13f9bc8 | 2020-03-24 19:09:28 -0700 | [diff] [blame] | 5774 | waitForIdle(); |
| 5775 | // Flag should be modified |
| 5776 | recordToCheck = mService.getNotificationRecord(nr.getSbn().getKey()); |
| 5777 | assertTrue(recordToCheck.isFlagBubbleRemoved()); |
| 5778 | |
| 5779 | |
| 5780 | // Update the notif |
| 5781 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 5782 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
| 5783 | waitForIdle(); |
| 5784 | // And the flag is reset |
| 5785 | recordToCheck = mService.getNotificationRecord(nr.getSbn().getKey()); |
| 5786 | assertFalse(recordToCheck.isFlagBubbleRemoved()); |
| 5787 | } |
| 5788 | |
| 5789 | @Test |
| 5790 | public void testNotificationBubbleIsFlagRemoved_resetOnBubbleChangedTrue() throws Exception { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 5791 | setUpPrefsForBubbles(PKG, mUid, |
| 5792 | true /* global */, |
| 5793 | BUBBLE_PREFERENCE_ALL /* app */, |
| 5794 | true /* channel */); |
| 5795 | |
Mady Mellor | 13f9bc8 | 2020-03-24 19:09:28 -0700 | [diff] [blame] | 5796 | // Notif with bubble metadata |
| 5797 | NotificationRecord nr = generateMessageBubbleNotifRecord(mTestNotificationChannel, |
| 5798 | "testNotificationBubbleIsFlagRemoved_trueOnBubbleChangedTrue"); |
| 5799 | |
| 5800 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 5801 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
| 5802 | waitForIdle(); |
| 5803 | // Flag shouldn't be modified |
| 5804 | NotificationRecord recordToCheck = mService.getNotificationRecord(nr.getSbn().getKey()); |
| 5805 | assertFalse(recordToCheck.isFlagBubbleRemoved()); |
| 5806 | |
| 5807 | // Notify we're not a bubble |
Mady Mellor | 9adfe6a | 2020-03-30 17:23:26 -0700 | [diff] [blame] | 5808 | mService.mNotificationDelegate.onNotificationBubbleChanged(nr.getKey(), false, 0); |
Mady Mellor | 13f9bc8 | 2020-03-24 19:09:28 -0700 | [diff] [blame] | 5809 | waitForIdle(); |
| 5810 | // Flag should be modified |
| 5811 | recordToCheck = mService.getNotificationRecord(nr.getSbn().getKey()); |
| 5812 | assertTrue(recordToCheck.isFlagBubbleRemoved()); |
| 5813 | |
| 5814 | // Notify we are a bubble |
Mady Mellor | 9adfe6a | 2020-03-30 17:23:26 -0700 | [diff] [blame] | 5815 | mService.mNotificationDelegate.onNotificationBubbleChanged(nr.getKey(), true, 0); |
Mady Mellor | 13f9bc8 | 2020-03-24 19:09:28 -0700 | [diff] [blame] | 5816 | waitForIdle(); |
| 5817 | // And the flag is reset |
| 5818 | assertFalse(recordToCheck.isFlagBubbleRemoved()); |
| 5819 | } |
| 5820 | |
| 5821 | @Test |
Mady Mellor | f44b683 | 2020-01-14 13:26:14 -0800 | [diff] [blame] | 5822 | public void testOnBubbleNotificationSuppressionChanged() throws Exception { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 5823 | setUpPrefsForBubbles(PKG, mUid, |
| 5824 | true /* global */, |
| 5825 | BUBBLE_PREFERENCE_ALL /* app */, |
| 5826 | true /* channel */); |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 5827 | |
Mady Mellor | f44b683 | 2020-01-14 13:26:14 -0800 | [diff] [blame] | 5828 | // Bubble notification |
| 5829 | NotificationRecord nr = generateMessageBubbleNotifRecord(mTestNotificationChannel, "tag"); |
| 5830 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5831 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 5832 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Mady Mellor | f44b683 | 2020-01-14 13:26:14 -0800 | [diff] [blame] | 5833 | waitForIdle(); |
| 5834 | |
| 5835 | // NOT suppressed |
| 5836 | Notification n = mBinderService.getActiveNotifications(PKG)[0].getNotification(); |
| 5837 | assertFalse(n.getBubbleMetadata().isNotificationSuppressed()); |
| 5838 | |
| 5839 | // Reset as this is called when the notif is first sent |
| 5840 | reset(mListeners); |
| 5841 | |
| 5842 | // Test: update suppression to true |
| 5843 | mService.mNotificationDelegate.onBubbleNotificationSuppressionChanged(nr.getKey(), true); |
| 5844 | waitForIdle(); |
| 5845 | |
| 5846 | // Check |
| 5847 | n = mBinderService.getActiveNotifications(PKG)[0].getNotification(); |
| 5848 | assertTrue(n.getBubbleMetadata().isNotificationSuppressed()); |
| 5849 | |
| 5850 | // Reset to check again |
| 5851 | reset(mListeners); |
| 5852 | |
| 5853 | // Test: update suppression to false |
| 5854 | mService.mNotificationDelegate.onBubbleNotificationSuppressionChanged(nr.getKey(), false); |
| 5855 | waitForIdle(); |
| 5856 | |
| 5857 | // Check |
| 5858 | n = mBinderService.getActiveNotifications(PKG)[0].getNotification(); |
| 5859 | assertFalse(n.getBubbleMetadata().isNotificationSuppressed()); |
| 5860 | } |
| 5861 | |
| 5862 | @Test |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 5863 | public void testGrantInlineReplyUriPermission_recordExists() throws Exception { |
| 5864 | NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel, 0); |
| 5865 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5866 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 5867 | waitForIdle(); |
| 5868 | |
| 5869 | // A notification exists for the given record |
| 5870 | StatusBarNotification[] notifsBefore = mBinderService.getActiveNotifications(PKG); |
| 5871 | assertEquals(1, notifsBefore.length); |
| 5872 | |
| 5873 | reset(mPackageManager); |
| 5874 | |
| 5875 | Uri uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 1); |
| 5876 | |
| 5877 | mService.mNotificationDelegate.grantInlineReplyUriPermission( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5878 | nr.getKey(), uri, nr.getSbn().getUser(), nr.getSbn().getPackageName(), |
| 5879 | nr.getSbn().getUid()); |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 5880 | |
| 5881 | // Grant permission called for the UID of SystemUI under the target user ID |
| 5882 | verify(mUgm, times(1)).grantUriPermissionFromOwner(any(), |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5883 | eq(nr.getSbn().getUid()), eq(nr.getSbn().getPackageName()), eq(uri), anyInt(), |
| 5884 | anyInt(), eq(nr.getSbn().getUserId())); |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 5885 | } |
| 5886 | |
| 5887 | @Test |
Aran Ink | 979c976 | 2019-10-24 16:09:45 -0400 | [diff] [blame] | 5888 | public void testGrantInlineReplyUriPermission_noRecordExists() throws Exception { |
| 5889 | NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel, 0); |
| 5890 | waitForIdle(); |
| 5891 | |
| 5892 | // No notifications exist for the given record |
| 5893 | StatusBarNotification[] notifsBefore = mBinderService.getActiveNotifications(PKG); |
| 5894 | assertEquals(0, notifsBefore.length); |
| 5895 | |
| 5896 | Uri uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 1); |
| 5897 | int uid = 0; // sysui on primary user |
| 5898 | |
| 5899 | mService.mNotificationDelegate.grantInlineReplyUriPermission( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5900 | nr.getKey(), uri, nr.getSbn().getUser(), nr.getSbn().getPackageName(), |
| 5901 | nr.getSbn().getUid()); |
Aran Ink | 979c976 | 2019-10-24 16:09:45 -0400 | [diff] [blame] | 5902 | |
| 5903 | // Grant permission still called if no NotificationRecord exists for the given key |
| 5904 | verify(mUgm, times(1)).grantUriPermissionFromOwner(any(), |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5905 | eq(nr.getSbn().getUid()), eq(nr.getSbn().getPackageName()), eq(uri), anyInt(), |
| 5906 | anyInt(), eq(nr.getSbn().getUserId())); |
Aran Ink | 979c976 | 2019-10-24 16:09:45 -0400 | [diff] [blame] | 5907 | } |
| 5908 | |
| 5909 | @Test |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 5910 | public void testGrantInlineReplyUriPermission_userAll() throws Exception { |
| 5911 | // generate a NotificationRecord for USER_ALL to make sure it's converted into USER_SYSTEM |
| 5912 | NotificationRecord nr = |
| 5913 | generateNotificationRecord(mTestNotificationChannel, UserHandle.USER_ALL); |
| 5914 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5915 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 5916 | waitForIdle(); |
| 5917 | |
| 5918 | // A notification exists for the given record |
| 5919 | StatusBarNotification[] notifsBefore = mBinderService.getActiveNotifications(PKG); |
| 5920 | assertEquals(1, notifsBefore.length); |
| 5921 | |
| 5922 | reset(mPackageManager); |
| 5923 | |
| 5924 | Uri uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 1); |
| 5925 | |
| 5926 | mService.mNotificationDelegate.grantInlineReplyUriPermission( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5927 | nr.getKey(), uri, nr.getSbn().getUser(), nr.getSbn().getPackageName(), |
| 5928 | nr.getSbn().getUid()); |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 5929 | |
| 5930 | // Target user for the grant is USER_ALL instead of USER_SYSTEM |
| 5931 | verify(mUgm, times(1)).grantUriPermissionFromOwner(any(), |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5932 | eq(nr.getSbn().getUid()), eq(nr.getSbn().getPackageName()), eq(uri), anyInt(), |
| 5933 | anyInt(), eq(UserHandle.USER_SYSTEM)); |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 5934 | } |
| 5935 | |
| 5936 | @Test |
| 5937 | public void testGrantInlineReplyUriPermission_acrossUsers() throws Exception { |
| 5938 | // generate a NotificationRecord for USER_ALL to make sure it's converted into USER_SYSTEM |
| 5939 | int otherUserId = 11; |
| 5940 | NotificationRecord nr = |
| 5941 | generateNotificationRecord(mTestNotificationChannel, otherUserId); |
| 5942 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5943 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 5944 | waitForIdle(); |
| 5945 | |
| 5946 | // A notification exists for the given record |
| 5947 | StatusBarNotification[] notifsBefore = mBinderService.getActiveNotifications(PKG); |
| 5948 | assertEquals(1, notifsBefore.length); |
| 5949 | |
| 5950 | reset(mPackageManager); |
| 5951 | |
| 5952 | Uri uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 1); |
| 5953 | |
| 5954 | int uid = 0; // sysui on primary user |
Aran Ink | 979c976 | 2019-10-24 16:09:45 -0400 | [diff] [blame] | 5955 | int otherUserUid = (otherUserId * 100000) + 1; // sysui as a different user |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 5956 | String sysuiPackage = "sysui"; |
| 5957 | final String[] sysuiPackages = new String[] { sysuiPackage }; |
| 5958 | when(mPackageManager.getPackagesForUid(uid)).thenReturn(sysuiPackages); |
| 5959 | |
| 5960 | // Make sure to mock call for USER_SYSTEM and not USER_ALL, since it's been replaced by the |
| 5961 | // time this is called |
| 5962 | when(mPackageManager.getPackageUid(sysuiPackage, 0, otherUserId)) |
| 5963 | .thenReturn(otherUserUid); |
| 5964 | |
Aran Ink | 979c976 | 2019-10-24 16:09:45 -0400 | [diff] [blame] | 5965 | mService.mNotificationDelegate.grantInlineReplyUriPermission( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5966 | nr.getKey(), uri, nr.getSbn().getUser(), nr.getSbn().getPackageName(), uid); |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 5967 | |
| 5968 | // Target user for the grant is USER_ALL instead of USER_SYSTEM |
| 5969 | verify(mUgm, times(1)).grantUriPermissionFromOwner(any(), |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5970 | eq(otherUserUid), eq(nr.getSbn().getPackageName()), eq(uri), anyInt(), anyInt(), |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 5971 | eq(otherUserId)); |
| 5972 | } |
| 5973 | |
| 5974 | @Test |
Aran Ink | 979c976 | 2019-10-24 16:09:45 -0400 | [diff] [blame] | 5975 | public void testClearInlineReplyUriPermission_uriRecordExists() throws Exception { |
| 5976 | NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel, 0); |
| 5977 | reset(mPackageManager); |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 5978 | |
Aran Ink | 979c976 | 2019-10-24 16:09:45 -0400 | [diff] [blame] | 5979 | Uri uri1 = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 1); |
| 5980 | Uri uri2 = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 2); |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 5981 | |
Aran Ink | 979c976 | 2019-10-24 16:09:45 -0400 | [diff] [blame] | 5982 | // create an inline record with two uris in it |
| 5983 | mService.mNotificationDelegate.grantInlineReplyUriPermission( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5984 | nr.getKey(), uri1, nr.getSbn().getUser(), nr.getSbn().getPackageName(), |
| 5985 | nr.getSbn().getUid()); |
Aran Ink | 979c976 | 2019-10-24 16:09:45 -0400 | [diff] [blame] | 5986 | mService.mNotificationDelegate.grantInlineReplyUriPermission( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5987 | nr.getKey(), uri2, nr.getSbn().getUser(), nr.getSbn().getPackageName(), |
| 5988 | nr.getSbn().getUid()); |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 5989 | |
Aran Ink | 979c976 | 2019-10-24 16:09:45 -0400 | [diff] [blame] | 5990 | InlineReplyUriRecord record = mService.mInlineReplyRecordsByKey.get(nr.getKey()); |
| 5991 | assertNotNull(record); // record exists |
| 5992 | assertEquals(record.getUris().size(), 2); // record has two uris in it |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 5993 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5994 | mService.mNotificationDelegate.clearInlineReplyUriPermissions(nr.getKey(), |
| 5995 | nr.getSbn().getUid()); |
Aran Ink | 979c976 | 2019-10-24 16:09:45 -0400 | [diff] [blame] | 5996 | |
| 5997 | // permissionOwner destroyed |
| 5998 | verify(mUgmInternal, times(1)).revokeUriPermissionFromOwner( |
| 5999 | eq(record.getPermissionOwner()), eq(null), eq(~0), eq(nr.getUserId())); |
| 6000 | } |
| 6001 | |
| 6002 | |
| 6003 | @Test |
| 6004 | public void testClearInlineReplyUriPermission_noUriRecordExists() throws Exception { |
| 6005 | NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel, 0); |
| 6006 | reset(mPackageManager); |
| 6007 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 6008 | mService.mNotificationDelegate.clearInlineReplyUriPermissions(nr.getKey(), |
| 6009 | nr.getSbn().getUid()); |
Aran Ink | 979c976 | 2019-10-24 16:09:45 -0400 | [diff] [blame] | 6010 | |
| 6011 | // no permissionOwner destroyed |
| 6012 | verify(mUgmInternal, times(0)).revokeUriPermissionFromOwner( |
| 6013 | any(), eq(null), eq(~0), eq(nr.getUserId())); |
| 6014 | } |
| 6015 | |
| 6016 | @Test |
| 6017 | public void testClearInlineReplyUriPermission_userAll() throws Exception { |
| 6018 | NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel, |
| 6019 | UserHandle.USER_ALL); |
| 6020 | reset(mPackageManager); |
| 6021 | |
| 6022 | Uri uri1 = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 1); |
| 6023 | Uri uri2 = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 2); |
| 6024 | |
| 6025 | // create an inline record a uri in it |
| 6026 | mService.mNotificationDelegate.grantInlineReplyUriPermission( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 6027 | nr.getKey(), uri1, nr.getSbn().getUser(), nr.getSbn().getPackageName(), |
| 6028 | nr.getSbn().getUid()); |
Aran Ink | 979c976 | 2019-10-24 16:09:45 -0400 | [diff] [blame] | 6029 | |
| 6030 | InlineReplyUriRecord record = mService.mInlineReplyRecordsByKey.get(nr.getKey()); |
| 6031 | assertNotNull(record); // record exists |
| 6032 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 6033 | mService.mNotificationDelegate.clearInlineReplyUriPermissions( |
| 6034 | nr.getKey(), nr.getSbn().getUid()); |
Aran Ink | 979c976 | 2019-10-24 16:09:45 -0400 | [diff] [blame] | 6035 | |
| 6036 | // permissionOwner destroyed for USER_SYSTEM, not USER_ALL |
| 6037 | verify(mUgmInternal, times(1)).revokeUriPermissionFromOwner( |
| 6038 | eq(record.getPermissionOwner()), eq(null), eq(~0), eq(USER_SYSTEM)); |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 6039 | } |
| 6040 | |
| 6041 | @Test |
Mady Mellor | 5c11a2e | 2019-04-25 17:26:15 -0700 | [diff] [blame] | 6042 | public void testNotificationBubbles_disabled_lowRamDevice() throws Exception { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 6043 | setUpPrefsForBubbles(PKG, mUid, |
| 6044 | true /* global */, |
| 6045 | BUBBLE_PREFERENCE_ALL /* app */, |
| 6046 | true /* channel */); |
Mady Mellor | 5c11a2e | 2019-04-25 17:26:15 -0700 | [diff] [blame] | 6047 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 6048 | // And we are low ram |
| 6049 | when(mActivityManager.isLowRamDevice()).thenReturn(true); |
| 6050 | |
| 6051 | // Notification that would typically bubble |
| 6052 | NotificationRecord nr = generateMessageBubbleNotifRecord(mTestNotificationChannel, |
| 6053 | "testNotificationBubbles_disabled_lowRamDevice"); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 6054 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 6055 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Mady Mellor | 5c11a2e | 2019-04-25 17:26:15 -0700 | [diff] [blame] | 6056 | waitForIdle(); |
| 6057 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 6058 | // But we wouldn't be a bubble because the device is low ram & all bubbles are disabled. |
Mady Mellor | 5c11a2e | 2019-04-25 17:26:15 -0700 | [diff] [blame] | 6059 | StatusBarNotification[] notifsAfter = mBinderService.getActiveNotifications(PKG); |
| 6060 | assertEquals(1, notifsAfter.length); |
| 6061 | assertEquals((notifsAfter[0].getNotification().flags & FLAG_BUBBLE), 0); |
Mady Mellor | 5c11a2e | 2019-04-25 17:26:15 -0700 | [diff] [blame] | 6062 | } |
Julia Reynolds | b681ffe | 2019-06-19 13:40:46 -0400 | [diff] [blame] | 6063 | |
| 6064 | @Test |
| 6065 | public void testRemoveLargeRemoteViews() throws Exception { |
| 6066 | int removeSize = mContext.getResources().getInteger( |
| 6067 | com.android.internal.R.integer.config_notificationStripRemoteViewSizeBytes); |
| 6068 | |
| 6069 | RemoteViews rv = mock(RemoteViews.class); |
| 6070 | when(rv.estimateMemoryUsage()).thenReturn(removeSize); |
| 6071 | when(rv.clone()).thenReturn(rv); |
| 6072 | RemoteViews rv1 = mock(RemoteViews.class); |
| 6073 | when(rv1.estimateMemoryUsage()).thenReturn(removeSize); |
| 6074 | when(rv1.clone()).thenReturn(rv1); |
| 6075 | RemoteViews rv2 = mock(RemoteViews.class); |
| 6076 | when(rv2.estimateMemoryUsage()).thenReturn(removeSize); |
| 6077 | when(rv2.clone()).thenReturn(rv2); |
| 6078 | RemoteViews rv3 = mock(RemoteViews.class); |
| 6079 | when(rv3.estimateMemoryUsage()).thenReturn(removeSize); |
| 6080 | when(rv3.clone()).thenReturn(rv3); |
| 6081 | RemoteViews rv4 = mock(RemoteViews.class); |
| 6082 | when(rv4.estimateMemoryUsage()).thenReturn(removeSize); |
| 6083 | when(rv4.clone()).thenReturn(rv4); |
| 6084 | // note: different! |
| 6085 | RemoteViews rv5 = mock(RemoteViews.class); |
| 6086 | when(rv5.estimateMemoryUsage()).thenReturn(removeSize - 1); |
| 6087 | when(rv5.clone()).thenReturn(rv5); |
| 6088 | |
| 6089 | Notification np = new Notification.Builder(mContext, "test") |
| 6090 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 6091 | .setContentText("test") |
| 6092 | .setCustomContentView(rv) |
| 6093 | .setCustomBigContentView(rv1) |
| 6094 | .setCustomHeadsUpContentView(rv2) |
| 6095 | .build(); |
| 6096 | Notification n = new Notification.Builder(mContext, "test") |
| 6097 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 6098 | .setContentText("test") |
| 6099 | .setCustomContentView(rv3) |
| 6100 | .setCustomBigContentView(rv4) |
| 6101 | .setCustomHeadsUpContentView(rv5) |
| 6102 | .setPublicVersion(np) |
| 6103 | .build(); |
| 6104 | |
| 6105 | assertNotNull(np.contentView); |
| 6106 | assertNotNull(np.bigContentView); |
| 6107 | assertNotNull(np.headsUpContentView); |
| 6108 | |
| 6109 | assertTrue(n.publicVersion.extras.containsKey(Notification.EXTRA_CONTAINS_CUSTOM_VIEW)); |
| 6110 | assertNotNull(n.publicVersion.contentView); |
| 6111 | assertNotNull(n.publicVersion.bigContentView); |
| 6112 | assertNotNull(n.publicVersion.headsUpContentView); |
| 6113 | |
| 6114 | mService.fixNotification(n, PKG, "tag", 9, 0); |
| 6115 | |
| 6116 | assertNull(n.contentView); |
| 6117 | assertNull(n.bigContentView); |
| 6118 | assertNotNull(n.headsUpContentView); |
| 6119 | assertNull(n.publicVersion.contentView); |
| 6120 | assertNull(n.publicVersion.bigContentView); |
| 6121 | assertNull(n.publicVersion.headsUpContentView); |
| 6122 | |
| 6123 | verify(mUsageStats, times(5)).registerImageRemoved(PKG); |
| 6124 | } |
Mady Mellor | 06b770c | 2019-08-29 18:01:00 -0700 | [diff] [blame] | 6125 | |
Colin Cross | 5ea7fb7 | 2019-12-18 17:16:36 -0800 | [diff] [blame] | 6126 | @Test |
Mady Mellor | a773196 | 2019-06-17 17:57:02 -0700 | [diff] [blame] | 6127 | public void testNotificationBubbles_flagAutoExpandForeground_fails_notForeground() |
| 6128 | throws Exception { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 6129 | setUpPrefsForBubbles(PKG, mUid, |
| 6130 | true /* global */, |
| 6131 | BUBBLE_PREFERENCE_ALL /* app */, |
| 6132 | true /* channel */); |
Mady Mellor | a773196 | 2019-06-17 17:57:02 -0700 | [diff] [blame] | 6133 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 6134 | NotificationRecord nr = generateMessageBubbleNotifRecord(mTestNotificationChannel, |
| 6135 | "testNotificationBubbles_flagAutoExpandForeground_fails_notForeground"); |
| 6136 | // Modify metadata flags |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 6137 | nr.getSbn().getNotification().getBubbleMetadata().setFlags( |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 6138 | Notification.BubbleMetadata.FLAG_AUTO_EXPAND_BUBBLE |
| 6139 | | Notification.BubbleMetadata.FLAG_SUPPRESS_NOTIFICATION); |
Mady Mellor | a773196 | 2019-06-17 17:57:02 -0700 | [diff] [blame] | 6140 | |
| 6141 | // Ensure we're not foreground |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 6142 | when(mActivityManager.getPackageImportance(nr.getSbn().getPackageName())).thenReturn( |
Mady Mellor | a773196 | 2019-06-17 17:57:02 -0700 | [diff] [blame] | 6143 | IMPORTANCE_VISIBLE); |
| 6144 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 6145 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 6146 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Mady Mellor | a773196 | 2019-06-17 17:57:02 -0700 | [diff] [blame] | 6147 | waitForIdle(); |
| 6148 | |
| 6149 | // yes allowed, yes messaging, yes bubble |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 6150 | Notification notif = mService.getNotificationRecord(nr.getSbn().getKey()).getNotification(); |
Mady Mellor | a773196 | 2019-06-17 17:57:02 -0700 | [diff] [blame] | 6151 | assertTrue(notif.isBubbleNotification()); |
| 6152 | |
| 6153 | // Our flags should have failed since we're not foreground |
| 6154 | assertFalse(notif.getBubbleMetadata().getAutoExpandBubble()); |
| 6155 | assertFalse(notif.getBubbleMetadata().isNotificationSuppressed()); |
| 6156 | } |
| 6157 | |
| 6158 | @Test |
| 6159 | public void testNotificationBubbles_flagAutoExpandForeground_succeeds_foreground() |
| 6160 | throws RemoteException { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 6161 | setUpPrefsForBubbles(PKG, mUid, |
| 6162 | true /* global */, |
| 6163 | BUBBLE_PREFERENCE_ALL /* app */, |
| 6164 | true /* channel */); |
Mady Mellor | a773196 | 2019-06-17 17:57:02 -0700 | [diff] [blame] | 6165 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 6166 | NotificationRecord nr = generateMessageBubbleNotifRecord(mTestNotificationChannel, |
| 6167 | "testNotificationBubbles_flagAutoExpandForeground_succeeds_foreground"); |
| 6168 | // Modify metadata flags |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 6169 | nr.getSbn().getNotification().getBubbleMetadata().setFlags( |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 6170 | Notification.BubbleMetadata.FLAG_AUTO_EXPAND_BUBBLE |
| 6171 | | Notification.BubbleMetadata.FLAG_SUPPRESS_NOTIFICATION); |
Mady Mellor | a773196 | 2019-06-17 17:57:02 -0700 | [diff] [blame] | 6172 | |
| 6173 | // Ensure we are in the foreground |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 6174 | when(mActivityManager.getPackageImportance(nr.getSbn().getPackageName())).thenReturn( |
Mady Mellor | a773196 | 2019-06-17 17:57:02 -0700 | [diff] [blame] | 6175 | IMPORTANCE_FOREGROUND); |
| 6176 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 6177 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 6178 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Mady Mellor | a773196 | 2019-06-17 17:57:02 -0700 | [diff] [blame] | 6179 | waitForIdle(); |
| 6180 | |
| 6181 | // yes allowed, yes messaging, yes bubble |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 6182 | Notification notif = mService.getNotificationRecord(nr.getSbn().getKey()).getNotification(); |
Mady Mellor | a773196 | 2019-06-17 17:57:02 -0700 | [diff] [blame] | 6183 | assertTrue(notif.isBubbleNotification()); |
| 6184 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 6185 | // Our flags should have passed since we are foreground |
Mady Mellor | a773196 | 2019-06-17 17:57:02 -0700 | [diff] [blame] | 6186 | assertTrue(notif.getBubbleMetadata().getAutoExpandBubble()); |
| 6187 | assertTrue(notif.getBubbleMetadata().isNotificationSuppressed()); |
| 6188 | } |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 6189 | |
| 6190 | @Test |
Mady Mellor | 2ac2d3a | 2020-01-08 17:18:54 -0800 | [diff] [blame] | 6191 | public void testNotificationBubbles_flagRemoved_whenShortcutRemoved() |
| 6192 | throws RemoteException { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 6193 | setUpPrefsForBubbles(PKG, mUid, |
| 6194 | true /* global */, |
| 6195 | BUBBLE_PREFERENCE_ALL /* app */, |
| 6196 | true /* channel */); |
Mady Mellor | 2ac2d3a | 2020-01-08 17:18:54 -0800 | [diff] [blame] | 6197 | |
| 6198 | ArgumentCaptor<LauncherApps.Callback> launcherAppsCallback = |
| 6199 | ArgumentCaptor.forClass(LauncherApps.Callback.class); |
| 6200 | |
| 6201 | // Messaging notification with shortcut info |
| 6202 | Notification.BubbleMetadata metadata = |
Julia Reynolds | c76888d | 2020-05-04 15:36:09 -0400 | [diff] [blame] | 6203 | new Notification.BubbleMetadata.Builder(VALID_CONVO_SHORTCUT_ID).build(); |
Mady Mellor | 2ac2d3a | 2020-01-08 17:18:54 -0800 | [diff] [blame] | 6204 | Notification.Builder nb = getMessageStyleNotifBuilder(false /* addDefaultMetadata */, |
| 6205 | null /* groupKey */, false /* isSummary */); |
Julia Reynolds | c76888d | 2020-05-04 15:36:09 -0400 | [diff] [blame] | 6206 | nb.setShortcutId(VALID_CONVO_SHORTCUT_ID); |
Mady Mellor | 2ac2d3a | 2020-01-08 17:18:54 -0800 | [diff] [blame] | 6207 | nb.setBubbleMetadata(metadata); |
| 6208 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 6209 | "tag", mUid, 0, nb.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Mady Mellor | 2ac2d3a | 2020-01-08 17:18:54 -0800 | [diff] [blame] | 6210 | NotificationRecord nr = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 6211 | |
Mady Mellor | 2ac2d3a | 2020-01-08 17:18:54 -0800 | [diff] [blame] | 6212 | // Test: Send the bubble notification |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 6213 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 6214 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Mady Mellor | 2ac2d3a | 2020-01-08 17:18:54 -0800 | [diff] [blame] | 6215 | waitForIdle(); |
| 6216 | |
| 6217 | // Verify: |
| 6218 | |
| 6219 | // Make sure we register the callback for shortcut changes |
| 6220 | verify(mLauncherApps, times(1)).registerCallback(launcherAppsCallback.capture(), any()); |
| 6221 | |
| 6222 | // yes allowed, yes messaging w/shortcut, yes bubble |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 6223 | Notification notif = mService.getNotificationRecord(nr.getSbn().getKey()).getNotification(); |
Mady Mellor | 2ac2d3a | 2020-01-08 17:18:54 -0800 | [diff] [blame] | 6224 | assertTrue(notif.isBubbleNotification()); |
| 6225 | |
Danning Chen | 28410f3 | 2020-04-21 10:28:52 -0700 | [diff] [blame] | 6226 | // Make sure the shortcut is cached. |
| 6227 | verify(mShortcutServiceInternal).cacheShortcuts( |
Julia Reynolds | c76888d | 2020-05-04 15:36:09 -0400 | [diff] [blame] | 6228 | anyInt(), any(), eq(PKG), eq(Collections.singletonList(VALID_CONVO_SHORTCUT_ID)), |
Mehdi Alizadeh | a3d22ce | 2020-04-27 18:52:52 -0700 | [diff] [blame] | 6229 | eq(USER_SYSTEM), eq(ShortcutInfo.FLAG_CACHED_NOTIFICATIONS)); |
Danning Chen | 28410f3 | 2020-04-21 10:28:52 -0700 | [diff] [blame] | 6230 | |
Mady Mellor | 2ac2d3a | 2020-01-08 17:18:54 -0800 | [diff] [blame] | 6231 | // Test: Remove the shortcut |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 6232 | when(mLauncherApps.getShortcuts(any(), any())).thenReturn(null); |
Mady Mellor | 2ac2d3a | 2020-01-08 17:18:54 -0800 | [diff] [blame] | 6233 | launcherAppsCallback.getValue().onShortcutsChanged(PKG, Collections.emptyList(), |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 6234 | UserHandle.getUserHandleForUid(mUid)); |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 6235 | waitForIdle(); |
Mady Mellor | 2ac2d3a | 2020-01-08 17:18:54 -0800 | [diff] [blame] | 6236 | |
| 6237 | // Verify: |
| 6238 | |
| 6239 | // Make sure callback is unregistered |
| 6240 | verify(mLauncherApps, times(1)).unregisterCallback(launcherAppsCallback.getValue()); |
| 6241 | |
| 6242 | // We're no longer a bubble |
Mady Mellor | db0a602 | 2020-05-13 11:34:50 -0700 | [diff] [blame] | 6243 | NotificationRecord notif2 = mService.getNotificationRecord( |
| 6244 | nr.getSbn().getKey()); |
| 6245 | assertNull(notif2.getShortcutInfo()); |
| 6246 | assertFalse(notif2.getNotification().isBubbleNotification()); |
Mady Mellor | 2ac2d3a | 2020-01-08 17:18:54 -0800 | [diff] [blame] | 6247 | } |
| 6248 | |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 6249 | @Test |
| 6250 | public void testNotificationBubbles_shortcut_stopListeningWhenNotifRemoved() |
| 6251 | throws RemoteException { |
Danning Chen | 28410f3 | 2020-04-21 10:28:52 -0700 | [diff] [blame] | 6252 | final String shortcutId = "someshortcutId"; |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 6253 | setUpPrefsForBubbles(PKG, mUid, |
| 6254 | true /* global */, |
| 6255 | BUBBLE_PREFERENCE_ALL /* app */, |
| 6256 | true /* channel */); |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 6257 | |
| 6258 | ArgumentCaptor<LauncherApps.Callback> launcherAppsCallback = |
| 6259 | ArgumentCaptor.forClass(LauncherApps.Callback.class); |
| 6260 | |
| 6261 | // Messaging notification with shortcut info |
Mady Mellor | aa9ce17 | 2020-03-17 10:34:20 -0700 | [diff] [blame] | 6262 | Notification.BubbleMetadata metadata = new Notification.BubbleMetadata.Builder( |
Danning Chen | 28410f3 | 2020-04-21 10:28:52 -0700 | [diff] [blame] | 6263 | shortcutId).build(); |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 6264 | Notification.Builder nb = getMessageStyleNotifBuilder(false /* addDefaultMetadata */, |
| 6265 | null /* groupKey */, false /* isSummary */); |
Danning Chen | 28410f3 | 2020-04-21 10:28:52 -0700 | [diff] [blame] | 6266 | nb.setShortcutId(shortcutId); |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 6267 | nb.setBubbleMetadata(metadata); |
| 6268 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 6269 | "tag", mUid, 0, nb.build(), UserHandle.getUserHandleForUid(mUid), null, 0); |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 6270 | NotificationRecord nr = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 6271 | |
| 6272 | // Pretend the shortcut exists |
| 6273 | List<ShortcutInfo> shortcutInfos = new ArrayList<>(); |
| 6274 | ShortcutInfo info = mock(ShortcutInfo.class); |
Danning Chen | 07cfaa6 | 2020-04-20 15:16:41 -0700 | [diff] [blame] | 6275 | when(info.getPackage()).thenReturn(PKG); |
Danning Chen | 28410f3 | 2020-04-21 10:28:52 -0700 | [diff] [blame] | 6276 | when(info.getId()).thenReturn(shortcutId); |
Danning Chen | 07cfaa6 | 2020-04-20 15:16:41 -0700 | [diff] [blame] | 6277 | when(info.getUserId()).thenReturn(USER_SYSTEM); |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 6278 | when(info.isLongLived()).thenReturn(true); |
Julia Reynolds | fa27307 | 2020-04-14 15:31:21 -0400 | [diff] [blame] | 6279 | when(info.isEnabled()).thenReturn(true); |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 6280 | shortcutInfos.add(info); |
| 6281 | when(mLauncherApps.getShortcuts(any(), any())).thenReturn(shortcutInfos); |
Julia Reynolds | fa27307 | 2020-04-14 15:31:21 -0400 | [diff] [blame] | 6282 | when(mShortcutServiceInternal.isSharingShortcut(anyInt(), anyString(), anyString(), |
| 6283 | anyString(), anyInt(), any())).thenReturn(true); |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 6284 | |
| 6285 | // Test: Send the bubble notification |
| 6286 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 6287 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
| 6288 | waitForIdle(); |
| 6289 | |
| 6290 | // Verify: |
| 6291 | |
| 6292 | // Make sure we register the callback for shortcut changes |
| 6293 | verify(mLauncherApps, times(1)).registerCallback(launcherAppsCallback.capture(), any()); |
| 6294 | |
| 6295 | // yes allowed, yes messaging w/shortcut, yes bubble |
| 6296 | Notification notif = mService.getNotificationRecord(nr.getSbn().getKey()).getNotification(); |
| 6297 | assertTrue(notif.isBubbleNotification()); |
| 6298 | |
Danning Chen | 28410f3 | 2020-04-21 10:28:52 -0700 | [diff] [blame] | 6299 | // Make sure the shortcut is cached. |
| 6300 | verify(mShortcutServiceInternal).cacheShortcuts( |
| 6301 | anyInt(), any(), eq(PKG), eq(Collections.singletonList(shortcutId)), |
Mehdi Alizadeh | a3d22ce | 2020-04-27 18:52:52 -0700 | [diff] [blame] | 6302 | eq(USER_SYSTEM), eq(ShortcutInfo.FLAG_CACHED_NOTIFICATIONS)); |
Danning Chen | 28410f3 | 2020-04-21 10:28:52 -0700 | [diff] [blame] | 6303 | |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 6304 | // Test: Remove the notification |
| 6305 | mBinderService.cancelNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 6306 | nr.getSbn().getId(), nr.getSbn().getUserId()); |
| 6307 | waitForIdle(); |
| 6308 | |
| 6309 | // Verify: |
| 6310 | |
| 6311 | // Make sure callback is unregistered |
| 6312 | verify(mLauncherApps, times(1)).unregisterCallback(launcherAppsCallback.getValue()); |
| 6313 | } |
| 6314 | |
Mady Mellor | 2ac2d3a | 2020-01-08 17:18:54 -0800 | [diff] [blame] | 6315 | @Test |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 6316 | public void testNotificationBubbles_bubbleChildrenStay_whenGroupSummaryDismissed() |
| 6317 | throws Exception { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 6318 | setUpPrefsForBubbles(PKG, mUid, |
| 6319 | true /* global */, |
| 6320 | BUBBLE_PREFERENCE_ALL /* app */, |
| 6321 | true /* channel */); |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 6322 | |
| 6323 | NotificationRecord nrSummary = addGroupWithBubblesAndValidateAdded( |
| 6324 | true /* summaryAutoCancel */); |
| 6325 | |
| 6326 | // Dismiss summary |
| 6327 | final NotificationVisibility nv = NotificationVisibility.obtain(nrSummary.getKey(), 1, 2, |
| 6328 | true); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 6329 | mService.mNotificationDelegate.onNotificationClear(mUid, 0, PKG, |
| 6330 | nrSummary.getSbn().getTag(), |
| 6331 | nrSummary.getSbn().getId(), nrSummary.getUserId(), nrSummary.getKey(), |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 6332 | NotificationStats.DISMISSAL_SHADE, |
| 6333 | NotificationStats.DISMISS_SENTIMENT_NEUTRAL, nv); |
| 6334 | waitForIdle(); |
| 6335 | |
| 6336 | // The bubble should still exist |
| 6337 | StatusBarNotification[] notifsAfter = mBinderService.getActiveNotifications(PKG); |
| 6338 | assertEquals(1, notifsAfter.length); |
| 6339 | } |
| 6340 | |
| 6341 | @Test |
| 6342 | public void testNotificationBubbles_bubbleChildrenStay_whenGroupSummaryClicked() |
| 6343 | throws Exception { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 6344 | setUpPrefsForBubbles(PKG, mUid, |
| 6345 | true /* global */, |
| 6346 | BUBBLE_PREFERENCE_ALL /* app */, |
| 6347 | true /* channel */); |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 6348 | |
| 6349 | NotificationRecord nrSummary = addGroupWithBubblesAndValidateAdded( |
| 6350 | true /* summaryAutoCancel */); |
| 6351 | |
| 6352 | // Click summary |
| 6353 | final NotificationVisibility nv = NotificationVisibility.obtain(nrSummary.getKey(), 1, 2, |
| 6354 | true); |
| 6355 | mService.mNotificationDelegate.onNotificationClick(mUid, Binder.getCallingPid(), |
| 6356 | nrSummary.getKey(), nv); |
| 6357 | waitForIdle(); |
| 6358 | |
| 6359 | // The bubble should still exist |
| 6360 | StatusBarNotification[] notifsAfter = mBinderService.getActiveNotifications(PKG); |
| 6361 | assertEquals(1, notifsAfter.length); |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 6362 | |
| 6363 | // Check we got the click log and associated dismissal logs |
| 6364 | assertEquals(6, mNotificationRecordLogger.numCalls()); |
| 6365 | // Skip the notification-creation logs |
| 6366 | assertEquals(NotificationRecordLogger.NotificationEvent.NOTIFICATION_CLICKED, |
| 6367 | mNotificationRecordLogger.event(3)); |
| 6368 | assertEquals(NotificationRecordLogger.NotificationCancelledEvent.NOTIFICATION_CANCEL_CLICK, |
| 6369 | mNotificationRecordLogger.event(4)); |
| 6370 | assertEquals(NotificationRecordLogger.NotificationCancelledEvent |
| 6371 | .NOTIFICATION_CANCEL_GROUP_SUMMARY_CANCELED, |
| 6372 | mNotificationRecordLogger.event(5)); |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 6373 | } |
Julia Reynolds | 57a974b | 2019-10-07 11:51:47 -0400 | [diff] [blame] | 6374 | |
| 6375 | @Test |
Beverly | 9e6a764 | 2020-02-13 10:11:29 -0500 | [diff] [blame] | 6376 | public void testNotificationBubbles_bubbleStays_whenClicked() |
| 6377 | throws Exception { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 6378 | setUpPrefsForBubbles(PKG, mUid, |
| 6379 | true /* global */, |
| 6380 | BUBBLE_PREFERENCE_ALL /* app */, |
| 6381 | true /* channel */); |
| 6382 | |
Beverly | 9e6a764 | 2020-02-13 10:11:29 -0500 | [diff] [blame] | 6383 | // GIVEN a notification that has the auto cancels flag (cancel on click) and is a bubble |
Beverly | 9e6a764 | 2020-02-13 10:11:29 -0500 | [diff] [blame] | 6384 | final NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel); |
| 6385 | nr.getSbn().getNotification().flags |= FLAG_BUBBLE | FLAG_AUTO_CANCEL; |
| 6386 | mService.addNotification(nr); |
| 6387 | |
| 6388 | // WHEN we click the notification |
| 6389 | final NotificationVisibility nv = NotificationVisibility.obtain(nr.getKey(), 1, 2, true); |
| 6390 | mService.mNotificationDelegate.onNotificationClick(mUid, Binder.getCallingPid(), |
| 6391 | nr.getKey(), nv); |
| 6392 | waitForIdle(); |
| 6393 | |
| 6394 | // THEN the bubble should still exist |
| 6395 | StatusBarNotification[] notifsAfter = mBinderService.getActiveNotifications(PKG); |
| 6396 | assertEquals(1, notifsAfter.length); |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 6397 | |
| 6398 | // Check we got the click log |
| 6399 | assertEquals(1, mNotificationRecordLogger.numCalls()); |
| 6400 | assertEquals(NotificationRecordLogger.NotificationEvent.NOTIFICATION_CLICKED, |
| 6401 | mNotificationRecordLogger.event(0)); |
Beverly | 9e6a764 | 2020-02-13 10:11:29 -0500 | [diff] [blame] | 6402 | } |
| 6403 | |
| 6404 | @Test |
Julia Reynolds | 57a974b | 2019-10-07 11:51:47 -0400 | [diff] [blame] | 6405 | public void testLoadDefaultApprovedServices_emptyResources() { |
| 6406 | TestableResources tr = mContext.getOrCreateTestableResources(); |
| 6407 | tr.addOverride(com.android.internal.R.string.config_defaultListenerAccessPackages, ""); |
| 6408 | tr.addOverride(com.android.internal.R.string.config_defaultDndAccessPackages, ""); |
| 6409 | tr.addOverride(com.android.internal.R.string.config_defaultAssistantAccessComponent, ""); |
| 6410 | setDefaultAssistantInDeviceConfig(""); |
| 6411 | |
| 6412 | mService.loadDefaultApprovedServices(USER_SYSTEM); |
| 6413 | |
| 6414 | verify(mListeners, never()).addDefaultComponentOrPackage(anyString()); |
| 6415 | verify(mConditionProviders, never()).addDefaultComponentOrPackage(anyString()); |
| 6416 | verify(mAssistants, never()).addDefaultComponentOrPackage(anyString()); |
| 6417 | } |
| 6418 | |
| 6419 | @Test |
| 6420 | public void testLoadDefaultApprovedServices_dnd() { |
| 6421 | TestableResources tr = mContext.getOrCreateTestableResources(); |
| 6422 | tr.addOverride(com.android.internal.R.string.config_defaultDndAccessPackages, "test"); |
| 6423 | when(mListeners.queryPackageForServices(anyString(), anyInt(), anyInt())) |
| 6424 | .thenReturn(new ArraySet<>()); |
| 6425 | |
| 6426 | mService.loadDefaultApprovedServices(USER_SYSTEM); |
| 6427 | |
Jay Aliomer | 76e1f272 | 2020-03-05 12:36:38 -0500 | [diff] [blame] | 6428 | verify(mConditionProviders, times(1)).loadDefaultsFromConfig(); |
Julia Reynolds | 57a974b | 2019-10-07 11:51:47 -0400 | [diff] [blame] | 6429 | } |
| 6430 | |
| 6431 | // TODO: add tests for the rest of the non-empty cases |
Julia Reynolds | b317ff7 | 2019-11-26 14:20:51 -0500 | [diff] [blame] | 6432 | |
| 6433 | @Test |
| 6434 | public void testOnUnlockUser() { |
| 6435 | UserInfo ui = new UserInfo(); |
| 6436 | ui.id = 10; |
| 6437 | mService.onUnlockUser(ui); |
| 6438 | waitForIdle(); |
| 6439 | |
| 6440 | verify(mHistoryManager, timeout(MAX_POST_DELAY).times(1)).onUserUnlocked(ui.id); |
| 6441 | } |
| 6442 | |
| 6443 | @Test |
| 6444 | public void testOnStopUser() { |
| 6445 | UserInfo ui = new UserInfo(); |
| 6446 | ui.id = 10; |
| 6447 | mService.onStopUser(ui); |
| 6448 | waitForIdle(); |
| 6449 | |
| 6450 | verify(mHistoryManager, timeout(MAX_POST_DELAY).times(1)).onUserStopped(ui.id); |
| 6451 | } |
| 6452 | |
| 6453 | @Test |
| 6454 | public void testOnBootPhase() { |
| 6455 | mService.onBootPhase(SystemService.PHASE_ACTIVITY_MANAGER_READY); |
| 6456 | |
| 6457 | verify(mHistoryManager, never()).onBootPhaseAppsCanStart(); |
| 6458 | |
| 6459 | mService.onBootPhase(SystemService.PHASE_THIRD_PARTY_APPS_CAN_START); |
| 6460 | |
| 6461 | verify(mHistoryManager, times(1)).onBootPhaseAppsCanStart(); |
| 6462 | } |
| 6463 | |
| 6464 | @Test |
| 6465 | public void testHandleOnPackageChanged() { |
| 6466 | String[] pkgs = new String[] {PKG, PKG_N_MR1}; |
| 6467 | int[] uids = new int[] {mUid, UserHandle.PER_USER_RANGE + 1}; |
| 6468 | |
| 6469 | mService.handleOnPackageChanged(false, USER_SYSTEM, pkgs, uids); |
| 6470 | |
| 6471 | verify(mHistoryManager, never()).onPackageRemoved(anyInt(), anyString()); |
| 6472 | |
| 6473 | mService.handleOnPackageChanged(true, USER_SYSTEM, pkgs, uids); |
| 6474 | |
| 6475 | verify(mHistoryManager, times(1)).onPackageRemoved(UserHandle.getUserId(uids[0]), pkgs[0]); |
| 6476 | verify(mHistoryManager, times(1)).onPackageRemoved(UserHandle.getUserId(uids[1]), pkgs[1]); |
| 6477 | } |
| 6478 | |
| 6479 | @Test |
| 6480 | public void testNotificationHistory_addNoisyNotification() throws Exception { |
| 6481 | NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel, |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 6482 | null /* tvExtender */); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 6483 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 6484 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Julia Reynolds | b317ff7 | 2019-11-26 14:20:51 -0500 | [diff] [blame] | 6485 | waitForIdle(); |
| 6486 | |
| 6487 | verify(mHistoryManager, times(1)).addNotification(any()); |
| 6488 | } |
Julia Reynolds | 0f76734 | 2019-12-18 09:11:55 -0500 | [diff] [blame] | 6489 | |
| 6490 | @Test |
| 6491 | public void createConversationNotificationChannel() throws Exception { |
| 6492 | NotificationChannel original = new NotificationChannel("a", "a", IMPORTANCE_HIGH); |
| 6493 | original.setAllowBubbles(!original.canBubble()); |
| 6494 | original.setShowBadge(!original.canShowBadge()); |
| 6495 | |
| 6496 | Parcel parcel = Parcel.obtain(); |
| 6497 | original.writeToParcel(parcel, 0); |
| 6498 | parcel.setDataPosition(0); |
| 6499 | NotificationChannel orig = NotificationChannel.CREATOR.createFromParcel(parcel); |
| 6500 | assertEquals(original, orig); |
| 6501 | assertFalse(TextUtils.isEmpty(orig.getName())); |
| 6502 | |
| 6503 | mBinderService.createNotificationChannels(PKG, new ParceledListSlice(Arrays.asList( |
| 6504 | orig))); |
| 6505 | |
Julia Reynolds | 7c26752 | 2020-01-16 11:26:41 -0500 | [diff] [blame] | 6506 | mBinderService.createConversationNotificationChannelForPackage( |
| 6507 | PKG, mUid, "key", orig, "friend"); |
Julia Reynolds | 0f76734 | 2019-12-18 09:11:55 -0500 | [diff] [blame] | 6508 | |
| 6509 | NotificationChannel friendChannel = mBinderService.getConversationNotificationChannel( |
Julia Reynolds | 12ba4cf | 2020-01-10 16:01:38 -0500 | [diff] [blame] | 6510 | PKG, 0, PKG, original.getId(), false, "friend"); |
Julia Reynolds | 0f76734 | 2019-12-18 09:11:55 -0500 | [diff] [blame] | 6511 | |
| 6512 | assertEquals(original.getName(), friendChannel.getName()); |
| 6513 | assertEquals(original.getId(), friendChannel.getParentChannelId()); |
| 6514 | assertEquals("friend", friendChannel.getConversationId()); |
| 6515 | assertEquals(null, original.getConversationId()); |
| 6516 | assertEquals(original.canShowBadge(), friendChannel.canShowBadge()); |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 6517 | assertFalse(friendChannel.canBubble()); // can't be modified by app |
Julia Reynolds | 0f76734 | 2019-12-18 09:11:55 -0500 | [diff] [blame] | 6518 | assertFalse(original.getId().equals(friendChannel.getId())); |
| 6519 | assertNotNull(friendChannel.getId()); |
| 6520 | } |
| 6521 | |
| 6522 | @Test |
| 6523 | public void deleteConversationNotificationChannels() throws Exception { |
| 6524 | NotificationChannel messagesParent = |
| 6525 | new NotificationChannel("messages", "messages", IMPORTANCE_HIGH); |
| 6526 | Parcel msgParcel = Parcel.obtain(); |
| 6527 | messagesParent.writeToParcel(msgParcel, 0); |
| 6528 | msgParcel.setDataPosition(0); |
| 6529 | |
| 6530 | NotificationChannel callsParent = |
| 6531 | new NotificationChannel("calls", "calls", IMPORTANCE_HIGH); |
| 6532 | Parcel callParcel = Parcel.obtain(); |
| 6533 | callsParent.writeToParcel(callParcel, 0); |
| 6534 | callParcel.setDataPosition(0); |
| 6535 | |
| 6536 | mBinderService.createNotificationChannels(PKG, new ParceledListSlice(Arrays.asList( |
| 6537 | messagesParent, callsParent))); |
| 6538 | |
| 6539 | String conversationId = "friend"; |
| 6540 | |
| 6541 | mBinderService.createConversationNotificationChannelForPackage( |
Julia Reynolds | 7c26752 | 2020-01-16 11:26:41 -0500 | [diff] [blame] | 6542 | PKG, mUid, "key", NotificationChannel.CREATOR.createFromParcel(msgParcel), |
| 6543 | conversationId); |
Julia Reynolds | 0f76734 | 2019-12-18 09:11:55 -0500 | [diff] [blame] | 6544 | mBinderService.createConversationNotificationChannelForPackage( |
Julia Reynolds | 7c26752 | 2020-01-16 11:26:41 -0500 | [diff] [blame] | 6545 | PKG, mUid, "key", NotificationChannel.CREATOR.createFromParcel(callParcel), |
Julia Reynolds | 0f76734 | 2019-12-18 09:11:55 -0500 | [diff] [blame] | 6546 | conversationId); |
| 6547 | |
| 6548 | NotificationChannel messagesChild = mBinderService.getConversationNotificationChannel( |
Julia Reynolds | 12ba4cf | 2020-01-10 16:01:38 -0500 | [diff] [blame] | 6549 | PKG, 0, PKG, messagesParent.getId(), false, conversationId); |
Julia Reynolds | 0f76734 | 2019-12-18 09:11:55 -0500 | [diff] [blame] | 6550 | NotificationChannel callsChild = mBinderService.getConversationNotificationChannel( |
Julia Reynolds | 12ba4cf | 2020-01-10 16:01:38 -0500 | [diff] [blame] | 6551 | PKG, 0, PKG, callsParent.getId(), false, conversationId); |
Julia Reynolds | 0f76734 | 2019-12-18 09:11:55 -0500 | [diff] [blame] | 6552 | |
| 6553 | assertEquals(messagesParent.getId(), messagesChild.getParentChannelId()); |
| 6554 | assertEquals(conversationId, messagesChild.getConversationId()); |
| 6555 | |
| 6556 | assertEquals(callsParent.getId(), callsChild.getParentChannelId()); |
| 6557 | assertEquals(conversationId, callsChild.getConversationId()); |
| 6558 | |
| 6559 | mBinderService.deleteConversationNotificationChannels(PKG, mUid, conversationId); |
| 6560 | |
| 6561 | assertNull(mBinderService.getConversationNotificationChannel( |
Julia Reynolds | 12ba4cf | 2020-01-10 16:01:38 -0500 | [diff] [blame] | 6562 | PKG, 0, PKG, messagesParent.getId(), false, conversationId)); |
Julia Reynolds | 0f76734 | 2019-12-18 09:11:55 -0500 | [diff] [blame] | 6563 | assertNull(mBinderService.getConversationNotificationChannel( |
Julia Reynolds | 12ba4cf | 2020-01-10 16:01:38 -0500 | [diff] [blame] | 6564 | PKG, 0, PKG, callsParent.getId(), false, conversationId)); |
Julia Reynolds | 0f76734 | 2019-12-18 09:11:55 -0500 | [diff] [blame] | 6565 | } |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 6566 | |
| 6567 | @Test |
| 6568 | public void testCorrectCategory_systemOn_appCannotTurnOff() { |
| 6569 | int requested = 0; |
| 6570 | int system = PRIORITY_CATEGORY_CALLS | PRIORITY_CATEGORY_CONVERSATIONS; |
| 6571 | |
| 6572 | int actual = mService.correctCategory(requested, PRIORITY_CATEGORY_CONVERSATIONS, |
| 6573 | system); |
| 6574 | |
| 6575 | assertEquals(PRIORITY_CATEGORY_CONVERSATIONS, actual); |
| 6576 | } |
| 6577 | |
| 6578 | @Test |
| 6579 | public void testCorrectCategory_systemOff_appTurnOff_noChanges() { |
| 6580 | int requested = PRIORITY_CATEGORY_CALLS; |
| 6581 | int system = PRIORITY_CATEGORY_CALLS; |
| 6582 | |
| 6583 | int actual = mService.correctCategory(requested, PRIORITY_CATEGORY_CONVERSATIONS, |
| 6584 | system); |
| 6585 | |
| 6586 | assertEquals(PRIORITY_CATEGORY_CALLS, actual); |
| 6587 | } |
| 6588 | |
| 6589 | @Test |
| 6590 | public void testCorrectCategory_systemOn_appTurnOn_noChanges() { |
| 6591 | int requested = PRIORITY_CATEGORY_CALLS | PRIORITY_CATEGORY_CONVERSATIONS; |
| 6592 | int system = PRIORITY_CATEGORY_CALLS | PRIORITY_CATEGORY_CONVERSATIONS; |
| 6593 | |
| 6594 | int actual = mService.correctCategory(requested, PRIORITY_CATEGORY_CONVERSATIONS, |
| 6595 | system); |
| 6596 | |
| 6597 | assertEquals(PRIORITY_CATEGORY_CALLS | PRIORITY_CATEGORY_CONVERSATIONS, actual); |
| 6598 | } |
| 6599 | |
| 6600 | @Test |
| 6601 | public void testCorrectCategory_systemOff_appCannotTurnOn() { |
| 6602 | int requested = PRIORITY_CATEGORY_CALLS | PRIORITY_CATEGORY_CONVERSATIONS; |
| 6603 | int system = PRIORITY_CATEGORY_CALLS; |
| 6604 | |
| 6605 | int actual = mService.correctCategory(requested, PRIORITY_CATEGORY_CONVERSATIONS, |
| 6606 | system); |
| 6607 | |
| 6608 | assertEquals(PRIORITY_CATEGORY_CALLS, actual); |
| 6609 | } |
Julia Reynolds | 882f206 | 2020-02-05 12:11:38 -0500 | [diff] [blame] | 6610 | |
| 6611 | @Test |
| 6612 | public void testGetConversationsForPackage_hasShortcut() throws Exception { |
| 6613 | mService.setPreferencesHelper(mPreferencesHelper); |
| 6614 | ArrayList<ConversationChannelWrapper> convos = new ArrayList<>(); |
| 6615 | ConversationChannelWrapper convo1 = new ConversationChannelWrapper(); |
| 6616 | NotificationChannel channel1 = new NotificationChannel("a", "a", 1); |
| 6617 | channel1.setConversationId("parent1", "convo 1"); |
| 6618 | convo1.setNotificationChannel(channel1); |
| 6619 | convos.add(convo1); |
| 6620 | |
| 6621 | ConversationChannelWrapper convo2 = new ConversationChannelWrapper(); |
| 6622 | NotificationChannel channel2 = new NotificationChannel("b", "b", 1); |
| 6623 | channel2.setConversationId("parent1", "convo 2"); |
| 6624 | convo2.setNotificationChannel(channel2); |
| 6625 | convos.add(convo2); |
| 6626 | when(mPreferencesHelper.getConversations(anyString(), anyInt())).thenReturn(convos); |
| 6627 | |
Julia Reynolds | 882f206 | 2020-02-05 12:11:38 -0500 | [diff] [blame] | 6628 | ShortcutInfo si = mock(ShortcutInfo.class); |
Danning Chen | 07cfaa6 | 2020-04-20 15:16:41 -0700 | [diff] [blame] | 6629 | when(si.getPackage()).thenReturn(PKG_P); |
| 6630 | when(si.getId()).thenReturn("convo"); |
| 6631 | when(si.getUserId()).thenReturn(USER_SYSTEM); |
Julia Reynolds | 8d34af8 | 2020-05-20 14:24:36 -0400 | [diff] [blame] | 6632 | when(si.getLabel()).thenReturn("Hello"); |
Mady Mellor | 774d5fe | 2020-03-05 11:35:03 -0800 | [diff] [blame] | 6633 | when(si.isLongLived()).thenReturn(true); |
Julia Reynolds | fa27307 | 2020-04-14 15:31:21 -0400 | [diff] [blame] | 6634 | when(si.isEnabled()).thenReturn(true); |
Julia Reynolds | 882f206 | 2020-02-05 12:11:38 -0500 | [diff] [blame] | 6635 | when(mLauncherApps.getShortcuts(any(), any())).thenReturn(Arrays.asList(si)); |
Julia Reynolds | fa27307 | 2020-04-14 15:31:21 -0400 | [diff] [blame] | 6636 | when(mShortcutServiceInternal.isSharingShortcut(anyInt(), anyString(), anyString(), |
| 6637 | anyString(), anyInt(), any())).thenReturn(true); |
Julia Reynolds | 882f206 | 2020-02-05 12:11:38 -0500 | [diff] [blame] | 6638 | |
| 6639 | List<ConversationChannelWrapper> conversations = |
| 6640 | mBinderService.getConversationsForPackage(PKG_P, mUid).getList(); |
| 6641 | assertEquals(si, conversations.get(0).getShortcutInfo()); |
| 6642 | assertEquals(si, conversations.get(1).getShortcutInfo()); |
Mady Mellor | 774d5fe | 2020-03-05 11:35:03 -0800 | [diff] [blame] | 6643 | } |
Julia Reynolds | 882f206 | 2020-02-05 12:11:38 -0500 | [diff] [blame] | 6644 | |
Mady Mellor | 774d5fe | 2020-03-05 11:35:03 -0800 | [diff] [blame] | 6645 | @Test |
| 6646 | public void testGetConversationsForPackage_shortcut_notLongLived() throws Exception { |
| 6647 | mService.setPreferencesHelper(mPreferencesHelper); |
| 6648 | ArrayList<ConversationChannelWrapper> convos = new ArrayList<>(); |
| 6649 | ConversationChannelWrapper convo1 = new ConversationChannelWrapper(); |
| 6650 | NotificationChannel channel1 = new NotificationChannel("a", "a", 1); |
| 6651 | channel1.setConversationId("parent1", "convo 1"); |
| 6652 | convo1.setNotificationChannel(channel1); |
| 6653 | convos.add(convo1); |
| 6654 | |
| 6655 | ConversationChannelWrapper convo2 = new ConversationChannelWrapper(); |
| 6656 | NotificationChannel channel2 = new NotificationChannel("b", "b", 1); |
| 6657 | channel2.setConversationId("parent1", "convo 2"); |
| 6658 | convo2.setNotificationChannel(channel2); |
| 6659 | convos.add(convo2); |
| 6660 | when(mPreferencesHelper.getConversations(anyString(), anyInt())).thenReturn(convos); |
| 6661 | |
| 6662 | ShortcutInfo si = mock(ShortcutInfo.class); |
Danning Chen | 07cfaa6 | 2020-04-20 15:16:41 -0700 | [diff] [blame] | 6663 | when(si.getPackage()).thenReturn(PKG_P); |
| 6664 | when(si.getId()).thenReturn("convo"); |
| 6665 | when(si.getUserId()).thenReturn(USER_SYSTEM); |
Julia Reynolds | 8d34af8 | 2020-05-20 14:24:36 -0400 | [diff] [blame] | 6666 | when(si.getLabel()).thenReturn("Hello"); |
Mady Mellor | 774d5fe | 2020-03-05 11:35:03 -0800 | [diff] [blame] | 6667 | when(si.isLongLived()).thenReturn(false); |
| 6668 | when(mLauncherApps.getShortcuts(any(), any())).thenReturn(Arrays.asList(si)); |
| 6669 | |
| 6670 | List<ConversationChannelWrapper> conversations = |
| 6671 | mBinderService.getConversationsForPackage(PKG_P, mUid).getList(); |
| 6672 | assertNull(conversations.get(0).getShortcutInfo()); |
| 6673 | assertNull(conversations.get(1).getShortcutInfo()); |
Julia Reynolds | 882f206 | 2020-02-05 12:11:38 -0500 | [diff] [blame] | 6674 | } |
| 6675 | |
| 6676 | @Test |
| 6677 | public void testGetConversationsForPackage_doesNotHaveShortcut() throws Exception { |
| 6678 | mService.setPreferencesHelper(mPreferencesHelper); |
| 6679 | ArrayList<ConversationChannelWrapper> convos = new ArrayList<>(); |
| 6680 | ConversationChannelWrapper convo1 = new ConversationChannelWrapper(); |
| 6681 | NotificationChannel channel1 = new NotificationChannel("a", "a", 1); |
| 6682 | channel1.setConversationId("parent1", "convo 1"); |
| 6683 | convo1.setNotificationChannel(channel1); |
| 6684 | convos.add(convo1); |
| 6685 | |
| 6686 | ConversationChannelWrapper convo2 = new ConversationChannelWrapper(); |
| 6687 | NotificationChannel channel2 = new NotificationChannel("b", "b", 1); |
| 6688 | channel2.setConversationId("parent1", "convo 2"); |
| 6689 | convo2.setNotificationChannel(channel2); |
| 6690 | convos.add(convo2); |
| 6691 | when(mPreferencesHelper.getConversations(anyString(), anyInt())).thenReturn(convos); |
Julia Reynolds | c76888d | 2020-05-04 15:36:09 -0400 | [diff] [blame] | 6692 | when(mLauncherApps.getShortcuts(any(), any())).thenReturn(null); |
Julia Reynolds | 882f206 | 2020-02-05 12:11:38 -0500 | [diff] [blame] | 6693 | |
| 6694 | List<ConversationChannelWrapper> conversations = |
| 6695 | mBinderService.getConversationsForPackage(PKG_P, mUid).getList(); |
| 6696 | assertNull(conversations.get(0).getShortcutInfo()); |
| 6697 | assertNull(conversations.get(1).getShortcutInfo()); |
| 6698 | } |
Mady Mellor | 57b3416 | 2020-04-01 08:44:00 -0700 | [diff] [blame] | 6699 | |
| 6700 | @Test |
| 6701 | public void testShortcutHelperNull_doesntCrashEnqueue() throws RemoteException { |
| 6702 | mService.setShortcutHelper(null); |
| 6703 | NotificationRecord nr = |
| 6704 | generateMessageBubbleNotifRecord(mTestNotificationChannel, |
| 6705 | "testShortcutHelperNull_doesntCrashEnqueue"); |
| 6706 | try { |
| 6707 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 6708 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
| 6709 | waitForIdle(); |
| 6710 | } catch (Exception e) { |
| 6711 | fail(e.getMessage()); |
| 6712 | } |
| 6713 | } |
Julia Reynolds | a7dac43 | 2020-04-23 12:17:31 -0400 | [diff] [blame] | 6714 | |
| 6715 | @Test |
Julia Reynolds | 8582df5 | 2020-04-24 18:30:59 -0400 | [diff] [blame] | 6716 | public void testRecordMessages_invalidMsg() throws RemoteException { |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 6717 | Notification.Builder nb = getMessageStyleNotifBuilder(false /* addDefaultMetadata */, |
| 6718 | null /* groupKey */, false /* isSummary */); |
| 6719 | nb.setShortcutId(null); |
| 6720 | StatusBarNotification sbn = new StatusBarNotification(PKG_P, PKG_P, 1, |
| 6721 | "testRecordMessages_invalidMsg", mUid, 0, nb.build(), |
| 6722 | UserHandle.getUserHandleForUid(mUid), null, 0); |
| 6723 | NotificationRecord nr = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 6724 | |
Julia Reynolds | c76888d | 2020-05-04 15:36:09 -0400 | [diff] [blame] | 6725 | when(mLauncherApps.getShortcuts(any(), any())).thenReturn(null); |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 6726 | mBinderService.enqueueNotificationWithTag(PKG_P, PKG_P, nr.getSbn().getTag(), |
Julia Reynolds | a7dac43 | 2020-04-23 12:17:31 -0400 | [diff] [blame] | 6727 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
| 6728 | waitForIdle(); |
| 6729 | |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 6730 | assertTrue(mBinderService.isInInvalidMsgState(PKG_P, mUid)); |
| 6731 | } |
| 6732 | |
| 6733 | @Test |
| 6734 | public void testRecordMessages_invalidMsg_notMessageStyle() throws RemoteException { |
| 6735 | Notification.Builder nb = new Notification.Builder(mContext, |
| 6736 | mTestNotificationChannel.getId()) |
| 6737 | .setContentTitle("foo") |
| 6738 | .setShortcutId(null) |
| 6739 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 6740 | .setCategory(Notification.CATEGORY_MESSAGE); |
| 6741 | StatusBarNotification sbn = new StatusBarNotification(PKG_O, PKG_O, 1, |
| 6742 | "testRecordMessages_invalidMsg_notMessageStyle", mUid, 0, nb.build(), |
| 6743 | UserHandle.getUserHandleForUid(mUid), null, 0); |
| 6744 | NotificationRecord nr = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 6745 | |
| 6746 | when(mLauncherApps.getShortcuts(any(), any())).thenReturn(null); |
| 6747 | mBinderService.enqueueNotificationWithTag(PKG_O, PKG_O, nr.getSbn().getTag(), |
| 6748 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
| 6749 | waitForIdle(); |
| 6750 | |
| 6751 | // PKG_O is allowed to be in conversation space b/c of override in |
| 6752 | // TestableNotificationManagerService |
| 6753 | assertTrue(mBinderService.isInInvalidMsgState(PKG_O, mUid)); |
Julia Reynolds | a7dac43 | 2020-04-23 12:17:31 -0400 | [diff] [blame] | 6754 | } |
Julia Reynolds | 8582df5 | 2020-04-24 18:30:59 -0400 | [diff] [blame] | 6755 | |
| 6756 | @Test |
| 6757 | public void testRecordMessages_validMsg() throws RemoteException { |
Julia Reynolds | 8582df5 | 2020-04-24 18:30:59 -0400 | [diff] [blame] | 6758 | Notification.Builder nb = getMessageStyleNotifBuilder(false /* addDefaultMetadata */, |
| 6759 | null /* groupKey */, false /* isSummary */); |
Julia Reynolds | bc23c7e | 2020-05-13 18:16:32 -0400 | [diff] [blame] | 6760 | nb.setShortcutId(null); |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 6761 | StatusBarNotification sbn = new StatusBarNotification(PKG_P, PKG_P, 1, |
| 6762 | "testRecordMessages_validMsg", mUid, 0, nb.build(), |
| 6763 | UserHandle.getUserHandleForUid(mUid), null, 0); |
Julia Reynolds | 8582df5 | 2020-04-24 18:30:59 -0400 | [diff] [blame] | 6764 | NotificationRecord nr = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 6765 | |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 6766 | mBinderService.enqueueNotificationWithTag(PKG_P, PKG_P, nr.getSbn().getTag(), |
Julia Reynolds | 8582df5 | 2020-04-24 18:30:59 -0400 | [diff] [blame] | 6767 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
| 6768 | waitForIdle(); |
| 6769 | |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 6770 | assertTrue(mBinderService.isInInvalidMsgState(PKG_P, mUid)); |
Julia Reynolds | bc23c7e | 2020-05-13 18:16:32 -0400 | [diff] [blame] | 6771 | |
| 6772 | nr = generateMessageBubbleNotifRecord(mTestNotificationChannel, |
| 6773 | "testRecordMessages_validMsg"); |
| 6774 | |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 6775 | mBinderService.enqueueNotificationWithTag(PKG_P, PKG_P, nr.getSbn().getTag(), |
Julia Reynolds | bc23c7e | 2020-05-13 18:16:32 -0400 | [diff] [blame] | 6776 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
| 6777 | waitForIdle(); |
| 6778 | |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 6779 | assertFalse(mBinderService.isInInvalidMsgState(PKG_P, mUid)); |
Julia Reynolds | bc23c7e | 2020-05-13 18:16:32 -0400 | [diff] [blame] | 6780 | } |
| 6781 | |
| 6782 | @Test |
| 6783 | public void testRecordMessages_invalidMsg_afterValidMsg() throws RemoteException { |
| 6784 | NotificationRecord nr = generateMessageBubbleNotifRecord(mTestNotificationChannel, |
| 6785 | "testRecordMessages_invalidMsg_afterValidMsg_1"); |
| 6786 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 6787 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
| 6788 | waitForIdle(); |
| 6789 | assertTrue(mService.getNotificationRecord(nr.getKey()).isConversation()); |
| 6790 | |
| 6791 | mBinderService.cancelAllNotifications(PKG, mUid); |
| 6792 | waitForIdle(); |
| 6793 | |
| 6794 | Notification.Builder nb = getMessageStyleNotifBuilder(false /* addDefaultMetadata */, |
| 6795 | null /* groupKey */, false /* isSummary */); |
| 6796 | nb.setShortcutId(null); |
| 6797 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, |
| 6798 | "testRecordMessages_invalidMsg_afterValidMsg_2", mUid, 0, nb.build(), |
Julia Reynolds | e61758a | 2020-06-10 10:26:44 -0400 | [diff] [blame] | 6799 | UserHandle.getUserHandleForUid(mUid), null, 0); |
Julia Reynolds | bc23c7e | 2020-05-13 18:16:32 -0400 | [diff] [blame] | 6800 | nr = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 6801 | |
| 6802 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 6803 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
| 6804 | waitForIdle(); |
| 6805 | |
| 6806 | assertFalse(mService.getNotificationRecord(nr.getKey()).isConversation()); |
Julia Reynolds | 8582df5 | 2020-04-24 18:30:59 -0400 | [diff] [blame] | 6807 | } |
Julia Reynolds | 1a80b29 | 2020-05-01 11:55:57 -0400 | [diff] [blame] | 6808 | |
| 6809 | @Test |
| 6810 | public void testCanPostFgsWhenOverLimit() throws RemoteException { |
| 6811 | for (int i = 0; i < NotificationManagerService.MAX_PACKAGE_NOTIFICATIONS; i++) { |
| 6812 | StatusBarNotification sbn = generateNotificationRecord(mTestNotificationChannel, |
| 6813 | i, null, false).getSbn(); |
| 6814 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 6815 | "testCanPostFgsWhenOverLimit", |
| 6816 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 6817 | } |
| 6818 | |
| 6819 | final StatusBarNotification sbn = generateNotificationRecord(null).getSbn(); |
| 6820 | sbn.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
| 6821 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 6822 | "testCanPostFgsWhenOverLimit - fgs over limit!", |
| 6823 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 6824 | |
| 6825 | waitForIdle(); |
| 6826 | |
| 6827 | StatusBarNotification[] notifs = |
| 6828 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 6829 | assertEquals(NotificationManagerService.MAX_PACKAGE_NOTIFICATIONS + 1, notifs.length); |
| 6830 | assertEquals(NotificationManagerService.MAX_PACKAGE_NOTIFICATIONS + 1, |
| 6831 | mService.getNotificationRecordCount()); |
| 6832 | } |
| 6833 | |
| 6834 | @Test |
| 6835 | public void testCannotPostNonFgsWhenOverLimit() throws RemoteException { |
| 6836 | for (int i = 0; i < NotificationManagerService.MAX_PACKAGE_NOTIFICATIONS; i++) { |
| 6837 | StatusBarNotification sbn = generateNotificationRecord(mTestNotificationChannel, |
| 6838 | i, null, false).getSbn(); |
| 6839 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 6840 | "testCanPostFgsWhenOverLimit", |
| 6841 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 6842 | waitForIdle(); |
| 6843 | } |
| 6844 | |
| 6845 | final StatusBarNotification sbn = generateNotificationRecord(mTestNotificationChannel, |
| 6846 | 100, null, false).getSbn(); |
| 6847 | sbn.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
| 6848 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 6849 | "testCanPostFgsWhenOverLimit - fgs over limit!", |
| 6850 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 6851 | |
| 6852 | final StatusBarNotification sbn2 = generateNotificationRecord(mTestNotificationChannel, |
| 6853 | 101, null, false).getSbn(); |
| 6854 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 6855 | "testCanPostFgsWhenOverLimit - non fgs over limit!", |
| 6856 | sbn2.getId(), sbn2.getNotification(), sbn2.getUserId()); |
| 6857 | |
| 6858 | waitForIdle(); |
| 6859 | |
| 6860 | StatusBarNotification[] notifs = |
| 6861 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 6862 | assertEquals(NotificationManagerService.MAX_PACKAGE_NOTIFICATIONS + 1, notifs.length); |
| 6863 | assertEquals(NotificationManagerService.MAX_PACKAGE_NOTIFICATIONS + 1, |
| 6864 | mService.getNotificationRecordCount()); |
| 6865 | } |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 6866 | } |