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; |
| 181 | import org.junit.Test; |
| 182 | import org.junit.runner.RunWith; |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 183 | import org.mockito.ArgumentCaptor; |
| 184 | import org.mockito.Mock; |
| 185 | import org.mockito.MockitoAnnotations; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 186 | import org.mockito.stubbing.Answer; |
Julia Reynolds | 469144c | 2019-06-21 14:30:28 -0400 | [diff] [blame] | 187 | import org.xmlpull.v1.XmlPullParser; |
| 188 | import org.xmlpull.v1.XmlSerializer; |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 189 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 190 | import java.io.BufferedInputStream; |
Julia Reynolds | 469144c | 2019-06-21 14:30:28 -0400 | [diff] [blame] | 191 | import java.io.BufferedOutputStream; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 192 | import java.io.ByteArrayInputStream; |
Julia Reynolds | 469144c | 2019-06-21 14:30:28 -0400 | [diff] [blame] | 193 | import java.io.ByteArrayOutputStream; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 194 | import java.io.File; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 195 | import java.io.FileOutputStream; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 196 | import java.util.ArrayList; |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 197 | import java.util.Arrays; |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 198 | import java.util.Collections; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 199 | import java.util.List; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 200 | import java.util.Map; |
Robin Lee | d107af6 | 2018-04-27 13:55:56 +0200 | [diff] [blame] | 201 | import java.util.function.Consumer; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 202 | |
Julia Reynolds | b681ffe | 2019-06-19 13:40:46 -0400 | [diff] [blame] | 203 | |
Geoffrey Pitsch | 8185d38 | 2017-05-19 18:41:32 -0400 | [diff] [blame] | 204 | @SmallTest |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 205 | @RunWith(AndroidTestingRunner.class) |
| 206 | @RunWithLooper |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 207 | public class NotificationManagerServiceTest extends UiServiceTestCase { |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 208 | private static final String TEST_CHANNEL_ID = "NotificationManagerServiceTestChannelId"; |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 209 | |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 210 | private final int mUid = Binder.getCallingUid(); |
Julia Reynolds | d78263d | 2018-01-30 10:40:41 -0500 | [diff] [blame] | 211 | private TestableNotificationManagerService mService; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 212 | private INotificationManager mBinderService; |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 213 | private NotificationManagerInternal mInternalService; |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 214 | private ShortcutHelper mShortcutHelper; |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 215 | @Mock |
| 216 | private IPackageManager mPackageManager; |
| 217 | @Mock |
| 218 | private PackageManager mPackageManagerClient; |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 219 | @Mock |
| 220 | private WindowManagerInternal mWindowManagerInternal; |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 221 | private TestableContext mContext = spy(getContext()); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 222 | private final String PKG = mContext.getPackageName(); |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 223 | private TestableLooper mTestableLooper; |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 224 | @Mock |
| 225 | private RankingHelper mRankingHelper; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 226 | @Mock private PreferencesHelper mPreferencesHelper; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 227 | AtomicFile mPolicyFile; |
| 228 | File mFile; |
| 229 | @Mock |
Geoffrey Pitsch | d5bcf21 | 2017-06-01 15:45:35 -0400 | [diff] [blame] | 230 | private NotificationUsageStats mUsageStats; |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 231 | @Mock |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 232 | private UsageStatsManagerInternal mAppUsageStats; |
| 233 | @Mock |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 234 | private AudioManager mAudioManager; |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 235 | @Mock |
Mady Mellor | 2ac2d3a | 2020-01-08 17:18:54 -0800 | [diff] [blame] | 236 | private LauncherApps mLauncherApps; |
| 237 | @Mock |
Julia Reynolds | fa27307 | 2020-04-14 15:31:21 -0400 | [diff] [blame] | 238 | private ShortcutServiceInternal mShortcutServiceInternal; |
| 239 | @Mock |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 240 | ActivityManager mActivityManager; |
Kristian Monsen | 05f3479 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 241 | @Mock |
| 242 | Resources mResources; |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 243 | @Mock |
| 244 | RankingHandler mRankingHandler; |
Julia Reynolds | 3ff26d2 | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 245 | |
Julia Reynolds | b317ff7 | 2019-11-26 14:20:51 -0500 | [diff] [blame] | 246 | private static final int MAX_POST_DELAY = 1000; |
| 247 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 248 | private NotificationChannel mTestNotificationChannel = new NotificationChannel( |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 249 | TEST_CHANNEL_ID, TEST_CHANNEL_ID, IMPORTANCE_DEFAULT); |
Gustav Sennton | a8e38aa | 2019-01-22 14:55:39 +0000 | [diff] [blame] | 250 | |
| 251 | private static final int NOTIFICATION_LOCATION_UNKNOWN = 0; |
| 252 | |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 253 | @Mock |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 254 | private NotificationListeners mListeners; |
| 255 | @Mock private NotificationAssistants mAssistants; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 256 | @Mock private ConditionProviders mConditionProviders; |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 257 | private ManagedServices.ManagedServiceInfo mListener; |
| 258 | @Mock private ICompanionDeviceManager mCompanionMgr; |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 259 | @Mock SnoozeHelper mSnoozeHelper; |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 260 | @Mock GroupHelper mGroupHelper; |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 261 | @Mock |
| 262 | IBinder mPermOwner; |
| 263 | @Mock |
| 264 | IActivityManager mAm; |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 265 | @Mock |
Bernardo Rufino | e6cb310 | 2020-03-06 20:33:11 +0000 | [diff] [blame] | 266 | ActivityTaskManagerInternal mAtm; |
| 267 | @Mock |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 268 | IUriGrantsManager mUgm; |
| 269 | @Mock |
| 270 | UriGrantsManagerInternal mUgmInternal; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 271 | @Mock |
| 272 | AppOpsManager mAppOpsManager; |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 273 | @Mock |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 274 | private TestableNotificationManagerService.NotificationAssistantAccessGrantedCallback |
| 275 | mNotificationAssistantAccessGrantedCallback; |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 276 | @Mock |
| 277 | UserManager mUm; |
Julia Reynolds | b317ff7 | 2019-11-26 14:20:51 -0500 | [diff] [blame] | 278 | @Mock |
| 279 | NotificationHistoryManager mHistoryManager; |
Yotam Aron | 7429997 | 2020-01-16 16:20:58 +0200 | [diff] [blame] | 280 | @Mock |
| 281 | StatsManager mStatsManager; |
Will Brockman | 2b6959e | 2020-01-22 09:59:50 -0500 | [diff] [blame] | 282 | NotificationRecordLoggerFake mNotificationRecordLogger = new NotificationRecordLoggerFake(); |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 283 | private InstanceIdSequence mNotificationInstanceIdSequence = new InstanceIdSequenceFake( |
| 284 | 1 << 30); |
Bernardo Rufino | 18725b6 | 2020-01-21 14:12:43 +0000 | [diff] [blame] | 285 | @Mock |
| 286 | StatusBarManagerInternal mStatusBar; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 287 | |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 288 | // Use a Testable subclass so we can simulate calls from the system without failing. |
| 289 | private static class TestableNotificationManagerService extends NotificationManagerService { |
Julia Reynolds | d78263d | 2018-01-30 10:40:41 -0500 | [diff] [blame] | 290 | int countSystemChecks = 0; |
Brad Stenning | 8c991ea | 2018-07-31 13:33:01 -0700 | [diff] [blame] | 291 | boolean isSystemUid = true; |
Gustav Sennton | 44dc588 | 2018-12-13 14:38:50 +0000 | [diff] [blame] | 292 | int countLogSmartSuggestionsVisible = 0; |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 293 | @Nullable |
| 294 | NotificationAssistantAccessGrantedCallback mNotificationAssistantAccessGrantedCallback; |
Julia Reynolds | d78263d | 2018-01-30 10:40:41 -0500 | [diff] [blame] | 295 | |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 296 | TestableNotificationManagerService(Context context, NotificationRecordLogger logger, |
| 297 | InstanceIdSequence notificationInstanceIdSequence) { |
| 298 | super(context, logger, notificationInstanceIdSequence); |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 299 | } |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 300 | |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 301 | RankingHelper getRankingHelper() { |
| 302 | return mRankingHelper; |
| 303 | } |
| 304 | |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 305 | @Override |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 306 | protected boolean isCallingUidSystem() { |
Julia Reynolds | d78263d | 2018-01-30 10:40:41 -0500 | [diff] [blame] | 307 | countSystemChecks++; |
Brad Stenning | 8c991ea | 2018-07-31 13:33:01 -0700 | [diff] [blame] | 308 | return isSystemUid; |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | @Override |
| 312 | protected boolean isCallerSystemOrPhone() { |
Julia Reynolds | d78263d | 2018-01-30 10:40:41 -0500 | [diff] [blame] | 313 | countSystemChecks++; |
Brad Stenning | 8c991ea | 2018-07-31 13:33:01 -0700 | [diff] [blame] | 314 | return isSystemUid; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 315 | } |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 316 | |
| 317 | @Override |
| 318 | protected ICompanionDeviceManager getCompanionManager() { |
| 319 | return null; |
| 320 | } |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 321 | |
| 322 | @Override |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 323 | protected void reportUserInteraction(NotificationRecord r) { |
| 324 | return; |
| 325 | } |
Julia Reynolds | b62dad4 | 2018-11-26 16:33:02 -0500 | [diff] [blame] | 326 | |
| 327 | @Override |
| 328 | protected void handleSavePolicyFile() { |
| 329 | return; |
| 330 | } |
Gustav Sennton | 44dc588 | 2018-12-13 14:38:50 +0000 | [diff] [blame] | 331 | |
| 332 | @Override |
Gustav Sennton | c7d0d32 | 2019-01-07 15:36:41 +0000 | [diff] [blame] | 333 | void logSmartSuggestionsVisible(NotificationRecord r, int notificationLocation) { |
| 334 | super.logSmartSuggestionsVisible(r, notificationLocation); |
Gustav Sennton | 44dc588 | 2018-12-13 14:38:50 +0000 | [diff] [blame] | 335 | countLogSmartSuggestionsVisible++; |
| 336 | } |
| 337 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 338 | @Override |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 339 | protected void setNotificationAssistantAccessGrantedForUserInternal( |
| 340 | ComponentName assistant, int userId, boolean granted) { |
| 341 | if (mNotificationAssistantAccessGrantedCallback != null) { |
| 342 | mNotificationAssistantAccessGrantedCallback.onGranted(assistant, userId, granted); |
| 343 | return; |
| 344 | } |
| 345 | super.setNotificationAssistantAccessGrantedForUserInternal(assistant, userId, granted); |
| 346 | } |
| 347 | |
| 348 | private void setNotificationAssistantAccessGrantedCallback( |
| 349 | @Nullable NotificationAssistantAccessGrantedCallback callback) { |
| 350 | this.mNotificationAssistantAccessGrantedCallback = callback; |
| 351 | } |
| 352 | |
| 353 | interface NotificationAssistantAccessGrantedCallback { |
| 354 | void onGranted(ComponentName assistant, int userId, boolean granted); |
| 355 | } |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 356 | } |
| 357 | |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 358 | private class TestableToastCallback extends ITransientNotification.Stub { |
| 359 | @Override |
| 360 | public void show(IBinder windowToken) { |
| 361 | } |
| 362 | |
| 363 | @Override |
| 364 | public void hide() { |
| 365 | } |
| 366 | } |
| 367 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 368 | @Before |
| 369 | public void setUp() throws Exception { |
Beverly | 24d103b | 2020-02-18 17:40:57 -0500 | [diff] [blame] | 370 | // Shell permisssions will override permissions of our app, so add all necessary permissions |
Beverly | 080782f | 2020-02-24 16:42:57 -0500 | [diff] [blame] | 371 | // for this test here: |
Stanislav Zholnin | 872afd4 | 2019-03-12 15:57:25 +0000 | [diff] [blame] | 372 | InstrumentationRegistry.getInstrumentation().getUiAutomation().adoptShellPermissionIdentity( |
Beverly | 24d103b | 2020-02-18 17:40:57 -0500 | [diff] [blame] | 373 | "android.permission.WRITE_DEVICE_CONFIG", |
| 374 | "android.permission.READ_DEVICE_CONFIG", |
| 375 | "android.permission.READ_CONTACTS"); |
Stanislav Zholnin | 872afd4 | 2019-03-12 15:57:25 +0000 | [diff] [blame] | 376 | |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 377 | MockitoAnnotations.initMocks(this); |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 378 | |
Mady Mellor | 13f9bc8 | 2020-03-24 19:09:28 -0700 | [diff] [blame] | 379 | DeviceIdleInternal deviceIdleInternal = mock(DeviceIdleInternal.class); |
| 380 | when(deviceIdleInternal.getNotificationWhitelistDuration()).thenReturn(3000L); |
| 381 | ActivityManagerInternal activityManagerInternal = mock(ActivityManagerInternal.class); |
| 382 | |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 383 | LocalServices.removeServiceForTest(UriGrantsManagerInternal.class); |
| 384 | LocalServices.addService(UriGrantsManagerInternal.class, mUgmInternal); |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 385 | LocalServices.removeServiceForTest(WindowManagerInternal.class); |
| 386 | LocalServices.addService(WindowManagerInternal.class, mWindowManagerInternal); |
Bernardo Rufino | 18725b6 | 2020-01-21 14:12:43 +0000 | [diff] [blame] | 387 | LocalServices.removeServiceForTest(StatusBarManagerInternal.class); |
| 388 | LocalServices.addService(StatusBarManagerInternal.class, mStatusBar); |
Mady Mellor | 13f9bc8 | 2020-03-24 19:09:28 -0700 | [diff] [blame] | 389 | LocalServices.removeServiceForTest(DeviceIdleInternal.class); |
| 390 | LocalServices.addService(DeviceIdleInternal.class, deviceIdleInternal); |
| 391 | LocalServices.removeServiceForTest(ActivityManagerInternal.class); |
| 392 | LocalServices.addService(ActivityManagerInternal.class, activityManagerInternal); |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 393 | |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 394 | doNothing().when(mContext).sendBroadcastAsUser(any(), any(), any()); |
| 395 | |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 396 | mService = new TestableNotificationManagerService(mContext, mNotificationRecordLogger, |
| 397 | mNotificationInstanceIdSequence); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 398 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 399 | // Use this testable looper. |
| 400 | mTestableLooper = TestableLooper.get(this); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 401 | // MockPackageManager - default returns ApplicationInfo with matching calling UID |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 402 | mContext.setMockPackageManager(mPackageManagerClient); |
Julia Reynolds | b681ffe | 2019-06-19 13:40:46 -0400 | [diff] [blame] | 403 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 404 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), anyInt())) |
Julia Reynolds | b681ffe | 2019-06-19 13:40:46 -0400 | [diff] [blame] | 405 | .thenAnswer((Answer<ApplicationInfo>) invocation -> { |
| 406 | Object[] args = invocation.getArguments(); |
| 407 | return getApplicationInfo((String) args[0], mUid); |
| 408 | }); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 409 | when(mPackageManagerClient.getApplicationInfoAsUser(anyString(), anyInt(), anyInt())) |
Julia Reynolds | b681ffe | 2019-06-19 13:40:46 -0400 | [diff] [blame] | 410 | .thenAnswer((Answer<ApplicationInfo>) invocation -> { |
| 411 | Object[] args = invocation.getArguments(); |
| 412 | return getApplicationInfo((String) args[0], mUid); |
| 413 | }); |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 414 | when(mPackageManagerClient.getPackageUidAsUser(any(), anyInt())).thenReturn(mUid); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 415 | final LightsManager mockLightsManager = mock(LightsManager.class); |
Ivailo Karamanolev | f773e10 | 2020-01-16 16:10:42 +0100 | [diff] [blame] | 416 | when(mockLightsManager.getLight(anyInt())).thenReturn(mock(LogicalLight.class)); |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 417 | when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_NORMAL); |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 418 | when(mPackageManagerClient.hasSystemFeature(FEATURE_WATCH)).thenReturn(false); |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 419 | when(mUgmInternal.newUriPermissionOwner(anyString())).thenReturn(mPermOwner); |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 420 | when(mPackageManager.getPackagesForUid(mUid)).thenReturn(new String[]{PKG}); |
Julia Reynolds | 4214da9 | 2019-04-10 15:04:06 -0400 | [diff] [blame] | 421 | when(mPackageManagerClient.getPackagesForUid(anyInt())).thenReturn(new String[]{PKG}); |
Julia Reynolds | e99db5a | 2019-04-16 12:50:04 -0400 | [diff] [blame] | 422 | mContext.addMockSystemService(AppOpsManager.class, mock(AppOpsManager.class)); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 423 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 424 | // 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] | 425 | mFile = new File(mContext.getCacheDir(), "test.xml"); |
| 426 | mFile.createNewFile(); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 427 | final String preupgradeXml = "<notification-policy></notification-policy>"; |
| 428 | mPolicyFile = new AtomicFile(mFile); |
| 429 | FileOutputStream fos = mPolicyFile.startWrite(); |
| 430 | fos.write(preupgradeXml.getBytes()); |
| 431 | mPolicyFile.finishWrite(fos); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 432 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 433 | // Setup managed services |
| 434 | mListener = mListeners.new ManagedServiceInfo( |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 435 | null, new ComponentName(PKG, "test_class"), mUid, true, null, 0); |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 436 | ComponentName defaultComponent = ComponentName.unflattenFromString("config/device"); |
| 437 | ArraySet<ComponentName> components = new ArraySet<>(); |
| 438 | components.add(defaultComponent); |
| 439 | when(mListeners.getDefaultComponents()).thenReturn(components); |
| 440 | when(mConditionProviders.getDefaultPackages()) |
| 441 | .thenReturn(new ArraySet<>(Arrays.asList("config"))); |
| 442 | when(mAssistants.getDefaultComponents()).thenReturn(components); |
| 443 | when(mAssistants.queryPackageForServices( |
| 444 | anyString(), anyInt(), anyInt())).thenReturn(components); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 445 | when(mListeners.checkServiceTokenLocked(any())).thenReturn(mListener); |
| 446 | ManagedServices.Config listenerConfig = new ManagedServices.Config(); |
| 447 | listenerConfig.xmlTag = NotificationListeners.TAG_ENABLED_NOTIFICATION_LISTENERS; |
| 448 | when(mListeners.getConfig()).thenReturn(listenerConfig); |
| 449 | ManagedServices.Config assistantConfig = new ManagedServices.Config(); |
| 450 | assistantConfig.xmlTag = NotificationAssistants.TAG_ENABLED_NOTIFICATION_ASSISTANTS; |
| 451 | when(mAssistants.getConfig()).thenReturn(assistantConfig); |
| 452 | ManagedServices.Config dndConfig = new ManagedServices.Config(); |
| 453 | dndConfig.xmlTag = ConditionProviders.TAG_ENABLED_DND_APPS; |
| 454 | when(mConditionProviders.getConfig()).thenReturn(dndConfig); |
| 455 | |
Julia Reynolds | 418a8ff | 2019-03-21 10:45:10 -0400 | [diff] [blame] | 456 | when(mAssistants.isAdjustmentAllowed(anyString())).thenReturn(true); |
| 457 | |
Julia Reynolds | b317ff7 | 2019-11-26 14:20:51 -0500 | [diff] [blame] | 458 | mService.init(mService.new WorkerHandler(mTestableLooper.getLooper()), |
| 459 | mRankingHandler, mPackageManager, mPackageManagerClient, mockLightsManager, |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 460 | mListeners, mAssistants, mConditionProviders, |
| 461 | mCompanionMgr, mSnoozeHelper, mUsageStats, mPolicyFile, mActivityManager, |
Bernardo Rufino | e6cb310 | 2020-03-06 20:33:11 +0000 | [diff] [blame] | 462 | mGroupHelper, mAm, mAtm, mAppUsageStats, |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 463 | mock(DevicePolicyManagerInternal.class), mUgm, mUgmInternal, |
Will Brockman | af25fbd | 2020-03-26 15:49:47 -0400 | [diff] [blame] | 464 | mAppOpsManager, mUm, mHistoryManager, mStatsManager, |
| 465 | mock(TelephonyManager.class)); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 466 | mService.onBootPhase(SystemService.PHASE_SYSTEM_SERVICES_READY); |
| 467 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 468 | mService.setAudioManager(mAudioManager); |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 469 | |
| 470 | mShortcutHelper = mService.getShortcutHelper(); |
| 471 | mShortcutHelper.setLauncherApps(mLauncherApps); |
Julia Reynolds | fa27307 | 2020-04-14 15:31:21 -0400 | [diff] [blame] | 472 | mShortcutHelper.setShortcutServiceInternal(mShortcutServiceInternal); |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 473 | |
| 474 | // Set the testable bubble extractor |
| 475 | RankingHelper rankingHelper = mService.getRankingHelper(); |
| 476 | BubbleExtractor extractor = rankingHelper.findExtractor(BubbleExtractor.class); |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 477 | extractor.setActivityManager(mActivityManager); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 478 | |
| 479 | // Tests call directly into the Binder. |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 480 | mBinderService = mService.getBinderService(); |
| 481 | mInternalService = mService.getInternalService(); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 482 | |
| 483 | mBinderService.createNotificationChannels( |
| 484 | PKG, new ParceledListSlice(Arrays.asList(mTestNotificationChannel))); |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 485 | assertNotNull(mBinderService.getNotificationChannel( |
| 486 | PKG, mContext.getUserId(), PKG, TEST_CHANNEL_ID)); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 487 | clearInvocations(mRankingHandler); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 488 | } |
| 489 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 490 | @After |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 491 | public void assertNotificationRecordLoggerCallsValid() { |
| 492 | for (NotificationRecordLoggerFake.CallRecord call : mNotificationRecordLogger.getCalls()) { |
| 493 | if (call.wasLogged) { |
| 494 | assertNotNull(call.event); |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | @After |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 500 | public void tearDown() throws Exception { |
Julia Reynolds | b681ffe | 2019-06-19 13:40:46 -0400 | [diff] [blame] | 501 | if (mFile != null) mFile.delete(); |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 502 | clearDeviceConfig(); |
Julia Reynolds | b317ff7 | 2019-11-26 14:20:51 -0500 | [diff] [blame] | 503 | mService.unregisterDeviceConfigChange(); |
Stanislav Zholnin | 872afd4 | 2019-03-12 15:57:25 +0000 | [diff] [blame] | 504 | InstrumentationRegistry.getInstrumentation() |
| 505 | .getUiAutomation().dropShellPermissionIdentity(); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 506 | } |
| 507 | |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 508 | private ArrayMap<Boolean, ArrayList<ComponentName>> generateResetComponentValues() { |
| 509 | ArrayMap<Boolean, ArrayList<ComponentName>> changed = new ArrayMap<>(); |
| 510 | changed.put(true, new ArrayList<>()); |
| 511 | changed.put(false, new ArrayList<>()); |
| 512 | return changed; |
| 513 | } |
Julia Reynolds | b681ffe | 2019-06-19 13:40:46 -0400 | [diff] [blame] | 514 | private ApplicationInfo getApplicationInfo(String pkg, int uid) { |
| 515 | final ApplicationInfo applicationInfo = new ApplicationInfo(); |
| 516 | applicationInfo.uid = uid; |
| 517 | switch (pkg) { |
| 518 | case PKG_N_MR1: |
| 519 | applicationInfo.targetSdkVersion = Build.VERSION_CODES.N_MR1; |
| 520 | break; |
| 521 | case PKG_O: |
| 522 | applicationInfo.targetSdkVersion = Build.VERSION_CODES.O; |
| 523 | break; |
| 524 | case PKG_P: |
| 525 | applicationInfo.targetSdkVersion = Build.VERSION_CODES.P; |
| 526 | break; |
| 527 | default: |
| 528 | applicationInfo.targetSdkVersion = Build.VERSION_CODES.CUR_DEVELOPMENT; |
| 529 | break; |
| 530 | } |
| 531 | return applicationInfo; |
| 532 | } |
| 533 | |
Julia Reynolds | 7bcb57b | 2018-01-22 10:37:58 -0500 | [diff] [blame] | 534 | public void waitForIdle() { |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 535 | mTestableLooper.processAllMessages(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 536 | } |
| 537 | |
Julia Reynolds | 12ba4cf | 2020-01-10 16:01:38 -0500 | [diff] [blame] | 538 | private void setUpPrefsForBubbles(String pkg, int uid, boolean globalEnabled, |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 539 | int pkgPref, boolean channelEnabled) { |
Julia Reynolds | 12ba4cf | 2020-01-10 16:01:38 -0500 | [diff] [blame] | 540 | Settings.Global.putInt(mContext.getContentResolver(), |
| 541 | Settings.Global.NOTIFICATION_BUBBLES, globalEnabled ? 1 : 0); |
| 542 | mService.mPreferencesHelper.updateBubblesEnabled(); |
| 543 | assertEquals(globalEnabled, mService.mPreferencesHelper.bubblesEnabled()); |
| 544 | try { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 545 | mBinderService.setBubblesAllowed(pkg, uid, pkgPref); |
Julia Reynolds | 12ba4cf | 2020-01-10 16:01:38 -0500 | [diff] [blame] | 546 | } catch (RemoteException e) { |
| 547 | e.printStackTrace(); |
| 548 | } |
Mady Mellor | c682034 | 2019-05-20 12:04:36 -0700 | [diff] [blame] | 549 | mTestNotificationChannel.setAllowBubbles(channelEnabled); |
| 550 | } |
| 551 | |
Julia Reynolds | 7bcb57b | 2018-01-22 10:37:58 -0500 | [diff] [blame] | 552 | private StatusBarNotification generateSbn(String pkg, int uid, long postTime, int userId) { |
| 553 | Notification.Builder nb = new Notification.Builder(mContext, "a") |
| 554 | .setContentTitle("foo") |
| 555 | .setSmallIcon(android.R.drawable.sym_def_app_icon); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 556 | StatusBarNotification sbn = new StatusBarNotification(pkg, pkg, uid, |
| 557 | "tag" + System.currentTimeMillis(), uid, 0, |
Julia Reynolds | 7bcb57b | 2018-01-22 10:37:58 -0500 | [diff] [blame] | 558 | nb.build(), new UserHandle(userId), null, postTime); |
| 559 | return sbn; |
| 560 | } |
| 561 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 562 | private NotificationRecord generateNotificationRecord(NotificationChannel channel, int id, |
| 563 | String groupKey, boolean isSummary) { |
| 564 | Notification.Builder nb = new Notification.Builder(mContext, channel.getId()) |
| 565 | .setContentTitle("foo") |
| 566 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 567 | .setGroup(groupKey) |
| 568 | .setGroupSummary(isSummary); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 569 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, id, |
| 570 | "tag" + System.currentTimeMillis(), mUid, 0, |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 571 | nb.build(), new UserHandle(mUid), null, 0); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 572 | return new NotificationRecord(mContext, sbn, channel); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 573 | } |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 574 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 575 | private NotificationRecord generateNotificationRecord(NotificationChannel channel) { |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 576 | return generateNotificationRecord(channel, null); |
| 577 | } |
| 578 | |
| 579 | private NotificationRecord generateNotificationRecord(NotificationChannel channel, |
| 580 | Notification.TvExtender extender) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 581 | if (channel == null) { |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 582 | channel = mTestNotificationChannel; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 583 | } |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 584 | Notification.Builder nb = new Notification.Builder(mContext, channel.getId()) |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 585 | .setContentTitle("foo") |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 586 | .setSmallIcon(android.R.drawable.sym_def_app_icon); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 587 | if (extender != null) { |
| 588 | nb.extend(extender); |
| 589 | } |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 590 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 8, "tag", mUid, 0, |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 591 | nb.build(), new UserHandle(mUid), null, 0); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 592 | return new NotificationRecord(mContext, sbn, channel); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 593 | } |
| 594 | |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 595 | private NotificationRecord generateNotificationRecord(NotificationChannel channel, int userId) { |
| 596 | if (channel == null) { |
| 597 | channel = mTestNotificationChannel; |
| 598 | } |
| 599 | Notification.Builder nb = new Notification.Builder(mContext, channel.getId()) |
| 600 | .setContentTitle("foo") |
| 601 | .setSmallIcon(android.R.drawable.sym_def_app_icon); |
| 602 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, "tag", mUid, 0, |
| 603 | nb.build(), new UserHandle(userId), null, 0); |
| 604 | return new NotificationRecord(mContext, sbn, channel); |
| 605 | } |
| 606 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 607 | private NotificationRecord generateMessageBubbleNotifRecord(NotificationChannel channel, |
| 608 | String tag) { |
| 609 | return generateMessageBubbleNotifRecord(true, channel, 1, tag, null, false); |
| 610 | } |
| 611 | |
| 612 | private NotificationRecord generateMessageBubbleNotifRecord(boolean addMetadata, |
| 613 | NotificationChannel channel, int id, String tag, String groupKey, boolean isSummary) { |
| 614 | if (channel == null) { |
| 615 | channel = mTestNotificationChannel; |
| 616 | } |
| 617 | if (tag == null) { |
| 618 | tag = "tag"; |
| 619 | } |
| 620 | Notification.Builder nb = getMessageStyleNotifBuilder(addMetadata, groupKey, isSummary); |
| 621 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, id, |
| 622 | tag, mUid, 0, |
| 623 | nb.build(), new UserHandle(mUid), null, 0); |
| 624 | return new NotificationRecord(mContext, sbn, channel); |
| 625 | } |
| 626 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 627 | private Map<String, Answer> getSignalExtractorSideEffects() { |
| 628 | Map<String, Answer> answers = new ArrayMap<>(); |
| 629 | |
| 630 | answers.put("override group key", invocationOnMock -> { |
| 631 | ((NotificationRecord) invocationOnMock.getArguments()[0]) |
| 632 | .setOverrideGroupKey("bananas"); |
| 633 | return null; |
| 634 | }); |
| 635 | answers.put("override people", invocationOnMock -> { |
| 636 | ((NotificationRecord) invocationOnMock.getArguments()[0]) |
| 637 | .setPeopleOverride(new ArrayList<>()); |
| 638 | return null; |
| 639 | }); |
| 640 | answers.put("snooze criteria", invocationOnMock -> { |
| 641 | ((NotificationRecord) invocationOnMock.getArguments()[0]) |
| 642 | .setSnoozeCriteria(new ArrayList<>()); |
| 643 | return null; |
| 644 | }); |
| 645 | answers.put("notification channel", invocationOnMock -> { |
| 646 | ((NotificationRecord) invocationOnMock.getArguments()[0]) |
| 647 | .updateNotificationChannel(new NotificationChannel("a", "", IMPORTANCE_LOW)); |
| 648 | return null; |
| 649 | }); |
| 650 | answers.put("badging", invocationOnMock -> { |
| 651 | NotificationRecord r = (NotificationRecord) invocationOnMock.getArguments()[0]; |
| 652 | r.setShowBadge(!r.canShowBadge()); |
| 653 | return null; |
| 654 | }); |
Julia Reynolds | 4509ce7 | 2019-01-31 13:12:43 -0500 | [diff] [blame] | 655 | answers.put("bubbles", invocationOnMock -> { |
| 656 | NotificationRecord r = (NotificationRecord) invocationOnMock.getArguments()[0]; |
| 657 | r.setAllowBubble(!r.canBubble()); |
| 658 | return null; |
| 659 | }); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 660 | answers.put("package visibility", invocationOnMock -> { |
| 661 | ((NotificationRecord) invocationOnMock.getArguments()[0]).setPackageVisibilityOverride( |
| 662 | Notification.VISIBILITY_SECRET); |
| 663 | return null; |
| 664 | }); |
| 665 | |
| 666 | return answers; |
| 667 | } |
| 668 | |
Julia Reynolds | c7dcdc2 | 2019-03-25 10:26:14 -0400 | [diff] [blame] | 669 | private void clearDeviceConfig() { |
| 670 | DeviceConfig.resetToDefaults( |
| 671 | Settings.RESET_MODE_PACKAGE_DEFAULTS, DeviceConfig.NAMESPACE_SYSTEMUI); |
| 672 | } |
| 673 | |
| 674 | private void setDefaultAssistantInDeviceConfig(String componentName) { |
| 675 | DeviceConfig.setProperty( |
| 676 | DeviceConfig.NAMESPACE_SYSTEMUI, |
| 677 | SystemUiDeviceConfigFlags.NAS_DEFAULT_SERVICE, |
| 678 | componentName, |
| 679 | false); |
| 680 | } |
| 681 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 682 | private Notification.Builder getMessageStyleNotifBuilder(boolean addBubbleMetadata, |
| 683 | String groupKey, boolean isSummary) { |
| 684 | // Give it a person |
| 685 | Person person = new Person.Builder() |
| 686 | .setName("bubblebot") |
| 687 | .build(); |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 688 | RemoteInput remoteInput = new RemoteInput.Builder("reply_key").setLabel("reply").build(); |
| 689 | PendingIntent inputIntent = PendingIntent.getActivity(mContext, 0, new Intent(), 0); |
| 690 | Icon icon = Icon.createWithResource(mContext, android.R.drawable.sym_def_app_icon); |
| 691 | Notification.Action replyAction = new Notification.Action.Builder(icon, "Reply", |
| 692 | inputIntent).addRemoteInput(remoteInput) |
| 693 | .build(); |
| 694 | // Make it messaging style |
| 695 | Notification.Builder nb = new Notification.Builder(mContext, |
| 696 | mTestNotificationChannel.getId()) |
| 697 | .setContentTitle("foo") |
| 698 | .setStyle(new Notification.MessagingStyle(person) |
| 699 | .setConversationTitle("Bubble Chat") |
| 700 | .addMessage("Hello?", |
| 701 | SystemClock.currentThreadTimeMillis() - 300000, person) |
| 702 | .addMessage("Is it me you're looking for?", |
| 703 | SystemClock.currentThreadTimeMillis(), person) |
| 704 | ) |
| 705 | .setActions(replyAction) |
| 706 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 707 | .setGroupSummary(isSummary); |
| 708 | if (groupKey != null) { |
| 709 | nb.setGroup(groupKey); |
| 710 | } |
| 711 | if (addBubbleMetadata) { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 712 | nb.setBubbleMetadata(getBubbleMetadata()); |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 713 | } |
| 714 | return nb; |
| 715 | } |
| 716 | |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 717 | private Notification.BubbleMetadata getBubbleMetadata() { |
| 718 | PendingIntent pendingIntent = mock(PendingIntent.class); |
| 719 | Intent intent = mock(Intent.class); |
| 720 | when(pendingIntent.getIntent()).thenReturn(intent); |
| 721 | |
| 722 | ActivityInfo info = new ActivityInfo(); |
| 723 | info.resizeMode = RESIZE_MODE_RESIZEABLE; |
| 724 | when(intent.resolveActivityInfo(any(), anyInt())).thenReturn(info); |
| 725 | |
| 726 | return new Notification.BubbleMetadata.Builder( |
| 727 | pendingIntent, |
| 728 | Icon.createWithResource(mContext, android.R.drawable.sym_def_app_icon)) |
| 729 | .build(); |
| 730 | } |
| 731 | |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 732 | private NotificationRecord addGroupWithBubblesAndValidateAdded(boolean summaryAutoCancel) |
| 733 | throws RemoteException { |
| 734 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 735 | String groupKey = "BUBBLE_GROUP"; |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 736 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 737 | // Notification that has bubble metadata |
| 738 | NotificationRecord nrBubble = generateMessageBubbleNotifRecord(true /* addMetadata */, |
| 739 | mTestNotificationChannel, 1 /* id */, "tag", groupKey, false /* isSummary */); |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 740 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 741 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nrBubble.getSbn().getTag(), |
| 742 | nrBubble.getSbn().getId(), nrBubble.getSbn().getNotification(), |
| 743 | nrBubble.getSbn().getUserId()); |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 744 | waitForIdle(); |
| 745 | |
| 746 | // Make sure we are a bubble |
| 747 | StatusBarNotification[] notifsAfter = mBinderService.getActiveNotifications(PKG); |
| 748 | assertEquals(1, notifsAfter.length); |
| 749 | assertTrue((notifsAfter[0].getNotification().flags & FLAG_BUBBLE) != 0); |
| 750 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 751 | // Notification without bubble metadata |
| 752 | NotificationRecord nrPlain = generateMessageBubbleNotifRecord(false /* addMetadata */, |
| 753 | mTestNotificationChannel, 2 /* id */, "tag", groupKey, false /* isSummary */); |
| 754 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 755 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nrPlain.getSbn().getTag(), |
| 756 | nrPlain.getSbn().getId(), nrPlain.getSbn().getNotification(), |
| 757 | nrPlain.getSbn().getUserId()); |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 758 | waitForIdle(); |
| 759 | |
| 760 | notifsAfter = mBinderService.getActiveNotifications(PKG); |
| 761 | assertEquals(2, notifsAfter.length); |
| 762 | |
| 763 | // Summary notification for both of those |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 764 | NotificationRecord nrSummary = generateMessageBubbleNotifRecord(false /* addMetadata */, |
| 765 | mTestNotificationChannel, 3 /* id */, "tag", groupKey, true /* isSummary */); |
| 766 | |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 767 | if (summaryAutoCancel) { |
| 768 | nrSummary.getNotification().flags |= FLAG_AUTO_CANCEL; |
| 769 | } |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 770 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nrSummary.getSbn().getTag(), |
| 771 | nrSummary.getSbn().getId(), nrSummary.getSbn().getNotification(), |
| 772 | nrSummary.getSbn().getUserId()); |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 773 | waitForIdle(); |
| 774 | |
| 775 | notifsAfter = mBinderService.getActiveNotifications(PKG); |
| 776 | assertEquals(3, notifsAfter.length); |
| 777 | |
| 778 | return nrSummary; |
| 779 | } |
| 780 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 781 | @Test |
Jay Aliomer | fea8025 | 2019-11-20 18:14:24 -0500 | [diff] [blame] | 782 | public void testDefaultAssistant_overrideDefault() { |
| 783 | final int userId = 0; |
| 784 | final String testComponent = "package/class"; |
| 785 | final List<UserInfo> userInfos = new ArrayList<>(); |
| 786 | userInfos.add(new UserInfo(0, "", 0)); |
| 787 | final ArraySet<ComponentName> validAssistants = new ArraySet<>(); |
| 788 | validAssistants.add(ComponentName.unflattenFromString(testComponent)); |
Jay Aliomer | fea8025 | 2019-11-20 18:14:24 -0500 | [diff] [blame] | 789 | when(mActivityManager.isLowRamDevice()).thenReturn(false); |
| 790 | when(mAssistants.queryPackageForServices(isNull(), anyInt(), anyInt())) |
| 791 | .thenReturn(validAssistants); |
Jay Aliomer | 76e1f272 | 2020-03-05 12:36:38 -0500 | [diff] [blame] | 792 | when(mAssistants.getDefaultComponents()).thenReturn(validAssistants); |
Jay Aliomer | fea8025 | 2019-11-20 18:14:24 -0500 | [diff] [blame] | 793 | when(mUm.getEnabledProfiles(anyInt())).thenReturn(userInfos); |
| 794 | |
| 795 | mService.setDefaultAssistantForUser(userId); |
| 796 | |
| 797 | verify(mAssistants).setPackageOrComponentEnabled( |
| 798 | eq(testComponent), eq(userId), eq(true), eq(true)); |
Jay Aliomer | fea8025 | 2019-11-20 18:14:24 -0500 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | @Test |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 802 | public void testCreateNotificationChannels_SingleChannel() throws Exception { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 803 | final NotificationChannel channel = |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 804 | new NotificationChannel("id", "name", IMPORTANCE_DEFAULT); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 805 | mBinderService.createNotificationChannels(PKG, |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 806 | new ParceledListSlice(Arrays.asList(channel))); |
| 807 | final NotificationChannel createdChannel = |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 808 | mBinderService.getNotificationChannel(PKG, mContext.getUserId(), PKG, "id"); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 809 | assertTrue(createdChannel != null); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 810 | } |
| 811 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 812 | @Test |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 813 | public void testCreateNotificationChannels_NullChannelThrowsException() throws Exception { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 814 | try { |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 815 | mBinderService.createNotificationChannels(PKG, |
Kristian Monsen | 05f3479 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 816 | new ParceledListSlice(Arrays.asList((Object[])null))); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 817 | fail("Exception should be thrown immediately."); |
| 818 | } catch (NullPointerException e) { |
| 819 | // pass |
| 820 | } |
| 821 | } |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 822 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 823 | @Test |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 824 | public void testCreateNotificationChannels_TwoChannels() throws Exception { |
| 825 | final NotificationChannel channel1 = |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 826 | new NotificationChannel("id1", "name", IMPORTANCE_DEFAULT); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 827 | final NotificationChannel channel2 = |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 828 | new NotificationChannel("id2", "name", IMPORTANCE_DEFAULT); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 829 | mBinderService.createNotificationChannels(PKG, |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 830 | new ParceledListSlice(Arrays.asList(channel1, channel2))); |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 831 | assertTrue(mBinderService.getNotificationChannel( |
| 832 | PKG, mContext.getUserId(), PKG, "id1") != null); |
| 833 | assertTrue(mBinderService.getNotificationChannel( |
| 834 | PKG, mContext.getUserId(), PKG, "id2") != null); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 835 | } |
| 836 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 837 | @Test |
Geoffrey Pitsch | 76a3aa0 | 2017-07-26 15:07:34 -0400 | [diff] [blame] | 838 | public void testCreateNotificationChannels_SecondCreateDoesNotChangeImportance() |
| 839 | throws Exception { |
| 840 | final NotificationChannel channel = |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 841 | new NotificationChannel("id", "name", IMPORTANCE_DEFAULT); |
Geoffrey Pitsch | 76a3aa0 | 2017-07-26 15:07:34 -0400 | [diff] [blame] | 842 | mBinderService.createNotificationChannels(PKG, |
| 843 | new ParceledListSlice(Arrays.asList(channel))); |
| 844 | |
| 845 | // Recreating the channel doesn't throw, but ignores importance. |
| 846 | final NotificationChannel dupeChannel = |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 847 | new NotificationChannel("id", "name", IMPORTANCE_HIGH); |
Geoffrey Pitsch | 76a3aa0 | 2017-07-26 15:07:34 -0400 | [diff] [blame] | 848 | mBinderService.createNotificationChannels(PKG, |
| 849 | new ParceledListSlice(Arrays.asList(dupeChannel))); |
| 850 | final NotificationChannel createdChannel = |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 851 | mBinderService.getNotificationChannel(PKG, mContext.getUserId(), PKG, "id"); |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 852 | assertEquals(IMPORTANCE_DEFAULT, createdChannel.getImportance()); |
Geoffrey Pitsch | 76a3aa0 | 2017-07-26 15:07:34 -0400 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | @Test |
| 856 | public void testCreateNotificationChannels_SecondCreateAllowedToDowngradeImportance() |
| 857 | throws Exception { |
| 858 | final NotificationChannel channel = |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 859 | new NotificationChannel("id", "name", IMPORTANCE_DEFAULT); |
Geoffrey Pitsch | 76a3aa0 | 2017-07-26 15:07:34 -0400 | [diff] [blame] | 860 | mBinderService.createNotificationChannels(PKG, |
| 861 | new ParceledListSlice(Arrays.asList(channel))); |
| 862 | |
| 863 | // Recreating with a lower importance is allowed to modify the channel. |
| 864 | final NotificationChannel dupeChannel = |
| 865 | new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_LOW); |
| 866 | mBinderService.createNotificationChannels(PKG, |
| 867 | new ParceledListSlice(Arrays.asList(dupeChannel))); |
| 868 | final NotificationChannel createdChannel = |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 869 | mBinderService.getNotificationChannel(PKG, mContext.getUserId(), PKG, "id"); |
Geoffrey Pitsch | 76a3aa0 | 2017-07-26 15:07:34 -0400 | [diff] [blame] | 870 | assertEquals(NotificationManager.IMPORTANCE_LOW, createdChannel.getImportance()); |
| 871 | } |
| 872 | |
| 873 | @Test |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 874 | public void testCreateNotificationChannels_CannotDowngradeImportanceIfAlreadyUpdated() |
| 875 | throws Exception { |
| 876 | final NotificationChannel channel = |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 877 | new NotificationChannel("id", "name", IMPORTANCE_DEFAULT); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 878 | mBinderService.createNotificationChannels(PKG, |
| 879 | new ParceledListSlice(Arrays.asList(channel))); |
| 880 | |
| 881 | // The user modifies importance directly, can no longer be changed by the app. |
| 882 | final NotificationChannel updatedChannel = |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 883 | new NotificationChannel("id", "name", IMPORTANCE_HIGH); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 884 | mBinderService.updateNotificationChannelForPackage(PKG, mUid, updatedChannel); |
| 885 | |
| 886 | // Recreating with a lower importance leaves channel unchanged. |
| 887 | final NotificationChannel dupeChannel = |
| 888 | new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_LOW); |
| 889 | mBinderService.createNotificationChannels(PKG, |
| 890 | new ParceledListSlice(Arrays.asList(dupeChannel))); |
| 891 | final NotificationChannel createdChannel = |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 892 | mBinderService.getNotificationChannel(PKG, mContext.getUserId(), PKG, "id"); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 893 | assertEquals(IMPORTANCE_HIGH, createdChannel.getImportance()); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | @Test |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 897 | public void testCreateNotificationChannels_IdenticalChannelsInListIgnoresSecond() |
| 898 | throws Exception { |
| 899 | final NotificationChannel channel1 = |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 900 | new NotificationChannel("id", "name", IMPORTANCE_DEFAULT); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 901 | final NotificationChannel channel2 = |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 902 | new NotificationChannel("id", "name", IMPORTANCE_HIGH); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 903 | mBinderService.createNotificationChannels(PKG, |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 904 | new ParceledListSlice(Arrays.asList(channel1, channel2))); |
| 905 | final NotificationChannel createdChannel = |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 906 | mBinderService.getNotificationChannel(PKG, mContext.getUserId(), PKG, "id"); |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 907 | assertEquals(IMPORTANCE_DEFAULT, createdChannel.getImportance()); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 908 | } |
| 909 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 910 | @Test |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 911 | public void testBlockedNotifications_suspended() throws Exception { |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 912 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(true); |
| 913 | |
| 914 | NotificationChannel channel = new NotificationChannel("id", "name", |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 915 | IMPORTANCE_HIGH); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 916 | NotificationRecord r = generateNotificationRecord(channel); |
Beverly | 3c707b4 | 2018-09-14 09:49:07 -0400 | [diff] [blame] | 917 | |
| 918 | // isBlocked is only used for user blocking, not app suspension |
| 919 | assertFalse(mService.isBlocked(r, mUsageStats)); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 920 | } |
| 921 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 922 | @Test |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 923 | public void testBlockedNotifications_blockedChannel() throws Exception { |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 924 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false); |
| 925 | |
| 926 | NotificationChannel channel = new NotificationChannel("id", "name", |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 927 | NotificationManager.IMPORTANCE_NONE); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 928 | NotificationRecord r = generateNotificationRecord(channel); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 929 | assertTrue(mService.isBlocked(r, mUsageStats)); |
Geoffrey Pitsch | d5bcf21 | 2017-06-01 15:45:35 -0400 | [diff] [blame] | 930 | verify(mUsageStats, times(1)).registerBlocked(eq(r)); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 931 | |
| 932 | mBinderService.createNotificationChannels( |
| 933 | PKG, new ParceledListSlice(Arrays.asList(channel))); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 934 | final StatusBarNotification sbn = generateNotificationRecord(channel).getSbn(); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 935 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 936 | "testBlockedNotifications_blockedChannel", |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 937 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 938 | waitForIdle(); |
| 939 | assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length); |
| 940 | } |
| 941 | |
| 942 | @Test |
| 943 | public void testEnqueuedBlockedNotifications_appBlockedChannelForegroundService() |
| 944 | throws Exception { |
| 945 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false); |
| 946 | |
| 947 | NotificationChannel channel = new NotificationChannel("blocked", "name", |
| 948 | NotificationManager.IMPORTANCE_NONE); |
| 949 | mBinderService.createNotificationChannels( |
| 950 | PKG, new ParceledListSlice(Arrays.asList(channel))); |
| 951 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 952 | final StatusBarNotification sbn = generateNotificationRecord(channel).getSbn(); |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 953 | sbn.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 954 | mBinderService.enqueueNotificationWithTag(PKG, PKG, sbn.getTag(), |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 955 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 956 | waitForIdle(); |
| 957 | assertEquals(1, mBinderService.getActiveNotifications(sbn.getPackageName()).length); |
| 958 | assertEquals(IMPORTANCE_LOW, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 959 | mService.getNotificationRecord(sbn.getKey()).getImportance()); |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 960 | assertEquals(IMPORTANCE_LOW, mBinderService.getNotificationChannel( |
| 961 | PKG, mContext.getUserId(), PKG, channel.getId()).getImportance()); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | @Test |
| 965 | public void testEnqueuedBlockedNotifications_userBlockedChannelForegroundService() |
| 966 | throws Exception { |
| 967 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false); |
| 968 | |
| 969 | NotificationChannel channel = |
| 970 | new NotificationChannel("blockedbyuser", "name", IMPORTANCE_HIGH); |
| 971 | mBinderService.createNotificationChannels( |
| 972 | PKG, new ParceledListSlice(Arrays.asList(channel))); |
| 973 | |
| 974 | NotificationChannel update = |
| 975 | new NotificationChannel("blockedbyuser", "name", IMPORTANCE_NONE); |
| 976 | mBinderService.updateNotificationChannelForPackage(PKG, mUid, update); |
| 977 | waitForIdle(); |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 978 | assertEquals(IMPORTANCE_NONE, mBinderService.getNotificationChannel( |
| 979 | PKG, mContext.getUserId(), PKG, channel.getId()).getImportance()); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 980 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 981 | StatusBarNotification sbn = generateNotificationRecord(channel).getSbn(); |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 982 | sbn.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 983 | mBinderService.enqueueNotificationWithTag(PKG, PKG, sbn.getTag(), |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 984 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 985 | waitForIdle(); |
Dianne Hackborn | 025d4a5 | 2018-04-30 16:23:26 -0700 | [diff] [blame] | 986 | // The first time a foreground service notification is shown, we allow the channel |
| 987 | // to be updated to allow it to be seen. |
| 988 | assertEquals(1, mBinderService.getActiveNotifications(sbn.getPackageName()).length); |
| 989 | assertEquals(IMPORTANCE_LOW, |
| 990 | mService.getNotificationRecord(sbn.getKey()).getImportance()); |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 991 | assertEquals(IMPORTANCE_LOW, mBinderService.getNotificationChannel( |
| 992 | PKG, mContext.getUserId(), PKG, channel.getId()).getImportance()); |
Julia Reynolds | e4a47dd | 2019-06-07 13:40:59 -0400 | [diff] [blame] | 993 | mBinderService.cancelNotificationWithTag(PKG, PKG, "tag", sbn.getId(), sbn.getUserId()); |
Dianne Hackborn | 025d4a5 | 2018-04-30 16:23:26 -0700 | [diff] [blame] | 994 | waitForIdle(); |
| 995 | |
| 996 | update = new NotificationChannel("blockedbyuser", "name", IMPORTANCE_NONE); |
| 997 | update.setFgServiceShown(true); |
| 998 | mBinderService.updateNotificationChannelForPackage(PKG, mUid, update); |
| 999 | waitForIdle(); |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 1000 | assertEquals(IMPORTANCE_NONE, mBinderService.getNotificationChannel( |
| 1001 | PKG, mContext.getUserId(), PKG, channel.getId()).getImportance()); |
Dianne Hackborn | 025d4a5 | 2018-04-30 16:23:26 -0700 | [diff] [blame] | 1002 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1003 | sbn = generateNotificationRecord(channel).getSbn(); |
Dianne Hackborn | 025d4a5 | 2018-04-30 16:23:26 -0700 | [diff] [blame] | 1004 | sbn.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1005 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1006 | "testEnqueuedBlockedNotifications_userBlockedChannelForegroundService", |
Dianne Hackborn | 025d4a5 | 2018-04-30 16:23:26 -0700 | [diff] [blame] | 1007 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 1008 | waitForIdle(); |
| 1009 | // The second time it is shown, we keep the user's preference. |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 1010 | assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1011 | assertNull(mService.getNotificationRecord(sbn.getKey())); |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 1012 | assertEquals(IMPORTANCE_NONE, mBinderService.getNotificationChannel( |
| 1013 | PKG, mContext.getUserId(), PKG, channel.getId()).getImportance()); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 1014 | } |
| 1015 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 1016 | @Test |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 1017 | public void testBlockedNotifications_blockedChannelGroup() throws Exception { |
| 1018 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1019 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1020 | when(mPreferencesHelper.isGroupBlocked(anyString(), anyInt(), anyString())). |
| 1021 | thenReturn(true); |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 1022 | |
| 1023 | NotificationChannel channel = new NotificationChannel("id", "name", |
| 1024 | NotificationManager.IMPORTANCE_HIGH); |
| 1025 | channel.setGroup("something"); |
| 1026 | NotificationRecord r = generateNotificationRecord(channel); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1027 | assertTrue(mService.isBlocked(r, mUsageStats)); |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 1028 | verify(mUsageStats, times(1)).registerBlocked(eq(r)); |
| 1029 | } |
| 1030 | |
| 1031 | @Test |
Julia Reynolds | 4da7970 | 2017-06-01 11:06:10 -0400 | [diff] [blame] | 1032 | public void testEnqueuedBlockedNotifications_blockedApp() throws Exception { |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 1033 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false); |
| 1034 | |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 1035 | mBinderService.setNotificationsEnabledForPackage(PKG, mUid, false); |
Julia Reynolds | 4da7970 | 2017-06-01 11:06:10 -0400 | [diff] [blame] | 1036 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1037 | final StatusBarNotification sbn = generateNotificationRecord(null).getSbn(); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1038 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1039 | "testEnqueuedBlockedNotifications_blockedApp", |
Julia Reynolds | 4da7970 | 2017-06-01 11:06:10 -0400 | [diff] [blame] | 1040 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 1041 | waitForIdle(); |
| 1042 | assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 1043 | } |
| 1044 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 1045 | @Test |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 1046 | public void testEnqueuedBlockedNotifications_blockedAppForegroundService() throws Exception { |
| 1047 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false); |
| 1048 | |
| 1049 | mBinderService.setNotificationsEnabledForPackage(PKG, mUid, false); |
| 1050 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1051 | final StatusBarNotification sbn = generateNotificationRecord(null).getSbn(); |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 1052 | sbn.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1053 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1054 | "testEnqueuedBlockedNotifications_blockedAppForegroundService", |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 1055 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 1056 | waitForIdle(); |
| 1057 | assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1058 | assertNull(mService.getNotificationRecord(sbn.getKey())); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 1059 | } |
| 1060 | |
Brad Stenning | 8c991ea | 2018-07-31 13:33:01 -0700 | [diff] [blame] | 1061 | /** |
| 1062 | * Confirm the system user on automotive devices can use car categories |
| 1063 | */ |
| 1064 | @Test |
| 1065 | public void testEnqueuedRestrictedNotifications_asSystem() throws Exception { |
| 1066 | when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE, 0)) |
| 1067 | .thenReturn(true); |
| 1068 | List<String> categories = Arrays.asList(Notification.CATEGORY_CAR_EMERGENCY, |
| 1069 | Notification.CATEGORY_CAR_WARNING, |
| 1070 | Notification.CATEGORY_CAR_INFORMATION); |
| 1071 | int id = 0; |
| 1072 | for (String category: categories) { |
| 1073 | final StatusBarNotification sbn = |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1074 | generateNotificationRecord(mTestNotificationChannel, ++id, "", false).getSbn(); |
Brad Stenning | 8c991ea | 2018-07-31 13:33:01 -0700 | [diff] [blame] | 1075 | sbn.getNotification().category = category; |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1076 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1077 | "testEnqueuedRestrictedNotifications_asSystem", |
Brad Stenning | 8c991ea | 2018-07-31 13:33:01 -0700 | [diff] [blame] | 1078 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 1079 | } |
| 1080 | waitForIdle(); |
| 1081 | assertEquals(categories.size(), mBinderService.getActiveNotifications(PKG).length); |
| 1082 | } |
| 1083 | |
| 1084 | |
| 1085 | /** |
| 1086 | * Confirm restricted notification categories only apply to automotive. |
| 1087 | */ |
| 1088 | @Test |
| 1089 | public void testEnqueuedRestrictedNotifications_notAutomotive() throws Exception { |
| 1090 | mService.isSystemUid = false; |
| 1091 | when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE, 0)) |
| 1092 | .thenReturn(false); |
| 1093 | List<String> categories = Arrays.asList(Notification.CATEGORY_CAR_EMERGENCY, |
| 1094 | Notification.CATEGORY_CAR_WARNING, |
| 1095 | Notification.CATEGORY_CAR_INFORMATION); |
| 1096 | int id = 0; |
| 1097 | for (String category: categories) { |
| 1098 | final StatusBarNotification sbn = |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1099 | generateNotificationRecord(mTestNotificationChannel, ++id, "", false).getSbn(); |
Brad Stenning | 8c991ea | 2018-07-31 13:33:01 -0700 | [diff] [blame] | 1100 | sbn.getNotification().category = category; |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1101 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1102 | "testEnqueuedRestrictedNotifications_notAutomotive", |
Brad Stenning | 8c991ea | 2018-07-31 13:33:01 -0700 | [diff] [blame] | 1103 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 1104 | } |
| 1105 | waitForIdle(); |
| 1106 | assertEquals(categories.size(), mBinderService.getActiveNotifications(PKG).length); |
| 1107 | } |
| 1108 | |
| 1109 | /** |
| 1110 | * Confirm if a non-system user tries to use the car categories on a automotive device that |
| 1111 | * they will get a security exception |
| 1112 | */ |
| 1113 | @Test |
| 1114 | public void testEnqueuedRestrictedNotifications_badUser() throws Exception { |
| 1115 | mService.isSystemUid = false; |
| 1116 | when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE, 0)) |
| 1117 | .thenReturn(true); |
| 1118 | List<String> categories = Arrays.asList(Notification.CATEGORY_CAR_EMERGENCY, |
| 1119 | Notification.CATEGORY_CAR_WARNING, |
| 1120 | Notification.CATEGORY_CAR_INFORMATION); |
| 1121 | for (String category: categories) { |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1122 | final StatusBarNotification sbn = generateNotificationRecord(null).getSbn(); |
Brad Stenning | 8c991ea | 2018-07-31 13:33:01 -0700 | [diff] [blame] | 1123 | sbn.getNotification().category = category; |
| 1124 | try { |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1125 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1126 | "testEnqueuedRestrictedNotifications_badUser", |
Brad Stenning | 8c991ea | 2018-07-31 13:33:01 -0700 | [diff] [blame] | 1127 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 1128 | fail("Calls from non system apps should not allow use of restricted categories"); |
| 1129 | } catch (SecurityException e) { |
| 1130 | // pass |
| 1131 | } |
| 1132 | } |
| 1133 | waitForIdle(); |
| 1134 | assertEquals(0, mBinderService.getActiveNotifications(PKG).length); |
| 1135 | } |
| 1136 | |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 1137 | @Test |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 1138 | public void testBlockedNotifications_blockedByAssistant() throws Exception { |
| 1139 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false); |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 1140 | when(mAssistants.isSameUser(any(), anyInt())).thenReturn(true); |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 1141 | |
| 1142 | NotificationChannel channel = new NotificationChannel("id", "name", |
| 1143 | NotificationManager.IMPORTANCE_HIGH); |
| 1144 | NotificationRecord r = generateNotificationRecord(channel); |
| 1145 | mService.addEnqueuedNotification(r); |
| 1146 | |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 1147 | Bundle bundle = new Bundle(); |
Julia Reynolds | c7dcdc2 | 2019-03-25 10:26:14 -0400 | [diff] [blame] | 1148 | bundle.putInt(KEY_IMPORTANCE, IMPORTANCE_NONE); |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 1149 | Adjustment adjustment = new Adjustment( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1150 | r.getSbn().getPackageName(), r.getKey(), bundle, "", r.getUser().getIdentifier()); |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 1151 | mBinderService.applyEnqueuedAdjustmentFromAssistant(null, adjustment); |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 1152 | |
| 1153 | NotificationManagerService.PostNotificationRunnable runnable = |
| 1154 | mService.new PostNotificationRunnable(r.getKey()); |
| 1155 | runnable.run(); |
| 1156 | waitForIdle(); |
| 1157 | |
| 1158 | verify(mUsageStats, never()).registerPostedByApp(any()); |
| 1159 | } |
| 1160 | |
| 1161 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1162 | public void testEnqueueNotificationWithTag_PopulatesGetActiveNotifications() throws Exception { |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1163 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1164 | "testEnqueueNotificationWithTag_PopulatesGetActiveNotifications", 0, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1165 | generateNotificationRecord(null).getNotification(), 0); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1166 | waitForIdle(); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1167 | StatusBarNotification[] notifs = mBinderService.getActiveNotifications(PKG); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1168 | assertEquals(1, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1169 | assertEquals(1, mService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1170 | } |
| 1171 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 1172 | @Test |
Will Brockman | d3d4933 | 2020-02-10 19:43:03 -0500 | [diff] [blame] | 1173 | public void testEnqueueNotificationWithTag_WritesExpectedLogs() throws Exception { |
Will Brockman | 2b6959e | 2020-01-22 09:59:50 -0500 | [diff] [blame] | 1174 | final String tag = "testEnqueueNotificationWithTag_WritesExpectedLog"; |
| 1175 | mBinderService.enqueueNotificationWithTag(PKG, PKG, tag, 0, |
| 1176 | generateNotificationRecord(null).getNotification(), 0); |
| 1177 | waitForIdle(); |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 1178 | assertEquals(1, mNotificationRecordLogger.numCalls()); |
Will Brockman | d3d4933 | 2020-02-10 19:43:03 -0500 | [diff] [blame] | 1179 | |
Will Brockman | 2b6959e | 2020-01-22 09:59:50 -0500 | [diff] [blame] | 1180 | NotificationRecordLoggerFake.CallRecord call = mNotificationRecordLogger.get(0); |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 1181 | assertTrue(call.wasLogged); |
Will Brockman | 75c6057 | 2020-01-31 10:30:27 -0500 | [diff] [blame] | 1182 | assertEquals(NotificationRecordLogger.NotificationReportedEvent.NOTIFICATION_POSTED, |
| 1183 | call.event); |
Will Brockman | 2b6959e | 2020-01-22 09:59:50 -0500 | [diff] [blame] | 1184 | assertNotNull(call.r); |
| 1185 | assertNull(call.old); |
| 1186 | assertEquals(0, call.position); |
| 1187 | assertEquals(0, call.buzzBeepBlink); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1188 | assertEquals(PKG, call.r.getSbn().getPackageName()); |
| 1189 | assertEquals(0, call.r.getSbn().getId()); |
| 1190 | assertEquals(tag, call.r.getSbn().getTag()); |
Will Brockman | 4f0f9d2 | 2020-02-23 21:18:11 -0500 | [diff] [blame] | 1191 | assertEquals(1, call.getInstanceId()); // Fake instance IDs are assigned in order |
Will Brockman | 2b6959e | 2020-01-22 09:59:50 -0500 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | @Test |
| 1195 | public void testEnqueueNotificationWithTag_LogsOnMajorUpdates() throws Exception { |
| 1196 | final String tag = "testEnqueueNotificationWithTag_LogsOnMajorUpdates"; |
| 1197 | Notification original = new Notification.Builder(mContext, |
| 1198 | mTestNotificationChannel.getId()) |
| 1199 | .setSmallIcon(android.R.drawable.sym_def_app_icon).build(); |
| 1200 | mBinderService.enqueueNotificationWithTag(PKG, PKG, tag, 0, original, 0); |
| 1201 | Notification update = new Notification.Builder(mContext, |
| 1202 | mTestNotificationChannel.getId()) |
| 1203 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 1204 | .setCategory(Notification.CATEGORY_ALARM).build(); |
| 1205 | mBinderService.enqueueNotificationWithTag(PKG, PKG, tag, 0, update, 0); |
| 1206 | waitForIdle(); |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 1207 | assertEquals(2, mNotificationRecordLogger.numCalls()); |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 1208 | |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 1209 | assertTrue(mNotificationRecordLogger.get(0).wasLogged); |
Will Brockman | 2b6959e | 2020-01-22 09:59:50 -0500 | [diff] [blame] | 1210 | assertEquals( |
Will Brockman | 75c6057 | 2020-01-31 10:30:27 -0500 | [diff] [blame] | 1211 | NotificationRecordLogger.NotificationReportedEvent.NOTIFICATION_POSTED, |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 1212 | mNotificationRecordLogger.event(0)); |
Will Brockman | 4f0f9d2 | 2020-02-23 21:18:11 -0500 | [diff] [blame] | 1213 | assertEquals(1, mNotificationRecordLogger.get(0).getInstanceId()); |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 1214 | |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 1215 | assertTrue(mNotificationRecordLogger.get(1).wasLogged); |
Will Brockman | 2b6959e | 2020-01-22 09:59:50 -0500 | [diff] [blame] | 1216 | assertEquals( |
Will Brockman | 75c6057 | 2020-01-31 10:30:27 -0500 | [diff] [blame] | 1217 | NotificationRecordLogger.NotificationReportedEvent.NOTIFICATION_UPDATED, |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 1218 | mNotificationRecordLogger.event(1)); |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 1219 | // Instance ID doesn't change on update of an active notification |
Will Brockman | 4f0f9d2 | 2020-02-23 21:18:11 -0500 | [diff] [blame] | 1220 | assertEquals(1, mNotificationRecordLogger.get(1).getInstanceId()); |
Will Brockman | 2b6959e | 2020-01-22 09:59:50 -0500 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | @Test |
Will Brockman | d3d4933 | 2020-02-10 19:43:03 -0500 | [diff] [blame] | 1224 | public void testEnqueueNotificationWithTag_DoesNotLogOnMinorUpdate() throws Exception { |
| 1225 | final String tag = "testEnqueueNotificationWithTag_DoesNotLogOnMinorUpdate"; |
Will Brockman | 2b6959e | 2020-01-22 09:59:50 -0500 | [diff] [blame] | 1226 | mBinderService.enqueueNotificationWithTag(PKG, PKG, tag, 0, |
| 1227 | generateNotificationRecord(null).getNotification(), 0); |
| 1228 | mBinderService.enqueueNotificationWithTag(PKG, PKG, tag, 0, |
| 1229 | generateNotificationRecord(null).getNotification(), 0); |
| 1230 | waitForIdle(); |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 1231 | assertEquals(2, mNotificationRecordLogger.numCalls()); |
| 1232 | assertTrue(mNotificationRecordLogger.get(0).wasLogged); |
Will Brockman | d3d4933 | 2020-02-10 19:43:03 -0500 | [diff] [blame] | 1233 | assertEquals( |
| 1234 | NotificationRecordLogger.NotificationReportedEvent.NOTIFICATION_POSTED, |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 1235 | mNotificationRecordLogger.event(0)); |
| 1236 | assertFalse(mNotificationRecordLogger.get(1).wasLogged); |
| 1237 | assertNull(mNotificationRecordLogger.event(1)); |
Will Brockman | d3d4933 | 2020-02-10 19:43:03 -0500 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | @Test |
| 1241 | public void testEnqueueNotificationWithTag_DoesNotLogOnTitleUpdate() throws Exception { |
| 1242 | final String tag = "testEnqueueNotificationWithTag_DoesNotLogOnTitleUpdate"; |
| 1243 | mBinderService.enqueueNotificationWithTag(PKG, PKG, tag, 0, |
| 1244 | generateNotificationRecord(null).getNotification(), |
| 1245 | 0); |
| 1246 | final Notification notif = generateNotificationRecord(null).getNotification(); |
| 1247 | notif.extras.putString(Notification.EXTRA_TITLE, "Changed title"); |
| 1248 | mBinderService.enqueueNotificationWithTag(PKG, PKG, tag, 0, notif, 0); |
| 1249 | waitForIdle(); |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 1250 | assertEquals(2, mNotificationRecordLogger.numCalls()); |
Will Brockman | d3d4933 | 2020-02-10 19:43:03 -0500 | [diff] [blame] | 1251 | assertEquals( |
| 1252 | NotificationRecordLogger.NotificationReportedEvent.NOTIFICATION_POSTED, |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 1253 | mNotificationRecordLogger.event(0)); |
| 1254 | assertNull(mNotificationRecordLogger.event(1)); |
Will Brockman | 2b6959e | 2020-01-22 09:59:50 -0500 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | @Test |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 1258 | public void testEnqueueNotificationWithTag_LogsAgainAfterCancel() throws Exception { |
| 1259 | final String tag = "testEnqueueNotificationWithTag_LogsAgainAfterCancel"; |
| 1260 | Notification notification = new Notification.Builder(mContext, |
| 1261 | mTestNotificationChannel.getId()) |
| 1262 | .setSmallIcon(android.R.drawable.sym_def_app_icon).build(); |
| 1263 | mBinderService.enqueueNotificationWithTag(PKG, PKG, tag, 0, notification, 0); |
| 1264 | waitForIdle(); |
| 1265 | mBinderService.cancelNotificationWithTag(PKG, PKG, tag, 0, 0); |
| 1266 | waitForIdle(); |
| 1267 | mBinderService.enqueueNotificationWithTag(PKG, PKG, tag, 0, notification, 0); |
| 1268 | waitForIdle(); |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 1269 | assertEquals(3, mNotificationRecordLogger.numCalls()); |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 1270 | |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 1271 | assertEquals( |
Will Brockman | 75c6057 | 2020-01-31 10:30:27 -0500 | [diff] [blame] | 1272 | NotificationRecordLogger.NotificationReportedEvent.NOTIFICATION_POSTED, |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 1273 | mNotificationRecordLogger.event(0)); |
| 1274 | assertTrue(mNotificationRecordLogger.get(0).wasLogged); |
Will Brockman | 4f0f9d2 | 2020-02-23 21:18:11 -0500 | [diff] [blame] | 1275 | assertEquals(1, mNotificationRecordLogger.get(0).getInstanceId()); |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 1276 | |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 1277 | assertEquals( |
Will Brockman | 75c6057 | 2020-01-31 10:30:27 -0500 | [diff] [blame] | 1278 | NotificationRecordLogger.NotificationCancelledEvent.NOTIFICATION_CANCEL_APP_CANCEL, |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 1279 | mNotificationRecordLogger.event(1)); |
Will Brockman | 4f0f9d2 | 2020-02-23 21:18:11 -0500 | [diff] [blame] | 1280 | assertEquals(1, mNotificationRecordLogger.get(1).getInstanceId()); |
Will Brockman | 75c6057 | 2020-01-31 10:30:27 -0500 | [diff] [blame] | 1281 | |
| 1282 | assertEquals( |
| 1283 | NotificationRecordLogger.NotificationReportedEvent.NOTIFICATION_POSTED, |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 1284 | mNotificationRecordLogger.event(2)); |
| 1285 | assertTrue(mNotificationRecordLogger.get(2).wasLogged); |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 1286 | // New instance ID because notification was canceled before re-post |
Will Brockman | 4f0f9d2 | 2020-02-23 21:18:11 -0500 | [diff] [blame] | 1287 | assertEquals(2, mNotificationRecordLogger.get(2).getInstanceId()); |
Will Brockman | 75c6057 | 2020-01-31 10:30:27 -0500 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | @Test |
| 1291 | public void testCancelNonexistentNotification() throws Exception { |
| 1292 | mBinderService.cancelNotificationWithTag(PKG, PKG, |
| 1293 | "testCancelNonexistentNotification", 0, 0); |
| 1294 | waitForIdle(); |
| 1295 | // The notification record logger doesn't even get called when a nonexistent notification |
| 1296 | // is cancelled, because that happens very frequently and is not interesting. |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 1297 | assertEquals(0, mNotificationRecordLogger.numCalls()); |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1301 | public void testCancelNotificationImmediatelyAfterEnqueue() throws Exception { |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1302 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1303 | "testCancelNotificationImmediatelyAfterEnqueue", 0, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1304 | generateNotificationRecord(null).getNotification(), 0); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1305 | mBinderService.cancelNotificationWithTag(PKG, PKG, |
| 1306 | "testCancelNotificationImmediatelyAfterEnqueue", 0, 0); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1307 | waitForIdle(); |
| 1308 | StatusBarNotification[] notifs = |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1309 | mBinderService.getActiveNotifications(PKG); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1310 | assertEquals(0, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1311 | assertEquals(0, mService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1312 | } |
| 1313 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 1314 | @Test |
Evan Laird | d04af51 | 2020-01-09 11:18:09 -0500 | [diff] [blame] | 1315 | public void testPostCancelPostNotifiesListeners() throws Exception { |
| 1316 | // WHEN a notification is posted |
| 1317 | final StatusBarNotification sbn = generateNotificationRecord(null).getSbn(); |
| 1318 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", sbn.getId(), |
| 1319 | sbn.getNotification(), sbn.getUserId()); |
| 1320 | // THEN it is canceled |
| 1321 | mBinderService.cancelNotificationWithTag(PKG, PKG, "tag", sbn.getId(), sbn.getUserId()); |
| 1322 | // THEN it is posted again (before the cancel has a chance to finish) |
| 1323 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", sbn.getId(), |
| 1324 | sbn.getNotification(), sbn.getUserId()); |
| 1325 | // THEN the later enqueue isn't swallowed by the cancel. I.e., ordering is respected |
| 1326 | waitForIdle(); |
| 1327 | |
| 1328 | // The final enqueue made it to the listener instead of being canceled |
| 1329 | StatusBarNotification[] notifs = |
| 1330 | mBinderService.getActiveNotifications(PKG); |
| 1331 | assertEquals(1, notifs.length); |
| 1332 | assertEquals(1, mService.getNotificationRecordCount()); |
| 1333 | } |
| 1334 | |
| 1335 | @Test |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 1336 | public void testCancelNotificationWhilePostedAndEnqueued() throws Exception { |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1337 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1338 | "testCancelNotificationWhilePostedAndEnqueued", 0, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1339 | generateNotificationRecord(null).getNotification(), 0); |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 1340 | waitForIdle(); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1341 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1342 | "testCancelNotificationWhilePostedAndEnqueued", 0, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1343 | generateNotificationRecord(null).getNotification(), 0); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1344 | mBinderService.cancelNotificationWithTag(PKG, PKG, |
| 1345 | "testCancelNotificationWhilePostedAndEnqueued", 0, 0); |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 1346 | waitForIdle(); |
| 1347 | StatusBarNotification[] notifs = |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1348 | mBinderService.getActiveNotifications(PKG); |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 1349 | assertEquals(0, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1350 | assertEquals(0, mService.getNotificationRecordCount()); |
| 1351 | ArgumentCaptor<NotificationStats> captor = ArgumentCaptor.forClass(NotificationStats.class); |
| 1352 | verify(mListeners, times(1)).notifyRemovedLocked(any(), anyInt(), captor.capture()); |
| 1353 | assertEquals(NotificationStats.DISMISSAL_OTHER, captor.getValue().getDismissalSurface()); |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1357 | public void testCancelNotificationsFromListenerImmediatelyAfterEnqueue() throws Exception { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1358 | NotificationRecord r = generateNotificationRecord(null); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1359 | final StatusBarNotification sbn = r.getSbn(); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1360 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1361 | "testCancelNotificationsFromListenerImmediatelyAfterEnqueue", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1362 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1363 | mBinderService.cancelNotificationsFromListener(null, null); |
| 1364 | waitForIdle(); |
| 1365 | StatusBarNotification[] notifs = |
| 1366 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 1367 | assertEquals(0, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1368 | assertEquals(0, mService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1369 | } |
| 1370 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 1371 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1372 | public void testCancelAllNotificationsImmediatelyAfterEnqueue() throws Exception { |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1373 | final StatusBarNotification sbn = generateNotificationRecord(null).getSbn(); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1374 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1375 | "testCancelAllNotificationsImmediatelyAfterEnqueue", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1376 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1377 | mBinderService.cancelAllNotifications(PKG, sbn.getUserId()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1378 | waitForIdle(); |
| 1379 | StatusBarNotification[] notifs = |
| 1380 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 1381 | assertEquals(0, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1382 | assertEquals(0, mService.getNotificationRecordCount()); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | @Test |
Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 1386 | public void testCancelImmediatelyAfterEnqueueNotifiesListeners_ForegroundServiceFlag() |
| 1387 | throws Exception { |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1388 | final StatusBarNotification sbn = generateNotificationRecord(null).getSbn(); |
Evan Laird | 3ceaa9b | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 1389 | sbn.getNotification().flags = |
| 1390 | Notification.FLAG_ONGOING_EVENT | FLAG_FOREGROUND_SERVICE; |
| 1391 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
| 1392 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 1393 | mBinderService.cancelNotificationWithTag(PKG, PKG, "tag", sbn.getId(), sbn.getUserId()); |
| 1394 | waitForIdle(); |
| 1395 | verify(mListeners, times(1)).notifyPostedLocked(any(), any()); |
| 1396 | verify(mListeners, times(1)).notifyRemovedLocked(any(), anyInt(), any()); |
| 1397 | } |
| 1398 | |
| 1399 | @Test |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1400 | public void testUserInitiatedClearAll_noLeak() throws Exception { |
| 1401 | final NotificationRecord n = generateNotificationRecord( |
| 1402 | mTestNotificationChannel, 1, "group", true); |
| 1403 | |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1404 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1405 | "testUserInitiatedClearAll_noLeak", |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1406 | n.getSbn().getId(), n.getSbn().getNotification(), n.getSbn().getUserId()); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1407 | waitForIdle(); |
| 1408 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1409 | mService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(), |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1410 | n.getUserId()); |
| 1411 | waitForIdle(); |
| 1412 | StatusBarNotification[] notifs = |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1413 | mBinderService.getActiveNotifications(n.getSbn().getPackageName()); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1414 | assertEquals(0, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1415 | assertEquals(0, mService.getNotificationRecordCount()); |
| 1416 | ArgumentCaptor<NotificationStats> captor = ArgumentCaptor.forClass(NotificationStats.class); |
| 1417 | verify(mListeners, times(1)).notifyRemovedLocked(any(), anyInt(), captor.capture()); |
| 1418 | assertEquals(NotificationStats.DISMISSAL_OTHER, captor.getValue().getDismissalSurface()); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1419 | } |
| 1420 | |
| 1421 | @Test |
| 1422 | public void testCancelAllNotificationsCancelsChildren() throws Exception { |
| 1423 | final NotificationRecord parent = generateNotificationRecord( |
| 1424 | mTestNotificationChannel, 1, "group1", true); |
| 1425 | final NotificationRecord child = generateNotificationRecord( |
| 1426 | mTestNotificationChannel, 2, "group1", false); |
| 1427 | |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1428 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1429 | "testCancelAllNotificationsCancelsChildren", |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1430 | parent.getSbn().getId(), parent.getSbn().getNotification(), |
| 1431 | parent.getSbn().getUserId()); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1432 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1433 | "testCancelAllNotificationsCancelsChildren", |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1434 | child.getSbn().getId(), child.getSbn().getNotification(), |
| 1435 | child.getSbn().getUserId()); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1436 | waitForIdle(); |
| 1437 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1438 | mBinderService.cancelAllNotifications(PKG, parent.getSbn().getUserId()); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1439 | waitForIdle(); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1440 | assertEquals(0, mService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1441 | } |
| 1442 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 1443 | @Test |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 1444 | public void testCancelAllNotificationsMultipleEnqueuedDoesNotCrash() throws Exception { |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1445 | final StatusBarNotification sbn = generateNotificationRecord(null).getSbn(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 1446 | for (int i = 0; i < 10; i++) { |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1447 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1448 | "testCancelAllNotificationsMultipleEnqueuedDoesNotCrash", |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 1449 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 1450 | } |
| 1451 | mBinderService.cancelAllNotifications(PKG, sbn.getUserId()); |
| 1452 | waitForIdle(); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1453 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1454 | assertEquals(0, mService.getNotificationRecordCount()); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | @Test |
| 1458 | public void testCancelGroupSummaryMultipleEnqueuedChildrenDoesNotCrash() throws Exception { |
| 1459 | final NotificationRecord parent = generateNotificationRecord( |
| 1460 | mTestNotificationChannel, 1, "group1", true); |
| 1461 | final NotificationRecord parentAsChild = generateNotificationRecord( |
| 1462 | mTestNotificationChannel, 1, "group1", false); |
| 1463 | final NotificationRecord child = generateNotificationRecord( |
| 1464 | mTestNotificationChannel, 2, "group1", false); |
| 1465 | |
| 1466 | // fully post parent notification |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1467 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1468 | "testCancelGroupSummaryMultipleEnqueuedChildrenDoesNotCrash", |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1469 | parent.getSbn().getId(), parent.getSbn().getNotification(), |
| 1470 | parent.getSbn().getUserId()); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 1471 | waitForIdle(); |
| 1472 | |
| 1473 | // enqueue the child several times |
| 1474 | for (int i = 0; i < 10; i++) { |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1475 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1476 | "testCancelGroupSummaryMultipleEnqueuedChildrenDoesNotCrash", |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1477 | child.getSbn().getId(), child.getSbn().getNotification(), |
| 1478 | child.getSbn().getUserId()); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 1479 | } |
| 1480 | // make the parent a child, which will cancel the child notification |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1481 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1482 | "testCancelGroupSummaryMultipleEnqueuedChildrenDoesNotCrash", |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1483 | parentAsChild.getSbn().getId(), parentAsChild.getSbn().getNotification(), |
| 1484 | parentAsChild.getSbn().getUserId()); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 1485 | waitForIdle(); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1486 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1487 | assertEquals(0, mService.getNotificationRecordCount()); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | @Test |
Jay Aliomer | efe1c92 | 2019-09-19 16:42:16 -0400 | [diff] [blame] | 1491 | public void testAutobundledSummary_notificationAdded() { |
| 1492 | NotificationRecord summary = |
| 1493 | generateNotificationRecord(mTestNotificationChannel, 0, "pkg", true); |
| 1494 | summary.getNotification().flags |= Notification.FLAG_AUTOGROUP_SUMMARY; |
| 1495 | mService.addNotification(summary); |
| 1496 | mService.mSummaryByGroupKey.put("pkg", summary); |
| 1497 | mService.mAutobundledSummaries.put(0, new ArrayMap<>()); |
| 1498 | mService.mAutobundledSummaries.get(0).put("pkg", summary.getKey()); |
Mady Mellor | 6d775f8 | 2019-11-12 16:12:19 -0800 | [diff] [blame] | 1499 | mService.updateAutobundledSummaryFlags(0, "pkg", true, false); |
Jay Aliomer | efe1c92 | 2019-09-19 16:42:16 -0400 | [diff] [blame] | 1500 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1501 | assertTrue(summary.getSbn().isOngoing()); |
Jay Aliomer | efe1c92 | 2019-09-19 16:42:16 -0400 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | @Test |
| 1505 | public void testAutobundledSummary_notificationRemoved() { |
| 1506 | NotificationRecord summary = |
| 1507 | generateNotificationRecord(mTestNotificationChannel, 0, "pkg", true); |
| 1508 | summary.getNotification().flags |= Notification.FLAG_AUTOGROUP_SUMMARY; |
| 1509 | summary.getNotification().flags |= Notification.FLAG_ONGOING_EVENT; |
| 1510 | mService.addNotification(summary); |
| 1511 | mService.mAutobundledSummaries.put(0, new ArrayMap<>()); |
| 1512 | mService.mAutobundledSummaries.get(0).put("pkg", summary.getKey()); |
| 1513 | mService.mSummaryByGroupKey.put("pkg", summary); |
| 1514 | |
Mady Mellor | 6d775f8 | 2019-11-12 16:12:19 -0800 | [diff] [blame] | 1515 | mService.updateAutobundledSummaryFlags(0, "pkg", false, false); |
Jay Aliomer | efe1c92 | 2019-09-19 16:42:16 -0400 | [diff] [blame] | 1516 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1517 | assertFalse(summary.getSbn().isOngoing()); |
Jay Aliomer | efe1c92 | 2019-09-19 16:42:16 -0400 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1521 | public void testCancelAllNotifications_IgnoreForegroundService() throws Exception { |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1522 | final StatusBarNotification sbn = generateNotificationRecord(null).getSbn(); |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 1523 | sbn.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1524 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1525 | "testCancelAllNotifications_IgnoreForegroundService", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1526 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1527 | mBinderService.cancelAllNotifications(PKG, sbn.getUserId()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1528 | waitForIdle(); |
| 1529 | StatusBarNotification[] notifs = |
| 1530 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 1531 | assertEquals(1, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1532 | assertEquals(1, mService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1533 | } |
| 1534 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 1535 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1536 | public void testCancelAllNotifications_IgnoreOtherPackages() throws Exception { |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1537 | final StatusBarNotification sbn = generateNotificationRecord(null).getSbn(); |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 1538 | sbn.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1539 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1540 | "testCancelAllNotifications_IgnoreOtherPackages", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1541 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1542 | mBinderService.cancelAllNotifications("other_pkg_name", sbn.getUserId()); |
| 1543 | waitForIdle(); |
| 1544 | StatusBarNotification[] notifs = |
| 1545 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 1546 | assertEquals(1, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1547 | assertEquals(1, mService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1548 | } |
| 1549 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 1550 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1551 | public void testCancelAllNotifications_NullPkgRemovesAll() throws Exception { |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1552 | final StatusBarNotification sbn = generateNotificationRecord(null).getSbn(); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1553 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1554 | "testCancelAllNotifications_NullPkgRemovesAll", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1555 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1556 | mBinderService.cancelAllNotifications(null, sbn.getUserId()); |
| 1557 | waitForIdle(); |
| 1558 | StatusBarNotification[] notifs = |
| 1559 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 1560 | assertEquals(0, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1561 | assertEquals(0, 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_NullPkgIgnoresUserAllNotifications() throws Exception { |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1566 | final StatusBarNotification sbn = generateNotificationRecord(null).getSbn(); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1567 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1568 | "testCancelAllNotifications_NullPkgIgnoresUserAllNotifications", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1569 | sbn.getId(), sbn.getNotification(), UserHandle.USER_ALL); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1570 | // Null pkg is how we signal a user switch. |
| 1571 | mBinderService.cancelAllNotifications(null, 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 | } |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1578 | |
| 1579 | @Test |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 1580 | public void testAppInitiatedCancelAllNotifications_CancelsNoClearFlag() throws Exception { |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1581 | final StatusBarNotification sbn = generateNotificationRecord(null).getSbn(); |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 1582 | sbn.getNotification().flags |= Notification.FLAG_NO_CLEAR; |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1583 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1584 | "testAppInitiatedCancelAllNotifications_CancelsNoClearFlag", |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 1585 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 1586 | mBinderService.cancelAllNotifications(PKG, sbn.getUserId()); |
| 1587 | waitForIdle(); |
| 1588 | StatusBarNotification[] notifs = |
| 1589 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 1590 | assertEquals(0, notifs.length); |
| 1591 | } |
| 1592 | |
| 1593 | @Test |
| 1594 | public void testCancelAllNotifications_CancelsNoClearFlag() throws Exception { |
| 1595 | final NotificationRecord notif = generateNotificationRecord( |
| 1596 | mTestNotificationChannel, 1, "group", true); |
| 1597 | notif.getNotification().flags |= Notification.FLAG_NO_CLEAR; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1598 | mService.addNotification(notif); |
| 1599 | mService.cancelAllNotificationsInt(mUid, 0, PKG, null, 0, 0, true, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 1600 | notif.getUserId(), 0, null); |
| 1601 | waitForIdle(); |
| 1602 | StatusBarNotification[] notifs = |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1603 | mBinderService.getActiveNotifications(notif.getSbn().getPackageName()); |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 1604 | assertEquals(0, notifs.length); |
| 1605 | } |
| 1606 | |
| 1607 | @Test |
| 1608 | public void testUserInitiatedCancelAllOnClearAll_NoClearFlag() throws Exception { |
| 1609 | final NotificationRecord notif = generateNotificationRecord( |
| 1610 | mTestNotificationChannel, 1, "group", true); |
| 1611 | notif.getNotification().flags |= Notification.FLAG_NO_CLEAR; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1612 | mService.addNotification(notif); |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 1613 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1614 | mService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(), |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 1615 | notif.getUserId()); |
| 1616 | waitForIdle(); |
| 1617 | StatusBarNotification[] notifs = |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1618 | mBinderService.getActiveNotifications(notif.getSbn().getPackageName()); |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 1619 | assertEquals(1, notifs.length); |
| 1620 | } |
| 1621 | |
| 1622 | @Test |
| 1623 | public void testCancelAllCancelNotificationsFromListener_NoClearFlag() throws Exception { |
| 1624 | final NotificationRecord parent = generateNotificationRecord( |
| 1625 | mTestNotificationChannel, 1, "group", true); |
| 1626 | final NotificationRecord child = generateNotificationRecord( |
| 1627 | mTestNotificationChannel, 2, "group", false); |
| 1628 | final NotificationRecord child2 = generateNotificationRecord( |
| 1629 | mTestNotificationChannel, 3, "group", false); |
| 1630 | child2.getNotification().flags |= Notification.FLAG_NO_CLEAR; |
| 1631 | final NotificationRecord newGroup = generateNotificationRecord( |
| 1632 | mTestNotificationChannel, 4, "group2", false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1633 | mService.addNotification(parent); |
| 1634 | mService.addNotification(child); |
| 1635 | mService.addNotification(child2); |
| 1636 | mService.addNotification(newGroup); |
| 1637 | mService.getBinderService().cancelNotificationsFromListener(null, null); |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 1638 | waitForIdle(); |
| 1639 | StatusBarNotification[] notifs = |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1640 | mBinderService.getActiveNotifications(parent.getSbn().getPackageName()); |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 1641 | assertEquals(1, notifs.length); |
| 1642 | } |
| 1643 | |
| 1644 | @Test |
| 1645 | public void testUserInitiatedCancelAllWithGroup_NoClearFlag() throws Exception { |
| 1646 | final NotificationRecord parent = generateNotificationRecord( |
| 1647 | mTestNotificationChannel, 1, "group", true); |
| 1648 | final NotificationRecord child = generateNotificationRecord( |
| 1649 | mTestNotificationChannel, 2, "group", false); |
| 1650 | final NotificationRecord child2 = generateNotificationRecord( |
| 1651 | mTestNotificationChannel, 3, "group", false); |
| 1652 | child2.getNotification().flags |= Notification.FLAG_NO_CLEAR; |
| 1653 | final NotificationRecord newGroup = generateNotificationRecord( |
| 1654 | mTestNotificationChannel, 4, "group2", false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1655 | mService.addNotification(parent); |
| 1656 | mService.addNotification(child); |
| 1657 | mService.addNotification(child2); |
| 1658 | mService.addNotification(newGroup); |
| 1659 | mService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(), |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 1660 | parent.getUserId()); |
| 1661 | waitForIdle(); |
| 1662 | StatusBarNotification[] notifs = |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1663 | mBinderService.getActiveNotifications(parent.getSbn().getPackageName()); |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 1664 | assertEquals(1, notifs.length); |
| 1665 | } |
| 1666 | |
| 1667 | @Test |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 1668 | public void testRemoveForegroundServiceFlag_ImmediatelyAfterEnqueue() throws Exception { |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1669 | Notification n = |
| 1670 | new Notification.Builder(mContext, mTestNotificationChannel.getId()) |
| 1671 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 1672 | .build(); |
| 1673 | StatusBarNotification sbn = new StatusBarNotification("a", "a", 0, null, mUid, 0, |
| 1674 | n, new UserHandle(mUid), null, 0); |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 1675 | sbn.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1676 | mBinderService.enqueueNotificationWithTag(PKG, PKG, null, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1677 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 1678 | mInternalService.removeForegroundServiceFlagFromNotification(PKG, sbn.getId(), |
| 1679 | sbn.getUserId()); |
| 1680 | waitForIdle(); |
| 1681 | StatusBarNotification[] notifs = |
| 1682 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 1683 | assertEquals(0, notifs[0].getNotification().flags & FLAG_FOREGROUND_SERVICE); |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 1684 | } |
| 1685 | |
| 1686 | @Test |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 1687 | public void testCancelAfterSecondEnqueueDoesNotSpecifyForegroundFlag() throws Exception { |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1688 | final StatusBarNotification sbn = generateNotificationRecord(null).getSbn(); |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 1689 | sbn.getNotification().flags = |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 1690 | Notification.FLAG_ONGOING_EVENT | FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1691 | mBinderService.enqueueNotificationWithTag(PKG, PKG, sbn.getTag(), |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 1692 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 1693 | sbn.getNotification().flags = Notification.FLAG_ONGOING_EVENT; |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1694 | mBinderService.enqueueNotificationWithTag(PKG, PKG, sbn.getTag(), |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 1695 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1696 | mBinderService.cancelNotificationWithTag(PKG, PKG, sbn.getTag(), sbn.getId(), |
| 1697 | sbn.getUserId()); |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 1698 | waitForIdle(); |
| 1699 | assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1700 | assertEquals(0, mService.getNotificationRecordCount()); |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 1701 | } |
| 1702 | |
| 1703 | @Test |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1704 | public void testCancelAllCancelNotificationsFromListener_ForegroundServiceFlag() |
| 1705 | throws Exception { |
| 1706 | final NotificationRecord parent = generateNotificationRecord( |
| 1707 | mTestNotificationChannel, 1, "group", true); |
| 1708 | final NotificationRecord child = generateNotificationRecord( |
| 1709 | mTestNotificationChannel, 2, "group", false); |
| 1710 | final NotificationRecord child2 = generateNotificationRecord( |
| 1711 | mTestNotificationChannel, 3, "group", false); |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 1712 | child2.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1713 | final NotificationRecord newGroup = generateNotificationRecord( |
| 1714 | mTestNotificationChannel, 4, "group2", false); |
| 1715 | mService.addNotification(parent); |
| 1716 | mService.addNotification(child); |
| 1717 | mService.addNotification(child2); |
| 1718 | mService.addNotification(newGroup); |
| 1719 | mService.getBinderService().cancelNotificationsFromListener(null, null); |
| 1720 | waitForIdle(); |
| 1721 | StatusBarNotification[] notifs = |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1722 | mBinderService.getActiveNotifications(parent.getSbn().getPackageName()); |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1723 | assertEquals(0, notifs.length); |
| 1724 | } |
| 1725 | |
| 1726 | @Test |
| 1727 | public void testCancelAllCancelNotificationsFromListener_ForegroundServiceFlagWithParameter() |
| 1728 | throws Exception { |
| 1729 | final NotificationRecord parent = generateNotificationRecord( |
| 1730 | mTestNotificationChannel, 1, "group", true); |
| 1731 | final NotificationRecord child = generateNotificationRecord( |
| 1732 | mTestNotificationChannel, 2, "group", false); |
| 1733 | final NotificationRecord child2 = generateNotificationRecord( |
| 1734 | mTestNotificationChannel, 3, "group", false); |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 1735 | child2.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1736 | final NotificationRecord newGroup = generateNotificationRecord( |
| 1737 | mTestNotificationChannel, 4, "group2", false); |
| 1738 | mService.addNotification(parent); |
| 1739 | mService.addNotification(child); |
| 1740 | mService.addNotification(child2); |
| 1741 | mService.addNotification(newGroup); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1742 | String[] keys = {parent.getSbn().getKey(), child.getSbn().getKey(), |
| 1743 | child2.getSbn().getKey(), newGroup.getSbn().getKey()}; |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1744 | mService.getBinderService().cancelNotificationsFromListener(null, keys); |
| 1745 | waitForIdle(); |
| 1746 | StatusBarNotification[] notifs = |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1747 | mBinderService.getActiveNotifications(parent.getSbn().getPackageName()); |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1748 | assertEquals(1, notifs.length); |
| 1749 | } |
| 1750 | |
| 1751 | @Test |
| 1752 | public void testUserInitiatedCancelAllWithGroup_ForegroundServiceFlag() throws Exception { |
| 1753 | final NotificationRecord parent = generateNotificationRecord( |
| 1754 | mTestNotificationChannel, 1, "group", true); |
| 1755 | final NotificationRecord child = generateNotificationRecord( |
| 1756 | mTestNotificationChannel, 2, "group", false); |
| 1757 | final NotificationRecord child2 = generateNotificationRecord( |
| 1758 | mTestNotificationChannel, 3, "group", false); |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 1759 | child2.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1760 | final NotificationRecord newGroup = generateNotificationRecord( |
| 1761 | mTestNotificationChannel, 4, "group2", false); |
| 1762 | mService.addNotification(parent); |
| 1763 | mService.addNotification(child); |
| 1764 | mService.addNotification(child2); |
| 1765 | mService.addNotification(newGroup); |
| 1766 | mService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(), |
| 1767 | parent.getUserId()); |
| 1768 | waitForIdle(); |
| 1769 | StatusBarNotification[] notifs = |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1770 | mBinderService.getActiveNotifications(parent.getSbn().getPackageName()); |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1771 | assertEquals(0, notifs.length); |
| 1772 | } |
| 1773 | |
| 1774 | @Test |
Will Brockman | 8da8c8e | 2020-04-30 12:46:15 -0400 | [diff] [blame^] | 1775 | public void testGroupInstanceIds() throws Exception { |
| 1776 | final NotificationRecord group1 = generateNotificationRecord( |
| 1777 | mTestNotificationChannel, 1, "group1", true); |
| 1778 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "testFindGroupNotificationsLocked", |
| 1779 | group1.getSbn().getId(), group1.getSbn().getNotification(), |
| 1780 | group1.getSbn().getUserId()); |
| 1781 | waitForIdle(); |
| 1782 | |
| 1783 | // same group, child, should be returned |
| 1784 | final NotificationRecord group1Child = generateNotificationRecord( |
| 1785 | mTestNotificationChannel, 2, "group1", false); |
| 1786 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "testFindGroupNotificationsLocked", |
| 1787 | group1Child.getSbn().getId(), |
| 1788 | group1Child.getSbn().getNotification(), group1Child.getSbn().getUserId()); |
| 1789 | waitForIdle(); |
| 1790 | |
| 1791 | assertEquals(2, mNotificationRecordLogger.numCalls()); |
| 1792 | assertEquals(mNotificationRecordLogger.get(0).getInstanceId(), |
| 1793 | mNotificationRecordLogger.get(1).groupInstanceId.getId()); |
| 1794 | } |
| 1795 | |
| 1796 | @Test |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1797 | public void testFindGroupNotificationsLocked() throws Exception { |
| 1798 | // make sure the same notification can be found in both lists and returned |
| 1799 | final NotificationRecord group1 = generateNotificationRecord( |
| 1800 | mTestNotificationChannel, 1, "group1", true); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1801 | mService.addEnqueuedNotification(group1); |
| 1802 | mService.addNotification(group1); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1803 | |
| 1804 | // should not be returned |
| 1805 | final NotificationRecord group2 = generateNotificationRecord( |
| 1806 | mTestNotificationChannel, 2, "group2", true); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1807 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "testFindGroupNotificationsLocked", |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1808 | group2.getSbn().getId(), group2.getSbn().getNotification(), |
| 1809 | group2.getSbn().getUserId()); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1810 | waitForIdle(); |
| 1811 | |
| 1812 | // should not be returned |
| 1813 | final NotificationRecord nonGroup = generateNotificationRecord( |
| 1814 | mTestNotificationChannel, 3, null, false); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1815 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "testFindGroupNotificationsLocked", |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1816 | nonGroup.getSbn().getId(), nonGroup.getSbn().getNotification(), |
| 1817 | nonGroup.getSbn().getUserId()); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1818 | waitForIdle(); |
| 1819 | |
| 1820 | // same group, child, should be returned |
| 1821 | final NotificationRecord group1Child = generateNotificationRecord( |
| 1822 | mTestNotificationChannel, 4, "group1", false); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1823 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "testFindGroupNotificationsLocked", |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1824 | group1Child.getSbn().getId(), |
| 1825 | group1Child.getSbn().getNotification(), group1Child.getSbn().getUserId()); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1826 | waitForIdle(); |
| 1827 | |
| 1828 | List<NotificationRecord> inGroup1 = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1829 | mService.findGroupNotificationsLocked(PKG, group1.getGroupKey(), |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1830 | group1.getSbn().getUserId()); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1831 | assertEquals(3, inGroup1.size()); |
| 1832 | for (NotificationRecord record : inGroup1) { |
| 1833 | assertTrue(record.getGroupKey().equals(group1.getGroupKey())); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1834 | assertTrue(record.getSbn().getId() == 1 || record.getSbn().getId() == 4); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1835 | } |
| 1836 | } |
| 1837 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1838 | @Test |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1839 | public void testCancelAllNotifications_CancelsNoClearFlagOnGoing() throws Exception { |
| 1840 | final NotificationRecord notif = generateNotificationRecord( |
| 1841 | mTestNotificationChannel, 1, "group", true); |
| 1842 | notif.getNotification().flags |= Notification.FLAG_NO_CLEAR; |
| 1843 | mService.addNotification(notif); |
| 1844 | mService.cancelAllNotificationsInt(mUid, 0, PKG, null, 0, |
| 1845 | Notification.FLAG_ONGOING_EVENT, true, notif.getUserId(), 0, null); |
| 1846 | waitForIdle(); |
| 1847 | StatusBarNotification[] notifs = |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1848 | mBinderService.getActiveNotifications(notif.getSbn().getPackageName()); |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1849 | assertEquals(0, notifs.length); |
| 1850 | } |
| 1851 | |
| 1852 | @Test |
| 1853 | public void testCancelAllCancelNotificationsFromListener_NoClearFlagWithParameter() |
| 1854 | throws Exception { |
| 1855 | final NotificationRecord parent = generateNotificationRecord( |
| 1856 | mTestNotificationChannel, 1, "group", true); |
| 1857 | final NotificationRecord child = generateNotificationRecord( |
| 1858 | mTestNotificationChannel, 2, "group", false); |
| 1859 | final NotificationRecord child2 = generateNotificationRecord( |
| 1860 | mTestNotificationChannel, 3, "group", false); |
| 1861 | child2.getNotification().flags |= Notification.FLAG_NO_CLEAR; |
| 1862 | final NotificationRecord newGroup = generateNotificationRecord( |
| 1863 | mTestNotificationChannel, 4, "group2", false); |
| 1864 | mService.addNotification(parent); |
| 1865 | mService.addNotification(child); |
| 1866 | mService.addNotification(child2); |
| 1867 | mService.addNotification(newGroup); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1868 | String[] keys = {parent.getSbn().getKey(), child.getSbn().getKey(), |
| 1869 | child2.getSbn().getKey(), newGroup.getSbn().getKey()}; |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1870 | mService.getBinderService().cancelNotificationsFromListener(null, keys); |
| 1871 | waitForIdle(); |
| 1872 | StatusBarNotification[] notifs = |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1873 | mBinderService.getActiveNotifications(parent.getSbn().getPackageName()); |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1874 | assertEquals(0, notifs.length); |
| 1875 | } |
| 1876 | |
| 1877 | @Test |
| 1878 | public void testAppInitiatedCancelAllNotifications_CancelsOnGoingFlag() throws Exception { |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1879 | final StatusBarNotification sbn = generateNotificationRecord(null).getSbn(); |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1880 | sbn.getNotification().flags |= Notification.FLAG_ONGOING_EVENT; |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1881 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 1882 | "testAppInitiatedCancelAllNotifications_CancelsOnGoingFlag", |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1883 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 1884 | mBinderService.cancelAllNotifications(PKG, sbn.getUserId()); |
| 1885 | waitForIdle(); |
| 1886 | StatusBarNotification[] notifs = |
| 1887 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 1888 | assertEquals(0, notifs.length); |
| 1889 | } |
| 1890 | |
| 1891 | @Test |
| 1892 | public void testCancelAllNotifications_CancelsOnGoingFlag() throws Exception { |
| 1893 | final NotificationRecord notif = generateNotificationRecord( |
| 1894 | mTestNotificationChannel, 1, "group", true); |
| 1895 | notif.getNotification().flags |= Notification.FLAG_ONGOING_EVENT; |
| 1896 | mService.addNotification(notif); |
| 1897 | mService.cancelAllNotificationsInt(mUid, 0, PKG, null, 0, 0, true, |
| 1898 | notif.getUserId(), 0, null); |
| 1899 | waitForIdle(); |
| 1900 | StatusBarNotification[] notifs = |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1901 | mBinderService.getActiveNotifications(notif.getSbn().getPackageName()); |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1902 | assertEquals(0, notifs.length); |
| 1903 | } |
| 1904 | |
| 1905 | @Test |
| 1906 | public void testUserInitiatedCancelAllOnClearAll_OnGoingFlag() throws Exception { |
| 1907 | final NotificationRecord notif = generateNotificationRecord( |
| 1908 | mTestNotificationChannel, 1, "group", true); |
| 1909 | notif.getNotification().flags |= Notification.FLAG_ONGOING_EVENT; |
| 1910 | mService.addNotification(notif); |
| 1911 | |
| 1912 | mService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(), |
| 1913 | notif.getUserId()); |
| 1914 | waitForIdle(); |
| 1915 | StatusBarNotification[] notifs = |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1916 | mBinderService.getActiveNotifications(notif.getSbn().getPackageName()); |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1917 | assertEquals(1, notifs.length); |
| 1918 | } |
| 1919 | |
| 1920 | @Test |
| 1921 | public void testCancelAllCancelNotificationsFromListener_OnGoingFlag() throws Exception { |
| 1922 | final NotificationRecord parent = generateNotificationRecord( |
| 1923 | mTestNotificationChannel, 1, "group", true); |
| 1924 | final NotificationRecord child = generateNotificationRecord( |
| 1925 | mTestNotificationChannel, 2, "group", false); |
| 1926 | final NotificationRecord child2 = generateNotificationRecord( |
| 1927 | mTestNotificationChannel, 3, "group", false); |
| 1928 | child2.getNotification().flags |= Notification.FLAG_ONGOING_EVENT; |
| 1929 | final NotificationRecord newGroup = generateNotificationRecord( |
| 1930 | mTestNotificationChannel, 4, "group2", false); |
| 1931 | mService.addNotification(parent); |
| 1932 | mService.addNotification(child); |
| 1933 | mService.addNotification(child2); |
| 1934 | mService.addNotification(newGroup); |
| 1935 | mService.getBinderService().cancelNotificationsFromListener(null, null); |
| 1936 | waitForIdle(); |
| 1937 | StatusBarNotification[] notifs = |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1938 | mBinderService.getActiveNotifications(parent.getSbn().getPackageName()); |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1939 | assertEquals(1, notifs.length); |
| 1940 | } |
| 1941 | |
| 1942 | @Test |
| 1943 | public void testCancelAllCancelNotificationsFromListener_OnGoingFlagWithParameter() |
| 1944 | throws Exception { |
| 1945 | final NotificationRecord parent = generateNotificationRecord( |
| 1946 | mTestNotificationChannel, 1, "group", true); |
| 1947 | final NotificationRecord child = generateNotificationRecord( |
| 1948 | mTestNotificationChannel, 2, "group", false); |
| 1949 | final NotificationRecord child2 = generateNotificationRecord( |
| 1950 | mTestNotificationChannel, 3, "group", false); |
| 1951 | child2.getNotification().flags |= Notification.FLAG_ONGOING_EVENT; |
| 1952 | final NotificationRecord newGroup = generateNotificationRecord( |
| 1953 | mTestNotificationChannel, 4, "group2", false); |
| 1954 | mService.addNotification(parent); |
| 1955 | mService.addNotification(child); |
| 1956 | mService.addNotification(child2); |
| 1957 | mService.addNotification(newGroup); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1958 | String[] keys = {parent.getSbn().getKey(), child.getSbn().getKey(), |
| 1959 | child2.getSbn().getKey(), newGroup.getSbn().getKey()}; |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1960 | mService.getBinderService().cancelNotificationsFromListener(null, keys); |
| 1961 | waitForIdle(); |
| 1962 | StatusBarNotification[] notifs = |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1963 | mBinderService.getActiveNotifications(parent.getSbn().getPackageName()); |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1964 | assertEquals(0, notifs.length); |
| 1965 | } |
| 1966 | |
| 1967 | @Test |
| 1968 | public void testUserInitiatedCancelAllWithGroup_OnGoingFlag() throws Exception { |
| 1969 | final NotificationRecord parent = generateNotificationRecord( |
| 1970 | mTestNotificationChannel, 1, "group", true); |
| 1971 | final NotificationRecord child = generateNotificationRecord( |
| 1972 | mTestNotificationChannel, 2, "group", false); |
| 1973 | final NotificationRecord child2 = generateNotificationRecord( |
| 1974 | mTestNotificationChannel, 3, "group", false); |
| 1975 | child2.getNotification().flags |= Notification.FLAG_ONGOING_EVENT; |
| 1976 | final NotificationRecord newGroup = generateNotificationRecord( |
| 1977 | mTestNotificationChannel, 4, "group2", false); |
| 1978 | mService.addNotification(parent); |
| 1979 | mService.addNotification(child); |
| 1980 | mService.addNotification(child2); |
| 1981 | mService.addNotification(newGroup); |
| 1982 | mService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(), |
| 1983 | parent.getUserId()); |
| 1984 | waitForIdle(); |
| 1985 | StatusBarNotification[] notifs = |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1986 | mBinderService.getActiveNotifications(parent.getSbn().getPackageName()); |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1987 | assertEquals(1, notifs.length); |
| 1988 | } |
| 1989 | |
| 1990 | @Test |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1991 | public void testTvExtenderChannelOverride_onTv() throws Exception { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1992 | mService.setIsTelevision(true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1993 | mService.setPreferencesHelper(mPreferencesHelper); |
| 1994 | when(mPreferencesHelper.getNotificationChannel( |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1995 | anyString(), anyInt(), eq("foo"), anyBoolean())).thenReturn( |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 1996 | new NotificationChannel("foo", "foo", IMPORTANCE_HIGH)); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1997 | |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 1998 | Notification.TvExtender tv = new Notification.TvExtender().setChannelId("foo"); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 1999 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "testTvExtenderChannelOverride_onTv", 0, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 2000 | generateNotificationRecord(null, tv).getNotification(), 0); |
Julia Reynolds | 12ba4cf | 2020-01-10 16:01:38 -0500 | [diff] [blame] | 2001 | verify(mPreferencesHelper, times(1)).getConversationNotificationChannel( |
| 2002 | anyString(), anyInt(), eq("foo"), eq(null), anyBoolean(), anyBoolean()); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 2003 | } |
| 2004 | |
| 2005 | @Test |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 2006 | public void testTvExtenderChannelOverride_notOnTv() throws Exception { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2007 | mService.setIsTelevision(false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2008 | mService.setPreferencesHelper(mPreferencesHelper); |
| 2009 | when(mPreferencesHelper.getNotificationChannel( |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 2010 | anyString(), anyInt(), anyString(), anyBoolean())).thenReturn( |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 2011 | mTestNotificationChannel); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 2012 | |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 2013 | Notification.TvExtender tv = new Notification.TvExtender().setChannelId("foo"); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 2014 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "testTvExtenderChannelOverride_notOnTv", |
| 2015 | 0, generateNotificationRecord(null, tv).getNotification(), 0); |
Julia Reynolds | 12ba4cf | 2020-01-10 16:01:38 -0500 | [diff] [blame] | 2016 | verify(mPreferencesHelper, times(1)).getConversationNotificationChannel( |
Julia Reynolds | 0f76734 | 2019-12-18 09:11:55 -0500 | [diff] [blame] | 2017 | anyString(), anyInt(), eq(mTestNotificationChannel.getId()), eq(null), |
Julia Reynolds | 12ba4cf | 2020-01-10 16:01:38 -0500 | [diff] [blame] | 2018 | anyBoolean(), anyBoolean()); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 2019 | } |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2020 | |
| 2021 | @Test |
Julia Reynolds | fc9767b | 2018-01-22 17:45:16 -0500 | [diff] [blame] | 2022 | public void testUpdateAppNotifyCreatorBlock() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2023 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | fc9767b | 2018-01-22 17:45:16 -0500 | [diff] [blame] | 2024 | |
Julia Reynolds | c4c6e9f | 2019-06-03 12:48:49 -0400 | [diff] [blame] | 2025 | mBinderService.setNotificationsEnabledForPackage(PKG, 0, true); |
Julia Reynolds | fc9767b | 2018-01-22 17:45:16 -0500 | [diff] [blame] | 2026 | ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class); |
| 2027 | verify(mContext, times(1)).sendBroadcastAsUser(captor.capture(), any(), eq(null)); |
| 2028 | |
| 2029 | assertEquals(NotificationManager.ACTION_APP_BLOCK_STATE_CHANGED, |
| 2030 | captor.getValue().getAction()); |
| 2031 | assertEquals(PKG, captor.getValue().getPackage()); |
Julia Reynolds | c4c6e9f | 2019-06-03 12:48:49 -0400 | [diff] [blame] | 2032 | assertFalse(captor.getValue().getBooleanExtra(EXTRA_BLOCKED_STATE, true)); |
| 2033 | } |
| 2034 | |
| 2035 | @Test |
| 2036 | public void testUpdateAppNotifyCreatorBlock_notIfMatchesExistingSetting() throws Exception { |
| 2037 | mService.setPreferencesHelper(mPreferencesHelper); |
| 2038 | |
| 2039 | mBinderService.setNotificationsEnabledForPackage(PKG, 0, false); |
| 2040 | verify(mContext, never()).sendBroadcastAsUser(any(), any(), eq(null)); |
Julia Reynolds | fc9767b | 2018-01-22 17:45:16 -0500 | [diff] [blame] | 2041 | } |
| 2042 | |
| 2043 | @Test |
| 2044 | public void testUpdateAppNotifyCreatorUnblock() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2045 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | fc9767b | 2018-01-22 17:45:16 -0500 | [diff] [blame] | 2046 | |
| 2047 | mBinderService.setNotificationsEnabledForPackage(PKG, 0, true); |
| 2048 | ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class); |
| 2049 | verify(mContext, times(1)).sendBroadcastAsUser(captor.capture(), any(), eq(null)); |
| 2050 | |
| 2051 | assertEquals(NotificationManager.ACTION_APP_BLOCK_STATE_CHANGED, |
| 2052 | captor.getValue().getAction()); |
| 2053 | assertEquals(PKG, captor.getValue().getPackage()); |
| 2054 | assertFalse(captor.getValue().getBooleanExtra(EXTRA_BLOCKED_STATE, true)); |
| 2055 | } |
| 2056 | |
| 2057 | @Test |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2058 | public void testUpdateChannelNotifyCreatorBlock() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2059 | mService.setPreferencesHelper(mPreferencesHelper); |
| 2060 | when(mPreferencesHelper.getNotificationChannel(eq(PKG), anyInt(), |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2061 | eq(mTestNotificationChannel.getId()), anyBoolean())) |
| 2062 | .thenReturn(mTestNotificationChannel); |
| 2063 | |
| 2064 | NotificationChannel updatedChannel = |
| 2065 | new NotificationChannel(mTestNotificationChannel.getId(), |
| 2066 | mTestNotificationChannel.getName(), IMPORTANCE_NONE); |
| 2067 | |
| 2068 | mBinderService.updateNotificationChannelForPackage(PKG, 0, updatedChannel); |
| 2069 | ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class); |
| 2070 | verify(mContext, times(1)).sendBroadcastAsUser(captor.capture(), any(), eq(null)); |
| 2071 | |
| 2072 | assertEquals(NotificationManager.ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED, |
| 2073 | captor.getValue().getAction()); |
| 2074 | assertEquals(PKG, captor.getValue().getPackage()); |
| 2075 | assertEquals(mTestNotificationChannel.getId(), captor.getValue().getStringExtra( |
Julia Reynolds | 44ff7c9 | 2018-02-05 10:02:30 -0500 | [diff] [blame] | 2076 | NotificationManager.EXTRA_NOTIFICATION_CHANNEL_ID)); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2077 | assertTrue(captor.getValue().getBooleanExtra(EXTRA_BLOCKED_STATE, false)); |
| 2078 | } |
| 2079 | |
| 2080 | @Test |
| 2081 | public void testUpdateChannelNotifyCreatorUnblock() throws Exception { |
| 2082 | NotificationChannel existingChannel = |
| 2083 | new NotificationChannel(mTestNotificationChannel.getId(), |
| 2084 | mTestNotificationChannel.getName(), IMPORTANCE_NONE); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2085 | mService.setPreferencesHelper(mPreferencesHelper); |
| 2086 | when(mPreferencesHelper.getNotificationChannel(eq(PKG), anyInt(), |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2087 | eq(mTestNotificationChannel.getId()), anyBoolean())) |
| 2088 | .thenReturn(existingChannel); |
| 2089 | |
| 2090 | mBinderService.updateNotificationChannelForPackage(PKG, 0, mTestNotificationChannel); |
| 2091 | ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class); |
| 2092 | verify(mContext, times(1)).sendBroadcastAsUser(captor.capture(), any(), eq(null)); |
| 2093 | |
| 2094 | assertEquals(NotificationManager.ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED, |
| 2095 | captor.getValue().getAction()); |
| 2096 | assertEquals(PKG, captor.getValue().getPackage()); |
| 2097 | assertEquals(mTestNotificationChannel.getId(), captor.getValue().getStringExtra( |
Julia Reynolds | 44ff7c9 | 2018-02-05 10:02:30 -0500 | [diff] [blame] | 2098 | NotificationManager.EXTRA_NOTIFICATION_CHANNEL_ID)); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2099 | assertFalse(captor.getValue().getBooleanExtra(EXTRA_BLOCKED_STATE, false)); |
| 2100 | } |
| 2101 | |
| 2102 | @Test |
| 2103 | public void testUpdateChannelNoNotifyCreatorOtherChanges() throws Exception { |
| 2104 | NotificationChannel existingChannel = |
| 2105 | new NotificationChannel(mTestNotificationChannel.getId(), |
| 2106 | mTestNotificationChannel.getName(), IMPORTANCE_MAX); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2107 | mService.setPreferencesHelper(mPreferencesHelper); |
| 2108 | when(mPreferencesHelper.getNotificationChannel(eq(PKG), anyInt(), |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2109 | eq(mTestNotificationChannel.getId()), anyBoolean())) |
| 2110 | .thenReturn(existingChannel); |
| 2111 | |
| 2112 | mBinderService.updateNotificationChannelForPackage(PKG, 0, mTestNotificationChannel); |
| 2113 | verify(mContext, never()).sendBroadcastAsUser(any(), any(), eq(null)); |
| 2114 | } |
| 2115 | |
| 2116 | @Test |
| 2117 | public void testUpdateGroupNotifyCreatorBlock() throws Exception { |
| 2118 | NotificationChannelGroup existing = new NotificationChannelGroup("id", "name"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2119 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 2120 | when(mPreferencesHelper.getNotificationChannelGroup(eq(existing.getId()), |
| 2121 | eq(PKG), anyInt())) |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2122 | .thenReturn(existing); |
| 2123 | |
| 2124 | NotificationChannelGroup updated = new NotificationChannelGroup("id", "name"); |
| 2125 | updated.setBlocked(true); |
| 2126 | |
| 2127 | mBinderService.updateNotificationChannelGroupForPackage(PKG, 0, updated); |
| 2128 | ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class); |
| 2129 | verify(mContext, times(1)).sendBroadcastAsUser(captor.capture(), any(), eq(null)); |
| 2130 | |
| 2131 | assertEquals(NotificationManager.ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED, |
| 2132 | captor.getValue().getAction()); |
| 2133 | assertEquals(PKG, captor.getValue().getPackage()); |
| 2134 | assertEquals(existing.getId(), captor.getValue().getStringExtra( |
Julia Reynolds | 44ff7c9 | 2018-02-05 10:02:30 -0500 | [diff] [blame] | 2135 | NotificationManager.EXTRA_NOTIFICATION_CHANNEL_GROUP_ID)); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2136 | assertTrue(captor.getValue().getBooleanExtra(EXTRA_BLOCKED_STATE, false)); |
| 2137 | } |
| 2138 | |
| 2139 | @Test |
| 2140 | public void testUpdateGroupNotifyCreatorUnblock() throws Exception { |
| 2141 | NotificationChannelGroup existing = new NotificationChannelGroup("id", "name"); |
| 2142 | existing.setBlocked(true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2143 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 2144 | when(mPreferencesHelper.getNotificationChannelGroup(eq(existing.getId()), |
| 2145 | eq(PKG), anyInt())) |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2146 | .thenReturn(existing); |
| 2147 | |
| 2148 | mBinderService.updateNotificationChannelGroupForPackage( |
| 2149 | PKG, 0, new NotificationChannelGroup("id", "name")); |
| 2150 | ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class); |
| 2151 | verify(mContext, times(1)).sendBroadcastAsUser(captor.capture(), any(), eq(null)); |
| 2152 | |
| 2153 | assertEquals(NotificationManager.ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED, |
| 2154 | captor.getValue().getAction()); |
| 2155 | assertEquals(PKG, captor.getValue().getPackage()); |
| 2156 | assertEquals(existing.getId(), captor.getValue().getStringExtra( |
Julia Reynolds | 44ff7c9 | 2018-02-05 10:02:30 -0500 | [diff] [blame] | 2157 | NotificationManager.EXTRA_NOTIFICATION_CHANNEL_GROUP_ID)); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2158 | assertFalse(captor.getValue().getBooleanExtra(EXTRA_BLOCKED_STATE, false)); |
| 2159 | } |
| 2160 | |
| 2161 | @Test |
| 2162 | public void testUpdateGroupNoNotifyCreatorOtherChanges() throws Exception { |
| 2163 | NotificationChannelGroup existing = new NotificationChannelGroup("id", "name"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2164 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 2165 | when(mPreferencesHelper.getNotificationChannelGroup( |
| 2166 | eq(existing.getId()), eq(PKG), anyInt())) |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2167 | .thenReturn(existing); |
| 2168 | |
| 2169 | mBinderService.updateNotificationChannelGroupForPackage( |
| 2170 | PKG, 0, new NotificationChannelGroup("id", "new name")); |
| 2171 | verify(mContext, never()).sendBroadcastAsUser(any(), any(), eq(null)); |
| 2172 | } |
| 2173 | |
| 2174 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2175 | public void testCreateChannelNotifyListener() throws Exception { |
| 2176 | List<String> associations = new ArrayList<>(); |
| 2177 | associations.add("a"); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 2178 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2179 | mService.setPreferencesHelper(mPreferencesHelper); |
| 2180 | when(mPreferencesHelper.getNotificationChannel(eq(PKG), anyInt(), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2181 | eq(mTestNotificationChannel.getId()), anyBoolean())) |
| 2182 | .thenReturn(mTestNotificationChannel); |
| 2183 | NotificationChannel channel2 = new NotificationChannel("a", "b", IMPORTANCE_LOW); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2184 | when(mPreferencesHelper.getNotificationChannel(eq(PKG), anyInt(), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2185 | eq(channel2.getId()), anyBoolean())) |
| 2186 | .thenReturn(channel2); |
Julia Reynolds | dafd3a4 | 2019-05-24 13:33:28 -0400 | [diff] [blame] | 2187 | when(mPreferencesHelper.createNotificationChannel(eq(PKG), anyInt(), |
| 2188 | eq(channel2), anyBoolean(), anyBoolean())) |
| 2189 | .thenReturn(true); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2190 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2191 | reset(mListeners); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2192 | mBinderService.createNotificationChannels(PKG, |
| 2193 | new ParceledListSlice(Arrays.asList(mTestNotificationChannel, channel2))); |
Julia Reynolds | dafd3a4 | 2019-05-24 13:33:28 -0400 | [diff] [blame] | 2194 | verify(mListeners, never()).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2195 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2196 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED)); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2197 | verify(mListeners, times(1)).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2198 | eq(Process.myUserHandle()), eq(channel2), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2199 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED)); |
| 2200 | } |
| 2201 | |
| 2202 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2203 | public void testCreateChannelGroupNotifyListener() throws Exception { |
| 2204 | List<String> associations = new ArrayList<>(); |
| 2205 | associations.add("a"); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 2206 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2207 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2208 | NotificationChannelGroup group1 = new NotificationChannelGroup("a", "b"); |
| 2209 | NotificationChannelGroup group2 = new NotificationChannelGroup("n", "m"); |
| 2210 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2211 | reset(mListeners); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2212 | mBinderService.createNotificationChannelGroups(PKG, |
| 2213 | new ParceledListSlice(Arrays.asList(group1, group2))); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2214 | verify(mListeners, times(1)).notifyNotificationChannelGroupChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2215 | eq(Process.myUserHandle()), eq(group1), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2216 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED)); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2217 | verify(mListeners, times(1)).notifyNotificationChannelGroupChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2218 | eq(Process.myUserHandle()), eq(group2), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2219 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED)); |
| 2220 | } |
| 2221 | |
| 2222 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2223 | public void testUpdateChannelNotifyListener() throws Exception { |
| 2224 | List<String> associations = new ArrayList<>(); |
| 2225 | associations.add("a"); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 2226 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2227 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2228 | mTestNotificationChannel.setLightColor(Color.CYAN); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2229 | when(mPreferencesHelper.getNotificationChannel(eq(PKG), anyInt(), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2230 | eq(mTestNotificationChannel.getId()), anyBoolean())) |
| 2231 | .thenReturn(mTestNotificationChannel); |
| 2232 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2233 | reset(mListeners); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2234 | mBinderService.updateNotificationChannelForPackage(PKG, 0, mTestNotificationChannel); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2235 | verify(mListeners, times(1)).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2236 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2237 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED)); |
| 2238 | } |
| 2239 | |
| 2240 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2241 | public void testDeleteChannelNotifyListener() throws Exception { |
| 2242 | List<String> associations = new ArrayList<>(); |
| 2243 | associations.add("a"); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 2244 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2245 | mService.setPreferencesHelper(mPreferencesHelper); |
| 2246 | when(mPreferencesHelper.getNotificationChannel(eq(PKG), anyInt(), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2247 | eq(mTestNotificationChannel.getId()), anyBoolean())) |
| 2248 | .thenReturn(mTestNotificationChannel); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2249 | reset(mListeners); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2250 | mBinderService.deleteNotificationChannel(PKG, mTestNotificationChannel.getId()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2251 | verify(mListeners, times(1)).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2252 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2253 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_DELETED)); |
| 2254 | } |
| 2255 | |
| 2256 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2257 | public void testDeleteChannelGroupNotifyListener() throws Exception { |
| 2258 | List<String> associations = new ArrayList<>(); |
| 2259 | associations.add("a"); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 2260 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2261 | NotificationChannelGroup ncg = new NotificationChannelGroup("a", "b/c"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2262 | mService.setPreferencesHelper(mPreferencesHelper); |
| 2263 | when(mPreferencesHelper.getNotificationChannelGroup(eq(ncg.getId()), eq(PKG), anyInt())) |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2264 | .thenReturn(ncg); |
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.deleteNotificationChannelGroup(PKG, ncg.getId()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2267 | verify(mListeners, times(1)).notifyNotificationChannelGroupChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2268 | eq(Process.myUserHandle()), eq(ncg), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2269 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_DELETED)); |
| 2270 | } |
| 2271 | |
| 2272 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2273 | public void testUpdateNotificationChannelFromPrivilegedListener_success() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2274 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2275 | List<String> associations = new ArrayList<>(); |
| 2276 | associations.add("a"); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 2277 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2278 | when(mPreferencesHelper.getNotificationChannel(eq(PKG), anyInt(), |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2279 | eq(mTestNotificationChannel.getId()), anyBoolean())) |
| 2280 | .thenReturn(mTestNotificationChannel); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2281 | |
| 2282 | mBinderService.updateNotificationChannelFromPrivilegedListener( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2283 | null, PKG, Process.myUserHandle(), mTestNotificationChannel); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2284 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2285 | verify(mPreferencesHelper, times(1)).updateNotificationChannel( |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 2286 | anyString(), anyInt(), any(), anyBoolean()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2287 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2288 | verify(mListeners, never()).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2289 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2290 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED)); |
| 2291 | } |
| 2292 | |
| 2293 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2294 | public void testUpdateNotificationChannelFromPrivilegedListener_noAccess() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2295 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2296 | List<String> associations = new ArrayList<>(); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 2297 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2298 | |
| 2299 | try { |
| 2300 | mBinderService.updateNotificationChannelFromPrivilegedListener( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2301 | null, PKG, Process.myUserHandle(), mTestNotificationChannel); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2302 | fail("listeners that don't have a companion device shouldn't be able to call this"); |
| 2303 | } catch (SecurityException e) { |
| 2304 | // pass |
| 2305 | } |
| 2306 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2307 | verify(mPreferencesHelper, never()).updateNotificationChannel( |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 2308 | anyString(), anyInt(), any(), anyBoolean()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2309 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2310 | verify(mListeners, never()).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2311 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
| 2312 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED)); |
| 2313 | } |
| 2314 | |
| 2315 | @Test |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2316 | public void testUpdateNotificationChannelFromPrivilegedListener_badUser() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2317 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2318 | List<String> associations = new ArrayList<>(); |
| 2319 | associations.add("a"); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 2320 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2321 | mListener = mock(ManagedServices.ManagedServiceInfo.class); |
Julia Reynolds | 4da7970 | 2017-06-01 11:06:10 -0400 | [diff] [blame] | 2322 | mListener.component = new ComponentName(PKG, PKG); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2323 | when(mListener.enabledAndUserMatches(anyInt())).thenReturn(false); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2324 | when(mListeners.checkServiceTokenLocked(any())).thenReturn(mListener); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2325 | |
| 2326 | try { |
| 2327 | mBinderService.updateNotificationChannelFromPrivilegedListener( |
| 2328 | null, PKG, UserHandle.ALL, mTestNotificationChannel); |
| 2329 | fail("incorrectly allowed a change to a user listener cannot see"); |
| 2330 | } catch (SecurityException e) { |
| 2331 | // pass |
| 2332 | } |
| 2333 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2334 | verify(mPreferencesHelper, never()).updateNotificationChannel( |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 2335 | anyString(), anyInt(), any(), anyBoolean()); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2336 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2337 | verify(mListeners, never()).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2338 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2339 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED)); |
| 2340 | } |
| 2341 | |
| 2342 | @Test |
Julia Reynolds | 48a6ed9 | 2018-10-22 12:52:03 -0400 | [diff] [blame] | 2343 | public void testGetNotificationChannelFromPrivilegedListener_cdm_success() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2344 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2345 | List<String> associations = new ArrayList<>(); |
| 2346 | associations.add("a"); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 2347 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2348 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2349 | mBinderService.getNotificationChannelsFromPrivilegedListener( |
| 2350 | null, PKG, Process.myUserHandle()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2351 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2352 | verify(mPreferencesHelper, times(1)).getNotificationChannels( |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2353 | anyString(), anyInt(), anyBoolean()); |
| 2354 | } |
| 2355 | |
| 2356 | @Test |
Julia Reynolds | 48a6ed9 | 2018-10-22 12:52:03 -0400 | [diff] [blame] | 2357 | public void testGetNotificationChannelFromPrivilegedListener_cdm_noAccess() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2358 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2359 | List<String> associations = new ArrayList<>(); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 2360 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2361 | |
| 2362 | try { |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2363 | mBinderService.getNotificationChannelsFromPrivilegedListener( |
| 2364 | null, PKG, Process.myUserHandle()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2365 | fail("listeners that don't have a companion device shouldn't be able to call this"); |
| 2366 | } catch (SecurityException e) { |
| 2367 | // pass |
| 2368 | } |
| 2369 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2370 | verify(mPreferencesHelper, never()).getNotificationChannels( |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2371 | anyString(), anyInt(), anyBoolean()); |
| 2372 | } |
| 2373 | |
| 2374 | @Test |
Julia Reynolds | 48a6ed9 | 2018-10-22 12:52:03 -0400 | [diff] [blame] | 2375 | public void testGetNotificationChannelFromPrivilegedListener_assistant_success() |
| 2376 | throws Exception { |
| 2377 | mService.setPreferencesHelper(mPreferencesHelper); |
| 2378 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(new ArrayList<>()); |
| 2379 | when(mAssistants.isServiceTokenValidLocked(any())).thenReturn(true); |
| 2380 | |
| 2381 | mBinderService.getNotificationChannelsFromPrivilegedListener( |
| 2382 | null, PKG, Process.myUserHandle()); |
| 2383 | |
| 2384 | verify(mPreferencesHelper, times(1)).getNotificationChannels( |
| 2385 | anyString(), anyInt(), anyBoolean()); |
| 2386 | } |
| 2387 | |
| 2388 | @Test |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 2389 | public void testGetNotificationChannelFromPrivilegedListener_assistant_noAccess() |
| 2390 | throws Exception { |
Julia Reynolds | 48a6ed9 | 2018-10-22 12:52:03 -0400 | [diff] [blame] | 2391 | mService.setPreferencesHelper(mPreferencesHelper); |
| 2392 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(new ArrayList<>()); |
| 2393 | when(mAssistants.isServiceTokenValidLocked(any())).thenReturn(false); |
| 2394 | |
| 2395 | try { |
| 2396 | mBinderService.getNotificationChannelsFromPrivilegedListener( |
| 2397 | null, PKG, Process.myUserHandle()); |
| 2398 | fail("listeners that don't have a companion device shouldn't be able to call this"); |
| 2399 | } catch (SecurityException e) { |
| 2400 | // pass |
| 2401 | } |
| 2402 | |
| 2403 | verify(mPreferencesHelper, never()).getNotificationChannels( |
| 2404 | anyString(), anyInt(), anyBoolean()); |
| 2405 | } |
| 2406 | |
| 2407 | @Test |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2408 | public void testGetNotificationChannelFromPrivilegedListener_badUser() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2409 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2410 | List<String> associations = new ArrayList<>(); |
| 2411 | associations.add("a"); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 2412 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2413 | mListener = mock(ManagedServices.ManagedServiceInfo.class); |
| 2414 | when(mListener.enabledAndUserMatches(anyInt())).thenReturn(false); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2415 | when(mListeners.checkServiceTokenLocked(any())).thenReturn(mListener); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2416 | |
| 2417 | try { |
| 2418 | mBinderService.getNotificationChannelsFromPrivilegedListener( |
| 2419 | null, PKG, Process.myUserHandle()); |
| 2420 | fail("listener getting channels from a user they cannot see"); |
| 2421 | } catch (SecurityException e) { |
| 2422 | // pass |
| 2423 | } |
| 2424 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2425 | verify(mPreferencesHelper, never()).getNotificationChannels( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2426 | anyString(), anyInt(), anyBoolean()); |
| 2427 | } |
| 2428 | |
| 2429 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2430 | public void testGetNotificationChannelGroupsFromPrivilegedListener_success() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2431 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2432 | List<String> associations = new ArrayList<>(); |
| 2433 | associations.add("a"); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 2434 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2435 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2436 | mBinderService.getNotificationChannelGroupsFromPrivilegedListener( |
| 2437 | null, PKG, Process.myUserHandle()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2438 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2439 | verify(mPreferencesHelper, times(1)).getNotificationChannelGroups(anyString(), anyInt()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2440 | } |
| 2441 | |
| 2442 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2443 | public void testGetNotificationChannelGroupsFromPrivilegedListener_noAccess() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2444 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2445 | List<String> associations = new ArrayList<>(); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 2446 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2447 | |
| 2448 | try { |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2449 | mBinderService.getNotificationChannelGroupsFromPrivilegedListener( |
| 2450 | null, PKG, Process.myUserHandle()); |
| 2451 | fail("listeners that don't have a companion device shouldn't be able to call this"); |
| 2452 | } catch (SecurityException e) { |
| 2453 | // pass |
| 2454 | } |
| 2455 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2456 | verify(mPreferencesHelper, never()).getNotificationChannelGroups(anyString(), anyInt()); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2457 | } |
| 2458 | |
| 2459 | @Test |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2460 | public void testGetNotificationChannelGroupsFromPrivilegedListener_badUser() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2461 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2462 | List<String> associations = new ArrayList<>(); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 2463 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2464 | mListener = mock(ManagedServices.ManagedServiceInfo.class); |
| 2465 | when(mListener.enabledAndUserMatches(anyInt())).thenReturn(false); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2466 | when(mListeners.checkServiceTokenLocked(any())).thenReturn(mListener); |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 2467 | try { |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2468 | mBinderService.getNotificationChannelGroupsFromPrivilegedListener( |
| 2469 | null, PKG, Process.myUserHandle()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2470 | fail("listeners that don't have a companion device shouldn't be able to call this"); |
| 2471 | } catch (SecurityException e) { |
| 2472 | // pass |
| 2473 | } |
| 2474 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2475 | verify(mPreferencesHelper, never()).getNotificationChannelGroups(anyString(), anyInt()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2476 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 2477 | |
| 2478 | @Test |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 2479 | public void testHasCompanionDevice_failure() throws Exception { |
| 2480 | when(mCompanionMgr.getAssociations(anyString(), anyInt())).thenThrow( |
| 2481 | new IllegalArgumentException()); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2482 | mService.hasCompanionDevice(mListener); |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 2483 | } |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 2484 | |
| 2485 | @Test |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 2486 | public void testHasCompanionDevice_noService() { |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 2487 | mService = new TestableNotificationManagerService(mContext, mNotificationRecordLogger, |
| 2488 | mNotificationInstanceIdSequence); |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 2489 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2490 | assertFalse(mService.hasCompanionDevice(mListener)); |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 2491 | } |
| 2492 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2493 | @Test |
Jay Aliomer | 24642da | 2019-07-30 09:57:41 -0400 | [diff] [blame] | 2494 | public void testSnoozeRunnable_reSnoozeASingleSnoozedNotification() throws Exception { |
| 2495 | final NotificationRecord notification = generateNotificationRecord( |
| 2496 | mTestNotificationChannel, 1, null, true); |
| 2497 | mService.addNotification(notification); |
| 2498 | when(mSnoozeHelper.getNotification(any())).thenReturn(notification); |
| 2499 | |
| 2500 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
| 2501 | mService.new SnoozeNotificationRunnable( |
| 2502 | notification.getKey(), 100, null); |
| 2503 | snoozeNotificationRunnable.run(); |
| 2504 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable2 = |
| 2505 | mService.new SnoozeNotificationRunnable( |
| 2506 | notification.getKey(), 100, null); |
| 2507 | snoozeNotificationRunnable.run(); |
| 2508 | |
| 2509 | // snooze twice |
| 2510 | verify(mSnoozeHelper, times(2)).snooze(any(NotificationRecord.class), anyLong()); |
| 2511 | } |
| 2512 | |
| 2513 | @Test |
| 2514 | public void testSnoozeRunnable_reSnoozeASnoozedNotificationWithGroupKey() throws Exception { |
| 2515 | final NotificationRecord notification = generateNotificationRecord( |
| 2516 | mTestNotificationChannel, 1, "group", true); |
| 2517 | mService.addNotification(notification); |
| 2518 | when(mSnoozeHelper.getNotification(any())).thenReturn(notification); |
| 2519 | |
| 2520 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
| 2521 | mService.new SnoozeNotificationRunnable( |
| 2522 | notification.getKey(), 100, null); |
| 2523 | snoozeNotificationRunnable.run(); |
| 2524 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable2 = |
| 2525 | mService.new SnoozeNotificationRunnable( |
| 2526 | notification.getKey(), 100, null); |
| 2527 | snoozeNotificationRunnable.run(); |
| 2528 | |
| 2529 | // snooze twice |
| 2530 | verify(mSnoozeHelper, times(2)).snooze(any(NotificationRecord.class), anyLong()); |
| 2531 | } |
| 2532 | |
| 2533 | @Test |
| 2534 | public void testSnoozeRunnable_reSnoozeMultipleNotificationsWithGroupKey() throws Exception { |
| 2535 | final NotificationRecord notification = generateNotificationRecord( |
| 2536 | mTestNotificationChannel, 1, "group", true); |
| 2537 | final NotificationRecord notification2 = generateNotificationRecord( |
| 2538 | mTestNotificationChannel, 2, "group", true); |
| 2539 | mService.addNotification(notification); |
| 2540 | mService.addNotification(notification2); |
| 2541 | when(mSnoozeHelper.getNotification(any())).thenReturn(notification); |
| 2542 | when(mSnoozeHelper.getNotifications( |
| 2543 | anyString(), anyString(), anyInt())).thenReturn(new ArrayList<>()); |
| 2544 | |
| 2545 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
| 2546 | mService.new SnoozeNotificationRunnable( |
| 2547 | notification.getKey(), 100, null); |
| 2548 | snoozeNotificationRunnable.run(); |
| 2549 | when(mSnoozeHelper.getNotifications(anyString(), anyString(), anyInt())) |
| 2550 | .thenReturn(new ArrayList<>(Arrays.asList(notification, notification2))); |
| 2551 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable2 = |
| 2552 | mService.new SnoozeNotificationRunnable( |
| 2553 | notification.getKey(), 100, null); |
| 2554 | snoozeNotificationRunnable.run(); |
| 2555 | |
| 2556 | // snooze twice |
| 2557 | verify(mSnoozeHelper, times(4)).snooze(any(NotificationRecord.class), anyLong()); |
| 2558 | } |
| 2559 | |
| 2560 | @Test |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2561 | public void testSnoozeRunnable_snoozeNonGrouped() throws Exception { |
| 2562 | final NotificationRecord nonGrouped = generateNotificationRecord( |
| 2563 | mTestNotificationChannel, 1, null, false); |
| 2564 | final NotificationRecord grouped = generateNotificationRecord( |
| 2565 | mTestNotificationChannel, 2, "group", false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2566 | mService.addNotification(grouped); |
| 2567 | mService.addNotification(nonGrouped); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2568 | |
| 2569 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2570 | mService.new SnoozeNotificationRunnable( |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2571 | nonGrouped.getKey(), 100, null); |
| 2572 | snoozeNotificationRunnable.run(); |
| 2573 | |
| 2574 | // only snooze the one notification |
| 2575 | verify(mSnoozeHelper, times(1)).snooze(any(NotificationRecord.class), anyLong()); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2576 | assertTrue(nonGrouped.getStats().hasSnoozed()); |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 2577 | |
| 2578 | assertEquals(2, mNotificationRecordLogger.numCalls()); |
| 2579 | assertEquals(NotificationRecordLogger.NotificationEvent.NOTIFICATION_SNOOZED, |
| 2580 | mNotificationRecordLogger.event(0)); |
| 2581 | assertEquals( |
| 2582 | NotificationRecordLogger.NotificationCancelledEvent.NOTIFICATION_CANCEL_SNOOZED, |
| 2583 | mNotificationRecordLogger.event(1)); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2584 | } |
| 2585 | |
| 2586 | @Test |
| 2587 | public void testSnoozeRunnable_snoozeSummary_withChildren() throws Exception { |
| 2588 | final NotificationRecord parent = generateNotificationRecord( |
| 2589 | mTestNotificationChannel, 1, "group", true); |
| 2590 | final NotificationRecord child = generateNotificationRecord( |
| 2591 | mTestNotificationChannel, 2, "group", false); |
| 2592 | final NotificationRecord child2 = generateNotificationRecord( |
| 2593 | mTestNotificationChannel, 3, "group", false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2594 | mService.addNotification(parent); |
| 2595 | mService.addNotification(child); |
| 2596 | mService.addNotification(child2); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2597 | |
| 2598 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2599 | mService.new SnoozeNotificationRunnable( |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2600 | parent.getKey(), 100, null); |
| 2601 | snoozeNotificationRunnable.run(); |
| 2602 | |
| 2603 | // snooze parent and children |
| 2604 | verify(mSnoozeHelper, times(3)).snooze(any(NotificationRecord.class), anyLong()); |
| 2605 | } |
| 2606 | |
| 2607 | @Test |
| 2608 | public void testSnoozeRunnable_snoozeGroupChild_fellowChildren() throws Exception { |
| 2609 | final NotificationRecord parent = generateNotificationRecord( |
| 2610 | mTestNotificationChannel, 1, "group", true); |
| 2611 | final NotificationRecord child = generateNotificationRecord( |
| 2612 | mTestNotificationChannel, 2, "group", false); |
| 2613 | final NotificationRecord child2 = generateNotificationRecord( |
| 2614 | mTestNotificationChannel, 3, "group", false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2615 | mService.addNotification(parent); |
| 2616 | mService.addNotification(child); |
| 2617 | mService.addNotification(child2); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2618 | |
| 2619 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2620 | mService.new SnoozeNotificationRunnable( |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2621 | child2.getKey(), 100, null); |
| 2622 | snoozeNotificationRunnable.run(); |
| 2623 | |
| 2624 | // only snooze the one child |
| 2625 | verify(mSnoozeHelper, times(1)).snooze(any(NotificationRecord.class), anyLong()); |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 2626 | |
| 2627 | assertEquals(2, mNotificationRecordLogger.numCalls()); |
| 2628 | assertEquals(NotificationRecordLogger.NotificationEvent.NOTIFICATION_SNOOZED, |
| 2629 | mNotificationRecordLogger.event(0)); |
| 2630 | assertEquals(NotificationRecordLogger.NotificationCancelledEvent |
| 2631 | .NOTIFICATION_CANCEL_SNOOZED, mNotificationRecordLogger.event(1)); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2632 | } |
| 2633 | |
| 2634 | @Test |
| 2635 | public void testSnoozeRunnable_snoozeGroupChild_onlyChildOfSummary() throws Exception { |
| 2636 | final NotificationRecord parent = generateNotificationRecord( |
| 2637 | mTestNotificationChannel, 1, "group", true); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 2638 | assertTrue(parent.getSbn().getNotification().isGroupSummary()); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2639 | final NotificationRecord child = generateNotificationRecord( |
| 2640 | mTestNotificationChannel, 2, "group", false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2641 | mService.addNotification(parent); |
| 2642 | mService.addNotification(child); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2643 | |
| 2644 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2645 | mService.new SnoozeNotificationRunnable( |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2646 | child.getKey(), 100, null); |
| 2647 | snoozeNotificationRunnable.run(); |
| 2648 | |
| 2649 | // snooze child and summary |
| 2650 | verify(mSnoozeHelper, times(2)).snooze(any(NotificationRecord.class), anyLong()); |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 2651 | |
| 2652 | assertEquals(4, mNotificationRecordLogger.numCalls()); |
| 2653 | assertEquals(NotificationRecordLogger.NotificationEvent.NOTIFICATION_SNOOZED, |
| 2654 | mNotificationRecordLogger.event(0)); |
| 2655 | assertEquals( |
| 2656 | NotificationRecordLogger.NotificationCancelledEvent.NOTIFICATION_CANCEL_SNOOZED, |
| 2657 | mNotificationRecordLogger.event(1)); |
| 2658 | assertEquals(NotificationRecordLogger.NotificationEvent.NOTIFICATION_SNOOZED, |
| 2659 | mNotificationRecordLogger.event(2)); |
| 2660 | assertEquals( |
| 2661 | NotificationRecordLogger.NotificationCancelledEvent.NOTIFICATION_CANCEL_SNOOZED, |
| 2662 | mNotificationRecordLogger.event(3)); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2663 | } |
| 2664 | |
| 2665 | @Test |
| 2666 | public void testSnoozeRunnable_snoozeGroupChild_noOthersInGroup() throws Exception { |
| 2667 | final NotificationRecord child = generateNotificationRecord( |
| 2668 | mTestNotificationChannel, 2, "group", false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2669 | mService.addNotification(child); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2670 | |
| 2671 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2672 | mService.new SnoozeNotificationRunnable( |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2673 | child.getKey(), 100, null); |
| 2674 | snoozeNotificationRunnable.run(); |
| 2675 | |
| 2676 | // snooze child only |
| 2677 | verify(mSnoozeHelper, times(1)).snooze(any(NotificationRecord.class), anyLong()); |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 2678 | |
| 2679 | assertEquals(2, mNotificationRecordLogger.numCalls()); |
| 2680 | assertEquals(NotificationRecordLogger.NotificationEvent.NOTIFICATION_SNOOZED, |
| 2681 | mNotificationRecordLogger.event(0)); |
| 2682 | assertEquals( |
| 2683 | NotificationRecordLogger.NotificationCancelledEvent.NOTIFICATION_CANCEL_SNOOZED, |
| 2684 | mNotificationRecordLogger.event(1)); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2685 | } |
| 2686 | |
| 2687 | @Test |
| 2688 | public void testPostGroupChild_unsnoozeParent() throws Exception { |
| 2689 | final NotificationRecord child = generateNotificationRecord( |
| 2690 | mTestNotificationChannel, 2, "group", false); |
| 2691 | |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 2692 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "testPostNonGroup_noUnsnoozing", |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 2693 | child.getSbn().getId(), child.getSbn().getNotification(), |
| 2694 | child.getSbn().getUserId()); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2695 | waitForIdle(); |
| 2696 | |
| 2697 | verify(mSnoozeHelper, times(1)).repostGroupSummary( |
| 2698 | anyString(), anyInt(), eq(child.getGroupKey())); |
| 2699 | } |
| 2700 | |
| 2701 | @Test |
| 2702 | public void testPostNonGroup_noUnsnoozing() throws Exception { |
| 2703 | final NotificationRecord record = generateNotificationRecord( |
| 2704 | mTestNotificationChannel, 2, null, false); |
| 2705 | |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 2706 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "testPostNonGroup_noUnsnoozing", |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 2707 | record.getSbn().getId(), record.getSbn().getNotification(), |
| 2708 | record.getSbn().getUserId()); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2709 | waitForIdle(); |
| 2710 | |
| 2711 | verify(mSnoozeHelper, never()).repostGroupSummary(anyString(), anyInt(), anyString()); |
| 2712 | } |
| 2713 | |
| 2714 | @Test |
| 2715 | public void testPostGroupSummary_noUnsnoozing() throws Exception { |
| 2716 | final NotificationRecord parent = generateNotificationRecord( |
| 2717 | mTestNotificationChannel, 2, "group", true); |
| 2718 | |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 2719 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "testPostGroupSummary_noUnsnoozing", |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 2720 | parent.getSbn().getId(), parent.getSbn().getNotification(), |
| 2721 | parent.getSbn().getUserId()); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2722 | waitForIdle(); |
| 2723 | |
| 2724 | verify(mSnoozeHelper, never()).repostGroupSummary(anyString(), anyInt(), anyString()); |
| 2725 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2726 | |
| 2727 | @Test |
Julia Reynolds | 0d07378 | 2020-02-13 16:07:52 -0500 | [diff] [blame] | 2728 | public void testSystemNotificationListenerCanUnsnooze() throws Exception { |
| 2729 | final NotificationRecord nr = generateNotificationRecord( |
| 2730 | mTestNotificationChannel, 2, "group", false); |
| 2731 | |
| 2732 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 2733 | "testSystemNotificationListenerCanUnsnooze", |
| 2734 | nr.getSbn().getId(), nr.getSbn().getNotification(), |
| 2735 | nr.getSbn().getUserId()); |
| 2736 | waitForIdle(); |
| 2737 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
| 2738 | mService.new SnoozeNotificationRunnable( |
| 2739 | nr.getKey(), 100, null); |
| 2740 | snoozeNotificationRunnable.run(); |
| 2741 | |
| 2742 | ManagedServices.ManagedServiceInfo listener = mListeners.new ManagedServiceInfo( |
| 2743 | null, new ComponentName(PKG, "test_class"), mUid, true, null, 0); |
| 2744 | listener.isSystem = true; |
| 2745 | when(mListeners.checkServiceTokenLocked(any())).thenReturn(listener); |
| 2746 | |
| 2747 | mBinderService.unsnoozeNotificationFromSystemListener(null, nr.getKey()); |
| 2748 | waitForIdle(); |
| 2749 | StatusBarNotification[] notifs = mBinderService.getActiveNotifications(PKG); |
| 2750 | assertEquals(1, notifs.length); |
| 2751 | assertNotNull(notifs[0].getKey());//mService.getNotificationRecord(nr.getSbn().getKey())); |
| 2752 | } |
| 2753 | |
| 2754 | @Test |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 2755 | public void testSetListenerAccessForUser() throws Exception { |
| 2756 | UserHandle user = UserHandle.of(10); |
| 2757 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2758 | mBinderService.setNotificationListenerAccessGrantedForUser(c, user.getIdentifier(), true); |
| 2759 | |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 2760 | |
| 2761 | verify(mContext, times(1)).sendBroadcastAsUser(any(), eq(user), any()); |
| 2762 | verify(mListeners, times(1)).setPackageOrComponentEnabled( |
| 2763 | c.flattenToString(), user.getIdentifier(), true, true); |
| 2764 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 2765 | c.flattenToString(), user.getIdentifier(), false, true); |
| 2766 | verify(mAssistants, never()).setPackageOrComponentEnabled( |
| 2767 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2768 | } |
| 2769 | |
| 2770 | @Test |
| 2771 | public void testSetAssistantAccessForUser() throws Exception { |
| 2772 | UserHandle user = UserHandle.of(10); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2773 | List<UserInfo> uis = new ArrayList<>(); |
| 2774 | UserInfo ui = new UserInfo(); |
| 2775 | ui.id = 10; |
| 2776 | uis.add(ui); |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 2777 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2778 | when(mUm.getEnabledProfiles(10)).thenReturn(uis); |
| 2779 | |
| 2780 | mBinderService.setNotificationAssistantAccessGrantedForUser(c, user.getIdentifier(), true); |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 2781 | |
| 2782 | verify(mContext, times(1)).sendBroadcastAsUser(any(), eq(user), any()); |
| 2783 | verify(mAssistants, times(1)).setPackageOrComponentEnabled( |
| 2784 | c.flattenToString(), user.getIdentifier(), true, true); |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 2785 | verify(mAssistants).setUserSet(10, true); |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 2786 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 2787 | c.flattenToString(), user.getIdentifier(), false, true); |
| 2788 | verify(mListeners, never()).setPackageOrComponentEnabled( |
| 2789 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2790 | } |
| 2791 | |
| 2792 | @Test |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 2793 | public void testGetAssistantAllowedForUser() throws Exception { |
| 2794 | UserHandle user = UserHandle.of(10); |
| 2795 | try { |
| 2796 | mBinderService.getAllowedNotificationAssistantForUser(user.getIdentifier()); |
| 2797 | } catch (IllegalStateException e) { |
| 2798 | if (!e.getMessage().contains("At most one NotificationAssistant")) { |
| 2799 | throw e; |
| 2800 | } |
| 2801 | } |
| 2802 | verify(mAssistants, times(1)).getAllowedComponents(user.getIdentifier()); |
| 2803 | } |
| 2804 | |
| 2805 | @Test |
| 2806 | public void testGetAssistantAllowed() throws Exception { |
| 2807 | try { |
| 2808 | mBinderService.getAllowedNotificationAssistant(); |
| 2809 | } catch (IllegalStateException e) { |
| 2810 | if (!e.getMessage().contains("At most one NotificationAssistant")) { |
| 2811 | throw e; |
| 2812 | } |
| 2813 | } |
| 2814 | verify(mAssistants, times(1)).getAllowedComponents(0); |
| 2815 | } |
| 2816 | |
| 2817 | @Test |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 2818 | public void testSetDndAccessForUser() throws Exception { |
| 2819 | UserHandle user = UserHandle.of(10); |
| 2820 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2821 | mBinderService.setNotificationPolicyAccessGrantedForUser( |
| 2822 | c.getPackageName(), user.getIdentifier(), true); |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 2823 | |
| 2824 | verify(mContext, times(1)).sendBroadcastAsUser(any(), eq(user), any()); |
| 2825 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 2826 | c.getPackageName(), user.getIdentifier(), true, true); |
| 2827 | verify(mAssistants, never()).setPackageOrComponentEnabled( |
| 2828 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2829 | verify(mListeners, never()).setPackageOrComponentEnabled( |
| 2830 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2831 | } |
| 2832 | |
| 2833 | @Test |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2834 | public void testSetListenerAccess() throws Exception { |
| 2835 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2836 | mBinderService.setNotificationListenerAccessGranted(c, true); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2837 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2838 | verify(mListeners, times(1)).setPackageOrComponentEnabled( |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2839 | c.flattenToString(), 0, true, true); |
| 2840 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 2841 | c.flattenToString(), 0, false, true); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2842 | verify(mAssistants, never()).setPackageOrComponentEnabled( |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2843 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2844 | } |
| 2845 | |
| 2846 | @Test |
| 2847 | public void testSetAssistantAccess() throws Exception { |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2848 | List<UserInfo> uis = new ArrayList<>(); |
| 2849 | UserInfo ui = new UserInfo(); |
| 2850 | ui.id = 0; |
| 2851 | uis.add(ui); |
| 2852 | when(mUm.getEnabledProfiles(ui.id)).thenReturn(uis); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2853 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2854 | |
| 2855 | mBinderService.setNotificationAssistantAccessGranted(c, true); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2856 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2857 | verify(mAssistants, times(1)).setPackageOrComponentEnabled( |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2858 | c.flattenToString(), 0, true, true); |
| 2859 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 2860 | c.flattenToString(), 0, false, true); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2861 | verify(mListeners, never()).setPackageOrComponentEnabled( |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2862 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2863 | } |
| 2864 | |
| 2865 | @Test |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2866 | public void testSetAssistantAccess_multiProfile() throws Exception { |
| 2867 | List<UserInfo> uis = new ArrayList<>(); |
| 2868 | UserInfo ui = new UserInfo(); |
| 2869 | ui.id = 0; |
| 2870 | uis.add(ui); |
| 2871 | UserInfo ui10 = new UserInfo(); |
| 2872 | ui10.id = 10; |
| 2873 | uis.add(ui10); |
| 2874 | when(mUm.getEnabledProfiles(ui.id)).thenReturn(uis); |
| 2875 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 2876 | |
| 2877 | mBinderService.setNotificationAssistantAccessGranted(c, true); |
| 2878 | |
| 2879 | verify(mAssistants, times(1)).setPackageOrComponentEnabled( |
| 2880 | c.flattenToString(), 0, true, true); |
| 2881 | verify(mAssistants, times(1)).setPackageOrComponentEnabled( |
| 2882 | c.flattenToString(), 10, true, true); |
| 2883 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 2884 | c.flattenToString(), 0, false, true); |
| 2885 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 2886 | c.flattenToString(), 10, false, true); |
| 2887 | verify(mListeners, never()).setPackageOrComponentEnabled( |
| 2888 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2889 | } |
| 2890 | |
| 2891 | @Test |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 2892 | public void testSetAssistantAccess_nullWithAllowedAssistant() throws Exception { |
| 2893 | ArrayList<ComponentName> componentList = new ArrayList<>(); |
| 2894 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 2895 | componentList.add(c); |
| 2896 | when(mAssistants.getAllowedComponents(anyInt())).thenReturn(componentList); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2897 | List<UserInfo> uis = new ArrayList<>(); |
| 2898 | UserInfo ui = new UserInfo(); |
| 2899 | ui.id = 0; |
| 2900 | uis.add(ui); |
| 2901 | when(mUm.getEnabledProfiles(ui.id)).thenReturn(uis); |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 2902 | |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2903 | mBinderService.setNotificationAssistantAccessGranted(null, true); |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 2904 | |
| 2905 | verify(mAssistants, times(1)).setPackageOrComponentEnabled( |
| 2906 | c.flattenToString(), 0, true, false); |
| 2907 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 2908 | c.flattenToString(), 0, false, false); |
| 2909 | verify(mListeners, never()).setPackageOrComponentEnabled( |
| 2910 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2911 | } |
| 2912 | |
| 2913 | @Test |
| 2914 | public void testSetAssistantAccessForUser_nullWithAllowedAssistant() throws Exception { |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2915 | List<UserInfo> uis = new ArrayList<>(); |
| 2916 | UserInfo ui = new UserInfo(); |
| 2917 | ui.id = 10; |
| 2918 | uis.add(ui); |
| 2919 | UserHandle user = ui.getUserHandle(); |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 2920 | ArrayList<ComponentName> componentList = new ArrayList<>(); |
| 2921 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 2922 | componentList.add(c); |
| 2923 | when(mAssistants.getAllowedComponents(anyInt())).thenReturn(componentList); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2924 | when(mUm.getEnabledProfiles(10)).thenReturn(uis); |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 2925 | |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2926 | mBinderService.setNotificationAssistantAccessGrantedForUser( |
| 2927 | null, user.getIdentifier(), true); |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 2928 | |
| 2929 | verify(mAssistants, times(1)).setPackageOrComponentEnabled( |
| 2930 | c.flattenToString(), user.getIdentifier(), true, false); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2931 | verify(mAssistants).setUserSet(10, true); |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 2932 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 2933 | c.flattenToString(), user.getIdentifier(), false, false); |
| 2934 | verify(mListeners, never()).setPackageOrComponentEnabled( |
| 2935 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2936 | } |
| 2937 | |
| 2938 | @Test |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2939 | public void testSetAssistantAccessForUser_workProfile_nullWithAllowedAssistant() |
| 2940 | throws Exception { |
| 2941 | List<UserInfo> uis = new ArrayList<>(); |
| 2942 | UserInfo ui = new UserInfo(); |
| 2943 | ui.id = 0; |
| 2944 | uis.add(ui); |
| 2945 | UserInfo ui10 = new UserInfo(); |
| 2946 | ui10.id = 10; |
| 2947 | uis.add(ui10); |
| 2948 | UserHandle user = ui.getUserHandle(); |
| 2949 | ArrayList<ComponentName> componentList = new ArrayList<>(); |
| 2950 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 2951 | componentList.add(c); |
| 2952 | when(mAssistants.getAllowedComponents(anyInt())).thenReturn(componentList); |
| 2953 | when(mUm.getEnabledProfiles(ui.id)).thenReturn(uis); |
| 2954 | |
| 2955 | mBinderService.setNotificationAssistantAccessGrantedForUser( |
| 2956 | null, user.getIdentifier(), true); |
| 2957 | |
| 2958 | verify(mAssistants, times(1)).setPackageOrComponentEnabled( |
| 2959 | c.flattenToString(), user.getIdentifier(), true, false); |
| 2960 | verify(mAssistants, times(1)).setPackageOrComponentEnabled( |
| 2961 | c.flattenToString(), ui10.id, true, false); |
| 2962 | verify(mAssistants).setUserSet(0, true); |
| 2963 | verify(mAssistants).setUserSet(10, true); |
| 2964 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 2965 | c.flattenToString(), user.getIdentifier(), false, false); |
| 2966 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 2967 | c.flattenToString(), ui10.id, false, false); |
| 2968 | verify(mListeners, never()).setPackageOrComponentEnabled( |
| 2969 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2970 | } |
| 2971 | |
| 2972 | @Test |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2973 | public void testSetDndAccess() throws Exception { |
| 2974 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2975 | |
| 2976 | mBinderService.setNotificationPolicyAccessGranted(c.getPackageName(), true); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2977 | |
| 2978 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 2979 | c.getPackageName(), 0, true, true); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2980 | verify(mAssistants, never()).setPackageOrComponentEnabled( |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2981 | any(), anyInt(), anyBoolean(), anyBoolean()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2982 | verify(mListeners, never()).setPackageOrComponentEnabled( |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2983 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2984 | } |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 2985 | |
| 2986 | @Test |
Rajeev Kumar | bc79dcd | 2020-02-04 19:54:30 -0800 | [diff] [blame] | 2987 | public void testSetListenerAccess_onLowRam() throws Exception { |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 2988 | when(mActivityManager.isLowRamDevice()).thenReturn(true); |
| 2989 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 2990 | mBinderService.setNotificationListenerAccessGranted(c, true); |
| 2991 | |
Rajeev Kumar | bc79dcd | 2020-02-04 19:54:30 -0800 | [diff] [blame] | 2992 | verify(mListeners).setPackageOrComponentEnabled( |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 2993 | anyString(), anyInt(), anyBoolean(), anyBoolean()); |
Rajeev Kumar | bc79dcd | 2020-02-04 19:54:30 -0800 | [diff] [blame] | 2994 | verify(mConditionProviders).setPackageOrComponentEnabled( |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 2995 | anyString(), anyInt(), anyBoolean(), anyBoolean()); |
Rajeev Kumar | bc79dcd | 2020-02-04 19:54:30 -0800 | [diff] [blame] | 2996 | verify(mAssistants).migrateToXml(); |
| 2997 | verify(mAssistants).resetDefaultAssistantsIfNecessary(); |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 2998 | } |
| 2999 | |
| 3000 | @Test |
Rajeev Kumar | bc79dcd | 2020-02-04 19:54:30 -0800 | [diff] [blame] | 3001 | public void testSetAssistantAccess_onLowRam() throws Exception { |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 3002 | when(mActivityManager.isLowRamDevice()).thenReturn(true); |
| 3003 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 3004 | List<UserInfo> uis = new ArrayList<>(); |
| 3005 | UserInfo ui = new UserInfo(); |
| 3006 | ui.id = 0; |
| 3007 | uis.add(ui); |
| 3008 | when(mUm.getEnabledProfiles(ui.id)).thenReturn(uis); |
| 3009 | |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 3010 | mBinderService.setNotificationAssistantAccessGranted(c, true); |
| 3011 | |
Rajeev Kumar | bc79dcd | 2020-02-04 19:54:30 -0800 | [diff] [blame] | 3012 | verify(mListeners).migrateToXml(); |
| 3013 | verify(mListeners).notifyNotificationChannelChanged(anyString(), any(), any(), |
| 3014 | anyInt()); |
| 3015 | verify(mConditionProviders).setPackageOrComponentEnabled( |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 3016 | anyString(), anyInt(), anyBoolean(), anyBoolean()); |
Rajeev Kumar | bc79dcd | 2020-02-04 19:54:30 -0800 | [diff] [blame] | 3017 | verify(mAssistants).migrateToXml(); |
| 3018 | verify(mAssistants).resetDefaultAssistantsIfNecessary(); |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 3019 | } |
| 3020 | |
| 3021 | @Test |
Rajeev Kumar | bc79dcd | 2020-02-04 19:54:30 -0800 | [diff] [blame] | 3022 | public void testSetDndAccess_onLowRam() throws Exception { |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 3023 | when(mActivityManager.isLowRamDevice()).thenReturn(true); |
| 3024 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 3025 | mBinderService.setNotificationPolicyAccessGranted(c.getPackageName(), true); |
| 3026 | |
Rajeev Kumar | bc79dcd | 2020-02-04 19:54:30 -0800 | [diff] [blame] | 3027 | verify(mListeners).migrateToXml(); |
| 3028 | verify(mListeners).notifyNotificationChannelChanged(anyString(), any(), any(), |
| 3029 | anyInt()); |
| 3030 | verify(mConditionProviders).setPackageOrComponentEnabled( |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 3031 | anyString(), anyInt(), anyBoolean(), anyBoolean()); |
Rajeev Kumar | bc79dcd | 2020-02-04 19:54:30 -0800 | [diff] [blame] | 3032 | verify(mAssistants).migrateToXml(); |
| 3033 | verify(mAssistants).resetDefaultAssistantsIfNecessary(); |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 3034 | } |
| 3035 | |
| 3036 | @Test |
| 3037 | public void testSetListenerAccess_doesNothingOnLowRam_exceptWatch() throws Exception { |
| 3038 | when(mPackageManagerClient.hasSystemFeature(FEATURE_WATCH)).thenReturn(true); |
| 3039 | when(mActivityManager.isLowRamDevice()).thenReturn(true); |
| 3040 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 3041 | |
| 3042 | mBinderService.setNotificationListenerAccessGranted(c, true); |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 3043 | |
| 3044 | verify(mListeners, times(1)).setPackageOrComponentEnabled( |
| 3045 | c.flattenToString(), 0, true, true); |
| 3046 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 3047 | c.flattenToString(), 0, false, true); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3048 | verify(mAssistants, never()).setPackageOrComponentEnabled( |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 3049 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 3050 | } |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 3051 | |
| 3052 | @Test |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 3053 | public void testSetAssistantAccess_doesNothingOnLowRam_exceptWatch() throws Exception { |
| 3054 | when(mPackageManagerClient.hasSystemFeature(FEATURE_WATCH)).thenReturn(true); |
| 3055 | when(mActivityManager.isLowRamDevice()).thenReturn(true); |
| 3056 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 3057 | List<UserInfo> uis = new ArrayList<>(); |
| 3058 | UserInfo ui = new UserInfo(); |
| 3059 | ui.id = 0; |
| 3060 | uis.add(ui); |
| 3061 | when(mUm.getEnabledProfiles(ui.id)).thenReturn(uis); |
| 3062 | |
| 3063 | mBinderService.setNotificationAssistantAccessGranted(c, true); |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 3064 | |
| 3065 | verify(mListeners, never()).setPackageOrComponentEnabled( |
| 3066 | anyString(), anyInt(), anyBoolean(), anyBoolean()); |
| 3067 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 3068 | c.flattenToString(), 0, false, true); |
| 3069 | verify(mAssistants, times(1)).setPackageOrComponentEnabled( |
| 3070 | c.flattenToString(), 0, true, true); |
| 3071 | } |
| 3072 | |
| 3073 | @Test |
| 3074 | public void testSetDndAccess_doesNothingOnLowRam_exceptWatch() throws Exception { |
| 3075 | when(mPackageManagerClient.hasSystemFeature(FEATURE_WATCH)).thenReturn(true); |
| 3076 | when(mActivityManager.isLowRamDevice()).thenReturn(true); |
| 3077 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 3078 | |
| 3079 | mBinderService.setNotificationPolicyAccessGranted(c.getPackageName(), true); |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 3080 | |
| 3081 | verify(mListeners, never()).setPackageOrComponentEnabled( |
| 3082 | anyString(), anyInt(), anyBoolean(), anyBoolean()); |
| 3083 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 3084 | c.getPackageName(), 0, true, true); |
| 3085 | verify(mAssistants, never()).setPackageOrComponentEnabled( |
| 3086 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 3087 | } |
| 3088 | |
| 3089 | @Test |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 3090 | public void testOnlyAutogroupIfGroupChanged_noPriorNoti_autogroups() throws Exception { |
| 3091 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel, 0, null, false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3092 | mService.addEnqueuedNotification(r); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 3093 | NotificationManagerService.PostNotificationRunnable runnable = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3094 | mService.new PostNotificationRunnable(r.getKey()); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 3095 | runnable.run(); |
| 3096 | waitForIdle(); |
| 3097 | |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 3098 | verify(mGroupHelper, times(1)).onNotificationPosted(any(), anyBoolean()); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 3099 | } |
| 3100 | |
| 3101 | @Test |
| 3102 | public void testOnlyAutogroupIfGroupChanged_groupChanged_autogroups() |
| 3103 | throws Exception { |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3104 | NotificationRecord r = |
| 3105 | generateNotificationRecord(mTestNotificationChannel, 0, "group", false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3106 | mService.addNotification(r); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 3107 | |
| 3108 | r = generateNotificationRecord(mTestNotificationChannel, 0, null, false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3109 | mService.addEnqueuedNotification(r); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 3110 | NotificationManagerService.PostNotificationRunnable runnable = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3111 | mService.new PostNotificationRunnable(r.getKey()); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 3112 | runnable.run(); |
| 3113 | waitForIdle(); |
| 3114 | |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 3115 | verify(mGroupHelper, times(1)).onNotificationPosted(any(), anyBoolean()); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 3116 | } |
| 3117 | |
| 3118 | @Test |
| 3119 | public void testOnlyAutogroupIfGroupChanged_noGroupChanged_autogroups() |
| 3120 | throws Exception { |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 3121 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel, 0, "group", |
| 3122 | false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3123 | mService.addNotification(r); |
| 3124 | mService.addEnqueuedNotification(r); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 3125 | |
| 3126 | NotificationManagerService.PostNotificationRunnable runnable = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3127 | mService.new PostNotificationRunnable(r.getKey()); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 3128 | runnable.run(); |
| 3129 | waitForIdle(); |
| 3130 | |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 3131 | verify(mGroupHelper, never()).onNotificationPosted(any(), anyBoolean()); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 3132 | } |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 3133 | |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 3134 | @Test |
Brad Stenning | d2e7a97 | 2018-10-01 09:08:42 -0700 | [diff] [blame] | 3135 | public void testDontAutogroupIfCritical() throws Exception { |
| 3136 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel, 0, null, false); |
| 3137 | r.setCriticality(CriticalNotificationExtractor.CRITICAL_LOW); |
| 3138 | mService.addEnqueuedNotification(r); |
| 3139 | NotificationManagerService.PostNotificationRunnable runnable = |
| 3140 | mService.new PostNotificationRunnable(r.getKey()); |
| 3141 | runnable.run(); |
| 3142 | |
| 3143 | r = generateNotificationRecord(mTestNotificationChannel, 1, null, false); |
| 3144 | r.setCriticality(CriticalNotificationExtractor.CRITICAL); |
| 3145 | runnable = mService.new PostNotificationRunnable(r.getKey()); |
| 3146 | mService.addEnqueuedNotification(r); |
| 3147 | |
| 3148 | runnable.run(); |
| 3149 | waitForIdle(); |
| 3150 | |
| 3151 | verify(mGroupHelper, never()).onNotificationPosted(any(), anyBoolean()); |
| 3152 | } |
| 3153 | |
| 3154 | @Test |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 3155 | public void testNoFakeColorizedPermission() throws Exception { |
| 3156 | when(mPackageManagerClient.checkPermission(any(), any())).thenReturn(PERMISSION_DENIED); |
| 3157 | Notification.Builder nb = new Notification.Builder(mContext, |
| 3158 | mTestNotificationChannel.getId()) |
| 3159 | .setContentTitle("foo") |
| 3160 | .setColorized(true) |
| 3161 | .setFlag(Notification.FLAG_CAN_COLORIZE, true) |
| 3162 | .setSmallIcon(android.R.drawable.sym_def_app_icon); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 3163 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, |
| 3164 | "testNoFakeColorizedPermission", mUid, 0, |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 3165 | nb.build(), new UserHandle(mUid), null, 0); |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 3166 | NotificationRecord nr = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 3167 | |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 3168 | mBinderService.enqueueNotificationWithTag(PKG, PKG, sbn.getTag(), |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3169 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 3170 | waitForIdle(); |
| 3171 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3172 | NotificationRecord posted = mService.findNotificationLocked( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3173 | PKG, nr.getSbn().getTag(), nr.getSbn().getId(), nr.getSbn().getUserId()); |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 3174 | |
| 3175 | assertFalse(posted.getNotification().isColorized()); |
| 3176 | } |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 3177 | |
| 3178 | @Test |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 3179 | public void testGetNotificationCountLocked() { |
| 3180 | String sampleTagToExclude = null; |
| 3181 | int sampleIdToExclude = 0; |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 3182 | for (int i = 0; i < 20; i++) { |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 3183 | NotificationRecord r = |
| 3184 | generateNotificationRecord(mTestNotificationChannel, i, null, false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3185 | mService.addEnqueuedNotification(r); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 3186 | |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 3187 | } |
| 3188 | for (int i = 0; i < 20; i++) { |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 3189 | NotificationRecord r = |
| 3190 | generateNotificationRecord(mTestNotificationChannel, i, null, false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3191 | mService.addNotification(r); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3192 | sampleTagToExclude = r.getSbn().getTag(); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 3193 | sampleIdToExclude = i; |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 3194 | } |
| 3195 | |
| 3196 | // another package |
| 3197 | Notification n = |
| 3198 | new Notification.Builder(mContext, mTestNotificationChannel.getId()) |
| 3199 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 3200 | .build(); |
| 3201 | |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 3202 | StatusBarNotification sbn = new StatusBarNotification("a", "a", 0, "tag", mUid, 0, |
| 3203 | n, new UserHandle(mUid), null, 0); |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 3204 | NotificationRecord otherPackage = |
| 3205 | new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3206 | mService.addEnqueuedNotification(otherPackage); |
| 3207 | mService.addNotification(otherPackage); |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 3208 | |
| 3209 | // 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] | 3210 | // anything that's currently enqueued or posted |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 3211 | int userId = new UserHandle(mUid).getIdentifier(); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3212 | assertEquals(40, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3213 | mService.getNotificationCountLocked(PKG, userId, 0, null)); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3214 | assertEquals(40, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3215 | mService.getNotificationCountLocked(PKG, userId, 0, "tag2")); |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 3216 | |
| 3217 | // return all for package "a" - "banana" tag isn't used |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3218 | assertEquals(2, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3219 | mService.getNotificationCountLocked("a", userId, 0, "banana")); |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 3220 | |
| 3221 | // 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] | 3222 | assertEquals(39, mService.getNotificationCountLocked( |
| 3223 | PKG, userId, sampleIdToExclude, sampleTagToExclude)); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3224 | } |
| 3225 | |
| 3226 | @Test |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 3227 | public void testAddAutogroup_requestsSort() throws Exception { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3228 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3229 | mService.addNotification(r); |
| 3230 | mService.addAutogroupKeyLocked(r.getKey()); |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 3231 | |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 3232 | verify(mRankingHandler, times(1)).requestSort(); |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 3233 | } |
| 3234 | |
| 3235 | @Test |
| 3236 | public void testRemoveAutogroup_requestsSort() throws Exception { |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 3237 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3238 | r.setOverrideGroupKey("TEST"); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3239 | mService.addNotification(r); |
| 3240 | mService.removeAutogroupKeyLocked(r.getKey()); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3241 | |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 3242 | verify(mRankingHandler, times(1)).requestSort(); |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 3243 | } |
| 3244 | |
| 3245 | @Test |
| 3246 | public void testReaddAutogroup_noSort() throws Exception { |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 3247 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3248 | r.setOverrideGroupKey("TEST"); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3249 | mService.addNotification(r); |
| 3250 | mService.addAutogroupKeyLocked(r.getKey()); |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 3251 | |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 3252 | verify(mRankingHandler, never()).requestSort(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3253 | } |
| 3254 | |
| 3255 | @Test |
| 3256 | public void testHandleRankingSort_sendsUpdateOnSignalExtractorChange() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 3257 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3258 | NotificationManagerService.WorkerHandler handler = mock( |
| 3259 | NotificationManagerService.WorkerHandler.class); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3260 | mService.setHandler(handler); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3261 | |
| 3262 | Map<String, Answer> answers = getSignalExtractorSideEffects(); |
| 3263 | for (String message : answers.keySet()) { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3264 | mService.clearNotifications(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3265 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3266 | mService.addNotification(r); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3267 | |
| 3268 | doAnswer(answers.get(message)).when(mRankingHelper).extractSignals(r); |
| 3269 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3270 | mService.handleRankingSort(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3271 | } |
| 3272 | verify(handler, times(answers.size())).scheduleSendRankingUpdate(); |
| 3273 | } |
| 3274 | |
| 3275 | @Test |
| 3276 | public void testHandleRankingSort_noUpdateWhenNoSignalChange() throws Exception { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3277 | mService.setRankingHelper(mRankingHelper); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3278 | NotificationManagerService.WorkerHandler handler = mock( |
| 3279 | NotificationManagerService.WorkerHandler.class); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3280 | mService.setHandler(handler); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3281 | |
| 3282 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3283 | mService.addNotification(r); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3284 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3285 | mService.handleRankingSort(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3286 | verify(handler, never()).scheduleSendRankingUpdate(); |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 3287 | } |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3288 | |
| 3289 | @Test |
| 3290 | public void testReadPolicyXml_readApprovedServicesFromXml() throws Exception { |
Julia Reynolds | d6d5a59 | 2018-04-02 11:03:32 -0400 | [diff] [blame] | 3291 | final String upgradeXml = "<notification-policy version=\"1\">" |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3292 | + "<ranking></ranking>" |
| 3293 | + "<enabled_listeners>" |
| 3294 | + "<service_listing approved=\"test\" user=\"0\" primary=\"true\" />" |
| 3295 | + "</enabled_listeners>" |
| 3296 | + "<enabled_assistants>" |
| 3297 | + "<service_listing approved=\"test\" user=\"0\" primary=\"true\" />" |
| 3298 | + "</enabled_assistants>" |
| 3299 | + "<dnd_apps>" |
| 3300 | + "<service_listing approved=\"test\" user=\"0\" primary=\"true\" />" |
| 3301 | + "</dnd_apps>" |
| 3302 | + "</notification-policy>"; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3303 | mService.readPolicyXml( |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 3304 | new BufferedInputStream(new ByteArrayInputStream(upgradeXml.getBytes())), |
| 3305 | false, |
| 3306 | UserHandle.USER_ALL); |
| 3307 | verify(mListeners, times(1)).readXml(any(), any(), anyBoolean(), anyInt()); |
| 3308 | verify(mConditionProviders, times(1)).readXml(any(), any(), anyBoolean(), anyInt()); |
| 3309 | verify(mAssistants, times(1)).readXml(any(), any(), anyBoolean(), anyInt()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3310 | |
| 3311 | // numbers are inflated for setup |
| 3312 | verify(mListeners, times(1)).migrateToXml(); |
| 3313 | verify(mConditionProviders, times(1)).migrateToXml(); |
| 3314 | verify(mAssistants, times(1)).migrateToXml(); |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 3315 | verify(mAssistants, times(2)).resetDefaultAssistantsIfNecessary(); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3316 | } |
| 3317 | |
| 3318 | @Test |
Jay Aliomer | 4dc508d | 2019-08-06 17:36:31 -0400 | [diff] [blame] | 3319 | public void testReadPolicyXml_readSnoozedNotificationsFromXml() throws Exception { |
| 3320 | final String upgradeXml = "<notification-policy version=\"1\">" |
| 3321 | + "<snoozed-notifications>></snoozed-notifications>" |
| 3322 | + "</notification-policy>"; |
| 3323 | mService.readPolicyXml( |
| 3324 | new BufferedInputStream(new ByteArrayInputStream(upgradeXml.getBytes())), |
| 3325 | false, |
| 3326 | UserHandle.USER_ALL); |
Julia Reynolds | 9050d9a | 2020-02-20 09:38:05 -0500 | [diff] [blame] | 3327 | verify(mSnoozeHelper, times(1)).readXml(any(XmlPullParser.class), anyLong()); |
Jay Aliomer | 4dc508d | 2019-08-06 17:36:31 -0400 | [diff] [blame] | 3328 | } |
| 3329 | |
| 3330 | @Test |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3331 | public void testReadPolicyXml_readApprovedServicesFromSettings() throws Exception { |
| 3332 | final String preupgradeXml = "<notification-policy version=\"1\">" |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3333 | + "<ranking></ranking>" |
| 3334 | + "</notification-policy>"; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3335 | mService.readPolicyXml( |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 3336 | new BufferedInputStream(new ByteArrayInputStream(preupgradeXml.getBytes())), |
| 3337 | false, |
| 3338 | UserHandle.USER_ALL); |
| 3339 | verify(mListeners, never()).readXml(any(), any(), anyBoolean(), anyInt()); |
| 3340 | verify(mConditionProviders, never()).readXml(any(), any(), anyBoolean(), anyInt()); |
| 3341 | verify(mAssistants, never()).readXml(any(), any(), anyBoolean(), anyInt()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3342 | |
| 3343 | // numbers are inflated for setup |
| 3344 | verify(mListeners, times(2)).migrateToXml(); |
| 3345 | verify(mConditionProviders, times(2)).migrateToXml(); |
| 3346 | verify(mAssistants, times(2)).migrateToXml(); |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 3347 | verify(mAssistants, times(2)).resetDefaultAssistantsIfNecessary(); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3348 | } |
| 3349 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 3350 | @Test |
| 3351 | public void testReadPolicyXml_doesNotRestoreManagedServicesForManagedUser() throws Exception { |
| 3352 | final String policyXml = "<notification-policy version=\"1\">" |
| 3353 | + "<ranking></ranking>" |
| 3354 | + "<enabled_listeners>" |
| 3355 | + "<service_listing approved=\"test\" user=\"10\" primary=\"true\" />" |
| 3356 | + "</enabled_listeners>" |
| 3357 | + "<enabled_assistants>" |
| 3358 | + "<service_listing approved=\"test\" user=\"10\" primary=\"true\" />" |
| 3359 | + "</enabled_assistants>" |
| 3360 | + "<dnd_apps>" |
| 3361 | + "<service_listing approved=\"test\" user=\"10\" primary=\"true\" />" |
| 3362 | + "</dnd_apps>" |
| 3363 | + "</notification-policy>"; |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 3364 | when(mUm.isManagedProfile(10)).thenReturn(true); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 3365 | mService.readPolicyXml( |
| 3366 | new BufferedInputStream(new ByteArrayInputStream(policyXml.getBytes())), |
| 3367 | true, |
| 3368 | 10); |
| 3369 | verify(mListeners, never()).readXml(any(), any(), eq(true), eq(10)); |
| 3370 | verify(mConditionProviders, never()).readXml(any(), any(), eq(true), eq(10)); |
| 3371 | verify(mAssistants, never()).readXml(any(), any(), eq(true), eq(10)); |
| 3372 | } |
| 3373 | |
| 3374 | @Test |
| 3375 | public void testReadPolicyXml_restoresManagedServicesForNonManagedUser() throws Exception { |
| 3376 | final String policyXml = "<notification-policy version=\"1\">" |
| 3377 | + "<ranking></ranking>" |
| 3378 | + "<enabled_listeners>" |
| 3379 | + "<service_listing approved=\"test\" user=\"10\" primary=\"true\" />" |
| 3380 | + "</enabled_listeners>" |
| 3381 | + "<enabled_assistants>" |
| 3382 | + "<service_listing approved=\"test\" user=\"10\" primary=\"true\" />" |
| 3383 | + "</enabled_assistants>" |
| 3384 | + "<dnd_apps>" |
| 3385 | + "<service_listing approved=\"test\" user=\"10\" primary=\"true\" />" |
| 3386 | + "</dnd_apps>" |
| 3387 | + "</notification-policy>"; |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 3388 | when(mUm.isManagedProfile(10)).thenReturn(false); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 3389 | mService.readPolicyXml( |
| 3390 | new BufferedInputStream(new ByteArrayInputStream(policyXml.getBytes())), |
| 3391 | true, |
| 3392 | 10); |
| 3393 | verify(mListeners, times(1)).readXml(any(), any(), eq(true), eq(10)); |
| 3394 | verify(mConditionProviders, times(1)).readXml(any(), any(), eq(true), eq(10)); |
| 3395 | verify(mAssistants, times(1)).readXml(any(), any(), eq(true), eq(10)); |
| 3396 | } |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 3397 | |
| 3398 | @Test |
| 3399 | public void testLocaleChangedCallsUpdateDefaultZenModeRules() throws Exception { |
| 3400 | ZenModeHelper mZenModeHelper = mock(ZenModeHelper.class); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3401 | mService.mZenModeHelper = mZenModeHelper; |
| 3402 | mService.mLocaleChangeReceiver.onReceive(mContext, |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 3403 | new Intent(Intent.ACTION_LOCALE_CHANGED)); |
| 3404 | |
| 3405 | verify(mZenModeHelper, times(1)).updateDefaultZenRules(); |
| 3406 | } |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3407 | |
| 3408 | @Test |
| 3409 | public void testBumpFGImportance_noChannelChangePreOApp() throws Exception { |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3410 | String preOPkg = PKG_N_MR1; |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3411 | final ApplicationInfo legacy = new ApplicationInfo(); |
| 3412 | legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1; |
| 3413 | when(mPackageManagerClient.getApplicationInfoAsUser(eq(preOPkg), anyInt(), anyInt())) |
| 3414 | .thenReturn(legacy); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 3415 | when(mPackageManagerClient.getPackageUidAsUser(eq(preOPkg), anyInt())) |
| 3416 | .thenReturn(Binder.getCallingUid()); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3417 | getContext().setMockPackageManager(mPackageManagerClient); |
| 3418 | |
| 3419 | Notification.Builder nb = new Notification.Builder(mContext, |
| 3420 | NotificationChannel.DEFAULT_CHANNEL_ID) |
| 3421 | .setContentTitle("foo") |
| 3422 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 3423 | .setFlag(FLAG_FOREGROUND_SERVICE, true) |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3424 | .setPriority(Notification.PRIORITY_MIN); |
| 3425 | |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 3426 | StatusBarNotification sbn = new StatusBarNotification(preOPkg, preOPkg, 9, |
| 3427 | "testBumpFGImportance_noChannelChangePreOApp", |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3428 | Binder.getCallingUid(), 0, nb.build(), new UserHandle(Binder.getCallingUid()), null, |
| 3429 | 0); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3430 | |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 3431 | mBinderService.enqueueNotificationWithTag(sbn.getPackageName(), sbn.getOpPkg(), |
| 3432 | sbn.getTag(), sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3433 | waitForIdle(); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 3434 | |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3435 | assertEquals(IMPORTANCE_LOW, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3436 | mService.getNotificationRecord(sbn.getKey()).getImportance()); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3437 | |
| 3438 | nb = new Notification.Builder(mContext) |
| 3439 | .setContentTitle("foo") |
| 3440 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 3441 | .setFlag(FLAG_FOREGROUND_SERVICE, true) |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3442 | .setPriority(Notification.PRIORITY_MIN); |
| 3443 | |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 3444 | sbn = new StatusBarNotification(preOPkg, preOPkg, 9, |
| 3445 | "testBumpFGImportance_noChannelChangePreOApp", Binder.getCallingUid(), |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 3446 | 0, nb.build(), new UserHandle(Binder.getCallingUid()), null, 0); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3447 | |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 3448 | mBinderService.enqueueNotificationWithTag(preOPkg, preOPkg, |
| 3449 | "testBumpFGImportance_noChannelChangePreOApp", |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3450 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 3451 | waitForIdle(); |
| 3452 | assertEquals(IMPORTANCE_LOW, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3453 | mService.getNotificationRecord(sbn.getKey()).getImportance()); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3454 | |
| 3455 | NotificationChannel defaultChannel = mBinderService.getNotificationChannel( |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 3456 | preOPkg, mContext.getUserId(), preOPkg, NotificationChannel.DEFAULT_CHANNEL_ID); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3457 | assertEquals(IMPORTANCE_UNSPECIFIED, defaultChannel.getImportance()); |
| 3458 | } |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3459 | |
| 3460 | @Test |
| 3461 | public void testStats_updatedOnDirectReply() throws Exception { |
| 3462 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3463 | mService.addNotification(r); |
| 3464 | |
| 3465 | mService.mNotificationDelegate.onNotificationDirectReplied(r.getKey()); |
| 3466 | assertTrue(mService.getNotificationRecord(r.getKey()).getStats().hasDirectReplied()); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3467 | verify(mAssistants).notifyAssistantNotificationDirectReplyLocked(eq(r.getSbn())); |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 3468 | |
| 3469 | assertEquals(1, mNotificationRecordLogger.numCalls()); |
| 3470 | assertEquals(NotificationRecordLogger.NotificationEvent.NOTIFICATION_DIRECT_REPLIED, |
| 3471 | mNotificationRecordLogger.event(0)); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3472 | } |
| 3473 | |
| 3474 | @Test |
Julia Reynolds | 84dc96b | 2017-11-14 09:51:01 -0500 | [diff] [blame] | 3475 | public void testStats_updatedOnUserExpansion() throws Exception { |
| 3476 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3477 | mService.addNotification(r); |
| 3478 | |
Gustav Sennton | a8e38aa | 2019-01-22 14:55:39 +0000 | [diff] [blame] | 3479 | mService.mNotificationDelegate.onNotificationExpansionChanged(r.getKey(), true, true, |
| 3480 | NOTIFICATION_LOCATION_UNKNOWN); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3481 | verify(mAssistants).notifyAssistantExpansionChangedLocked(eq(r.getSbn()), eq(true), |
| 3482 | eq((true))); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3483 | assertTrue(mService.getNotificationRecord(r.getKey()).getStats().hasExpanded()); |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 3484 | |
Gustav Sennton | a8e38aa | 2019-01-22 14:55:39 +0000 | [diff] [blame] | 3485 | mService.mNotificationDelegate.onNotificationExpansionChanged(r.getKey(), true, false, |
| 3486 | NOTIFICATION_LOCATION_UNKNOWN); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3487 | verify(mAssistants).notifyAssistantExpansionChangedLocked(eq(r.getSbn()), eq(true), |
| 3488 | eq((false))); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3489 | assertTrue(mService.getNotificationRecord(r.getKey()).getStats().hasExpanded()); |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 3490 | |
| 3491 | assertEquals(2, mNotificationRecordLogger.numCalls()); |
| 3492 | assertEquals(NotificationRecordLogger.NotificationEvent.NOTIFICATION_DETAIL_OPEN_USER, |
| 3493 | mNotificationRecordLogger.event(0)); |
| 3494 | assertEquals(NotificationRecordLogger.NotificationEvent.NOTIFICATION_DETAIL_CLOSE_USER, |
| 3495 | mNotificationRecordLogger.event(1)); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3496 | } |
| 3497 | |
| 3498 | @Test |
Julia Reynolds | 84dc96b | 2017-11-14 09:51:01 -0500 | [diff] [blame] | 3499 | public void testStats_notUpdatedOnAutoExpansion() throws Exception { |
| 3500 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3501 | mService.addNotification(r); |
| 3502 | |
Gustav Sennton | a8e38aa | 2019-01-22 14:55:39 +0000 | [diff] [blame] | 3503 | mService.mNotificationDelegate.onNotificationExpansionChanged(r.getKey(), false, true, |
| 3504 | NOTIFICATION_LOCATION_UNKNOWN); |
Julia Reynolds | 84dc96b | 2017-11-14 09:51:01 -0500 | [diff] [blame] | 3505 | assertFalse(mService.getNotificationRecord(r.getKey()).getStats().hasExpanded()); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3506 | verify(mAssistants).notifyAssistantExpansionChangedLocked(eq(r.getSbn()), eq(false), |
| 3507 | eq((true))); |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 3508 | |
Gustav Sennton | a8e38aa | 2019-01-22 14:55:39 +0000 | [diff] [blame] | 3509 | mService.mNotificationDelegate.onNotificationExpansionChanged(r.getKey(), false, false, |
| 3510 | NOTIFICATION_LOCATION_UNKNOWN); |
Julia Reynolds | 84dc96b | 2017-11-14 09:51:01 -0500 | [diff] [blame] | 3511 | assertFalse(mService.getNotificationRecord(r.getKey()).getStats().hasExpanded()); |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 3512 | verify(mAssistants).notifyAssistantExpansionChangedLocked( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3513 | eq(r.getSbn()), eq(false), eq((false))); |
Julia Reynolds | 84dc96b | 2017-11-14 09:51:01 -0500 | [diff] [blame] | 3514 | } |
| 3515 | |
| 3516 | @Test |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3517 | public void testStats_updatedOnViewSettings() throws Exception { |
| 3518 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3519 | mService.addNotification(r); |
| 3520 | |
| 3521 | mService.mNotificationDelegate.onNotificationSettingsViewed(r.getKey()); |
| 3522 | assertTrue(mService.getNotificationRecord(r.getKey()).getStats().hasViewedSettings()); |
| 3523 | } |
| 3524 | |
| 3525 | @Test |
| 3526 | public void testStats_updatedOnVisibilityChanged() throws Exception { |
| 3527 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3528 | mService.addNotification(r); |
| 3529 | |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 3530 | final NotificationVisibility nv = NotificationVisibility.obtain(r.getKey(), 1, 2, true); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3531 | mService.mNotificationDelegate.onNotificationVisibilityChanged( |
| 3532 | new NotificationVisibility[] {nv}, new NotificationVisibility[]{}); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3533 | verify(mAssistants).notifyAssistantVisibilityChangedLocked(eq(r.getSbn()), eq(true)); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3534 | assertTrue(mService.getNotificationRecord(r.getKey()).getStats().hasSeen()); |
| 3535 | mService.mNotificationDelegate.onNotificationVisibilityChanged( |
| 3536 | new NotificationVisibility[] {}, new NotificationVisibility[]{nv}); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3537 | verify(mAssistants).notifyAssistantVisibilityChangedLocked(eq(r.getSbn()), eq(false)); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3538 | assertTrue(mService.getNotificationRecord(r.getKey()).getStats().hasSeen()); |
| 3539 | } |
| 3540 | |
| 3541 | @Test |
| 3542 | public void testStats_dismissalSurface() throws Exception { |
| 3543 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
Will Brockman | 4f0f9d2 | 2020-02-23 21:18:11 -0500 | [diff] [blame] | 3544 | r.getSbn().setInstanceId(mNotificationInstanceIdSequence.newInstanceId()); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3545 | mService.addNotification(r); |
| 3546 | |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 3547 | final NotificationVisibility nv = NotificationVisibility.obtain(r.getKey(), 0, 1, true); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3548 | mService.mNotificationDelegate.onNotificationClear(mUid, 0, PKG, r.getSbn().getTag(), |
| 3549 | r.getSbn().getId(), r.getUserId(), r.getKey(), NotificationStats.DISMISSAL_AOD, |
Julia Reynolds | fd4099d | 2018-08-21 11:06:06 -0400 | [diff] [blame] | 3550 | NotificationStats.DISMISS_SENTIMENT_POSITIVE, nv); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3551 | waitForIdle(); |
| 3552 | |
| 3553 | assertEquals(NotificationStats.DISMISSAL_AOD, r.getStats().getDismissalSurface()); |
Will Brockman | 75c6057 | 2020-01-31 10:30:27 -0500 | [diff] [blame] | 3554 | |
| 3555 | // Using mService.addNotification() does not generate a NotificationRecordLogger log, |
| 3556 | // so we only get the cancel notification. |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 3557 | assertEquals(1, mNotificationRecordLogger.numCalls()); |
Will Brockman | 75c6057 | 2020-01-31 10:30:27 -0500 | [diff] [blame] | 3558 | |
Will Brockman | 75c6057 | 2020-01-31 10:30:27 -0500 | [diff] [blame] | 3559 | assertEquals( |
| 3560 | NotificationRecordLogger.NotificationCancelledEvent.NOTIFICATION_CANCEL_USER_AOD, |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 3561 | mNotificationRecordLogger.event(0)); |
Will Brockman | 4f0f9d2 | 2020-02-23 21:18:11 -0500 | [diff] [blame] | 3562 | assertEquals(1, mNotificationRecordLogger.get(0).getInstanceId()); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3563 | } |
| 3564 | |
| 3565 | @Test |
Julia Reynolds | fd4099d | 2018-08-21 11:06:06 -0400 | [diff] [blame] | 3566 | public void testStats_dismissalSentiment() throws Exception { |
| 3567 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3568 | mService.addNotification(r); |
| 3569 | |
| 3570 | final NotificationVisibility nv = NotificationVisibility.obtain(r.getKey(), 0, 1, true); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3571 | mService.mNotificationDelegate.onNotificationClear(mUid, 0, PKG, r.getSbn().getTag(), |
| 3572 | r.getSbn().getId(), r.getUserId(), r.getKey(), NotificationStats.DISMISSAL_AOD, |
Julia Reynolds | fd4099d | 2018-08-21 11:06:06 -0400 | [diff] [blame] | 3573 | NotificationStats.DISMISS_SENTIMENT_NEGATIVE, nv); |
| 3574 | waitForIdle(); |
| 3575 | |
| 3576 | assertEquals(NotificationStats.DISMISS_SENTIMENT_NEGATIVE, |
| 3577 | r.getStats().getDismissalSentiment()); |
| 3578 | } |
| 3579 | |
| 3580 | @Test |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3581 | public void testApplyAdjustmentMultiUser() throws Exception { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3582 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3583 | mService.addNotification(r); |
| 3584 | NotificationManagerService.WorkerHandler handler = mock( |
| 3585 | NotificationManagerService.WorkerHandler.class); |
| 3586 | mService.setHandler(handler); |
| 3587 | |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3588 | when(mAssistants.isSameUser(eq(null), anyInt())).thenReturn(false); |
| 3589 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3590 | Bundle signals = new Bundle(); |
| 3591 | signals.putInt(Adjustment.KEY_USER_SENTIMENT, |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3592 | USER_SENTIMENT_NEGATIVE); |
| 3593 | Adjustment adjustment = new Adjustment( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3594 | r.getSbn().getPackageName(), r.getKey(), signals, "", r.getUser().getIdentifier()); |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3595 | mBinderService.applyAdjustmentFromAssistant(null, adjustment); |
| 3596 | |
| 3597 | waitForIdle(); |
| 3598 | |
| 3599 | verify(handler, timeout(300).times(0)).scheduleSendRankingUpdate(); |
| 3600 | } |
| 3601 | |
| 3602 | @Test |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 3603 | public void testAssistantBlockingTriggersCancel() throws Exception { |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 3604 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3605 | mService.addNotification(r); |
| 3606 | NotificationManagerService.WorkerHandler handler = mock( |
| 3607 | NotificationManagerService.WorkerHandler.class); |
| 3608 | mService.setHandler(handler); |
| 3609 | |
| 3610 | Bundle signals = new Bundle(); |
Julia Reynolds | c7dcdc2 | 2019-03-25 10:26:14 -0400 | [diff] [blame] | 3611 | signals.putInt(KEY_IMPORTANCE, IMPORTANCE_NONE); |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 3612 | Adjustment adjustment = new Adjustment( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3613 | r.getSbn().getPackageName(), r.getKey(), signals, "", r.getUser().getIdentifier()); |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 3614 | when(mAssistants.isSameUser(any(), anyInt())).thenReturn(true); |
| 3615 | mBinderService.applyAdjustmentFromAssistant(null, adjustment); |
| 3616 | |
| 3617 | waitForIdle(); |
| 3618 | |
| 3619 | verify(handler, timeout(300).times(0)).scheduleSendRankingUpdate(); |
| 3620 | verify(handler, times(1)).scheduleCancelNotification(any()); |
| 3621 | } |
| 3622 | |
| 3623 | @Test |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3624 | public void testApplyEnqueuedAdjustmentFromAssistant_singleUser() throws Exception { |
| 3625 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3626 | mService.addEnqueuedNotification(r); |
| 3627 | NotificationManagerService.WorkerHandler handler = mock( |
| 3628 | NotificationManagerService.WorkerHandler.class); |
| 3629 | mService.setHandler(handler); |
| 3630 | when(mAssistants.isSameUser(eq(null), anyInt())).thenReturn(true); |
| 3631 | |
| 3632 | Bundle signals = new Bundle(); |
| 3633 | signals.putInt(Adjustment.KEY_USER_SENTIMENT, |
| 3634 | USER_SENTIMENT_NEGATIVE); |
| 3635 | Adjustment adjustment = new Adjustment( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3636 | r.getSbn().getPackageName(), r.getKey(), signals, "", r.getUser().getIdentifier()); |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3637 | mBinderService.applyEnqueuedAdjustmentFromAssistant(null, adjustment); |
| 3638 | |
| 3639 | assertEquals(USER_SENTIMENT_NEGATIVE, r.getUserSentiment()); |
| 3640 | } |
| 3641 | |
| 3642 | @Test |
Julia Reynolds | dc6adc6 | 2019-04-08 10:35:40 -0400 | [diff] [blame] | 3643 | public void testApplyEnqueuedAdjustmentFromAssistant_importance() throws Exception { |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 3644 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3645 | mService.addEnqueuedNotification(r); |
| 3646 | NotificationManagerService.WorkerHandler handler = mock( |
| 3647 | NotificationManagerService.WorkerHandler.class); |
| 3648 | mService.setHandler(handler); |
| 3649 | when(mAssistants.isSameUser(eq(null), anyInt())).thenReturn(true); |
| 3650 | |
| 3651 | Bundle signals = new Bundle(); |
Julia Reynolds | c7dcdc2 | 2019-03-25 10:26:14 -0400 | [diff] [blame] | 3652 | signals.putInt(KEY_IMPORTANCE, IMPORTANCE_LOW); |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [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 | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 3655 | mBinderService.applyEnqueuedAdjustmentFromAssistant(null, adjustment); |
| 3656 | |
| 3657 | assertEquals(IMPORTANCE_LOW, r.getImportance()); |
| 3658 | } |
| 3659 | |
| 3660 | @Test |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3661 | public void testApplyEnqueuedAdjustmentFromAssistant_crossUser() throws Exception { |
| 3662 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3663 | mService.addEnqueuedNotification(r); |
| 3664 | NotificationManagerService.WorkerHandler handler = mock( |
| 3665 | NotificationManagerService.WorkerHandler.class); |
| 3666 | mService.setHandler(handler); |
| 3667 | when(mAssistants.isSameUser(eq(null), anyInt())).thenReturn(false); |
| 3668 | |
| 3669 | Bundle signals = new Bundle(); |
| 3670 | signals.putInt(Adjustment.KEY_USER_SENTIMENT, |
| 3671 | USER_SENTIMENT_NEGATIVE); |
| 3672 | Adjustment adjustment = new Adjustment( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3673 | r.getSbn().getPackageName(), r.getKey(), signals, "", r.getUser().getIdentifier()); |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3674 | mBinderService.applyEnqueuedAdjustmentFromAssistant(null, adjustment); |
| 3675 | |
| 3676 | assertEquals(USER_SENTIMENT_NEUTRAL, r.getUserSentiment()); |
| 3677 | |
| 3678 | waitForIdle(); |
| 3679 | |
| 3680 | verify(handler, timeout(300).times(0)).scheduleSendRankingUpdate(); |
| 3681 | } |
| 3682 | |
| 3683 | @Test |
| 3684 | public void testUserSentimentChangeTriggersUpdate() throws Exception { |
| 3685 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3686 | mService.addNotification(r); |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3687 | when(mAssistants.isSameUser(eq(null), anyInt())).thenReturn(true); |
| 3688 | |
| 3689 | Bundle signals = new Bundle(); |
| 3690 | signals.putInt(Adjustment.KEY_USER_SENTIMENT, |
| 3691 | USER_SENTIMENT_NEGATIVE); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3692 | Adjustment adjustment = new Adjustment( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3693 | r.getSbn().getPackageName(), r.getKey(), signals, "", r.getUser().getIdentifier()); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3694 | mBinderService.applyAdjustmentFromAssistant(null, adjustment); |
| 3695 | |
| 3696 | waitForIdle(); |
| 3697 | |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 3698 | verify(mRankingHandler, timeout(300).times(1)).requestSort(); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3699 | } |
Julia Reynolds | 7bcb57b | 2018-01-22 10:37:58 -0500 | [diff] [blame] | 3700 | |
| 3701 | @Test |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3702 | public void testTooLateAdjustmentTriggersUpdate() throws Exception { |
| 3703 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3704 | mService.addNotification(r); |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3705 | when(mAssistants.isSameUser(eq(null), anyInt())).thenReturn(true); |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3706 | |
| 3707 | Bundle signals = new Bundle(); |
| 3708 | signals.putInt(Adjustment.KEY_USER_SENTIMENT, |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3709 | USER_SENTIMENT_NEGATIVE); |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3710 | Adjustment adjustment = new Adjustment( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3711 | r.getSbn().getPackageName(), r.getKey(), signals, "", r.getUser().getIdentifier()); |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3712 | mBinderService.applyEnqueuedAdjustmentFromAssistant(null, adjustment); |
| 3713 | |
| 3714 | waitForIdle(); |
| 3715 | |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 3716 | verify(mRankingHandler, times(1)).requestSort(); |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3717 | } |
| 3718 | |
| 3719 | @Test |
| 3720 | public void testEnqueuedAdjustmentAppliesAdjustments() throws Exception { |
| 3721 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3722 | mService.addEnqueuedNotification(r); |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3723 | when(mAssistants.isSameUser(eq(null), anyInt())).thenReturn(true); |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3724 | |
| 3725 | Bundle signals = new Bundle(); |
| 3726 | signals.putInt(Adjustment.KEY_USER_SENTIMENT, |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3727 | USER_SENTIMENT_NEGATIVE); |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3728 | Adjustment adjustment = new Adjustment( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 3729 | r.getSbn().getPackageName(), r.getKey(), signals, "", r.getUser().getIdentifier()); |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3730 | mBinderService.applyEnqueuedAdjustmentFromAssistant(null, adjustment); |
| 3731 | |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 3732 | assertEquals(USER_SENTIMENT_NEGATIVE, r.getUserSentiment()); |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3733 | } |
| 3734 | |
| 3735 | @Test |
Julia Reynolds | d78263d | 2018-01-30 10:40:41 -0500 | [diff] [blame] | 3736 | public void testRestore() throws Exception { |
| 3737 | int systemChecks = mService.countSystemChecks; |
Julia Reynolds | 57a974b | 2019-10-07 11:51:47 -0400 | [diff] [blame] | 3738 | mBinderService.applyRestore(null, USER_SYSTEM); |
Julia Reynolds | d78263d | 2018-01-30 10:40:41 -0500 | [diff] [blame] | 3739 | assertEquals(1, mService.countSystemChecks - systemChecks); |
| 3740 | } |
| 3741 | |
| 3742 | @Test |
Julia Reynolds | 469144c | 2019-06-21 14:30:28 -0400 | [diff] [blame] | 3743 | public void testBackupEmptySound() throws Exception { |
| 3744 | NotificationChannel channel = new NotificationChannel("a", "ab", IMPORTANCE_DEFAULT); |
| 3745 | channel.setSound(Uri.EMPTY, null); |
| 3746 | |
| 3747 | XmlSerializer serializer = new FastXmlSerializer(); |
| 3748 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 3749 | serializer.setOutput(new BufferedOutputStream(baos), "utf-8"); |
| 3750 | channel.writeXmlForBackup(serializer, getContext()); |
| 3751 | |
| 3752 | XmlPullParser parser = Xml.newPullParser(); |
| 3753 | parser.setInput(new BufferedInputStream( |
| 3754 | new ByteArrayInputStream(baos.toByteArray())), null); |
| 3755 | NotificationChannel restored = new NotificationChannel("a", "ab", IMPORTANCE_DEFAULT); |
| 3756 | restored.populateFromXmlForRestore(parser, getContext()); |
| 3757 | |
| 3758 | assertNull(restored.getSound()); |
| 3759 | } |
| 3760 | |
| 3761 | @Test |
Julia Reynolds | d78263d | 2018-01-30 10:40:41 -0500 | [diff] [blame] | 3762 | public void testBackup() throws Exception { |
| 3763 | int systemChecks = mService.countSystemChecks; |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 3764 | when(mListeners.queryPackageForServices(anyString(), anyInt(), anyInt())) |
| 3765 | .thenReturn(new ArraySet<>()); |
Julia Reynolds | d78263d | 2018-01-30 10:40:41 -0500 | [diff] [blame] | 3766 | mBinderService.getBackupPayload(1); |
| 3767 | assertEquals(1, mService.countSystemChecks - systemChecks); |
| 3768 | } |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3769 | |
| 3770 | @Test |
Julia Reynolds | 469144c | 2019-06-21 14:30:28 -0400 | [diff] [blame] | 3771 | public void testEmptyVibration_noException() throws Exception { |
| 3772 | NotificationChannel channel = new NotificationChannel("a", "ab", IMPORTANCE_DEFAULT); |
| 3773 | channel.setVibrationPattern(new long[0]); |
| 3774 | |
| 3775 | XmlSerializer serializer = new FastXmlSerializer(); |
| 3776 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 3777 | serializer.setOutput(new BufferedOutputStream(baos), "utf-8"); |
| 3778 | channel.writeXml(serializer); |
| 3779 | } |
| 3780 | |
| 3781 | @Test |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3782 | public void updateUriPermissions_update() throws Exception { |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3783 | NotificationChannel c = new NotificationChannel( |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 3784 | TEST_CHANNEL_ID, TEST_CHANNEL_ID, IMPORTANCE_DEFAULT); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3785 | c.setSound(null, Notification.AUDIO_ATTRIBUTES_DEFAULT); |
| 3786 | Message message1 = new Message("", 0, ""); |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3787 | message1.setData("", |
| 3788 | ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 1)); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3789 | Message message2 = new Message("", 1, ""); |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3790 | message2.setData("", |
| 3791 | ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 2)); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3792 | |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3793 | Notification.Builder nbA = new Notification.Builder(mContext, c.getId()) |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3794 | .setContentTitle("foo") |
| 3795 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 3796 | .setStyle(new Notification.MessagingStyle("") |
| 3797 | .addMessage(message1) |
| 3798 | .addMessage(message2)); |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3799 | NotificationRecord recordA = new NotificationRecord(mContext, new StatusBarNotification( |
| 3800 | PKG, PKG, 0, "tag", mUid, 0, nbA.build(), new UserHandle(mUid), null, 0), c); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3801 | |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3802 | // First post means we grant access to both |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 3803 | reset(mUgm); |
| 3804 | reset(mUgmInternal); |
| 3805 | when(mUgmInternal.newUriPermissionOwner(any())).thenReturn(new Binder()); |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3806 | mService.updateUriPermissions(recordA, null, mContext.getPackageName(), |
Julia Reynolds | 57a974b | 2019-10-07 11:51:47 -0400 | [diff] [blame] | 3807 | USER_SYSTEM); |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 3808 | verify(mUgm, times(1)).grantUriPermissionFromOwner(any(), anyInt(), any(), |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3809 | eq(message1.getDataUri()), anyInt(), anyInt(), anyInt()); |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 3810 | verify(mUgm, times(1)).grantUriPermissionFromOwner(any(), anyInt(), any(), |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3811 | eq(message2.getDataUri()), anyInt(), anyInt(), anyInt()); |
| 3812 | |
| 3813 | Notification.Builder nbB = new Notification.Builder(mContext, c.getId()) |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3814 | .setContentTitle("foo") |
| 3815 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 3816 | .setStyle(new Notification.MessagingStyle("").addMessage(message2)); |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3817 | NotificationRecord recordB = new NotificationRecord(mContext, new StatusBarNotification(PKG, |
| 3818 | PKG, 0, "tag", mUid, 0, nbB.build(), new UserHandle(mUid), null, 0), c); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3819 | |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3820 | // Update means we drop access to first |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 3821 | reset(mUgmInternal); |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3822 | mService.updateUriPermissions(recordB, recordA, mContext.getPackageName(), |
Julia Reynolds | 57a974b | 2019-10-07 11:51:47 -0400 | [diff] [blame] | 3823 | USER_SYSTEM); |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 3824 | verify(mUgmInternal, times(1)).revokeUriPermissionFromOwner(any(), |
| 3825 | eq(message1.getDataUri()), anyInt(), anyInt()); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3826 | |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3827 | // Update back means we grant access to first again |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 3828 | reset(mUgm); |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3829 | mService.updateUriPermissions(recordA, recordB, mContext.getPackageName(), |
Julia Reynolds | 57a974b | 2019-10-07 11:51:47 -0400 | [diff] [blame] | 3830 | USER_SYSTEM); |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 3831 | verify(mUgm, times(1)).grantUriPermissionFromOwner(any(), anyInt(), any(), |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3832 | eq(message1.getDataUri()), anyInt(), anyInt(), anyInt()); |
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 | // And update to empty means we drop everything |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 3835 | reset(mUgmInternal); |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3836 | mService.updateUriPermissions(null, recordB, mContext.getPackageName(), |
Julia Reynolds | 57a974b | 2019-10-07 11:51:47 -0400 | [diff] [blame] | 3837 | USER_SYSTEM); |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 3838 | verify(mUgmInternal, times(1)).revokeUriPermissionFromOwner(any(), eq(null), |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3839 | anyInt(), anyInt()); |
| 3840 | } |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 3841 | |
| 3842 | @Test |
Julia Reynolds | ea58c20 | 2020-02-03 12:52:49 -0500 | [diff] [blame] | 3843 | public void updateUriPermissions_posterDoesNotOwnUri() throws Exception { |
| 3844 | NotificationChannel c = new NotificationChannel( |
| 3845 | TEST_CHANNEL_ID, TEST_CHANNEL_ID, IMPORTANCE_DEFAULT); |
| 3846 | c.setSound(null, Notification.AUDIO_ATTRIBUTES_DEFAULT); |
| 3847 | Message message1 = new Message("", 0, ""); |
| 3848 | message1.setData("", |
| 3849 | ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 1)); |
| 3850 | |
| 3851 | Notification.Builder nbA = new Notification.Builder(mContext, c.getId()) |
| 3852 | .setContentTitle("foo") |
| 3853 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 3854 | .setStyle(new Notification.MessagingStyle("") |
| 3855 | .addMessage(message1)); |
| 3856 | NotificationRecord recordA = new NotificationRecord(mContext, new StatusBarNotification( |
| 3857 | PKG, PKG, 0, "tag", mUid, 0, nbA.build(), new UserHandle(mUid), null, 0), c); |
| 3858 | |
| 3859 | doThrow(new SecurityException("no access")).when(mUgm) |
| 3860 | .grantUriPermissionFromOwner( |
| 3861 | any(), anyInt(), any(), any(), anyInt(), anyInt(), anyInt()); |
| 3862 | |
| 3863 | when(mUgmInternal.newUriPermissionOwner(any())).thenReturn(new Binder()); |
| 3864 | mService.updateUriPermissions(recordA, null, mContext.getPackageName(), USER_SYSTEM); |
| 3865 | |
| 3866 | // yay, no crash |
| 3867 | } |
| 3868 | |
| 3869 | @Test |
Robin Lee | d107af6 | 2018-04-27 13:55:56 +0200 | [diff] [blame] | 3870 | public void testVisitUris() throws Exception { |
| 3871 | final Uri audioContents = Uri.parse("content://com.example/audio"); |
| 3872 | final Uri backgroundImage = Uri.parse("content://com.example/background"); |
| 3873 | |
| 3874 | Bundle extras = new Bundle(); |
| 3875 | extras.putParcelable(Notification.EXTRA_AUDIO_CONTENTS_URI, audioContents); |
| 3876 | extras.putString(Notification.EXTRA_BACKGROUND_IMAGE_URI, backgroundImage.toString()); |
| 3877 | |
| 3878 | Notification n = new Notification.Builder(mContext, "a") |
| 3879 | .setContentTitle("notification with uris") |
| 3880 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 3881 | .addExtras(extras) |
| 3882 | .build(); |
| 3883 | |
| 3884 | Consumer<Uri> visitor = (Consumer<Uri>) spy(Consumer.class); |
| 3885 | n.visitUris(visitor); |
| 3886 | verify(visitor, times(1)).accept(eq(audioContents)); |
| 3887 | verify(visitor, times(1)).accept(eq(backgroundImage)); |
| 3888 | } |
| 3889 | |
| 3890 | @Test |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 3891 | public void testSetNotificationPolicy_preP_setOldFields() { |
| 3892 | ZenModeHelper mZenModeHelper = mock(ZenModeHelper.class); |
| 3893 | mService.mZenModeHelper = mZenModeHelper; |
| 3894 | NotificationManager.Policy userPolicy = |
| 3895 | new NotificationManager.Policy(0, 0, 0, SUPPRESSED_EFFECT_BADGE); |
| 3896 | when(mZenModeHelper.getNotificationPolicy()).thenReturn(userPolicy); |
| 3897 | |
| 3898 | NotificationManager.Policy appPolicy = new NotificationManager.Policy(0, 0, 0, |
| 3899 | SUPPRESSED_EFFECT_SCREEN_ON | SUPPRESSED_EFFECT_SCREEN_OFF); |
| 3900 | |
| 3901 | int expected = SUPPRESSED_EFFECT_BADGE |
| 3902 | | SUPPRESSED_EFFECT_SCREEN_ON | SUPPRESSED_EFFECT_SCREEN_OFF |
Julia Reynolds | eac2da2 | 2018-04-12 10:48:46 -0400 | [diff] [blame] | 3903 | | SUPPRESSED_EFFECT_PEEK | SUPPRESSED_EFFECT_LIGHTS |
| 3904 | | SUPPRESSED_EFFECT_FULL_SCREEN_INTENT; |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 3905 | int actual = mService.calculateSuppressedVisualEffects(appPolicy, userPolicy, O_MR1); |
| 3906 | |
| 3907 | assertEquals(expected, actual); |
| 3908 | } |
| 3909 | |
| 3910 | @Test |
| 3911 | public void testSetNotificationPolicy_preP_setNewFields() { |
| 3912 | ZenModeHelper mZenModeHelper = mock(ZenModeHelper.class); |
| 3913 | mService.mZenModeHelper = mZenModeHelper; |
| 3914 | NotificationManager.Policy userPolicy = |
| 3915 | new NotificationManager.Policy(0, 0, 0, SUPPRESSED_EFFECT_BADGE); |
| 3916 | when(mZenModeHelper.getNotificationPolicy()).thenReturn(userPolicy); |
| 3917 | |
| 3918 | NotificationManager.Policy appPolicy = new NotificationManager.Policy(0, 0, 0, |
| 3919 | SUPPRESSED_EFFECT_NOTIFICATION_LIST); |
| 3920 | |
| 3921 | int expected = SUPPRESSED_EFFECT_BADGE; |
| 3922 | int actual = mService.calculateSuppressedVisualEffects(appPolicy, userPolicy, O_MR1); |
| 3923 | |
| 3924 | assertEquals(expected, actual); |
| 3925 | } |
| 3926 | |
| 3927 | @Test |
| 3928 | public void testSetNotificationPolicy_preP_setOldNewFields() { |
| 3929 | ZenModeHelper mZenModeHelper = mock(ZenModeHelper.class); |
| 3930 | mService.mZenModeHelper = mZenModeHelper; |
| 3931 | NotificationManager.Policy userPolicy = |
| 3932 | new NotificationManager.Policy(0, 0, 0, SUPPRESSED_EFFECT_BADGE); |
| 3933 | when(mZenModeHelper.getNotificationPolicy()).thenReturn(userPolicy); |
| 3934 | |
| 3935 | NotificationManager.Policy appPolicy = new NotificationManager.Policy(0, 0, 0, |
| 3936 | SUPPRESSED_EFFECT_SCREEN_ON | SUPPRESSED_EFFECT_STATUS_BAR); |
| 3937 | |
| 3938 | int expected = |
| 3939 | SUPPRESSED_EFFECT_BADGE | SUPPRESSED_EFFECT_SCREEN_ON | SUPPRESSED_EFFECT_PEEK; |
| 3940 | int actual = mService.calculateSuppressedVisualEffects(appPolicy, userPolicy, O_MR1); |
| 3941 | |
| 3942 | assertEquals(expected, actual); |
| 3943 | } |
| 3944 | |
| 3945 | @Test |
| 3946 | public void testSetNotificationPolicy_P_setOldFields() { |
| 3947 | ZenModeHelper mZenModeHelper = mock(ZenModeHelper.class); |
| 3948 | mService.mZenModeHelper = mZenModeHelper; |
| 3949 | NotificationManager.Policy userPolicy = |
| 3950 | new NotificationManager.Policy(0, 0, 0, SUPPRESSED_EFFECT_BADGE); |
| 3951 | when(mZenModeHelper.getNotificationPolicy()).thenReturn(userPolicy); |
| 3952 | |
| 3953 | NotificationManager.Policy appPolicy = new NotificationManager.Policy(0, 0, 0, |
| 3954 | SUPPRESSED_EFFECT_SCREEN_ON | SUPPRESSED_EFFECT_SCREEN_OFF); |
| 3955 | |
| 3956 | int expected = SUPPRESSED_EFFECT_SCREEN_ON | SUPPRESSED_EFFECT_SCREEN_OFF |
| 3957 | | SUPPRESSED_EFFECT_PEEK | SUPPRESSED_EFFECT_AMBIENT |
| 3958 | | SUPPRESSED_EFFECT_LIGHTS | SUPPRESSED_EFFECT_FULL_SCREEN_INTENT; |
| 3959 | int actual = mService.calculateSuppressedVisualEffects(appPolicy, userPolicy, P); |
| 3960 | |
| 3961 | assertEquals(expected, actual); |
| 3962 | } |
| 3963 | |
| 3964 | @Test |
| 3965 | public void testSetNotificationPolicy_P_setNewFields() { |
| 3966 | ZenModeHelper mZenModeHelper = mock(ZenModeHelper.class); |
| 3967 | mService.mZenModeHelper = mZenModeHelper; |
| 3968 | NotificationManager.Policy userPolicy = |
| 3969 | new NotificationManager.Policy(0, 0, 0, SUPPRESSED_EFFECT_BADGE); |
| 3970 | when(mZenModeHelper.getNotificationPolicy()).thenReturn(userPolicy); |
| 3971 | |
| 3972 | NotificationManager.Policy appPolicy = new NotificationManager.Policy(0, 0, 0, |
| 3973 | SUPPRESSED_EFFECT_NOTIFICATION_LIST | SUPPRESSED_EFFECT_AMBIENT |
| 3974 | | SUPPRESSED_EFFECT_LIGHTS | SUPPRESSED_EFFECT_FULL_SCREEN_INTENT); |
| 3975 | |
| 3976 | int expected = SUPPRESSED_EFFECT_NOTIFICATION_LIST | SUPPRESSED_EFFECT_SCREEN_OFF |
| 3977 | | SUPPRESSED_EFFECT_AMBIENT | SUPPRESSED_EFFECT_LIGHTS |
| 3978 | | SUPPRESSED_EFFECT_FULL_SCREEN_INTENT; |
| 3979 | int actual = mService.calculateSuppressedVisualEffects(appPolicy, userPolicy, P); |
| 3980 | |
| 3981 | assertEquals(expected, actual); |
| 3982 | } |
| 3983 | |
| 3984 | @Test |
| 3985 | public void testSetNotificationPolicy_P_setOldNewFields() { |
| 3986 | ZenModeHelper mZenModeHelper = mock(ZenModeHelper.class); |
| 3987 | mService.mZenModeHelper = mZenModeHelper; |
| 3988 | NotificationManager.Policy userPolicy = |
| 3989 | new NotificationManager.Policy(0, 0, 0, SUPPRESSED_EFFECT_BADGE); |
| 3990 | when(mZenModeHelper.getNotificationPolicy()).thenReturn(userPolicy); |
| 3991 | |
| 3992 | NotificationManager.Policy appPolicy = new NotificationManager.Policy(0, 0, 0, |
| 3993 | SUPPRESSED_EFFECT_SCREEN_ON | SUPPRESSED_EFFECT_STATUS_BAR); |
| 3994 | |
| 3995 | int expected = SUPPRESSED_EFFECT_STATUS_BAR; |
| 3996 | int actual = mService.calculateSuppressedVisualEffects(appPolicy, userPolicy, P); |
| 3997 | |
| 3998 | assertEquals(expected, actual); |
| 3999 | |
| 4000 | appPolicy = new NotificationManager.Policy(0, 0, 0, |
| 4001 | SUPPRESSED_EFFECT_SCREEN_ON | SUPPRESSED_EFFECT_AMBIENT |
| 4002 | | SUPPRESSED_EFFECT_LIGHTS | SUPPRESSED_EFFECT_FULL_SCREEN_INTENT); |
| 4003 | |
| 4004 | expected = SUPPRESSED_EFFECT_SCREEN_OFF | SUPPRESSED_EFFECT_AMBIENT |
| 4005 | | SUPPRESSED_EFFECT_LIGHTS | SUPPRESSED_EFFECT_FULL_SCREEN_INTENT; |
| 4006 | actual = mService.calculateSuppressedVisualEffects(appPolicy, userPolicy, P); |
| 4007 | |
| 4008 | assertEquals(expected, actual); |
| 4009 | } |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4010 | |
| 4011 | @Test |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 4012 | public void testVisualDifference_foreground() { |
| 4013 | Notification.Builder nb1 = new Notification.Builder(mContext, "") |
| 4014 | .setContentTitle("foo"); |
| 4015 | StatusBarNotification sbn1 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 4016 | nb1.build(), new UserHandle(mUid), null, 0); |
| 4017 | NotificationRecord r1 = |
| 4018 | new NotificationRecord(mContext, sbn1, mock(NotificationChannel.class)); |
| 4019 | |
| 4020 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 4021 | .setFlag(FLAG_FOREGROUND_SERVICE, true) |
| 4022 | .setContentTitle("bar"); |
| 4023 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 4024 | nb2.build(), new UserHandle(mUid), null, 0); |
| 4025 | NotificationRecord r2 = |
| 4026 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 4027 | |
| 4028 | assertFalse(mService.isVisuallyInterruptive(r1, r2)); |
| 4029 | } |
| 4030 | |
| 4031 | @Test |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4032 | public void testVisualDifference_diffTitle() { |
| 4033 | Notification.Builder nb1 = new Notification.Builder(mContext, "") |
| 4034 | .setContentTitle("foo"); |
| 4035 | StatusBarNotification sbn1 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 4036 | nb1.build(), new UserHandle(mUid), null, 0); |
| 4037 | NotificationRecord r1 = |
| 4038 | new NotificationRecord(mContext, sbn1, mock(NotificationChannel.class)); |
| 4039 | |
| 4040 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 4041 | .setContentTitle("bar"); |
| 4042 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 4043 | nb2.build(), new UserHandle(mUid), null, 0); |
| 4044 | NotificationRecord r2 = |
| 4045 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 4046 | |
| 4047 | assertTrue(mService.isVisuallyInterruptive(r1, r2)); |
| 4048 | } |
| 4049 | |
| 4050 | @Test |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4051 | public void testVisualDifference_inboxStyle() { |
| 4052 | Notification.Builder nb1 = new Notification.Builder(mContext, "") |
| 4053 | .setStyle(new Notification.InboxStyle() |
| 4054 | .addLine("line1").addLine("line2")); |
| 4055 | StatusBarNotification sbn1 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 4056 | nb1.build(), new UserHandle(mUid), null, 0); |
| 4057 | NotificationRecord r1 = |
| 4058 | new NotificationRecord(mContext, sbn1, mock(NotificationChannel.class)); |
| 4059 | |
| 4060 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 4061 | .setStyle(new Notification.InboxStyle() |
| 4062 | .addLine("line1").addLine("line2_changed")); |
| 4063 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 4064 | nb2.build(), new UserHandle(mUid), null, 0); |
| 4065 | NotificationRecord r2 = |
| 4066 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 4067 | |
| 4068 | assertTrue(mService.isVisuallyInterruptive(r1, r2)); // line 2 changed unnoticed |
| 4069 | |
| 4070 | Notification.Builder nb3 = new Notification.Builder(mContext, "") |
| 4071 | .setStyle(new Notification.InboxStyle() |
| 4072 | .addLine("line1")); |
| 4073 | StatusBarNotification sbn3 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 4074 | nb3.build(), new UserHandle(mUid), null, 0); |
| 4075 | NotificationRecord r3 = |
| 4076 | new NotificationRecord(mContext, sbn3, mock(NotificationChannel.class)); |
| 4077 | |
| 4078 | assertTrue(mService.isVisuallyInterruptive(r1, r3)); // line 2 removed unnoticed |
| 4079 | |
| 4080 | Notification.Builder nb4 = new Notification.Builder(mContext, "") |
| 4081 | .setStyle(new Notification.InboxStyle() |
| 4082 | .addLine("line1").addLine("line2").addLine("line3")); |
| 4083 | StatusBarNotification sbn4 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 4084 | nb4.build(), new UserHandle(mUid), null, 0); |
| 4085 | NotificationRecord r4 = |
| 4086 | new NotificationRecord(mContext, sbn4, mock(NotificationChannel.class)); |
| 4087 | |
| 4088 | assertTrue(mService.isVisuallyInterruptive(r1, r4)); // line 3 added unnoticed |
| 4089 | |
| 4090 | Notification.Builder nb5 = new Notification.Builder(mContext, "") |
| 4091 | .setContentText("not an inbox"); |
| 4092 | StatusBarNotification sbn5 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 4093 | nb5.build(), new UserHandle(mUid), null, 0); |
| 4094 | NotificationRecord r5 = |
| 4095 | new NotificationRecord(mContext, sbn5, mock(NotificationChannel.class)); |
| 4096 | |
| 4097 | assertTrue(mService.isVisuallyInterruptive(r1, r5)); // changed Styles, went unnoticed |
| 4098 | } |
| 4099 | |
| 4100 | @Test |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4101 | public void testVisualDifference_diffText() { |
| 4102 | Notification.Builder nb1 = new Notification.Builder(mContext, "") |
| 4103 | .setContentText("foo"); |
| 4104 | StatusBarNotification sbn1 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 4105 | nb1.build(), new UserHandle(mUid), null, 0); |
| 4106 | NotificationRecord r1 = |
| 4107 | new NotificationRecord(mContext, sbn1, mock(NotificationChannel.class)); |
| 4108 | |
| 4109 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 4110 | .setContentText("bar"); |
| 4111 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 4112 | nb2.build(), new UserHandle(mUid), null, 0); |
| 4113 | NotificationRecord r2 = |
| 4114 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 4115 | |
| 4116 | assertTrue(mService.isVisuallyInterruptive(r1, r2)); |
| 4117 | } |
| 4118 | |
| 4119 | @Test |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4120 | public void testVisualDifference_sameText() { |
| 4121 | Notification.Builder nb1 = new Notification.Builder(mContext, "") |
| 4122 | .setContentText("foo"); |
| 4123 | StatusBarNotification sbn1 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 4124 | nb1.build(), new UserHandle(mUid), null, 0); |
| 4125 | NotificationRecord r1 = |
| 4126 | new NotificationRecord(mContext, sbn1, mock(NotificationChannel.class)); |
| 4127 | |
| 4128 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 4129 | .setContentText("foo"); |
| 4130 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 4131 | nb2.build(), new UserHandle(mUid), null, 0); |
| 4132 | NotificationRecord r2 = |
| 4133 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 4134 | |
| 4135 | assertFalse(mService.isVisuallyInterruptive(r1, r2)); |
| 4136 | } |
| 4137 | |
| 4138 | @Test |
| 4139 | public void testVisualDifference_sameTextButStyled() { |
| 4140 | Notification.Builder nb1 = new Notification.Builder(mContext, "") |
| 4141 | .setContentText(Html.fromHtml("<b>foo</b>")); |
| 4142 | StatusBarNotification sbn1 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 4143 | nb1.build(), new UserHandle(mUid), null, 0); |
| 4144 | NotificationRecord r1 = |
| 4145 | new NotificationRecord(mContext, sbn1, mock(NotificationChannel.class)); |
| 4146 | |
| 4147 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 4148 | .setContentText(Html.fromHtml("<b>foo</b>")); |
| 4149 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 4150 | nb2.build(), new UserHandle(mUid), null, 0); |
| 4151 | NotificationRecord r2 = |
| 4152 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 4153 | |
| 4154 | assertFalse(mService.isVisuallyInterruptive(r1, r2)); |
| 4155 | } |
| 4156 | |
| 4157 | @Test |
| 4158 | public void testVisualDifference_diffTextButStyled() { |
| 4159 | Notification.Builder nb1 = new Notification.Builder(mContext, "") |
| 4160 | .setContentText(Html.fromHtml("<b>foo</b>")); |
| 4161 | StatusBarNotification sbn1 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 4162 | nb1.build(), new UserHandle(mUid), null, 0); |
| 4163 | NotificationRecord r1 = |
| 4164 | new NotificationRecord(mContext, sbn1, mock(NotificationChannel.class)); |
| 4165 | |
| 4166 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 4167 | .setContentText(Html.fromHtml("<b>bar</b>")); |
| 4168 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 4169 | nb2.build(), new UserHandle(mUid), null, 0); |
| 4170 | NotificationRecord r2 = |
| 4171 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 4172 | |
| 4173 | assertTrue(mService.isVisuallyInterruptive(r1, r2)); |
| 4174 | } |
| 4175 | |
| 4176 | @Test |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4177 | public void testVisualDifference_diffProgress() { |
| 4178 | Notification.Builder nb1 = new Notification.Builder(mContext, "") |
| 4179 | .setProgress(100, 90, false); |
| 4180 | StatusBarNotification sbn1 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 4181 | nb1.build(), new UserHandle(mUid), null, 0); |
| 4182 | NotificationRecord r1 = |
| 4183 | new NotificationRecord(mContext, sbn1, mock(NotificationChannel.class)); |
| 4184 | |
| 4185 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 4186 | .setProgress(100, 100, false); |
| 4187 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 4188 | nb2.build(), new UserHandle(mUid), null, 0); |
| 4189 | NotificationRecord r2 = |
| 4190 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 4191 | |
| 4192 | assertTrue(mService.isVisuallyInterruptive(r1, r2)); |
| 4193 | } |
| 4194 | |
| 4195 | @Test |
| 4196 | public void testVisualDifference_diffProgressNotDone() { |
| 4197 | Notification.Builder nb1 = new Notification.Builder(mContext, "") |
| 4198 | .setProgress(100, 90, false); |
| 4199 | StatusBarNotification sbn1 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 4200 | nb1.build(), new UserHandle(mUid), null, 0); |
| 4201 | NotificationRecord r1 = |
| 4202 | new NotificationRecord(mContext, sbn1, mock(NotificationChannel.class)); |
| 4203 | |
| 4204 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 4205 | .setProgress(100, 91, false); |
| 4206 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 4207 | nb2.build(), new UserHandle(mUid), null, 0); |
| 4208 | NotificationRecord r2 = |
| 4209 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 4210 | |
| 4211 | assertFalse(mService.isVisuallyInterruptive(r1, r2)); |
| 4212 | } |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 4213 | |
| 4214 | @Test |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4215 | public void testVisualDifference_sameProgressStillDone() { |
| 4216 | Notification.Builder nb1 = new Notification.Builder(mContext, "") |
| 4217 | .setProgress(100, 100, false); |
| 4218 | StatusBarNotification sbn1 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 4219 | nb1.build(), new UserHandle(mUid), null, 0); |
| 4220 | NotificationRecord r1 = |
| 4221 | new NotificationRecord(mContext, sbn1, mock(NotificationChannel.class)); |
| 4222 | |
| 4223 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 4224 | .setProgress(100, 100, false); |
| 4225 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 4226 | nb2.build(), new UserHandle(mUid), null, 0); |
| 4227 | NotificationRecord r2 = |
| 4228 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 4229 | |
| 4230 | assertFalse(mService.isVisuallyInterruptive(r1, r2)); |
| 4231 | } |
| 4232 | |
| 4233 | @Test |
Julia Reynolds | a4fb9da | 2018-06-04 12:27:58 -0400 | [diff] [blame] | 4234 | public void testVisualDifference_summary() { |
| 4235 | Notification.Builder nb1 = new Notification.Builder(mContext, "") |
| 4236 | .setGroup("bananas") |
| 4237 | .setFlag(Notification.FLAG_GROUP_SUMMARY, true) |
| 4238 | .setContentText("foo"); |
| 4239 | StatusBarNotification sbn1 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 4240 | nb1.build(), new UserHandle(mUid), null, 0); |
| 4241 | NotificationRecord r1 = |
| 4242 | new NotificationRecord(mContext, sbn1, mock(NotificationChannel.class)); |
| 4243 | |
| 4244 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 4245 | .setGroup("bananas") |
| 4246 | .setFlag(Notification.FLAG_GROUP_SUMMARY, true) |
| 4247 | .setContentText("bar"); |
| 4248 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 4249 | nb2.build(), new UserHandle(mUid), null, 0); |
| 4250 | NotificationRecord r2 = |
| 4251 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 4252 | |
| 4253 | assertFalse(mService.isVisuallyInterruptive(r1, r2)); |
| 4254 | } |
| 4255 | |
| 4256 | @Test |
Julia Reynolds | 760fa76 | 2018-06-19 15:39:23 -0400 | [diff] [blame] | 4257 | public void testVisualDifference_summaryNewNotification() { |
| 4258 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 4259 | .setGroup("bananas") |
| 4260 | .setFlag(Notification.FLAG_GROUP_SUMMARY, true) |
| 4261 | .setContentText("bar"); |
| 4262 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 4263 | nb2.build(), new UserHandle(mUid), null, 0); |
| 4264 | NotificationRecord r2 = |
| 4265 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 4266 | |
| 4267 | assertFalse(mService.isVisuallyInterruptive(null, r2)); |
| 4268 | } |
| 4269 | |
| 4270 | @Test |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 4271 | public void testHideAndUnhideNotificationsOnSuspendedPackageBroadcast() { |
| 4272 | // post 2 notification from this package |
| 4273 | final NotificationRecord notif1 = generateNotificationRecord( |
| 4274 | mTestNotificationChannel, 1, null, true); |
| 4275 | final NotificationRecord notif2 = generateNotificationRecord( |
| 4276 | mTestNotificationChannel, 2, null, false); |
| 4277 | mService.addNotification(notif1); |
| 4278 | mService.addNotification(notif2); |
| 4279 | |
| 4280 | // on broadcast, hide the 2 notifications |
| 4281 | mService.simulatePackageSuspendBroadcast(true, PKG); |
| 4282 | ArgumentCaptor<List> captorHide = ArgumentCaptor.forClass(List.class); |
| 4283 | verify(mListeners, times(1)).notifyHiddenLocked(captorHide.capture()); |
| 4284 | assertEquals(2, captorHide.getValue().size()); |
| 4285 | |
| 4286 | // on broadcast, unhide the 2 notifications |
| 4287 | mService.simulatePackageSuspendBroadcast(false, PKG); |
| 4288 | ArgumentCaptor<List> captorUnhide = ArgumentCaptor.forClass(List.class); |
| 4289 | verify(mListeners, times(1)).notifyUnhiddenLocked(captorUnhide.capture()); |
| 4290 | assertEquals(2, captorUnhide.getValue().size()); |
| 4291 | } |
| 4292 | |
| 4293 | @Test |
| 4294 | public void testNoNotificationsHiddenOnSuspendedPackageBroadcast() { |
| 4295 | // post 2 notification from this package |
| 4296 | final NotificationRecord notif1 = generateNotificationRecord( |
| 4297 | mTestNotificationChannel, 1, null, true); |
| 4298 | final NotificationRecord notif2 = generateNotificationRecord( |
| 4299 | mTestNotificationChannel, 2, null, false); |
| 4300 | mService.addNotification(notif1); |
| 4301 | mService.addNotification(notif2); |
| 4302 | |
| 4303 | // on broadcast, nothing is hidden since no notifications are of package "test_package" |
| 4304 | mService.simulatePackageSuspendBroadcast(true, "test_package"); |
| 4305 | ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class); |
| 4306 | verify(mListeners, times(1)).notifyHiddenLocked(captor.capture()); |
| 4307 | assertEquals(0, captor.getValue().size()); |
| 4308 | } |
Kristian Monsen | 05f3479 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 4309 | |
| 4310 | @Test |
Julia Reynolds | 0e5a343 | 2019-01-17 09:40:46 -0500 | [diff] [blame] | 4311 | public void testHideAndUnhideNotificationsOnDistractingPackageBroadcast() { |
| 4312 | // Post 2 notifications from 2 packages |
| 4313 | NotificationRecord pkgA = new NotificationRecord(mContext, |
| 4314 | generateSbn("a", 1000, 9, 0), mTestNotificationChannel); |
| 4315 | mService.addNotification(pkgA); |
| 4316 | NotificationRecord pkgB = new NotificationRecord(mContext, |
| 4317 | generateSbn("b", 1001, 9, 0), mTestNotificationChannel); |
| 4318 | mService.addNotification(pkgB); |
| 4319 | |
| 4320 | // on broadcast, hide one of the packages |
| 4321 | mService.simulatePackageDistractionBroadcast( |
| 4322 | PackageManager.RESTRICTION_HIDE_NOTIFICATIONS, new String[] {"a"}); |
| 4323 | ArgumentCaptor<List<NotificationRecord>> captorHide = ArgumentCaptor.forClass(List.class); |
| 4324 | verify(mListeners, times(1)).notifyHiddenLocked(captorHide.capture()); |
| 4325 | assertEquals(1, captorHide.getValue().size()); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 4326 | assertEquals("a", captorHide.getValue().get(0).getSbn().getPackageName()); |
Julia Reynolds | 0e5a343 | 2019-01-17 09:40:46 -0500 | [diff] [blame] | 4327 | |
| 4328 | // on broadcast, unhide the package |
| 4329 | mService.simulatePackageDistractionBroadcast( |
| 4330 | PackageManager.RESTRICTION_HIDE_FROM_SUGGESTIONS, new String[] {"a"}); |
| 4331 | ArgumentCaptor<List<NotificationRecord>> captorUnhide = ArgumentCaptor.forClass(List.class); |
| 4332 | verify(mListeners, times(1)).notifyUnhiddenLocked(captorUnhide.capture()); |
| 4333 | assertEquals(1, captorUnhide.getValue().size()); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 4334 | assertEquals("a", captorUnhide.getValue().get(0).getSbn().getPackageName()); |
Julia Reynolds | 0e5a343 | 2019-01-17 09:40:46 -0500 | [diff] [blame] | 4335 | } |
| 4336 | |
| 4337 | @Test |
| 4338 | public void testHideAndUnhideNotificationsOnDistractingPackageBroadcast_multiPkg() { |
| 4339 | // Post 2 notifications from 2 packages |
| 4340 | NotificationRecord pkgA = new NotificationRecord(mContext, |
| 4341 | generateSbn("a", 1000, 9, 0), mTestNotificationChannel); |
| 4342 | mService.addNotification(pkgA); |
| 4343 | NotificationRecord pkgB = new NotificationRecord(mContext, |
| 4344 | generateSbn("b", 1001, 9, 0), mTestNotificationChannel); |
| 4345 | mService.addNotification(pkgB); |
| 4346 | |
| 4347 | // on broadcast, hide one of the packages |
| 4348 | mService.simulatePackageDistractionBroadcast( |
| 4349 | PackageManager.RESTRICTION_HIDE_NOTIFICATIONS, new String[] {"a", "b"}); |
| 4350 | ArgumentCaptor<List<NotificationRecord>> captorHide = ArgumentCaptor.forClass(List.class); |
woongki min | bbc9728 | 2019-09-25 11:50:48 +0900 | [diff] [blame] | 4351 | |
| 4352 | // should be called only once. |
| 4353 | verify(mListeners, times(1)).notifyHiddenLocked(captorHide.capture()); |
Julia Reynolds | 0e5a343 | 2019-01-17 09:40:46 -0500 | [diff] [blame] | 4354 | assertEquals(2, captorHide.getValue().size()); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 4355 | assertEquals("a", captorHide.getValue().get(0).getSbn().getPackageName()); |
| 4356 | assertEquals("b", captorHide.getValue().get(1).getSbn().getPackageName()); |
Julia Reynolds | 0e5a343 | 2019-01-17 09:40:46 -0500 | [diff] [blame] | 4357 | |
| 4358 | // on broadcast, unhide the package |
| 4359 | mService.simulatePackageDistractionBroadcast( |
| 4360 | PackageManager.RESTRICTION_HIDE_FROM_SUGGESTIONS, new String[] {"a", "b"}); |
| 4361 | ArgumentCaptor<List<NotificationRecord>> captorUnhide = ArgumentCaptor.forClass(List.class); |
woongki min | bbc9728 | 2019-09-25 11:50:48 +0900 | [diff] [blame] | 4362 | |
| 4363 | // should be called only once. |
| 4364 | verify(mListeners, times(1)).notifyUnhiddenLocked(captorUnhide.capture()); |
Julia Reynolds | 0e5a343 | 2019-01-17 09:40:46 -0500 | [diff] [blame] | 4365 | assertEquals(2, captorUnhide.getValue().size()); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 4366 | assertEquals("a", captorUnhide.getValue().get(0).getSbn().getPackageName()); |
| 4367 | assertEquals("b", captorUnhide.getValue().get(1).getSbn().getPackageName()); |
Julia Reynolds | 0e5a343 | 2019-01-17 09:40:46 -0500 | [diff] [blame] | 4368 | } |
| 4369 | |
| 4370 | @Test |
| 4371 | public void testNoNotificationsHiddenOnDistractingPackageBroadcast() { |
| 4372 | // post notification from this package |
| 4373 | final NotificationRecord notif1 = generateNotificationRecord( |
| 4374 | mTestNotificationChannel, 1, null, true); |
| 4375 | mService.addNotification(notif1); |
| 4376 | |
| 4377 | // on broadcast, nothing is hidden since no notifications are of package "test_package" |
| 4378 | mService.simulatePackageDistractionBroadcast( |
| 4379 | PackageManager.RESTRICTION_HIDE_NOTIFICATIONS, new String[] {"test_package"}); |
| 4380 | ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class); |
| 4381 | verify(mListeners, times(1)).notifyHiddenLocked(captor.capture()); |
| 4382 | assertEquals(0, captor.getValue().size()); |
| 4383 | } |
| 4384 | |
| 4385 | @Test |
Rajeev Kumar | bc79dcd | 2020-02-04 19:54:30 -0800 | [diff] [blame] | 4386 | public void testCanUseManagedServicesNullPkg() { |
| 4387 | assertEquals(true, mService.canUseManagedServices(null, 0, null)); |
Kristian Monsen | 05f3479 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 4388 | } |
| 4389 | |
Kristian Monsen | 05f3479 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 4390 | |
| 4391 | @Test |
Rajeev Kumar | bc79dcd | 2020-02-04 19:54:30 -0800 | [diff] [blame] | 4392 | public void testCanUseManagedServicesNoValidPkg() { |
Julia Reynolds | d0ceefa | 2019-03-03 16:10:52 -0500 | [diff] [blame] | 4393 | assertEquals(true, mService.canUseManagedServices("d", 0, null)); |
| 4394 | } |
| 4395 | |
| 4396 | @Test |
| 4397 | public void testCanUseManagedServices_hasPermission() throws Exception { |
| 4398 | when(mPackageManager.checkPermission("perm", "pkg", 0)) |
| 4399 | .thenReturn(PackageManager.PERMISSION_GRANTED); |
| 4400 | |
| 4401 | assertEquals(true, mService.canUseManagedServices("pkg", 0, "perm")); |
| 4402 | } |
| 4403 | |
| 4404 | @Test |
| 4405 | public void testCanUseManagedServices_noPermission() throws Exception { |
| 4406 | when(mPackageManager.checkPermission("perm", "pkg", 0)) |
| 4407 | .thenReturn(PackageManager.PERMISSION_DENIED); |
| 4408 | |
| 4409 | assertEquals(false, mService.canUseManagedServices("pkg", 0, "perm")); |
| 4410 | } |
| 4411 | |
| 4412 | @Test |
| 4413 | public void testCanUseManagedServices_permDoesNotMatter() { |
| 4414 | assertEquals(true, mService.canUseManagedServices("pkg", 0, null)); |
Kristian Monsen | 05f3479 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 4415 | } |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 4416 | |
| 4417 | @Test |
| 4418 | public void testOnNotificationVisibilityChanged_triggersInterruptionUsageStat() { |
| 4419 | final NotificationRecord r = generateNotificationRecord( |
| 4420 | mTestNotificationChannel, 1, null, true); |
| 4421 | r.setTextChanged(true); |
| 4422 | mService.addNotification(r); |
| 4423 | |
| 4424 | mService.mNotificationDelegate.onNotificationVisibilityChanged(new NotificationVisibility[] |
| 4425 | {NotificationVisibility.obtain(r.getKey(), 1, 1, true)}, |
| 4426 | new NotificationVisibility[]{}); |
| 4427 | |
| 4428 | verify(mAppUsageStats).reportInterruptiveNotification(anyString(), anyString(), anyInt()); |
| 4429 | } |
| 4430 | |
| 4431 | @Test |
Will Brockman | d3d4933 | 2020-02-10 19:43:03 -0500 | [diff] [blame] | 4432 | public void testOnNotificationVisibilityChanged_triggersVisibilityLog() { |
| 4433 | final NotificationRecord r = generateNotificationRecord( |
| 4434 | mTestNotificationChannel, 1, null, true); |
| 4435 | r.setTextChanged(true); |
Will Brockman | 4f0f9d2 | 2020-02-23 21:18:11 -0500 | [diff] [blame] | 4436 | r.getSbn().setInstanceId(mNotificationInstanceIdSequence.newInstanceId()); |
Will Brockman | d3d4933 | 2020-02-10 19:43:03 -0500 | [diff] [blame] | 4437 | mService.addNotification(r); |
| 4438 | |
| 4439 | mService.mNotificationDelegate.onNotificationVisibilityChanged(new NotificationVisibility[] |
| 4440 | {NotificationVisibility.obtain(r.getKey(), 1, 1, true)}, |
| 4441 | new NotificationVisibility[]{}); |
| 4442 | |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 4443 | assertEquals(1, mNotificationRecordLogger.numCalls()); |
Will Brockman | d3d4933 | 2020-02-10 19:43:03 -0500 | [diff] [blame] | 4444 | assertEquals(NotificationRecordLogger.NotificationEvent.NOTIFICATION_OPEN, |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 4445 | mNotificationRecordLogger.event(0)); |
Will Brockman | 4f0f9d2 | 2020-02-23 21:18:11 -0500 | [diff] [blame] | 4446 | assertEquals(1, mNotificationRecordLogger.get(0).getInstanceId()); |
Will Brockman | d3d4933 | 2020-02-10 19:43:03 -0500 | [diff] [blame] | 4447 | |
| 4448 | mService.mNotificationDelegate.onNotificationVisibilityChanged( |
| 4449 | new NotificationVisibility[]{}, |
| 4450 | new NotificationVisibility[] |
| 4451 | {NotificationVisibility.obtain(r.getKey(), 1, 1, true)} |
| 4452 | ); |
| 4453 | |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 4454 | assertEquals(2, mNotificationRecordLogger.numCalls()); |
Will Brockman | d3d4933 | 2020-02-10 19:43:03 -0500 | [diff] [blame] | 4455 | assertEquals(NotificationRecordLogger.NotificationEvent.NOTIFICATION_CLOSE, |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 4456 | mNotificationRecordLogger.event(1)); |
Will Brockman | 4f0f9d2 | 2020-02-23 21:18:11 -0500 | [diff] [blame] | 4457 | assertEquals(1, mNotificationRecordLogger.get(1).getInstanceId()); |
Will Brockman | d3d4933 | 2020-02-10 19:43:03 -0500 | [diff] [blame] | 4458 | } |
| 4459 | |
| 4460 | @Test |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 4461 | public void testSetNotificationsShownFromListener_triggersInterruptionUsageStat() |
| 4462 | throws RemoteException { |
| 4463 | final NotificationRecord r = generateNotificationRecord( |
| 4464 | mTestNotificationChannel, 1, null, true); |
| 4465 | r.setTextChanged(true); |
| 4466 | mService.addNotification(r); |
| 4467 | |
| 4468 | mBinderService.setNotificationsShownFromListener(null, new String[] {r.getKey()}); |
| 4469 | |
| 4470 | verify(mAppUsageStats).reportInterruptiveNotification(anyString(), anyString(), anyInt()); |
| 4471 | } |
| 4472 | |
| 4473 | @Test |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 4474 | public void testMaybeRecordInterruptionLocked_doesNotRecordTwice() |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 4475 | throws RemoteException { |
| 4476 | final NotificationRecord r = generateNotificationRecord( |
| 4477 | mTestNotificationChannel, 1, null, true); |
| 4478 | r.setInterruptive(true); |
| 4479 | mService.addNotification(r); |
| 4480 | |
| 4481 | mService.maybeRecordInterruptionLocked(r); |
| 4482 | mService.maybeRecordInterruptionLocked(r); |
| 4483 | |
| 4484 | verify(mAppUsageStats, times(1)).reportInterruptiveNotification( |
| 4485 | anyString(), anyString(), anyInt()); |
Julia Reynolds | fd9f834 | 2020-03-06 09:36:00 -0500 | [diff] [blame] | 4486 | verify(mHistoryManager, times(1)).addNotification(any()); |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 4487 | } |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 4488 | |
| 4489 | @Test |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 4490 | public void testBubble() throws Exception { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 4491 | mBinderService.setBubblesAllowed(PKG, mUid, BUBBLE_PREFERENCE_NONE); |
| 4492 | assertFalse(mBinderService.areBubblesAllowed(PKG)); |
| 4493 | assertEquals(mBinderService.getBubblePreferenceForPackage(PKG, mUid), |
| 4494 | BUBBLE_PREFERENCE_NONE); |
Julia Reynolds | 33ab8a0 | 2018-12-17 16:19:52 -0500 | [diff] [blame] | 4495 | } |
| 4496 | |
| 4497 | @Test |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 4498 | public void testUserApprovedBubblesForPackageSelected() throws Exception { |
| 4499 | mBinderService.setBubblesAllowed(PKG, mUid, BUBBLE_PREFERENCE_SELECTED); |
| 4500 | assertEquals(mBinderService.getBubblePreferenceForPackage(PKG, mUid), |
| 4501 | BUBBLE_PREFERENCE_SELECTED); |
| 4502 | } |
| 4503 | |
| 4504 | @Test |
| 4505 | public void testUserApprovedBubblesForPackageAll() throws Exception { |
| 4506 | mBinderService.setBubblesAllowed(PKG, mUid, BUBBLE_PREFERENCE_ALL); |
| 4507 | assertTrue(mBinderService.areBubblesAllowed(PKG)); |
| 4508 | assertEquals(mBinderService.getBubblePreferenceForPackage(PKG, mUid), |
| 4509 | BUBBLE_PREFERENCE_ALL); |
Mady Mellor | 9db685a | 2019-01-23 13:23:37 -0800 | [diff] [blame] | 4510 | } |
| 4511 | |
| 4512 | @Test |
| 4513 | public void testUserRejectsBubblesForPackage() throws Exception { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 4514 | mBinderService.setBubblesAllowed(PKG, mUid, BUBBLE_PREFERENCE_NONE); |
| 4515 | assertFalse(mBinderService.areBubblesAllowed(PKG)); |
Mady Mellor | 9db685a | 2019-01-23 13:23:37 -0800 | [diff] [blame] | 4516 | } |
| 4517 | |
| 4518 | @Test |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 4519 | public void testIsCallerInstantApp_primaryUser() throws Exception { |
| 4520 | ApplicationInfo info = new ApplicationInfo(); |
| 4521 | info.privateFlags = ApplicationInfo.PRIVATE_FLAG_INSTANT; |
| 4522 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), eq(0))).thenReturn(info); |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 4523 | when(mPackageManager.getPackagesForUid(anyInt())).thenReturn(new String[]{"any"}); |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 4524 | |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 4525 | assertTrue(mService.isCallerInstantApp(45770, 0)); |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 4526 | |
| 4527 | info.privateFlags = 0; |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 4528 | assertFalse(mService.isCallerInstantApp(575370, 0)); |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 4529 | } |
| 4530 | |
| 4531 | @Test |
| 4532 | public void testIsCallerInstantApp_secondaryUser() throws Exception { |
| 4533 | ApplicationInfo info = new ApplicationInfo(); |
| 4534 | info.privateFlags = ApplicationInfo.PRIVATE_FLAG_INSTANT; |
| 4535 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), eq(10))).thenReturn(info); |
| 4536 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), eq(0))).thenReturn(null); |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 4537 | when(mPackageManager.getPackagesForUid(anyInt())).thenReturn(new String[]{"any"}); |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 4538 | |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 4539 | assertTrue(mService.isCallerInstantApp(68638450, 10)); |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 4540 | } |
| 4541 | |
| 4542 | @Test |
Julia Reynolds | 86869c9 | 2019-06-21 10:45:06 -0400 | [diff] [blame] | 4543 | public void testIsCallerInstantApp_userAllNotification() throws Exception { |
| 4544 | ApplicationInfo info = new ApplicationInfo(); |
| 4545 | info.privateFlags = ApplicationInfo.PRIVATE_FLAG_INSTANT; |
Julia Reynolds | 57a974b | 2019-10-07 11:51:47 -0400 | [diff] [blame] | 4546 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), eq(USER_SYSTEM))) |
Julia Reynolds | 86869c9 | 2019-06-21 10:45:06 -0400 | [diff] [blame] | 4547 | .thenReturn(info); |
| 4548 | when(mPackageManager.getPackagesForUid(anyInt())).thenReturn(new String[]{"any"}); |
| 4549 | |
| 4550 | assertTrue(mService.isCallerInstantApp(45770, UserHandle.USER_ALL)); |
| 4551 | |
| 4552 | info.privateFlags = 0; |
| 4553 | assertFalse(mService.isCallerInstantApp(575370, UserHandle.USER_ALL )); |
| 4554 | } |
| 4555 | |
| 4556 | @Test |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 4557 | public void testResolveNotificationUid_sameApp_nonSystemUser() throws Exception { |
| 4558 | ApplicationInfo info = new ApplicationInfo(); |
| 4559 | info.uid = Binder.getCallingUid(); |
| 4560 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), eq(10))).thenReturn(info); |
| 4561 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), eq(0))).thenReturn(null); |
| 4562 | |
| 4563 | int actualUid = mService.resolveNotificationUid("caller", "caller", info.uid, 10); |
| 4564 | |
| 4565 | assertEquals(info.uid, actualUid); |
| 4566 | } |
| 4567 | |
| 4568 | @Test |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 4569 | public void testResolveNotificationUid_sameApp() throws Exception { |
| 4570 | ApplicationInfo info = new ApplicationInfo(); |
| 4571 | info.uid = Binder.getCallingUid(); |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 4572 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), eq(0))).thenReturn(info); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 4573 | |
| 4574 | int actualUid = mService.resolveNotificationUid("caller", "caller", info.uid, 0); |
| 4575 | |
| 4576 | assertEquals(info.uid, actualUid); |
| 4577 | } |
| 4578 | |
| 4579 | @Test |
Julia Reynolds | ecc1b57 | 2018-10-01 16:19:24 -0400 | [diff] [blame] | 4580 | public void testResolveNotificationUid_sameAppDiffPackage() throws Exception { |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 4581 | ApplicationInfo info = new ApplicationInfo(); |
| 4582 | info.uid = Binder.getCallingUid(); |
Julia Reynolds | ecc1b57 | 2018-10-01 16:19:24 -0400 | [diff] [blame] | 4583 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), eq(0))).thenReturn(info); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 4584 | |
Julia Reynolds | ecc1b57 | 2018-10-01 16:19:24 -0400 | [diff] [blame] | 4585 | int actualUid = mService.resolveNotificationUid("caller", "callerAlso", info.uid, 0); |
| 4586 | |
| 4587 | assertEquals(info.uid, actualUid); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 4588 | } |
| 4589 | |
| 4590 | @Test |
| 4591 | public void testResolveNotificationUid_sameAppWrongUid() throws Exception { |
| 4592 | ApplicationInfo info = new ApplicationInfo(); |
| 4593 | info.uid = 1356347; |
| 4594 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), anyInt())).thenReturn(info); |
| 4595 | |
| 4596 | try { |
| 4597 | mService.resolveNotificationUid("caller", "caller", 9, 0); |
| 4598 | fail("Incorrect uid didn't throw security exception"); |
| 4599 | } catch (SecurityException e) { |
| 4600 | // yay |
| 4601 | } |
| 4602 | } |
| 4603 | |
| 4604 | @Test |
| 4605 | public void testResolveNotificationUid_delegateAllowed() throws Exception { |
| 4606 | int expectedUid = 123; |
| 4607 | |
| 4608 | when(mPackageManagerClient.getPackageUidAsUser("target", 0)).thenReturn(expectedUid); |
| 4609 | mService.setPreferencesHelper(mPreferencesHelper); |
| 4610 | when(mPreferencesHelper.isDelegateAllowed(anyString(), anyInt(), anyString(), anyInt())) |
| 4611 | .thenReturn(true); |
| 4612 | |
| 4613 | assertEquals(expectedUid, mService.resolveNotificationUid("caller", "target", 9, 0)); |
| 4614 | } |
| 4615 | |
| 4616 | @Test |
| 4617 | public void testResolveNotificationUid_androidAllowed() throws Exception { |
| 4618 | int expectedUid = 123; |
| 4619 | |
| 4620 | when(mPackageManagerClient.getPackageUidAsUser("target", 0)).thenReturn(expectedUid); |
| 4621 | // no delegate |
| 4622 | |
| 4623 | assertEquals(expectedUid, mService.resolveNotificationUid("android", "target", 0, 0)); |
| 4624 | } |
| 4625 | |
| 4626 | @Test |
Julia Reynolds | a46a769 | 2019-08-15 14:35:46 -0400 | [diff] [blame] | 4627 | public void testPostFromAndroidForNonExistentPackage() throws Exception { |
| 4628 | final String notReal = "NOT REAL"; |
| 4629 | when(mPackageManagerClient.getPackageUidAsUser(anyString(), anyInt())).thenThrow( |
| 4630 | PackageManager.NameNotFoundException.class); |
| 4631 | ApplicationInfo ai = new ApplicationInfo(); |
| 4632 | ai.uid = -1; |
| 4633 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), anyInt())).thenReturn(ai); |
| 4634 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 4635 | final StatusBarNotification sbn = generateNotificationRecord(null).getSbn(); |
Julia Reynolds | a46a769 | 2019-08-15 14:35:46 -0400 | [diff] [blame] | 4636 | try { |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 4637 | mInternalService.enqueueNotification(notReal, "android", 0, 0, |
| 4638 | "testPostFromAndroidForNonExistentPackage", |
Julia Reynolds | a46a769 | 2019-08-15 14:35:46 -0400 | [diff] [blame] | 4639 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 4640 | fail("can't post notifications for nonexistent packages, even if you exist"); |
| 4641 | } catch (SecurityException e) { |
| 4642 | // yay |
| 4643 | } |
| 4644 | } |
| 4645 | |
| 4646 | @Test |
| 4647 | public void testCancelFromAndroidForNonExistentPackage() throws Exception { |
| 4648 | final String notReal = "NOT REAL"; |
| 4649 | when(mPackageManagerClient.getPackageUidAsUser(eq(notReal), anyInt())).thenThrow( |
| 4650 | PackageManager.NameNotFoundException.class); |
| 4651 | ApplicationInfo ai = new ApplicationInfo(); |
| 4652 | ai.uid = -1; |
| 4653 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), anyInt())).thenReturn(ai); |
| 4654 | |
| 4655 | // unlike the post case, ignore instead of throwing |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 4656 | final StatusBarNotification sbn = generateNotificationRecord(null).getSbn(); |
Julia Reynolds | a46a769 | 2019-08-15 14:35:46 -0400 | [diff] [blame] | 4657 | |
| 4658 | mInternalService.cancelNotification(notReal, "android", 0, 0, "tag", |
| 4659 | sbn.getId(), sbn.getUserId()); |
| 4660 | } |
| 4661 | |
| 4662 | @Test |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 4663 | public void testResolveNotificationUid_delegateNotAllowed() throws Exception { |
| 4664 | when(mPackageManagerClient.getPackageUidAsUser("target", 0)).thenReturn(123); |
| 4665 | // no delegate |
| 4666 | |
| 4667 | try { |
| 4668 | mService.resolveNotificationUid("caller", "target", 9, 0); |
| 4669 | fail("Incorrect uid didn't throw security exception"); |
| 4670 | } catch (SecurityException e) { |
| 4671 | // yay |
| 4672 | } |
| 4673 | } |
Julia Reynolds | 564273f | 2018-09-13 15:53:11 -0400 | [diff] [blame] | 4674 | |
| 4675 | @Test |
| 4676 | public void testRemoveForegroundServiceFlagFromNotification_enqueued() { |
| 4677 | Notification n = new Notification.Builder(mContext, "").build(); |
| 4678 | n.flags |= FLAG_FOREGROUND_SERVICE; |
| 4679 | |
| 4680 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 9, null, mUid, 0, |
| 4681 | n, new UserHandle(mUid), null, 0); |
| 4682 | NotificationRecord r = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 4683 | |
| 4684 | mService.addEnqueuedNotification(r); |
| 4685 | |
| 4686 | mInternalService.removeForegroundServiceFlagFromNotification( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 4687 | PKG, r.getSbn().getId(), r.getSbn().getUserId()); |
Julia Reynolds | 564273f | 2018-09-13 15:53:11 -0400 | [diff] [blame] | 4688 | |
| 4689 | waitForIdle(); |
| 4690 | |
| 4691 | verify(mListeners, timeout(200).times(0)).notifyPostedLocked(any(), any()); |
| 4692 | } |
| 4693 | |
| 4694 | @Test |
| 4695 | public void testRemoveForegroundServiceFlagFromNotification_posted() { |
| 4696 | Notification n = new Notification.Builder(mContext, "").build(); |
| 4697 | n.flags |= FLAG_FOREGROUND_SERVICE; |
| 4698 | |
| 4699 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 9, null, mUid, 0, |
| 4700 | n, new UserHandle(mUid), null, 0); |
| 4701 | NotificationRecord r = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 4702 | |
| 4703 | mService.addNotification(r); |
| 4704 | |
| 4705 | mInternalService.removeForegroundServiceFlagFromNotification( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 4706 | PKG, r.getSbn().getId(), r.getSbn().getUserId()); |
Julia Reynolds | 564273f | 2018-09-13 15:53:11 -0400 | [diff] [blame] | 4707 | |
| 4708 | waitForIdle(); |
| 4709 | |
| 4710 | ArgumentCaptor<NotificationRecord> captor = |
| 4711 | ArgumentCaptor.forClass(NotificationRecord.class); |
| 4712 | verify(mListeners, times(1)).notifyPostedLocked(captor.capture(), any()); |
| 4713 | |
| 4714 | assertEquals(0, captor.getValue().getNotification().flags); |
| 4715 | } |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 4716 | |
| 4717 | @Test |
Bernardo Rufino | 18725b6 | 2020-01-21 14:12:43 +0000 | [diff] [blame] | 4718 | public void testAllowForegroundCustomToasts() throws Exception { |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 4719 | final String testPackage = "testPackageName"; |
| 4720 | assertEquals(0, mService.mToastQueue.size()); |
| 4721 | mService.isSystemUid = false; |
| 4722 | |
| 4723 | // package is not suspended |
| 4724 | when(mPackageManager.isPackageSuspendedForUser(testPackage, UserHandle.getUserId(mUid))) |
| 4725 | .thenReturn(false); |
| 4726 | |
| 4727 | // notifications from this package are blocked by the user |
| 4728 | mService.setPreferencesHelper(mPreferencesHelper); |
| 4729 | when(mPreferencesHelper.getImportance(testPackage, mUid)).thenReturn(IMPORTANCE_NONE); |
| 4730 | |
Bernardo Rufino | e6cb310 | 2020-03-06 20:33:11 +0000 | [diff] [blame] | 4731 | setAppInForegroundForToasts(mUid, true); |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 4732 | |
| 4733 | // enqueue toast -> toast should still enqueue |
Bernardo Rufino | 52af6db | 2019-12-20 18:26:07 +0000 | [diff] [blame] | 4734 | ((INotificationManager) mService.mService).enqueueToast(testPackage, new Binder(), |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 4735 | new TestableToastCallback(), 2000, 0); |
| 4736 | assertEquals(1, mService.mToastQueue.size()); |
| 4737 | } |
| 4738 | |
| 4739 | @Test |
Bernardo Rufino | 18725b6 | 2020-01-21 14:12:43 +0000 | [diff] [blame] | 4740 | public void testDisallowBackgroundCustomToasts() throws Exception { |
| 4741 | final String testPackage = "testPackageName"; |
| 4742 | assertEquals(0, mService.mToastQueue.size()); |
| 4743 | mService.isSystemUid = false; |
| 4744 | |
| 4745 | // package is not suspended |
| 4746 | when(mPackageManager.isPackageSuspendedForUser(testPackage, UserHandle.getUserId(mUid))) |
| 4747 | .thenReturn(false); |
| 4748 | |
Bernardo Rufino | e6cb310 | 2020-03-06 20:33:11 +0000 | [diff] [blame] | 4749 | setAppInForegroundForToasts(mUid, false); |
Bernardo Rufino | 18725b6 | 2020-01-21 14:12:43 +0000 | [diff] [blame] | 4750 | |
| 4751 | // enqueue toast -> no toasts enqueued |
| 4752 | ((INotificationManager) mService.mService).enqueueToast(testPackage, new Binder(), |
| 4753 | new TestableToastCallback(), 2000, 0); |
| 4754 | assertEquals(0, mService.mToastQueue.size()); |
| 4755 | } |
| 4756 | |
| 4757 | @Test |
| 4758 | public void testAllowForegroundTextToasts() throws Exception { |
| 4759 | final String testPackage = "testPackageName"; |
| 4760 | assertEquals(0, mService.mToastQueue.size()); |
| 4761 | mService.isSystemUid = false; |
| 4762 | |
| 4763 | // package is not suspended |
| 4764 | when(mPackageManager.isPackageSuspendedForUser(testPackage, UserHandle.getUserId(mUid))) |
| 4765 | .thenReturn(false); |
| 4766 | |
Bernardo Rufino | e6cb310 | 2020-03-06 20:33:11 +0000 | [diff] [blame] | 4767 | setAppInForegroundForToasts(mUid, true); |
Bernardo Rufino | 18725b6 | 2020-01-21 14:12:43 +0000 | [diff] [blame] | 4768 | |
| 4769 | // enqueue toast -> toast should still enqueue |
| 4770 | ((INotificationManager) mService.mService).enqueueTextToast(testPackage, new Binder(), |
| 4771 | "Text", 2000, 0, null); |
| 4772 | assertEquals(1, mService.mToastQueue.size()); |
| 4773 | } |
| 4774 | |
| 4775 | @Test |
| 4776 | public void testAllowBackgroundTextToasts() throws Exception { |
| 4777 | final String testPackage = "testPackageName"; |
| 4778 | assertEquals(0, mService.mToastQueue.size()); |
| 4779 | mService.isSystemUid = false; |
| 4780 | |
| 4781 | // package is not suspended |
| 4782 | when(mPackageManager.isPackageSuspendedForUser(testPackage, UserHandle.getUserId(mUid))) |
| 4783 | .thenReturn(false); |
| 4784 | |
Bernardo Rufino | e6cb310 | 2020-03-06 20:33:11 +0000 | [diff] [blame] | 4785 | setAppInForegroundForToasts(mUid, false); |
Bernardo Rufino | 18725b6 | 2020-01-21 14:12:43 +0000 | [diff] [blame] | 4786 | |
| 4787 | // enqueue toast -> toast should still enqueue |
| 4788 | ((INotificationManager) mService.mService).enqueueTextToast(testPackage, new Binder(), |
| 4789 | "Text", 2000, 0, null); |
| 4790 | assertEquals(1, mService.mToastQueue.size()); |
| 4791 | } |
| 4792 | |
| 4793 | @Test |
| 4794 | public void testTextToastsCallStatusBar() throws Exception { |
| 4795 | final String testPackage = "testPackageName"; |
| 4796 | assertEquals(0, mService.mToastQueue.size()); |
| 4797 | mService.isSystemUid = false; |
| 4798 | |
| 4799 | // package is not suspended |
| 4800 | when(mPackageManager.isPackageSuspendedForUser(testPackage, UserHandle.getUserId(mUid))) |
| 4801 | .thenReturn(false); |
| 4802 | |
| 4803 | // enqueue toast -> no toasts enqueued |
| 4804 | ((INotificationManager) mService.mService).enqueueTextToast(testPackage, new Binder(), |
| 4805 | "Text", 2000, 0, null); |
Bernardo Rufino | 78f5d83 | 2020-04-03 11:50:23 +0100 | [diff] [blame] | 4806 | verify(mStatusBar).showToast(anyInt(), any(), any(), any(), any(), anyInt(), any()); |
Bernardo Rufino | 18725b6 | 2020-01-21 14:12:43 +0000 | [diff] [blame] | 4807 | } |
| 4808 | |
| 4809 | @Test |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 4810 | public void testDisallowToastsFromSuspendedPackages() throws Exception { |
| 4811 | final String testPackage = "testPackageName"; |
| 4812 | assertEquals(0, mService.mToastQueue.size()); |
| 4813 | mService.isSystemUid = false; |
| 4814 | |
| 4815 | // package is suspended |
| 4816 | when(mPackageManager.isPackageSuspendedForUser(testPackage, UserHandle.getUserId(mUid))) |
| 4817 | .thenReturn(true); |
| 4818 | |
| 4819 | // notifications from this package are NOT blocked by the user |
| 4820 | mService.setPreferencesHelper(mPreferencesHelper); |
| 4821 | when(mPreferencesHelper.getImportance(testPackage, mUid)).thenReturn(IMPORTANCE_LOW); |
| 4822 | |
| 4823 | // enqueue toast -> no toasts enqueued |
Bernardo Rufino | 52af6db | 2019-12-20 18:26:07 +0000 | [diff] [blame] | 4824 | ((INotificationManager) mService.mService).enqueueToast(testPackage, new Binder(), |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 4825 | new TestableToastCallback(), 2000, 0); |
| 4826 | assertEquals(0, mService.mToastQueue.size()); |
| 4827 | } |
| 4828 | |
| 4829 | @Test |
| 4830 | public void testDisallowToastsFromBlockedApps() throws Exception { |
| 4831 | final String testPackage = "testPackageName"; |
| 4832 | assertEquals(0, mService.mToastQueue.size()); |
| 4833 | mService.isSystemUid = false; |
| 4834 | |
| 4835 | // package is not suspended |
| 4836 | when(mPackageManager.isPackageSuspendedForUser(testPackage, UserHandle.getUserId(mUid))) |
| 4837 | .thenReturn(false); |
| 4838 | |
| 4839 | // notifications from this package are blocked by the user |
| 4840 | mService.setPreferencesHelper(mPreferencesHelper); |
| 4841 | when(mPreferencesHelper.getImportance(testPackage, mUid)).thenReturn(IMPORTANCE_NONE); |
| 4842 | |
Bernardo Rufino | e6cb310 | 2020-03-06 20:33:11 +0000 | [diff] [blame] | 4843 | setAppInForegroundForToasts(mUid, false); |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 4844 | |
| 4845 | // enqueue toast -> no toasts enqueued |
Bernardo Rufino | 52af6db | 2019-12-20 18:26:07 +0000 | [diff] [blame] | 4846 | ((INotificationManager) mService.mService).enqueueToast(testPackage, new Binder(), |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 4847 | new TestableToastCallback(), 2000, 0); |
| 4848 | assertEquals(0, mService.mToastQueue.size()); |
| 4849 | } |
| 4850 | |
| 4851 | @Test |
| 4852 | public void testAlwaysAllowSystemToasts() throws Exception { |
| 4853 | final String testPackage = "testPackageName"; |
| 4854 | assertEquals(0, mService.mToastQueue.size()); |
| 4855 | mService.isSystemUid = true; |
| 4856 | |
| 4857 | // package is suspended |
| 4858 | when(mPackageManager.isPackageSuspendedForUser(testPackage, UserHandle.getUserId(mUid))) |
| 4859 | .thenReturn(true); |
| 4860 | |
| 4861 | // notifications from this package ARE blocked by the user |
| 4862 | mService.setPreferencesHelper(mPreferencesHelper); |
| 4863 | when(mPreferencesHelper.getImportance(testPackage, mUid)).thenReturn(IMPORTANCE_NONE); |
| 4864 | |
Bernardo Rufino | e6cb310 | 2020-03-06 20:33:11 +0000 | [diff] [blame] | 4865 | setAppInForegroundForToasts(mUid, false); |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 4866 | |
| 4867 | // enqueue toast -> system toast can still be 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(1, mService.mToastQueue.size()); |
| 4871 | } |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 4872 | |
Bernardo Rufino | e6cb310 | 2020-03-06 20:33:11 +0000 | [diff] [blame] | 4873 | private void setAppInForegroundForToasts(int uid, boolean inForeground) { |
| 4874 | int importance = (inForeground) ? IMPORTANCE_FOREGROUND : IMPORTANCE_NONE; |
| 4875 | when(mActivityManager.getUidImportance(mUid)).thenReturn(importance); |
| 4876 | when(mAtm.hasResumedActivity(uid)).thenReturn(inForeground); |
| 4877 | } |
| 4878 | |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 4879 | @Test |
Alex Mang | 509e554 | 2020-01-03 13:06:40 -0800 | [diff] [blame] | 4880 | public void testOnPanelRevealedAndHidden() { |
| 4881 | int items = 5; |
| 4882 | mService.mNotificationDelegate.onPanelRevealed(false, items); |
| 4883 | verify(mAssistants, times(1)).onPanelRevealed(eq(items)); |
| 4884 | |
| 4885 | mService.mNotificationDelegate.onPanelHidden(); |
| 4886 | verify(mAssistants, times(1)).onPanelHidden(); |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 4887 | |
| 4888 | assertEquals(2, mNotificationRecordLogger.numCalls()); |
| 4889 | assertEquals(NotificationRecordLogger.NotificationPanelEvent.NOTIFICATION_PANEL_OPEN, |
| 4890 | mNotificationRecordLogger.event(0)); |
| 4891 | assertEquals(NotificationRecordLogger.NotificationPanelEvent.NOTIFICATION_PANEL_CLOSE, |
| 4892 | mNotificationRecordLogger.event(1)); |
Alex Mang | 509e554 | 2020-01-03 13:06:40 -0800 | [diff] [blame] | 4893 | } |
| 4894 | |
| 4895 | @Test |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 4896 | public void testOnNotificationSmartReplySent() { |
| 4897 | final int replyIndex = 2; |
| 4898 | final String reply = "Hello"; |
Milo Sredkov | 13d8811 | 2019-02-01 12:23:24 +0000 | [diff] [blame] | 4899 | final boolean modifiedBeforeSending = true; |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 4900 | final boolean generatedByAssistant = true; |
| 4901 | |
| 4902 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
Milo Sredkov | 13d8811 | 2019-02-01 12:23:24 +0000 | [diff] [blame] | 4903 | r.setSuggestionsGeneratedByAssistant(generatedByAssistant); |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 4904 | mService.addNotification(r); |
| 4905 | |
| 4906 | mService.mNotificationDelegate.onNotificationSmartReplySent( |
Milo Sredkov | 13d8811 | 2019-02-01 12:23:24 +0000 | [diff] [blame] | 4907 | r.getKey(), replyIndex, reply, NOTIFICATION_LOCATION_UNKNOWN, |
| 4908 | modifiedBeforeSending); |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 4909 | verify(mAssistants).notifyAssistantSuggestedReplySent( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 4910 | eq(r.getSbn()), eq(reply), eq(generatedByAssistant)); |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 4911 | assertEquals(1, mNotificationRecordLogger.numCalls()); |
| 4912 | assertEquals(NotificationRecordLogger.NotificationEvent.NOTIFICATION_SMART_REPLIED, |
| 4913 | mNotificationRecordLogger.event(0)); |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 4914 | } |
Tony Mak | 7d4b3a5 | 2018-11-27 17:29:36 +0000 | [diff] [blame] | 4915 | |
| 4916 | @Test |
| 4917 | public void testOnNotificationActionClick() { |
| 4918 | final int actionIndex = 2; |
| 4919 | final Notification.Action action = |
| 4920 | new Notification.Action.Builder(null, "text", null).build(); |
| 4921 | final boolean generatedByAssistant = false; |
| 4922 | |
| 4923 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 4924 | mService.addNotification(r); |
| 4925 | |
| 4926 | NotificationVisibility notificationVisibility = |
| 4927 | NotificationVisibility.obtain(r.getKey(), 1, 2, true); |
| 4928 | mService.mNotificationDelegate.onNotificationActionClick( |
| 4929 | 10, 10, r.getKey(), actionIndex, action, notificationVisibility, |
| 4930 | generatedByAssistant); |
| 4931 | verify(mAssistants).notifyAssistantActionClicked( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 4932 | eq(r.getSbn()), eq(actionIndex), eq(action), eq(generatedByAssistant)); |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 4933 | |
| 4934 | assertEquals(1, mNotificationRecordLogger.numCalls()); |
| 4935 | assertEquals(NotificationRecordLogger.NotificationEvent.NOTIFICATION_ACTION_CLICKED, |
| 4936 | mNotificationRecordLogger.event(0)); |
Tony Mak | 7d4b3a5 | 2018-11-27 17:29:36 +0000 | [diff] [blame] | 4937 | } |
Gustav Sennton | 44dc588 | 2018-12-13 14:38:50 +0000 | [diff] [blame] | 4938 | |
| 4939 | @Test |
| 4940 | public void testLogSmartSuggestionsVisible_triggerOnExpandAndVisible() { |
| 4941 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 4942 | mService.addNotification(r); |
| 4943 | |
Gustav Sennton | a8e38aa | 2019-01-22 14:55:39 +0000 | [diff] [blame] | 4944 | mService.mNotificationDelegate.onNotificationExpansionChanged(r.getKey(), false, true, |
| 4945 | NOTIFICATION_LOCATION_UNKNOWN); |
Gustav Sennton | 44dc588 | 2018-12-13 14:38:50 +0000 | [diff] [blame] | 4946 | NotificationVisibility[] notificationVisibility = new NotificationVisibility[] { |
| 4947 | NotificationVisibility.obtain(r.getKey(), 0, 0, true) |
| 4948 | }; |
| 4949 | mService.mNotificationDelegate.onNotificationVisibilityChanged(notificationVisibility, |
| 4950 | new NotificationVisibility[0]); |
| 4951 | |
| 4952 | assertEquals(1, mService.countLogSmartSuggestionsVisible); |
| 4953 | } |
| 4954 | |
| 4955 | @Test |
| 4956 | public void testLogSmartSuggestionsVisible_noTriggerOnExpand() { |
| 4957 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 4958 | mService.addNotification(r); |
| 4959 | |
Gustav Sennton | a8e38aa | 2019-01-22 14:55:39 +0000 | [diff] [blame] | 4960 | mService.mNotificationDelegate.onNotificationExpansionChanged(r.getKey(), false, true, |
| 4961 | NOTIFICATION_LOCATION_UNKNOWN); |
Gustav Sennton | 44dc588 | 2018-12-13 14:38:50 +0000 | [diff] [blame] | 4962 | |
| 4963 | assertEquals(0, mService.countLogSmartSuggestionsVisible); |
| 4964 | } |
| 4965 | |
| 4966 | @Test |
| 4967 | public void testLogSmartSuggestionsVisible_noTriggerOnVisible() { |
| 4968 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 4969 | mService.addNotification(r); |
| 4970 | |
Julia Reynolds | 9533413 | 2018-12-19 11:15:35 -0500 | [diff] [blame] | 4971 | NotificationVisibility[] notificationVisibility = new NotificationVisibility[]{ |
Gustav Sennton | 44dc588 | 2018-12-13 14:38:50 +0000 | [diff] [blame] | 4972 | NotificationVisibility.obtain(r.getKey(), 0, 0, true) |
| 4973 | }; |
| 4974 | mService.mNotificationDelegate.onNotificationVisibilityChanged(notificationVisibility, |
| 4975 | new NotificationVisibility[0]); |
| 4976 | |
| 4977 | assertEquals(0, mService.countLogSmartSuggestionsVisible); |
| 4978 | } |
Julia Reynolds | 9533413 | 2018-12-19 11:15:35 -0500 | [diff] [blame] | 4979 | |
Colin Cross | 5ea7fb7 | 2019-12-18 17:16:36 -0800 | [diff] [blame] | 4980 | @Test |
Julia Reynolds | 9533413 | 2018-12-19 11:15:35 -0500 | [diff] [blame] | 4981 | public void testReportSeen_delegated() { |
| 4982 | Notification.Builder nb = |
| 4983 | new Notification.Builder(mContext, mTestNotificationChannel.getId()) |
| 4984 | .setContentTitle("foo") |
| 4985 | .setSmallIcon(android.R.drawable.sym_def_app_icon); |
| 4986 | |
| 4987 | StatusBarNotification sbn = new StatusBarNotification(PKG, "opPkg", 0, "tag", mUid, 0, |
| 4988 | nb.build(), new UserHandle(mUid), null, 0); |
| 4989 | NotificationRecord r = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 4990 | |
| 4991 | mService.reportSeen(r); |
| 4992 | verify(mAppUsageStats, never()).reportEvent(anyString(), anyInt(), anyInt()); |
| 4993 | |
| 4994 | } |
| 4995 | |
| 4996 | @Test |
| 4997 | public void testReportSeen_notDelegated() { |
| 4998 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 4999 | |
| 5000 | mService.reportSeen(r); |
| 5001 | verify(mAppUsageStats, times(1)).reportEvent(anyString(), anyInt(), anyInt()); |
| 5002 | } |
Julia Reynolds | 3207e2f | 2018-12-20 09:39:53 -0500 | [diff] [blame] | 5003 | |
| 5004 | @Test |
| 5005 | public void testNotificationStats_notificationError() { |
| 5006 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 5007 | mService.addNotification(r); |
| 5008 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5009 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, r.getSbn().getId(), |
| 5010 | r.getSbn().getTag(), mUid, 0, |
Julia Reynolds | 3207e2f | 2018-12-20 09:39:53 -0500 | [diff] [blame] | 5011 | new Notification.Builder(mContext, mTestNotificationChannel.getId()).build(), |
| 5012 | new UserHandle(mUid), null, 0); |
| 5013 | NotificationRecord update = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 5014 | mService.addEnqueuedNotification(update); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5015 | assertNull(update.getSbn().getNotification().getSmallIcon()); |
Julia Reynolds | 3207e2f | 2018-12-20 09:39:53 -0500 | [diff] [blame] | 5016 | |
| 5017 | NotificationManagerService.PostNotificationRunnable runnable = |
| 5018 | mService.new PostNotificationRunnable(update.getKey()); |
| 5019 | runnable.run(); |
| 5020 | waitForIdle(); |
| 5021 | |
| 5022 | ArgumentCaptor<NotificationStats> captor = ArgumentCaptor.forClass(NotificationStats.class); |
| 5023 | verify(mListeners).notifyRemovedLocked(any(), anyInt(), captor.capture()); |
| 5024 | assertNotNull(captor.getValue()); |
| 5025 | } |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 5026 | |
| 5027 | @Test |
Julia Reynolds | 7a6d07a | 2019-03-18 11:31:56 -0400 | [diff] [blame] | 5028 | public void testCanNotifyAsUser_crossUser() throws Exception { |
| 5029 | // same user no problem |
| 5030 | mBinderService.canNotifyAsPackage("src", "target", mContext.getUserId()); |
| 5031 | |
| 5032 | // cross user, no permission, problem |
| 5033 | try { |
| 5034 | mBinderService.canNotifyAsPackage("src", "target", mContext.getUserId() + 1); |
| 5035 | fail("Should not be callable cross user without cross user permission"); |
| 5036 | } catch (SecurityException e) { |
| 5037 | // good |
| 5038 | } |
| 5039 | |
| 5040 | // cross user, with permission, no problem |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 5041 | enableInteractAcrossUsers(); |
Julia Reynolds | 7a6d07a | 2019-03-18 11:31:56 -0400 | [diff] [blame] | 5042 | mBinderService.canNotifyAsPackage("src", "target", mContext.getUserId() + 1); |
| 5043 | } |
| 5044 | |
| 5045 | @Test |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 5046 | public void testgetNotificationChannels_crossUser() throws Exception { |
| 5047 | // same user no problem |
| 5048 | mBinderService.getNotificationChannels("src", "target", mContext.getUserId()); |
| 5049 | |
| 5050 | // cross user, no permission, problem |
| 5051 | try { |
| 5052 | mBinderService.getNotificationChannels("src", "target", mContext.getUserId() + 1); |
| 5053 | fail("Should not be callable cross user without cross user permission"); |
| 5054 | } catch (SecurityException e) { |
| 5055 | // good |
| 5056 | } |
| 5057 | |
| 5058 | // cross user, with permission, no problem |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 5059 | enableInteractAcrossUsers(); |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 5060 | mBinderService.getNotificationChannels("src", "target", mContext.getUserId() + 1); |
| 5061 | } |
| 5062 | |
| 5063 | @Test |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 5064 | public void setDefaultAssistantForUser_fromConfigXml() { |
| 5065 | clearDeviceConfig(); |
| 5066 | ComponentName xmlConfig = new ComponentName("config", "xml"); |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 5067 | ArraySet<ComponentName> components = new ArraySet<>(Arrays.asList(xmlConfig)); |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 5068 | when(mResources |
| 5069 | .getString( |
| 5070 | com.android.internal.R.string.config_defaultAssistantAccessComponent)) |
| 5071 | .thenReturn(xmlConfig.flattenToString()); |
| 5072 | when(mContext.getResources()).thenReturn(mResources); |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 5073 | when(mAssistants.queryPackageForServices(eq(null), anyInt(), anyInt())) |
| 5074 | .thenReturn(components); |
| 5075 | when(mAssistants.getDefaultComponents()) |
| 5076 | .thenReturn(components); |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 5077 | mService.setNotificationAssistantAccessGrantedCallback( |
| 5078 | mNotificationAssistantAccessGrantedCallback); |
| 5079 | |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 5080 | |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 5081 | mService.setDefaultAssistantForUser(0); |
| 5082 | |
| 5083 | verify(mNotificationAssistantAccessGrantedCallback) |
| 5084 | .onGranted(eq(xmlConfig), eq(0), eq(true)); |
| 5085 | } |
| 5086 | |
| 5087 | @Test |
| 5088 | public void setDefaultAssistantForUser_fromDeviceConfig() { |
| 5089 | ComponentName xmlConfig = new ComponentName("xml", "config"); |
| 5090 | ComponentName deviceConfig = new ComponentName("device", "config"); |
| 5091 | setDefaultAssistantInDeviceConfig(deviceConfig.flattenToString()); |
| 5092 | when(mResources |
| 5093 | .getString(com.android.internal.R.string.config_defaultAssistantAccessComponent)) |
| 5094 | .thenReturn(xmlConfig.flattenToString()); |
| 5095 | when(mContext.getResources()).thenReturn(mResources); |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 5096 | when(mAssistants.queryPackageForServices(eq(null), anyInt(), anyInt())) |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 5097 | .thenReturn(new ArraySet<>(Arrays.asList(xmlConfig, deviceConfig))); |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 5098 | when(mAssistants.getDefaultComponents()) |
| 5099 | .thenReturn(new ArraySet<>(Arrays.asList(deviceConfig))); |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 5100 | mService.setNotificationAssistantAccessGrantedCallback( |
| 5101 | mNotificationAssistantAccessGrantedCallback); |
| 5102 | |
| 5103 | mService.setDefaultAssistantForUser(0); |
| 5104 | |
| 5105 | verify(mNotificationAssistantAccessGrantedCallback) |
| 5106 | .onGranted(eq(deviceConfig), eq(0), eq(true)); |
| 5107 | } |
| 5108 | |
| 5109 | @Test |
| 5110 | public void setDefaultAssistantForUser_deviceConfigInvalid() { |
| 5111 | ComponentName xmlConfig = new ComponentName("xml", "config"); |
| 5112 | ComponentName deviceConfig = new ComponentName("device", "config"); |
| 5113 | setDefaultAssistantInDeviceConfig(deviceConfig.flattenToString()); |
| 5114 | when(mResources |
| 5115 | .getString(com.android.internal.R.string.config_defaultAssistantAccessComponent)) |
| 5116 | .thenReturn(xmlConfig.flattenToString()); |
| 5117 | when(mContext.getResources()).thenReturn(mResources); |
| 5118 | // Only xmlConfig is valid, deviceConfig is not. |
| 5119 | when(mAssistants.queryPackageForServices(eq(null), anyInt(), eq(0))) |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 5120 | .thenReturn(new ArraySet<>(Collections.singleton(xmlConfig))); |
| 5121 | when(mAssistants.getDefaultComponents()) |
| 5122 | .thenReturn(new ArraySet<>(Arrays.asList(xmlConfig, deviceConfig))); |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 5123 | mService.setNotificationAssistantAccessGrantedCallback( |
| 5124 | mNotificationAssistantAccessGrantedCallback); |
| 5125 | |
| 5126 | mService.setDefaultAssistantForUser(0); |
| 5127 | |
| 5128 | verify(mNotificationAssistantAccessGrantedCallback) |
| 5129 | .onGranted(eq(xmlConfig), eq(0), eq(true)); |
| 5130 | } |
| 5131 | |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 5132 | @Test |
Jay Aliomer | 4204f25 | 2019-08-26 11:36:53 -0400 | [diff] [blame] | 5133 | public void clearMultipleDefaultAssistantPackagesShouldEnableOnlyOne() throws RemoteException { |
| 5134 | ArrayMap<Boolean, ArrayList<ComponentName>> changedListeners = |
| 5135 | generateResetComponentValues(); |
| 5136 | when(mListeners.resetComponents(anyString(), anyInt())).thenReturn(changedListeners); |
| 5137 | ArrayMap<Boolean, ArrayList<ComponentName>> changes = new ArrayMap<>(); |
| 5138 | ComponentName deviceConfig1 = new ComponentName("device", "config1"); |
| 5139 | ComponentName deviceConfig2 = new ComponentName("device", "config2"); |
| 5140 | changes.put(true, new ArrayList(Arrays.asList(deviceConfig1, deviceConfig2))); |
| 5141 | changes.put(false, new ArrayList()); |
| 5142 | when(mAssistants.resetComponents(anyString(), anyInt())).thenReturn(changes); |
| 5143 | mService.getBinderService().clearData("device", 0, false); |
| 5144 | verify(mAssistants, times(1)) |
| 5145 | .setPackageOrComponentEnabled( |
| 5146 | eq("device/config2"), |
| 5147 | eq(0), eq(true), eq(false)); |
| 5148 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 5149 | eq("device"), eq(0), eq(false), eq(true)); |
| 5150 | } |
| 5151 | |
| 5152 | @Test |
| 5153 | public void clearDefaultListenersPackageShouldEnableIt() throws RemoteException { |
| 5154 | ArrayMap<Boolean, ArrayList<ComponentName>> changedAssistants = |
| 5155 | generateResetComponentValues(); |
| 5156 | when(mAssistants.resetComponents(anyString(), anyInt())).thenReturn(changedAssistants); |
| 5157 | ComponentName deviceConfig = new ComponentName("device", "config"); |
| 5158 | ArrayMap<Boolean, ArrayList<ComponentName>> changes = new ArrayMap<>(); |
| 5159 | changes.put(true, new ArrayList(Arrays.asList(deviceConfig))); |
| 5160 | changes.put(false, new ArrayList()); |
| 5161 | when(mListeners.resetComponents(anyString(), anyInt())) |
| 5162 | .thenReturn(changes); |
| 5163 | mService.getBinderService().clearData("device", 0, false); |
| 5164 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 5165 | eq("device"), eq(0), eq(false), eq(true)); |
| 5166 | } |
| 5167 | |
| 5168 | @Test |
| 5169 | public void clearDefaultDnDPackageShouldEnableIt() throws RemoteException { |
| 5170 | ComponentName deviceConfig = new ComponentName("device", "config"); |
| 5171 | ArrayMap<Boolean, ArrayList<ComponentName>> changed = generateResetComponentValues(); |
| 5172 | when(mAssistants.resetComponents(anyString(), anyInt())).thenReturn(changed); |
| 5173 | when(mListeners.resetComponents(anyString(), anyInt())).thenReturn(changed); |
| 5174 | mService.getBinderService().clearData("device", 0, false); |
| 5175 | verify(mConditionProviders, times(1)).resetPackage( |
| 5176 | eq("device"), eq(0)); |
| 5177 | } |
| 5178 | |
| 5179 | @Test |
Mady Mellor | 65dcaa9 | 2019-04-03 12:21:44 -0700 | [diff] [blame] | 5180 | public void testFlagBubble() throws RemoteException { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 5181 | setUpPrefsForBubbles(PKG, mUid, |
| 5182 | true /* global */, |
| 5183 | BUBBLE_PREFERENCE_ALL /* app */, |
| 5184 | true /* channel */); |
Mady Mellor | 65dcaa9 | 2019-04-03 12:21:44 -0700 | [diff] [blame] | 5185 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5186 | NotificationRecord nr = |
| 5187 | generateMessageBubbleNotifRecord(mTestNotificationChannel, "testFlagBubble"); |
Mady Mellor | 65dcaa9 | 2019-04-03 12:21:44 -0700 | [diff] [blame] | 5188 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5189 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 5190 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Mady Mellor | 65dcaa9 | 2019-04-03 12:21:44 -0700 | [diff] [blame] | 5191 | waitForIdle(); |
| 5192 | |
| 5193 | StatusBarNotification[] notifs = mBinderService.getActiveNotifications(PKG); |
| 5194 | assertEquals(1, notifs.length); |
| 5195 | assertTrue((notifs[0].getNotification().flags & FLAG_BUBBLE) != 0); |
| 5196 | assertTrue(mService.getNotificationRecord( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5197 | nr.getSbn().getKey()).getNotification().isBubbleNotification()); |
Mady Mellor | 65dcaa9 | 2019-04-03 12:21:44 -0700 | [diff] [blame] | 5198 | } |
| 5199 | |
| 5200 | @Test |
Mady Mellor | c682034 | 2019-05-20 12:04:36 -0700 | [diff] [blame] | 5201 | public void testFlagBubble_noFlag_appNotAllowed() throws RemoteException { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 5202 | setUpPrefsForBubbles(PKG, mUid, |
| 5203 | true /* global */, |
| 5204 | BUBBLE_PREFERENCE_NONE /* app */, |
| 5205 | true /* channel */); |
Mady Mellor | c682034 | 2019-05-20 12:04:36 -0700 | [diff] [blame] | 5206 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5207 | NotificationRecord nr = generateMessageBubbleNotifRecord(mTestNotificationChannel, |
| 5208 | "testFlagBubble_noFlag_appNotAllowed"); |
Mady Mellor | c682034 | 2019-05-20 12:04:36 -0700 | [diff] [blame] | 5209 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5210 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 5211 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Mady Mellor | c682034 | 2019-05-20 12:04:36 -0700 | [diff] [blame] | 5212 | waitForIdle(); |
| 5213 | |
| 5214 | StatusBarNotification[] notifs = mBinderService.getActiveNotifications(PKG); |
| 5215 | assertEquals(1, notifs.length); |
| 5216 | assertEquals((notifs[0].getNotification().flags & FLAG_BUBBLE), 0); |
| 5217 | assertFalse(mService.getNotificationRecord( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5218 | nr.getSbn().getKey()).getNotification().isBubbleNotification()); |
Mady Mellor | c682034 | 2019-05-20 12:04:36 -0700 | [diff] [blame] | 5219 | } |
| 5220 | |
| 5221 | @Test |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5222 | public void testFlagBubbleNotifs_noFlag_whenAppForeground() throws RemoteException { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 5223 | setUpPrefsForBubbles(PKG, mUid, |
| 5224 | true /* global */, |
| 5225 | BUBBLE_PREFERENCE_ALL /* app */, |
| 5226 | true /* channel */); |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 5227 | |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 5228 | // Notif with bubble metadata but not our other misc requirements |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5229 | Notification.Builder nb = new Notification.Builder(mContext, |
| 5230 | mTestNotificationChannel.getId()) |
| 5231 | .setContentTitle("foo") |
| 5232 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 5233 | .setBubbleMetadata(getBubbleMetadata()); |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5234 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, "tag", mUid, 0, |
| 5235 | nb.build(), new UserHandle(mUid), null, 0); |
| 5236 | NotificationRecord nr = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 5237 | |
| 5238 | // Say we're foreground |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5239 | when(mActivityManager.getPackageImportance(nr.getSbn().getPackageName())).thenReturn( |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 5240 | IMPORTANCE_FOREGROUND); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5241 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 5242 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 5243 | waitForIdle(); |
| 5244 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5245 | // if notif isn't configured properly it doesn't get to bubble just because app is |
| 5246 | // foreground. |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 5247 | assertFalse(mService.getNotificationRecord( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5248 | nr.getSbn().getKey()).getNotification().isBubbleNotification()); |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 5249 | } |
| 5250 | |
| 5251 | @Test |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 5252 | public void testFlagBubbleNotifs_flag_messaging() throws RemoteException { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 5253 | setUpPrefsForBubbles(PKG, mUid, |
| 5254 | true /* global */, |
| 5255 | BUBBLE_PREFERENCE_ALL /* app */, |
| 5256 | true /* channel */); |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 5257 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5258 | NotificationRecord nr = generateMessageBubbleNotifRecord(mTestNotificationChannel, |
| 5259 | "testFlagBubbleNotifs_flag_messaging"); |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 5260 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5261 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 5262 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 5263 | waitForIdle(); |
| 5264 | |
| 5265 | // yes allowed, yes messaging, yes bubble |
| 5266 | assertTrue(mService.getNotificationRecord( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5267 | nr.getSbn().getKey()).getNotification().isBubbleNotification()); |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 5268 | } |
| 5269 | |
| 5270 | @Test |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 5271 | public void testFlagBubbleNotifs_noFlag_messaging_appNotAllowed() throws RemoteException { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 5272 | setUpPrefsForBubbles(PKG, mUid, |
| 5273 | true /* global */, |
| 5274 | BUBBLE_PREFERENCE_NONE /* app */, |
| 5275 | true /* channel */); |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 5276 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5277 | NotificationRecord nr = generateMessageBubbleNotifRecord(mTestNotificationChannel, |
| 5278 | "testFlagBubbleNotifs_noFlag_messaging_appNotAllowed"); |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 5279 | |
| 5280 | // Post the notification |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5281 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 5282 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 5283 | waitForIdle(); |
| 5284 | |
| 5285 | // not allowed, no bubble |
| 5286 | assertFalse(mService.getNotificationRecord( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5287 | nr.getSbn().getKey()).getNotification().isBubbleNotification()); |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 5288 | } |
| 5289 | |
| 5290 | @Test |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 5291 | public void testFlagBubbleNotifs_noFlag_notBubble() 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 | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5297 | // Messaging notif WITHOUT bubble metadata |
| 5298 | Notification.Builder nb = getMessageStyleNotifBuilder(false /* addBubbleMetadata */, |
| 5299 | null /* groupKey */, false /* isSummary */); |
| 5300 | |
| 5301 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, |
| 5302 | "testFlagBubbleNotifs_noFlag_notBubble", mUid, 0, |
| 5303 | nb.build(), new UserHandle(mUid), null, 0); |
| 5304 | NotificationRecord nr = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 5305 | |
| 5306 | // Post the notification |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5307 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 5308 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 5309 | waitForIdle(); |
| 5310 | |
| 5311 | // no bubble metadata, no bubble |
| 5312 | assertFalse(mService.getNotificationRecord( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5313 | nr.getSbn().getKey()).getNotification().isBubbleNotification()); |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 5314 | } |
| 5315 | |
| 5316 | @Test |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 5317 | public void testFlagBubbleNotifs_noFlag_messaging_channelNotAllowed() throws RemoteException { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 5318 | setUpPrefsForBubbles(PKG, mUid, |
| 5319 | true /* global */, |
| 5320 | BUBBLE_PREFERENCE_ALL /* app */, |
| 5321 | false /* channel */); |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 5322 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5323 | NotificationRecord nr = generateMessageBubbleNotifRecord(mTestNotificationChannel, |
| 5324 | "testFlagBubbleNotifs_noFlag_messaging_channelNotAllowed"); |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 5325 | nr.getChannel().lockFields(USER_LOCKED_ALLOW_BUBBLE); |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 5326 | |
| 5327 | // Post the notification |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5328 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 5329 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 5330 | waitForIdle(); |
| 5331 | |
| 5332 | // channel not allowed, no bubble |
| 5333 | assertFalse(mService.getNotificationRecord( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5334 | nr.getSbn().getKey()).getNotification().isBubbleNotification()); |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 5335 | } |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 5336 | |
Mady Mellor | 49b1bf1 | 2019-03-29 12:00:02 -0700 | [diff] [blame] | 5337 | @Test |
Beverly | 9e6a764 | 2020-02-13 10:11:29 -0500 | [diff] [blame] | 5338 | public void testCancelNotificationsFromApp_cancelsBubbles() throws Exception { |
Mady Mellor | 49b1bf1 | 2019-03-29 12:00:02 -0700 | [diff] [blame] | 5339 | final NotificationRecord nrBubble = generateNotificationRecord(mTestNotificationChannel); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5340 | nrBubble.getSbn().getNotification().flags |= FLAG_BUBBLE; |
Mady Mellor | 49b1bf1 | 2019-03-29 12:00:02 -0700 | [diff] [blame] | 5341 | |
| 5342 | // Post the notification |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 5343 | mBinderService.enqueueNotificationWithTag(PKG, PKG, |
| 5344 | "testAppCancelNotifications_cancelsBubbles", |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5345 | nrBubble.getSbn().getId(), nrBubble.getSbn().getNotification(), |
| 5346 | nrBubble.getSbn().getUserId()); |
Mady Mellor | 49b1bf1 | 2019-03-29 12:00:02 -0700 | [diff] [blame] | 5347 | waitForIdle(); |
| 5348 | |
| 5349 | StatusBarNotification[] notifs = mBinderService.getActiveNotifications(PKG); |
| 5350 | assertEquals(1, notifs.length); |
| 5351 | assertEquals(1, mService.getNotificationRecordCount()); |
| 5352 | |
Julia Reynolds | b6c8374 | 2019-07-30 18:03:40 -0400 | [diff] [blame] | 5353 | mBinderService.cancelNotificationWithTag(PKG, PKG, |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5354 | "testAppCancelNotifications_cancelsBubbles", nrBubble.getSbn().getId(), |
| 5355 | nrBubble.getSbn().getUserId()); |
Mady Mellor | 49b1bf1 | 2019-03-29 12:00:02 -0700 | [diff] [blame] | 5356 | waitForIdle(); |
| 5357 | |
| 5358 | StatusBarNotification[] notifs2 = mBinderService.getActiveNotifications(PKG); |
| 5359 | assertEquals(0, notifs2.length); |
| 5360 | assertEquals(0, mService.getNotificationRecordCount()); |
| 5361 | } |
| 5362 | |
| 5363 | @Test |
Beverly | 9e6a764 | 2020-02-13 10:11:29 -0500 | [diff] [blame] | 5364 | public void testCancelAllNotificationsFromApp_cancelsBubble() throws Exception { |
| 5365 | final NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel); |
| 5366 | nr.getSbn().getNotification().flags |= FLAG_BUBBLE; |
| 5367 | mService.addNotification(nr); |
| 5368 | |
| 5369 | mBinderService.cancelAllNotifications(PKG, nr.getSbn().getUserId()); |
| 5370 | waitForIdle(); |
| 5371 | |
| 5372 | StatusBarNotification[] notifs = mBinderService.getActiveNotifications(PKG); |
| 5373 | assertEquals(0, notifs.length); |
| 5374 | assertEquals(0, mService.getNotificationRecordCount()); |
| 5375 | } |
| 5376 | |
| 5377 | @Test |
Mady Mellor | 49b1bf1 | 2019-03-29 12:00:02 -0700 | [diff] [blame] | 5378 | public void testCancelAllNotificationsFromListener_ignoresBubbles() throws Exception { |
| 5379 | final NotificationRecord nrNormal = generateNotificationRecord(mTestNotificationChannel); |
| 5380 | final NotificationRecord nrBubble = generateNotificationRecord(mTestNotificationChannel); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5381 | nrBubble.getSbn().getNotification().flags |= FLAG_BUBBLE; |
Mady Mellor | 49b1bf1 | 2019-03-29 12:00:02 -0700 | [diff] [blame] | 5382 | |
| 5383 | mService.addNotification(nrNormal); |
| 5384 | mService.addNotification(nrBubble); |
| 5385 | |
| 5386 | mService.getBinderService().cancelNotificationsFromListener(null, null); |
| 5387 | waitForIdle(); |
| 5388 | |
| 5389 | StatusBarNotification[] notifs = mBinderService.getActiveNotifications(PKG); |
| 5390 | assertEquals(1, notifs.length); |
| 5391 | assertEquals(1, mService.getNotificationRecordCount()); |
| 5392 | } |
| 5393 | |
| 5394 | @Test |
| 5395 | public void testCancelNotificationsFromListener_ignoresBubbles() throws Exception { |
| 5396 | final NotificationRecord nrNormal = generateNotificationRecord(mTestNotificationChannel); |
| 5397 | final NotificationRecord nrBubble = generateNotificationRecord(mTestNotificationChannel); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5398 | nrBubble.getSbn().getNotification().flags |= FLAG_BUBBLE; |
Mady Mellor | 49b1bf1 | 2019-03-29 12:00:02 -0700 | [diff] [blame] | 5399 | |
| 5400 | mService.addNotification(nrNormal); |
| 5401 | mService.addNotification(nrBubble); |
| 5402 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5403 | String[] keys = {nrNormal.getSbn().getKey(), nrBubble.getSbn().getKey()}; |
Mady Mellor | 49b1bf1 | 2019-03-29 12:00:02 -0700 | [diff] [blame] | 5404 | mService.getBinderService().cancelNotificationsFromListener(null, keys); |
| 5405 | waitForIdle(); |
| 5406 | |
| 5407 | StatusBarNotification[] notifs = mBinderService.getActiveNotifications(PKG); |
| 5408 | assertEquals(1, notifs.length); |
| 5409 | assertEquals(1, mService.getNotificationRecordCount()); |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 5410 | |
| 5411 | assertEquals(1, mNotificationRecordLogger.numCalls()); |
| 5412 | assertEquals(NotificationRecordLogger.NotificationCancelledEvent |
| 5413 | .NOTIFICATION_CANCEL_LISTENER_CANCEL, mNotificationRecordLogger.event(0)); |
Mady Mellor | 49b1bf1 | 2019-03-29 12:00:02 -0700 | [diff] [blame] | 5414 | } |
Julia Reynolds | ad6dd35 | 2019-03-07 16:46:22 -0500 | [diff] [blame] | 5415 | |
Julia Reynolds | 4214da9 | 2019-04-10 15:04:06 -0400 | [diff] [blame] | 5416 | @Test |
Beverly | 9e6a764 | 2020-02-13 10:11:29 -0500 | [diff] [blame] | 5417 | public void testCancelAllNotificationsFromStatusBar_ignoresBubble() throws Exception { |
| 5418 | // GIVEN a notification bubble |
| 5419 | final NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel); |
| 5420 | nr.getSbn().getNotification().flags |= FLAG_BUBBLE; |
| 5421 | mService.addNotification(nr); |
| 5422 | |
| 5423 | // WHEN the status bar clears all notifications |
| 5424 | mService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(), |
| 5425 | nr.getSbn().getUserId()); |
| 5426 | waitForIdle(); |
| 5427 | |
| 5428 | // THEN the bubble notification does not get removed |
| 5429 | StatusBarNotification[] notifs = mBinderService.getActiveNotifications(PKG); |
| 5430 | assertEquals(1, notifs.length); |
| 5431 | assertEquals(1, mService.getNotificationRecordCount()); |
| 5432 | } |
| 5433 | |
| 5434 | |
| 5435 | @Test |
Julia Reynolds | 088c448 | 2019-04-10 12:43:27 -0400 | [diff] [blame] | 5436 | public void testGetAllowedAssistantAdjustments() throws Exception { |
| 5437 | List<String> capabilities = mBinderService.getAllowedAssistantAdjustments(null); |
Julia Reynolds | ad6dd35 | 2019-03-07 16:46:22 -0500 | [diff] [blame] | 5438 | assertNotNull(capabilities); |
| 5439 | |
| 5440 | for (int i = capabilities.size() - 1; i >= 0; i--) { |
| 5441 | String capability = capabilities.get(i); |
Julia Reynolds | 088c448 | 2019-04-10 12:43:27 -0400 | [diff] [blame] | 5442 | mBinderService.disallowAssistantAdjustment(capability); |
| 5443 | assertEquals(i + 1, mBinderService.getAllowedAssistantAdjustments(null).size()); |
| 5444 | List<String> currentCapabilities = mBinderService.getAllowedAssistantAdjustments(null); |
Julia Reynolds | ad6dd35 | 2019-03-07 16:46:22 -0500 | [diff] [blame] | 5445 | assertNotNull(currentCapabilities); |
| 5446 | assertFalse(currentCapabilities.contains(capability)); |
| 5447 | } |
| 5448 | } |
Julia Reynolds | c7dcdc2 | 2019-03-25 10:26:14 -0400 | [diff] [blame] | 5449 | |
Julia Reynolds | 4214da9 | 2019-04-10 15:04:06 -0400 | [diff] [blame] | 5450 | @Test |
Julia Reynolds | c7dcdc2 | 2019-03-25 10:26:14 -0400 | [diff] [blame] | 5451 | public void testAdjustRestrictedKey() throws Exception { |
| 5452 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
Julia Reynolds | 4214da9 | 2019-04-10 15:04:06 -0400 | [diff] [blame] | 5453 | mService.addNotification(r); |
| 5454 | when(mAssistants.isSameUser(any(), anyInt())).thenReturn(true); |
Julia Reynolds | c7dcdc2 | 2019-03-25 10:26:14 -0400 | [diff] [blame] | 5455 | |
| 5456 | when(mAssistants.isAdjustmentAllowed(KEY_IMPORTANCE)).thenReturn(true); |
| 5457 | when(mAssistants.isAdjustmentAllowed(KEY_USER_SENTIMENT)).thenReturn(false); |
| 5458 | |
| 5459 | Bundle signals = new Bundle(); |
| 5460 | signals.putInt(KEY_IMPORTANCE, IMPORTANCE_LOW); |
| 5461 | signals.putInt(KEY_USER_SENTIMENT, USER_SENTIMENT_NEGATIVE); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5462 | Adjustment adjustment = new Adjustment(r.getSbn().getPackageName(), r.getKey(), signals, |
Julia Reynolds | c7dcdc2 | 2019-03-25 10:26:14 -0400 | [diff] [blame] | 5463 | "", r.getUser().getIdentifier()); |
| 5464 | |
| 5465 | mBinderService.applyAdjustmentFromAssistant(null, adjustment); |
| 5466 | r.applyAdjustments(); |
| 5467 | |
| 5468 | assertEquals(IMPORTANCE_LOW, r.getAssistantImportance()); |
| 5469 | assertEquals(USER_SENTIMENT_NEUTRAL, r.getUserSentiment()); |
| 5470 | } |
Julia Reynolds | 657d164 | 2019-03-27 12:15:57 -0400 | [diff] [blame] | 5471 | |
Julia Reynolds | 4214da9 | 2019-04-10 15:04:06 -0400 | [diff] [blame] | 5472 | @Test |
Julia Reynolds | a94365d | 2019-04-09 10:48:43 -0400 | [diff] [blame] | 5473 | public void testAutomaticZenRuleValidation_policyFilterAgreement() throws Exception { |
Julia Reynolds | 4214da9 | 2019-04-10 15:04:06 -0400 | [diff] [blame] | 5474 | when(mConditionProviders.isPackageOrComponentAllowed(anyString(), anyInt())) |
| 5475 | .thenReturn(true); |
| 5476 | mService.setZenHelper(mock(ZenModeHelper.class)); |
| 5477 | ComponentName owner = new ComponentName(mContext, this.getClass()); |
Julia Reynolds | a94365d | 2019-04-09 10:48:43 -0400 | [diff] [blame] | 5478 | ZenPolicy zenPolicy = new ZenPolicy.Builder().allowAlarms(true).build(); |
| 5479 | boolean isEnabled = true; |
| 5480 | AutomaticZenRule rule = new AutomaticZenRule("test", owner, owner, mock(Uri.class), |
| 5481 | zenPolicy, NotificationManager.INTERRUPTION_FILTER_NONE, isEnabled); |
| 5482 | |
| 5483 | try { |
| 5484 | mBinderService.addAutomaticZenRule(rule); |
Julia Reynolds | 4214da9 | 2019-04-10 15:04:06 -0400 | [diff] [blame] | 5485 | fail("Zen policy only applies to priority only mode"); |
Julia Reynolds | a94365d | 2019-04-09 10:48:43 -0400 | [diff] [blame] | 5486 | } catch (IllegalArgumentException e) { |
| 5487 | // yay |
| 5488 | } |
| 5489 | |
| 5490 | rule = new AutomaticZenRule("test", owner, owner, mock(Uri.class), |
| 5491 | zenPolicy, NotificationManager.INTERRUPTION_FILTER_PRIORITY, isEnabled); |
| 5492 | mBinderService.addAutomaticZenRule(rule); |
| 5493 | |
| 5494 | rule = new AutomaticZenRule("test", owner, owner, mock(Uri.class), |
| 5495 | null, NotificationManager.INTERRUPTION_FILTER_NONE, isEnabled); |
| 5496 | mBinderService.addAutomaticZenRule(rule); |
| 5497 | } |
| 5498 | |
Julia Reynolds | 4214da9 | 2019-04-10 15:04:06 -0400 | [diff] [blame] | 5499 | @Test |
Julia Reynolds | 657d164 | 2019-03-27 12:15:57 -0400 | [diff] [blame] | 5500 | public void testAreNotificationsEnabledForPackage_crossUser() throws Exception { |
| 5501 | try { |
| 5502 | mBinderService.areNotificationsEnabledForPackage(mContext.getPackageName(), |
| 5503 | mUid + UserHandle.PER_USER_RANGE); |
| 5504 | fail("Cannot call cross user without permission"); |
| 5505 | } catch (SecurityException e) { |
| 5506 | // pass |
| 5507 | } |
| 5508 | |
| 5509 | // cross user, with permission, no problem |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 5510 | enableInteractAcrossUsers(); |
Julia Reynolds | 657d164 | 2019-03-27 12:15:57 -0400 | [diff] [blame] | 5511 | mBinderService.areNotificationsEnabledForPackage(mContext.getPackageName(), |
| 5512 | mUid + UserHandle.PER_USER_RANGE); |
| 5513 | } |
Julia Reynolds | 2f7592d | 2019-03-27 12:17:23 -0400 | [diff] [blame] | 5514 | |
Julia Reynolds | 4214da9 | 2019-04-10 15:04:06 -0400 | [diff] [blame] | 5515 | @Test |
Julia Reynolds | 2f7592d | 2019-03-27 12:17:23 -0400 | [diff] [blame] | 5516 | public void testAreBubblesAllowedForPackage_crossUser() throws Exception { |
| 5517 | try { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 5518 | mBinderService.getBubblePreferenceForPackage(mContext.getPackageName(), |
Julia Reynolds | 2f7592d | 2019-03-27 12:17:23 -0400 | [diff] [blame] | 5519 | mUid + UserHandle.PER_USER_RANGE); |
| 5520 | fail("Cannot call cross user without permission"); |
| 5521 | } catch (SecurityException e) { |
| 5522 | // pass |
| 5523 | } |
| 5524 | |
| 5525 | // cross user, with permission, no problem |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 5526 | enableInteractAcrossUsers(); |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 5527 | mBinderService.getBubblePreferenceForPackage(mContext.getPackageName(), |
Julia Reynolds | 2f7592d | 2019-03-27 12:17:23 -0400 | [diff] [blame] | 5528 | mUid + UserHandle.PER_USER_RANGE); |
| 5529 | } |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5530 | |
Will Brockman | cfd9830 | 2020-01-29 15:57:30 -0500 | [diff] [blame] | 5531 | private void enableInteractAcrossUsers() { |
| 5532 | TestablePermissions perms = mContext.getTestablePermissions(); |
| 5533 | perms.setPermission(android.Manifest.permission.INTERACT_ACROSS_USERS, PERMISSION_GRANTED); |
| 5534 | } |
| 5535 | |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5536 | @Test |
| 5537 | public void testNotificationBubbleChanged_false() throws Exception { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 5538 | setUpPrefsForBubbles(PKG, mUid, |
| 5539 | true /* global */, |
| 5540 | BUBBLE_PREFERENCE_ALL /* app */, |
| 5541 | true /* channel */); |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5542 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5543 | // Notif with bubble metadata |
| 5544 | NotificationRecord nr = generateMessageBubbleNotifRecord(mTestNotificationChannel, |
| 5545 | "testNotificationBubbleChanged_false"); |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5546 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5547 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 5548 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5549 | waitForIdle(); |
| 5550 | |
Mady Mellor | 66efd5e | 2019-05-15 13:38:11 -0700 | [diff] [blame] | 5551 | // Reset as this is called when the notif is first sent |
| 5552 | reset(mListeners); |
| 5553 | |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5554 | // First we were a bubble |
| 5555 | StatusBarNotification[] notifsBefore = mBinderService.getActiveNotifications(PKG); |
| 5556 | assertEquals(1, notifsBefore.length); |
| 5557 | assertTrue((notifsBefore[0].getNotification().flags & FLAG_BUBBLE) != 0); |
| 5558 | |
| 5559 | // Notify we're not a bubble |
Mady Mellor | 9adfe6a | 2020-03-30 17:23:26 -0700 | [diff] [blame] | 5560 | mService.mNotificationDelegate.onNotificationBubbleChanged(nr.getKey(), false, 0); |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5561 | waitForIdle(); |
| 5562 | |
Mady Mellor | 3a0a1b4 | 2019-05-23 06:40:21 -0700 | [diff] [blame] | 5563 | // Make sure we are not a bubble |
| 5564 | StatusBarNotification[] notifsAfter = mBinderService.getActiveNotifications(PKG); |
| 5565 | assertEquals(1, notifsAfter.length); |
| 5566 | assertEquals((notifsAfter[0].getNotification().flags & FLAG_BUBBLE), 0); |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5567 | } |
| 5568 | |
| 5569 | @Test |
| 5570 | public void testNotificationBubbleChanged_true() throws Exception { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 5571 | setUpPrefsForBubbles(PKG, mUid, |
| 5572 | true /* global */, |
| 5573 | BUBBLE_PREFERENCE_ALL /* app */, |
| 5574 | true /* channel */); |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5575 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5576 | // Notif that is not a bubble |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5577 | NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel, |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5578 | 1, null, false); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5579 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 5580 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5581 | waitForIdle(); |
| 5582 | |
| 5583 | // Would be a normal notification because wouldn't have met requirements to bubble |
| 5584 | StatusBarNotification[] notifsBefore = mBinderService.getActiveNotifications(PKG); |
| 5585 | assertEquals(1, notifsBefore.length); |
| 5586 | assertEquals((notifsBefore[0].getNotification().flags & FLAG_BUBBLE), 0); |
| 5587 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5588 | // Update the notification to be message style / meet bubble requirements |
| 5589 | NotificationRecord nr2 = generateMessageBubbleNotifRecord(mTestNotificationChannel, |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5590 | nr.getSbn().getTag()); |
| 5591 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr2.getSbn().getTag(), |
| 5592 | nr2.getSbn().getId(), nr2.getSbn().getNotification(), nr2.getSbn().getUserId()); |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5593 | waitForIdle(); |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5594 | |
Mady Mellor | 66efd5e | 2019-05-15 13:38:11 -0700 | [diff] [blame] | 5595 | // Reset as this is called when the notif is first sent |
| 5596 | reset(mListeners); |
| 5597 | |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5598 | // Notify we are now a bubble |
Mady Mellor | 9adfe6a | 2020-03-30 17:23:26 -0700 | [diff] [blame] | 5599 | mService.mNotificationDelegate.onNotificationBubbleChanged(nr.getKey(), true, 0); |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5600 | waitForIdle(); |
| 5601 | |
Mady Mellor | 3a0a1b4 | 2019-05-23 06:40:21 -0700 | [diff] [blame] | 5602 | // Make sure we are a bubble |
| 5603 | StatusBarNotification[] notifsAfter = mBinderService.getActiveNotifications(PKG); |
| 5604 | assertEquals(1, notifsAfter.length); |
| 5605 | assertTrue((notifsAfter[0].getNotification().flags & FLAG_BUBBLE) != 0); |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5606 | } |
| 5607 | |
| 5608 | @Test |
| 5609 | public void testNotificationBubbleChanged_true_notAllowed() throws Exception { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 5610 | setUpPrefsForBubbles(PKG, mUid, |
| 5611 | true /* global */, |
| 5612 | BUBBLE_PREFERENCE_ALL /* app */, |
| 5613 | true /* channel */); |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5614 | |
| 5615 | // Notif that is not a bubble |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5616 | NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5617 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 5618 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5619 | waitForIdle(); |
| 5620 | |
Mady Mellor | 66efd5e | 2019-05-15 13:38:11 -0700 | [diff] [blame] | 5621 | // Reset as this is called when the notif is first sent |
| 5622 | reset(mListeners); |
| 5623 | |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5624 | // Would be a normal notification because wouldn't have met requirements to bubble |
| 5625 | StatusBarNotification[] notifsBefore = mBinderService.getActiveNotifications(PKG); |
| 5626 | assertEquals(1, notifsBefore.length); |
| 5627 | assertEquals((notifsBefore[0].getNotification().flags & FLAG_BUBBLE), 0); |
| 5628 | |
| 5629 | // Notify we are now a bubble |
Mady Mellor | 9adfe6a | 2020-03-30 17:23:26 -0700 | [diff] [blame] | 5630 | mService.mNotificationDelegate.onNotificationBubbleChanged(nr.getKey(), true, 0); |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5631 | waitForIdle(); |
| 5632 | |
| 5633 | // We still wouldn't be a bubble because the notification didn't meet requirements |
| 5634 | StatusBarNotification[] notifsAfter = mBinderService.getActiveNotifications(PKG); |
| 5635 | assertEquals(1, notifsAfter.length); |
| 5636 | assertEquals((notifsAfter[0].getNotification().flags & FLAG_BUBBLE), 0); |
| 5637 | } |
Mady Mellor | 5c11a2e | 2019-04-25 17:26:15 -0700 | [diff] [blame] | 5638 | |
| 5639 | @Test |
Mady Mellor | 13f9bc8 | 2020-03-24 19:09:28 -0700 | [diff] [blame] | 5640 | public void testNotificationBubbleIsFlagRemoved_resetOnUpdate() throws Exception { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 5641 | setUpPrefsForBubbles(PKG, mUid, |
| 5642 | true /* global */, |
| 5643 | BUBBLE_PREFERENCE_ALL /* app */, |
| 5644 | true /* channel */); |
| 5645 | |
Mady Mellor | 13f9bc8 | 2020-03-24 19:09:28 -0700 | [diff] [blame] | 5646 | // Notif with bubble metadata |
| 5647 | NotificationRecord nr = generateMessageBubbleNotifRecord(mTestNotificationChannel, |
| 5648 | "testNotificationBubbleIsFlagRemoved_resetOnUpdate"); |
| 5649 | |
| 5650 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 5651 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
| 5652 | waitForIdle(); |
| 5653 | // Flag shouldn't be modified |
| 5654 | NotificationRecord recordToCheck = mService.getNotificationRecord(nr.getSbn().getKey()); |
| 5655 | assertFalse(recordToCheck.isFlagBubbleRemoved()); |
| 5656 | |
| 5657 | // Notify we're not a bubble |
Mady Mellor | 9adfe6a | 2020-03-30 17:23:26 -0700 | [diff] [blame] | 5658 | mService.mNotificationDelegate.onNotificationBubbleChanged(nr.getKey(), false, 0); |
Mady Mellor | 13f9bc8 | 2020-03-24 19:09:28 -0700 | [diff] [blame] | 5659 | waitForIdle(); |
| 5660 | // Flag should be modified |
| 5661 | recordToCheck = mService.getNotificationRecord(nr.getSbn().getKey()); |
| 5662 | assertTrue(recordToCheck.isFlagBubbleRemoved()); |
| 5663 | |
| 5664 | |
| 5665 | // Update the notif |
| 5666 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 5667 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
| 5668 | waitForIdle(); |
| 5669 | // And the flag is reset |
| 5670 | recordToCheck = mService.getNotificationRecord(nr.getSbn().getKey()); |
| 5671 | assertFalse(recordToCheck.isFlagBubbleRemoved()); |
| 5672 | } |
| 5673 | |
| 5674 | @Test |
| 5675 | public void testNotificationBubbleIsFlagRemoved_resetOnBubbleChangedTrue() throws Exception { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 5676 | setUpPrefsForBubbles(PKG, mUid, |
| 5677 | true /* global */, |
| 5678 | BUBBLE_PREFERENCE_ALL /* app */, |
| 5679 | true /* channel */); |
| 5680 | |
Mady Mellor | 13f9bc8 | 2020-03-24 19:09:28 -0700 | [diff] [blame] | 5681 | // Notif with bubble metadata |
| 5682 | NotificationRecord nr = generateMessageBubbleNotifRecord(mTestNotificationChannel, |
| 5683 | "testNotificationBubbleIsFlagRemoved_trueOnBubbleChangedTrue"); |
| 5684 | |
| 5685 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 5686 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
| 5687 | waitForIdle(); |
| 5688 | // Flag shouldn't be modified |
| 5689 | NotificationRecord recordToCheck = mService.getNotificationRecord(nr.getSbn().getKey()); |
| 5690 | assertFalse(recordToCheck.isFlagBubbleRemoved()); |
| 5691 | |
| 5692 | // Notify we're not a bubble |
Mady Mellor | 9adfe6a | 2020-03-30 17:23:26 -0700 | [diff] [blame] | 5693 | mService.mNotificationDelegate.onNotificationBubbleChanged(nr.getKey(), false, 0); |
Mady Mellor | 13f9bc8 | 2020-03-24 19:09:28 -0700 | [diff] [blame] | 5694 | waitForIdle(); |
| 5695 | // Flag should be modified |
| 5696 | recordToCheck = mService.getNotificationRecord(nr.getSbn().getKey()); |
| 5697 | assertTrue(recordToCheck.isFlagBubbleRemoved()); |
| 5698 | |
| 5699 | // Notify we are a bubble |
Mady Mellor | 9adfe6a | 2020-03-30 17:23:26 -0700 | [diff] [blame] | 5700 | mService.mNotificationDelegate.onNotificationBubbleChanged(nr.getKey(), true, 0); |
Mady Mellor | 13f9bc8 | 2020-03-24 19:09:28 -0700 | [diff] [blame] | 5701 | waitForIdle(); |
| 5702 | // And the flag is reset |
| 5703 | assertFalse(recordToCheck.isFlagBubbleRemoved()); |
| 5704 | } |
| 5705 | |
| 5706 | @Test |
Mady Mellor | f44b683 | 2020-01-14 13:26:14 -0800 | [diff] [blame] | 5707 | public void testOnBubbleNotificationSuppressionChanged() throws Exception { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 5708 | setUpPrefsForBubbles(PKG, mUid, |
| 5709 | true /* global */, |
| 5710 | BUBBLE_PREFERENCE_ALL /* app */, |
| 5711 | true /* channel */); |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 5712 | |
Mady Mellor | f44b683 | 2020-01-14 13:26:14 -0800 | [diff] [blame] | 5713 | // Bubble notification |
| 5714 | NotificationRecord nr = generateMessageBubbleNotifRecord(mTestNotificationChannel, "tag"); |
| 5715 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5716 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 5717 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Mady Mellor | f44b683 | 2020-01-14 13:26:14 -0800 | [diff] [blame] | 5718 | waitForIdle(); |
| 5719 | |
| 5720 | // NOT suppressed |
| 5721 | Notification n = mBinderService.getActiveNotifications(PKG)[0].getNotification(); |
| 5722 | assertFalse(n.getBubbleMetadata().isNotificationSuppressed()); |
| 5723 | |
| 5724 | // Reset as this is called when the notif is first sent |
| 5725 | reset(mListeners); |
| 5726 | |
| 5727 | // Test: update suppression to true |
| 5728 | mService.mNotificationDelegate.onBubbleNotificationSuppressionChanged(nr.getKey(), true); |
| 5729 | waitForIdle(); |
| 5730 | |
| 5731 | // Check |
| 5732 | n = mBinderService.getActiveNotifications(PKG)[0].getNotification(); |
| 5733 | assertTrue(n.getBubbleMetadata().isNotificationSuppressed()); |
| 5734 | |
| 5735 | // Reset to check again |
| 5736 | reset(mListeners); |
| 5737 | |
| 5738 | // Test: update suppression to false |
| 5739 | mService.mNotificationDelegate.onBubbleNotificationSuppressionChanged(nr.getKey(), false); |
| 5740 | waitForIdle(); |
| 5741 | |
| 5742 | // Check |
| 5743 | n = mBinderService.getActiveNotifications(PKG)[0].getNotification(); |
| 5744 | assertFalse(n.getBubbleMetadata().isNotificationSuppressed()); |
| 5745 | } |
| 5746 | |
| 5747 | @Test |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 5748 | public void testGrantInlineReplyUriPermission_recordExists() throws Exception { |
| 5749 | NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel, 0); |
| 5750 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5751 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 5752 | waitForIdle(); |
| 5753 | |
| 5754 | // A notification exists for the given record |
| 5755 | StatusBarNotification[] notifsBefore = mBinderService.getActiveNotifications(PKG); |
| 5756 | assertEquals(1, notifsBefore.length); |
| 5757 | |
| 5758 | reset(mPackageManager); |
| 5759 | |
| 5760 | Uri uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 1); |
| 5761 | |
| 5762 | mService.mNotificationDelegate.grantInlineReplyUriPermission( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5763 | nr.getKey(), uri, nr.getSbn().getUser(), nr.getSbn().getPackageName(), |
| 5764 | nr.getSbn().getUid()); |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 5765 | |
| 5766 | // Grant permission called for the UID of SystemUI under the target user ID |
| 5767 | verify(mUgm, times(1)).grantUriPermissionFromOwner(any(), |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5768 | eq(nr.getSbn().getUid()), eq(nr.getSbn().getPackageName()), eq(uri), anyInt(), |
| 5769 | anyInt(), eq(nr.getSbn().getUserId())); |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 5770 | } |
| 5771 | |
| 5772 | @Test |
Aran Ink | 979c976 | 2019-10-24 16:09:45 -0400 | [diff] [blame] | 5773 | public void testGrantInlineReplyUriPermission_noRecordExists() throws Exception { |
| 5774 | NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel, 0); |
| 5775 | waitForIdle(); |
| 5776 | |
| 5777 | // No notifications exist for the given record |
| 5778 | StatusBarNotification[] notifsBefore = mBinderService.getActiveNotifications(PKG); |
| 5779 | assertEquals(0, notifsBefore.length); |
| 5780 | |
| 5781 | Uri uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 1); |
| 5782 | int uid = 0; // sysui on primary user |
| 5783 | |
| 5784 | mService.mNotificationDelegate.grantInlineReplyUriPermission( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5785 | nr.getKey(), uri, nr.getSbn().getUser(), nr.getSbn().getPackageName(), |
| 5786 | nr.getSbn().getUid()); |
Aran Ink | 979c976 | 2019-10-24 16:09:45 -0400 | [diff] [blame] | 5787 | |
| 5788 | // Grant permission still called if no NotificationRecord exists for the given key |
| 5789 | verify(mUgm, times(1)).grantUriPermissionFromOwner(any(), |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5790 | eq(nr.getSbn().getUid()), eq(nr.getSbn().getPackageName()), eq(uri), anyInt(), |
| 5791 | anyInt(), eq(nr.getSbn().getUserId())); |
Aran Ink | 979c976 | 2019-10-24 16:09:45 -0400 | [diff] [blame] | 5792 | } |
| 5793 | |
| 5794 | @Test |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 5795 | public void testGrantInlineReplyUriPermission_userAll() throws Exception { |
| 5796 | // generate a NotificationRecord for USER_ALL to make sure it's converted into USER_SYSTEM |
| 5797 | NotificationRecord nr = |
| 5798 | generateNotificationRecord(mTestNotificationChannel, UserHandle.USER_ALL); |
| 5799 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5800 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 5801 | waitForIdle(); |
| 5802 | |
| 5803 | // A notification exists for the given record |
| 5804 | StatusBarNotification[] notifsBefore = mBinderService.getActiveNotifications(PKG); |
| 5805 | assertEquals(1, notifsBefore.length); |
| 5806 | |
| 5807 | reset(mPackageManager); |
| 5808 | |
| 5809 | Uri uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 1); |
| 5810 | |
| 5811 | mService.mNotificationDelegate.grantInlineReplyUriPermission( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5812 | nr.getKey(), uri, nr.getSbn().getUser(), nr.getSbn().getPackageName(), |
| 5813 | nr.getSbn().getUid()); |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 5814 | |
| 5815 | // Target user for the grant is USER_ALL instead of USER_SYSTEM |
| 5816 | verify(mUgm, times(1)).grantUriPermissionFromOwner(any(), |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5817 | eq(nr.getSbn().getUid()), eq(nr.getSbn().getPackageName()), eq(uri), anyInt(), |
| 5818 | anyInt(), eq(UserHandle.USER_SYSTEM)); |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 5819 | } |
| 5820 | |
| 5821 | @Test |
| 5822 | public void testGrantInlineReplyUriPermission_acrossUsers() throws Exception { |
| 5823 | // generate a NotificationRecord for USER_ALL to make sure it's converted into USER_SYSTEM |
| 5824 | int otherUserId = 11; |
| 5825 | NotificationRecord nr = |
| 5826 | generateNotificationRecord(mTestNotificationChannel, otherUserId); |
| 5827 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5828 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 5829 | waitForIdle(); |
| 5830 | |
| 5831 | // A notification exists for the given record |
| 5832 | StatusBarNotification[] notifsBefore = mBinderService.getActiveNotifications(PKG); |
| 5833 | assertEquals(1, notifsBefore.length); |
| 5834 | |
| 5835 | reset(mPackageManager); |
| 5836 | |
| 5837 | Uri uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 1); |
| 5838 | |
| 5839 | int uid = 0; // sysui on primary user |
Aran Ink | 979c976 | 2019-10-24 16:09:45 -0400 | [diff] [blame] | 5840 | int otherUserUid = (otherUserId * 100000) + 1; // sysui as a different user |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 5841 | String sysuiPackage = "sysui"; |
| 5842 | final String[] sysuiPackages = new String[] { sysuiPackage }; |
| 5843 | when(mPackageManager.getPackagesForUid(uid)).thenReturn(sysuiPackages); |
| 5844 | |
| 5845 | // Make sure to mock call for USER_SYSTEM and not USER_ALL, since it's been replaced by the |
| 5846 | // time this is called |
| 5847 | when(mPackageManager.getPackageUid(sysuiPackage, 0, otherUserId)) |
| 5848 | .thenReturn(otherUserUid); |
| 5849 | |
Aran Ink | 979c976 | 2019-10-24 16:09:45 -0400 | [diff] [blame] | 5850 | mService.mNotificationDelegate.grantInlineReplyUriPermission( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5851 | nr.getKey(), uri, nr.getSbn().getUser(), nr.getSbn().getPackageName(), uid); |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 5852 | |
| 5853 | // Target user for the grant is USER_ALL instead of USER_SYSTEM |
| 5854 | verify(mUgm, times(1)).grantUriPermissionFromOwner(any(), |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5855 | eq(otherUserUid), eq(nr.getSbn().getPackageName()), eq(uri), anyInt(), anyInt(), |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 5856 | eq(otherUserId)); |
| 5857 | } |
| 5858 | |
| 5859 | @Test |
Aran Ink | 979c976 | 2019-10-24 16:09:45 -0400 | [diff] [blame] | 5860 | public void testClearInlineReplyUriPermission_uriRecordExists() throws Exception { |
| 5861 | NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel, 0); |
| 5862 | reset(mPackageManager); |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 5863 | |
Aran Ink | 979c976 | 2019-10-24 16:09:45 -0400 | [diff] [blame] | 5864 | Uri uri1 = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 1); |
| 5865 | Uri uri2 = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 2); |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 5866 | |
Aran Ink | 979c976 | 2019-10-24 16:09:45 -0400 | [diff] [blame] | 5867 | // create an inline record with two uris in it |
| 5868 | mService.mNotificationDelegate.grantInlineReplyUriPermission( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5869 | nr.getKey(), uri1, nr.getSbn().getUser(), nr.getSbn().getPackageName(), |
| 5870 | nr.getSbn().getUid()); |
Aran Ink | 979c976 | 2019-10-24 16:09:45 -0400 | [diff] [blame] | 5871 | mService.mNotificationDelegate.grantInlineReplyUriPermission( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5872 | nr.getKey(), uri2, nr.getSbn().getUser(), nr.getSbn().getPackageName(), |
| 5873 | nr.getSbn().getUid()); |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 5874 | |
Aran Ink | 979c976 | 2019-10-24 16:09:45 -0400 | [diff] [blame] | 5875 | InlineReplyUriRecord record = mService.mInlineReplyRecordsByKey.get(nr.getKey()); |
| 5876 | assertNotNull(record); // record exists |
| 5877 | assertEquals(record.getUris().size(), 2); // record has two uris in it |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 5878 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5879 | mService.mNotificationDelegate.clearInlineReplyUriPermissions(nr.getKey(), |
| 5880 | nr.getSbn().getUid()); |
Aran Ink | 979c976 | 2019-10-24 16:09:45 -0400 | [diff] [blame] | 5881 | |
| 5882 | // permissionOwner destroyed |
| 5883 | verify(mUgmInternal, times(1)).revokeUriPermissionFromOwner( |
| 5884 | eq(record.getPermissionOwner()), eq(null), eq(~0), eq(nr.getUserId())); |
| 5885 | } |
| 5886 | |
| 5887 | |
| 5888 | @Test |
| 5889 | public void testClearInlineReplyUriPermission_noUriRecordExists() throws Exception { |
| 5890 | NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel, 0); |
| 5891 | reset(mPackageManager); |
| 5892 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5893 | mService.mNotificationDelegate.clearInlineReplyUriPermissions(nr.getKey(), |
| 5894 | nr.getSbn().getUid()); |
Aran Ink | 979c976 | 2019-10-24 16:09:45 -0400 | [diff] [blame] | 5895 | |
| 5896 | // no permissionOwner destroyed |
| 5897 | verify(mUgmInternal, times(0)).revokeUriPermissionFromOwner( |
| 5898 | any(), eq(null), eq(~0), eq(nr.getUserId())); |
| 5899 | } |
| 5900 | |
| 5901 | @Test |
| 5902 | public void testClearInlineReplyUriPermission_userAll() throws Exception { |
| 5903 | NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel, |
| 5904 | UserHandle.USER_ALL); |
| 5905 | reset(mPackageManager); |
| 5906 | |
| 5907 | Uri uri1 = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 1); |
| 5908 | Uri uri2 = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 2); |
| 5909 | |
| 5910 | // create an inline record a uri in it |
| 5911 | mService.mNotificationDelegate.grantInlineReplyUriPermission( |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5912 | nr.getKey(), uri1, nr.getSbn().getUser(), nr.getSbn().getPackageName(), |
| 5913 | nr.getSbn().getUid()); |
Aran Ink | 979c976 | 2019-10-24 16:09:45 -0400 | [diff] [blame] | 5914 | |
| 5915 | InlineReplyUriRecord record = mService.mInlineReplyRecordsByKey.get(nr.getKey()); |
| 5916 | assertNotNull(record); // record exists |
| 5917 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5918 | mService.mNotificationDelegate.clearInlineReplyUriPermissions( |
| 5919 | nr.getKey(), nr.getSbn().getUid()); |
Aran Ink | 979c976 | 2019-10-24 16:09:45 -0400 | [diff] [blame] | 5920 | |
| 5921 | // permissionOwner destroyed for USER_SYSTEM, not USER_ALL |
| 5922 | verify(mUgmInternal, times(1)).revokeUriPermissionFromOwner( |
| 5923 | eq(record.getPermissionOwner()), eq(null), eq(~0), eq(USER_SYSTEM)); |
Aran Ink | fd2bfd3 | 2019-10-04 16:30:01 -0400 | [diff] [blame] | 5924 | } |
| 5925 | |
| 5926 | @Test |
Mady Mellor | 5c11a2e | 2019-04-25 17:26:15 -0700 | [diff] [blame] | 5927 | public void testNotificationBubbles_disabled_lowRamDevice() throws Exception { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 5928 | setUpPrefsForBubbles(PKG, mUid, |
| 5929 | true /* global */, |
| 5930 | BUBBLE_PREFERENCE_ALL /* app */, |
| 5931 | true /* channel */); |
Mady Mellor | 5c11a2e | 2019-04-25 17:26:15 -0700 | [diff] [blame] | 5932 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5933 | // And we are low ram |
| 5934 | when(mActivityManager.isLowRamDevice()).thenReturn(true); |
| 5935 | |
| 5936 | // Notification that would typically bubble |
| 5937 | NotificationRecord nr = generateMessageBubbleNotifRecord(mTestNotificationChannel, |
| 5938 | "testNotificationBubbles_disabled_lowRamDevice"); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 5939 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 5940 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Mady Mellor | 5c11a2e | 2019-04-25 17:26:15 -0700 | [diff] [blame] | 5941 | waitForIdle(); |
| 5942 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 5943 | // 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] | 5944 | StatusBarNotification[] notifsAfter = mBinderService.getActiveNotifications(PKG); |
| 5945 | assertEquals(1, notifsAfter.length); |
| 5946 | assertEquals((notifsAfter[0].getNotification().flags & FLAG_BUBBLE), 0); |
Mady Mellor | 5c11a2e | 2019-04-25 17:26:15 -0700 | [diff] [blame] | 5947 | } |
Julia Reynolds | b681ffe | 2019-06-19 13:40:46 -0400 | [diff] [blame] | 5948 | |
| 5949 | @Test |
| 5950 | public void testRemoveLargeRemoteViews() throws Exception { |
| 5951 | int removeSize = mContext.getResources().getInteger( |
| 5952 | com.android.internal.R.integer.config_notificationStripRemoteViewSizeBytes); |
| 5953 | |
| 5954 | RemoteViews rv = mock(RemoteViews.class); |
| 5955 | when(rv.estimateMemoryUsage()).thenReturn(removeSize); |
| 5956 | when(rv.clone()).thenReturn(rv); |
| 5957 | RemoteViews rv1 = mock(RemoteViews.class); |
| 5958 | when(rv1.estimateMemoryUsage()).thenReturn(removeSize); |
| 5959 | when(rv1.clone()).thenReturn(rv1); |
| 5960 | RemoteViews rv2 = mock(RemoteViews.class); |
| 5961 | when(rv2.estimateMemoryUsage()).thenReturn(removeSize); |
| 5962 | when(rv2.clone()).thenReturn(rv2); |
| 5963 | RemoteViews rv3 = mock(RemoteViews.class); |
| 5964 | when(rv3.estimateMemoryUsage()).thenReturn(removeSize); |
| 5965 | when(rv3.clone()).thenReturn(rv3); |
| 5966 | RemoteViews rv4 = mock(RemoteViews.class); |
| 5967 | when(rv4.estimateMemoryUsage()).thenReturn(removeSize); |
| 5968 | when(rv4.clone()).thenReturn(rv4); |
| 5969 | // note: different! |
| 5970 | RemoteViews rv5 = mock(RemoteViews.class); |
| 5971 | when(rv5.estimateMemoryUsage()).thenReturn(removeSize - 1); |
| 5972 | when(rv5.clone()).thenReturn(rv5); |
| 5973 | |
| 5974 | Notification np = new Notification.Builder(mContext, "test") |
| 5975 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 5976 | .setContentText("test") |
| 5977 | .setCustomContentView(rv) |
| 5978 | .setCustomBigContentView(rv1) |
| 5979 | .setCustomHeadsUpContentView(rv2) |
| 5980 | .build(); |
| 5981 | Notification n = new Notification.Builder(mContext, "test") |
| 5982 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 5983 | .setContentText("test") |
| 5984 | .setCustomContentView(rv3) |
| 5985 | .setCustomBigContentView(rv4) |
| 5986 | .setCustomHeadsUpContentView(rv5) |
| 5987 | .setPublicVersion(np) |
| 5988 | .build(); |
| 5989 | |
| 5990 | assertNotNull(np.contentView); |
| 5991 | assertNotNull(np.bigContentView); |
| 5992 | assertNotNull(np.headsUpContentView); |
| 5993 | |
| 5994 | assertTrue(n.publicVersion.extras.containsKey(Notification.EXTRA_CONTAINS_CUSTOM_VIEW)); |
| 5995 | assertNotNull(n.publicVersion.contentView); |
| 5996 | assertNotNull(n.publicVersion.bigContentView); |
| 5997 | assertNotNull(n.publicVersion.headsUpContentView); |
| 5998 | |
| 5999 | mService.fixNotification(n, PKG, "tag", 9, 0); |
| 6000 | |
| 6001 | assertNull(n.contentView); |
| 6002 | assertNull(n.bigContentView); |
| 6003 | assertNotNull(n.headsUpContentView); |
| 6004 | assertNull(n.publicVersion.contentView); |
| 6005 | assertNull(n.publicVersion.bigContentView); |
| 6006 | assertNull(n.publicVersion.headsUpContentView); |
| 6007 | |
| 6008 | verify(mUsageStats, times(5)).registerImageRemoved(PKG); |
| 6009 | } |
Mady Mellor | 06b770c | 2019-08-29 18:01:00 -0700 | [diff] [blame] | 6010 | |
Colin Cross | 5ea7fb7 | 2019-12-18 17:16:36 -0800 | [diff] [blame] | 6011 | @Test |
Mady Mellor | a773196 | 2019-06-17 17:57:02 -0700 | [diff] [blame] | 6012 | public void testNotificationBubbles_flagAutoExpandForeground_fails_notForeground() |
| 6013 | throws Exception { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 6014 | setUpPrefsForBubbles(PKG, mUid, |
| 6015 | true /* global */, |
| 6016 | BUBBLE_PREFERENCE_ALL /* app */, |
| 6017 | true /* channel */); |
Mady Mellor | a773196 | 2019-06-17 17:57:02 -0700 | [diff] [blame] | 6018 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 6019 | NotificationRecord nr = generateMessageBubbleNotifRecord(mTestNotificationChannel, |
| 6020 | "testNotificationBubbles_flagAutoExpandForeground_fails_notForeground"); |
| 6021 | // Modify metadata flags |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 6022 | nr.getSbn().getNotification().getBubbleMetadata().setFlags( |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 6023 | Notification.BubbleMetadata.FLAG_AUTO_EXPAND_BUBBLE |
| 6024 | | Notification.BubbleMetadata.FLAG_SUPPRESS_NOTIFICATION); |
Mady Mellor | a773196 | 2019-06-17 17:57:02 -0700 | [diff] [blame] | 6025 | |
| 6026 | // Ensure we're not foreground |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 6027 | when(mActivityManager.getPackageImportance(nr.getSbn().getPackageName())).thenReturn( |
Mady Mellor | a773196 | 2019-06-17 17:57:02 -0700 | [diff] [blame] | 6028 | IMPORTANCE_VISIBLE); |
| 6029 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 6030 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 6031 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Mady Mellor | a773196 | 2019-06-17 17:57:02 -0700 | [diff] [blame] | 6032 | waitForIdle(); |
| 6033 | |
| 6034 | // yes allowed, yes messaging, yes bubble |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 6035 | Notification notif = mService.getNotificationRecord(nr.getSbn().getKey()).getNotification(); |
Mady Mellor | a773196 | 2019-06-17 17:57:02 -0700 | [diff] [blame] | 6036 | assertTrue(notif.isBubbleNotification()); |
| 6037 | |
| 6038 | // Our flags should have failed since we're not foreground |
| 6039 | assertFalse(notif.getBubbleMetadata().getAutoExpandBubble()); |
| 6040 | assertFalse(notif.getBubbleMetadata().isNotificationSuppressed()); |
| 6041 | } |
| 6042 | |
| 6043 | @Test |
| 6044 | public void testNotificationBubbles_flagAutoExpandForeground_succeeds_foreground() |
| 6045 | throws RemoteException { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 6046 | setUpPrefsForBubbles(PKG, mUid, |
| 6047 | true /* global */, |
| 6048 | BUBBLE_PREFERENCE_ALL /* app */, |
| 6049 | true /* channel */); |
Mady Mellor | a773196 | 2019-06-17 17:57:02 -0700 | [diff] [blame] | 6050 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 6051 | NotificationRecord nr = generateMessageBubbleNotifRecord(mTestNotificationChannel, |
| 6052 | "testNotificationBubbles_flagAutoExpandForeground_succeeds_foreground"); |
| 6053 | // Modify metadata flags |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 6054 | nr.getSbn().getNotification().getBubbleMetadata().setFlags( |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 6055 | Notification.BubbleMetadata.FLAG_AUTO_EXPAND_BUBBLE |
| 6056 | | Notification.BubbleMetadata.FLAG_SUPPRESS_NOTIFICATION); |
Mady Mellor | a773196 | 2019-06-17 17:57:02 -0700 | [diff] [blame] | 6057 | |
| 6058 | // Ensure we are in the foreground |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 6059 | when(mActivityManager.getPackageImportance(nr.getSbn().getPackageName())).thenReturn( |
Mady Mellor | a773196 | 2019-06-17 17:57:02 -0700 | [diff] [blame] | 6060 | IMPORTANCE_FOREGROUND); |
| 6061 | |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 6062 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 6063 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Mady Mellor | a773196 | 2019-06-17 17:57:02 -0700 | [diff] [blame] | 6064 | waitForIdle(); |
| 6065 | |
| 6066 | // yes allowed, yes messaging, yes bubble |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 6067 | Notification notif = mService.getNotificationRecord(nr.getSbn().getKey()).getNotification(); |
Mady Mellor | a773196 | 2019-06-17 17:57:02 -0700 | [diff] [blame] | 6068 | assertTrue(notif.isBubbleNotification()); |
| 6069 | |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 6070 | // Our flags should have passed since we are foreground |
Mady Mellor | a773196 | 2019-06-17 17:57:02 -0700 | [diff] [blame] | 6071 | assertTrue(notif.getBubbleMetadata().getAutoExpandBubble()); |
| 6072 | assertTrue(notif.getBubbleMetadata().isNotificationSuppressed()); |
| 6073 | } |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 6074 | |
| 6075 | @Test |
Mady Mellor | 2ac2d3a | 2020-01-08 17:18:54 -0800 | [diff] [blame] | 6076 | public void testNotificationBubbles_flagRemoved_whenShortcutRemoved() |
| 6077 | throws RemoteException { |
Danning Chen | 28410f3 | 2020-04-21 10:28:52 -0700 | [diff] [blame] | 6078 | final String shortcutId = "someshortcutId"; |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 6079 | setUpPrefsForBubbles(PKG, mUid, |
| 6080 | true /* global */, |
| 6081 | BUBBLE_PREFERENCE_ALL /* app */, |
| 6082 | true /* channel */); |
Mady Mellor | 2ac2d3a | 2020-01-08 17:18:54 -0800 | [diff] [blame] | 6083 | |
| 6084 | ArgumentCaptor<LauncherApps.Callback> launcherAppsCallback = |
| 6085 | ArgumentCaptor.forClass(LauncherApps.Callback.class); |
| 6086 | |
| 6087 | // Messaging notification with shortcut info |
| 6088 | Notification.BubbleMetadata metadata = |
Danning Chen | 28410f3 | 2020-04-21 10:28:52 -0700 | [diff] [blame] | 6089 | new Notification.BubbleMetadata.Builder(shortcutId).build(); |
Mady Mellor | 2ac2d3a | 2020-01-08 17:18:54 -0800 | [diff] [blame] | 6090 | Notification.Builder nb = getMessageStyleNotifBuilder(false /* addDefaultMetadata */, |
| 6091 | null /* groupKey */, false /* isSummary */); |
Danning Chen | 28410f3 | 2020-04-21 10:28:52 -0700 | [diff] [blame] | 6092 | nb.setShortcutId(shortcutId); |
Mady Mellor | 2ac2d3a | 2020-01-08 17:18:54 -0800 | [diff] [blame] | 6093 | nb.setBubbleMetadata(metadata); |
| 6094 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, |
| 6095 | "tag", mUid, 0, nb.build(), new UserHandle(mUid), null, 0); |
| 6096 | NotificationRecord nr = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 6097 | |
| 6098 | // Pretend the shortcut exists |
| 6099 | List<ShortcutInfo> shortcutInfos = new ArrayList<>(); |
Julia Reynolds | d61bdf1 | 2020-02-25 12:25:07 -0500 | [diff] [blame] | 6100 | ShortcutInfo info = mock(ShortcutInfo.class); |
Danning Chen | 07cfaa6 | 2020-04-20 15:16:41 -0700 | [diff] [blame] | 6101 | when(info.getPackage()).thenReturn(PKG); |
Danning Chen | 28410f3 | 2020-04-21 10:28:52 -0700 | [diff] [blame] | 6102 | when(info.getId()).thenReturn(shortcutId); |
Danning Chen | 07cfaa6 | 2020-04-20 15:16:41 -0700 | [diff] [blame] | 6103 | when(info.getUserId()).thenReturn(USER_SYSTEM); |
Julia Reynolds | d61bdf1 | 2020-02-25 12:25:07 -0500 | [diff] [blame] | 6104 | when(info.isLongLived()).thenReturn(true); |
Julia Reynolds | fa27307 | 2020-04-14 15:31:21 -0400 | [diff] [blame] | 6105 | when(info.isEnabled()).thenReturn(true); |
Julia Reynolds | d61bdf1 | 2020-02-25 12:25:07 -0500 | [diff] [blame] | 6106 | shortcutInfos.add(info); |
Mady Mellor | 2ac2d3a | 2020-01-08 17:18:54 -0800 | [diff] [blame] | 6107 | when(mLauncherApps.getShortcuts(any(), any())).thenReturn(shortcutInfos); |
Julia Reynolds | fa27307 | 2020-04-14 15:31:21 -0400 | [diff] [blame] | 6108 | when(mShortcutServiceInternal.isSharingShortcut(anyInt(), anyString(), anyString(), |
| 6109 | anyString(), anyInt(), any())).thenReturn(true); |
Mady Mellor | 2ac2d3a | 2020-01-08 17:18:54 -0800 | [diff] [blame] | 6110 | |
| 6111 | // Test: Send the bubble notification |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 6112 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 6113 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Mady Mellor | 2ac2d3a | 2020-01-08 17:18:54 -0800 | [diff] [blame] | 6114 | waitForIdle(); |
| 6115 | |
| 6116 | // Verify: |
| 6117 | |
| 6118 | // Make sure we register the callback for shortcut changes |
| 6119 | verify(mLauncherApps, times(1)).registerCallback(launcherAppsCallback.capture(), any()); |
| 6120 | |
| 6121 | // yes allowed, yes messaging w/shortcut, yes bubble |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 6122 | Notification notif = mService.getNotificationRecord(nr.getSbn().getKey()).getNotification(); |
Mady Mellor | 2ac2d3a | 2020-01-08 17:18:54 -0800 | [diff] [blame] | 6123 | assertTrue(notif.isBubbleNotification()); |
| 6124 | |
Danning Chen | 28410f3 | 2020-04-21 10:28:52 -0700 | [diff] [blame] | 6125 | // Make sure the shortcut is cached. |
| 6126 | verify(mShortcutServiceInternal).cacheShortcuts( |
| 6127 | anyInt(), any(), eq(PKG), eq(Collections.singletonList(shortcutId)), |
| 6128 | eq(USER_SYSTEM)); |
| 6129 | |
Mady Mellor | 2ac2d3a | 2020-01-08 17:18:54 -0800 | [diff] [blame] | 6130 | // Test: Remove the shortcut |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 6131 | when(mLauncherApps.getShortcuts(any(), any())).thenReturn(null); |
Mady Mellor | 2ac2d3a | 2020-01-08 17:18:54 -0800 | [diff] [blame] | 6132 | launcherAppsCallback.getValue().onShortcutsChanged(PKG, Collections.emptyList(), |
| 6133 | new UserHandle(mUid)); |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 6134 | waitForIdle(); |
Mady Mellor | 2ac2d3a | 2020-01-08 17:18:54 -0800 | [diff] [blame] | 6135 | |
| 6136 | // Verify: |
| 6137 | |
| 6138 | // Make sure callback is unregistered |
| 6139 | verify(mLauncherApps, times(1)).unregisterCallback(launcherAppsCallback.getValue()); |
| 6140 | |
| 6141 | // We're no longer a bubble |
Julia Reynolds | d61bdf1 | 2020-02-25 12:25:07 -0500 | [diff] [blame] | 6142 | Notification notif2 = mService.getNotificationRecord( |
| 6143 | nr.getSbn().getKey()).getNotification(); |
Mady Mellor | 2ac2d3a | 2020-01-08 17:18:54 -0800 | [diff] [blame] | 6144 | assertFalse(notif2.isBubbleNotification()); |
| 6145 | } |
| 6146 | |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 6147 | |
| 6148 | @Test |
| 6149 | public void testNotificationBubbles_shortcut_stopListeningWhenNotifRemoved() |
| 6150 | throws RemoteException { |
Danning Chen | 28410f3 | 2020-04-21 10:28:52 -0700 | [diff] [blame] | 6151 | final String shortcutId = "someshortcutId"; |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 6152 | setUpPrefsForBubbles(PKG, mUid, |
| 6153 | true /* global */, |
| 6154 | BUBBLE_PREFERENCE_ALL /* app */, |
| 6155 | true /* channel */); |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 6156 | |
| 6157 | ArgumentCaptor<LauncherApps.Callback> launcherAppsCallback = |
| 6158 | ArgumentCaptor.forClass(LauncherApps.Callback.class); |
| 6159 | |
| 6160 | // Messaging notification with shortcut info |
Mady Mellor | aa9ce17 | 2020-03-17 10:34:20 -0700 | [diff] [blame] | 6161 | Notification.BubbleMetadata metadata = new Notification.BubbleMetadata.Builder( |
Danning Chen | 28410f3 | 2020-04-21 10:28:52 -0700 | [diff] [blame] | 6162 | shortcutId).build(); |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 6163 | Notification.Builder nb = getMessageStyleNotifBuilder(false /* addDefaultMetadata */, |
| 6164 | null /* groupKey */, false /* isSummary */); |
Danning Chen | 28410f3 | 2020-04-21 10:28:52 -0700 | [diff] [blame] | 6165 | nb.setShortcutId(shortcutId); |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 6166 | nb.setBubbleMetadata(metadata); |
| 6167 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, |
| 6168 | "tag", mUid, 0, nb.build(), new UserHandle(mUid), null, 0); |
| 6169 | NotificationRecord nr = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 6170 | |
| 6171 | // Pretend the shortcut exists |
| 6172 | List<ShortcutInfo> shortcutInfos = new ArrayList<>(); |
| 6173 | ShortcutInfo info = mock(ShortcutInfo.class); |
Danning Chen | 07cfaa6 | 2020-04-20 15:16:41 -0700 | [diff] [blame] | 6174 | when(info.getPackage()).thenReturn(PKG); |
Danning Chen | 28410f3 | 2020-04-21 10:28:52 -0700 | [diff] [blame] | 6175 | when(info.getId()).thenReturn(shortcutId); |
Danning Chen | 07cfaa6 | 2020-04-20 15:16:41 -0700 | [diff] [blame] | 6176 | when(info.getUserId()).thenReturn(USER_SYSTEM); |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 6177 | when(info.isLongLived()).thenReturn(true); |
Julia Reynolds | fa27307 | 2020-04-14 15:31:21 -0400 | [diff] [blame] | 6178 | when(info.isEnabled()).thenReturn(true); |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 6179 | shortcutInfos.add(info); |
| 6180 | when(mLauncherApps.getShortcuts(any(), any())).thenReturn(shortcutInfos); |
Julia Reynolds | fa27307 | 2020-04-14 15:31:21 -0400 | [diff] [blame] | 6181 | when(mShortcutServiceInternal.isSharingShortcut(anyInt(), anyString(), anyString(), |
| 6182 | anyString(), anyInt(), any())).thenReturn(true); |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 6183 | |
| 6184 | // Test: Send the bubble notification |
| 6185 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 6186 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
| 6187 | waitForIdle(); |
| 6188 | |
| 6189 | // Verify: |
| 6190 | |
| 6191 | // Make sure we register the callback for shortcut changes |
| 6192 | verify(mLauncherApps, times(1)).registerCallback(launcherAppsCallback.capture(), any()); |
| 6193 | |
| 6194 | // yes allowed, yes messaging w/shortcut, yes bubble |
| 6195 | Notification notif = mService.getNotificationRecord(nr.getSbn().getKey()).getNotification(); |
| 6196 | assertTrue(notif.isBubbleNotification()); |
| 6197 | |
Danning Chen | 28410f3 | 2020-04-21 10:28:52 -0700 | [diff] [blame] | 6198 | // Make sure the shortcut is cached. |
| 6199 | verify(mShortcutServiceInternal).cacheShortcuts( |
| 6200 | anyInt(), any(), eq(PKG), eq(Collections.singletonList(shortcutId)), |
| 6201 | eq(USER_SYSTEM)); |
| 6202 | |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 6203 | // Test: Remove the notification |
| 6204 | mBinderService.cancelNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 6205 | nr.getSbn().getId(), nr.getSbn().getUserId()); |
| 6206 | waitForIdle(); |
| 6207 | |
| 6208 | // Verify: |
| 6209 | |
| 6210 | // Make sure callback is unregistered |
| 6211 | verify(mLauncherApps, times(1)).unregisterCallback(launcherAppsCallback.getValue()); |
| 6212 | } |
| 6213 | |
Mady Mellor | 2ac2d3a | 2020-01-08 17:18:54 -0800 | [diff] [blame] | 6214 | @Test |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 6215 | public void testNotificationBubbles_bubbleChildrenStay_whenGroupSummaryDismissed() |
| 6216 | throws Exception { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 6217 | setUpPrefsForBubbles(PKG, mUid, |
| 6218 | true /* global */, |
| 6219 | BUBBLE_PREFERENCE_ALL /* app */, |
| 6220 | true /* channel */); |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 6221 | |
| 6222 | NotificationRecord nrSummary = addGroupWithBubblesAndValidateAdded( |
| 6223 | true /* summaryAutoCancel */); |
| 6224 | |
| 6225 | // Dismiss summary |
| 6226 | final NotificationVisibility nv = NotificationVisibility.obtain(nrSummary.getKey(), 1, 2, |
| 6227 | true); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 6228 | mService.mNotificationDelegate.onNotificationClear(mUid, 0, PKG, |
| 6229 | nrSummary.getSbn().getTag(), |
| 6230 | nrSummary.getSbn().getId(), nrSummary.getUserId(), nrSummary.getKey(), |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 6231 | NotificationStats.DISMISSAL_SHADE, |
| 6232 | NotificationStats.DISMISS_SENTIMENT_NEUTRAL, nv); |
| 6233 | waitForIdle(); |
| 6234 | |
| 6235 | // The bubble should still exist |
| 6236 | StatusBarNotification[] notifsAfter = mBinderService.getActiveNotifications(PKG); |
| 6237 | assertEquals(1, notifsAfter.length); |
| 6238 | } |
| 6239 | |
| 6240 | @Test |
| 6241 | public void testNotificationBubbles_bubbleChildrenStay_whenGroupSummaryClicked() |
| 6242 | throws Exception { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 6243 | setUpPrefsForBubbles(PKG, mUid, |
| 6244 | true /* global */, |
| 6245 | BUBBLE_PREFERENCE_ALL /* app */, |
| 6246 | true /* channel */); |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 6247 | |
| 6248 | NotificationRecord nrSummary = addGroupWithBubblesAndValidateAdded( |
| 6249 | true /* summaryAutoCancel */); |
| 6250 | |
| 6251 | // Click summary |
| 6252 | final NotificationVisibility nv = NotificationVisibility.obtain(nrSummary.getKey(), 1, 2, |
| 6253 | true); |
| 6254 | mService.mNotificationDelegate.onNotificationClick(mUid, Binder.getCallingPid(), |
| 6255 | nrSummary.getKey(), nv); |
| 6256 | waitForIdle(); |
| 6257 | |
| 6258 | // The bubble should still exist |
| 6259 | StatusBarNotification[] notifsAfter = mBinderService.getActiveNotifications(PKG); |
| 6260 | assertEquals(1, notifsAfter.length); |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 6261 | |
| 6262 | // Check we got the click log and associated dismissal logs |
| 6263 | assertEquals(6, mNotificationRecordLogger.numCalls()); |
| 6264 | // Skip the notification-creation logs |
| 6265 | assertEquals(NotificationRecordLogger.NotificationEvent.NOTIFICATION_CLICKED, |
| 6266 | mNotificationRecordLogger.event(3)); |
| 6267 | assertEquals(NotificationRecordLogger.NotificationCancelledEvent.NOTIFICATION_CANCEL_CLICK, |
| 6268 | mNotificationRecordLogger.event(4)); |
| 6269 | assertEquals(NotificationRecordLogger.NotificationCancelledEvent |
| 6270 | .NOTIFICATION_CANCEL_GROUP_SUMMARY_CANCELED, |
| 6271 | mNotificationRecordLogger.event(5)); |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 6272 | } |
Julia Reynolds | 57a974b | 2019-10-07 11:51:47 -0400 | [diff] [blame] | 6273 | |
| 6274 | @Test |
Beverly | 9e6a764 | 2020-02-13 10:11:29 -0500 | [diff] [blame] | 6275 | public void testNotificationBubbles_bubbleStays_whenClicked() |
| 6276 | throws Exception { |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 6277 | setUpPrefsForBubbles(PKG, mUid, |
| 6278 | true /* global */, |
| 6279 | BUBBLE_PREFERENCE_ALL /* app */, |
| 6280 | true /* channel */); |
| 6281 | |
Beverly | 9e6a764 | 2020-02-13 10:11:29 -0500 | [diff] [blame] | 6282 | // 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] | 6283 | final NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel); |
| 6284 | nr.getSbn().getNotification().flags |= FLAG_BUBBLE | FLAG_AUTO_CANCEL; |
| 6285 | mService.addNotification(nr); |
| 6286 | |
| 6287 | // WHEN we click the notification |
| 6288 | final NotificationVisibility nv = NotificationVisibility.obtain(nr.getKey(), 1, 2, true); |
| 6289 | mService.mNotificationDelegate.onNotificationClick(mUid, Binder.getCallingPid(), |
| 6290 | nr.getKey(), nv); |
| 6291 | waitForIdle(); |
| 6292 | |
| 6293 | // THEN the bubble should still exist |
| 6294 | StatusBarNotification[] notifsAfter = mBinderService.getActiveNotifications(PKG); |
| 6295 | assertEquals(1, notifsAfter.length); |
Will Brockman | 9918db9 | 2020-03-06 16:48:39 -0500 | [diff] [blame] | 6296 | |
| 6297 | // Check we got the click log |
| 6298 | assertEquals(1, mNotificationRecordLogger.numCalls()); |
| 6299 | assertEquals(NotificationRecordLogger.NotificationEvent.NOTIFICATION_CLICKED, |
| 6300 | mNotificationRecordLogger.event(0)); |
Beverly | 9e6a764 | 2020-02-13 10:11:29 -0500 | [diff] [blame] | 6301 | } |
| 6302 | |
| 6303 | @Test |
Julia Reynolds | 57a974b | 2019-10-07 11:51:47 -0400 | [diff] [blame] | 6304 | public void testLoadDefaultApprovedServices_emptyResources() { |
| 6305 | TestableResources tr = mContext.getOrCreateTestableResources(); |
| 6306 | tr.addOverride(com.android.internal.R.string.config_defaultListenerAccessPackages, ""); |
| 6307 | tr.addOverride(com.android.internal.R.string.config_defaultDndAccessPackages, ""); |
| 6308 | tr.addOverride(com.android.internal.R.string.config_defaultAssistantAccessComponent, ""); |
| 6309 | setDefaultAssistantInDeviceConfig(""); |
| 6310 | |
| 6311 | mService.loadDefaultApprovedServices(USER_SYSTEM); |
| 6312 | |
| 6313 | verify(mListeners, never()).addDefaultComponentOrPackage(anyString()); |
| 6314 | verify(mConditionProviders, never()).addDefaultComponentOrPackage(anyString()); |
| 6315 | verify(mAssistants, never()).addDefaultComponentOrPackage(anyString()); |
| 6316 | } |
| 6317 | |
| 6318 | @Test |
| 6319 | public void testLoadDefaultApprovedServices_dnd() { |
| 6320 | TestableResources tr = mContext.getOrCreateTestableResources(); |
| 6321 | tr.addOverride(com.android.internal.R.string.config_defaultDndAccessPackages, "test"); |
| 6322 | when(mListeners.queryPackageForServices(anyString(), anyInt(), anyInt())) |
| 6323 | .thenReturn(new ArraySet<>()); |
| 6324 | |
| 6325 | mService.loadDefaultApprovedServices(USER_SYSTEM); |
| 6326 | |
Jay Aliomer | 76e1f272 | 2020-03-05 12:36:38 -0500 | [diff] [blame] | 6327 | verify(mConditionProviders, times(1)).loadDefaultsFromConfig(); |
Julia Reynolds | 57a974b | 2019-10-07 11:51:47 -0400 | [diff] [blame] | 6328 | } |
| 6329 | |
| 6330 | // TODO: add tests for the rest of the non-empty cases |
Julia Reynolds | b317ff7 | 2019-11-26 14:20:51 -0500 | [diff] [blame] | 6331 | |
| 6332 | @Test |
| 6333 | public void testOnUnlockUser() { |
| 6334 | UserInfo ui = new UserInfo(); |
| 6335 | ui.id = 10; |
| 6336 | mService.onUnlockUser(ui); |
| 6337 | waitForIdle(); |
| 6338 | |
| 6339 | verify(mHistoryManager, timeout(MAX_POST_DELAY).times(1)).onUserUnlocked(ui.id); |
| 6340 | } |
| 6341 | |
| 6342 | @Test |
| 6343 | public void testOnStopUser() { |
| 6344 | UserInfo ui = new UserInfo(); |
| 6345 | ui.id = 10; |
| 6346 | mService.onStopUser(ui); |
| 6347 | waitForIdle(); |
| 6348 | |
| 6349 | verify(mHistoryManager, timeout(MAX_POST_DELAY).times(1)).onUserStopped(ui.id); |
| 6350 | } |
| 6351 | |
| 6352 | @Test |
| 6353 | public void testOnBootPhase() { |
| 6354 | mService.onBootPhase(SystemService.PHASE_ACTIVITY_MANAGER_READY); |
| 6355 | |
| 6356 | verify(mHistoryManager, never()).onBootPhaseAppsCanStart(); |
| 6357 | |
| 6358 | mService.onBootPhase(SystemService.PHASE_THIRD_PARTY_APPS_CAN_START); |
| 6359 | |
| 6360 | verify(mHistoryManager, times(1)).onBootPhaseAppsCanStart(); |
| 6361 | } |
| 6362 | |
| 6363 | @Test |
| 6364 | public void testHandleOnPackageChanged() { |
| 6365 | String[] pkgs = new String[] {PKG, PKG_N_MR1}; |
| 6366 | int[] uids = new int[] {mUid, UserHandle.PER_USER_RANGE + 1}; |
| 6367 | |
| 6368 | mService.handleOnPackageChanged(false, USER_SYSTEM, pkgs, uids); |
| 6369 | |
| 6370 | verify(mHistoryManager, never()).onPackageRemoved(anyInt(), anyString()); |
| 6371 | |
| 6372 | mService.handleOnPackageChanged(true, USER_SYSTEM, pkgs, uids); |
| 6373 | |
| 6374 | verify(mHistoryManager, times(1)).onPackageRemoved(UserHandle.getUserId(uids[0]), pkgs[0]); |
| 6375 | verify(mHistoryManager, times(1)).onPackageRemoved(UserHandle.getUserId(uids[1]), pkgs[1]); |
| 6376 | } |
| 6377 | |
| 6378 | @Test |
| 6379 | public void testNotificationHistory_addNoisyNotification() throws Exception { |
| 6380 | NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel, |
Mady Mellor | 9e923e1 | 2019-12-17 16:08:46 -0800 | [diff] [blame] | 6381 | null /* tvExtender */); |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 6382 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 6383 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
Julia Reynolds | b317ff7 | 2019-11-26 14:20:51 -0500 | [diff] [blame] | 6384 | waitForIdle(); |
| 6385 | |
| 6386 | verify(mHistoryManager, times(1)).addNotification(any()); |
| 6387 | } |
Julia Reynolds | 0f76734 | 2019-12-18 09:11:55 -0500 | [diff] [blame] | 6388 | |
| 6389 | @Test |
| 6390 | public void createConversationNotificationChannel() throws Exception { |
| 6391 | NotificationChannel original = new NotificationChannel("a", "a", IMPORTANCE_HIGH); |
| 6392 | original.setAllowBubbles(!original.canBubble()); |
| 6393 | original.setShowBadge(!original.canShowBadge()); |
| 6394 | |
| 6395 | Parcel parcel = Parcel.obtain(); |
| 6396 | original.writeToParcel(parcel, 0); |
| 6397 | parcel.setDataPosition(0); |
| 6398 | NotificationChannel orig = NotificationChannel.CREATOR.createFromParcel(parcel); |
| 6399 | assertEquals(original, orig); |
| 6400 | assertFalse(TextUtils.isEmpty(orig.getName())); |
| 6401 | |
| 6402 | mBinderService.createNotificationChannels(PKG, new ParceledListSlice(Arrays.asList( |
| 6403 | orig))); |
| 6404 | |
Julia Reynolds | 7c26752 | 2020-01-16 11:26:41 -0500 | [diff] [blame] | 6405 | mBinderService.createConversationNotificationChannelForPackage( |
| 6406 | PKG, mUid, "key", orig, "friend"); |
Julia Reynolds | 0f76734 | 2019-12-18 09:11:55 -0500 | [diff] [blame] | 6407 | |
| 6408 | NotificationChannel friendChannel = mBinderService.getConversationNotificationChannel( |
Julia Reynolds | 12ba4cf | 2020-01-10 16:01:38 -0500 | [diff] [blame] | 6409 | PKG, 0, PKG, original.getId(), false, "friend"); |
Julia Reynolds | 0f76734 | 2019-12-18 09:11:55 -0500 | [diff] [blame] | 6410 | |
| 6411 | assertEquals(original.getName(), friendChannel.getName()); |
| 6412 | assertEquals(original.getId(), friendChannel.getParentChannelId()); |
| 6413 | assertEquals("friend", friendChannel.getConversationId()); |
| 6414 | assertEquals(null, original.getConversationId()); |
| 6415 | assertEquals(original.canShowBadge(), friendChannel.canShowBadge()); |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 6416 | assertFalse(friendChannel.canBubble()); // can't be modified by app |
Julia Reynolds | 0f76734 | 2019-12-18 09:11:55 -0500 | [diff] [blame] | 6417 | assertFalse(original.getId().equals(friendChannel.getId())); |
| 6418 | assertNotNull(friendChannel.getId()); |
| 6419 | } |
| 6420 | |
| 6421 | @Test |
| 6422 | public void deleteConversationNotificationChannels() throws Exception { |
| 6423 | NotificationChannel messagesParent = |
| 6424 | new NotificationChannel("messages", "messages", IMPORTANCE_HIGH); |
| 6425 | Parcel msgParcel = Parcel.obtain(); |
| 6426 | messagesParent.writeToParcel(msgParcel, 0); |
| 6427 | msgParcel.setDataPosition(0); |
| 6428 | |
| 6429 | NotificationChannel callsParent = |
| 6430 | new NotificationChannel("calls", "calls", IMPORTANCE_HIGH); |
| 6431 | Parcel callParcel = Parcel.obtain(); |
| 6432 | callsParent.writeToParcel(callParcel, 0); |
| 6433 | callParcel.setDataPosition(0); |
| 6434 | |
| 6435 | mBinderService.createNotificationChannels(PKG, new ParceledListSlice(Arrays.asList( |
| 6436 | messagesParent, callsParent))); |
| 6437 | |
| 6438 | String conversationId = "friend"; |
| 6439 | |
| 6440 | mBinderService.createConversationNotificationChannelForPackage( |
Julia Reynolds | 7c26752 | 2020-01-16 11:26:41 -0500 | [diff] [blame] | 6441 | PKG, mUid, "key", NotificationChannel.CREATOR.createFromParcel(msgParcel), |
| 6442 | conversationId); |
Julia Reynolds | 0f76734 | 2019-12-18 09:11:55 -0500 | [diff] [blame] | 6443 | mBinderService.createConversationNotificationChannelForPackage( |
Julia Reynolds | 7c26752 | 2020-01-16 11:26:41 -0500 | [diff] [blame] | 6444 | PKG, mUid, "key", NotificationChannel.CREATOR.createFromParcel(callParcel), |
Julia Reynolds | 0f76734 | 2019-12-18 09:11:55 -0500 | [diff] [blame] | 6445 | conversationId); |
| 6446 | |
| 6447 | NotificationChannel messagesChild = mBinderService.getConversationNotificationChannel( |
Julia Reynolds | 12ba4cf | 2020-01-10 16:01:38 -0500 | [diff] [blame] | 6448 | PKG, 0, PKG, messagesParent.getId(), false, conversationId); |
Julia Reynolds | 0f76734 | 2019-12-18 09:11:55 -0500 | [diff] [blame] | 6449 | NotificationChannel callsChild = mBinderService.getConversationNotificationChannel( |
Julia Reynolds | 12ba4cf | 2020-01-10 16:01:38 -0500 | [diff] [blame] | 6450 | PKG, 0, PKG, callsParent.getId(), false, conversationId); |
Julia Reynolds | 0f76734 | 2019-12-18 09:11:55 -0500 | [diff] [blame] | 6451 | |
| 6452 | assertEquals(messagesParent.getId(), messagesChild.getParentChannelId()); |
| 6453 | assertEquals(conversationId, messagesChild.getConversationId()); |
| 6454 | |
| 6455 | assertEquals(callsParent.getId(), callsChild.getParentChannelId()); |
| 6456 | assertEquals(conversationId, callsChild.getConversationId()); |
| 6457 | |
| 6458 | mBinderService.deleteConversationNotificationChannels(PKG, mUid, conversationId); |
| 6459 | |
| 6460 | assertNull(mBinderService.getConversationNotificationChannel( |
Julia Reynolds | 12ba4cf | 2020-01-10 16:01:38 -0500 | [diff] [blame] | 6461 | PKG, 0, PKG, messagesParent.getId(), false, conversationId)); |
Julia Reynolds | 0f76734 | 2019-12-18 09:11:55 -0500 | [diff] [blame] | 6462 | assertNull(mBinderService.getConversationNotificationChannel( |
Julia Reynolds | 12ba4cf | 2020-01-10 16:01:38 -0500 | [diff] [blame] | 6463 | PKG, 0, PKG, callsParent.getId(), false, conversationId)); |
Julia Reynolds | 0f76734 | 2019-12-18 09:11:55 -0500 | [diff] [blame] | 6464 | } |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 6465 | |
| 6466 | @Test |
| 6467 | public void testCorrectCategory_systemOn_appCannotTurnOff() { |
| 6468 | int requested = 0; |
| 6469 | int system = PRIORITY_CATEGORY_CALLS | PRIORITY_CATEGORY_CONVERSATIONS; |
| 6470 | |
| 6471 | int actual = mService.correctCategory(requested, PRIORITY_CATEGORY_CONVERSATIONS, |
| 6472 | system); |
| 6473 | |
| 6474 | assertEquals(PRIORITY_CATEGORY_CONVERSATIONS, actual); |
| 6475 | } |
| 6476 | |
| 6477 | @Test |
| 6478 | public void testCorrectCategory_systemOff_appTurnOff_noChanges() { |
| 6479 | int requested = PRIORITY_CATEGORY_CALLS; |
| 6480 | int system = PRIORITY_CATEGORY_CALLS; |
| 6481 | |
| 6482 | int actual = mService.correctCategory(requested, PRIORITY_CATEGORY_CONVERSATIONS, |
| 6483 | system); |
| 6484 | |
| 6485 | assertEquals(PRIORITY_CATEGORY_CALLS, actual); |
| 6486 | } |
| 6487 | |
| 6488 | @Test |
| 6489 | public void testCorrectCategory_systemOn_appTurnOn_noChanges() { |
| 6490 | int requested = PRIORITY_CATEGORY_CALLS | PRIORITY_CATEGORY_CONVERSATIONS; |
| 6491 | int system = PRIORITY_CATEGORY_CALLS | PRIORITY_CATEGORY_CONVERSATIONS; |
| 6492 | |
| 6493 | int actual = mService.correctCategory(requested, PRIORITY_CATEGORY_CONVERSATIONS, |
| 6494 | system); |
| 6495 | |
| 6496 | assertEquals(PRIORITY_CATEGORY_CALLS | PRIORITY_CATEGORY_CONVERSATIONS, actual); |
| 6497 | } |
| 6498 | |
| 6499 | @Test |
| 6500 | public void testCorrectCategory_systemOff_appCannotTurnOn() { |
| 6501 | int requested = PRIORITY_CATEGORY_CALLS | PRIORITY_CATEGORY_CONVERSATIONS; |
| 6502 | int system = PRIORITY_CATEGORY_CALLS; |
| 6503 | |
| 6504 | int actual = mService.correctCategory(requested, PRIORITY_CATEGORY_CONVERSATIONS, |
| 6505 | system); |
| 6506 | |
| 6507 | assertEquals(PRIORITY_CATEGORY_CALLS, actual); |
| 6508 | } |
Julia Reynolds | 882f206 | 2020-02-05 12:11:38 -0500 | [diff] [blame] | 6509 | |
| 6510 | @Test |
| 6511 | public void testGetConversationsForPackage_hasShortcut() throws Exception { |
| 6512 | mService.setPreferencesHelper(mPreferencesHelper); |
| 6513 | ArrayList<ConversationChannelWrapper> convos = new ArrayList<>(); |
| 6514 | ConversationChannelWrapper convo1 = new ConversationChannelWrapper(); |
| 6515 | NotificationChannel channel1 = new NotificationChannel("a", "a", 1); |
| 6516 | channel1.setConversationId("parent1", "convo 1"); |
| 6517 | convo1.setNotificationChannel(channel1); |
| 6518 | convos.add(convo1); |
| 6519 | |
| 6520 | ConversationChannelWrapper convo2 = new ConversationChannelWrapper(); |
| 6521 | NotificationChannel channel2 = new NotificationChannel("b", "b", 1); |
| 6522 | channel2.setConversationId("parent1", "convo 2"); |
| 6523 | convo2.setNotificationChannel(channel2); |
| 6524 | convos.add(convo2); |
| 6525 | when(mPreferencesHelper.getConversations(anyString(), anyInt())).thenReturn(convos); |
| 6526 | |
Julia Reynolds | 882f206 | 2020-02-05 12:11:38 -0500 | [diff] [blame] | 6527 | ShortcutInfo si = mock(ShortcutInfo.class); |
Danning Chen | 07cfaa6 | 2020-04-20 15:16:41 -0700 | [diff] [blame] | 6528 | when(si.getPackage()).thenReturn(PKG_P); |
| 6529 | when(si.getId()).thenReturn("convo"); |
| 6530 | when(si.getUserId()).thenReturn(USER_SYSTEM); |
Julia Reynolds | 882f206 | 2020-02-05 12:11:38 -0500 | [diff] [blame] | 6531 | when(si.getShortLabel()).thenReturn("Hello"); |
Mady Mellor | 774d5fe | 2020-03-05 11:35:03 -0800 | [diff] [blame] | 6532 | when(si.isLongLived()).thenReturn(true); |
Julia Reynolds | fa27307 | 2020-04-14 15:31:21 -0400 | [diff] [blame] | 6533 | when(si.isEnabled()).thenReturn(true); |
Julia Reynolds | 882f206 | 2020-02-05 12:11:38 -0500 | [diff] [blame] | 6534 | when(mLauncherApps.getShortcuts(any(), any())).thenReturn(Arrays.asList(si)); |
Julia Reynolds | fa27307 | 2020-04-14 15:31:21 -0400 | [diff] [blame] | 6535 | when(mShortcutServiceInternal.isSharingShortcut(anyInt(), anyString(), anyString(), |
| 6536 | anyString(), anyInt(), any())).thenReturn(true); |
Julia Reynolds | 882f206 | 2020-02-05 12:11:38 -0500 | [diff] [blame] | 6537 | |
| 6538 | List<ConversationChannelWrapper> conversations = |
| 6539 | mBinderService.getConversationsForPackage(PKG_P, mUid).getList(); |
| 6540 | assertEquals(si, conversations.get(0).getShortcutInfo()); |
| 6541 | assertEquals(si, conversations.get(1).getShortcutInfo()); |
Mady Mellor | 774d5fe | 2020-03-05 11:35:03 -0800 | [diff] [blame] | 6542 | } |
Julia Reynolds | 882f206 | 2020-02-05 12:11:38 -0500 | [diff] [blame] | 6543 | |
Mady Mellor | 774d5fe | 2020-03-05 11:35:03 -0800 | [diff] [blame] | 6544 | @Test |
| 6545 | public void testGetConversationsForPackage_shortcut_notLongLived() throws Exception { |
| 6546 | mService.setPreferencesHelper(mPreferencesHelper); |
| 6547 | ArrayList<ConversationChannelWrapper> convos = new ArrayList<>(); |
| 6548 | ConversationChannelWrapper convo1 = new ConversationChannelWrapper(); |
| 6549 | NotificationChannel channel1 = new NotificationChannel("a", "a", 1); |
| 6550 | channel1.setConversationId("parent1", "convo 1"); |
| 6551 | convo1.setNotificationChannel(channel1); |
| 6552 | convos.add(convo1); |
| 6553 | |
| 6554 | ConversationChannelWrapper convo2 = new ConversationChannelWrapper(); |
| 6555 | NotificationChannel channel2 = new NotificationChannel("b", "b", 1); |
| 6556 | channel2.setConversationId("parent1", "convo 2"); |
| 6557 | convo2.setNotificationChannel(channel2); |
| 6558 | convos.add(convo2); |
| 6559 | when(mPreferencesHelper.getConversations(anyString(), anyInt())).thenReturn(convos); |
| 6560 | |
| 6561 | ShortcutInfo si = mock(ShortcutInfo.class); |
Danning Chen | 07cfaa6 | 2020-04-20 15:16:41 -0700 | [diff] [blame] | 6562 | when(si.getPackage()).thenReturn(PKG_P); |
| 6563 | when(si.getId()).thenReturn("convo"); |
| 6564 | when(si.getUserId()).thenReturn(USER_SYSTEM); |
Mady Mellor | 774d5fe | 2020-03-05 11:35:03 -0800 | [diff] [blame] | 6565 | when(si.getShortLabel()).thenReturn("Hello"); |
| 6566 | when(si.isLongLived()).thenReturn(false); |
| 6567 | when(mLauncherApps.getShortcuts(any(), any())).thenReturn(Arrays.asList(si)); |
| 6568 | |
| 6569 | List<ConversationChannelWrapper> conversations = |
| 6570 | mBinderService.getConversationsForPackage(PKG_P, mUid).getList(); |
| 6571 | assertNull(conversations.get(0).getShortcutInfo()); |
| 6572 | assertNull(conversations.get(1).getShortcutInfo()); |
Julia Reynolds | 882f206 | 2020-02-05 12:11:38 -0500 | [diff] [blame] | 6573 | } |
| 6574 | |
| 6575 | @Test |
| 6576 | public void testGetConversationsForPackage_doesNotHaveShortcut() throws Exception { |
| 6577 | mService.setPreferencesHelper(mPreferencesHelper); |
| 6578 | ArrayList<ConversationChannelWrapper> convos = new ArrayList<>(); |
| 6579 | ConversationChannelWrapper convo1 = new ConversationChannelWrapper(); |
| 6580 | NotificationChannel channel1 = new NotificationChannel("a", "a", 1); |
| 6581 | channel1.setConversationId("parent1", "convo 1"); |
| 6582 | convo1.setNotificationChannel(channel1); |
| 6583 | convos.add(convo1); |
| 6584 | |
| 6585 | ConversationChannelWrapper convo2 = new ConversationChannelWrapper(); |
| 6586 | NotificationChannel channel2 = new NotificationChannel("b", "b", 1); |
| 6587 | channel2.setConversationId("parent1", "convo 2"); |
| 6588 | convo2.setNotificationChannel(channel2); |
| 6589 | convos.add(convo2); |
| 6590 | when(mPreferencesHelper.getConversations(anyString(), anyInt())).thenReturn(convos); |
| 6591 | |
| 6592 | List<ConversationChannelWrapper> conversations = |
| 6593 | mBinderService.getConversationsForPackage(PKG_P, mUid).getList(); |
| 6594 | assertNull(conversations.get(0).getShortcutInfo()); |
| 6595 | assertNull(conversations.get(1).getShortcutInfo()); |
| 6596 | } |
Mady Mellor | 57b3416 | 2020-04-01 08:44:00 -0700 | [diff] [blame] | 6597 | |
| 6598 | @Test |
| 6599 | public void testShortcutHelperNull_doesntCrashEnqueue() throws RemoteException { |
| 6600 | mService.setShortcutHelper(null); |
| 6601 | NotificationRecord nr = |
| 6602 | generateMessageBubbleNotifRecord(mTestNotificationChannel, |
| 6603 | "testShortcutHelperNull_doesntCrashEnqueue"); |
| 6604 | try { |
| 6605 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 6606 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
| 6607 | waitForIdle(); |
| 6608 | } catch (Exception e) { |
| 6609 | fail(e.getMessage()); |
| 6610 | } |
| 6611 | } |
Julia Reynolds | a7dac43 | 2020-04-23 12:17:31 -0400 | [diff] [blame] | 6612 | |
| 6613 | @Test |
| 6614 | public void testRecordMessages() throws RemoteException { |
| 6615 | NotificationRecord nr = |
| 6616 | generateMessageBubbleNotifRecord(mTestNotificationChannel, |
| 6617 | "testRecordMessages"); |
| 6618 | mBinderService.enqueueNotificationWithTag(PKG, PKG, nr.getSbn().getTag(), |
| 6619 | nr.getSbn().getId(), nr.getSbn().getNotification(), nr.getSbn().getUserId()); |
| 6620 | waitForIdle(); |
| 6621 | |
| 6622 | assertTrue(mBinderService.hasSentMessage(PKG, mUid)); |
| 6623 | } |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 6624 | } |