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; |
| 20 | import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_GONE; |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 21 | import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE; |
| 22 | import static android.app.Notification.CATEGORY_CALL; |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 23 | import static android.app.Notification.FLAG_AUTO_CANCEL; |
Mady Mellor | 49b1bf1 | 2019-03-29 12:00:02 -0700 | [diff] [blame] | 24 | import static android.app.Notification.FLAG_BUBBLE; |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 25 | import static android.app.Notification.FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 26 | import static android.app.NotificationManager.EXTRA_BLOCKED_STATE; |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 27 | import static android.app.NotificationManager.IMPORTANCE_DEFAULT; |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 28 | import static android.app.NotificationManager.IMPORTANCE_HIGH; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 29 | import static android.app.NotificationManager.IMPORTANCE_LOW; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 30 | import static android.app.NotificationManager.IMPORTANCE_MAX; |
Julia Reynolds | 4da7970 | 2017-06-01 11:06:10 -0400 | [diff] [blame] | 31 | import static android.app.NotificationManager.IMPORTANCE_NONE; |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 32 | import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED; |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 33 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_AMBIENT; |
| 34 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_BADGE; |
| 35 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_FULL_SCREEN_INTENT; |
| 36 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_LIGHTS; |
| 37 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_NOTIFICATION_LIST; |
| 38 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK; |
| 39 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_OFF; |
| 40 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_ON; |
| 41 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_STATUS_BAR; |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 42 | import static android.content.pm.PackageManager.FEATURE_WATCH; |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 43 | import static android.content.pm.PackageManager.PERMISSION_DENIED; |
Julia Reynolds | 7a6d07a | 2019-03-18 11:31:56 -0400 | [diff] [blame] | 44 | import static android.content.pm.PackageManager.PERMISSION_GRANTED; |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 45 | import static android.os.Build.VERSION_CODES.O_MR1; |
| 46 | import static android.os.Build.VERSION_CODES.P; |
Julia Reynolds | c7dcdc2 | 2019-03-25 10:26:14 -0400 | [diff] [blame] | 47 | import static android.service.notification.Adjustment.KEY_IMPORTANCE; |
| 48 | import static android.service.notification.Adjustment.KEY_USER_SENTIMENT; |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 49 | import static android.service.notification.NotificationListenerService.Ranking.USER_SENTIMENT_NEGATIVE; |
| 50 | import static android.service.notification.NotificationListenerService.Ranking.USER_SENTIMENT_NEUTRAL; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 51 | |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 52 | import static junit.framework.Assert.assertEquals; |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 53 | import static junit.framework.Assert.assertFalse; |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 54 | import static junit.framework.Assert.assertNotNull; |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 55 | import static junit.framework.Assert.assertNull; |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 56 | import static junit.framework.Assert.assertTrue; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 57 | import static junit.framework.Assert.fail; |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 58 | |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 59 | import static org.mockito.Matchers.anyBoolean; |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 60 | import static org.mockito.Matchers.anyLong; |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 61 | import static org.mockito.Matchers.anyString; |
| 62 | import static org.mockito.Matchers.eq; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 63 | import static org.mockito.Mockito.any; |
| 64 | import static org.mockito.Mockito.anyInt; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 65 | import static org.mockito.Mockito.doAnswer; |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 66 | import static org.mockito.Mockito.doNothing; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 67 | import static org.mockito.Mockito.mock; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 68 | import static org.mockito.Mockito.never; |
| 69 | import static org.mockito.Mockito.reset; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 70 | import static org.mockito.Mockito.spy; |
| 71 | import static org.mockito.Mockito.timeout; |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 72 | import static org.mockito.Mockito.times; |
| 73 | import static org.mockito.Mockito.verify; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 74 | import static org.mockito.Mockito.when; |
| 75 | |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 76 | import android.app.ActivityManager; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 77 | import android.app.AppOpsManager; |
Julia Reynolds | a94365d | 2019-04-09 10:48:43 -0400 | [diff] [blame] | 78 | import android.app.AutomaticZenRule; |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 79 | import android.app.IActivityManager; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 80 | import android.app.INotificationManager; |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 81 | import android.app.ITransientNotification; |
| 82 | import android.app.IUriGrantsManager; |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 83 | import android.app.Notification; |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 84 | import android.app.Notification.MessagingStyle.Message; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 85 | import android.app.NotificationChannel; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 86 | import android.app.NotificationChannelGroup; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 87 | import android.app.NotificationManager; |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 88 | import android.app.PendingIntent; |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 89 | import android.app.Person; |
Mady Mellor | a10448e | 2019-04-26 13:50:58 -0700 | [diff] [blame] | 90 | import android.app.RemoteInput; |
Jason Parks | 50322ff | 2018-03-27 10:23:33 -0500 | [diff] [blame] | 91 | import android.app.admin.DevicePolicyManagerInternal; |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 92 | import android.app.usage.UsageStatsManagerInternal; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 93 | import android.companion.ICompanionDeviceManager; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 94 | import android.content.ComponentName; |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 95 | import android.content.ContentUris; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 96 | import android.content.Context; |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 97 | import android.content.Intent; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 98 | import android.content.pm.ApplicationInfo; |
| 99 | import android.content.pm.IPackageManager; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 100 | import android.content.pm.PackageManager; |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 101 | import android.content.pm.ParceledListSlice; |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 102 | import android.content.pm.UserInfo; |
Kristian Monsen | 05f3479 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 103 | import android.content.res.Resources; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 104 | import android.graphics.Color; |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 105 | import android.graphics.drawable.Icon; |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 106 | import android.media.AudioManager; |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 107 | import android.net.Uri; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 108 | import android.os.Binder; |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 109 | import android.os.Build; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 110 | import android.os.Bundle; |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 111 | import android.os.IBinder; |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 112 | import android.os.Process; |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 113 | import android.os.RemoteException; |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 114 | import android.os.SystemClock; |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 115 | import android.os.UserHandle; |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 116 | import android.os.UserManager; |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 117 | import android.provider.DeviceConfig; |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 118 | import android.provider.MediaStore; |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 119 | import android.provider.Settings; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 120 | import android.service.notification.Adjustment; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 121 | import android.service.notification.NotificationListenerService; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 122 | import android.service.notification.NotificationStats; |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 123 | import android.service.notification.StatusBarNotification; |
Julia Reynolds | a94365d | 2019-04-09 10:48:43 -0400 | [diff] [blame] | 124 | import android.service.notification.ZenPolicy; |
Geoffrey Pitsch | 8185d38 | 2017-05-19 18:41:32 -0400 | [diff] [blame] | 125 | import android.test.suitebuilder.annotation.SmallTest; |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 126 | import android.testing.AndroidTestingRunner; |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 127 | import android.testing.TestableContext; |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 128 | import android.testing.TestableLooper; |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 129 | import android.testing.TestableLooper.RunWithLooper; |
Julia Reynolds | 7a6d07a | 2019-03-18 11:31:56 -0400 | [diff] [blame] | 130 | import android.testing.TestablePermissions; |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 131 | import android.text.Html; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 132 | import android.util.ArrayMap; |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 133 | import android.util.ArraySet; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 134 | import android.util.AtomicFile; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 135 | |
Mady Mellor | 49b1bf1 | 2019-03-29 12:00:02 -0700 | [diff] [blame] | 136 | import androidx.annotation.Nullable; |
| 137 | import androidx.test.InstrumentationRegistry; |
| 138 | |
Kristian Monsen | 05f3479 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 139 | import com.android.internal.R; |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 140 | import com.android.internal.config.sysui.SystemUiDeviceConfigFlags; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 141 | import com.android.internal.statusbar.NotificationVisibility; |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 142 | import com.android.server.LocalServices; |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 143 | import com.android.server.SystemService; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 144 | import com.android.server.UiServiceTestCase; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 145 | import com.android.server.lights.Light; |
| 146 | import com.android.server.lights.LightsManager; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 147 | import com.android.server.notification.NotificationManagerService.NotificationAssistants; |
| 148 | import com.android.server.notification.NotificationManagerService.NotificationListeners; |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 149 | import com.android.server.uri.UriGrantsManagerInternal; |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 150 | import com.android.server.wm.WindowManagerInternal; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 151 | |
| 152 | import org.junit.After; |
| 153 | import org.junit.Before; |
| 154 | import org.junit.Test; |
| 155 | import org.junit.runner.RunWith; |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 156 | import org.mockito.ArgumentCaptor; |
| 157 | import org.mockito.Mock; |
| 158 | import org.mockito.MockitoAnnotations; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 159 | import org.mockito.stubbing.Answer; |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 160 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 161 | import java.io.BufferedInputStream; |
| 162 | import java.io.ByteArrayInputStream; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 163 | import java.io.File; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 164 | import java.io.FileOutputStream; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 165 | import java.util.ArrayList; |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 166 | import java.util.Arrays; |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 167 | import java.util.Collections; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 168 | import java.util.List; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 169 | import java.util.Map; |
Robin Lee | d107af6 | 2018-04-27 13:55:56 +0200 | [diff] [blame] | 170 | import java.util.function.Consumer; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 171 | |
Geoffrey Pitsch | 8185d38 | 2017-05-19 18:41:32 -0400 | [diff] [blame] | 172 | @SmallTest |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 173 | @RunWith(AndroidTestingRunner.class) |
| 174 | @RunWithLooper |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 175 | public class NotificationManagerServiceTest extends UiServiceTestCase { |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 176 | private static final String TEST_CHANNEL_ID = "NotificationManagerServiceTestChannelId"; |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 177 | private static final String CLEAR_DEVICE_CONFIG_KEY_CMD = |
| 178 | "device_config delete " + DeviceConfig.NAMESPACE_SYSTEMUI + " " |
| 179 | + SystemUiDeviceConfigFlags.NAS_DEFAULT_SERVICE; |
| 180 | private static final String SET_DEFAULT_ASSISTANT_DEVICE_CONFIG_CMD = |
| 181 | "device_config put " + DeviceConfig.NAMESPACE_SYSTEMUI + " " |
| 182 | + SystemUiDeviceConfigFlags.NAS_DEFAULT_SERVICE; |
| 183 | |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 184 | private final int mUid = Binder.getCallingUid(); |
Julia Reynolds | d78263d | 2018-01-30 10:40:41 -0500 | [diff] [blame] | 185 | private TestableNotificationManagerService mService; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 186 | private INotificationManager mBinderService; |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 187 | private NotificationManagerInternal mInternalService; |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 188 | @Mock |
| 189 | private IPackageManager mPackageManager; |
| 190 | @Mock |
| 191 | private PackageManager mPackageManagerClient; |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 192 | @Mock |
| 193 | private WindowManagerInternal mWindowManagerInternal; |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 194 | private TestableContext mContext = spy(getContext()); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 195 | private final String PKG = mContext.getPackageName(); |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 196 | private TestableLooper mTestableLooper; |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 197 | @Mock |
| 198 | private RankingHelper mRankingHelper; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 199 | @Mock private PreferencesHelper mPreferencesHelper; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 200 | AtomicFile mPolicyFile; |
| 201 | File mFile; |
| 202 | @Mock |
Geoffrey Pitsch | d5bcf21 | 2017-06-01 15:45:35 -0400 | [diff] [blame] | 203 | private NotificationUsageStats mUsageStats; |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 204 | @Mock |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 205 | private UsageStatsManagerInternal mAppUsageStats; |
| 206 | @Mock |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 207 | private AudioManager mAudioManager; |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 208 | @Mock |
| 209 | ActivityManager mActivityManager; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 210 | NotificationManagerService.WorkerHandler mHandler; |
Kristian Monsen | 05f3479 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 211 | @Mock |
| 212 | Resources mResources; |
Julia Reynolds | 3ff26d2 | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 213 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 214 | private NotificationChannel mTestNotificationChannel = new NotificationChannel( |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 215 | TEST_CHANNEL_ID, TEST_CHANNEL_ID, IMPORTANCE_DEFAULT); |
Gustav Sennton | a8e38aa | 2019-01-22 14:55:39 +0000 | [diff] [blame] | 216 | |
| 217 | private static final int NOTIFICATION_LOCATION_UNKNOWN = 0; |
| 218 | |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 219 | @Mock |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 220 | private NotificationListeners mListeners; |
| 221 | @Mock private NotificationAssistants mAssistants; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 222 | @Mock private ConditionProviders mConditionProviders; |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 223 | private ManagedServices.ManagedServiceInfo mListener; |
| 224 | @Mock private ICompanionDeviceManager mCompanionMgr; |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 225 | @Mock SnoozeHelper mSnoozeHelper; |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 226 | @Mock GroupHelper mGroupHelper; |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 227 | @Mock |
| 228 | IBinder mPermOwner; |
| 229 | @Mock |
| 230 | IActivityManager mAm; |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 231 | @Mock |
| 232 | IUriGrantsManager mUgm; |
| 233 | @Mock |
| 234 | UriGrantsManagerInternal mUgmInternal; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 235 | @Mock |
| 236 | AppOpsManager mAppOpsManager; |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 237 | @Mock |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 238 | private TestableNotificationManagerService.NotificationAssistantAccessGrantedCallback |
| 239 | mNotificationAssistantAccessGrantedCallback; |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 240 | @Mock |
| 241 | UserManager mUm; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 242 | |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 243 | // Use a Testable subclass so we can simulate calls from the system without failing. |
| 244 | private static class TestableNotificationManagerService extends NotificationManagerService { |
Julia Reynolds | d78263d | 2018-01-30 10:40:41 -0500 | [diff] [blame] | 245 | int countSystemChecks = 0; |
Brad Stenning | 8c991ea | 2018-07-31 13:33:01 -0700 | [diff] [blame] | 246 | boolean isSystemUid = true; |
Gustav Sennton | 44dc588 | 2018-12-13 14:38:50 +0000 | [diff] [blame] | 247 | int countLogSmartSuggestionsVisible = 0; |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 248 | @Nullable |
| 249 | NotificationAssistantAccessGrantedCallback mNotificationAssistantAccessGrantedCallback; |
Julia Reynolds | d78263d | 2018-01-30 10:40:41 -0500 | [diff] [blame] | 250 | |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 251 | TestableNotificationManagerService(Context context) { |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 252 | super(context); |
| 253 | } |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 254 | |
| 255 | @Override |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 256 | protected boolean isCallingUidSystem() { |
Julia Reynolds | d78263d | 2018-01-30 10:40:41 -0500 | [diff] [blame] | 257 | countSystemChecks++; |
Brad Stenning | 8c991ea | 2018-07-31 13:33:01 -0700 | [diff] [blame] | 258 | return isSystemUid; |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | @Override |
| 262 | protected boolean isCallerSystemOrPhone() { |
Julia Reynolds | d78263d | 2018-01-30 10:40:41 -0500 | [diff] [blame] | 263 | countSystemChecks++; |
Brad Stenning | 8c991ea | 2018-07-31 13:33:01 -0700 | [diff] [blame] | 264 | return isSystemUid; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 265 | } |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 266 | |
| 267 | @Override |
| 268 | protected ICompanionDeviceManager getCompanionManager() { |
| 269 | return null; |
| 270 | } |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 271 | |
| 272 | @Override |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 273 | protected void reportUserInteraction(NotificationRecord r) { |
| 274 | return; |
| 275 | } |
Julia Reynolds | b62dad4 | 2018-11-26 16:33:02 -0500 | [diff] [blame] | 276 | |
| 277 | @Override |
| 278 | protected void handleSavePolicyFile() { |
| 279 | return; |
| 280 | } |
Gustav Sennton | 44dc588 | 2018-12-13 14:38:50 +0000 | [diff] [blame] | 281 | |
| 282 | @Override |
Gustav Sennton | c7d0d32 | 2019-01-07 15:36:41 +0000 | [diff] [blame] | 283 | void logSmartSuggestionsVisible(NotificationRecord r, int notificationLocation) { |
| 284 | super.logSmartSuggestionsVisible(r, notificationLocation); |
Gustav Sennton | 44dc588 | 2018-12-13 14:38:50 +0000 | [diff] [blame] | 285 | countLogSmartSuggestionsVisible++; |
| 286 | } |
| 287 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 288 | @Override |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 289 | protected void setNotificationAssistantAccessGrantedForUserInternal( |
| 290 | ComponentName assistant, int userId, boolean granted) { |
| 291 | if (mNotificationAssistantAccessGrantedCallback != null) { |
| 292 | mNotificationAssistantAccessGrantedCallback.onGranted(assistant, userId, granted); |
| 293 | return; |
| 294 | } |
| 295 | super.setNotificationAssistantAccessGrantedForUserInternal(assistant, userId, granted); |
| 296 | } |
| 297 | |
| 298 | private void setNotificationAssistantAccessGrantedCallback( |
| 299 | @Nullable NotificationAssistantAccessGrantedCallback callback) { |
| 300 | this.mNotificationAssistantAccessGrantedCallback = callback; |
| 301 | } |
| 302 | |
| 303 | interface NotificationAssistantAccessGrantedCallback { |
| 304 | void onGranted(ComponentName assistant, int userId, boolean granted); |
| 305 | } |
| 306 | |
Mady Mellor | ca0c24c | 2019-05-16 16:14:32 -0700 | [diff] [blame] | 307 | @Override |
| 308 | protected boolean canLaunchInActivityView(Context context, PendingIntent pendingIntent, |
| 309 | String packageName) { |
| 310 | // Tests for this not being true are in CTS NotificationManagerTest |
| 311 | return true; |
| 312 | } |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 313 | } |
| 314 | |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 315 | private class TestableToastCallback extends ITransientNotification.Stub { |
| 316 | @Override |
| 317 | public void show(IBinder windowToken) { |
| 318 | } |
| 319 | |
| 320 | @Override |
| 321 | public void hide() { |
| 322 | } |
| 323 | } |
| 324 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 325 | @Before |
| 326 | public void setUp() throws Exception { |
Stanislav Zholnin | 872afd4 | 2019-03-12 15:57:25 +0000 | [diff] [blame] | 327 | InstrumentationRegistry.getInstrumentation().getUiAutomation().adoptShellPermissionIdentity( |
| 328 | "android.permission.WRITE_DEVICE_CONFIG", "android.permission.READ_DEVICE_CONFIG"); |
| 329 | |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 330 | MockitoAnnotations.initMocks(this); |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 331 | |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 332 | LocalServices.removeServiceForTest(UriGrantsManagerInternal.class); |
| 333 | LocalServices.addService(UriGrantsManagerInternal.class, mUgmInternal); |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 334 | LocalServices.removeServiceForTest(WindowManagerInternal.class); |
| 335 | LocalServices.addService(WindowManagerInternal.class, mWindowManagerInternal); |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 336 | |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 337 | doNothing().when(mContext).sendBroadcastAsUser(any(), any(), any()); |
| 338 | |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 339 | mService = new TestableNotificationManagerService(mContext); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 340 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 341 | // Use this testable looper. |
| 342 | mTestableLooper = TestableLooper.get(this); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 343 | mHandler = mService.new WorkerHandler(mTestableLooper.getLooper()); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 344 | // MockPackageManager - default returns ApplicationInfo with matching calling UID |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 345 | mContext.setMockPackageManager(mPackageManagerClient); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 346 | final ApplicationInfo applicationInfo = new ApplicationInfo(); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 347 | applicationInfo.uid = mUid; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 348 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), anyInt())) |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 349 | .thenReturn(applicationInfo); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 350 | when(mPackageManagerClient.getApplicationInfoAsUser(anyString(), anyInt(), anyInt())) |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 351 | .thenReturn(applicationInfo); |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 352 | when(mPackageManagerClient.getPackageUidAsUser(any(), anyInt())).thenReturn(mUid); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 353 | final LightsManager mockLightsManager = mock(LightsManager.class); |
| 354 | when(mockLightsManager.getLight(anyInt())).thenReturn(mock(Light.class)); |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 355 | when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_NORMAL); |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 356 | when(mPackageManagerClient.hasSystemFeature(FEATURE_WATCH)).thenReturn(false); |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 357 | when(mUgmInternal.newUriPermissionOwner(anyString())).thenReturn(mPermOwner); |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 358 | when(mPackageManager.getPackagesForUid(mUid)).thenReturn(new String[]{PKG}); |
Julia Reynolds | 4214da9 | 2019-04-10 15:04:06 -0400 | [diff] [blame] | 359 | when(mPackageManagerClient.getPackagesForUid(anyInt())).thenReturn(new String[]{PKG}); |
Julia Reynolds | e99db5a | 2019-04-16 12:50:04 -0400 | [diff] [blame] | 360 | mContext.addMockSystemService(AppOpsManager.class, mock(AppOpsManager.class)); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 361 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 362 | // 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] | 363 | mFile = new File(mContext.getCacheDir(), "test.xml"); |
| 364 | mFile.createNewFile(); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 365 | final String preupgradeXml = "<notification-policy></notification-policy>"; |
| 366 | mPolicyFile = new AtomicFile(mFile); |
| 367 | FileOutputStream fos = mPolicyFile.startWrite(); |
| 368 | fos.write(preupgradeXml.getBytes()); |
| 369 | mPolicyFile.finishWrite(fos); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 370 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 371 | // Setup managed services |
| 372 | mListener = mListeners.new ManagedServiceInfo( |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 373 | null, new ComponentName(PKG, "test_class"), mUid, true, null, 0); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 374 | when(mListeners.checkServiceTokenLocked(any())).thenReturn(mListener); |
| 375 | ManagedServices.Config listenerConfig = new ManagedServices.Config(); |
| 376 | listenerConfig.xmlTag = NotificationListeners.TAG_ENABLED_NOTIFICATION_LISTENERS; |
| 377 | when(mListeners.getConfig()).thenReturn(listenerConfig); |
| 378 | ManagedServices.Config assistantConfig = new ManagedServices.Config(); |
| 379 | assistantConfig.xmlTag = NotificationAssistants.TAG_ENABLED_NOTIFICATION_ASSISTANTS; |
| 380 | when(mAssistants.getConfig()).thenReturn(assistantConfig); |
| 381 | ManagedServices.Config dndConfig = new ManagedServices.Config(); |
| 382 | dndConfig.xmlTag = ConditionProviders.TAG_ENABLED_DND_APPS; |
| 383 | when(mConditionProviders.getConfig()).thenReturn(dndConfig); |
| 384 | |
Julia Reynolds | 418a8ff | 2019-03-21 10:45:10 -0400 | [diff] [blame] | 385 | when(mAssistants.isAdjustmentAllowed(anyString())).thenReturn(true); |
| 386 | |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 387 | |
| 388 | mService.init(mTestableLooper.getLooper(), |
| 389 | mPackageManager, mPackageManagerClient, mockLightsManager, |
| 390 | mListeners, mAssistants, mConditionProviders, |
| 391 | mCompanionMgr, mSnoozeHelper, mUsageStats, mPolicyFile, mActivityManager, |
| 392 | mGroupHelper, mAm, mAppUsageStats, |
| 393 | mock(DevicePolicyManagerInternal.class), mUgm, mUgmInternal, |
| 394 | mAppOpsManager, mUm); |
| 395 | mService.onBootPhase(SystemService.PHASE_SYSTEM_SERVICES_READY); |
| 396 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 397 | mService.setAudioManager(mAudioManager); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 398 | |
| 399 | // Tests call directly into the Binder. |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 400 | mBinderService = mService.getBinderService(); |
| 401 | mInternalService = mService.getInternalService(); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 402 | |
| 403 | mBinderService.createNotificationChannels( |
| 404 | PKG, new ParceledListSlice(Arrays.asList(mTestNotificationChannel))); |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 405 | assertNotNull(mBinderService.getNotificationChannel( |
| 406 | PKG, mContext.getUserId(), PKG, TEST_CHANNEL_ID)); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 407 | } |
| 408 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 409 | @After |
| 410 | public void tearDown() throws Exception { |
| 411 | mFile.delete(); |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 412 | clearDeviceConfig(); |
Stanislav Zholnin | 872afd4 | 2019-03-12 15:57:25 +0000 | [diff] [blame] | 413 | InstrumentationRegistry.getInstrumentation() |
| 414 | .getUiAutomation().dropShellPermissionIdentity(); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 415 | } |
| 416 | |
Julia Reynolds | 7bcb57b | 2018-01-22 10:37:58 -0500 | [diff] [blame] | 417 | public void waitForIdle() { |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 418 | mTestableLooper.processAllMessages(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 419 | } |
| 420 | |
Mady Mellor | c682034 | 2019-05-20 12:04:36 -0700 | [diff] [blame] | 421 | private void setUpPrefsForBubbles(boolean globalEnabled, boolean pkgEnabled, |
| 422 | boolean channelEnabled) { |
| 423 | mService.setPreferencesHelper(mPreferencesHelper); |
Lyn Han | 4463f84 | 2019-07-09 15:27:28 -0700 | [diff] [blame] | 424 | when(mPreferencesHelper.bubblesEnabled()).thenReturn(globalEnabled); |
Mady Mellor | c682034 | 2019-05-20 12:04:36 -0700 | [diff] [blame] | 425 | when(mPreferencesHelper.areBubblesAllowed(anyString(), anyInt())).thenReturn(pkgEnabled); |
| 426 | when(mPreferencesHelper.getNotificationChannel( |
| 427 | anyString(), anyInt(), anyString(), anyBoolean())).thenReturn( |
| 428 | mTestNotificationChannel); |
| 429 | when(mPreferencesHelper.getImportance(anyString(), anyInt())).thenReturn( |
| 430 | mTestNotificationChannel.getImportance()); |
| 431 | mTestNotificationChannel.setAllowBubbles(channelEnabled); |
| 432 | } |
| 433 | |
Julia Reynolds | 7bcb57b | 2018-01-22 10:37:58 -0500 | [diff] [blame] | 434 | private StatusBarNotification generateSbn(String pkg, int uid, long postTime, int userId) { |
| 435 | Notification.Builder nb = new Notification.Builder(mContext, "a") |
| 436 | .setContentTitle("foo") |
| 437 | .setSmallIcon(android.R.drawable.sym_def_app_icon); |
| 438 | StatusBarNotification sbn = new StatusBarNotification(pkg, pkg, uid, "tag", uid, 0, |
| 439 | nb.build(), new UserHandle(userId), null, postTime); |
| 440 | return sbn; |
| 441 | } |
| 442 | |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 443 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 444 | private NotificationRecord generateNotificationRecord(NotificationChannel channel, int id, |
| 445 | String groupKey, boolean isSummary) { |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 446 | return generateNotificationRecord(channel, id, groupKey, isSummary, false /* isBubble */); |
| 447 | } |
| 448 | |
| 449 | private NotificationRecord generateNotificationRecord(NotificationChannel channel, int id, |
| 450 | String groupKey, boolean isSummary, boolean isBubble) { |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 451 | Notification.Builder nb = new Notification.Builder(mContext, channel.getId()) |
| 452 | .setContentTitle("foo") |
| 453 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 454 | .setGroup(groupKey) |
| 455 | .setGroupSummary(isSummary); |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 456 | if (isBubble) { |
| 457 | nb.setBubbleMetadata(getBasicBubbleMetadataBuilder().build()); |
| 458 | } |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 459 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, id, "tag", mUid, 0, |
| 460 | nb.build(), new UserHandle(mUid), null, 0); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 461 | return new NotificationRecord(mContext, sbn, channel); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 462 | } |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 463 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 464 | private NotificationRecord generateNotificationRecord(NotificationChannel channel) { |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 465 | return generateNotificationRecord(channel, null); |
| 466 | } |
| 467 | |
| 468 | private NotificationRecord generateNotificationRecord(NotificationChannel channel, |
| 469 | Notification.TvExtender extender) { |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 470 | return generateNotificationRecord(channel, extender, false /* isBubble */); |
| 471 | } |
| 472 | |
| 473 | private NotificationRecord generateNotificationRecord(NotificationChannel channel, |
| 474 | Notification.TvExtender extender, boolean isBubble) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 475 | if (channel == null) { |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 476 | channel = mTestNotificationChannel; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 477 | } |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 478 | Notification.Builder nb = new Notification.Builder(mContext, channel.getId()) |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 479 | .setContentTitle("foo") |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 480 | .setSmallIcon(android.R.drawable.sym_def_app_icon); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 481 | if (extender != null) { |
| 482 | nb.extend(extender); |
| 483 | } |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 484 | if (isBubble) { |
| 485 | nb.setBubbleMetadata(getBasicBubbleMetadataBuilder().build()); |
| 486 | } |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 487 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, "tag", mUid, 0, |
| 488 | nb.build(), new UserHandle(mUid), null, 0); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 489 | return new NotificationRecord(mContext, sbn, channel); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 490 | } |
| 491 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 492 | private Map<String, Answer> getSignalExtractorSideEffects() { |
| 493 | Map<String, Answer> answers = new ArrayMap<>(); |
| 494 | |
| 495 | answers.put("override group key", invocationOnMock -> { |
| 496 | ((NotificationRecord) invocationOnMock.getArguments()[0]) |
| 497 | .setOverrideGroupKey("bananas"); |
| 498 | return null; |
| 499 | }); |
| 500 | answers.put("override people", invocationOnMock -> { |
| 501 | ((NotificationRecord) invocationOnMock.getArguments()[0]) |
| 502 | .setPeopleOverride(new ArrayList<>()); |
| 503 | return null; |
| 504 | }); |
| 505 | answers.put("snooze criteria", invocationOnMock -> { |
| 506 | ((NotificationRecord) invocationOnMock.getArguments()[0]) |
| 507 | .setSnoozeCriteria(new ArrayList<>()); |
| 508 | return null; |
| 509 | }); |
| 510 | answers.put("notification channel", invocationOnMock -> { |
| 511 | ((NotificationRecord) invocationOnMock.getArguments()[0]) |
| 512 | .updateNotificationChannel(new NotificationChannel("a", "", IMPORTANCE_LOW)); |
| 513 | return null; |
| 514 | }); |
| 515 | answers.put("badging", invocationOnMock -> { |
| 516 | NotificationRecord r = (NotificationRecord) invocationOnMock.getArguments()[0]; |
| 517 | r.setShowBadge(!r.canShowBadge()); |
| 518 | return null; |
| 519 | }); |
Julia Reynolds | 4509ce7 | 2019-01-31 13:12:43 -0500 | [diff] [blame] | 520 | answers.put("bubbles", invocationOnMock -> { |
| 521 | NotificationRecord r = (NotificationRecord) invocationOnMock.getArguments()[0]; |
| 522 | r.setAllowBubble(!r.canBubble()); |
| 523 | return null; |
| 524 | }); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 525 | answers.put("package visibility", invocationOnMock -> { |
| 526 | ((NotificationRecord) invocationOnMock.getArguments()[0]).setPackageVisibilityOverride( |
| 527 | Notification.VISIBILITY_SECRET); |
| 528 | return null; |
| 529 | }); |
| 530 | |
| 531 | return answers; |
| 532 | } |
| 533 | |
Julia Reynolds | c7dcdc2 | 2019-03-25 10:26:14 -0400 | [diff] [blame] | 534 | private void clearDeviceConfig() { |
| 535 | DeviceConfig.resetToDefaults( |
| 536 | Settings.RESET_MODE_PACKAGE_DEFAULTS, DeviceConfig.NAMESPACE_SYSTEMUI); |
| 537 | } |
| 538 | |
| 539 | private void setDefaultAssistantInDeviceConfig(String componentName) { |
| 540 | DeviceConfig.setProperty( |
| 541 | DeviceConfig.NAMESPACE_SYSTEMUI, |
| 542 | SystemUiDeviceConfigFlags.NAS_DEFAULT_SERVICE, |
| 543 | componentName, |
| 544 | false); |
| 545 | } |
| 546 | |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 547 | private Notification.BubbleMetadata.Builder getBasicBubbleMetadataBuilder() { |
| 548 | PendingIntent pi = PendingIntent.getActivity(mContext, 0, new Intent(), 0); |
| 549 | return new Notification.BubbleMetadata.Builder() |
| 550 | .setIntent(pi) |
| 551 | .setIcon(Icon.createWithResource(mContext, android.R.drawable.sym_def_app_icon)); |
| 552 | } |
| 553 | |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 554 | private NotificationRecord addGroupWithBubblesAndValidateAdded(boolean summaryAutoCancel) |
| 555 | throws RemoteException { |
| 556 | |
| 557 | // Notification that has bubble metadata |
| 558 | NotificationRecord nrBubble = generateNotificationRecord(mTestNotificationChannel, 1, |
| 559 | "BUBBLE_GROUP", false /* isSummary */, true /* isBubble */); |
| 560 | |
| 561 | // Make the package foreground so that we're allowed to be a bubble |
| 562 | when(mActivityManager.getPackageImportance(nrBubble.sbn.getPackageName())).thenReturn( |
| 563 | IMPORTANCE_FOREGROUND); |
| 564 | |
| 565 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
| 566 | nrBubble.sbn.getId(), nrBubble.sbn.getNotification(), nrBubble.sbn.getUserId()); |
| 567 | waitForIdle(); |
| 568 | |
| 569 | // Make sure we are a bubble |
| 570 | StatusBarNotification[] notifsAfter = mBinderService.getActiveNotifications(PKG); |
| 571 | assertEquals(1, notifsAfter.length); |
| 572 | assertTrue((notifsAfter[0].getNotification().flags & FLAG_BUBBLE) != 0); |
| 573 | |
| 574 | // Plain notification without bubble metadata |
| 575 | NotificationRecord nrPlain = generateNotificationRecord(mTestNotificationChannel, 2, |
| 576 | "BUBBLE_GROUP", false /* isSummary */, false /* isBubble */); |
| 577 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
| 578 | nrPlain.sbn.getId(), nrPlain.sbn.getNotification(), nrPlain.sbn.getUserId()); |
| 579 | waitForIdle(); |
| 580 | |
| 581 | notifsAfter = mBinderService.getActiveNotifications(PKG); |
| 582 | assertEquals(2, notifsAfter.length); |
| 583 | |
| 584 | // Summary notification for both of those |
| 585 | NotificationRecord nrSummary = generateNotificationRecord(mTestNotificationChannel, 3, |
| 586 | "BUBBLE_GROUP", true /* isSummary */, false /* isBubble */); |
| 587 | if (summaryAutoCancel) { |
| 588 | nrSummary.getNotification().flags |= FLAG_AUTO_CANCEL; |
| 589 | } |
| 590 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
| 591 | nrSummary.sbn.getId(), nrSummary.sbn.getNotification(), nrSummary.sbn.getUserId()); |
| 592 | waitForIdle(); |
| 593 | |
| 594 | notifsAfter = mBinderService.getActiveNotifications(PKG); |
| 595 | assertEquals(3, notifsAfter.length); |
| 596 | |
| 597 | return nrSummary; |
| 598 | } |
| 599 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 600 | @Test |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 601 | public void testCreateNotificationChannels_SingleChannel() throws Exception { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 602 | final NotificationChannel channel = |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 603 | new NotificationChannel("id", "name", IMPORTANCE_DEFAULT); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 604 | mBinderService.createNotificationChannels(PKG, |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 605 | new ParceledListSlice(Arrays.asList(channel))); |
| 606 | final NotificationChannel createdChannel = |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 607 | mBinderService.getNotificationChannel(PKG, mContext.getUserId(), PKG, "id"); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 608 | assertTrue(createdChannel != null); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 609 | } |
| 610 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 611 | @Test |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 612 | public void testCreateNotificationChannels_NullChannelThrowsException() throws Exception { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 613 | try { |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 614 | mBinderService.createNotificationChannels(PKG, |
Kristian Monsen | 05f3479 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 615 | new ParceledListSlice(Arrays.asList((Object[])null))); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 616 | fail("Exception should be thrown immediately."); |
| 617 | } catch (NullPointerException e) { |
| 618 | // pass |
| 619 | } |
| 620 | } |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 621 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 622 | @Test |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 623 | public void testCreateNotificationChannels_TwoChannels() throws Exception { |
| 624 | final NotificationChannel channel1 = |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 625 | new NotificationChannel("id1", "name", IMPORTANCE_DEFAULT); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 626 | final NotificationChannel channel2 = |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 627 | new NotificationChannel("id2", "name", IMPORTANCE_DEFAULT); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 628 | mBinderService.createNotificationChannels(PKG, |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 629 | new ParceledListSlice(Arrays.asList(channel1, channel2))); |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 630 | assertTrue(mBinderService.getNotificationChannel( |
| 631 | PKG, mContext.getUserId(), PKG, "id1") != null); |
| 632 | assertTrue(mBinderService.getNotificationChannel( |
| 633 | PKG, mContext.getUserId(), PKG, "id2") != null); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 634 | } |
| 635 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 636 | @Test |
Geoffrey Pitsch | 76a3aa0 | 2017-07-26 15:07:34 -0400 | [diff] [blame] | 637 | public void testCreateNotificationChannels_SecondCreateDoesNotChangeImportance() |
| 638 | throws Exception { |
| 639 | final NotificationChannel channel = |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 640 | new NotificationChannel("id", "name", IMPORTANCE_DEFAULT); |
Geoffrey Pitsch | 76a3aa0 | 2017-07-26 15:07:34 -0400 | [diff] [blame] | 641 | mBinderService.createNotificationChannels(PKG, |
| 642 | new ParceledListSlice(Arrays.asList(channel))); |
| 643 | |
| 644 | // Recreating the channel doesn't throw, but ignores importance. |
| 645 | final NotificationChannel dupeChannel = |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 646 | new NotificationChannel("id", "name", IMPORTANCE_HIGH); |
Geoffrey Pitsch | 76a3aa0 | 2017-07-26 15:07:34 -0400 | [diff] [blame] | 647 | mBinderService.createNotificationChannels(PKG, |
| 648 | new ParceledListSlice(Arrays.asList(dupeChannel))); |
| 649 | final NotificationChannel createdChannel = |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 650 | mBinderService.getNotificationChannel(PKG, mContext.getUserId(), PKG, "id"); |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 651 | assertEquals(IMPORTANCE_DEFAULT, createdChannel.getImportance()); |
Geoffrey Pitsch | 76a3aa0 | 2017-07-26 15:07:34 -0400 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | @Test |
| 655 | public void testCreateNotificationChannels_SecondCreateAllowedToDowngradeImportance() |
| 656 | throws Exception { |
| 657 | final NotificationChannel channel = |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 658 | new NotificationChannel("id", "name", IMPORTANCE_DEFAULT); |
Geoffrey Pitsch | 76a3aa0 | 2017-07-26 15:07:34 -0400 | [diff] [blame] | 659 | mBinderService.createNotificationChannels(PKG, |
| 660 | new ParceledListSlice(Arrays.asList(channel))); |
| 661 | |
| 662 | // Recreating with a lower importance is allowed to modify the channel. |
| 663 | final NotificationChannel dupeChannel = |
| 664 | new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_LOW); |
| 665 | mBinderService.createNotificationChannels(PKG, |
| 666 | new ParceledListSlice(Arrays.asList(dupeChannel))); |
| 667 | final NotificationChannel createdChannel = |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 668 | mBinderService.getNotificationChannel(PKG, mContext.getUserId(), PKG, "id"); |
Geoffrey Pitsch | 76a3aa0 | 2017-07-26 15:07:34 -0400 | [diff] [blame] | 669 | assertEquals(NotificationManager.IMPORTANCE_LOW, createdChannel.getImportance()); |
| 670 | } |
| 671 | |
| 672 | @Test |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 673 | public void testCreateNotificationChannels_CannotDowngradeImportanceIfAlreadyUpdated() |
| 674 | throws Exception { |
| 675 | final NotificationChannel channel = |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 676 | new NotificationChannel("id", "name", IMPORTANCE_DEFAULT); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 677 | mBinderService.createNotificationChannels(PKG, |
| 678 | new ParceledListSlice(Arrays.asList(channel))); |
| 679 | |
| 680 | // The user modifies importance directly, can no longer be changed by the app. |
| 681 | final NotificationChannel updatedChannel = |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 682 | new NotificationChannel("id", "name", IMPORTANCE_HIGH); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 683 | mBinderService.updateNotificationChannelForPackage(PKG, mUid, updatedChannel); |
| 684 | |
| 685 | // Recreating with a lower importance leaves channel unchanged. |
| 686 | final NotificationChannel dupeChannel = |
| 687 | new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_LOW); |
| 688 | mBinderService.createNotificationChannels(PKG, |
| 689 | new ParceledListSlice(Arrays.asList(dupeChannel))); |
| 690 | final NotificationChannel createdChannel = |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 691 | mBinderService.getNotificationChannel(PKG, mContext.getUserId(), PKG, "id"); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 692 | assertEquals(IMPORTANCE_HIGH, createdChannel.getImportance()); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | @Test |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 696 | public void testCreateNotificationChannels_IdenticalChannelsInListIgnoresSecond() |
| 697 | throws Exception { |
| 698 | final NotificationChannel channel1 = |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 699 | new NotificationChannel("id", "name", IMPORTANCE_DEFAULT); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 700 | final NotificationChannel channel2 = |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 701 | new NotificationChannel("id", "name", IMPORTANCE_HIGH); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 702 | mBinderService.createNotificationChannels(PKG, |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 703 | new ParceledListSlice(Arrays.asList(channel1, channel2))); |
| 704 | final NotificationChannel createdChannel = |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 705 | mBinderService.getNotificationChannel(PKG, mContext.getUserId(), PKG, "id"); |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 706 | assertEquals(IMPORTANCE_DEFAULT, createdChannel.getImportance()); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 707 | } |
| 708 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 709 | @Test |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 710 | public void testBlockedNotifications_suspended() throws Exception { |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 711 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(true); |
| 712 | |
| 713 | NotificationChannel channel = new NotificationChannel("id", "name", |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 714 | IMPORTANCE_HIGH); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 715 | NotificationRecord r = generateNotificationRecord(channel); |
Beverly | 3c707b4 | 2018-09-14 09:49:07 -0400 | [diff] [blame] | 716 | |
| 717 | // isBlocked is only used for user blocking, not app suspension |
| 718 | assertFalse(mService.isBlocked(r, mUsageStats)); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 719 | } |
| 720 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 721 | @Test |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 722 | public void testBlockedNotifications_blockedChannel() throws Exception { |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 723 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false); |
| 724 | |
| 725 | NotificationChannel channel = new NotificationChannel("id", "name", |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 726 | NotificationManager.IMPORTANCE_NONE); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 727 | NotificationRecord r = generateNotificationRecord(channel); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 728 | assertTrue(mService.isBlocked(r, mUsageStats)); |
Geoffrey Pitsch | d5bcf21 | 2017-06-01 15:45:35 -0400 | [diff] [blame] | 729 | verify(mUsageStats, times(1)).registerBlocked(eq(r)); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 730 | |
| 731 | mBinderService.createNotificationChannels( |
| 732 | PKG, new ParceledListSlice(Arrays.asList(channel))); |
| 733 | final StatusBarNotification sbn = generateNotificationRecord(channel).sbn; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 734 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 735 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 736 | waitForIdle(); |
| 737 | assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length); |
| 738 | } |
| 739 | |
| 740 | @Test |
| 741 | public void testEnqueuedBlockedNotifications_appBlockedChannelForegroundService() |
| 742 | throws Exception { |
| 743 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false); |
| 744 | |
| 745 | NotificationChannel channel = new NotificationChannel("blocked", "name", |
| 746 | NotificationManager.IMPORTANCE_NONE); |
| 747 | mBinderService.createNotificationChannels( |
| 748 | PKG, new ParceledListSlice(Arrays.asList(channel))); |
| 749 | |
| 750 | final StatusBarNotification sbn = generateNotificationRecord(channel).sbn; |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 751 | sbn.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 752 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 753 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 754 | waitForIdle(); |
| 755 | assertEquals(1, mBinderService.getActiveNotifications(sbn.getPackageName()).length); |
| 756 | assertEquals(IMPORTANCE_LOW, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 757 | mService.getNotificationRecord(sbn.getKey()).getImportance()); |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 758 | assertEquals(IMPORTANCE_LOW, mBinderService.getNotificationChannel( |
| 759 | PKG, mContext.getUserId(), PKG, channel.getId()).getImportance()); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | @Test |
| 763 | public void testEnqueuedBlockedNotifications_userBlockedChannelForegroundService() |
| 764 | throws Exception { |
| 765 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false); |
| 766 | |
| 767 | NotificationChannel channel = |
| 768 | new NotificationChannel("blockedbyuser", "name", IMPORTANCE_HIGH); |
| 769 | mBinderService.createNotificationChannels( |
| 770 | PKG, new ParceledListSlice(Arrays.asList(channel))); |
| 771 | |
| 772 | NotificationChannel update = |
| 773 | new NotificationChannel("blockedbyuser", "name", IMPORTANCE_NONE); |
| 774 | mBinderService.updateNotificationChannelForPackage(PKG, mUid, update); |
| 775 | waitForIdle(); |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 776 | assertEquals(IMPORTANCE_NONE, mBinderService.getNotificationChannel( |
| 777 | PKG, mContext.getUserId(), PKG, channel.getId()).getImportance()); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 778 | |
Dianne Hackborn | 025d4a5 | 2018-04-30 16:23:26 -0700 | [diff] [blame] | 779 | StatusBarNotification sbn = generateNotificationRecord(channel).sbn; |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 780 | sbn.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 781 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 782 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 783 | waitForIdle(); |
Dianne Hackborn | 025d4a5 | 2018-04-30 16:23:26 -0700 | [diff] [blame] | 784 | // The first time a foreground service notification is shown, we allow the channel |
| 785 | // to be updated to allow it to be seen. |
| 786 | assertEquals(1, mBinderService.getActiveNotifications(sbn.getPackageName()).length); |
| 787 | assertEquals(IMPORTANCE_LOW, |
| 788 | mService.getNotificationRecord(sbn.getKey()).getImportance()); |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 789 | assertEquals(IMPORTANCE_LOW, mBinderService.getNotificationChannel( |
| 790 | PKG, mContext.getUserId(), PKG, channel.getId()).getImportance()); |
Dianne Hackborn | 025d4a5 | 2018-04-30 16:23:26 -0700 | [diff] [blame] | 791 | mBinderService.cancelNotificationWithTag(PKG, "tag", sbn.getId(), sbn.getUserId()); |
| 792 | waitForIdle(); |
| 793 | |
| 794 | update = new NotificationChannel("blockedbyuser", "name", IMPORTANCE_NONE); |
| 795 | update.setFgServiceShown(true); |
| 796 | mBinderService.updateNotificationChannelForPackage(PKG, mUid, update); |
| 797 | waitForIdle(); |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 798 | assertEquals(IMPORTANCE_NONE, mBinderService.getNotificationChannel( |
| 799 | PKG, mContext.getUserId(), PKG, channel.getId()).getImportance()); |
Dianne Hackborn | 025d4a5 | 2018-04-30 16:23:26 -0700 | [diff] [blame] | 800 | |
| 801 | sbn = generateNotificationRecord(channel).sbn; |
| 802 | sbn.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 803 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
Dianne Hackborn | 025d4a5 | 2018-04-30 16:23:26 -0700 | [diff] [blame] | 804 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 805 | waitForIdle(); |
| 806 | // The second time it is shown, we keep the user's preference. |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 807 | assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 808 | assertNull(mService.getNotificationRecord(sbn.getKey())); |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 809 | assertEquals(IMPORTANCE_NONE, mBinderService.getNotificationChannel( |
| 810 | PKG, mContext.getUserId(), PKG, channel.getId()).getImportance()); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 811 | } |
| 812 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 813 | @Test |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 814 | public void testBlockedNotifications_blockedChannelGroup() throws Exception { |
| 815 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 816 | mService.setPreferencesHelper(mPreferencesHelper); |
| 817 | when(mPreferencesHelper.isGroupBlocked(anyString(), anyInt(), anyString())).thenReturn(true); |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 818 | |
| 819 | NotificationChannel channel = new NotificationChannel("id", "name", |
| 820 | NotificationManager.IMPORTANCE_HIGH); |
| 821 | channel.setGroup("something"); |
| 822 | NotificationRecord r = generateNotificationRecord(channel); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 823 | assertTrue(mService.isBlocked(r, mUsageStats)); |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 824 | verify(mUsageStats, times(1)).registerBlocked(eq(r)); |
| 825 | } |
| 826 | |
| 827 | @Test |
Julia Reynolds | 4da7970 | 2017-06-01 11:06:10 -0400 | [diff] [blame] | 828 | public void testEnqueuedBlockedNotifications_blockedApp() throws Exception { |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 829 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false); |
| 830 | |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 831 | mBinderService.setNotificationsEnabledForPackage(PKG, mUid, false); |
Julia Reynolds | 4da7970 | 2017-06-01 11:06:10 -0400 | [diff] [blame] | 832 | |
| 833 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 834 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
Julia Reynolds | 4da7970 | 2017-06-01 11:06:10 -0400 | [diff] [blame] | 835 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 836 | waitForIdle(); |
| 837 | assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 838 | } |
| 839 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 840 | @Test |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 841 | public void testEnqueuedBlockedNotifications_blockedAppForegroundService() throws Exception { |
| 842 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false); |
| 843 | |
| 844 | mBinderService.setNotificationsEnabledForPackage(PKG, mUid, false); |
| 845 | |
| 846 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 847 | sbn.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 848 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 849 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 850 | waitForIdle(); |
| 851 | assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 852 | assertNull(mService.getNotificationRecord(sbn.getKey())); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 853 | } |
| 854 | |
Brad Stenning | 8c991ea | 2018-07-31 13:33:01 -0700 | [diff] [blame] | 855 | /** |
| 856 | * Confirm the system user on automotive devices can use car categories |
| 857 | */ |
| 858 | @Test |
| 859 | public void testEnqueuedRestrictedNotifications_asSystem() throws Exception { |
| 860 | when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE, 0)) |
| 861 | .thenReturn(true); |
| 862 | List<String> categories = Arrays.asList(Notification.CATEGORY_CAR_EMERGENCY, |
| 863 | Notification.CATEGORY_CAR_WARNING, |
| 864 | Notification.CATEGORY_CAR_INFORMATION); |
| 865 | int id = 0; |
| 866 | for (String category: categories) { |
| 867 | final StatusBarNotification sbn = |
| 868 | generateNotificationRecord(mTestNotificationChannel, ++id, "", false).sbn; |
| 869 | sbn.getNotification().category = category; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 870 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
Brad Stenning | 8c991ea | 2018-07-31 13:33:01 -0700 | [diff] [blame] | 871 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 872 | } |
| 873 | waitForIdle(); |
| 874 | assertEquals(categories.size(), mBinderService.getActiveNotifications(PKG).length); |
| 875 | } |
| 876 | |
| 877 | |
| 878 | /** |
| 879 | * Confirm restricted notification categories only apply to automotive. |
| 880 | */ |
| 881 | @Test |
| 882 | public void testEnqueuedRestrictedNotifications_notAutomotive() throws Exception { |
| 883 | mService.isSystemUid = false; |
| 884 | when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE, 0)) |
| 885 | .thenReturn(false); |
| 886 | List<String> categories = Arrays.asList(Notification.CATEGORY_CAR_EMERGENCY, |
| 887 | Notification.CATEGORY_CAR_WARNING, |
| 888 | Notification.CATEGORY_CAR_INFORMATION); |
| 889 | int id = 0; |
| 890 | for (String category: categories) { |
| 891 | final StatusBarNotification sbn = |
| 892 | generateNotificationRecord(mTestNotificationChannel, ++id, "", false).sbn; |
| 893 | sbn.getNotification().category = category; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 894 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
Brad Stenning | 8c991ea | 2018-07-31 13:33:01 -0700 | [diff] [blame] | 895 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 896 | } |
| 897 | waitForIdle(); |
| 898 | assertEquals(categories.size(), mBinderService.getActiveNotifications(PKG).length); |
| 899 | } |
| 900 | |
| 901 | /** |
| 902 | * Confirm if a non-system user tries to use the car categories on a automotive device that |
| 903 | * they will get a security exception |
| 904 | */ |
| 905 | @Test |
| 906 | public void testEnqueuedRestrictedNotifications_badUser() throws Exception { |
| 907 | mService.isSystemUid = false; |
| 908 | when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE, 0)) |
| 909 | .thenReturn(true); |
| 910 | List<String> categories = Arrays.asList(Notification.CATEGORY_CAR_EMERGENCY, |
| 911 | Notification.CATEGORY_CAR_WARNING, |
| 912 | Notification.CATEGORY_CAR_INFORMATION); |
| 913 | for (String category: categories) { |
| 914 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
| 915 | sbn.getNotification().category = category; |
| 916 | try { |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 917 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
Brad Stenning | 8c991ea | 2018-07-31 13:33:01 -0700 | [diff] [blame] | 918 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 919 | fail("Calls from non system apps should not allow use of restricted categories"); |
| 920 | } catch (SecurityException e) { |
| 921 | // pass |
| 922 | } |
| 923 | } |
| 924 | waitForIdle(); |
| 925 | assertEquals(0, mBinderService.getActiveNotifications(PKG).length); |
| 926 | } |
| 927 | |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 928 | @Test |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 929 | public void testBlockedNotifications_blockedByAssistant() throws Exception { |
| 930 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false); |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 931 | when(mAssistants.isSameUser(any(), anyInt())).thenReturn(true); |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 932 | |
| 933 | NotificationChannel channel = new NotificationChannel("id", "name", |
| 934 | NotificationManager.IMPORTANCE_HIGH); |
| 935 | NotificationRecord r = generateNotificationRecord(channel); |
| 936 | mService.addEnqueuedNotification(r); |
| 937 | |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 938 | Bundle bundle = new Bundle(); |
Julia Reynolds | c7dcdc2 | 2019-03-25 10:26:14 -0400 | [diff] [blame] | 939 | bundle.putInt(KEY_IMPORTANCE, IMPORTANCE_NONE); |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 940 | Adjustment adjustment = new Adjustment( |
| 941 | r.sbn.getPackageName(), r.getKey(), bundle, "", r.getUser().getIdentifier()); |
| 942 | mBinderService.applyEnqueuedAdjustmentFromAssistant(null, adjustment); |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 943 | |
| 944 | NotificationManagerService.PostNotificationRunnable runnable = |
| 945 | mService.new PostNotificationRunnable(r.getKey()); |
| 946 | runnable.run(); |
| 947 | waitForIdle(); |
| 948 | |
| 949 | verify(mUsageStats, never()).registerPostedByApp(any()); |
| 950 | } |
| 951 | |
| 952 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 953 | public void testEnqueueNotificationWithTag_PopulatesGetActiveNotifications() throws Exception { |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 954 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", 0, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 955 | generateNotificationRecord(null).getNotification(), 0); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 956 | waitForIdle(); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 957 | StatusBarNotification[] notifs = mBinderService.getActiveNotifications(PKG); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 958 | assertEquals(1, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 959 | assertEquals(1, mService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 960 | } |
| 961 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 962 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 963 | public void testCancelNotificationImmediatelyAfterEnqueue() throws Exception { |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 964 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", 0, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 965 | generateNotificationRecord(null).getNotification(), 0); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 966 | mBinderService.cancelNotificationWithTag(PKG, "tag", 0, 0); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 967 | waitForIdle(); |
| 968 | StatusBarNotification[] notifs = |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 969 | mBinderService.getActiveNotifications(PKG); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 970 | assertEquals(0, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 971 | assertEquals(0, mService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 972 | } |
| 973 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 974 | @Test |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 975 | public void testCancelNotificationWhilePostedAndEnqueued() throws Exception { |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 976 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", 0, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 977 | generateNotificationRecord(null).getNotification(), 0); |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 978 | waitForIdle(); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 979 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", 0, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 980 | generateNotificationRecord(null).getNotification(), 0); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 981 | mBinderService.cancelNotificationWithTag(PKG, "tag", 0, 0); |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 982 | waitForIdle(); |
| 983 | StatusBarNotification[] notifs = |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 984 | mBinderService.getActiveNotifications(PKG); |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 985 | assertEquals(0, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 986 | assertEquals(0, mService.getNotificationRecordCount()); |
| 987 | ArgumentCaptor<NotificationStats> captor = ArgumentCaptor.forClass(NotificationStats.class); |
| 988 | verify(mListeners, times(1)).notifyRemovedLocked(any(), anyInt(), captor.capture()); |
| 989 | assertEquals(NotificationStats.DISMISSAL_OTHER, captor.getValue().getDismissalSurface()); |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 993 | public void testCancelNotificationsFromListenerImmediatelyAfterEnqueue() throws Exception { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 994 | NotificationRecord r = generateNotificationRecord(null); |
| 995 | final StatusBarNotification sbn = r.sbn; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 996 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 997 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 998 | mBinderService.cancelNotificationsFromListener(null, null); |
| 999 | waitForIdle(); |
| 1000 | StatusBarNotification[] notifs = |
| 1001 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 1002 | assertEquals(0, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1003 | assertEquals(0, mService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1004 | } |
| 1005 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 1006 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1007 | public void testCancelAllNotificationsImmediatelyAfterEnqueue() throws Exception { |
| 1008 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1009 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1010 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1011 | mBinderService.cancelAllNotifications(PKG, sbn.getUserId()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1012 | waitForIdle(); |
| 1013 | StatusBarNotification[] notifs = |
| 1014 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 1015 | assertEquals(0, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1016 | assertEquals(0, mService.getNotificationRecordCount()); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | @Test |
Evan Laird | 0559764 | 2019-08-05 17:11:54 -0400 | [diff] [blame] | 1020 | public void testCancelImmediatelyAfterEnqueueNotifiesListeners_ForegroundServiceFlag() |
| 1021 | throws Exception { |
| 1022 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
| 1023 | sbn.getNotification().flags = |
| 1024 | Notification.FLAG_ONGOING_EVENT | FLAG_FOREGROUND_SERVICE; |
| 1025 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
| 1026 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 1027 | mBinderService.cancelNotificationWithTag(PKG, "tag", sbn.getId(), sbn.getUserId()); |
| 1028 | waitForIdle(); |
| 1029 | verify(mListeners, times(1)).notifyPostedLocked(any(), any()); |
| 1030 | verify(mListeners, times(1)).notifyRemovedLocked(any(), anyInt(), any()); |
| 1031 | } |
| 1032 | |
| 1033 | @Test |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1034 | public void testUserInitiatedClearAll_noLeak() throws Exception { |
| 1035 | final NotificationRecord n = generateNotificationRecord( |
| 1036 | mTestNotificationChannel, 1, "group", true); |
| 1037 | |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1038 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1039 | n.sbn.getId(), n.sbn.getNotification(), n.sbn.getUserId()); |
| 1040 | waitForIdle(); |
| 1041 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1042 | mService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(), |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1043 | n.getUserId()); |
| 1044 | waitForIdle(); |
| 1045 | StatusBarNotification[] notifs = |
| 1046 | mBinderService.getActiveNotifications(n.sbn.getPackageName()); |
| 1047 | assertEquals(0, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1048 | assertEquals(0, mService.getNotificationRecordCount()); |
| 1049 | ArgumentCaptor<NotificationStats> captor = ArgumentCaptor.forClass(NotificationStats.class); |
| 1050 | verify(mListeners, times(1)).notifyRemovedLocked(any(), anyInt(), captor.capture()); |
| 1051 | assertEquals(NotificationStats.DISMISSAL_OTHER, captor.getValue().getDismissalSurface()); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | @Test |
| 1055 | public void testCancelAllNotificationsCancelsChildren() throws Exception { |
| 1056 | final NotificationRecord parent = generateNotificationRecord( |
| 1057 | mTestNotificationChannel, 1, "group1", true); |
| 1058 | final NotificationRecord child = generateNotificationRecord( |
| 1059 | mTestNotificationChannel, 2, "group1", false); |
| 1060 | |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1061 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1062 | parent.sbn.getId(), parent.sbn.getNotification(), parent.sbn.getUserId()); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1063 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1064 | child.sbn.getId(), child.sbn.getNotification(), child.sbn.getUserId()); |
| 1065 | waitForIdle(); |
| 1066 | |
| 1067 | mBinderService.cancelAllNotifications(PKG, parent.sbn.getUserId()); |
| 1068 | waitForIdle(); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1069 | assertEquals(0, mService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1070 | } |
| 1071 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 1072 | @Test |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 1073 | public void testCancelAllNotificationsMultipleEnqueuedDoesNotCrash() throws Exception { |
| 1074 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
| 1075 | for (int i = 0; i < 10; i++) { |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1076 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 1077 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 1078 | } |
| 1079 | mBinderService.cancelAllNotifications(PKG, sbn.getUserId()); |
| 1080 | waitForIdle(); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1081 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1082 | assertEquals(0, mService.getNotificationRecordCount()); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | @Test |
| 1086 | public void testCancelGroupSummaryMultipleEnqueuedChildrenDoesNotCrash() throws Exception { |
| 1087 | final NotificationRecord parent = generateNotificationRecord( |
| 1088 | mTestNotificationChannel, 1, "group1", true); |
| 1089 | final NotificationRecord parentAsChild = generateNotificationRecord( |
| 1090 | mTestNotificationChannel, 1, "group1", false); |
| 1091 | final NotificationRecord child = generateNotificationRecord( |
| 1092 | mTestNotificationChannel, 2, "group1", false); |
| 1093 | |
| 1094 | // fully post parent notification |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1095 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 1096 | parent.sbn.getId(), parent.sbn.getNotification(), parent.sbn.getUserId()); |
| 1097 | waitForIdle(); |
| 1098 | |
| 1099 | // enqueue the child several times |
| 1100 | for (int i = 0; i < 10; i++) { |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1101 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 1102 | child.sbn.getId(), child.sbn.getNotification(), child.sbn.getUserId()); |
| 1103 | } |
| 1104 | // make the parent a child, which will cancel the child notification |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1105 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 1106 | parentAsChild.sbn.getId(), parentAsChild.sbn.getNotification(), |
| 1107 | parentAsChild.sbn.getUserId()); |
| 1108 | waitForIdle(); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1109 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1110 | assertEquals(0, mService.getNotificationRecordCount()); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1114 | public void testCancelAllNotifications_IgnoreForegroundService() throws Exception { |
| 1115 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 1116 | sbn.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1117 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1118 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1119 | mBinderService.cancelAllNotifications(PKG, sbn.getUserId()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1120 | waitForIdle(); |
| 1121 | StatusBarNotification[] notifs = |
| 1122 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 1123 | assertEquals(1, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1124 | assertEquals(1, mService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1125 | } |
| 1126 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 1127 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1128 | public void testCancelAllNotifications_IgnoreOtherPackages() throws Exception { |
| 1129 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 1130 | sbn.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1131 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1132 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1133 | mBinderService.cancelAllNotifications("other_pkg_name", sbn.getUserId()); |
| 1134 | waitForIdle(); |
| 1135 | StatusBarNotification[] notifs = |
| 1136 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 1137 | assertEquals(1, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1138 | assertEquals(1, mService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1139 | } |
| 1140 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 1141 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1142 | public void testCancelAllNotifications_NullPkgRemovesAll() throws Exception { |
| 1143 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1144 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1145 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1146 | mBinderService.cancelAllNotifications(null, sbn.getUserId()); |
| 1147 | waitForIdle(); |
| 1148 | StatusBarNotification[] notifs = |
| 1149 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 1150 | assertEquals(0, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1151 | assertEquals(0, mService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1152 | } |
| 1153 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 1154 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1155 | public void testCancelAllNotifications_NullPkgIgnoresUserAllNotifications() throws Exception { |
| 1156 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1157 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1158 | sbn.getId(), sbn.getNotification(), UserHandle.USER_ALL); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1159 | // Null pkg is how we signal a user switch. |
| 1160 | mBinderService.cancelAllNotifications(null, sbn.getUserId()); |
| 1161 | waitForIdle(); |
| 1162 | StatusBarNotification[] notifs = |
| 1163 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 1164 | assertEquals(1, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1165 | assertEquals(1, mService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1166 | } |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1167 | |
| 1168 | @Test |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 1169 | public void testAppInitiatedCancelAllNotifications_CancelsNoClearFlag() throws Exception { |
| 1170 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
| 1171 | sbn.getNotification().flags |= Notification.FLAG_NO_CLEAR; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1172 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 1173 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 1174 | mBinderService.cancelAllNotifications(PKG, sbn.getUserId()); |
| 1175 | waitForIdle(); |
| 1176 | StatusBarNotification[] notifs = |
| 1177 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 1178 | assertEquals(0, notifs.length); |
| 1179 | } |
| 1180 | |
| 1181 | @Test |
| 1182 | public void testCancelAllNotifications_CancelsNoClearFlag() throws Exception { |
| 1183 | final NotificationRecord notif = generateNotificationRecord( |
| 1184 | mTestNotificationChannel, 1, "group", true); |
| 1185 | notif.getNotification().flags |= Notification.FLAG_NO_CLEAR; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1186 | mService.addNotification(notif); |
| 1187 | mService.cancelAllNotificationsInt(mUid, 0, PKG, null, 0, 0, true, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 1188 | notif.getUserId(), 0, null); |
| 1189 | waitForIdle(); |
| 1190 | StatusBarNotification[] notifs = |
| 1191 | mBinderService.getActiveNotifications(notif.sbn.getPackageName()); |
| 1192 | assertEquals(0, notifs.length); |
| 1193 | } |
| 1194 | |
| 1195 | @Test |
| 1196 | public void testUserInitiatedCancelAllOnClearAll_NoClearFlag() throws Exception { |
| 1197 | final NotificationRecord notif = generateNotificationRecord( |
| 1198 | mTestNotificationChannel, 1, "group", true); |
| 1199 | notif.getNotification().flags |= Notification.FLAG_NO_CLEAR; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1200 | mService.addNotification(notif); |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 1201 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1202 | mService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(), |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 1203 | notif.getUserId()); |
| 1204 | waitForIdle(); |
| 1205 | StatusBarNotification[] notifs = |
| 1206 | mBinderService.getActiveNotifications(notif.sbn.getPackageName()); |
| 1207 | assertEquals(1, notifs.length); |
| 1208 | } |
| 1209 | |
| 1210 | @Test |
| 1211 | public void testCancelAllCancelNotificationsFromListener_NoClearFlag() throws Exception { |
| 1212 | final NotificationRecord parent = generateNotificationRecord( |
| 1213 | mTestNotificationChannel, 1, "group", true); |
| 1214 | final NotificationRecord child = generateNotificationRecord( |
| 1215 | mTestNotificationChannel, 2, "group", false); |
| 1216 | final NotificationRecord child2 = generateNotificationRecord( |
| 1217 | mTestNotificationChannel, 3, "group", false); |
| 1218 | child2.getNotification().flags |= Notification.FLAG_NO_CLEAR; |
| 1219 | final NotificationRecord newGroup = generateNotificationRecord( |
| 1220 | mTestNotificationChannel, 4, "group2", false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1221 | mService.addNotification(parent); |
| 1222 | mService.addNotification(child); |
| 1223 | mService.addNotification(child2); |
| 1224 | mService.addNotification(newGroup); |
| 1225 | mService.getBinderService().cancelNotificationsFromListener(null, null); |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 1226 | waitForIdle(); |
| 1227 | StatusBarNotification[] notifs = |
| 1228 | mBinderService.getActiveNotifications(parent.sbn.getPackageName()); |
| 1229 | assertEquals(1, notifs.length); |
| 1230 | } |
| 1231 | |
| 1232 | @Test |
| 1233 | public void testUserInitiatedCancelAllWithGroup_NoClearFlag() throws Exception { |
| 1234 | final NotificationRecord parent = generateNotificationRecord( |
| 1235 | mTestNotificationChannel, 1, "group", true); |
| 1236 | final NotificationRecord child = generateNotificationRecord( |
| 1237 | mTestNotificationChannel, 2, "group", false); |
| 1238 | final NotificationRecord child2 = generateNotificationRecord( |
| 1239 | mTestNotificationChannel, 3, "group", false); |
| 1240 | child2.getNotification().flags |= Notification.FLAG_NO_CLEAR; |
| 1241 | final NotificationRecord newGroup = generateNotificationRecord( |
| 1242 | mTestNotificationChannel, 4, "group2", false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1243 | mService.addNotification(parent); |
| 1244 | mService.addNotification(child); |
| 1245 | mService.addNotification(child2); |
| 1246 | mService.addNotification(newGroup); |
| 1247 | mService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(), |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 1248 | parent.getUserId()); |
| 1249 | waitForIdle(); |
| 1250 | StatusBarNotification[] notifs = |
| 1251 | mBinderService.getActiveNotifications(parent.sbn.getPackageName()); |
| 1252 | assertEquals(1, notifs.length); |
| 1253 | } |
| 1254 | |
| 1255 | @Test |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 1256 | public void testRemoveForegroundServiceFlag_ImmediatelyAfterEnqueue() throws Exception { |
| 1257 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 1258 | sbn.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1259 | mBinderService.enqueueNotificationWithTag(PKG, PKG, null, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1260 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 1261 | mInternalService.removeForegroundServiceFlagFromNotification(PKG, sbn.getId(), |
| 1262 | sbn.getUserId()); |
| 1263 | waitForIdle(); |
| 1264 | StatusBarNotification[] notifs = |
| 1265 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 1266 | assertEquals(0, notifs[0].getNotification().flags & FLAG_FOREGROUND_SERVICE); |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 1267 | } |
| 1268 | |
| 1269 | @Test |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 1270 | public void testCancelAfterSecondEnqueueDoesNotSpecifyForegroundFlag() throws Exception { |
| 1271 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
| 1272 | sbn.getNotification().flags = |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 1273 | Notification.FLAG_ONGOING_EVENT | FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1274 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 1275 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 1276 | sbn.getNotification().flags = Notification.FLAG_ONGOING_EVENT; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1277 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 1278 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 1279 | mBinderService.cancelNotificationWithTag(PKG, "tag", sbn.getId(), sbn.getUserId()); |
| 1280 | waitForIdle(); |
| 1281 | assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1282 | assertEquals(0, mService.getNotificationRecordCount()); |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | @Test |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1286 | public void testCancelAllCancelNotificationsFromListener_ForegroundServiceFlag() |
| 1287 | throws Exception { |
| 1288 | final NotificationRecord parent = generateNotificationRecord( |
| 1289 | mTestNotificationChannel, 1, "group", true); |
| 1290 | final NotificationRecord child = generateNotificationRecord( |
| 1291 | mTestNotificationChannel, 2, "group", false); |
| 1292 | final NotificationRecord child2 = generateNotificationRecord( |
| 1293 | mTestNotificationChannel, 3, "group", false); |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 1294 | child2.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1295 | final NotificationRecord newGroup = generateNotificationRecord( |
| 1296 | mTestNotificationChannel, 4, "group2", false); |
| 1297 | mService.addNotification(parent); |
| 1298 | mService.addNotification(child); |
| 1299 | mService.addNotification(child2); |
| 1300 | mService.addNotification(newGroup); |
| 1301 | mService.getBinderService().cancelNotificationsFromListener(null, null); |
| 1302 | waitForIdle(); |
| 1303 | StatusBarNotification[] notifs = |
| 1304 | mBinderService.getActiveNotifications(parent.sbn.getPackageName()); |
| 1305 | assertEquals(0, notifs.length); |
| 1306 | } |
| 1307 | |
| 1308 | @Test |
| 1309 | public void testCancelAllCancelNotificationsFromListener_ForegroundServiceFlagWithParameter() |
| 1310 | throws Exception { |
| 1311 | final NotificationRecord parent = generateNotificationRecord( |
| 1312 | mTestNotificationChannel, 1, "group", true); |
| 1313 | final NotificationRecord child = generateNotificationRecord( |
| 1314 | mTestNotificationChannel, 2, "group", false); |
| 1315 | final NotificationRecord child2 = generateNotificationRecord( |
| 1316 | mTestNotificationChannel, 3, "group", false); |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 1317 | child2.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1318 | final NotificationRecord newGroup = generateNotificationRecord( |
| 1319 | mTestNotificationChannel, 4, "group2", false); |
| 1320 | mService.addNotification(parent); |
| 1321 | mService.addNotification(child); |
| 1322 | mService.addNotification(child2); |
| 1323 | mService.addNotification(newGroup); |
| 1324 | String[] keys = {parent.sbn.getKey(), child.sbn.getKey(), |
| 1325 | child2.sbn.getKey(), newGroup.sbn.getKey()}; |
| 1326 | mService.getBinderService().cancelNotificationsFromListener(null, keys); |
| 1327 | waitForIdle(); |
| 1328 | StatusBarNotification[] notifs = |
| 1329 | mBinderService.getActiveNotifications(parent.sbn.getPackageName()); |
| 1330 | assertEquals(1, notifs.length); |
| 1331 | } |
| 1332 | |
| 1333 | @Test |
| 1334 | public void testUserInitiatedCancelAllWithGroup_ForegroundServiceFlag() throws Exception { |
| 1335 | final NotificationRecord parent = generateNotificationRecord( |
| 1336 | mTestNotificationChannel, 1, "group", true); |
| 1337 | final NotificationRecord child = generateNotificationRecord( |
| 1338 | mTestNotificationChannel, 2, "group", false); |
| 1339 | final NotificationRecord child2 = generateNotificationRecord( |
| 1340 | mTestNotificationChannel, 3, "group", false); |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 1341 | child2.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1342 | final NotificationRecord newGroup = generateNotificationRecord( |
| 1343 | mTestNotificationChannel, 4, "group2", false); |
| 1344 | mService.addNotification(parent); |
| 1345 | mService.addNotification(child); |
| 1346 | mService.addNotification(child2); |
| 1347 | mService.addNotification(newGroup); |
| 1348 | mService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(), |
| 1349 | parent.getUserId()); |
| 1350 | waitForIdle(); |
| 1351 | StatusBarNotification[] notifs = |
| 1352 | mBinderService.getActiveNotifications(parent.sbn.getPackageName()); |
| 1353 | assertEquals(0, notifs.length); |
| 1354 | } |
| 1355 | |
| 1356 | @Test |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1357 | public void testFindGroupNotificationsLocked() throws Exception { |
| 1358 | // make sure the same notification can be found in both lists and returned |
| 1359 | final NotificationRecord group1 = generateNotificationRecord( |
| 1360 | mTestNotificationChannel, 1, "group1", true); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1361 | mService.addEnqueuedNotification(group1); |
| 1362 | mService.addNotification(group1); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1363 | |
| 1364 | // should not be returned |
| 1365 | final NotificationRecord group2 = generateNotificationRecord( |
| 1366 | mTestNotificationChannel, 2, "group2", true); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1367 | mBinderService.enqueueNotificationWithTag(PKG, PKG, null, |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1368 | group2.sbn.getId(), group2.sbn.getNotification(), group2.sbn.getUserId()); |
| 1369 | waitForIdle(); |
| 1370 | |
| 1371 | // should not be returned |
| 1372 | final NotificationRecord nonGroup = generateNotificationRecord( |
| 1373 | mTestNotificationChannel, 3, null, false); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1374 | mBinderService.enqueueNotificationWithTag(PKG, PKG, null, |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1375 | nonGroup.sbn.getId(), nonGroup.sbn.getNotification(), nonGroup.sbn.getUserId()); |
| 1376 | waitForIdle(); |
| 1377 | |
| 1378 | // same group, child, should be returned |
| 1379 | final NotificationRecord group1Child = generateNotificationRecord( |
| 1380 | mTestNotificationChannel, 4, "group1", false); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1381 | mBinderService.enqueueNotificationWithTag(PKG, PKG, null, group1Child.sbn.getId(), |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1382 | group1Child.sbn.getNotification(), group1Child.sbn.getUserId()); |
| 1383 | waitForIdle(); |
| 1384 | |
| 1385 | List<NotificationRecord> inGroup1 = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1386 | mService.findGroupNotificationsLocked(PKG, group1.getGroupKey(), |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1387 | group1.sbn.getUserId()); |
| 1388 | assertEquals(3, inGroup1.size()); |
| 1389 | for (NotificationRecord record : inGroup1) { |
| 1390 | assertTrue(record.getGroupKey().equals(group1.getGroupKey())); |
| 1391 | assertTrue(record.sbn.getId() == 1 || record.sbn.getId() == 4); |
| 1392 | } |
| 1393 | } |
| 1394 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1395 | @Test |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1396 | public void testCancelAllNotifications_CancelsNoClearFlagOnGoing() throws Exception { |
| 1397 | final NotificationRecord notif = generateNotificationRecord( |
| 1398 | mTestNotificationChannel, 1, "group", true); |
| 1399 | notif.getNotification().flags |= Notification.FLAG_NO_CLEAR; |
| 1400 | mService.addNotification(notif); |
| 1401 | mService.cancelAllNotificationsInt(mUid, 0, PKG, null, 0, |
| 1402 | Notification.FLAG_ONGOING_EVENT, true, notif.getUserId(), 0, null); |
| 1403 | waitForIdle(); |
| 1404 | StatusBarNotification[] notifs = |
| 1405 | mBinderService.getActiveNotifications(notif.sbn.getPackageName()); |
| 1406 | assertEquals(0, notifs.length); |
| 1407 | } |
| 1408 | |
| 1409 | @Test |
| 1410 | public void testCancelAllCancelNotificationsFromListener_NoClearFlagWithParameter() |
| 1411 | throws Exception { |
| 1412 | final NotificationRecord parent = generateNotificationRecord( |
| 1413 | mTestNotificationChannel, 1, "group", true); |
| 1414 | final NotificationRecord child = generateNotificationRecord( |
| 1415 | mTestNotificationChannel, 2, "group", false); |
| 1416 | final NotificationRecord child2 = generateNotificationRecord( |
| 1417 | mTestNotificationChannel, 3, "group", false); |
| 1418 | child2.getNotification().flags |= Notification.FLAG_NO_CLEAR; |
| 1419 | final NotificationRecord newGroup = generateNotificationRecord( |
| 1420 | mTestNotificationChannel, 4, "group2", false); |
| 1421 | mService.addNotification(parent); |
| 1422 | mService.addNotification(child); |
| 1423 | mService.addNotification(child2); |
| 1424 | mService.addNotification(newGroup); |
| 1425 | String[] keys = {parent.sbn.getKey(), child.sbn.getKey(), |
| 1426 | child2.sbn.getKey(), newGroup.sbn.getKey()}; |
| 1427 | mService.getBinderService().cancelNotificationsFromListener(null, keys); |
| 1428 | waitForIdle(); |
| 1429 | StatusBarNotification[] notifs = |
| 1430 | mBinderService.getActiveNotifications(parent.sbn.getPackageName()); |
| 1431 | assertEquals(0, notifs.length); |
| 1432 | } |
| 1433 | |
| 1434 | @Test |
| 1435 | public void testAppInitiatedCancelAllNotifications_CancelsOnGoingFlag() throws Exception { |
| 1436 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
| 1437 | sbn.getNotification().flags |= Notification.FLAG_ONGOING_EVENT; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1438 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 1439 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 1440 | mBinderService.cancelAllNotifications(PKG, sbn.getUserId()); |
| 1441 | waitForIdle(); |
| 1442 | StatusBarNotification[] notifs = |
| 1443 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 1444 | assertEquals(0, notifs.length); |
| 1445 | } |
| 1446 | |
| 1447 | @Test |
| 1448 | public void testCancelAllNotifications_CancelsOnGoingFlag() throws Exception { |
| 1449 | final NotificationRecord notif = generateNotificationRecord( |
| 1450 | mTestNotificationChannel, 1, "group", true); |
| 1451 | notif.getNotification().flags |= Notification.FLAG_ONGOING_EVENT; |
| 1452 | mService.addNotification(notif); |
| 1453 | mService.cancelAllNotificationsInt(mUid, 0, PKG, null, 0, 0, true, |
| 1454 | notif.getUserId(), 0, null); |
| 1455 | waitForIdle(); |
| 1456 | StatusBarNotification[] notifs = |
| 1457 | mBinderService.getActiveNotifications(notif.sbn.getPackageName()); |
| 1458 | assertEquals(0, notifs.length); |
| 1459 | } |
| 1460 | |
| 1461 | @Test |
| 1462 | public void testUserInitiatedCancelAllOnClearAll_OnGoingFlag() throws Exception { |
| 1463 | final NotificationRecord notif = generateNotificationRecord( |
| 1464 | mTestNotificationChannel, 1, "group", true); |
| 1465 | notif.getNotification().flags |= Notification.FLAG_ONGOING_EVENT; |
| 1466 | mService.addNotification(notif); |
| 1467 | |
| 1468 | mService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(), |
| 1469 | notif.getUserId()); |
| 1470 | waitForIdle(); |
| 1471 | StatusBarNotification[] notifs = |
| 1472 | mBinderService.getActiveNotifications(notif.sbn.getPackageName()); |
| 1473 | assertEquals(1, notifs.length); |
| 1474 | } |
| 1475 | |
| 1476 | @Test |
| 1477 | public void testCancelAllCancelNotificationsFromListener_OnGoingFlag() throws Exception { |
| 1478 | final NotificationRecord parent = generateNotificationRecord( |
| 1479 | mTestNotificationChannel, 1, "group", true); |
| 1480 | final NotificationRecord child = generateNotificationRecord( |
| 1481 | mTestNotificationChannel, 2, "group", false); |
| 1482 | final NotificationRecord child2 = generateNotificationRecord( |
| 1483 | mTestNotificationChannel, 3, "group", false); |
| 1484 | child2.getNotification().flags |= Notification.FLAG_ONGOING_EVENT; |
| 1485 | final NotificationRecord newGroup = generateNotificationRecord( |
| 1486 | mTestNotificationChannel, 4, "group2", false); |
| 1487 | mService.addNotification(parent); |
| 1488 | mService.addNotification(child); |
| 1489 | mService.addNotification(child2); |
| 1490 | mService.addNotification(newGroup); |
| 1491 | mService.getBinderService().cancelNotificationsFromListener(null, null); |
| 1492 | waitForIdle(); |
| 1493 | StatusBarNotification[] notifs = |
| 1494 | mBinderService.getActiveNotifications(parent.sbn.getPackageName()); |
| 1495 | assertEquals(1, notifs.length); |
| 1496 | } |
| 1497 | |
| 1498 | @Test |
| 1499 | public void testCancelAllCancelNotificationsFromListener_OnGoingFlagWithParameter() |
| 1500 | throws Exception { |
| 1501 | final NotificationRecord parent = generateNotificationRecord( |
| 1502 | mTestNotificationChannel, 1, "group", true); |
| 1503 | final NotificationRecord child = generateNotificationRecord( |
| 1504 | mTestNotificationChannel, 2, "group", false); |
| 1505 | final NotificationRecord child2 = generateNotificationRecord( |
| 1506 | mTestNotificationChannel, 3, "group", false); |
| 1507 | child2.getNotification().flags |= Notification.FLAG_ONGOING_EVENT; |
| 1508 | final NotificationRecord newGroup = generateNotificationRecord( |
| 1509 | mTestNotificationChannel, 4, "group2", false); |
| 1510 | mService.addNotification(parent); |
| 1511 | mService.addNotification(child); |
| 1512 | mService.addNotification(child2); |
| 1513 | mService.addNotification(newGroup); |
| 1514 | String[] keys = {parent.sbn.getKey(), child.sbn.getKey(), |
| 1515 | child2.sbn.getKey(), newGroup.sbn.getKey()}; |
| 1516 | mService.getBinderService().cancelNotificationsFromListener(null, keys); |
| 1517 | waitForIdle(); |
| 1518 | StatusBarNotification[] notifs = |
| 1519 | mBinderService.getActiveNotifications(parent.sbn.getPackageName()); |
| 1520 | assertEquals(0, notifs.length); |
| 1521 | } |
| 1522 | |
| 1523 | @Test |
| 1524 | public void testUserInitiatedCancelAllWithGroup_OnGoingFlag() throws Exception { |
| 1525 | final NotificationRecord parent = generateNotificationRecord( |
| 1526 | mTestNotificationChannel, 1, "group", true); |
| 1527 | final NotificationRecord child = generateNotificationRecord( |
| 1528 | mTestNotificationChannel, 2, "group", false); |
| 1529 | final NotificationRecord child2 = generateNotificationRecord( |
| 1530 | mTestNotificationChannel, 3, "group", false); |
| 1531 | child2.getNotification().flags |= Notification.FLAG_ONGOING_EVENT; |
| 1532 | final NotificationRecord newGroup = generateNotificationRecord( |
| 1533 | mTestNotificationChannel, 4, "group2", false); |
| 1534 | mService.addNotification(parent); |
| 1535 | mService.addNotification(child); |
| 1536 | mService.addNotification(child2); |
| 1537 | mService.addNotification(newGroup); |
| 1538 | mService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(), |
| 1539 | parent.getUserId()); |
| 1540 | waitForIdle(); |
| 1541 | StatusBarNotification[] notifs = |
| 1542 | mBinderService.getActiveNotifications(parent.sbn.getPackageName()); |
| 1543 | assertEquals(1, notifs.length); |
| 1544 | } |
| 1545 | |
| 1546 | @Test |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1547 | public void testTvExtenderChannelOverride_onTv() throws Exception { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1548 | mService.setIsTelevision(true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1549 | mService.setPreferencesHelper(mPreferencesHelper); |
| 1550 | when(mPreferencesHelper.getNotificationChannel( |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1551 | anyString(), anyInt(), eq("foo"), anyBoolean())).thenReturn( |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 1552 | new NotificationChannel("foo", "foo", IMPORTANCE_HIGH)); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1553 | |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 1554 | Notification.TvExtender tv = new Notification.TvExtender().setChannelId("foo"); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1555 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", 0, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1556 | generateNotificationRecord(null, tv).getNotification(), 0); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1557 | verify(mPreferencesHelper, times(1)).getNotificationChannel( |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1558 | anyString(), anyInt(), eq("foo"), anyBoolean()); |
| 1559 | } |
| 1560 | |
| 1561 | @Test |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1562 | public void testTvExtenderChannelOverride_notOnTv() throws Exception { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1563 | mService.setIsTelevision(false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1564 | mService.setPreferencesHelper(mPreferencesHelper); |
| 1565 | when(mPreferencesHelper.getNotificationChannel( |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1566 | anyString(), anyInt(), anyString(), anyBoolean())).thenReturn( |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1567 | mTestNotificationChannel); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1568 | |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 1569 | Notification.TvExtender tv = new Notification.TvExtender().setChannelId("foo"); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1570 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", 0, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1571 | generateNotificationRecord(null, tv).getNotification(), 0); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1572 | verify(mPreferencesHelper, times(1)).getNotificationChannel( |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1573 | anyString(), anyInt(), eq(mTestNotificationChannel.getId()), anyBoolean()); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1574 | } |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1575 | |
| 1576 | @Test |
Julia Reynolds | fc9767b | 2018-01-22 17:45:16 -0500 | [diff] [blame] | 1577 | public void testUpdateAppNotifyCreatorBlock() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1578 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | fc9767b | 2018-01-22 17:45:16 -0500 | [diff] [blame] | 1579 | |
Julia Reynolds | 4f5020d | 2019-06-03 12:48:49 -0400 | [diff] [blame] | 1580 | mBinderService.setNotificationsEnabledForPackage(PKG, 0, true); |
Julia Reynolds | fc9767b | 2018-01-22 17:45:16 -0500 | [diff] [blame] | 1581 | ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class); |
| 1582 | verify(mContext, times(1)).sendBroadcastAsUser(captor.capture(), any(), eq(null)); |
| 1583 | |
| 1584 | assertEquals(NotificationManager.ACTION_APP_BLOCK_STATE_CHANGED, |
| 1585 | captor.getValue().getAction()); |
| 1586 | assertEquals(PKG, captor.getValue().getPackage()); |
Julia Reynolds | 4f5020d | 2019-06-03 12:48:49 -0400 | [diff] [blame] | 1587 | assertFalse(captor.getValue().getBooleanExtra(EXTRA_BLOCKED_STATE, true)); |
| 1588 | } |
| 1589 | |
| 1590 | @Test |
| 1591 | public void testUpdateAppNotifyCreatorBlock_notIfMatchesExistingSetting() throws Exception { |
| 1592 | mService.setPreferencesHelper(mPreferencesHelper); |
| 1593 | |
| 1594 | mBinderService.setNotificationsEnabledForPackage(PKG, 0, false); |
| 1595 | verify(mContext, never()).sendBroadcastAsUser(any(), any(), eq(null)); |
Julia Reynolds | fc9767b | 2018-01-22 17:45:16 -0500 | [diff] [blame] | 1596 | } |
| 1597 | |
| 1598 | @Test |
| 1599 | public void testUpdateAppNotifyCreatorUnblock() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1600 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | fc9767b | 2018-01-22 17:45:16 -0500 | [diff] [blame] | 1601 | |
| 1602 | mBinderService.setNotificationsEnabledForPackage(PKG, 0, true); |
| 1603 | ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class); |
| 1604 | verify(mContext, times(1)).sendBroadcastAsUser(captor.capture(), any(), eq(null)); |
| 1605 | |
| 1606 | assertEquals(NotificationManager.ACTION_APP_BLOCK_STATE_CHANGED, |
| 1607 | captor.getValue().getAction()); |
| 1608 | assertEquals(PKG, captor.getValue().getPackage()); |
| 1609 | assertFalse(captor.getValue().getBooleanExtra(EXTRA_BLOCKED_STATE, true)); |
| 1610 | } |
| 1611 | |
| 1612 | @Test |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1613 | public void testUpdateChannelNotifyCreatorBlock() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1614 | mService.setPreferencesHelper(mPreferencesHelper); |
| 1615 | when(mPreferencesHelper.getNotificationChannel(eq(PKG), anyInt(), |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1616 | eq(mTestNotificationChannel.getId()), anyBoolean())) |
| 1617 | .thenReturn(mTestNotificationChannel); |
| 1618 | |
| 1619 | NotificationChannel updatedChannel = |
| 1620 | new NotificationChannel(mTestNotificationChannel.getId(), |
| 1621 | mTestNotificationChannel.getName(), IMPORTANCE_NONE); |
| 1622 | |
| 1623 | mBinderService.updateNotificationChannelForPackage(PKG, 0, updatedChannel); |
| 1624 | ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class); |
| 1625 | verify(mContext, times(1)).sendBroadcastAsUser(captor.capture(), any(), eq(null)); |
| 1626 | |
| 1627 | assertEquals(NotificationManager.ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED, |
| 1628 | captor.getValue().getAction()); |
| 1629 | assertEquals(PKG, captor.getValue().getPackage()); |
| 1630 | assertEquals(mTestNotificationChannel.getId(), captor.getValue().getStringExtra( |
Julia Reynolds | 44ff7c9 | 2018-02-05 10:02:30 -0500 | [diff] [blame] | 1631 | NotificationManager.EXTRA_NOTIFICATION_CHANNEL_ID)); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1632 | assertTrue(captor.getValue().getBooleanExtra(EXTRA_BLOCKED_STATE, false)); |
| 1633 | } |
| 1634 | |
| 1635 | @Test |
| 1636 | public void testUpdateChannelNotifyCreatorUnblock() throws Exception { |
| 1637 | NotificationChannel existingChannel = |
| 1638 | new NotificationChannel(mTestNotificationChannel.getId(), |
| 1639 | mTestNotificationChannel.getName(), IMPORTANCE_NONE); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1640 | mService.setPreferencesHelper(mPreferencesHelper); |
| 1641 | when(mPreferencesHelper.getNotificationChannel(eq(PKG), anyInt(), |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1642 | eq(mTestNotificationChannel.getId()), anyBoolean())) |
| 1643 | .thenReturn(existingChannel); |
| 1644 | |
| 1645 | mBinderService.updateNotificationChannelForPackage(PKG, 0, mTestNotificationChannel); |
| 1646 | ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class); |
| 1647 | verify(mContext, times(1)).sendBroadcastAsUser(captor.capture(), any(), eq(null)); |
| 1648 | |
| 1649 | assertEquals(NotificationManager.ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED, |
| 1650 | captor.getValue().getAction()); |
| 1651 | assertEquals(PKG, captor.getValue().getPackage()); |
| 1652 | assertEquals(mTestNotificationChannel.getId(), captor.getValue().getStringExtra( |
Julia Reynolds | 44ff7c9 | 2018-02-05 10:02:30 -0500 | [diff] [blame] | 1653 | NotificationManager.EXTRA_NOTIFICATION_CHANNEL_ID)); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1654 | assertFalse(captor.getValue().getBooleanExtra(EXTRA_BLOCKED_STATE, false)); |
| 1655 | } |
| 1656 | |
| 1657 | @Test |
| 1658 | public void testUpdateChannelNoNotifyCreatorOtherChanges() throws Exception { |
| 1659 | NotificationChannel existingChannel = |
| 1660 | new NotificationChannel(mTestNotificationChannel.getId(), |
| 1661 | mTestNotificationChannel.getName(), IMPORTANCE_MAX); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1662 | mService.setPreferencesHelper(mPreferencesHelper); |
| 1663 | when(mPreferencesHelper.getNotificationChannel(eq(PKG), anyInt(), |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1664 | eq(mTestNotificationChannel.getId()), anyBoolean())) |
| 1665 | .thenReturn(existingChannel); |
| 1666 | |
| 1667 | mBinderService.updateNotificationChannelForPackage(PKG, 0, mTestNotificationChannel); |
| 1668 | verify(mContext, never()).sendBroadcastAsUser(any(), any(), eq(null)); |
| 1669 | } |
| 1670 | |
| 1671 | @Test |
| 1672 | public void testUpdateGroupNotifyCreatorBlock() throws Exception { |
| 1673 | NotificationChannelGroup existing = new NotificationChannelGroup("id", "name"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1674 | mService.setPreferencesHelper(mPreferencesHelper); |
| 1675 | when(mPreferencesHelper.getNotificationChannelGroup(eq(existing.getId()), eq(PKG), anyInt())) |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1676 | .thenReturn(existing); |
| 1677 | |
| 1678 | NotificationChannelGroup updated = new NotificationChannelGroup("id", "name"); |
| 1679 | updated.setBlocked(true); |
| 1680 | |
| 1681 | mBinderService.updateNotificationChannelGroupForPackage(PKG, 0, updated); |
| 1682 | ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class); |
| 1683 | verify(mContext, times(1)).sendBroadcastAsUser(captor.capture(), any(), eq(null)); |
| 1684 | |
| 1685 | assertEquals(NotificationManager.ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED, |
| 1686 | captor.getValue().getAction()); |
| 1687 | assertEquals(PKG, captor.getValue().getPackage()); |
| 1688 | assertEquals(existing.getId(), captor.getValue().getStringExtra( |
Julia Reynolds | 44ff7c9 | 2018-02-05 10:02:30 -0500 | [diff] [blame] | 1689 | NotificationManager.EXTRA_NOTIFICATION_CHANNEL_GROUP_ID)); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1690 | assertTrue(captor.getValue().getBooleanExtra(EXTRA_BLOCKED_STATE, false)); |
| 1691 | } |
| 1692 | |
| 1693 | @Test |
| 1694 | public void testUpdateGroupNotifyCreatorUnblock() throws Exception { |
| 1695 | NotificationChannelGroup existing = new NotificationChannelGroup("id", "name"); |
| 1696 | existing.setBlocked(true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1697 | mService.setPreferencesHelper(mPreferencesHelper); |
| 1698 | when(mPreferencesHelper.getNotificationChannelGroup(eq(existing.getId()), eq(PKG), anyInt())) |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1699 | .thenReturn(existing); |
| 1700 | |
| 1701 | mBinderService.updateNotificationChannelGroupForPackage( |
| 1702 | PKG, 0, new NotificationChannelGroup("id", "name")); |
| 1703 | ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class); |
| 1704 | verify(mContext, times(1)).sendBroadcastAsUser(captor.capture(), any(), eq(null)); |
| 1705 | |
| 1706 | assertEquals(NotificationManager.ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED, |
| 1707 | captor.getValue().getAction()); |
| 1708 | assertEquals(PKG, captor.getValue().getPackage()); |
| 1709 | assertEquals(existing.getId(), captor.getValue().getStringExtra( |
Julia Reynolds | 44ff7c9 | 2018-02-05 10:02:30 -0500 | [diff] [blame] | 1710 | NotificationManager.EXTRA_NOTIFICATION_CHANNEL_GROUP_ID)); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1711 | assertFalse(captor.getValue().getBooleanExtra(EXTRA_BLOCKED_STATE, false)); |
| 1712 | } |
| 1713 | |
| 1714 | @Test |
| 1715 | public void testUpdateGroupNoNotifyCreatorOtherChanges() throws Exception { |
| 1716 | NotificationChannelGroup existing = new NotificationChannelGroup("id", "name"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1717 | mService.setPreferencesHelper(mPreferencesHelper); |
| 1718 | when(mPreferencesHelper.getNotificationChannelGroup(eq(existing.getId()), eq(PKG), anyInt())) |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1719 | .thenReturn(existing); |
| 1720 | |
| 1721 | mBinderService.updateNotificationChannelGroupForPackage( |
| 1722 | PKG, 0, new NotificationChannelGroup("id", "new name")); |
| 1723 | verify(mContext, never()).sendBroadcastAsUser(any(), any(), eq(null)); |
| 1724 | } |
| 1725 | |
| 1726 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1727 | public void testCreateChannelNotifyListener() throws Exception { |
| 1728 | List<String> associations = new ArrayList<>(); |
| 1729 | associations.add("a"); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 1730 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1731 | mService.setPreferencesHelper(mPreferencesHelper); |
| 1732 | when(mPreferencesHelper.getNotificationChannel(eq(PKG), anyInt(), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1733 | eq(mTestNotificationChannel.getId()), anyBoolean())) |
| 1734 | .thenReturn(mTestNotificationChannel); |
| 1735 | NotificationChannel channel2 = new NotificationChannel("a", "b", IMPORTANCE_LOW); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1736 | when(mPreferencesHelper.getNotificationChannel(eq(PKG), anyInt(), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1737 | eq(channel2.getId()), anyBoolean())) |
| 1738 | .thenReturn(channel2); |
Julia Reynolds | dafd3a4 | 2019-05-24 13:33:28 -0400 | [diff] [blame] | 1739 | when(mPreferencesHelper.createNotificationChannel(eq(PKG), anyInt(), |
| 1740 | eq(channel2), anyBoolean(), anyBoolean())) |
| 1741 | .thenReturn(true); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1742 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1743 | reset(mListeners); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1744 | mBinderService.createNotificationChannels(PKG, |
| 1745 | new ParceledListSlice(Arrays.asList(mTestNotificationChannel, channel2))); |
Julia Reynolds | dafd3a4 | 2019-05-24 13:33:28 -0400 | [diff] [blame] | 1746 | verify(mListeners, never()).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1747 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1748 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED)); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1749 | verify(mListeners, times(1)).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1750 | eq(Process.myUserHandle()), eq(channel2), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1751 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED)); |
| 1752 | } |
| 1753 | |
| 1754 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1755 | public void testCreateChannelGroupNotifyListener() throws Exception { |
| 1756 | List<String> associations = new ArrayList<>(); |
| 1757 | associations.add("a"); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 1758 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1759 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1760 | NotificationChannelGroup group1 = new NotificationChannelGroup("a", "b"); |
| 1761 | NotificationChannelGroup group2 = new NotificationChannelGroup("n", "m"); |
| 1762 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1763 | reset(mListeners); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1764 | mBinderService.createNotificationChannelGroups(PKG, |
| 1765 | new ParceledListSlice(Arrays.asList(group1, group2))); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1766 | verify(mListeners, times(1)).notifyNotificationChannelGroupChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1767 | eq(Process.myUserHandle()), eq(group1), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1768 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED)); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1769 | verify(mListeners, times(1)).notifyNotificationChannelGroupChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1770 | eq(Process.myUserHandle()), eq(group2), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1771 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED)); |
| 1772 | } |
| 1773 | |
| 1774 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1775 | public void testUpdateChannelNotifyListener() throws Exception { |
| 1776 | List<String> associations = new ArrayList<>(); |
| 1777 | associations.add("a"); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 1778 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1779 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1780 | mTestNotificationChannel.setLightColor(Color.CYAN); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1781 | when(mPreferencesHelper.getNotificationChannel(eq(PKG), anyInt(), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1782 | eq(mTestNotificationChannel.getId()), anyBoolean())) |
| 1783 | .thenReturn(mTestNotificationChannel); |
| 1784 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1785 | reset(mListeners); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1786 | mBinderService.updateNotificationChannelForPackage(PKG, 0, mTestNotificationChannel); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1787 | verify(mListeners, times(1)).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1788 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1789 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED)); |
| 1790 | } |
| 1791 | |
| 1792 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1793 | public void testDeleteChannelNotifyListener() throws Exception { |
| 1794 | List<String> associations = new ArrayList<>(); |
| 1795 | associations.add("a"); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 1796 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1797 | mService.setPreferencesHelper(mPreferencesHelper); |
| 1798 | when(mPreferencesHelper.getNotificationChannel(eq(PKG), anyInt(), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1799 | eq(mTestNotificationChannel.getId()), anyBoolean())) |
| 1800 | .thenReturn(mTestNotificationChannel); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1801 | reset(mListeners); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1802 | mBinderService.deleteNotificationChannel(PKG, mTestNotificationChannel.getId()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1803 | verify(mListeners, times(1)).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1804 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1805 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_DELETED)); |
| 1806 | } |
| 1807 | |
| 1808 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1809 | public void testDeleteChannelGroupNotifyListener() throws Exception { |
| 1810 | List<String> associations = new ArrayList<>(); |
| 1811 | associations.add("a"); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 1812 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1813 | NotificationChannelGroup ncg = new NotificationChannelGroup("a", "b/c"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1814 | mService.setPreferencesHelper(mPreferencesHelper); |
| 1815 | when(mPreferencesHelper.getNotificationChannelGroup(eq(ncg.getId()), eq(PKG), anyInt())) |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1816 | .thenReturn(ncg); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1817 | reset(mListeners); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1818 | mBinderService.deleteNotificationChannelGroup(PKG, ncg.getId()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1819 | verify(mListeners, times(1)).notifyNotificationChannelGroupChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1820 | eq(Process.myUserHandle()), eq(ncg), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1821 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_DELETED)); |
| 1822 | } |
| 1823 | |
| 1824 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1825 | public void testUpdateNotificationChannelFromPrivilegedListener_success() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1826 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1827 | List<String> associations = new ArrayList<>(); |
| 1828 | associations.add("a"); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 1829 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1830 | when(mPreferencesHelper.getNotificationChannel(eq(PKG), anyInt(), |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1831 | eq(mTestNotificationChannel.getId()), anyBoolean())) |
| 1832 | .thenReturn(mTestNotificationChannel); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1833 | |
| 1834 | mBinderService.updateNotificationChannelFromPrivilegedListener( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1835 | null, PKG, Process.myUserHandle(), mTestNotificationChannel); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1836 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1837 | verify(mPreferencesHelper, times(1)).updateNotificationChannel( |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 1838 | anyString(), anyInt(), any(), anyBoolean()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1839 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1840 | verify(mListeners, never()).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1841 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1842 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED)); |
| 1843 | } |
| 1844 | |
| 1845 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1846 | public void testUpdateNotificationChannelFromPrivilegedListener_noAccess() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1847 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1848 | List<String> associations = new ArrayList<>(); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 1849 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1850 | |
| 1851 | try { |
| 1852 | mBinderService.updateNotificationChannelFromPrivilegedListener( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1853 | null, PKG, Process.myUserHandle(), mTestNotificationChannel); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1854 | fail("listeners that don't have a companion device shouldn't be able to call this"); |
| 1855 | } catch (SecurityException e) { |
| 1856 | // pass |
| 1857 | } |
| 1858 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1859 | verify(mPreferencesHelper, never()).updateNotificationChannel( |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 1860 | anyString(), anyInt(), any(), anyBoolean()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1861 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1862 | verify(mListeners, never()).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1863 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
| 1864 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED)); |
| 1865 | } |
| 1866 | |
| 1867 | @Test |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1868 | public void testUpdateNotificationChannelFromPrivilegedListener_badUser() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1869 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1870 | List<String> associations = new ArrayList<>(); |
| 1871 | associations.add("a"); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 1872 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1873 | mListener = mock(ManagedServices.ManagedServiceInfo.class); |
Julia Reynolds | 4da7970 | 2017-06-01 11:06:10 -0400 | [diff] [blame] | 1874 | mListener.component = new ComponentName(PKG, PKG); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1875 | when(mListener.enabledAndUserMatches(anyInt())).thenReturn(false); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1876 | when(mListeners.checkServiceTokenLocked(any())).thenReturn(mListener); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1877 | |
| 1878 | try { |
| 1879 | mBinderService.updateNotificationChannelFromPrivilegedListener( |
| 1880 | null, PKG, UserHandle.ALL, mTestNotificationChannel); |
| 1881 | fail("incorrectly allowed a change to a user listener cannot see"); |
| 1882 | } catch (SecurityException e) { |
| 1883 | // pass |
| 1884 | } |
| 1885 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1886 | verify(mPreferencesHelper, never()).updateNotificationChannel( |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 1887 | anyString(), anyInt(), any(), anyBoolean()); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1888 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1889 | verify(mListeners, never()).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1890 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1891 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED)); |
| 1892 | } |
| 1893 | |
| 1894 | @Test |
Julia Reynolds | 48a6ed9 | 2018-10-22 12:52:03 -0400 | [diff] [blame] | 1895 | public void testGetNotificationChannelFromPrivilegedListener_cdm_success() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1896 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1897 | List<String> associations = new ArrayList<>(); |
| 1898 | associations.add("a"); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 1899 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1900 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1901 | mBinderService.getNotificationChannelsFromPrivilegedListener( |
| 1902 | null, PKG, Process.myUserHandle()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1903 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1904 | verify(mPreferencesHelper, times(1)).getNotificationChannels( |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1905 | anyString(), anyInt(), anyBoolean()); |
| 1906 | } |
| 1907 | |
| 1908 | @Test |
Julia Reynolds | 48a6ed9 | 2018-10-22 12:52:03 -0400 | [diff] [blame] | 1909 | public void testGetNotificationChannelFromPrivilegedListener_cdm_noAccess() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1910 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1911 | List<String> associations = new ArrayList<>(); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 1912 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1913 | |
| 1914 | try { |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1915 | mBinderService.getNotificationChannelsFromPrivilegedListener( |
| 1916 | null, PKG, Process.myUserHandle()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1917 | fail("listeners that don't have a companion device shouldn't be able to call this"); |
| 1918 | } catch (SecurityException e) { |
| 1919 | // pass |
| 1920 | } |
| 1921 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1922 | verify(mPreferencesHelper, never()).getNotificationChannels( |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1923 | anyString(), anyInt(), anyBoolean()); |
| 1924 | } |
| 1925 | |
| 1926 | @Test |
Julia Reynolds | 48a6ed9 | 2018-10-22 12:52:03 -0400 | [diff] [blame] | 1927 | public void testGetNotificationChannelFromPrivilegedListener_assistant_success() |
| 1928 | throws Exception { |
| 1929 | mService.setPreferencesHelper(mPreferencesHelper); |
| 1930 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(new ArrayList<>()); |
| 1931 | when(mAssistants.isServiceTokenValidLocked(any())).thenReturn(true); |
| 1932 | |
| 1933 | mBinderService.getNotificationChannelsFromPrivilegedListener( |
| 1934 | null, PKG, Process.myUserHandle()); |
| 1935 | |
| 1936 | verify(mPreferencesHelper, times(1)).getNotificationChannels( |
| 1937 | anyString(), anyInt(), anyBoolean()); |
| 1938 | } |
| 1939 | |
| 1940 | @Test |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 1941 | public void testGetNotificationChannelFromPrivilegedListener_assistant_noAccess() |
| 1942 | throws Exception { |
Julia Reynolds | 48a6ed9 | 2018-10-22 12:52:03 -0400 | [diff] [blame] | 1943 | mService.setPreferencesHelper(mPreferencesHelper); |
| 1944 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(new ArrayList<>()); |
| 1945 | when(mAssistants.isServiceTokenValidLocked(any())).thenReturn(false); |
| 1946 | |
| 1947 | try { |
| 1948 | mBinderService.getNotificationChannelsFromPrivilegedListener( |
| 1949 | null, PKG, Process.myUserHandle()); |
| 1950 | fail("listeners that don't have a companion device shouldn't be able to call this"); |
| 1951 | } catch (SecurityException e) { |
| 1952 | // pass |
| 1953 | } |
| 1954 | |
| 1955 | verify(mPreferencesHelper, never()).getNotificationChannels( |
| 1956 | anyString(), anyInt(), anyBoolean()); |
| 1957 | } |
| 1958 | |
| 1959 | @Test |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1960 | public void testGetNotificationChannelFromPrivilegedListener_badUser() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1961 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1962 | List<String> associations = new ArrayList<>(); |
| 1963 | associations.add("a"); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 1964 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1965 | mListener = mock(ManagedServices.ManagedServiceInfo.class); |
| 1966 | when(mListener.enabledAndUserMatches(anyInt())).thenReturn(false); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1967 | when(mListeners.checkServiceTokenLocked(any())).thenReturn(mListener); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1968 | |
| 1969 | try { |
| 1970 | mBinderService.getNotificationChannelsFromPrivilegedListener( |
| 1971 | null, PKG, Process.myUserHandle()); |
| 1972 | fail("listener getting channels from a user they cannot see"); |
| 1973 | } catch (SecurityException e) { |
| 1974 | // pass |
| 1975 | } |
| 1976 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1977 | verify(mPreferencesHelper, never()).getNotificationChannels( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1978 | anyString(), anyInt(), anyBoolean()); |
| 1979 | } |
| 1980 | |
| 1981 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1982 | public void testGetNotificationChannelGroupsFromPrivilegedListener_success() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1983 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1984 | List<String> associations = new ArrayList<>(); |
| 1985 | associations.add("a"); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 1986 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1987 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1988 | mBinderService.getNotificationChannelGroupsFromPrivilegedListener( |
| 1989 | null, PKG, Process.myUserHandle()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1990 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1991 | verify(mPreferencesHelper, times(1)).getNotificationChannelGroups(anyString(), anyInt()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1992 | } |
| 1993 | |
| 1994 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1995 | public void testGetNotificationChannelGroupsFromPrivilegedListener_noAccess() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1996 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1997 | List<String> associations = new ArrayList<>(); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 1998 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1999 | |
| 2000 | try { |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2001 | mBinderService.getNotificationChannelGroupsFromPrivilegedListener( |
| 2002 | null, PKG, Process.myUserHandle()); |
| 2003 | fail("listeners that don't have a companion device shouldn't be able to call this"); |
| 2004 | } catch (SecurityException e) { |
| 2005 | // pass |
| 2006 | } |
| 2007 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2008 | verify(mPreferencesHelper, never()).getNotificationChannelGroups(anyString(), anyInt()); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2009 | } |
| 2010 | |
| 2011 | @Test |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2012 | public void testGetNotificationChannelGroupsFromPrivilegedListener_badUser() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2013 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2014 | List<String> associations = new ArrayList<>(); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 2015 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2016 | mListener = mock(ManagedServices.ManagedServiceInfo.class); |
| 2017 | when(mListener.enabledAndUserMatches(anyInt())).thenReturn(false); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2018 | when(mListeners.checkServiceTokenLocked(any())).thenReturn(mListener); |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 2019 | try { |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2020 | mBinderService.getNotificationChannelGroupsFromPrivilegedListener( |
| 2021 | null, PKG, Process.myUserHandle()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2022 | fail("listeners that don't have a companion device shouldn't be able to call this"); |
| 2023 | } catch (SecurityException e) { |
| 2024 | // pass |
| 2025 | } |
| 2026 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2027 | verify(mPreferencesHelper, never()).getNotificationChannelGroups(anyString(), anyInt()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2028 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 2029 | |
| 2030 | @Test |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 2031 | public void testHasCompanionDevice_failure() throws Exception { |
| 2032 | when(mCompanionMgr.getAssociations(anyString(), anyInt())).thenThrow( |
| 2033 | new IllegalArgumentException()); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2034 | mService.hasCompanionDevice(mListener); |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 2035 | } |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 2036 | |
| 2037 | @Test |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 2038 | public void testHasCompanionDevice_noService() { |
| 2039 | mService = new TestableNotificationManagerService(mContext); |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 2040 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2041 | assertFalse(mService.hasCompanionDevice(mListener)); |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 2042 | } |
| 2043 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2044 | @Test |
| 2045 | public void testSnoozeRunnable_snoozeNonGrouped() throws Exception { |
| 2046 | final NotificationRecord nonGrouped = generateNotificationRecord( |
| 2047 | mTestNotificationChannel, 1, null, false); |
| 2048 | final NotificationRecord grouped = generateNotificationRecord( |
| 2049 | mTestNotificationChannel, 2, "group", false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2050 | mService.addNotification(grouped); |
| 2051 | mService.addNotification(nonGrouped); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2052 | |
| 2053 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2054 | mService.new SnoozeNotificationRunnable( |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2055 | nonGrouped.getKey(), 100, null); |
| 2056 | snoozeNotificationRunnable.run(); |
| 2057 | |
| 2058 | // only snooze the one notification |
| 2059 | verify(mSnoozeHelper, times(1)).snooze(any(NotificationRecord.class), anyLong()); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2060 | assertTrue(nonGrouped.getStats().hasSnoozed()); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2061 | } |
| 2062 | |
| 2063 | @Test |
| 2064 | public void testSnoozeRunnable_snoozeSummary_withChildren() throws Exception { |
| 2065 | final NotificationRecord parent = generateNotificationRecord( |
| 2066 | mTestNotificationChannel, 1, "group", true); |
| 2067 | final NotificationRecord child = generateNotificationRecord( |
| 2068 | mTestNotificationChannel, 2, "group", false); |
| 2069 | final NotificationRecord child2 = generateNotificationRecord( |
| 2070 | mTestNotificationChannel, 3, "group", false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2071 | mService.addNotification(parent); |
| 2072 | mService.addNotification(child); |
| 2073 | mService.addNotification(child2); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2074 | |
| 2075 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2076 | mService.new SnoozeNotificationRunnable( |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2077 | parent.getKey(), 100, null); |
| 2078 | snoozeNotificationRunnable.run(); |
| 2079 | |
| 2080 | // snooze parent and children |
| 2081 | verify(mSnoozeHelper, times(3)).snooze(any(NotificationRecord.class), anyLong()); |
| 2082 | } |
| 2083 | |
| 2084 | @Test |
| 2085 | public void testSnoozeRunnable_snoozeGroupChild_fellowChildren() throws Exception { |
| 2086 | final NotificationRecord parent = generateNotificationRecord( |
| 2087 | mTestNotificationChannel, 1, "group", true); |
| 2088 | final NotificationRecord child = generateNotificationRecord( |
| 2089 | mTestNotificationChannel, 2, "group", false); |
| 2090 | final NotificationRecord child2 = generateNotificationRecord( |
| 2091 | mTestNotificationChannel, 3, "group", false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2092 | mService.addNotification(parent); |
| 2093 | mService.addNotification(child); |
| 2094 | mService.addNotification(child2); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2095 | |
| 2096 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2097 | mService.new SnoozeNotificationRunnable( |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2098 | child2.getKey(), 100, null); |
| 2099 | snoozeNotificationRunnable.run(); |
| 2100 | |
| 2101 | // only snooze the one child |
| 2102 | verify(mSnoozeHelper, times(1)).snooze(any(NotificationRecord.class), anyLong()); |
| 2103 | } |
| 2104 | |
| 2105 | @Test |
| 2106 | public void testSnoozeRunnable_snoozeGroupChild_onlyChildOfSummary() throws Exception { |
| 2107 | final NotificationRecord parent = generateNotificationRecord( |
| 2108 | mTestNotificationChannel, 1, "group", true); |
| 2109 | assertTrue(parent.sbn.getNotification().isGroupSummary()); |
| 2110 | final NotificationRecord child = generateNotificationRecord( |
| 2111 | mTestNotificationChannel, 2, "group", false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2112 | mService.addNotification(parent); |
| 2113 | mService.addNotification(child); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2114 | |
| 2115 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2116 | mService.new SnoozeNotificationRunnable( |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2117 | child.getKey(), 100, null); |
| 2118 | snoozeNotificationRunnable.run(); |
| 2119 | |
| 2120 | // snooze child and summary |
| 2121 | verify(mSnoozeHelper, times(2)).snooze(any(NotificationRecord.class), anyLong()); |
| 2122 | } |
| 2123 | |
| 2124 | @Test |
| 2125 | public void testSnoozeRunnable_snoozeGroupChild_noOthersInGroup() throws Exception { |
| 2126 | final NotificationRecord child = generateNotificationRecord( |
| 2127 | mTestNotificationChannel, 2, "group", false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2128 | mService.addNotification(child); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2129 | |
| 2130 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2131 | mService.new SnoozeNotificationRunnable( |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2132 | child.getKey(), 100, null); |
| 2133 | snoozeNotificationRunnable.run(); |
| 2134 | |
| 2135 | // snooze child only |
| 2136 | verify(mSnoozeHelper, times(1)).snooze(any(NotificationRecord.class), anyLong()); |
| 2137 | } |
| 2138 | |
| 2139 | @Test |
| 2140 | public void testPostGroupChild_unsnoozeParent() throws Exception { |
| 2141 | final NotificationRecord child = generateNotificationRecord( |
| 2142 | mTestNotificationChannel, 2, "group", false); |
| 2143 | |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 2144 | mBinderService.enqueueNotificationWithTag(PKG, PKG, null, |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2145 | child.sbn.getId(), child.sbn.getNotification(), child.sbn.getUserId()); |
| 2146 | waitForIdle(); |
| 2147 | |
| 2148 | verify(mSnoozeHelper, times(1)).repostGroupSummary( |
| 2149 | anyString(), anyInt(), eq(child.getGroupKey())); |
| 2150 | } |
| 2151 | |
| 2152 | @Test |
| 2153 | public void testPostNonGroup_noUnsnoozing() throws Exception { |
| 2154 | final NotificationRecord record = generateNotificationRecord( |
| 2155 | mTestNotificationChannel, 2, null, false); |
| 2156 | |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 2157 | mBinderService.enqueueNotificationWithTag(PKG, PKG, null, |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2158 | record.sbn.getId(), record.sbn.getNotification(), record.sbn.getUserId()); |
| 2159 | waitForIdle(); |
| 2160 | |
| 2161 | verify(mSnoozeHelper, never()).repostGroupSummary(anyString(), anyInt(), anyString()); |
| 2162 | } |
| 2163 | |
| 2164 | @Test |
| 2165 | public void testPostGroupSummary_noUnsnoozing() throws Exception { |
| 2166 | final NotificationRecord parent = generateNotificationRecord( |
| 2167 | mTestNotificationChannel, 2, "group", true); |
| 2168 | |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 2169 | mBinderService.enqueueNotificationWithTag(PKG, PKG, null, |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 2170 | parent.sbn.getId(), parent.sbn.getNotification(), parent.sbn.getUserId()); |
| 2171 | waitForIdle(); |
| 2172 | |
| 2173 | verify(mSnoozeHelper, never()).repostGroupSummary(anyString(), anyInt(), anyString()); |
| 2174 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2175 | |
| 2176 | @Test |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 2177 | public void testSetListenerAccessForUser() throws Exception { |
| 2178 | UserHandle user = UserHandle.of(10); |
| 2179 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2180 | mBinderService.setNotificationListenerAccessGrantedForUser(c, user.getIdentifier(), true); |
| 2181 | |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 2182 | |
| 2183 | verify(mContext, times(1)).sendBroadcastAsUser(any(), eq(user), any()); |
| 2184 | verify(mListeners, times(1)).setPackageOrComponentEnabled( |
| 2185 | c.flattenToString(), user.getIdentifier(), true, true); |
| 2186 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 2187 | c.flattenToString(), user.getIdentifier(), false, true); |
| 2188 | verify(mAssistants, never()).setPackageOrComponentEnabled( |
| 2189 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2190 | } |
| 2191 | |
| 2192 | @Test |
| 2193 | public void testSetAssistantAccessForUser() throws Exception { |
| 2194 | UserHandle user = UserHandle.of(10); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2195 | List<UserInfo> uis = new ArrayList<>(); |
| 2196 | UserInfo ui = new UserInfo(); |
| 2197 | ui.id = 10; |
| 2198 | uis.add(ui); |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 2199 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2200 | when(mUm.getEnabledProfiles(10)).thenReturn(uis); |
| 2201 | |
| 2202 | mBinderService.setNotificationAssistantAccessGrantedForUser(c, user.getIdentifier(), true); |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 2203 | |
| 2204 | verify(mContext, times(1)).sendBroadcastAsUser(any(), eq(user), any()); |
| 2205 | verify(mAssistants, times(1)).setPackageOrComponentEnabled( |
| 2206 | c.flattenToString(), user.getIdentifier(), true, true); |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 2207 | verify(mAssistants).setUserSet(10, true); |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 2208 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 2209 | c.flattenToString(), user.getIdentifier(), false, true); |
| 2210 | verify(mListeners, never()).setPackageOrComponentEnabled( |
| 2211 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2212 | } |
| 2213 | |
| 2214 | @Test |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 2215 | public void testGetAssistantAllowedForUser() throws Exception { |
| 2216 | UserHandle user = UserHandle.of(10); |
| 2217 | try { |
| 2218 | mBinderService.getAllowedNotificationAssistantForUser(user.getIdentifier()); |
| 2219 | } catch (IllegalStateException e) { |
| 2220 | if (!e.getMessage().contains("At most one NotificationAssistant")) { |
| 2221 | throw e; |
| 2222 | } |
| 2223 | } |
| 2224 | verify(mAssistants, times(1)).getAllowedComponents(user.getIdentifier()); |
| 2225 | } |
| 2226 | |
| 2227 | @Test |
| 2228 | public void testGetAssistantAllowed() throws Exception { |
| 2229 | try { |
| 2230 | mBinderService.getAllowedNotificationAssistant(); |
| 2231 | } catch (IllegalStateException e) { |
| 2232 | if (!e.getMessage().contains("At most one NotificationAssistant")) { |
| 2233 | throw e; |
| 2234 | } |
| 2235 | } |
| 2236 | verify(mAssistants, times(1)).getAllowedComponents(0); |
| 2237 | } |
| 2238 | |
| 2239 | @Test |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 2240 | public void testSetDndAccessForUser() throws Exception { |
| 2241 | UserHandle user = UserHandle.of(10); |
| 2242 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2243 | mBinderService.setNotificationPolicyAccessGrantedForUser( |
| 2244 | c.getPackageName(), user.getIdentifier(), true); |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 2245 | |
| 2246 | verify(mContext, times(1)).sendBroadcastAsUser(any(), eq(user), any()); |
| 2247 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 2248 | c.getPackageName(), user.getIdentifier(), true, true); |
| 2249 | verify(mAssistants, never()).setPackageOrComponentEnabled( |
| 2250 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2251 | verify(mListeners, never()).setPackageOrComponentEnabled( |
| 2252 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2253 | } |
| 2254 | |
| 2255 | @Test |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2256 | public void testSetListenerAccess() throws Exception { |
| 2257 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2258 | mBinderService.setNotificationListenerAccessGranted(c, true); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2259 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2260 | verify(mListeners, times(1)).setPackageOrComponentEnabled( |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2261 | c.flattenToString(), 0, true, true); |
| 2262 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 2263 | c.flattenToString(), 0, false, true); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2264 | verify(mAssistants, never()).setPackageOrComponentEnabled( |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2265 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2266 | } |
| 2267 | |
| 2268 | @Test |
| 2269 | public void testSetAssistantAccess() throws Exception { |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2270 | List<UserInfo> uis = new ArrayList<>(); |
| 2271 | UserInfo ui = new UserInfo(); |
| 2272 | ui.id = 0; |
| 2273 | uis.add(ui); |
| 2274 | when(mUm.getEnabledProfiles(ui.id)).thenReturn(uis); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2275 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2276 | |
| 2277 | mBinderService.setNotificationAssistantAccessGranted(c, true); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2278 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2279 | verify(mAssistants, times(1)).setPackageOrComponentEnabled( |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2280 | c.flattenToString(), 0, true, true); |
| 2281 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 2282 | c.flattenToString(), 0, false, true); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2283 | verify(mListeners, never()).setPackageOrComponentEnabled( |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2284 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2285 | } |
| 2286 | |
| 2287 | @Test |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2288 | public void testSetAssistantAccess_multiProfile() throws Exception { |
| 2289 | List<UserInfo> uis = new ArrayList<>(); |
| 2290 | UserInfo ui = new UserInfo(); |
| 2291 | ui.id = 0; |
| 2292 | uis.add(ui); |
| 2293 | UserInfo ui10 = new UserInfo(); |
| 2294 | ui10.id = 10; |
| 2295 | uis.add(ui10); |
| 2296 | when(mUm.getEnabledProfiles(ui.id)).thenReturn(uis); |
| 2297 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 2298 | |
| 2299 | mBinderService.setNotificationAssistantAccessGranted(c, true); |
| 2300 | |
| 2301 | verify(mAssistants, times(1)).setPackageOrComponentEnabled( |
| 2302 | c.flattenToString(), 0, true, true); |
| 2303 | verify(mAssistants, times(1)).setPackageOrComponentEnabled( |
| 2304 | c.flattenToString(), 10, true, true); |
| 2305 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 2306 | c.flattenToString(), 0, false, true); |
| 2307 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 2308 | c.flattenToString(), 10, false, true); |
| 2309 | verify(mListeners, never()).setPackageOrComponentEnabled( |
| 2310 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2311 | } |
| 2312 | |
| 2313 | @Test |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 2314 | public void testSetAssistantAccess_nullWithAllowedAssistant() throws Exception { |
| 2315 | ArrayList<ComponentName> componentList = new ArrayList<>(); |
| 2316 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 2317 | componentList.add(c); |
| 2318 | when(mAssistants.getAllowedComponents(anyInt())).thenReturn(componentList); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2319 | List<UserInfo> uis = new ArrayList<>(); |
| 2320 | UserInfo ui = new UserInfo(); |
| 2321 | ui.id = 0; |
| 2322 | uis.add(ui); |
| 2323 | when(mUm.getEnabledProfiles(ui.id)).thenReturn(uis); |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 2324 | |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2325 | mBinderService.setNotificationAssistantAccessGranted(null, true); |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 2326 | |
| 2327 | verify(mAssistants, times(1)).setPackageOrComponentEnabled( |
| 2328 | c.flattenToString(), 0, true, false); |
| 2329 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 2330 | c.flattenToString(), 0, false, false); |
| 2331 | verify(mListeners, never()).setPackageOrComponentEnabled( |
| 2332 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2333 | } |
| 2334 | |
| 2335 | @Test |
| 2336 | public void testSetAssistantAccessForUser_nullWithAllowedAssistant() throws Exception { |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2337 | List<UserInfo> uis = new ArrayList<>(); |
| 2338 | UserInfo ui = new UserInfo(); |
| 2339 | ui.id = 10; |
| 2340 | uis.add(ui); |
| 2341 | UserHandle user = ui.getUserHandle(); |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 2342 | ArrayList<ComponentName> componentList = new ArrayList<>(); |
| 2343 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 2344 | componentList.add(c); |
| 2345 | when(mAssistants.getAllowedComponents(anyInt())).thenReturn(componentList); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2346 | when(mUm.getEnabledProfiles(10)).thenReturn(uis); |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 2347 | |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2348 | mBinderService.setNotificationAssistantAccessGrantedForUser( |
| 2349 | null, user.getIdentifier(), true); |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 2350 | |
| 2351 | verify(mAssistants, times(1)).setPackageOrComponentEnabled( |
| 2352 | c.flattenToString(), user.getIdentifier(), true, false); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2353 | verify(mAssistants).setUserSet(10, true); |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 2354 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 2355 | c.flattenToString(), user.getIdentifier(), false, false); |
| 2356 | verify(mListeners, never()).setPackageOrComponentEnabled( |
| 2357 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2358 | } |
| 2359 | |
| 2360 | @Test |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2361 | public void testSetAssistantAccessForUser_workProfile_nullWithAllowedAssistant() |
| 2362 | throws Exception { |
| 2363 | List<UserInfo> uis = new ArrayList<>(); |
| 2364 | UserInfo ui = new UserInfo(); |
| 2365 | ui.id = 0; |
| 2366 | uis.add(ui); |
| 2367 | UserInfo ui10 = new UserInfo(); |
| 2368 | ui10.id = 10; |
| 2369 | uis.add(ui10); |
| 2370 | UserHandle user = ui.getUserHandle(); |
| 2371 | ArrayList<ComponentName> componentList = new ArrayList<>(); |
| 2372 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 2373 | componentList.add(c); |
| 2374 | when(mAssistants.getAllowedComponents(anyInt())).thenReturn(componentList); |
| 2375 | when(mUm.getEnabledProfiles(ui.id)).thenReturn(uis); |
| 2376 | |
| 2377 | mBinderService.setNotificationAssistantAccessGrantedForUser( |
| 2378 | null, user.getIdentifier(), true); |
| 2379 | |
| 2380 | verify(mAssistants, times(1)).setPackageOrComponentEnabled( |
| 2381 | c.flattenToString(), user.getIdentifier(), true, false); |
| 2382 | verify(mAssistants, times(1)).setPackageOrComponentEnabled( |
| 2383 | c.flattenToString(), ui10.id, true, false); |
| 2384 | verify(mAssistants).setUserSet(0, true); |
| 2385 | verify(mAssistants).setUserSet(10, true); |
| 2386 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 2387 | c.flattenToString(), user.getIdentifier(), false, false); |
| 2388 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 2389 | c.flattenToString(), ui10.id, false, false); |
| 2390 | verify(mListeners, never()).setPackageOrComponentEnabled( |
| 2391 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2392 | } |
| 2393 | |
| 2394 | @Test |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2395 | public void testSetDndAccess() throws Exception { |
| 2396 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2397 | |
| 2398 | mBinderService.setNotificationPolicyAccessGranted(c.getPackageName(), true); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2399 | |
| 2400 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 2401 | c.getPackageName(), 0, true, true); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2402 | verify(mAssistants, never()).setPackageOrComponentEnabled( |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2403 | any(), anyInt(), anyBoolean(), anyBoolean()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2404 | verify(mListeners, never()).setPackageOrComponentEnabled( |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2405 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2406 | } |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 2407 | |
| 2408 | @Test |
| 2409 | public void testSetListenerAccess_doesNothingOnLowRam() throws Exception { |
| 2410 | when(mActivityManager.isLowRamDevice()).thenReturn(true); |
| 2411 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 2412 | mBinderService.setNotificationListenerAccessGranted(c, true); |
| 2413 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2414 | verify(mListeners, never()).setPackageOrComponentEnabled( |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 2415 | anyString(), anyInt(), anyBoolean(), anyBoolean()); |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 2416 | verify(mConditionProviders, never()).setPackageOrComponentEnabled( |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 2417 | anyString(), anyInt(), anyBoolean(), anyBoolean()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2418 | verify(mAssistants, never()).setPackageOrComponentEnabled( |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 2419 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2420 | } |
| 2421 | |
| 2422 | @Test |
| 2423 | public void testSetAssistantAccess_doesNothingOnLowRam() throws Exception { |
| 2424 | when(mActivityManager.isLowRamDevice()).thenReturn(true); |
| 2425 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2426 | List<UserInfo> uis = new ArrayList<>(); |
| 2427 | UserInfo ui = new UserInfo(); |
| 2428 | ui.id = 0; |
| 2429 | uis.add(ui); |
| 2430 | when(mUm.getEnabledProfiles(ui.id)).thenReturn(uis); |
| 2431 | |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 2432 | mBinderService.setNotificationAssistantAccessGranted(c, true); |
| 2433 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2434 | verify(mListeners, never()).setPackageOrComponentEnabled( |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 2435 | anyString(), anyInt(), anyBoolean(), anyBoolean()); |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 2436 | verify(mConditionProviders, never()).setPackageOrComponentEnabled( |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 2437 | anyString(), anyInt(), anyBoolean(), anyBoolean()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2438 | verify(mAssistants, never()).setPackageOrComponentEnabled( |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 2439 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2440 | } |
| 2441 | |
| 2442 | @Test |
| 2443 | public void testSetDndAccess_doesNothingOnLowRam() throws Exception { |
| 2444 | when(mActivityManager.isLowRamDevice()).thenReturn(true); |
| 2445 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 2446 | mBinderService.setNotificationPolicyAccessGranted(c.getPackageName(), true); |
| 2447 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2448 | verify(mListeners, never()).setPackageOrComponentEnabled( |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 2449 | anyString(), anyInt(), anyBoolean(), anyBoolean()); |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 2450 | verify(mConditionProviders, never()).setPackageOrComponentEnabled( |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 2451 | anyString(), anyInt(), anyBoolean(), anyBoolean()); |
| 2452 | verify(mAssistants, never()).setPackageOrComponentEnabled( |
| 2453 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2454 | } |
| 2455 | |
| 2456 | @Test |
| 2457 | public void testSetListenerAccess_doesNothingOnLowRam_exceptWatch() throws Exception { |
| 2458 | when(mPackageManagerClient.hasSystemFeature(FEATURE_WATCH)).thenReturn(true); |
| 2459 | when(mActivityManager.isLowRamDevice()).thenReturn(true); |
| 2460 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2461 | |
| 2462 | mBinderService.setNotificationListenerAccessGranted(c, true); |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 2463 | |
| 2464 | verify(mListeners, times(1)).setPackageOrComponentEnabled( |
| 2465 | c.flattenToString(), 0, true, true); |
| 2466 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 2467 | c.flattenToString(), 0, false, true); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2468 | verify(mAssistants, never()).setPackageOrComponentEnabled( |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 2469 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2470 | } |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 2471 | |
| 2472 | @Test |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 2473 | public void testSetAssistantAccess_doesNothingOnLowRam_exceptWatch() throws Exception { |
| 2474 | when(mPackageManagerClient.hasSystemFeature(FEATURE_WATCH)).thenReturn(true); |
| 2475 | when(mActivityManager.isLowRamDevice()).thenReturn(true); |
| 2476 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2477 | List<UserInfo> uis = new ArrayList<>(); |
| 2478 | UserInfo ui = new UserInfo(); |
| 2479 | ui.id = 0; |
| 2480 | uis.add(ui); |
| 2481 | when(mUm.getEnabledProfiles(ui.id)).thenReturn(uis); |
| 2482 | |
| 2483 | mBinderService.setNotificationAssistantAccessGranted(c, true); |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 2484 | |
| 2485 | verify(mListeners, never()).setPackageOrComponentEnabled( |
| 2486 | anyString(), anyInt(), anyBoolean(), anyBoolean()); |
| 2487 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 2488 | c.flattenToString(), 0, false, true); |
| 2489 | verify(mAssistants, times(1)).setPackageOrComponentEnabled( |
| 2490 | c.flattenToString(), 0, true, true); |
| 2491 | } |
| 2492 | |
| 2493 | @Test |
| 2494 | public void testSetDndAccess_doesNothingOnLowRam_exceptWatch() throws Exception { |
| 2495 | when(mPackageManagerClient.hasSystemFeature(FEATURE_WATCH)).thenReturn(true); |
| 2496 | when(mActivityManager.isLowRamDevice()).thenReturn(true); |
| 2497 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 2498 | |
| 2499 | mBinderService.setNotificationPolicyAccessGranted(c.getPackageName(), true); |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 2500 | |
| 2501 | verify(mListeners, never()).setPackageOrComponentEnabled( |
| 2502 | anyString(), anyInt(), anyBoolean(), anyBoolean()); |
| 2503 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 2504 | c.getPackageName(), 0, true, true); |
| 2505 | verify(mAssistants, never()).setPackageOrComponentEnabled( |
| 2506 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 2507 | } |
| 2508 | |
| 2509 | @Test |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 2510 | public void testOnlyAutogroupIfGroupChanged_noPriorNoti_autogroups() throws Exception { |
| 2511 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel, 0, null, false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2512 | mService.addEnqueuedNotification(r); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 2513 | NotificationManagerService.PostNotificationRunnable runnable = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2514 | mService.new PostNotificationRunnable(r.getKey()); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 2515 | runnable.run(); |
| 2516 | waitForIdle(); |
| 2517 | |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 2518 | verify(mGroupHelper, times(1)).onNotificationPosted(any(), anyBoolean()); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 2519 | } |
| 2520 | |
| 2521 | @Test |
| 2522 | public void testOnlyAutogroupIfGroupChanged_groupChanged_autogroups() |
| 2523 | throws Exception { |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 2524 | NotificationRecord r = |
| 2525 | generateNotificationRecord(mTestNotificationChannel, 0, "group", false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2526 | mService.addNotification(r); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 2527 | |
| 2528 | r = generateNotificationRecord(mTestNotificationChannel, 0, null, false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2529 | mService.addEnqueuedNotification(r); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 2530 | NotificationManagerService.PostNotificationRunnable runnable = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2531 | mService.new PostNotificationRunnable(r.getKey()); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 2532 | runnable.run(); |
| 2533 | waitForIdle(); |
| 2534 | |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 2535 | verify(mGroupHelper, times(1)).onNotificationPosted(any(), anyBoolean()); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 2536 | } |
| 2537 | |
| 2538 | @Test |
| 2539 | public void testOnlyAutogroupIfGroupChanged_noGroupChanged_autogroups() |
| 2540 | throws Exception { |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 2541 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel, 0, "group", |
| 2542 | false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2543 | mService.addNotification(r); |
| 2544 | mService.addEnqueuedNotification(r); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 2545 | |
| 2546 | NotificationManagerService.PostNotificationRunnable runnable = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2547 | mService.new PostNotificationRunnable(r.getKey()); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 2548 | runnable.run(); |
| 2549 | waitForIdle(); |
| 2550 | |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 2551 | verify(mGroupHelper, never()).onNotificationPosted(any(), anyBoolean()); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 2552 | } |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 2553 | |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 2554 | @Test |
Brad Stenning | d2e7a97 | 2018-10-01 09:08:42 -0700 | [diff] [blame] | 2555 | public void testDontAutogroupIfCritical() throws Exception { |
| 2556 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel, 0, null, false); |
| 2557 | r.setCriticality(CriticalNotificationExtractor.CRITICAL_LOW); |
| 2558 | mService.addEnqueuedNotification(r); |
| 2559 | NotificationManagerService.PostNotificationRunnable runnable = |
| 2560 | mService.new PostNotificationRunnable(r.getKey()); |
| 2561 | runnable.run(); |
| 2562 | |
| 2563 | r = generateNotificationRecord(mTestNotificationChannel, 1, null, false); |
| 2564 | r.setCriticality(CriticalNotificationExtractor.CRITICAL); |
| 2565 | runnable = mService.new PostNotificationRunnable(r.getKey()); |
| 2566 | mService.addEnqueuedNotification(r); |
| 2567 | |
| 2568 | runnable.run(); |
| 2569 | waitForIdle(); |
| 2570 | |
| 2571 | verify(mGroupHelper, never()).onNotificationPosted(any(), anyBoolean()); |
| 2572 | } |
| 2573 | |
| 2574 | @Test |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 2575 | public void testNoFakeColorizedPermission() throws Exception { |
| 2576 | when(mPackageManagerClient.checkPermission(any(), any())).thenReturn(PERMISSION_DENIED); |
| 2577 | Notification.Builder nb = new Notification.Builder(mContext, |
| 2578 | mTestNotificationChannel.getId()) |
| 2579 | .setContentTitle("foo") |
| 2580 | .setColorized(true) |
| 2581 | .setFlag(Notification.FLAG_CAN_COLORIZE, true) |
| 2582 | .setSmallIcon(android.R.drawable.sym_def_app_icon); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 2583 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, "tag", mUid, 0, |
| 2584 | nb.build(), new UserHandle(mUid), null, 0); |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 2585 | NotificationRecord nr = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 2586 | |
| 2587 | mBinderService.enqueueNotificationWithTag(PKG, PKG, null, |
| 2588 | nr.sbn.getId(), nr.sbn.getNotification(), nr.sbn.getUserId()); |
| 2589 | waitForIdle(); |
| 2590 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2591 | NotificationRecord posted = mService.findNotificationLocked( |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 2592 | PKG, null, nr.sbn.getId(), nr.sbn.getUserId()); |
| 2593 | |
| 2594 | assertFalse(posted.getNotification().isColorized()); |
| 2595 | } |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 2596 | |
| 2597 | @Test |
| 2598 | public void testGetNotificationCountLocked() throws Exception { |
| 2599 | for (int i = 0; i < 20; i++) { |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 2600 | NotificationRecord r = |
| 2601 | generateNotificationRecord(mTestNotificationChannel, i, null, false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2602 | mService.addEnqueuedNotification(r); |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 2603 | } |
| 2604 | for (int i = 0; i < 20; i++) { |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 2605 | NotificationRecord r = |
| 2606 | generateNotificationRecord(mTestNotificationChannel, i, null, false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2607 | mService.addNotification(r); |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 2608 | } |
| 2609 | |
| 2610 | // another package |
| 2611 | Notification n = |
| 2612 | new Notification.Builder(mContext, mTestNotificationChannel.getId()) |
| 2613 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 2614 | .build(); |
| 2615 | |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 2616 | StatusBarNotification sbn = new StatusBarNotification("a", "a", 0, "tag", mUid, 0, |
| 2617 | n, new UserHandle(mUid), null, 0); |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 2618 | NotificationRecord otherPackage = |
| 2619 | new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2620 | mService.addEnqueuedNotification(otherPackage); |
| 2621 | mService.addNotification(otherPackage); |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 2622 | |
| 2623 | // Same notifications are enqueued as posted, everything counts b/c id and tag don't match |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 2624 | int userId = new UserHandle(mUid).getIdentifier(); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 2625 | assertEquals(40, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2626 | mService.getNotificationCountLocked(PKG, userId, 0, null)); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 2627 | assertEquals(40, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2628 | mService.getNotificationCountLocked(PKG, userId, 0, "tag2")); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 2629 | assertEquals(2, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2630 | mService.getNotificationCountLocked("a", userId, 0, "banana")); |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 2631 | |
| 2632 | // exclude a known notification - it's excluded from only the posted list, not enqueued |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 2633 | assertEquals(39, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2634 | mService.getNotificationCountLocked(PKG, userId, 0, "tag")); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2635 | } |
| 2636 | |
| 2637 | @Test |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 2638 | public void testAddAutogroup_requestsSort() throws Exception { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2639 | RankingHandler rh = mock(RankingHandler.class); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2640 | mService.setRankingHandler(rh); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2641 | |
| 2642 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2643 | mService.addNotification(r); |
| 2644 | mService.addAutogroupKeyLocked(r.getKey()); |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 2645 | |
| 2646 | verify(rh, times(1)).requestSort(); |
| 2647 | } |
| 2648 | |
| 2649 | @Test |
| 2650 | public void testRemoveAutogroup_requestsSort() throws Exception { |
| 2651 | RankingHandler rh = mock(RankingHandler.class); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2652 | mService.setRankingHandler(rh); |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 2653 | |
| 2654 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 2655 | r.setOverrideGroupKey("TEST"); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2656 | mService.addNotification(r); |
| 2657 | mService.removeAutogroupKeyLocked(r.getKey()); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2658 | |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 2659 | verify(rh, times(1)).requestSort(); |
| 2660 | } |
| 2661 | |
| 2662 | @Test |
| 2663 | public void testReaddAutogroup_noSort() throws Exception { |
| 2664 | RankingHandler rh = mock(RankingHandler.class); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2665 | mService.setRankingHandler(rh); |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 2666 | |
| 2667 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 2668 | r.setOverrideGroupKey("TEST"); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2669 | mService.addNotification(r); |
| 2670 | mService.addAutogroupKeyLocked(r.getKey()); |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 2671 | |
| 2672 | verify(rh, never()).requestSort(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2673 | } |
| 2674 | |
| 2675 | @Test |
| 2676 | public void testHandleRankingSort_sendsUpdateOnSignalExtractorChange() throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2677 | mService.setPreferencesHelper(mPreferencesHelper); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2678 | NotificationManagerService.WorkerHandler handler = mock( |
| 2679 | NotificationManagerService.WorkerHandler.class); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2680 | mService.setHandler(handler); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2681 | |
| 2682 | Map<String, Answer> answers = getSignalExtractorSideEffects(); |
| 2683 | for (String message : answers.keySet()) { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2684 | mService.clearNotifications(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2685 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2686 | mService.addNotification(r); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2687 | |
| 2688 | doAnswer(answers.get(message)).when(mRankingHelper).extractSignals(r); |
| 2689 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2690 | mService.handleRankingSort(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2691 | } |
| 2692 | verify(handler, times(answers.size())).scheduleSendRankingUpdate(); |
| 2693 | } |
| 2694 | |
| 2695 | @Test |
| 2696 | public void testHandleRankingSort_noUpdateWhenNoSignalChange() throws Exception { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2697 | mService.setRankingHelper(mRankingHelper); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2698 | NotificationManagerService.WorkerHandler handler = mock( |
| 2699 | NotificationManagerService.WorkerHandler.class); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2700 | mService.setHandler(handler); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2701 | |
| 2702 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2703 | mService.addNotification(r); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2704 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2705 | mService.handleRankingSort(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2706 | verify(handler, never()).scheduleSendRankingUpdate(); |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 2707 | } |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2708 | |
| 2709 | @Test |
| 2710 | public void testReadPolicyXml_readApprovedServicesFromXml() throws Exception { |
Julia Reynolds | d6d5a59 | 2018-04-02 11:03:32 -0400 | [diff] [blame] | 2711 | final String upgradeXml = "<notification-policy version=\"1\">" |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2712 | + "<ranking></ranking>" |
| 2713 | + "<enabled_listeners>" |
| 2714 | + "<service_listing approved=\"test\" user=\"0\" primary=\"true\" />" |
| 2715 | + "</enabled_listeners>" |
| 2716 | + "<enabled_assistants>" |
| 2717 | + "<service_listing approved=\"test\" user=\"0\" primary=\"true\" />" |
| 2718 | + "</enabled_assistants>" |
| 2719 | + "<dnd_apps>" |
| 2720 | + "<service_listing approved=\"test\" user=\"0\" primary=\"true\" />" |
| 2721 | + "</dnd_apps>" |
| 2722 | + "</notification-policy>"; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2723 | mService.readPolicyXml( |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2724 | new BufferedInputStream(new ByteArrayInputStream(upgradeXml.getBytes())), |
| 2725 | false, |
| 2726 | UserHandle.USER_ALL); |
| 2727 | verify(mListeners, times(1)).readXml(any(), any(), anyBoolean(), anyInt()); |
| 2728 | verify(mConditionProviders, times(1)).readXml(any(), any(), anyBoolean(), anyInt()); |
| 2729 | verify(mAssistants, times(1)).readXml(any(), any(), anyBoolean(), anyInt()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2730 | |
| 2731 | // numbers are inflated for setup |
| 2732 | verify(mListeners, times(1)).migrateToXml(); |
| 2733 | verify(mConditionProviders, times(1)).migrateToXml(); |
| 2734 | verify(mAssistants, times(1)).migrateToXml(); |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 2735 | verify(mAssistants, times(2)).resetDefaultAssistantsIfNecessary(); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2736 | } |
| 2737 | |
| 2738 | @Test |
| 2739 | public void testReadPolicyXml_readApprovedServicesFromSettings() throws Exception { |
| 2740 | final String preupgradeXml = "<notification-policy version=\"1\">" |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2741 | + "<ranking></ranking>" |
| 2742 | + "</notification-policy>"; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2743 | mService.readPolicyXml( |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2744 | new BufferedInputStream(new ByteArrayInputStream(preupgradeXml.getBytes())), |
| 2745 | false, |
| 2746 | UserHandle.USER_ALL); |
| 2747 | verify(mListeners, never()).readXml(any(), any(), anyBoolean(), anyInt()); |
| 2748 | verify(mConditionProviders, never()).readXml(any(), any(), anyBoolean(), anyInt()); |
| 2749 | verify(mAssistants, never()).readXml(any(), any(), anyBoolean(), anyInt()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2750 | |
| 2751 | // numbers are inflated for setup |
| 2752 | verify(mListeners, times(2)).migrateToXml(); |
| 2753 | verify(mConditionProviders, times(2)).migrateToXml(); |
| 2754 | verify(mAssistants, times(2)).migrateToXml(); |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 2755 | verify(mAssistants, times(2)).resetDefaultAssistantsIfNecessary(); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2756 | } |
| 2757 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2758 | @Test |
| 2759 | public void testReadPolicyXml_doesNotRestoreManagedServicesForManagedUser() throws Exception { |
| 2760 | final String policyXml = "<notification-policy version=\"1\">" |
| 2761 | + "<ranking></ranking>" |
| 2762 | + "<enabled_listeners>" |
| 2763 | + "<service_listing approved=\"test\" user=\"10\" primary=\"true\" />" |
| 2764 | + "</enabled_listeners>" |
| 2765 | + "<enabled_assistants>" |
| 2766 | + "<service_listing approved=\"test\" user=\"10\" primary=\"true\" />" |
| 2767 | + "</enabled_assistants>" |
| 2768 | + "<dnd_apps>" |
| 2769 | + "<service_listing approved=\"test\" user=\"10\" primary=\"true\" />" |
| 2770 | + "</dnd_apps>" |
| 2771 | + "</notification-policy>"; |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 2772 | when(mUm.isManagedProfile(10)).thenReturn(true); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2773 | mService.readPolicyXml( |
| 2774 | new BufferedInputStream(new ByteArrayInputStream(policyXml.getBytes())), |
| 2775 | true, |
| 2776 | 10); |
| 2777 | verify(mListeners, never()).readXml(any(), any(), eq(true), eq(10)); |
| 2778 | verify(mConditionProviders, never()).readXml(any(), any(), eq(true), eq(10)); |
| 2779 | verify(mAssistants, never()).readXml(any(), any(), eq(true), eq(10)); |
| 2780 | } |
| 2781 | |
| 2782 | @Test |
| 2783 | public void testReadPolicyXml_restoresManagedServicesForNonManagedUser() throws Exception { |
| 2784 | final String policyXml = "<notification-policy version=\"1\">" |
| 2785 | + "<ranking></ranking>" |
| 2786 | + "<enabled_listeners>" |
| 2787 | + "<service_listing approved=\"test\" user=\"10\" primary=\"true\" />" |
| 2788 | + "</enabled_listeners>" |
| 2789 | + "<enabled_assistants>" |
| 2790 | + "<service_listing approved=\"test\" user=\"10\" primary=\"true\" />" |
| 2791 | + "</enabled_assistants>" |
| 2792 | + "<dnd_apps>" |
| 2793 | + "<service_listing approved=\"test\" user=\"10\" primary=\"true\" />" |
| 2794 | + "</dnd_apps>" |
| 2795 | + "</notification-policy>"; |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 2796 | when(mUm.isManagedProfile(10)).thenReturn(false); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2797 | mService.readPolicyXml( |
| 2798 | new BufferedInputStream(new ByteArrayInputStream(policyXml.getBytes())), |
| 2799 | true, |
| 2800 | 10); |
| 2801 | verify(mListeners, times(1)).readXml(any(), any(), eq(true), eq(10)); |
| 2802 | verify(mConditionProviders, times(1)).readXml(any(), any(), eq(true), eq(10)); |
| 2803 | verify(mAssistants, times(1)).readXml(any(), any(), eq(true), eq(10)); |
| 2804 | } |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 2805 | |
| 2806 | @Test |
| 2807 | public void testLocaleChangedCallsUpdateDefaultZenModeRules() throws Exception { |
| 2808 | ZenModeHelper mZenModeHelper = mock(ZenModeHelper.class); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2809 | mService.mZenModeHelper = mZenModeHelper; |
| 2810 | mService.mLocaleChangeReceiver.onReceive(mContext, |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 2811 | new Intent(Intent.ACTION_LOCALE_CHANGED)); |
| 2812 | |
| 2813 | verify(mZenModeHelper, times(1)).updateDefaultZenRules(); |
| 2814 | } |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 2815 | |
| 2816 | @Test |
| 2817 | public void testBumpFGImportance_noChannelChangePreOApp() throws Exception { |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 2818 | String preOPkg = PKG_N_MR1; |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 2819 | final ApplicationInfo legacy = new ApplicationInfo(); |
| 2820 | legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1; |
| 2821 | when(mPackageManagerClient.getApplicationInfoAsUser(eq(preOPkg), anyInt(), anyInt())) |
| 2822 | .thenReturn(legacy); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 2823 | when(mPackageManagerClient.getPackageUidAsUser(eq(preOPkg), anyInt())) |
| 2824 | .thenReturn(Binder.getCallingUid()); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 2825 | getContext().setMockPackageManager(mPackageManagerClient); |
| 2826 | |
| 2827 | Notification.Builder nb = new Notification.Builder(mContext, |
| 2828 | NotificationChannel.DEFAULT_CHANNEL_ID) |
| 2829 | .setContentTitle("foo") |
| 2830 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 2831 | .setFlag(FLAG_FOREGROUND_SERVICE, true) |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 2832 | .setPriority(Notification.PRIORITY_MIN); |
| 2833 | |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 2834 | StatusBarNotification sbn = new StatusBarNotification(preOPkg, preOPkg, 9, "tag", |
| 2835 | Binder.getCallingUid(), 0, nb.build(), new UserHandle(Binder.getCallingUid()), null, 0); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 2836 | |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 2837 | mBinderService.enqueueNotificationWithTag(sbn.getPackageName(), sbn.getOpPkg(), |
| 2838 | sbn.getTag(), sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 2839 | waitForIdle(); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 2840 | |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 2841 | assertEquals(IMPORTANCE_LOW, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2842 | mService.getNotificationRecord(sbn.getKey()).getImportance()); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 2843 | |
| 2844 | nb = new Notification.Builder(mContext) |
| 2845 | .setContentTitle("foo") |
| 2846 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 2847 | .setFlag(FLAG_FOREGROUND_SERVICE, true) |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 2848 | .setPriority(Notification.PRIORITY_MIN); |
| 2849 | |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 2850 | sbn = new StatusBarNotification(preOPkg, preOPkg, 9, "tag", Binder.getCallingUid(), |
| 2851 | 0, nb.build(), new UserHandle(Binder.getCallingUid()), null, 0); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 2852 | |
| 2853 | mBinderService.enqueueNotificationWithTag(preOPkg, preOPkg, "tag", |
| 2854 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 2855 | waitForIdle(); |
| 2856 | assertEquals(IMPORTANCE_LOW, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2857 | mService.getNotificationRecord(sbn.getKey()).getImportance()); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 2858 | |
| 2859 | NotificationChannel defaultChannel = mBinderService.getNotificationChannel( |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 2860 | preOPkg, mContext.getUserId(), preOPkg, NotificationChannel.DEFAULT_CHANNEL_ID); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 2861 | assertEquals(IMPORTANCE_UNSPECIFIED, defaultChannel.getImportance()); |
| 2862 | } |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2863 | |
| 2864 | @Test |
| 2865 | public void testStats_updatedOnDirectReply() throws Exception { |
| 2866 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 2867 | mService.addNotification(r); |
| 2868 | |
| 2869 | mService.mNotificationDelegate.onNotificationDirectReplied(r.getKey()); |
| 2870 | assertTrue(mService.getNotificationRecord(r.getKey()).getStats().hasDirectReplied()); |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 2871 | verify(mAssistants).notifyAssistantNotificationDirectReplyLocked(eq(r.sbn)); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2872 | } |
| 2873 | |
| 2874 | @Test |
Julia Reynolds | 84dc96b | 2017-11-14 09:51:01 -0500 | [diff] [blame] | 2875 | public void testStats_updatedOnUserExpansion() throws Exception { |
| 2876 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2877 | mService.addNotification(r); |
| 2878 | |
Gustav Sennton | a8e38aa | 2019-01-22 14:55:39 +0000 | [diff] [blame] | 2879 | mService.mNotificationDelegate.onNotificationExpansionChanged(r.getKey(), true, true, |
| 2880 | NOTIFICATION_LOCATION_UNKNOWN); |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 2881 | verify(mAssistants).notifyAssistantExpansionChangedLocked(eq(r.sbn), eq(true), eq((true))); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2882 | assertTrue(mService.getNotificationRecord(r.getKey()).getStats().hasExpanded()); |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 2883 | |
Gustav Sennton | a8e38aa | 2019-01-22 14:55:39 +0000 | [diff] [blame] | 2884 | mService.mNotificationDelegate.onNotificationExpansionChanged(r.getKey(), true, false, |
| 2885 | NOTIFICATION_LOCATION_UNKNOWN); |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 2886 | verify(mAssistants).notifyAssistantExpansionChangedLocked(eq(r.sbn), eq(true), eq((false))); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2887 | assertTrue(mService.getNotificationRecord(r.getKey()).getStats().hasExpanded()); |
| 2888 | } |
| 2889 | |
| 2890 | @Test |
Julia Reynolds | 84dc96b | 2017-11-14 09:51:01 -0500 | [diff] [blame] | 2891 | public void testStats_notUpdatedOnAutoExpansion() throws Exception { |
| 2892 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 2893 | mService.addNotification(r); |
| 2894 | |
Gustav Sennton | a8e38aa | 2019-01-22 14:55:39 +0000 | [diff] [blame] | 2895 | mService.mNotificationDelegate.onNotificationExpansionChanged(r.getKey(), false, true, |
| 2896 | NOTIFICATION_LOCATION_UNKNOWN); |
Julia Reynolds | 84dc96b | 2017-11-14 09:51:01 -0500 | [diff] [blame] | 2897 | assertFalse(mService.getNotificationRecord(r.getKey()).getStats().hasExpanded()); |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 2898 | verify(mAssistants).notifyAssistantExpansionChangedLocked(eq(r.sbn), eq(false), eq((true))); |
| 2899 | |
Gustav Sennton | a8e38aa | 2019-01-22 14:55:39 +0000 | [diff] [blame] | 2900 | mService.mNotificationDelegate.onNotificationExpansionChanged(r.getKey(), false, false, |
| 2901 | NOTIFICATION_LOCATION_UNKNOWN); |
Julia Reynolds | 84dc96b | 2017-11-14 09:51:01 -0500 | [diff] [blame] | 2902 | assertFalse(mService.getNotificationRecord(r.getKey()).getStats().hasExpanded()); |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 2903 | verify(mAssistants).notifyAssistantExpansionChangedLocked( |
| 2904 | eq(r.sbn), eq(false), eq((false))); |
Julia Reynolds | 84dc96b | 2017-11-14 09:51:01 -0500 | [diff] [blame] | 2905 | } |
| 2906 | |
| 2907 | @Test |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2908 | public void testStats_updatedOnViewSettings() throws Exception { |
| 2909 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 2910 | mService.addNotification(r); |
| 2911 | |
| 2912 | mService.mNotificationDelegate.onNotificationSettingsViewed(r.getKey()); |
| 2913 | assertTrue(mService.getNotificationRecord(r.getKey()).getStats().hasViewedSettings()); |
| 2914 | } |
| 2915 | |
| 2916 | @Test |
| 2917 | public void testStats_updatedOnVisibilityChanged() throws Exception { |
| 2918 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 2919 | mService.addNotification(r); |
| 2920 | |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 2921 | final NotificationVisibility nv = NotificationVisibility.obtain(r.getKey(), 1, 2, true); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2922 | mService.mNotificationDelegate.onNotificationVisibilityChanged( |
| 2923 | new NotificationVisibility[] {nv}, new NotificationVisibility[]{}); |
| 2924 | assertTrue(mService.getNotificationRecord(r.getKey()).getStats().hasSeen()); |
| 2925 | mService.mNotificationDelegate.onNotificationVisibilityChanged( |
| 2926 | new NotificationVisibility[] {}, new NotificationVisibility[]{nv}); |
| 2927 | assertTrue(mService.getNotificationRecord(r.getKey()).getStats().hasSeen()); |
| 2928 | } |
| 2929 | |
| 2930 | @Test |
| 2931 | public void testStats_dismissalSurface() throws Exception { |
| 2932 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 2933 | mService.addNotification(r); |
| 2934 | |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 2935 | final NotificationVisibility nv = NotificationVisibility.obtain(r.getKey(), 0, 1, true); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2936 | mService.mNotificationDelegate.onNotificationClear(mUid, 0, PKG, r.sbn.getTag(), |
Julia Reynolds | fd4099d | 2018-08-21 11:06:06 -0400 | [diff] [blame] | 2937 | r.sbn.getId(), r.getUserId(), r.getKey(), NotificationStats.DISMISSAL_AOD, |
| 2938 | NotificationStats.DISMISS_SENTIMENT_POSITIVE, nv); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2939 | waitForIdle(); |
| 2940 | |
| 2941 | assertEquals(NotificationStats.DISMISSAL_AOD, r.getStats().getDismissalSurface()); |
| 2942 | } |
| 2943 | |
| 2944 | @Test |
Julia Reynolds | fd4099d | 2018-08-21 11:06:06 -0400 | [diff] [blame] | 2945 | public void testStats_dismissalSentiment() throws Exception { |
| 2946 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 2947 | mService.addNotification(r); |
| 2948 | |
| 2949 | final NotificationVisibility nv = NotificationVisibility.obtain(r.getKey(), 0, 1, true); |
| 2950 | mService.mNotificationDelegate.onNotificationClear(mUid, 0, PKG, r.sbn.getTag(), |
| 2951 | r.sbn.getId(), r.getUserId(), r.getKey(), NotificationStats.DISMISSAL_AOD, |
| 2952 | NotificationStats.DISMISS_SENTIMENT_NEGATIVE, nv); |
| 2953 | waitForIdle(); |
| 2954 | |
| 2955 | assertEquals(NotificationStats.DISMISS_SENTIMENT_NEGATIVE, |
| 2956 | r.getStats().getDismissalSentiment()); |
| 2957 | } |
| 2958 | |
| 2959 | @Test |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 2960 | public void testApplyAdjustmentMultiUser() throws Exception { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2961 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 2962 | mService.addNotification(r); |
| 2963 | NotificationManagerService.WorkerHandler handler = mock( |
| 2964 | NotificationManagerService.WorkerHandler.class); |
| 2965 | mService.setHandler(handler); |
| 2966 | |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 2967 | when(mAssistants.isSameUser(eq(null), anyInt())).thenReturn(false); |
| 2968 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2969 | Bundle signals = new Bundle(); |
| 2970 | signals.putInt(Adjustment.KEY_USER_SENTIMENT, |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 2971 | USER_SENTIMENT_NEGATIVE); |
| 2972 | Adjustment adjustment = new Adjustment( |
| 2973 | r.sbn.getPackageName(), r.getKey(), signals, "", r.getUser().getIdentifier()); |
| 2974 | mBinderService.applyAdjustmentFromAssistant(null, adjustment); |
| 2975 | |
| 2976 | waitForIdle(); |
| 2977 | |
| 2978 | verify(handler, timeout(300).times(0)).scheduleSendRankingUpdate(); |
| 2979 | } |
| 2980 | |
| 2981 | @Test |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 2982 | public void testAssistantBlockingTriggersCancel() throws Exception { |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 2983 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 2984 | mService.addNotification(r); |
| 2985 | NotificationManagerService.WorkerHandler handler = mock( |
| 2986 | NotificationManagerService.WorkerHandler.class); |
| 2987 | mService.setHandler(handler); |
| 2988 | |
| 2989 | Bundle signals = new Bundle(); |
Julia Reynolds | c7dcdc2 | 2019-03-25 10:26:14 -0400 | [diff] [blame] | 2990 | signals.putInt(KEY_IMPORTANCE, IMPORTANCE_NONE); |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 2991 | Adjustment adjustment = new Adjustment( |
| 2992 | r.sbn.getPackageName(), r.getKey(), signals, "", r.getUser().getIdentifier()); |
| 2993 | when(mAssistants.isSameUser(any(), anyInt())).thenReturn(true); |
| 2994 | mBinderService.applyAdjustmentFromAssistant(null, adjustment); |
| 2995 | |
| 2996 | waitForIdle(); |
| 2997 | |
| 2998 | verify(handler, timeout(300).times(0)).scheduleSendRankingUpdate(); |
| 2999 | verify(handler, times(1)).scheduleCancelNotification(any()); |
| 3000 | } |
| 3001 | |
| 3002 | @Test |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3003 | public void testApplyEnqueuedAdjustmentFromAssistant_singleUser() throws Exception { |
| 3004 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3005 | mService.addEnqueuedNotification(r); |
| 3006 | NotificationManagerService.WorkerHandler handler = mock( |
| 3007 | NotificationManagerService.WorkerHandler.class); |
| 3008 | mService.setHandler(handler); |
| 3009 | when(mAssistants.isSameUser(eq(null), anyInt())).thenReturn(true); |
| 3010 | |
| 3011 | Bundle signals = new Bundle(); |
| 3012 | signals.putInt(Adjustment.KEY_USER_SENTIMENT, |
| 3013 | USER_SENTIMENT_NEGATIVE); |
| 3014 | Adjustment adjustment = new Adjustment( |
| 3015 | r.sbn.getPackageName(), r.getKey(), signals, "", r.getUser().getIdentifier()); |
| 3016 | mBinderService.applyEnqueuedAdjustmentFromAssistant(null, adjustment); |
| 3017 | |
| 3018 | assertEquals(USER_SENTIMENT_NEGATIVE, r.getUserSentiment()); |
| 3019 | } |
| 3020 | |
| 3021 | @Test |
Julia Reynolds | dc6adc6 | 2019-04-08 10:35:40 -0400 | [diff] [blame] | 3022 | public void testApplyEnqueuedAdjustmentFromAssistant_importance() throws Exception { |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 3023 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3024 | mService.addEnqueuedNotification(r); |
| 3025 | NotificationManagerService.WorkerHandler handler = mock( |
| 3026 | NotificationManagerService.WorkerHandler.class); |
| 3027 | mService.setHandler(handler); |
| 3028 | when(mAssistants.isSameUser(eq(null), anyInt())).thenReturn(true); |
| 3029 | |
| 3030 | Bundle signals = new Bundle(); |
Julia Reynolds | c7dcdc2 | 2019-03-25 10:26:14 -0400 | [diff] [blame] | 3031 | signals.putInt(KEY_IMPORTANCE, IMPORTANCE_LOW); |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 3032 | Adjustment adjustment = new Adjustment( |
| 3033 | r.sbn.getPackageName(), r.getKey(), signals, "", r.getUser().getIdentifier()); |
| 3034 | mBinderService.applyEnqueuedAdjustmentFromAssistant(null, adjustment); |
| 3035 | |
| 3036 | assertEquals(IMPORTANCE_LOW, r.getImportance()); |
| 3037 | } |
| 3038 | |
| 3039 | @Test |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3040 | public void testApplyEnqueuedAdjustmentFromAssistant_crossUser() throws Exception { |
| 3041 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3042 | mService.addEnqueuedNotification(r); |
| 3043 | NotificationManagerService.WorkerHandler handler = mock( |
| 3044 | NotificationManagerService.WorkerHandler.class); |
| 3045 | mService.setHandler(handler); |
| 3046 | when(mAssistants.isSameUser(eq(null), anyInt())).thenReturn(false); |
| 3047 | |
| 3048 | Bundle signals = new Bundle(); |
| 3049 | signals.putInt(Adjustment.KEY_USER_SENTIMENT, |
| 3050 | USER_SENTIMENT_NEGATIVE); |
| 3051 | Adjustment adjustment = new Adjustment( |
| 3052 | r.sbn.getPackageName(), r.getKey(), signals, "", r.getUser().getIdentifier()); |
| 3053 | mBinderService.applyEnqueuedAdjustmentFromAssistant(null, adjustment); |
| 3054 | |
| 3055 | assertEquals(USER_SENTIMENT_NEUTRAL, r.getUserSentiment()); |
| 3056 | |
| 3057 | waitForIdle(); |
| 3058 | |
| 3059 | verify(handler, timeout(300).times(0)).scheduleSendRankingUpdate(); |
| 3060 | } |
| 3061 | |
| 3062 | @Test |
| 3063 | public void testUserSentimentChangeTriggersUpdate() throws Exception { |
| 3064 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3065 | mService.addNotification(r); |
| 3066 | NotificationManagerService.WorkerHandler handler = mock( |
| 3067 | NotificationManagerService.WorkerHandler.class); |
| 3068 | mService.setHandler(handler); |
| 3069 | when(mAssistants.isSameUser(eq(null), anyInt())).thenReturn(true); |
| 3070 | |
| 3071 | Bundle signals = new Bundle(); |
| 3072 | signals.putInt(Adjustment.KEY_USER_SENTIMENT, |
| 3073 | USER_SENTIMENT_NEGATIVE); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3074 | Adjustment adjustment = new Adjustment( |
| 3075 | r.sbn.getPackageName(), r.getKey(), signals, "", r.getUser().getIdentifier()); |
| 3076 | mBinderService.applyAdjustmentFromAssistant(null, adjustment); |
| 3077 | |
| 3078 | waitForIdle(); |
| 3079 | |
| 3080 | verify(handler, timeout(300).times(1)).scheduleSendRankingUpdate(); |
| 3081 | } |
Julia Reynolds | 7bcb57b | 2018-01-22 10:37:58 -0500 | [diff] [blame] | 3082 | |
| 3083 | @Test |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3084 | public void testTooLateAdjustmentTriggersUpdate() throws Exception { |
| 3085 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3086 | mService.addNotification(r); |
| 3087 | NotificationManagerService.WorkerHandler handler = mock( |
| 3088 | NotificationManagerService.WorkerHandler.class); |
| 3089 | mService.setHandler(handler); |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3090 | when(mAssistants.isSameUser(eq(null), anyInt())).thenReturn(true); |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3091 | |
| 3092 | Bundle signals = new Bundle(); |
| 3093 | signals.putInt(Adjustment.KEY_USER_SENTIMENT, |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3094 | USER_SENTIMENT_NEGATIVE); |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3095 | Adjustment adjustment = new Adjustment( |
| 3096 | r.sbn.getPackageName(), r.getKey(), signals, "", r.getUser().getIdentifier()); |
| 3097 | mBinderService.applyEnqueuedAdjustmentFromAssistant(null, adjustment); |
| 3098 | |
| 3099 | waitForIdle(); |
| 3100 | |
| 3101 | verify(handler, timeout(300).times(1)).scheduleSendRankingUpdate(); |
| 3102 | } |
| 3103 | |
| 3104 | @Test |
| 3105 | public void testEnqueuedAdjustmentAppliesAdjustments() throws Exception { |
| 3106 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 3107 | mService.addEnqueuedNotification(r); |
| 3108 | NotificationManagerService.WorkerHandler handler = mock( |
| 3109 | NotificationManagerService.WorkerHandler.class); |
| 3110 | mService.setHandler(handler); |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3111 | when(mAssistants.isSameUser(eq(null), anyInt())).thenReturn(true); |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3112 | |
| 3113 | Bundle signals = new Bundle(); |
| 3114 | signals.putInt(Adjustment.KEY_USER_SENTIMENT, |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3115 | USER_SENTIMENT_NEGATIVE); |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3116 | Adjustment adjustment = new Adjustment( |
| 3117 | r.sbn.getPackageName(), r.getKey(), signals, "", r.getUser().getIdentifier()); |
| 3118 | mBinderService.applyEnqueuedAdjustmentFromAssistant(null, adjustment); |
| 3119 | |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3120 | assertEquals(USER_SENTIMENT_NEGATIVE, |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3121 | r.getUserSentiment()); |
| 3122 | } |
| 3123 | |
| 3124 | @Test |
Julia Reynolds | d78263d | 2018-01-30 10:40:41 -0500 | [diff] [blame] | 3125 | public void testRestore() throws Exception { |
| 3126 | int systemChecks = mService.countSystemChecks; |
| 3127 | mBinderService.applyRestore(null, UserHandle.USER_SYSTEM); |
| 3128 | assertEquals(1, mService.countSystemChecks - systemChecks); |
| 3129 | } |
| 3130 | |
| 3131 | @Test |
| 3132 | public void testBackup() throws Exception { |
| 3133 | int systemChecks = mService.countSystemChecks; |
| 3134 | mBinderService.getBackupPayload(1); |
| 3135 | assertEquals(1, mService.countSystemChecks - systemChecks); |
| 3136 | } |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3137 | |
| 3138 | @Test |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3139 | public void updateUriPermissions_update() throws Exception { |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3140 | NotificationChannel c = new NotificationChannel( |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 3141 | TEST_CHANNEL_ID, TEST_CHANNEL_ID, IMPORTANCE_DEFAULT); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3142 | c.setSound(null, Notification.AUDIO_ATTRIBUTES_DEFAULT); |
| 3143 | Message message1 = new Message("", 0, ""); |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3144 | message1.setData("", |
| 3145 | ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 1)); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3146 | Message message2 = new Message("", 1, ""); |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3147 | message2.setData("", |
| 3148 | ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 2)); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3149 | |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3150 | Notification.Builder nbA = new Notification.Builder(mContext, c.getId()) |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3151 | .setContentTitle("foo") |
| 3152 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 3153 | .setStyle(new Notification.MessagingStyle("") |
| 3154 | .addMessage(message1) |
| 3155 | .addMessage(message2)); |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3156 | NotificationRecord recordA = new NotificationRecord(mContext, new StatusBarNotification( |
| 3157 | 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] | 3158 | |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3159 | // First post means we grant access to both |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 3160 | reset(mUgm); |
| 3161 | reset(mUgmInternal); |
| 3162 | when(mUgmInternal.newUriPermissionOwner(any())).thenReturn(new Binder()); |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3163 | mService.updateUriPermissions(recordA, null, mContext.getPackageName(), |
| 3164 | UserHandle.USER_SYSTEM); |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 3165 | verify(mUgm, times(1)).grantUriPermissionFromOwner(any(), anyInt(), any(), |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3166 | eq(message1.getDataUri()), anyInt(), anyInt(), anyInt()); |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 3167 | verify(mUgm, times(1)).grantUriPermissionFromOwner(any(), anyInt(), any(), |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3168 | eq(message2.getDataUri()), anyInt(), anyInt(), anyInt()); |
| 3169 | |
| 3170 | Notification.Builder nbB = new Notification.Builder(mContext, c.getId()) |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3171 | .setContentTitle("foo") |
| 3172 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 3173 | .setStyle(new Notification.MessagingStyle("").addMessage(message2)); |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3174 | NotificationRecord recordB = new NotificationRecord(mContext, new StatusBarNotification(PKG, |
| 3175 | 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] | 3176 | |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3177 | // Update means we drop access to first |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 3178 | reset(mUgmInternal); |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3179 | mService.updateUriPermissions(recordB, recordA, mContext.getPackageName(), |
| 3180 | UserHandle.USER_SYSTEM); |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 3181 | verify(mUgmInternal, times(1)).revokeUriPermissionFromOwner(any(), |
| 3182 | eq(message1.getDataUri()), anyInt(), anyInt()); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3183 | |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3184 | // Update back means we grant access to first again |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 3185 | reset(mUgm); |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3186 | mService.updateUriPermissions(recordA, recordB, mContext.getPackageName(), |
| 3187 | UserHandle.USER_SYSTEM); |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 3188 | verify(mUgm, times(1)).grantUriPermissionFromOwner(any(), anyInt(), any(), |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3189 | eq(message1.getDataUri()), anyInt(), anyInt(), anyInt()); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3190 | |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3191 | // And update to empty means we drop everything |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 3192 | reset(mUgmInternal); |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3193 | mService.updateUriPermissions(null, recordB, mContext.getPackageName(), |
| 3194 | UserHandle.USER_SYSTEM); |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 3195 | verify(mUgmInternal, times(1)).revokeUriPermissionFromOwner(any(), eq(null), |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3196 | anyInt(), anyInt()); |
| 3197 | } |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 3198 | |
| 3199 | @Test |
Robin Lee | d107af6 | 2018-04-27 13:55:56 +0200 | [diff] [blame] | 3200 | public void testVisitUris() throws Exception { |
| 3201 | final Uri audioContents = Uri.parse("content://com.example/audio"); |
| 3202 | final Uri backgroundImage = Uri.parse("content://com.example/background"); |
| 3203 | |
| 3204 | Bundle extras = new Bundle(); |
| 3205 | extras.putParcelable(Notification.EXTRA_AUDIO_CONTENTS_URI, audioContents); |
| 3206 | extras.putString(Notification.EXTRA_BACKGROUND_IMAGE_URI, backgroundImage.toString()); |
| 3207 | |
| 3208 | Notification n = new Notification.Builder(mContext, "a") |
| 3209 | .setContentTitle("notification with uris") |
| 3210 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 3211 | .addExtras(extras) |
| 3212 | .build(); |
| 3213 | |
| 3214 | Consumer<Uri> visitor = (Consumer<Uri>) spy(Consumer.class); |
| 3215 | n.visitUris(visitor); |
| 3216 | verify(visitor, times(1)).accept(eq(audioContents)); |
| 3217 | verify(visitor, times(1)).accept(eq(backgroundImage)); |
| 3218 | } |
| 3219 | |
| 3220 | @Test |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 3221 | public void testSetNotificationPolicy_preP_setOldFields() { |
| 3222 | ZenModeHelper mZenModeHelper = mock(ZenModeHelper.class); |
| 3223 | mService.mZenModeHelper = mZenModeHelper; |
| 3224 | NotificationManager.Policy userPolicy = |
| 3225 | new NotificationManager.Policy(0, 0, 0, SUPPRESSED_EFFECT_BADGE); |
| 3226 | when(mZenModeHelper.getNotificationPolicy()).thenReturn(userPolicy); |
| 3227 | |
| 3228 | NotificationManager.Policy appPolicy = new NotificationManager.Policy(0, 0, 0, |
| 3229 | SUPPRESSED_EFFECT_SCREEN_ON | SUPPRESSED_EFFECT_SCREEN_OFF); |
| 3230 | |
| 3231 | int expected = SUPPRESSED_EFFECT_BADGE |
| 3232 | | SUPPRESSED_EFFECT_SCREEN_ON | SUPPRESSED_EFFECT_SCREEN_OFF |
Julia Reynolds | eac2da2 | 2018-04-12 10:48:46 -0400 | [diff] [blame] | 3233 | | SUPPRESSED_EFFECT_PEEK | SUPPRESSED_EFFECT_LIGHTS |
| 3234 | | SUPPRESSED_EFFECT_FULL_SCREEN_INTENT; |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 3235 | int actual = mService.calculateSuppressedVisualEffects(appPolicy, userPolicy, O_MR1); |
| 3236 | |
| 3237 | assertEquals(expected, actual); |
| 3238 | } |
| 3239 | |
| 3240 | @Test |
| 3241 | public void testSetNotificationPolicy_preP_setNewFields() { |
| 3242 | ZenModeHelper mZenModeHelper = mock(ZenModeHelper.class); |
| 3243 | mService.mZenModeHelper = mZenModeHelper; |
| 3244 | NotificationManager.Policy userPolicy = |
| 3245 | new NotificationManager.Policy(0, 0, 0, SUPPRESSED_EFFECT_BADGE); |
| 3246 | when(mZenModeHelper.getNotificationPolicy()).thenReturn(userPolicy); |
| 3247 | |
| 3248 | NotificationManager.Policy appPolicy = new NotificationManager.Policy(0, 0, 0, |
| 3249 | SUPPRESSED_EFFECT_NOTIFICATION_LIST); |
| 3250 | |
| 3251 | int expected = SUPPRESSED_EFFECT_BADGE; |
| 3252 | int actual = mService.calculateSuppressedVisualEffects(appPolicy, userPolicy, O_MR1); |
| 3253 | |
| 3254 | assertEquals(expected, actual); |
| 3255 | } |
| 3256 | |
| 3257 | @Test |
| 3258 | public void testSetNotificationPolicy_preP_setOldNewFields() { |
| 3259 | ZenModeHelper mZenModeHelper = mock(ZenModeHelper.class); |
| 3260 | mService.mZenModeHelper = mZenModeHelper; |
| 3261 | NotificationManager.Policy userPolicy = |
| 3262 | new NotificationManager.Policy(0, 0, 0, SUPPRESSED_EFFECT_BADGE); |
| 3263 | when(mZenModeHelper.getNotificationPolicy()).thenReturn(userPolicy); |
| 3264 | |
| 3265 | NotificationManager.Policy appPolicy = new NotificationManager.Policy(0, 0, 0, |
| 3266 | SUPPRESSED_EFFECT_SCREEN_ON | SUPPRESSED_EFFECT_STATUS_BAR); |
| 3267 | |
| 3268 | int expected = |
| 3269 | SUPPRESSED_EFFECT_BADGE | SUPPRESSED_EFFECT_SCREEN_ON | SUPPRESSED_EFFECT_PEEK; |
| 3270 | int actual = mService.calculateSuppressedVisualEffects(appPolicy, userPolicy, O_MR1); |
| 3271 | |
| 3272 | assertEquals(expected, actual); |
| 3273 | } |
| 3274 | |
| 3275 | @Test |
| 3276 | public void testSetNotificationPolicy_P_setOldFields() { |
| 3277 | ZenModeHelper mZenModeHelper = mock(ZenModeHelper.class); |
| 3278 | mService.mZenModeHelper = mZenModeHelper; |
| 3279 | NotificationManager.Policy userPolicy = |
| 3280 | new NotificationManager.Policy(0, 0, 0, SUPPRESSED_EFFECT_BADGE); |
| 3281 | when(mZenModeHelper.getNotificationPolicy()).thenReturn(userPolicy); |
| 3282 | |
| 3283 | NotificationManager.Policy appPolicy = new NotificationManager.Policy(0, 0, 0, |
| 3284 | SUPPRESSED_EFFECT_SCREEN_ON | SUPPRESSED_EFFECT_SCREEN_OFF); |
| 3285 | |
| 3286 | int expected = SUPPRESSED_EFFECT_SCREEN_ON | SUPPRESSED_EFFECT_SCREEN_OFF |
| 3287 | | SUPPRESSED_EFFECT_PEEK | SUPPRESSED_EFFECT_AMBIENT |
| 3288 | | SUPPRESSED_EFFECT_LIGHTS | SUPPRESSED_EFFECT_FULL_SCREEN_INTENT; |
| 3289 | int actual = mService.calculateSuppressedVisualEffects(appPolicy, userPolicy, P); |
| 3290 | |
| 3291 | assertEquals(expected, actual); |
| 3292 | } |
| 3293 | |
| 3294 | @Test |
| 3295 | public void testSetNotificationPolicy_P_setNewFields() { |
| 3296 | ZenModeHelper mZenModeHelper = mock(ZenModeHelper.class); |
| 3297 | mService.mZenModeHelper = mZenModeHelper; |
| 3298 | NotificationManager.Policy userPolicy = |
| 3299 | new NotificationManager.Policy(0, 0, 0, SUPPRESSED_EFFECT_BADGE); |
| 3300 | when(mZenModeHelper.getNotificationPolicy()).thenReturn(userPolicy); |
| 3301 | |
| 3302 | NotificationManager.Policy appPolicy = new NotificationManager.Policy(0, 0, 0, |
| 3303 | SUPPRESSED_EFFECT_NOTIFICATION_LIST | SUPPRESSED_EFFECT_AMBIENT |
| 3304 | | SUPPRESSED_EFFECT_LIGHTS | SUPPRESSED_EFFECT_FULL_SCREEN_INTENT); |
| 3305 | |
| 3306 | int expected = SUPPRESSED_EFFECT_NOTIFICATION_LIST | SUPPRESSED_EFFECT_SCREEN_OFF |
| 3307 | | SUPPRESSED_EFFECT_AMBIENT | SUPPRESSED_EFFECT_LIGHTS |
| 3308 | | SUPPRESSED_EFFECT_FULL_SCREEN_INTENT; |
| 3309 | int actual = mService.calculateSuppressedVisualEffects(appPolicy, userPolicy, P); |
| 3310 | |
| 3311 | assertEquals(expected, actual); |
| 3312 | } |
| 3313 | |
| 3314 | @Test |
| 3315 | public void testSetNotificationPolicy_P_setOldNewFields() { |
| 3316 | ZenModeHelper mZenModeHelper = mock(ZenModeHelper.class); |
| 3317 | mService.mZenModeHelper = mZenModeHelper; |
| 3318 | NotificationManager.Policy userPolicy = |
| 3319 | new NotificationManager.Policy(0, 0, 0, SUPPRESSED_EFFECT_BADGE); |
| 3320 | when(mZenModeHelper.getNotificationPolicy()).thenReturn(userPolicy); |
| 3321 | |
| 3322 | NotificationManager.Policy appPolicy = new NotificationManager.Policy(0, 0, 0, |
| 3323 | SUPPRESSED_EFFECT_SCREEN_ON | SUPPRESSED_EFFECT_STATUS_BAR); |
| 3324 | |
| 3325 | int expected = SUPPRESSED_EFFECT_STATUS_BAR; |
| 3326 | int actual = mService.calculateSuppressedVisualEffects(appPolicy, userPolicy, P); |
| 3327 | |
| 3328 | assertEquals(expected, actual); |
| 3329 | |
| 3330 | appPolicy = new NotificationManager.Policy(0, 0, 0, |
| 3331 | SUPPRESSED_EFFECT_SCREEN_ON | SUPPRESSED_EFFECT_AMBIENT |
| 3332 | | SUPPRESSED_EFFECT_LIGHTS | SUPPRESSED_EFFECT_FULL_SCREEN_INTENT); |
| 3333 | |
| 3334 | expected = SUPPRESSED_EFFECT_SCREEN_OFF | SUPPRESSED_EFFECT_AMBIENT |
| 3335 | | SUPPRESSED_EFFECT_LIGHTS | SUPPRESSED_EFFECT_FULL_SCREEN_INTENT; |
| 3336 | actual = mService.calculateSuppressedVisualEffects(appPolicy, userPolicy, P); |
| 3337 | |
| 3338 | assertEquals(expected, actual); |
| 3339 | } |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 3340 | |
| 3341 | @Test |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 3342 | public void testVisualDifference_foreground() { |
| 3343 | Notification.Builder nb1 = new Notification.Builder(mContext, "") |
| 3344 | .setContentTitle("foo"); |
| 3345 | StatusBarNotification sbn1 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 3346 | nb1.build(), new UserHandle(mUid), null, 0); |
| 3347 | NotificationRecord r1 = |
| 3348 | new NotificationRecord(mContext, sbn1, mock(NotificationChannel.class)); |
| 3349 | |
| 3350 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 3351 | .setFlag(FLAG_FOREGROUND_SERVICE, true) |
| 3352 | .setContentTitle("bar"); |
| 3353 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 3354 | nb2.build(), new UserHandle(mUid), null, 0); |
| 3355 | NotificationRecord r2 = |
| 3356 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 3357 | |
| 3358 | assertFalse(mService.isVisuallyInterruptive(r1, r2)); |
| 3359 | } |
| 3360 | |
| 3361 | @Test |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 3362 | public void testVisualDifference_diffTitle() { |
| 3363 | Notification.Builder nb1 = new Notification.Builder(mContext, "") |
| 3364 | .setContentTitle("foo"); |
| 3365 | StatusBarNotification sbn1 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 3366 | nb1.build(), new UserHandle(mUid), null, 0); |
| 3367 | NotificationRecord r1 = |
| 3368 | new NotificationRecord(mContext, sbn1, mock(NotificationChannel.class)); |
| 3369 | |
| 3370 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 3371 | .setContentTitle("bar"); |
| 3372 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 3373 | nb2.build(), new UserHandle(mUid), null, 0); |
| 3374 | NotificationRecord r2 = |
| 3375 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 3376 | |
| 3377 | assertTrue(mService.isVisuallyInterruptive(r1, r2)); |
| 3378 | } |
| 3379 | |
| 3380 | @Test |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 3381 | public void testVisualDifference_inboxStyle() { |
| 3382 | Notification.Builder nb1 = new Notification.Builder(mContext, "") |
| 3383 | .setStyle(new Notification.InboxStyle() |
| 3384 | .addLine("line1").addLine("line2")); |
| 3385 | StatusBarNotification sbn1 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 3386 | nb1.build(), new UserHandle(mUid), null, 0); |
| 3387 | NotificationRecord r1 = |
| 3388 | new NotificationRecord(mContext, sbn1, mock(NotificationChannel.class)); |
| 3389 | |
| 3390 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 3391 | .setStyle(new Notification.InboxStyle() |
| 3392 | .addLine("line1").addLine("line2_changed")); |
| 3393 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 3394 | nb2.build(), new UserHandle(mUid), null, 0); |
| 3395 | NotificationRecord r2 = |
| 3396 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 3397 | |
| 3398 | assertTrue(mService.isVisuallyInterruptive(r1, r2)); // line 2 changed unnoticed |
| 3399 | |
| 3400 | Notification.Builder nb3 = new Notification.Builder(mContext, "") |
| 3401 | .setStyle(new Notification.InboxStyle() |
| 3402 | .addLine("line1")); |
| 3403 | StatusBarNotification sbn3 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 3404 | nb3.build(), new UserHandle(mUid), null, 0); |
| 3405 | NotificationRecord r3 = |
| 3406 | new NotificationRecord(mContext, sbn3, mock(NotificationChannel.class)); |
| 3407 | |
| 3408 | assertTrue(mService.isVisuallyInterruptive(r1, r3)); // line 2 removed unnoticed |
| 3409 | |
| 3410 | Notification.Builder nb4 = new Notification.Builder(mContext, "") |
| 3411 | .setStyle(new Notification.InboxStyle() |
| 3412 | .addLine("line1").addLine("line2").addLine("line3")); |
| 3413 | StatusBarNotification sbn4 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 3414 | nb4.build(), new UserHandle(mUid), null, 0); |
| 3415 | NotificationRecord r4 = |
| 3416 | new NotificationRecord(mContext, sbn4, mock(NotificationChannel.class)); |
| 3417 | |
| 3418 | assertTrue(mService.isVisuallyInterruptive(r1, r4)); // line 3 added unnoticed |
| 3419 | |
| 3420 | Notification.Builder nb5 = new Notification.Builder(mContext, "") |
| 3421 | .setContentText("not an inbox"); |
| 3422 | StatusBarNotification sbn5 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 3423 | nb5.build(), new UserHandle(mUid), null, 0); |
| 3424 | NotificationRecord r5 = |
| 3425 | new NotificationRecord(mContext, sbn5, mock(NotificationChannel.class)); |
| 3426 | |
| 3427 | assertTrue(mService.isVisuallyInterruptive(r1, r5)); // changed Styles, went unnoticed |
| 3428 | } |
| 3429 | |
| 3430 | @Test |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 3431 | public void testVisualDifference_diffText() { |
| 3432 | Notification.Builder nb1 = new Notification.Builder(mContext, "") |
| 3433 | .setContentText("foo"); |
| 3434 | StatusBarNotification sbn1 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 3435 | nb1.build(), new UserHandle(mUid), null, 0); |
| 3436 | NotificationRecord r1 = |
| 3437 | new NotificationRecord(mContext, sbn1, mock(NotificationChannel.class)); |
| 3438 | |
| 3439 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 3440 | .setContentText("bar"); |
| 3441 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 3442 | nb2.build(), new UserHandle(mUid), null, 0); |
| 3443 | NotificationRecord r2 = |
| 3444 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 3445 | |
| 3446 | assertTrue(mService.isVisuallyInterruptive(r1, r2)); |
| 3447 | } |
| 3448 | |
| 3449 | @Test |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 3450 | public void testVisualDifference_sameText() { |
| 3451 | Notification.Builder nb1 = new Notification.Builder(mContext, "") |
| 3452 | .setContentText("foo"); |
| 3453 | StatusBarNotification sbn1 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 3454 | nb1.build(), new UserHandle(mUid), null, 0); |
| 3455 | NotificationRecord r1 = |
| 3456 | new NotificationRecord(mContext, sbn1, mock(NotificationChannel.class)); |
| 3457 | |
| 3458 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 3459 | .setContentText("foo"); |
| 3460 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 3461 | nb2.build(), new UserHandle(mUid), null, 0); |
| 3462 | NotificationRecord r2 = |
| 3463 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 3464 | |
| 3465 | assertFalse(mService.isVisuallyInterruptive(r1, r2)); |
| 3466 | } |
| 3467 | |
| 3468 | @Test |
| 3469 | public void testVisualDifference_sameTextButStyled() { |
| 3470 | Notification.Builder nb1 = new Notification.Builder(mContext, "") |
| 3471 | .setContentText(Html.fromHtml("<b>foo</b>")); |
| 3472 | StatusBarNotification sbn1 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 3473 | nb1.build(), new UserHandle(mUid), null, 0); |
| 3474 | NotificationRecord r1 = |
| 3475 | new NotificationRecord(mContext, sbn1, mock(NotificationChannel.class)); |
| 3476 | |
| 3477 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 3478 | .setContentText(Html.fromHtml("<b>foo</b>")); |
| 3479 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 3480 | nb2.build(), new UserHandle(mUid), null, 0); |
| 3481 | NotificationRecord r2 = |
| 3482 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 3483 | |
| 3484 | assertFalse(mService.isVisuallyInterruptive(r1, r2)); |
| 3485 | } |
| 3486 | |
| 3487 | @Test |
| 3488 | public void testVisualDifference_diffTextButStyled() { |
| 3489 | Notification.Builder nb1 = new Notification.Builder(mContext, "") |
| 3490 | .setContentText(Html.fromHtml("<b>foo</b>")); |
| 3491 | StatusBarNotification sbn1 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 3492 | nb1.build(), new UserHandle(mUid), null, 0); |
| 3493 | NotificationRecord r1 = |
| 3494 | new NotificationRecord(mContext, sbn1, mock(NotificationChannel.class)); |
| 3495 | |
| 3496 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 3497 | .setContentText(Html.fromHtml("<b>bar</b>")); |
| 3498 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 3499 | nb2.build(), new UserHandle(mUid), null, 0); |
| 3500 | NotificationRecord r2 = |
| 3501 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 3502 | |
| 3503 | assertTrue(mService.isVisuallyInterruptive(r1, r2)); |
| 3504 | } |
| 3505 | |
| 3506 | @Test |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 3507 | public void testVisualDifference_diffProgress() { |
| 3508 | Notification.Builder nb1 = new Notification.Builder(mContext, "") |
| 3509 | .setProgress(100, 90, false); |
| 3510 | StatusBarNotification sbn1 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 3511 | nb1.build(), new UserHandle(mUid), null, 0); |
| 3512 | NotificationRecord r1 = |
| 3513 | new NotificationRecord(mContext, sbn1, mock(NotificationChannel.class)); |
| 3514 | |
| 3515 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 3516 | .setProgress(100, 100, false); |
| 3517 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 3518 | nb2.build(), new UserHandle(mUid), null, 0); |
| 3519 | NotificationRecord r2 = |
| 3520 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 3521 | |
| 3522 | assertTrue(mService.isVisuallyInterruptive(r1, r2)); |
| 3523 | } |
| 3524 | |
| 3525 | @Test |
| 3526 | public void testVisualDifference_diffProgressNotDone() { |
| 3527 | Notification.Builder nb1 = new Notification.Builder(mContext, "") |
| 3528 | .setProgress(100, 90, false); |
| 3529 | StatusBarNotification sbn1 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 3530 | nb1.build(), new UserHandle(mUid), null, 0); |
| 3531 | NotificationRecord r1 = |
| 3532 | new NotificationRecord(mContext, sbn1, mock(NotificationChannel.class)); |
| 3533 | |
| 3534 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 3535 | .setProgress(100, 91, false); |
| 3536 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 3537 | nb2.build(), new UserHandle(mUid), null, 0); |
| 3538 | NotificationRecord r2 = |
| 3539 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 3540 | |
| 3541 | assertFalse(mService.isVisuallyInterruptive(r1, r2)); |
| 3542 | } |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 3543 | |
| 3544 | @Test |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 3545 | public void testVisualDifference_sameProgressStillDone() { |
| 3546 | Notification.Builder nb1 = new Notification.Builder(mContext, "") |
| 3547 | .setProgress(100, 100, false); |
| 3548 | StatusBarNotification sbn1 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 3549 | nb1.build(), new UserHandle(mUid), null, 0); |
| 3550 | NotificationRecord r1 = |
| 3551 | new NotificationRecord(mContext, sbn1, mock(NotificationChannel.class)); |
| 3552 | |
| 3553 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 3554 | .setProgress(100, 100, false); |
| 3555 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 3556 | nb2.build(), new UserHandle(mUid), null, 0); |
| 3557 | NotificationRecord r2 = |
| 3558 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 3559 | |
| 3560 | assertFalse(mService.isVisuallyInterruptive(r1, r2)); |
| 3561 | } |
| 3562 | |
| 3563 | @Test |
Julia Reynolds | a4fb9da | 2018-06-04 12:27:58 -0400 | [diff] [blame] | 3564 | public void testVisualDifference_summary() { |
| 3565 | Notification.Builder nb1 = new Notification.Builder(mContext, "") |
| 3566 | .setGroup("bananas") |
| 3567 | .setFlag(Notification.FLAG_GROUP_SUMMARY, true) |
| 3568 | .setContentText("foo"); |
| 3569 | StatusBarNotification sbn1 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 3570 | nb1.build(), new UserHandle(mUid), null, 0); |
| 3571 | NotificationRecord r1 = |
| 3572 | new NotificationRecord(mContext, sbn1, mock(NotificationChannel.class)); |
| 3573 | |
| 3574 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 3575 | .setGroup("bananas") |
| 3576 | .setFlag(Notification.FLAG_GROUP_SUMMARY, true) |
| 3577 | .setContentText("bar"); |
| 3578 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 3579 | nb2.build(), new UserHandle(mUid), null, 0); |
| 3580 | NotificationRecord r2 = |
| 3581 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 3582 | |
| 3583 | assertFalse(mService.isVisuallyInterruptive(r1, r2)); |
| 3584 | } |
| 3585 | |
| 3586 | @Test |
Julia Reynolds | 760fa76 | 2018-06-19 15:39:23 -0400 | [diff] [blame] | 3587 | public void testVisualDifference_summaryNewNotification() { |
| 3588 | Notification.Builder nb2 = new Notification.Builder(mContext, "") |
| 3589 | .setGroup("bananas") |
| 3590 | .setFlag(Notification.FLAG_GROUP_SUMMARY, true) |
| 3591 | .setContentText("bar"); |
| 3592 | StatusBarNotification sbn2 = new StatusBarNotification(PKG, PKG, 0, "tag", mUid, 0, |
| 3593 | nb2.build(), new UserHandle(mUid), null, 0); |
| 3594 | NotificationRecord r2 = |
| 3595 | new NotificationRecord(mContext, sbn2, mock(NotificationChannel.class)); |
| 3596 | |
| 3597 | assertFalse(mService.isVisuallyInterruptive(null, r2)); |
| 3598 | } |
| 3599 | |
| 3600 | @Test |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 3601 | public void testHideAndUnhideNotificationsOnSuspendedPackageBroadcast() { |
| 3602 | // post 2 notification from this package |
| 3603 | final NotificationRecord notif1 = generateNotificationRecord( |
| 3604 | mTestNotificationChannel, 1, null, true); |
| 3605 | final NotificationRecord notif2 = generateNotificationRecord( |
| 3606 | mTestNotificationChannel, 2, null, false); |
| 3607 | mService.addNotification(notif1); |
| 3608 | mService.addNotification(notif2); |
| 3609 | |
| 3610 | // on broadcast, hide the 2 notifications |
| 3611 | mService.simulatePackageSuspendBroadcast(true, PKG); |
| 3612 | ArgumentCaptor<List> captorHide = ArgumentCaptor.forClass(List.class); |
| 3613 | verify(mListeners, times(1)).notifyHiddenLocked(captorHide.capture()); |
| 3614 | assertEquals(2, captorHide.getValue().size()); |
| 3615 | |
| 3616 | // on broadcast, unhide the 2 notifications |
| 3617 | mService.simulatePackageSuspendBroadcast(false, PKG); |
| 3618 | ArgumentCaptor<List> captorUnhide = ArgumentCaptor.forClass(List.class); |
| 3619 | verify(mListeners, times(1)).notifyUnhiddenLocked(captorUnhide.capture()); |
| 3620 | assertEquals(2, captorUnhide.getValue().size()); |
| 3621 | } |
| 3622 | |
| 3623 | @Test |
| 3624 | public void testNoNotificationsHiddenOnSuspendedPackageBroadcast() { |
| 3625 | // post 2 notification from this package |
| 3626 | final NotificationRecord notif1 = generateNotificationRecord( |
| 3627 | mTestNotificationChannel, 1, null, true); |
| 3628 | final NotificationRecord notif2 = generateNotificationRecord( |
| 3629 | mTestNotificationChannel, 2, null, false); |
| 3630 | mService.addNotification(notif1); |
| 3631 | mService.addNotification(notif2); |
| 3632 | |
| 3633 | // on broadcast, nothing is hidden since no notifications are of package "test_package" |
| 3634 | mService.simulatePackageSuspendBroadcast(true, "test_package"); |
| 3635 | ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class); |
| 3636 | verify(mListeners, times(1)).notifyHiddenLocked(captor.capture()); |
| 3637 | assertEquals(0, captor.getValue().size()); |
| 3638 | } |
Kristian Monsen | 05f3479 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 3639 | |
| 3640 | @Test |
Julia Reynolds | 0e5a343 | 2019-01-17 09:40:46 -0500 | [diff] [blame] | 3641 | public void testHideAndUnhideNotificationsOnDistractingPackageBroadcast() { |
| 3642 | // Post 2 notifications from 2 packages |
| 3643 | NotificationRecord pkgA = new NotificationRecord(mContext, |
| 3644 | generateSbn("a", 1000, 9, 0), mTestNotificationChannel); |
| 3645 | mService.addNotification(pkgA); |
| 3646 | NotificationRecord pkgB = new NotificationRecord(mContext, |
| 3647 | generateSbn("b", 1001, 9, 0), mTestNotificationChannel); |
| 3648 | mService.addNotification(pkgB); |
| 3649 | |
| 3650 | // on broadcast, hide one of the packages |
| 3651 | mService.simulatePackageDistractionBroadcast( |
| 3652 | PackageManager.RESTRICTION_HIDE_NOTIFICATIONS, new String[] {"a"}); |
| 3653 | ArgumentCaptor<List<NotificationRecord>> captorHide = ArgumentCaptor.forClass(List.class); |
| 3654 | verify(mListeners, times(1)).notifyHiddenLocked(captorHide.capture()); |
| 3655 | assertEquals(1, captorHide.getValue().size()); |
| 3656 | assertEquals("a", captorHide.getValue().get(0).sbn.getPackageName()); |
| 3657 | |
| 3658 | // on broadcast, unhide the package |
| 3659 | mService.simulatePackageDistractionBroadcast( |
| 3660 | PackageManager.RESTRICTION_HIDE_FROM_SUGGESTIONS, new String[] {"a"}); |
| 3661 | ArgumentCaptor<List<NotificationRecord>> captorUnhide = ArgumentCaptor.forClass(List.class); |
| 3662 | verify(mListeners, times(1)).notifyUnhiddenLocked(captorUnhide.capture()); |
| 3663 | assertEquals(1, captorUnhide.getValue().size()); |
| 3664 | assertEquals("a", captorUnhide.getValue().get(0).sbn.getPackageName()); |
| 3665 | } |
| 3666 | |
| 3667 | @Test |
| 3668 | public void testHideAndUnhideNotificationsOnDistractingPackageBroadcast_multiPkg() { |
| 3669 | // Post 2 notifications from 2 packages |
| 3670 | NotificationRecord pkgA = new NotificationRecord(mContext, |
| 3671 | generateSbn("a", 1000, 9, 0), mTestNotificationChannel); |
| 3672 | mService.addNotification(pkgA); |
| 3673 | NotificationRecord pkgB = new NotificationRecord(mContext, |
| 3674 | generateSbn("b", 1001, 9, 0), mTestNotificationChannel); |
| 3675 | mService.addNotification(pkgB); |
| 3676 | |
| 3677 | // on broadcast, hide one of the packages |
| 3678 | mService.simulatePackageDistractionBroadcast( |
| 3679 | PackageManager.RESTRICTION_HIDE_NOTIFICATIONS, new String[] {"a", "b"}); |
| 3680 | ArgumentCaptor<List<NotificationRecord>> captorHide = ArgumentCaptor.forClass(List.class); |
woongki min | bbc9728 | 2019-09-25 11:50:48 +0900 | [diff] [blame] | 3681 | |
| 3682 | // should be called only once. |
| 3683 | verify(mListeners, times(1)).notifyHiddenLocked(captorHide.capture()); |
Julia Reynolds | 0e5a343 | 2019-01-17 09:40:46 -0500 | [diff] [blame] | 3684 | assertEquals(2, captorHide.getValue().size()); |
| 3685 | assertEquals("a", captorHide.getValue().get(0).sbn.getPackageName()); |
| 3686 | assertEquals("b", captorHide.getValue().get(1).sbn.getPackageName()); |
| 3687 | |
| 3688 | // on broadcast, unhide the package |
| 3689 | mService.simulatePackageDistractionBroadcast( |
| 3690 | PackageManager.RESTRICTION_HIDE_FROM_SUGGESTIONS, new String[] {"a", "b"}); |
| 3691 | ArgumentCaptor<List<NotificationRecord>> captorUnhide = ArgumentCaptor.forClass(List.class); |
woongki min | bbc9728 | 2019-09-25 11:50:48 +0900 | [diff] [blame] | 3692 | |
| 3693 | // should be called only once. |
| 3694 | verify(mListeners, times(1)).notifyUnhiddenLocked(captorUnhide.capture()); |
Julia Reynolds | 0e5a343 | 2019-01-17 09:40:46 -0500 | [diff] [blame] | 3695 | assertEquals(2, captorUnhide.getValue().size()); |
| 3696 | assertEquals("a", captorUnhide.getValue().get(0).sbn.getPackageName()); |
| 3697 | assertEquals("b", captorUnhide.getValue().get(1).sbn.getPackageName()); |
| 3698 | } |
| 3699 | |
| 3700 | @Test |
| 3701 | public void testNoNotificationsHiddenOnDistractingPackageBroadcast() { |
| 3702 | // post notification from this package |
| 3703 | final NotificationRecord notif1 = generateNotificationRecord( |
| 3704 | mTestNotificationChannel, 1, null, true); |
| 3705 | mService.addNotification(notif1); |
| 3706 | |
| 3707 | // on broadcast, nothing is hidden since no notifications are of package "test_package" |
| 3708 | mService.simulatePackageDistractionBroadcast( |
| 3709 | PackageManager.RESTRICTION_HIDE_NOTIFICATIONS, new String[] {"test_package"}); |
| 3710 | ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class); |
| 3711 | verify(mListeners, times(1)).notifyHiddenLocked(captor.capture()); |
| 3712 | assertEquals(0, captor.getValue().size()); |
| 3713 | } |
| 3714 | |
| 3715 | @Test |
Kristian Monsen | 05f3479 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 3716 | public void testCanUseManagedServicesLowRamNoWatchNullPkg() { |
| 3717 | when(mPackageManagerClient.hasSystemFeature(FEATURE_WATCH)).thenReturn(false); |
| 3718 | when(mActivityManager.isLowRamDevice()).thenReturn(true); |
| 3719 | when(mResources.getStringArray(R.array.config_allowedManagedServicesOnLowRamDevices)) |
| 3720 | .thenReturn(new String[] {"a", "b", "c"}); |
| 3721 | when(mContext.getResources()).thenReturn(mResources); |
| 3722 | |
Julia Reynolds | d0ceefa | 2019-03-03 16:10:52 -0500 | [diff] [blame] | 3723 | assertEquals(false, mService.canUseManagedServices(null, 0, null)); |
Kristian Monsen | 05f3479 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 3724 | } |
| 3725 | |
| 3726 | @Test |
| 3727 | public void testCanUseManagedServicesLowRamNoWatchValidPkg() { |
| 3728 | when(mPackageManagerClient.hasSystemFeature(FEATURE_WATCH)).thenReturn(false); |
| 3729 | when(mActivityManager.isLowRamDevice()).thenReturn(true); |
| 3730 | when(mResources.getStringArray(R.array.config_allowedManagedServicesOnLowRamDevices)) |
| 3731 | .thenReturn(new String[] {"a", "b", "c"}); |
| 3732 | when(mContext.getResources()).thenReturn(mResources); |
| 3733 | |
Julia Reynolds | d0ceefa | 2019-03-03 16:10:52 -0500 | [diff] [blame] | 3734 | assertEquals(true, mService.canUseManagedServices("b", 0, null)); |
Kristian Monsen | 05f3479 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 3735 | } |
| 3736 | |
| 3737 | @Test |
| 3738 | public void testCanUseManagedServicesLowRamNoWatchNoValidPkg() { |
| 3739 | when(mPackageManagerClient.hasSystemFeature(FEATURE_WATCH)).thenReturn(false); |
| 3740 | when(mActivityManager.isLowRamDevice()).thenReturn(true); |
| 3741 | when(mResources.getStringArray(R.array.config_allowedManagedServicesOnLowRamDevices)) |
| 3742 | .thenReturn(new String[] {"a", "b", "c"}); |
| 3743 | when(mContext.getResources()).thenReturn(mResources); |
| 3744 | |
Julia Reynolds | d0ceefa | 2019-03-03 16:10:52 -0500 | [diff] [blame] | 3745 | assertEquals(false, mService.canUseManagedServices("d", 0, null)); |
Kristian Monsen | 05f3479 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 3746 | } |
| 3747 | |
| 3748 | @Test |
| 3749 | public void testCanUseManagedServicesLowRamWatchNoValidPkg() { |
| 3750 | when(mPackageManagerClient.hasSystemFeature(FEATURE_WATCH)).thenReturn(true); |
| 3751 | when(mActivityManager.isLowRamDevice()).thenReturn(true); |
| 3752 | when(mResources.getStringArray(R.array.config_allowedManagedServicesOnLowRamDevices)) |
| 3753 | .thenReturn(new String[] {"a", "b", "c"}); |
| 3754 | when(mContext.getResources()).thenReturn(mResources); |
| 3755 | |
Julia Reynolds | d0ceefa | 2019-03-03 16:10:52 -0500 | [diff] [blame] | 3756 | assertEquals(true, mService.canUseManagedServices("d", 0, null)); |
Kristian Monsen | 05f3479 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 3757 | } |
| 3758 | |
| 3759 | @Test |
| 3760 | public void testCanUseManagedServicesNoLowRamNoWatchValidPkg() { |
| 3761 | when(mPackageManagerClient.hasSystemFeature(FEATURE_WATCH)).thenReturn(false); |
| 3762 | when(mActivityManager.isLowRamDevice()).thenReturn(false); |
| 3763 | when(mResources.getStringArray(R.array.config_allowedManagedServicesOnLowRamDevices)) |
| 3764 | .thenReturn(new String[] {"a", "b", "c"}); |
| 3765 | when(mContext.getResources()).thenReturn(mResources); |
| 3766 | |
Julia Reynolds | d0ceefa | 2019-03-03 16:10:52 -0500 | [diff] [blame] | 3767 | assertEquals(true, mService.canUseManagedServices("d", 0 , null)); |
Kristian Monsen | 05f3479 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 3768 | } |
| 3769 | |
| 3770 | @Test |
| 3771 | public void testCanUseManagedServicesNoLowRamWatchValidPkg() { |
| 3772 | when(mPackageManagerClient.hasSystemFeature(FEATURE_WATCH)).thenReturn(true); |
| 3773 | when(mActivityManager.isLowRamDevice()).thenReturn(false); |
| 3774 | when(mResources.getStringArray(R.array.config_allowedManagedServicesOnLowRamDevices)) |
| 3775 | .thenReturn(new String[] {"a", "b", "c"}); |
| 3776 | when(mContext.getResources()).thenReturn(mResources); |
| 3777 | |
Julia Reynolds | d0ceefa | 2019-03-03 16:10:52 -0500 | [diff] [blame] | 3778 | assertEquals(true, mService.canUseManagedServices("d", 0, null)); |
| 3779 | } |
| 3780 | |
| 3781 | @Test |
| 3782 | public void testCanUseManagedServices_hasPermission() throws Exception { |
| 3783 | when(mPackageManager.checkPermission("perm", "pkg", 0)) |
| 3784 | .thenReturn(PackageManager.PERMISSION_GRANTED); |
| 3785 | |
| 3786 | assertEquals(true, mService.canUseManagedServices("pkg", 0, "perm")); |
| 3787 | } |
| 3788 | |
| 3789 | @Test |
| 3790 | public void testCanUseManagedServices_noPermission() throws Exception { |
| 3791 | when(mPackageManager.checkPermission("perm", "pkg", 0)) |
| 3792 | .thenReturn(PackageManager.PERMISSION_DENIED); |
| 3793 | |
| 3794 | assertEquals(false, mService.canUseManagedServices("pkg", 0, "perm")); |
| 3795 | } |
| 3796 | |
| 3797 | @Test |
| 3798 | public void testCanUseManagedServices_permDoesNotMatter() { |
| 3799 | assertEquals(true, mService.canUseManagedServices("pkg", 0, null)); |
Kristian Monsen | 05f3479 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 3800 | } |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 3801 | |
| 3802 | @Test |
| 3803 | public void testOnNotificationVisibilityChanged_triggersInterruptionUsageStat() { |
| 3804 | final NotificationRecord r = generateNotificationRecord( |
| 3805 | mTestNotificationChannel, 1, null, true); |
| 3806 | r.setTextChanged(true); |
| 3807 | mService.addNotification(r); |
| 3808 | |
| 3809 | mService.mNotificationDelegate.onNotificationVisibilityChanged(new NotificationVisibility[] |
| 3810 | {NotificationVisibility.obtain(r.getKey(), 1, 1, true)}, |
| 3811 | new NotificationVisibility[]{}); |
| 3812 | |
| 3813 | verify(mAppUsageStats).reportInterruptiveNotification(anyString(), anyString(), anyInt()); |
| 3814 | } |
| 3815 | |
| 3816 | @Test |
| 3817 | public void testSetNotificationsShownFromListener_triggersInterruptionUsageStat() |
| 3818 | throws RemoteException { |
| 3819 | final NotificationRecord r = generateNotificationRecord( |
| 3820 | mTestNotificationChannel, 1, null, true); |
| 3821 | r.setTextChanged(true); |
| 3822 | mService.addNotification(r); |
| 3823 | |
| 3824 | mBinderService.setNotificationsShownFromListener(null, new String[] {r.getKey()}); |
| 3825 | |
| 3826 | verify(mAppUsageStats).reportInterruptiveNotification(anyString(), anyString(), anyInt()); |
| 3827 | } |
| 3828 | |
| 3829 | @Test |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 3830 | public void testMaybeRecordInterruptionLocked_doesNotRecordTwice() |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 3831 | throws RemoteException { |
| 3832 | final NotificationRecord r = generateNotificationRecord( |
| 3833 | mTestNotificationChannel, 1, null, true); |
| 3834 | r.setInterruptive(true); |
| 3835 | mService.addNotification(r); |
| 3836 | |
| 3837 | mService.maybeRecordInterruptionLocked(r); |
| 3838 | mService.maybeRecordInterruptionLocked(r); |
| 3839 | |
| 3840 | verify(mAppUsageStats, times(1)).reportInterruptiveNotification( |
| 3841 | anyString(), anyString(), anyInt()); |
| 3842 | } |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 3843 | |
| 3844 | @Test |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 3845 | public void testBubble() throws Exception { |
| 3846 | mBinderService.setBubblesAllowed(PKG, mUid, false); |
| 3847 | assertFalse(mBinderService.areBubblesAllowedForPackage(PKG, mUid)); |
Julia Reynolds | 33ab8a0 | 2018-12-17 16:19:52 -0500 | [diff] [blame] | 3848 | } |
| 3849 | |
| 3850 | @Test |
Mady Mellor | 9db685a | 2019-01-23 13:23:37 -0800 | [diff] [blame] | 3851 | public void testUserApprovedBubblesForPackage() throws Exception { |
| 3852 | assertFalse(mBinderService.hasUserApprovedBubblesForPackage(PKG, mUid)); |
| 3853 | mBinderService.setBubblesAllowed(PKG, mUid, true); |
| 3854 | assertTrue(mBinderService.hasUserApprovedBubblesForPackage(PKG, mUid)); |
| 3855 | assertTrue(mBinderService.areBubblesAllowedForPackage(PKG, mUid)); |
| 3856 | } |
| 3857 | |
| 3858 | @Test |
| 3859 | public void testUserRejectsBubblesForPackage() throws Exception { |
| 3860 | assertFalse(mBinderService.hasUserApprovedBubblesForPackage(PKG, mUid)); |
| 3861 | mBinderService.setBubblesAllowed(PKG, mUid, false); |
| 3862 | assertTrue(mBinderService.hasUserApprovedBubblesForPackage(PKG, mUid)); |
| 3863 | assertFalse(mBinderService.areBubblesAllowedForPackage(PKG, mUid)); |
| 3864 | } |
| 3865 | |
| 3866 | @Test |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 3867 | public void testIsCallerInstantApp_primaryUser() throws Exception { |
| 3868 | ApplicationInfo info = new ApplicationInfo(); |
| 3869 | info.privateFlags = ApplicationInfo.PRIVATE_FLAG_INSTANT; |
| 3870 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), eq(0))).thenReturn(info); |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 3871 | when(mPackageManager.getPackagesForUid(anyInt())).thenReturn(new String[]{"any"}); |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 3872 | |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 3873 | assertTrue(mService.isCallerInstantApp(45770, 0)); |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 3874 | |
| 3875 | info.privateFlags = 0; |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 3876 | assertFalse(mService.isCallerInstantApp(575370, 0)); |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 3877 | } |
| 3878 | |
| 3879 | @Test |
| 3880 | public void testIsCallerInstantApp_secondaryUser() throws Exception { |
| 3881 | ApplicationInfo info = new ApplicationInfo(); |
| 3882 | info.privateFlags = ApplicationInfo.PRIVATE_FLAG_INSTANT; |
| 3883 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), eq(10))).thenReturn(info); |
| 3884 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), eq(0))).thenReturn(null); |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 3885 | when(mPackageManager.getPackagesForUid(anyInt())).thenReturn(new String[]{"any"}); |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 3886 | |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 3887 | assertTrue(mService.isCallerInstantApp(68638450, 10)); |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 3888 | } |
| 3889 | |
| 3890 | @Test |
Julia Reynolds | 86869c9 | 2019-06-21 10:45:06 -0400 | [diff] [blame] | 3891 | public void testIsCallerInstantApp_userAllNotification() throws Exception { |
| 3892 | ApplicationInfo info = new ApplicationInfo(); |
| 3893 | info.privateFlags = ApplicationInfo.PRIVATE_FLAG_INSTANT; |
| 3894 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), eq(UserHandle.USER_SYSTEM))) |
| 3895 | .thenReturn(info); |
| 3896 | when(mPackageManager.getPackagesForUid(anyInt())).thenReturn(new String[]{"any"}); |
| 3897 | |
| 3898 | assertTrue(mService.isCallerInstantApp(45770, UserHandle.USER_ALL)); |
| 3899 | |
| 3900 | info.privateFlags = 0; |
| 3901 | assertFalse(mService.isCallerInstantApp(575370, UserHandle.USER_ALL )); |
| 3902 | } |
| 3903 | |
| 3904 | @Test |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 3905 | public void testResolveNotificationUid_sameApp_nonSystemUser() throws Exception { |
| 3906 | ApplicationInfo info = new ApplicationInfo(); |
| 3907 | info.uid = Binder.getCallingUid(); |
| 3908 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), eq(10))).thenReturn(info); |
| 3909 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), eq(0))).thenReturn(null); |
| 3910 | |
| 3911 | int actualUid = mService.resolveNotificationUid("caller", "caller", info.uid, 10); |
| 3912 | |
| 3913 | assertEquals(info.uid, actualUid); |
| 3914 | } |
| 3915 | |
| 3916 | @Test |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 3917 | public void testResolveNotificationUid_sameApp() throws Exception { |
| 3918 | ApplicationInfo info = new ApplicationInfo(); |
| 3919 | info.uid = Binder.getCallingUid(); |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 3920 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), eq(0))).thenReturn(info); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 3921 | |
| 3922 | int actualUid = mService.resolveNotificationUid("caller", "caller", info.uid, 0); |
| 3923 | |
| 3924 | assertEquals(info.uid, actualUid); |
| 3925 | } |
| 3926 | |
| 3927 | @Test |
Julia Reynolds | ecc1b57 | 2018-10-01 16:19:24 -0400 | [diff] [blame] | 3928 | public void testResolveNotificationUid_sameAppDiffPackage() throws Exception { |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 3929 | ApplicationInfo info = new ApplicationInfo(); |
| 3930 | info.uid = Binder.getCallingUid(); |
Julia Reynolds | ecc1b57 | 2018-10-01 16:19:24 -0400 | [diff] [blame] | 3931 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), eq(0))).thenReturn(info); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 3932 | |
Julia Reynolds | ecc1b57 | 2018-10-01 16:19:24 -0400 | [diff] [blame] | 3933 | int actualUid = mService.resolveNotificationUid("caller", "callerAlso", info.uid, 0); |
| 3934 | |
| 3935 | assertEquals(info.uid, actualUid); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 3936 | } |
| 3937 | |
| 3938 | @Test |
| 3939 | public void testResolveNotificationUid_sameAppWrongUid() throws Exception { |
| 3940 | ApplicationInfo info = new ApplicationInfo(); |
| 3941 | info.uid = 1356347; |
| 3942 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), anyInt())).thenReturn(info); |
| 3943 | |
| 3944 | try { |
| 3945 | mService.resolveNotificationUid("caller", "caller", 9, 0); |
| 3946 | fail("Incorrect uid didn't throw security exception"); |
| 3947 | } catch (SecurityException e) { |
| 3948 | // yay |
| 3949 | } |
| 3950 | } |
| 3951 | |
| 3952 | @Test |
| 3953 | public void testResolveNotificationUid_delegateAllowed() throws Exception { |
| 3954 | int expectedUid = 123; |
| 3955 | |
| 3956 | when(mPackageManagerClient.getPackageUidAsUser("target", 0)).thenReturn(expectedUid); |
| 3957 | mService.setPreferencesHelper(mPreferencesHelper); |
| 3958 | when(mPreferencesHelper.isDelegateAllowed(anyString(), anyInt(), anyString(), anyInt())) |
| 3959 | .thenReturn(true); |
| 3960 | |
| 3961 | assertEquals(expectedUid, mService.resolveNotificationUid("caller", "target", 9, 0)); |
| 3962 | } |
| 3963 | |
| 3964 | @Test |
| 3965 | public void testResolveNotificationUid_androidAllowed() throws Exception { |
| 3966 | int expectedUid = 123; |
| 3967 | |
| 3968 | when(mPackageManagerClient.getPackageUidAsUser("target", 0)).thenReturn(expectedUid); |
| 3969 | // no delegate |
| 3970 | |
| 3971 | assertEquals(expectedUid, mService.resolveNotificationUid("android", "target", 0, 0)); |
| 3972 | } |
| 3973 | |
| 3974 | @Test |
| 3975 | public void testResolveNotificationUid_delegateNotAllowed() throws Exception { |
| 3976 | when(mPackageManagerClient.getPackageUidAsUser("target", 0)).thenReturn(123); |
| 3977 | // no delegate |
| 3978 | |
| 3979 | try { |
| 3980 | mService.resolveNotificationUid("caller", "target", 9, 0); |
| 3981 | fail("Incorrect uid didn't throw security exception"); |
| 3982 | } catch (SecurityException e) { |
| 3983 | // yay |
| 3984 | } |
| 3985 | } |
Julia Reynolds | 564273f | 2018-09-13 15:53:11 -0400 | [diff] [blame] | 3986 | |
| 3987 | @Test |
| 3988 | public void testRemoveForegroundServiceFlagFromNotification_enqueued() { |
| 3989 | Notification n = new Notification.Builder(mContext, "").build(); |
| 3990 | n.flags |= FLAG_FOREGROUND_SERVICE; |
| 3991 | |
| 3992 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 9, null, mUid, 0, |
| 3993 | n, new UserHandle(mUid), null, 0); |
| 3994 | NotificationRecord r = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 3995 | |
| 3996 | mService.addEnqueuedNotification(r); |
| 3997 | |
| 3998 | mInternalService.removeForegroundServiceFlagFromNotification( |
| 3999 | PKG, r.sbn.getId(), r.sbn.getUserId()); |
| 4000 | |
| 4001 | waitForIdle(); |
| 4002 | |
| 4003 | verify(mListeners, timeout(200).times(0)).notifyPostedLocked(any(), any()); |
| 4004 | } |
| 4005 | |
| 4006 | @Test |
| 4007 | public void testRemoveForegroundServiceFlagFromNotification_posted() { |
| 4008 | Notification n = new Notification.Builder(mContext, "").build(); |
| 4009 | n.flags |= FLAG_FOREGROUND_SERVICE; |
| 4010 | |
| 4011 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 9, null, mUid, 0, |
| 4012 | n, new UserHandle(mUid), null, 0); |
| 4013 | NotificationRecord r = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 4014 | |
| 4015 | mService.addNotification(r); |
| 4016 | |
| 4017 | mInternalService.removeForegroundServiceFlagFromNotification( |
| 4018 | PKG, r.sbn.getId(), r.sbn.getUserId()); |
| 4019 | |
| 4020 | waitForIdle(); |
| 4021 | |
| 4022 | ArgumentCaptor<NotificationRecord> captor = |
| 4023 | ArgumentCaptor.forClass(NotificationRecord.class); |
| 4024 | verify(mListeners, times(1)).notifyPostedLocked(captor.capture(), any()); |
| 4025 | |
| 4026 | assertEquals(0, captor.getValue().getNotification().flags); |
| 4027 | } |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 4028 | |
| 4029 | @Test |
| 4030 | public void testAllowForegroundToasts() throws Exception { |
| 4031 | final String testPackage = "testPackageName"; |
| 4032 | assertEquals(0, mService.mToastQueue.size()); |
| 4033 | mService.isSystemUid = false; |
| 4034 | |
| 4035 | // package is not suspended |
| 4036 | when(mPackageManager.isPackageSuspendedForUser(testPackage, UserHandle.getUserId(mUid))) |
| 4037 | .thenReturn(false); |
| 4038 | |
| 4039 | // notifications from this package are blocked by the user |
| 4040 | mService.setPreferencesHelper(mPreferencesHelper); |
| 4041 | when(mPreferencesHelper.getImportance(testPackage, mUid)).thenReturn(IMPORTANCE_NONE); |
| 4042 | |
| 4043 | // this app is in the foreground |
| 4044 | when(mActivityManager.getUidImportance(mUid)).thenReturn(IMPORTANCE_FOREGROUND); |
| 4045 | |
| 4046 | // enqueue toast -> toast should still enqueue |
| 4047 | ((INotificationManager)mService.mService).enqueueToast(testPackage, |
| 4048 | new TestableToastCallback(), 2000, 0); |
| 4049 | assertEquals(1, mService.mToastQueue.size()); |
| 4050 | } |
| 4051 | |
| 4052 | @Test |
| 4053 | public void testDisallowToastsFromSuspendedPackages() throws Exception { |
| 4054 | final String testPackage = "testPackageName"; |
| 4055 | assertEquals(0, mService.mToastQueue.size()); |
| 4056 | mService.isSystemUid = false; |
| 4057 | |
| 4058 | // package is suspended |
| 4059 | when(mPackageManager.isPackageSuspendedForUser(testPackage, UserHandle.getUserId(mUid))) |
| 4060 | .thenReturn(true); |
| 4061 | |
| 4062 | // notifications from this package are NOT blocked by the user |
| 4063 | mService.setPreferencesHelper(mPreferencesHelper); |
| 4064 | when(mPreferencesHelper.getImportance(testPackage, mUid)).thenReturn(IMPORTANCE_LOW); |
| 4065 | |
| 4066 | // enqueue toast -> no toasts enqueued |
| 4067 | ((INotificationManager)mService.mService).enqueueToast(testPackage, |
| 4068 | new TestableToastCallback(), 2000, 0); |
| 4069 | assertEquals(0, mService.mToastQueue.size()); |
| 4070 | } |
| 4071 | |
| 4072 | @Test |
| 4073 | public void testDisallowToastsFromBlockedApps() throws Exception { |
| 4074 | final String testPackage = "testPackageName"; |
| 4075 | assertEquals(0, mService.mToastQueue.size()); |
| 4076 | mService.isSystemUid = false; |
| 4077 | |
| 4078 | // package is not suspended |
| 4079 | when(mPackageManager.isPackageSuspendedForUser(testPackage, UserHandle.getUserId(mUid))) |
| 4080 | .thenReturn(false); |
| 4081 | |
| 4082 | // notifications from this package are blocked by the user |
| 4083 | mService.setPreferencesHelper(mPreferencesHelper); |
| 4084 | when(mPreferencesHelper.getImportance(testPackage, mUid)).thenReturn(IMPORTANCE_NONE); |
| 4085 | |
| 4086 | // this app is NOT in the foreground |
| 4087 | when(mActivityManager.getUidImportance(mUid)).thenReturn(IMPORTANCE_GONE); |
| 4088 | |
| 4089 | // enqueue toast -> no toasts enqueued |
| 4090 | ((INotificationManager)mService.mService).enqueueToast(testPackage, |
| 4091 | new TestableToastCallback(), 2000, 0); |
| 4092 | assertEquals(0, mService.mToastQueue.size()); |
| 4093 | } |
| 4094 | |
| 4095 | @Test |
| 4096 | public void testAlwaysAllowSystemToasts() throws Exception { |
| 4097 | final String testPackage = "testPackageName"; |
| 4098 | assertEquals(0, mService.mToastQueue.size()); |
| 4099 | mService.isSystemUid = true; |
| 4100 | |
| 4101 | // package is suspended |
| 4102 | when(mPackageManager.isPackageSuspendedForUser(testPackage, UserHandle.getUserId(mUid))) |
| 4103 | .thenReturn(true); |
| 4104 | |
| 4105 | // notifications from this package ARE blocked by the user |
| 4106 | mService.setPreferencesHelper(mPreferencesHelper); |
| 4107 | when(mPreferencesHelper.getImportance(testPackage, mUid)).thenReturn(IMPORTANCE_NONE); |
| 4108 | |
| 4109 | // this app is NOT in the foreground |
| 4110 | when(mActivityManager.getUidImportance(mUid)).thenReturn(IMPORTANCE_GONE); |
| 4111 | |
| 4112 | // enqueue toast -> system toast can still be enqueued |
| 4113 | ((INotificationManager)mService.mService).enqueueToast(testPackage, |
| 4114 | new TestableToastCallback(), 2000, 0); |
| 4115 | assertEquals(1, mService.mToastQueue.size()); |
| 4116 | } |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 4117 | |
| 4118 | @Test |
| 4119 | public void testOnNotificationSmartReplySent() { |
| 4120 | final int replyIndex = 2; |
| 4121 | final String reply = "Hello"; |
Milo Sredkov | 13d8811 | 2019-02-01 12:23:24 +0000 | [diff] [blame] | 4122 | final boolean modifiedBeforeSending = true; |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 4123 | final boolean generatedByAssistant = true; |
| 4124 | |
| 4125 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
Milo Sredkov | 13d8811 | 2019-02-01 12:23:24 +0000 | [diff] [blame] | 4126 | r.setSuggestionsGeneratedByAssistant(generatedByAssistant); |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 4127 | mService.addNotification(r); |
| 4128 | |
| 4129 | mService.mNotificationDelegate.onNotificationSmartReplySent( |
Milo Sredkov | 13d8811 | 2019-02-01 12:23:24 +0000 | [diff] [blame] | 4130 | r.getKey(), replyIndex, reply, NOTIFICATION_LOCATION_UNKNOWN, |
| 4131 | modifiedBeforeSending); |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 4132 | verify(mAssistants).notifyAssistantSuggestedReplySent( |
| 4133 | eq(r.sbn), eq(reply), eq(generatedByAssistant)); |
| 4134 | } |
Tony Mak | 7d4b3a5 | 2018-11-27 17:29:36 +0000 | [diff] [blame] | 4135 | |
| 4136 | @Test |
| 4137 | public void testOnNotificationActionClick() { |
| 4138 | final int actionIndex = 2; |
| 4139 | final Notification.Action action = |
| 4140 | new Notification.Action.Builder(null, "text", null).build(); |
| 4141 | final boolean generatedByAssistant = false; |
| 4142 | |
| 4143 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 4144 | mService.addNotification(r); |
| 4145 | |
| 4146 | NotificationVisibility notificationVisibility = |
| 4147 | NotificationVisibility.obtain(r.getKey(), 1, 2, true); |
| 4148 | mService.mNotificationDelegate.onNotificationActionClick( |
| 4149 | 10, 10, r.getKey(), actionIndex, action, notificationVisibility, |
| 4150 | generatedByAssistant); |
| 4151 | verify(mAssistants).notifyAssistantActionClicked( |
| 4152 | eq(r.sbn), eq(actionIndex), eq(action), eq(generatedByAssistant)); |
| 4153 | } |
Gustav Sennton | 44dc588 | 2018-12-13 14:38:50 +0000 | [diff] [blame] | 4154 | |
| 4155 | @Test |
| 4156 | public void testLogSmartSuggestionsVisible_triggerOnExpandAndVisible() { |
| 4157 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 4158 | mService.addNotification(r); |
| 4159 | |
Gustav Sennton | a8e38aa | 2019-01-22 14:55:39 +0000 | [diff] [blame] | 4160 | mService.mNotificationDelegate.onNotificationExpansionChanged(r.getKey(), false, true, |
| 4161 | NOTIFICATION_LOCATION_UNKNOWN); |
Gustav Sennton | 44dc588 | 2018-12-13 14:38:50 +0000 | [diff] [blame] | 4162 | NotificationVisibility[] notificationVisibility = new NotificationVisibility[] { |
| 4163 | NotificationVisibility.obtain(r.getKey(), 0, 0, true) |
| 4164 | }; |
| 4165 | mService.mNotificationDelegate.onNotificationVisibilityChanged(notificationVisibility, |
| 4166 | new NotificationVisibility[0]); |
| 4167 | |
| 4168 | assertEquals(1, mService.countLogSmartSuggestionsVisible); |
| 4169 | } |
| 4170 | |
| 4171 | @Test |
| 4172 | public void testLogSmartSuggestionsVisible_noTriggerOnExpand() { |
| 4173 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 4174 | mService.addNotification(r); |
| 4175 | |
Gustav Sennton | a8e38aa | 2019-01-22 14:55:39 +0000 | [diff] [blame] | 4176 | mService.mNotificationDelegate.onNotificationExpansionChanged(r.getKey(), false, true, |
| 4177 | NOTIFICATION_LOCATION_UNKNOWN); |
Gustav Sennton | 44dc588 | 2018-12-13 14:38:50 +0000 | [diff] [blame] | 4178 | |
| 4179 | assertEquals(0, mService.countLogSmartSuggestionsVisible); |
| 4180 | } |
| 4181 | |
| 4182 | @Test |
| 4183 | public void testLogSmartSuggestionsVisible_noTriggerOnVisible() { |
| 4184 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 4185 | mService.addNotification(r); |
| 4186 | |
Julia Reynolds | 9533413 | 2018-12-19 11:15:35 -0500 | [diff] [blame] | 4187 | NotificationVisibility[] notificationVisibility = new NotificationVisibility[]{ |
Gustav Sennton | 44dc588 | 2018-12-13 14:38:50 +0000 | [diff] [blame] | 4188 | NotificationVisibility.obtain(r.getKey(), 0, 0, true) |
| 4189 | }; |
| 4190 | mService.mNotificationDelegate.onNotificationVisibilityChanged(notificationVisibility, |
| 4191 | new NotificationVisibility[0]); |
| 4192 | |
| 4193 | assertEquals(0, mService.countLogSmartSuggestionsVisible); |
| 4194 | } |
Julia Reynolds | 9533413 | 2018-12-19 11:15:35 -0500 | [diff] [blame] | 4195 | |
| 4196 | public void testReportSeen_delegated() { |
| 4197 | Notification.Builder nb = |
| 4198 | new Notification.Builder(mContext, mTestNotificationChannel.getId()) |
| 4199 | .setContentTitle("foo") |
| 4200 | .setSmallIcon(android.R.drawable.sym_def_app_icon); |
| 4201 | |
| 4202 | StatusBarNotification sbn = new StatusBarNotification(PKG, "opPkg", 0, "tag", mUid, 0, |
| 4203 | nb.build(), new UserHandle(mUid), null, 0); |
| 4204 | NotificationRecord r = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 4205 | |
| 4206 | mService.reportSeen(r); |
| 4207 | verify(mAppUsageStats, never()).reportEvent(anyString(), anyInt(), anyInt()); |
| 4208 | |
| 4209 | } |
| 4210 | |
| 4211 | @Test |
| 4212 | public void testReportSeen_notDelegated() { |
| 4213 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 4214 | |
| 4215 | mService.reportSeen(r); |
| 4216 | verify(mAppUsageStats, times(1)).reportEvent(anyString(), anyInt(), anyInt()); |
| 4217 | } |
Julia Reynolds | 3207e2f | 2018-12-20 09:39:53 -0500 | [diff] [blame] | 4218 | |
| 4219 | @Test |
| 4220 | public void testNotificationStats_notificationError() { |
| 4221 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 4222 | mService.addNotification(r); |
| 4223 | |
| 4224 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, "tag", mUid, 0, |
| 4225 | new Notification.Builder(mContext, mTestNotificationChannel.getId()).build(), |
| 4226 | new UserHandle(mUid), null, 0); |
| 4227 | NotificationRecord update = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 4228 | mService.addEnqueuedNotification(update); |
| 4229 | assertNull(update.sbn.getNotification().getSmallIcon()); |
| 4230 | |
| 4231 | NotificationManagerService.PostNotificationRunnable runnable = |
| 4232 | mService.new PostNotificationRunnable(update.getKey()); |
| 4233 | runnable.run(); |
| 4234 | waitForIdle(); |
| 4235 | |
| 4236 | ArgumentCaptor<NotificationStats> captor = ArgumentCaptor.forClass(NotificationStats.class); |
| 4237 | verify(mListeners).notifyRemovedLocked(any(), anyInt(), captor.capture()); |
| 4238 | assertNotNull(captor.getValue()); |
| 4239 | } |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 4240 | |
| 4241 | @Test |
Julia Reynolds | 7a6d07a | 2019-03-18 11:31:56 -0400 | [diff] [blame] | 4242 | public void testCanNotifyAsUser_crossUser() throws Exception { |
| 4243 | // same user no problem |
| 4244 | mBinderService.canNotifyAsPackage("src", "target", mContext.getUserId()); |
| 4245 | |
| 4246 | // cross user, no permission, problem |
| 4247 | try { |
| 4248 | mBinderService.canNotifyAsPackage("src", "target", mContext.getUserId() + 1); |
| 4249 | fail("Should not be callable cross user without cross user permission"); |
| 4250 | } catch (SecurityException e) { |
| 4251 | // good |
| 4252 | } |
| 4253 | |
| 4254 | // cross user, with permission, no problem |
| 4255 | TestablePermissions perms = mContext.getTestablePermissions(); |
| 4256 | perms.setPermission(android.Manifest.permission.INTERACT_ACROSS_USERS, PERMISSION_GRANTED); |
| 4257 | mBinderService.canNotifyAsPackage("src", "target", mContext.getUserId() + 1); |
| 4258 | } |
| 4259 | |
| 4260 | @Test |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 4261 | public void testgetNotificationChannels_crossUser() throws Exception { |
| 4262 | // same user no problem |
| 4263 | mBinderService.getNotificationChannels("src", "target", mContext.getUserId()); |
| 4264 | |
| 4265 | // cross user, no permission, problem |
| 4266 | try { |
| 4267 | mBinderService.getNotificationChannels("src", "target", mContext.getUserId() + 1); |
| 4268 | fail("Should not be callable cross user without cross user permission"); |
| 4269 | } catch (SecurityException e) { |
| 4270 | // good |
| 4271 | } |
| 4272 | |
| 4273 | // cross user, with permission, no problem |
| 4274 | TestablePermissions perms = mContext.getTestablePermissions(); |
| 4275 | perms.setPermission(android.Manifest.permission.INTERACT_ACROSS_USERS, PERMISSION_GRANTED); |
| 4276 | mBinderService.getNotificationChannels("src", "target", mContext.getUserId() + 1); |
| 4277 | } |
| 4278 | |
| 4279 | @Test |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 4280 | public void setDefaultAssistantForUser_fromConfigXml() { |
| 4281 | clearDeviceConfig(); |
| 4282 | ComponentName xmlConfig = new ComponentName("config", "xml"); |
| 4283 | when(mResources |
| 4284 | .getString( |
| 4285 | com.android.internal.R.string.config_defaultAssistantAccessComponent)) |
| 4286 | .thenReturn(xmlConfig.flattenToString()); |
| 4287 | when(mContext.getResources()).thenReturn(mResources); |
| 4288 | when(mAssistants.queryPackageForServices(eq(null), anyInt(), eq(0))) |
| 4289 | .thenReturn(Collections.singleton(xmlConfig)); |
| 4290 | mService.setNotificationAssistantAccessGrantedCallback( |
| 4291 | mNotificationAssistantAccessGrantedCallback); |
| 4292 | |
| 4293 | mService.setDefaultAssistantForUser(0); |
| 4294 | |
| 4295 | verify(mNotificationAssistantAccessGrantedCallback) |
| 4296 | .onGranted(eq(xmlConfig), eq(0), eq(true)); |
| 4297 | } |
| 4298 | |
| 4299 | @Test |
| 4300 | public void setDefaultAssistantForUser_fromDeviceConfig() { |
| 4301 | ComponentName xmlConfig = new ComponentName("xml", "config"); |
| 4302 | ComponentName deviceConfig = new ComponentName("device", "config"); |
| 4303 | setDefaultAssistantInDeviceConfig(deviceConfig.flattenToString()); |
| 4304 | when(mResources |
| 4305 | .getString(com.android.internal.R.string.config_defaultAssistantAccessComponent)) |
| 4306 | .thenReturn(xmlConfig.flattenToString()); |
| 4307 | when(mContext.getResources()).thenReturn(mResources); |
| 4308 | when(mAssistants.queryPackageForServices(eq(null), anyInt(), eq(0))) |
| 4309 | .thenReturn(new ArraySet<>(Arrays.asList(xmlConfig, deviceConfig))); |
| 4310 | mService.setNotificationAssistantAccessGrantedCallback( |
| 4311 | mNotificationAssistantAccessGrantedCallback); |
| 4312 | |
| 4313 | mService.setDefaultAssistantForUser(0); |
| 4314 | |
| 4315 | verify(mNotificationAssistantAccessGrantedCallback) |
| 4316 | .onGranted(eq(deviceConfig), eq(0), eq(true)); |
| 4317 | } |
| 4318 | |
| 4319 | @Test |
| 4320 | public void setDefaultAssistantForUser_deviceConfigInvalid() { |
| 4321 | ComponentName xmlConfig = new ComponentName("xml", "config"); |
| 4322 | ComponentName deviceConfig = new ComponentName("device", "config"); |
| 4323 | setDefaultAssistantInDeviceConfig(deviceConfig.flattenToString()); |
| 4324 | when(mResources |
| 4325 | .getString(com.android.internal.R.string.config_defaultAssistantAccessComponent)) |
| 4326 | .thenReturn(xmlConfig.flattenToString()); |
| 4327 | when(mContext.getResources()).thenReturn(mResources); |
| 4328 | // Only xmlConfig is valid, deviceConfig is not. |
| 4329 | when(mAssistants.queryPackageForServices(eq(null), anyInt(), eq(0))) |
| 4330 | .thenReturn(Collections.singleton(xmlConfig)); |
| 4331 | mService.setNotificationAssistantAccessGrantedCallback( |
| 4332 | mNotificationAssistantAccessGrantedCallback); |
| 4333 | |
| 4334 | mService.setDefaultAssistantForUser(0); |
| 4335 | |
| 4336 | verify(mNotificationAssistantAccessGrantedCallback) |
| 4337 | .onGranted(eq(xmlConfig), eq(0), eq(true)); |
| 4338 | } |
| 4339 | |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 4340 | @Test |
Mady Mellor | 65dcaa9 | 2019-04-03 12:21:44 -0700 | [diff] [blame] | 4341 | public void testFlagBubble() throws RemoteException { |
| 4342 | // Bubbles are allowed! |
Mady Mellor | c682034 | 2019-05-20 12:04:36 -0700 | [diff] [blame] | 4343 | setUpPrefsForBubbles(true /* global */, true /* app */, true /* channel */); |
Mady Mellor | 65dcaa9 | 2019-04-03 12:21:44 -0700 | [diff] [blame] | 4344 | |
| 4345 | // Notif with bubble metadata but not our other misc requirements |
| 4346 | NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel, |
| 4347 | null /* tvExtender */, true /* isBubble */); |
| 4348 | |
| 4349 | // Say we're foreground |
| 4350 | when(mActivityManager.getPackageImportance(nr.sbn.getPackageName())).thenReturn( |
| 4351 | IMPORTANCE_FOREGROUND); |
| 4352 | |
| 4353 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
| 4354 | nr.sbn.getId(), nr.sbn.getNotification(), nr.sbn.getUserId()); |
| 4355 | waitForIdle(); |
| 4356 | |
| 4357 | StatusBarNotification[] notifs = mBinderService.getActiveNotifications(PKG); |
| 4358 | assertEquals(1, notifs.length); |
| 4359 | assertTrue((notifs[0].getNotification().flags & FLAG_BUBBLE) != 0); |
| 4360 | assertTrue(mService.getNotificationRecord( |
| 4361 | nr.sbn.getKey()).getNotification().isBubbleNotification()); |
| 4362 | } |
| 4363 | |
| 4364 | @Test |
Mady Mellor | c682034 | 2019-05-20 12:04:36 -0700 | [diff] [blame] | 4365 | public void testFlagBubble_noFlag_appNotAllowed() throws RemoteException { |
| 4366 | // Bubbles are allowed! |
| 4367 | setUpPrefsForBubbles(true /* global */, false /* app */, true /* channel */); |
| 4368 | |
| 4369 | // Notif with bubble metadata but not our other misc requirements |
| 4370 | NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel, |
| 4371 | null /* tvExtender */, true /* isBubble */); |
| 4372 | |
| 4373 | // Say we're foreground |
| 4374 | when(mActivityManager.getPackageImportance(nr.sbn.getPackageName())).thenReturn( |
| 4375 | IMPORTANCE_FOREGROUND); |
| 4376 | |
| 4377 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
| 4378 | nr.sbn.getId(), nr.sbn.getNotification(), nr.sbn.getUserId()); |
| 4379 | waitForIdle(); |
| 4380 | |
| 4381 | StatusBarNotification[] notifs = mBinderService.getActiveNotifications(PKG); |
| 4382 | assertEquals(1, notifs.length); |
| 4383 | assertEquals((notifs[0].getNotification().flags & FLAG_BUBBLE), 0); |
| 4384 | assertFalse(mService.getNotificationRecord( |
| 4385 | nr.sbn.getKey()).getNotification().isBubbleNotification()); |
| 4386 | } |
| 4387 | |
| 4388 | @Test |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4389 | public void testFlagBubbleNotifs_flag_appForeground() throws RemoteException { |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 4390 | // Bubbles are allowed! |
Mady Mellor | c682034 | 2019-05-20 12:04:36 -0700 | [diff] [blame] | 4391 | setUpPrefsForBubbles(true /* global */, true /* app */, true /* channel */); |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 4392 | |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4393 | // Notif with bubble metadata but not our other misc requirements |
| 4394 | NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel, |
| 4395 | null /* tvExtender */, true /* isBubble */); |
| 4396 | |
| 4397 | // Say we're foreground |
| 4398 | when(mActivityManager.getPackageImportance(nr.sbn.getPackageName())).thenReturn( |
| 4399 | IMPORTANCE_FOREGROUND); |
| 4400 | |
| 4401 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
| 4402 | nr.sbn.getId(), nr.sbn.getNotification(), nr.sbn.getUserId()); |
| 4403 | waitForIdle(); |
| 4404 | |
| 4405 | // yes allowed, yes foreground, yes bubble |
| 4406 | assertTrue(mService.getNotificationRecord( |
| 4407 | nr.sbn.getKey()).getNotification().isBubbleNotification()); |
| 4408 | } |
| 4409 | |
| 4410 | @Test |
| 4411 | public void testFlagBubbleNotifs_noFlag_appNotForeground() throws RemoteException { |
| 4412 | // Bubbles are allowed! |
Mady Mellor | c682034 | 2019-05-20 12:04:36 -0700 | [diff] [blame] | 4413 | setUpPrefsForBubbles(true /* global */, true /* app */, true /* channel */); |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4414 | |
| 4415 | // Notif with bubble metadata but not our other misc requirements |
| 4416 | NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel, |
| 4417 | null /* tvExtender */, true /* isBubble */); |
| 4418 | |
| 4419 | // Make sure we're NOT foreground |
| 4420 | when(mActivityManager.getPackageImportance(nr.sbn.getPackageName())).thenReturn( |
| 4421 | IMPORTANCE_VISIBLE); |
| 4422 | |
| 4423 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
| 4424 | nr.sbn.getId(), nr.sbn.getNotification(), nr.sbn.getUserId()); |
| 4425 | waitForIdle(); |
| 4426 | |
| 4427 | // yes allowed but NOT foreground, no bubble |
| 4428 | assertFalse(mService.getNotificationRecord( |
| 4429 | nr.sbn.getKey()).getNotification().isBubbleNotification()); |
| 4430 | } |
| 4431 | |
| 4432 | @Test |
| 4433 | public void testFlagBubbleNotifs_flag_previousForegroundFlag() throws RemoteException { |
| 4434 | // Bubbles are allowed! |
Mady Mellor | c682034 | 2019-05-20 12:04:36 -0700 | [diff] [blame] | 4435 | setUpPrefsForBubbles(true /* global */, true /* app */, true /* channel */); |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4436 | |
| 4437 | // Notif with bubble metadata but not our other misc requirements |
| 4438 | NotificationRecord nr1 = generateNotificationRecord(mTestNotificationChannel, |
| 4439 | null /* tvExtender */, true /* isBubble */); |
| 4440 | |
| 4441 | // Send notif when we're foreground |
| 4442 | when(mActivityManager.getPackageImportance(nr1.sbn.getPackageName())).thenReturn( |
| 4443 | IMPORTANCE_FOREGROUND); |
| 4444 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
| 4445 | nr1.sbn.getId(), nr1.sbn.getNotification(), nr1.sbn.getUserId()); |
| 4446 | waitForIdle(); |
| 4447 | |
| 4448 | // yes allowed, yes foreground, yes bubble |
| 4449 | assertTrue(mService.getNotificationRecord( |
| 4450 | nr1.sbn.getKey()).getNotification().isBubbleNotification()); |
| 4451 | |
| 4452 | // Send a new update when we're not foreground |
| 4453 | NotificationRecord nr2 = generateNotificationRecord(mTestNotificationChannel, |
| 4454 | null /* tvExtender */, true /* isBubble */); |
| 4455 | |
| 4456 | when(mActivityManager.getPackageImportance(nr2.sbn.getPackageName())).thenReturn( |
| 4457 | IMPORTANCE_VISIBLE); |
| 4458 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
| 4459 | nr2.sbn.getId(), nr2.sbn.getNotification(), nr2.sbn.getUserId()); |
| 4460 | waitForIdle(); |
| 4461 | |
| 4462 | // yes allowed, previously foreground / flagged, yes bubble |
| 4463 | assertTrue(mService.getNotificationRecord( |
| 4464 | nr2.sbn.getKey()).getNotification().isBubbleNotification()); |
| 4465 | |
| 4466 | StatusBarNotification[] notifs2 = mBinderService.getActiveNotifications(PKG); |
| 4467 | assertEquals(1, notifs2.length); |
| 4468 | assertEquals(1, mService.getNotificationRecordCount()); |
| 4469 | } |
| 4470 | |
| 4471 | @Test |
| 4472 | public void testFlagBubbleNotifs_noFlag_previousForegroundFlag_afterRemoval() |
| 4473 | throws RemoteException { |
| 4474 | // Bubbles are allowed! |
Mady Mellor | c682034 | 2019-05-20 12:04:36 -0700 | [diff] [blame] | 4475 | setUpPrefsForBubbles(true /* global */, true /* app */, true /* channel */); |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4476 | |
| 4477 | // Notif with bubble metadata but not our other misc requirements |
| 4478 | NotificationRecord nr1 = generateNotificationRecord(mTestNotificationChannel, |
| 4479 | null /* tvExtender */, true /* isBubble */); |
| 4480 | |
| 4481 | // Send notif when we're foreground |
| 4482 | when(mActivityManager.getPackageImportance(nr1.sbn.getPackageName())).thenReturn( |
| 4483 | IMPORTANCE_FOREGROUND); |
| 4484 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
| 4485 | nr1.sbn.getId(), nr1.sbn.getNotification(), nr1.sbn.getUserId()); |
| 4486 | waitForIdle(); |
| 4487 | |
| 4488 | // yes allowed, yes foreground, yes bubble |
| 4489 | assertTrue(mService.getNotificationRecord( |
| 4490 | nr1.sbn.getKey()).getNotification().isBubbleNotification()); |
| 4491 | |
| 4492 | // Remove the bubble |
| 4493 | mBinderService.cancelNotificationWithTag(PKG, "tag", nr1.sbn.getId(), |
| 4494 | nr1.sbn.getUserId()); |
| 4495 | waitForIdle(); |
| 4496 | |
| 4497 | StatusBarNotification[] notifs = mBinderService.getActiveNotifications(PKG); |
| 4498 | assertEquals(0, notifs.length); |
| 4499 | assertEquals(0, mService.getNotificationRecordCount()); |
| 4500 | |
| 4501 | // Send a new update when we're not foreground |
| 4502 | NotificationRecord nr2 = generateNotificationRecord(mTestNotificationChannel, |
| 4503 | null /* tvExtender */, true /* isBubble */); |
| 4504 | |
| 4505 | when(mActivityManager.getPackageImportance(nr2.sbn.getPackageName())).thenReturn( |
| 4506 | IMPORTANCE_VISIBLE); |
| 4507 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
| 4508 | nr2.sbn.getId(), nr2.sbn.getNotification(), nr2.sbn.getUserId()); |
| 4509 | waitForIdle(); |
| 4510 | |
| 4511 | // yes allowed, but was removed & no foreground, so no bubble |
| 4512 | assertFalse(mService.getNotificationRecord( |
| 4513 | nr2.sbn.getKey()).getNotification().isBubbleNotification()); |
| 4514 | |
| 4515 | StatusBarNotification[] notifs2 = mBinderService.getActiveNotifications(PKG); |
| 4516 | assertEquals(1, notifs2.length); |
| 4517 | assertEquals(1, mService.getNotificationRecordCount()); |
| 4518 | } |
| 4519 | |
| 4520 | @Test |
| 4521 | public void testFlagBubbleNotifs_flag_messaging() throws RemoteException { |
| 4522 | // Bubbles are allowed! |
Mady Mellor | c682034 | 2019-05-20 12:04:36 -0700 | [diff] [blame] | 4523 | setUpPrefsForBubbles(true /* global */, true /* app */, true /* channel */); |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4524 | |
| 4525 | // Give it bubble metadata |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 4526 | Notification.BubbleMetadata data = getBasicBubbleMetadataBuilder().build(); |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4527 | // Give it a person |
| 4528 | Person person = new Person.Builder() |
| 4529 | .setName("bubblebot") |
| 4530 | .build(); |
Mady Mellor | a10448e | 2019-04-26 13:50:58 -0700 | [diff] [blame] | 4531 | // It needs remote input to be bubble-able |
| 4532 | RemoteInput remoteInput = new RemoteInput.Builder("reply_key").setLabel("reply").build(); |
| 4533 | PendingIntent inputIntent = PendingIntent.getActivity(mContext, 0, new Intent(), 0); |
| 4534 | Icon icon = Icon.createWithResource(mContext, android.R.drawable.sym_def_app_icon); |
| 4535 | Notification.Action replyAction = new Notification.Action.Builder(icon, "Reply", |
| 4536 | inputIntent).addRemoteInput(remoteInput) |
| 4537 | .build(); |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4538 | // Make it messaging style |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 4539 | Notification.Builder nb = new Notification.Builder(mContext, |
| 4540 | mTestNotificationChannel.getId()) |
| 4541 | .setContentTitle("foo") |
| 4542 | .setBubbleMetadata(data) |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4543 | .setStyle(new Notification.MessagingStyle(person) |
| 4544 | .setConversationTitle("Bubble Chat") |
| 4545 | .addMessage("Hello?", |
| 4546 | SystemClock.currentThreadTimeMillis() - 300000, person) |
| 4547 | .addMessage("Is it me you're looking for?", |
| 4548 | SystemClock.currentThreadTimeMillis(), person) |
| 4549 | ) |
Mady Mellor | a10448e | 2019-04-26 13:50:58 -0700 | [diff] [blame] | 4550 | .setActions(replyAction) |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4551 | .setSmallIcon(android.R.drawable.sym_def_app_icon); |
| 4552 | |
| 4553 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, null, mUid, 0, |
| 4554 | nb.build(), new UserHandle(mUid), null, 0); |
| 4555 | NotificationRecord nr = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 4556 | |
| 4557 | mBinderService.enqueueNotificationWithTag(PKG, PKG, null, |
| 4558 | nr.sbn.getId(), nr.sbn.getNotification(), nr.sbn.getUserId()); |
| 4559 | waitForIdle(); |
| 4560 | |
| 4561 | // yes allowed, yes messaging, yes bubble |
| 4562 | assertTrue(mService.getNotificationRecord( |
| 4563 | sbn.getKey()).getNotification().isBubbleNotification()); |
| 4564 | } |
| 4565 | |
| 4566 | @Test |
| 4567 | public void testFlagBubbleNotifs_flag_phonecall() throws RemoteException { |
| 4568 | // Bubbles are allowed! |
Mady Mellor | c682034 | 2019-05-20 12:04:36 -0700 | [diff] [blame] | 4569 | setUpPrefsForBubbles(true /* global */, true /* app */, true /* channel */); |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4570 | |
| 4571 | // Give it bubble metadata |
| 4572 | Notification.BubbleMetadata data = getBasicBubbleMetadataBuilder().build(); |
| 4573 | // Give it a person |
| 4574 | Person person = new Person.Builder() |
| 4575 | .setName("bubblebot") |
| 4576 | .build(); |
| 4577 | // Make it a phone call |
| 4578 | Notification.Builder nb = new Notification.Builder(mContext, |
| 4579 | mTestNotificationChannel.getId()) |
| 4580 | .setCategory(CATEGORY_CALL) |
| 4581 | .addPerson(person) |
| 4582 | .setContentTitle("foo") |
| 4583 | .setBubbleMetadata(data) |
| 4584 | .setSmallIcon(android.R.drawable.sym_def_app_icon); |
| 4585 | |
| 4586 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, null, mUid, 0, |
| 4587 | nb.build(), new UserHandle(mUid), null, 0); |
| 4588 | // Make sure it has foreground service |
| 4589 | sbn.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
| 4590 | NotificationRecord nr = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 4591 | |
| 4592 | mBinderService.enqueueNotificationWithTag(PKG, PKG, null, |
| 4593 | nr.sbn.getId(), nr.sbn.getNotification(), nr.sbn.getUserId()); |
| 4594 | waitForIdle(); |
| 4595 | |
| 4596 | // yes phone call, yes person, yes foreground service, yes bubble |
| 4597 | assertTrue(mService.getNotificationRecord( |
| 4598 | sbn.getKey()).getNotification().isBubbleNotification()); |
| 4599 | } |
| 4600 | |
| 4601 | @Test |
| 4602 | public void testFlagBubbleNotifs_noFlag_phonecall_noForegroundService() throws RemoteException { |
| 4603 | // Bubbles are allowed! |
Mady Mellor | c682034 | 2019-05-20 12:04:36 -0700 | [diff] [blame] | 4604 | setUpPrefsForBubbles(true /* global */, true /* app */, true /* channel */); |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4605 | |
| 4606 | // Give it bubble metadata |
| 4607 | Notification.BubbleMetadata data = getBasicBubbleMetadataBuilder().build(); |
| 4608 | // Give it a person |
| 4609 | Person person = new Person.Builder() |
| 4610 | .setName("bubblebot") |
| 4611 | .build(); |
| 4612 | // Make it a phone call |
| 4613 | Notification.Builder nb = new Notification.Builder(mContext, |
| 4614 | mTestNotificationChannel.getId()) |
| 4615 | .setCategory(CATEGORY_CALL) |
| 4616 | .addPerson(person) |
| 4617 | .setContentTitle("foo") |
| 4618 | .setBubbleMetadata(data) |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 4619 | .setSmallIcon(android.R.drawable.sym_def_app_icon); |
| 4620 | |
| 4621 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, null, mUid, 0, |
| 4622 | nb.build(), new UserHandle(mUid), null, 0); |
| 4623 | NotificationRecord nr = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 4624 | |
| 4625 | mBinderService.enqueueNotificationWithTag(PKG, PKG, null, |
| 4626 | nr.sbn.getId(), nr.sbn.getNotification(), nr.sbn.getUserId()); |
| 4627 | waitForIdle(); |
| 4628 | |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4629 | // yes phone call, yes person, NO foreground service, no bubble |
| 4630 | assertFalse(mService.getNotificationRecord( |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 4631 | sbn.getKey()).getNotification().isBubbleNotification()); |
| 4632 | } |
| 4633 | |
| 4634 | @Test |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4635 | public void testFlagBubbleNotifs_noFlag_phonecall_noPerson() throws RemoteException { |
| 4636 | // Bubbles are allowed! |
Mady Mellor | c682034 | 2019-05-20 12:04:36 -0700 | [diff] [blame] | 4637 | setUpPrefsForBubbles(true /* global */, true /* app */, true /* channel */); |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4638 | |
| 4639 | // Give it bubble metadata |
| 4640 | Notification.BubbleMetadata data = getBasicBubbleMetadataBuilder().build(); |
| 4641 | // Make it a phone call |
| 4642 | Notification.Builder nb = new Notification.Builder(mContext, |
| 4643 | mTestNotificationChannel.getId()) |
| 4644 | .setCategory(CATEGORY_CALL) |
| 4645 | .setContentTitle("foo") |
| 4646 | .setBubbleMetadata(data) |
| 4647 | .setSmallIcon(android.R.drawable.sym_def_app_icon); |
| 4648 | |
| 4649 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, null, mUid, 0, |
| 4650 | nb.build(), new UserHandle(mUid), null, 0); |
| 4651 | // Make sure it has foreground service |
| 4652 | sbn.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
| 4653 | NotificationRecord nr = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 4654 | |
| 4655 | mBinderService.enqueueNotificationWithTag(PKG, PKG, null, |
| 4656 | nr.sbn.getId(), nr.sbn.getNotification(), nr.sbn.getUserId()); |
| 4657 | waitForIdle(); |
| 4658 | |
| 4659 | // yes phone call, yes foreground service, BUT NO person, no bubble |
| 4660 | assertFalse(mService.getNotificationRecord( |
| 4661 | sbn.getKey()).getNotification().isBubbleNotification()); |
| 4662 | } |
| 4663 | |
| 4664 | @Test |
| 4665 | public void testFlagBubbleNotifs_noFlag_phonecall_noCategory() throws RemoteException { |
| 4666 | // Bubbles are allowed! |
Mady Mellor | c682034 | 2019-05-20 12:04:36 -0700 | [diff] [blame] | 4667 | setUpPrefsForBubbles(true /* global */, true /* app */, true /* channel */); |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4668 | |
| 4669 | // Give it bubble metadata |
| 4670 | Notification.BubbleMetadata data = getBasicBubbleMetadataBuilder().build(); |
| 4671 | // Give it a person |
| 4672 | Person person = new Person.Builder() |
| 4673 | .setName("bubblebot") |
| 4674 | .build(); |
| 4675 | // No category |
| 4676 | Notification.Builder nb = new Notification.Builder(mContext, |
| 4677 | mTestNotificationChannel.getId()) |
| 4678 | .addPerson(person) |
| 4679 | .setContentTitle("foo") |
| 4680 | .setBubbleMetadata(data) |
| 4681 | .setSmallIcon(android.R.drawable.sym_def_app_icon); |
| 4682 | |
| 4683 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, null, mUid, 0, |
| 4684 | nb.build(), new UserHandle(mUid), null, 0); |
| 4685 | // Make sure it has foreground service |
| 4686 | sbn.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
| 4687 | NotificationRecord nr = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 4688 | |
| 4689 | mBinderService.enqueueNotificationWithTag(PKG, PKG, null, |
| 4690 | nr.sbn.getId(), nr.sbn.getNotification(), nr.sbn.getUserId()); |
| 4691 | waitForIdle(); |
| 4692 | |
| 4693 | // yes person, yes foreground service, BUT NO call, no bubble |
| 4694 | assertFalse(mService.getNotificationRecord( |
| 4695 | sbn.getKey()).getNotification().isBubbleNotification()); |
| 4696 | } |
| 4697 | |
| 4698 | @Test |
| 4699 | public void testFlagBubbleNotifs_noFlag_messaging_appNotAllowed() throws RemoteException { |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 4700 | // Bubbles are NOT allowed! |
Mady Mellor | c682034 | 2019-05-20 12:04:36 -0700 | [diff] [blame] | 4701 | setUpPrefsForBubbles(false /* global */, true /* app */, true /* channel */); |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 4702 | |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4703 | // Give it bubble metadata |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 4704 | Notification.BubbleMetadata data = getBasicBubbleMetadataBuilder().build(); |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4705 | // Give it a person |
| 4706 | Person person = new Person.Builder() |
| 4707 | .setName("bubblebot") |
| 4708 | .build(); |
| 4709 | // Make it messaging style |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 4710 | Notification.Builder nb = new Notification.Builder(mContext, |
| 4711 | mTestNotificationChannel.getId()) |
| 4712 | .setContentTitle("foo") |
| 4713 | .setBubbleMetadata(data) |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4714 | .setStyle(new Notification.MessagingStyle(person) |
| 4715 | .setConversationTitle("Bubble Chat") |
| 4716 | .addMessage("Hello?", |
| 4717 | SystemClock.currentThreadTimeMillis() - 300000, person) |
| 4718 | .addMessage("Is it me you're looking for?", |
| 4719 | SystemClock.currentThreadTimeMillis(), person) |
| 4720 | ) |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 4721 | .setSmallIcon(android.R.drawable.sym_def_app_icon); |
| 4722 | |
| 4723 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, null, mUid, 0, |
| 4724 | nb.build(), new UserHandle(mUid), null, 0); |
| 4725 | NotificationRecord nr = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 4726 | |
| 4727 | // Post the notification |
| 4728 | mBinderService.enqueueNotificationWithTag(PKG, PKG, null, |
| 4729 | nr.sbn.getId(), nr.sbn.getNotification(), nr.sbn.getUserId()); |
| 4730 | waitForIdle(); |
| 4731 | |
| 4732 | // not allowed, no bubble |
| 4733 | assertFalse(mService.getNotificationRecord( |
| 4734 | sbn.getKey()).getNotification().isBubbleNotification()); |
| 4735 | } |
| 4736 | |
| 4737 | @Test |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4738 | public void testFlagBubbleNotifs_noFlag_notBubble() throws RemoteException { |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 4739 | // Bubbles are allowed! |
Mady Mellor | c682034 | 2019-05-20 12:04:36 -0700 | [diff] [blame] | 4740 | setUpPrefsForBubbles(true /* global */, true /* app */, true /* channel */); |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 4741 | |
| 4742 | // Notif WITHOUT bubble metadata |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4743 | NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel); |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 4744 | |
| 4745 | // Post the notification |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4746 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 4747 | nr.sbn.getId(), nr.sbn.getNotification(), nr.sbn.getUserId()); |
| 4748 | waitForIdle(); |
| 4749 | |
| 4750 | // no bubble metadata, no bubble |
| 4751 | assertFalse(mService.getNotificationRecord( |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4752 | nr.sbn.getKey()).getNotification().isBubbleNotification()); |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 4753 | } |
| 4754 | |
| 4755 | @Test |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4756 | public void testFlagBubbleNotifs_noFlag_messaging_channelNotAllowed() throws RemoteException { |
Mady Mellor | c682034 | 2019-05-20 12:04:36 -0700 | [diff] [blame] | 4757 | // Bubbles are allowed except on this channel |
| 4758 | setUpPrefsForBubbles(true /* global */, true /* app */, false /* channel */); |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 4759 | |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4760 | // Give it bubble metadata |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 4761 | Notification.BubbleMetadata data = getBasicBubbleMetadataBuilder().build(); |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4762 | // Give it a person |
| 4763 | Person person = new Person.Builder() |
| 4764 | .setName("bubblebot") |
| 4765 | .build(); |
| 4766 | // Make it messaging style |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 4767 | Notification.Builder nb = new Notification.Builder(mContext, |
| 4768 | mTestNotificationChannel.getId()) |
| 4769 | .setContentTitle("foo") |
| 4770 | .setBubbleMetadata(data) |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4771 | .setStyle(new Notification.MessagingStyle(person) |
| 4772 | .setConversationTitle("Bubble Chat") |
| 4773 | .addMessage("Hello?", |
| 4774 | SystemClock.currentThreadTimeMillis() - 300000, person) |
| 4775 | .addMessage("Is it me you're looking for?", |
| 4776 | SystemClock.currentThreadTimeMillis(), person) |
| 4777 | ) |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 4778 | .setSmallIcon(android.R.drawable.sym_def_app_icon); |
| 4779 | |
| 4780 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, null, mUid, 0, |
| 4781 | nb.build(), new UserHandle(mUid), null, 0); |
| 4782 | NotificationRecord nr = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 4783 | |
| 4784 | // Post the notification |
| 4785 | mBinderService.enqueueNotificationWithTag(PKG, PKG, null, |
| 4786 | nr.sbn.getId(), nr.sbn.getNotification(), nr.sbn.getUserId()); |
| 4787 | waitForIdle(); |
| 4788 | |
| 4789 | // channel not allowed, no bubble |
| 4790 | assertFalse(mService.getNotificationRecord( |
| 4791 | sbn.getKey()).getNotification().isBubbleNotification()); |
| 4792 | } |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 4793 | |
Mady Mellor | 49b1bf1 | 2019-03-29 12:00:02 -0700 | [diff] [blame] | 4794 | @Test |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4795 | public void testFlagBubbleNotifs_noFlag_phonecall_notAllowed() throws RemoteException { |
Mady Mellor | c682034 | 2019-05-20 12:04:36 -0700 | [diff] [blame] | 4796 | // Bubbles are not allowed! |
| 4797 | setUpPrefsForBubbles(false /* global */, true /* app */, true /* channel */); |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4798 | |
| 4799 | // Give it bubble metadata |
| 4800 | Notification.BubbleMetadata data = getBasicBubbleMetadataBuilder().build(); |
| 4801 | // Give it a person |
| 4802 | Person person = new Person.Builder() |
| 4803 | .setName("bubblebot") |
| 4804 | .build(); |
| 4805 | // Make it a phone call |
| 4806 | Notification.Builder nb = new Notification.Builder(mContext, |
| 4807 | mTestNotificationChannel.getId()) |
| 4808 | .setCategory(CATEGORY_CALL) |
| 4809 | .addPerson(person) |
| 4810 | .setContentTitle("foo") |
| 4811 | .setBubbleMetadata(data) |
| 4812 | .setSmallIcon(android.R.drawable.sym_def_app_icon); |
| 4813 | |
| 4814 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, null, mUid, 0, |
| 4815 | nb.build(), new UserHandle(mUid), null, 0); |
| 4816 | // Make sure it has foreground service |
| 4817 | sbn.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
| 4818 | NotificationRecord nr = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 4819 | |
| 4820 | mBinderService.enqueueNotificationWithTag(PKG, PKG, null, |
| 4821 | nr.sbn.getId(), nr.sbn.getNotification(), nr.sbn.getUserId()); |
| 4822 | waitForIdle(); |
| 4823 | |
| 4824 | // yes phone call, yes person, yes foreground service, but not allowed, no bubble |
| 4825 | assertFalse(mService.getNotificationRecord( |
| 4826 | sbn.getKey()).getNotification().isBubbleNotification()); |
| 4827 | } |
| 4828 | |
| 4829 | @Test |
| 4830 | public void testFlagBubbleNotifs_noFlag_phonecall_channelNotAllowed() throws RemoteException { |
Mady Mellor | c682034 | 2019-05-20 12:04:36 -0700 | [diff] [blame] | 4831 | // Bubbles are allowed, but not on channel. |
| 4832 | setUpPrefsForBubbles(true /* global */, true /* app */, false /* channel */); |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4833 | |
| 4834 | // Give it bubble metadata |
| 4835 | Notification.BubbleMetadata data = getBasicBubbleMetadataBuilder().build(); |
| 4836 | // Give it a person |
| 4837 | Person person = new Person.Builder() |
| 4838 | .setName("bubblebot") |
| 4839 | .build(); |
| 4840 | // Make it a phone call |
| 4841 | Notification.Builder nb = new Notification.Builder(mContext, |
| 4842 | mTestNotificationChannel.getId()) |
| 4843 | .setCategory(CATEGORY_CALL) |
| 4844 | .addPerson(person) |
| 4845 | .setContentTitle("foo") |
| 4846 | .setBubbleMetadata(data) |
| 4847 | .setSmallIcon(android.R.drawable.sym_def_app_icon); |
| 4848 | |
| 4849 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, null, mUid, 0, |
| 4850 | nb.build(), new UserHandle(mUid), null, 0); |
| 4851 | // Make sure it has foreground service |
| 4852 | sbn.getNotification().flags |= FLAG_FOREGROUND_SERVICE; |
| 4853 | NotificationRecord nr = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 4854 | |
| 4855 | mBinderService.enqueueNotificationWithTag(PKG, PKG, null, |
| 4856 | nr.sbn.getId(), nr.sbn.getNotification(), nr.sbn.getUserId()); |
| 4857 | waitForIdle(); |
| 4858 | |
| 4859 | // yes phone call, yes person, yes foreground service, but channel not allowed, no bubble |
| 4860 | assertFalse(mService.getNotificationRecord( |
| 4861 | sbn.getKey()).getNotification().isBubbleNotification()); |
| 4862 | } |
| 4863 | |
| 4864 | @Test |
Mady Mellor | 49b1bf1 | 2019-03-29 12:00:02 -0700 | [diff] [blame] | 4865 | public void testCancelAllNotifications_cancelsBubble() throws Exception { |
| 4866 | final NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel); |
| 4867 | nr.sbn.getNotification().flags |= FLAG_BUBBLE; |
| 4868 | mService.addNotification(nr); |
| 4869 | |
| 4870 | mBinderService.cancelAllNotifications(PKG, nr.sbn.getUserId()); |
| 4871 | waitForIdle(); |
| 4872 | |
| 4873 | StatusBarNotification[] notifs = mBinderService.getActiveNotifications(PKG); |
| 4874 | assertEquals(0, notifs.length); |
| 4875 | assertEquals(0, mService.getNotificationRecordCount()); |
| 4876 | } |
| 4877 | |
| 4878 | @Test |
| 4879 | public void testAppCancelNotifications_cancelsBubbles() throws Exception { |
| 4880 | final NotificationRecord nrBubble = generateNotificationRecord(mTestNotificationChannel); |
| 4881 | nrBubble.sbn.getNotification().flags |= FLAG_BUBBLE; |
| 4882 | |
| 4883 | // Post the notification |
| 4884 | mBinderService.enqueueNotificationWithTag(PKG, PKG, null, |
| 4885 | nrBubble.sbn.getId(), nrBubble.sbn.getNotification(), nrBubble.sbn.getUserId()); |
| 4886 | waitForIdle(); |
| 4887 | |
| 4888 | StatusBarNotification[] notifs = mBinderService.getActiveNotifications(PKG); |
| 4889 | assertEquals(1, notifs.length); |
| 4890 | assertEquals(1, mService.getNotificationRecordCount()); |
| 4891 | |
| 4892 | mBinderService.cancelNotificationWithTag(PKG, null, nrBubble.sbn.getId(), |
| 4893 | nrBubble.sbn.getUserId()); |
| 4894 | waitForIdle(); |
| 4895 | |
| 4896 | StatusBarNotification[] notifs2 = mBinderService.getActiveNotifications(PKG); |
| 4897 | assertEquals(0, notifs2.length); |
| 4898 | assertEquals(0, mService.getNotificationRecordCount()); |
| 4899 | } |
| 4900 | |
| 4901 | @Test |
| 4902 | public void testCancelAllNotificationsFromListener_ignoresBubbles() throws Exception { |
| 4903 | final NotificationRecord nrNormal = generateNotificationRecord(mTestNotificationChannel); |
| 4904 | final NotificationRecord nrBubble = generateNotificationRecord(mTestNotificationChannel); |
| 4905 | nrBubble.sbn.getNotification().flags |= FLAG_BUBBLE; |
| 4906 | |
| 4907 | mService.addNotification(nrNormal); |
| 4908 | mService.addNotification(nrBubble); |
| 4909 | |
| 4910 | mService.getBinderService().cancelNotificationsFromListener(null, null); |
| 4911 | waitForIdle(); |
| 4912 | |
| 4913 | StatusBarNotification[] notifs = mBinderService.getActiveNotifications(PKG); |
| 4914 | assertEquals(1, notifs.length); |
| 4915 | assertEquals(1, mService.getNotificationRecordCount()); |
| 4916 | } |
| 4917 | |
| 4918 | @Test |
| 4919 | public void testCancelNotificationsFromListener_ignoresBubbles() throws Exception { |
| 4920 | final NotificationRecord nrNormal = generateNotificationRecord(mTestNotificationChannel); |
| 4921 | final NotificationRecord nrBubble = generateNotificationRecord(mTestNotificationChannel); |
| 4922 | nrBubble.sbn.getNotification().flags |= FLAG_BUBBLE; |
| 4923 | |
| 4924 | mService.addNotification(nrNormal); |
| 4925 | mService.addNotification(nrBubble); |
| 4926 | |
| 4927 | String[] keys = {nrNormal.sbn.getKey(), nrBubble.sbn.getKey()}; |
| 4928 | mService.getBinderService().cancelNotificationsFromListener(null, keys); |
| 4929 | waitForIdle(); |
| 4930 | |
| 4931 | StatusBarNotification[] notifs = mBinderService.getActiveNotifications(PKG); |
| 4932 | assertEquals(1, notifs.length); |
| 4933 | assertEquals(1, mService.getNotificationRecordCount()); |
| 4934 | } |
Julia Reynolds | ad6dd35 | 2019-03-07 16:46:22 -0500 | [diff] [blame] | 4935 | |
Julia Reynolds | 4214da9 | 2019-04-10 15:04:06 -0400 | [diff] [blame] | 4936 | @Test |
Julia Reynolds | 088c448 | 2019-04-10 12:43:27 -0400 | [diff] [blame] | 4937 | public void testGetAllowedAssistantAdjustments() throws Exception { |
| 4938 | List<String> capabilities = mBinderService.getAllowedAssistantAdjustments(null); |
Julia Reynolds | ad6dd35 | 2019-03-07 16:46:22 -0500 | [diff] [blame] | 4939 | assertNotNull(capabilities); |
| 4940 | |
| 4941 | for (int i = capabilities.size() - 1; i >= 0; i--) { |
| 4942 | String capability = capabilities.get(i); |
Julia Reynolds | 088c448 | 2019-04-10 12:43:27 -0400 | [diff] [blame] | 4943 | mBinderService.disallowAssistantAdjustment(capability); |
| 4944 | assertEquals(i + 1, mBinderService.getAllowedAssistantAdjustments(null).size()); |
| 4945 | List<String> currentCapabilities = mBinderService.getAllowedAssistantAdjustments(null); |
Julia Reynolds | ad6dd35 | 2019-03-07 16:46:22 -0500 | [diff] [blame] | 4946 | assertNotNull(currentCapabilities); |
| 4947 | assertFalse(currentCapabilities.contains(capability)); |
| 4948 | } |
| 4949 | } |
Julia Reynolds | c7dcdc2 | 2019-03-25 10:26:14 -0400 | [diff] [blame] | 4950 | |
Julia Reynolds | 4214da9 | 2019-04-10 15:04:06 -0400 | [diff] [blame] | 4951 | @Test |
Julia Reynolds | c7dcdc2 | 2019-03-25 10:26:14 -0400 | [diff] [blame] | 4952 | public void testAdjustRestrictedKey() throws Exception { |
| 4953 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
Julia Reynolds | 4214da9 | 2019-04-10 15:04:06 -0400 | [diff] [blame] | 4954 | mService.addNotification(r); |
| 4955 | when(mAssistants.isSameUser(any(), anyInt())).thenReturn(true); |
Julia Reynolds | c7dcdc2 | 2019-03-25 10:26:14 -0400 | [diff] [blame] | 4956 | |
| 4957 | when(mAssistants.isAdjustmentAllowed(KEY_IMPORTANCE)).thenReturn(true); |
| 4958 | when(mAssistants.isAdjustmentAllowed(KEY_USER_SENTIMENT)).thenReturn(false); |
| 4959 | |
| 4960 | Bundle signals = new Bundle(); |
| 4961 | signals.putInt(KEY_IMPORTANCE, IMPORTANCE_LOW); |
| 4962 | signals.putInt(KEY_USER_SENTIMENT, USER_SENTIMENT_NEGATIVE); |
| 4963 | Adjustment adjustment = new Adjustment(r.sbn.getPackageName(), r.getKey(), signals, |
| 4964 | "", r.getUser().getIdentifier()); |
| 4965 | |
| 4966 | mBinderService.applyAdjustmentFromAssistant(null, adjustment); |
| 4967 | r.applyAdjustments(); |
| 4968 | |
| 4969 | assertEquals(IMPORTANCE_LOW, r.getAssistantImportance()); |
| 4970 | assertEquals(USER_SENTIMENT_NEUTRAL, r.getUserSentiment()); |
| 4971 | } |
Julia Reynolds | 657d164 | 2019-03-27 12:15:57 -0400 | [diff] [blame] | 4972 | |
Julia Reynolds | 4214da9 | 2019-04-10 15:04:06 -0400 | [diff] [blame] | 4973 | @Test |
Julia Reynolds | a94365d | 2019-04-09 10:48:43 -0400 | [diff] [blame] | 4974 | public void testAutomaticZenRuleValidation_policyFilterAgreement() throws Exception { |
Julia Reynolds | 4214da9 | 2019-04-10 15:04:06 -0400 | [diff] [blame] | 4975 | when(mConditionProviders.isPackageOrComponentAllowed(anyString(), anyInt())) |
| 4976 | .thenReturn(true); |
| 4977 | mService.setZenHelper(mock(ZenModeHelper.class)); |
| 4978 | ComponentName owner = new ComponentName(mContext, this.getClass()); |
Julia Reynolds | a94365d | 2019-04-09 10:48:43 -0400 | [diff] [blame] | 4979 | ZenPolicy zenPolicy = new ZenPolicy.Builder().allowAlarms(true).build(); |
| 4980 | boolean isEnabled = true; |
| 4981 | AutomaticZenRule rule = new AutomaticZenRule("test", owner, owner, mock(Uri.class), |
| 4982 | zenPolicy, NotificationManager.INTERRUPTION_FILTER_NONE, isEnabled); |
| 4983 | |
| 4984 | try { |
| 4985 | mBinderService.addAutomaticZenRule(rule); |
Julia Reynolds | 4214da9 | 2019-04-10 15:04:06 -0400 | [diff] [blame] | 4986 | fail("Zen policy only applies to priority only mode"); |
Julia Reynolds | a94365d | 2019-04-09 10:48:43 -0400 | [diff] [blame] | 4987 | } catch (IllegalArgumentException e) { |
| 4988 | // yay |
| 4989 | } |
| 4990 | |
| 4991 | rule = new AutomaticZenRule("test", owner, owner, mock(Uri.class), |
| 4992 | zenPolicy, NotificationManager.INTERRUPTION_FILTER_PRIORITY, isEnabled); |
| 4993 | mBinderService.addAutomaticZenRule(rule); |
| 4994 | |
| 4995 | rule = new AutomaticZenRule("test", owner, owner, mock(Uri.class), |
| 4996 | null, NotificationManager.INTERRUPTION_FILTER_NONE, isEnabled); |
| 4997 | mBinderService.addAutomaticZenRule(rule); |
| 4998 | } |
| 4999 | |
Julia Reynolds | 4214da9 | 2019-04-10 15:04:06 -0400 | [diff] [blame] | 5000 | @Test |
Julia Reynolds | 657d164 | 2019-03-27 12:15:57 -0400 | [diff] [blame] | 5001 | public void testAreNotificationsEnabledForPackage_crossUser() throws Exception { |
| 5002 | try { |
| 5003 | mBinderService.areNotificationsEnabledForPackage(mContext.getPackageName(), |
| 5004 | mUid + UserHandle.PER_USER_RANGE); |
| 5005 | fail("Cannot call cross user without permission"); |
| 5006 | } catch (SecurityException e) { |
| 5007 | // pass |
| 5008 | } |
| 5009 | |
| 5010 | // cross user, with permission, no problem |
| 5011 | TestablePermissions perms = mContext.getTestablePermissions(); |
| 5012 | perms.setPermission(android.Manifest.permission.INTERACT_ACROSS_USERS, PERMISSION_GRANTED); |
| 5013 | mBinderService.areNotificationsEnabledForPackage(mContext.getPackageName(), |
| 5014 | mUid + UserHandle.PER_USER_RANGE); |
| 5015 | } |
Julia Reynolds | 2f7592d | 2019-03-27 12:17:23 -0400 | [diff] [blame] | 5016 | |
Julia Reynolds | 4214da9 | 2019-04-10 15:04:06 -0400 | [diff] [blame] | 5017 | @Test |
Julia Reynolds | 2f7592d | 2019-03-27 12:17:23 -0400 | [diff] [blame] | 5018 | public void testAreBubblesAllowedForPackage_crossUser() throws Exception { |
| 5019 | try { |
| 5020 | mBinderService.areBubblesAllowedForPackage(mContext.getPackageName(), |
| 5021 | mUid + UserHandle.PER_USER_RANGE); |
| 5022 | fail("Cannot call cross user without permission"); |
| 5023 | } catch (SecurityException e) { |
| 5024 | // pass |
| 5025 | } |
| 5026 | |
| 5027 | // cross user, with permission, no problem |
| 5028 | TestablePermissions perms = mContext.getTestablePermissions(); |
| 5029 | perms.setPermission(android.Manifest.permission.INTERACT_ACROSS_USERS, PERMISSION_GRANTED); |
| 5030 | mBinderService.areBubblesAllowedForPackage(mContext.getPackageName(), |
| 5031 | mUid + UserHandle.PER_USER_RANGE); |
| 5032 | } |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5033 | |
| 5034 | @Test |
| 5035 | public void testNotificationBubbleChanged_false() throws Exception { |
| 5036 | // Bubbles are allowed! |
Mady Mellor | c682034 | 2019-05-20 12:04:36 -0700 | [diff] [blame] | 5037 | setUpPrefsForBubbles(true /* global */, true /* app */, true /* channel */); |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5038 | |
| 5039 | // Notif with bubble metadata but not our other misc requirements |
| 5040 | NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel, |
| 5041 | null /* tvExtender */, true /* isBubble */); |
| 5042 | |
| 5043 | // Say we're foreground |
| 5044 | when(mActivityManager.getPackageImportance(nr.sbn.getPackageName())).thenReturn( |
| 5045 | IMPORTANCE_FOREGROUND); |
| 5046 | |
| 5047 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
| 5048 | nr.sbn.getId(), nr.sbn.getNotification(), nr.sbn.getUserId()); |
| 5049 | waitForIdle(); |
| 5050 | |
Mady Mellor | 66efd5e | 2019-05-15 13:38:11 -0700 | [diff] [blame] | 5051 | // Reset as this is called when the notif is first sent |
| 5052 | reset(mListeners); |
| 5053 | |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5054 | // First we were a bubble |
| 5055 | StatusBarNotification[] notifsBefore = mBinderService.getActiveNotifications(PKG); |
| 5056 | assertEquals(1, notifsBefore.length); |
| 5057 | assertTrue((notifsBefore[0].getNotification().flags & FLAG_BUBBLE) != 0); |
| 5058 | |
| 5059 | // Notify we're not a bubble |
| 5060 | mService.mNotificationDelegate.onNotificationBubbleChanged(nr.getKey(), false); |
| 5061 | waitForIdle(); |
| 5062 | |
Mady Mellor | 3a0a1b4 | 2019-05-23 06:40:21 -0700 | [diff] [blame] | 5063 | // Make sure we are not a bubble |
| 5064 | StatusBarNotification[] notifsAfter = mBinderService.getActiveNotifications(PKG); |
| 5065 | assertEquals(1, notifsAfter.length); |
| 5066 | assertEquals((notifsAfter[0].getNotification().flags & FLAG_BUBBLE), 0); |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5067 | } |
| 5068 | |
| 5069 | @Test |
| 5070 | public void testNotificationBubbleChanged_true() throws Exception { |
| 5071 | // Bubbles are allowed! |
Mady Mellor | c682034 | 2019-05-20 12:04:36 -0700 | [diff] [blame] | 5072 | setUpPrefsForBubbles(true /* global */, true /* app */, true /* channel */); |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5073 | |
| 5074 | // Plain notification that has bubble metadata |
| 5075 | NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel, |
| 5076 | null /* tvExtender */, true /* isBubble */); |
| 5077 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
| 5078 | nr.sbn.getId(), nr.sbn.getNotification(), nr.sbn.getUserId()); |
| 5079 | waitForIdle(); |
| 5080 | |
| 5081 | // Would be a normal notification because wouldn't have met requirements to bubble |
| 5082 | StatusBarNotification[] notifsBefore = mBinderService.getActiveNotifications(PKG); |
| 5083 | assertEquals(1, notifsBefore.length); |
| 5084 | assertEquals((notifsBefore[0].getNotification().flags & FLAG_BUBBLE), 0); |
| 5085 | |
| 5086 | // Make the package foreground so that we're allowed to be a bubble |
| 5087 | when(mActivityManager.getPackageImportance(nr.sbn.getPackageName())).thenReturn( |
| 5088 | IMPORTANCE_FOREGROUND); |
| 5089 | |
Mady Mellor | 66efd5e | 2019-05-15 13:38:11 -0700 | [diff] [blame] | 5090 | // Reset as this is called when the notif is first sent |
| 5091 | reset(mListeners); |
| 5092 | |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5093 | // Notify we are now a bubble |
| 5094 | mService.mNotificationDelegate.onNotificationBubbleChanged(nr.getKey(), true); |
| 5095 | waitForIdle(); |
| 5096 | |
Mady Mellor | 3a0a1b4 | 2019-05-23 06:40:21 -0700 | [diff] [blame] | 5097 | // Make sure we are a bubble |
| 5098 | StatusBarNotification[] notifsAfter = mBinderService.getActiveNotifications(PKG); |
| 5099 | assertEquals(1, notifsAfter.length); |
| 5100 | assertTrue((notifsAfter[0].getNotification().flags & FLAG_BUBBLE) != 0); |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5101 | } |
| 5102 | |
| 5103 | @Test |
| 5104 | public void testNotificationBubbleChanged_true_notAllowed() throws Exception { |
| 5105 | // Bubbles are allowed! |
Mady Mellor | c682034 | 2019-05-20 12:04:36 -0700 | [diff] [blame] | 5106 | setUpPrefsForBubbles(true /* global */, true /* app */, true /* channel */); |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5107 | |
| 5108 | // Notif that is not a bubble |
| 5109 | NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel, |
| 5110 | null /* tvExtender */, true /* isBubble */); |
| 5111 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
| 5112 | nr.sbn.getId(), nr.sbn.getNotification(), nr.sbn.getUserId()); |
| 5113 | waitForIdle(); |
| 5114 | |
Mady Mellor | 66efd5e | 2019-05-15 13:38:11 -0700 | [diff] [blame] | 5115 | // Reset as this is called when the notif is first sent |
| 5116 | reset(mListeners); |
| 5117 | |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5118 | // Would be a normal notification because wouldn't have met requirements to bubble |
| 5119 | StatusBarNotification[] notifsBefore = mBinderService.getActiveNotifications(PKG); |
| 5120 | assertEquals(1, notifsBefore.length); |
| 5121 | assertEquals((notifsBefore[0].getNotification().flags & FLAG_BUBBLE), 0); |
| 5122 | |
| 5123 | // Notify we are now a bubble |
| 5124 | mService.mNotificationDelegate.onNotificationBubbleChanged(nr.getKey(), true); |
| 5125 | waitForIdle(); |
| 5126 | |
| 5127 | // We still wouldn't be a bubble because the notification didn't meet requirements |
| 5128 | StatusBarNotification[] notifsAfter = mBinderService.getActiveNotifications(PKG); |
| 5129 | assertEquals(1, notifsAfter.length); |
| 5130 | assertEquals((notifsAfter[0].getNotification().flags & FLAG_BUBBLE), 0); |
| 5131 | } |
Mady Mellor | 5c11a2e | 2019-04-25 17:26:15 -0700 | [diff] [blame] | 5132 | |
| 5133 | @Test |
| 5134 | public void testNotificationBubbles_disabled_lowRamDevice() throws Exception { |
| 5135 | // Bubbles are allowed! |
Mady Mellor | c682034 | 2019-05-20 12:04:36 -0700 | [diff] [blame] | 5136 | setUpPrefsForBubbles(true /* global */, true /* app */, true /* channel */); |
Mady Mellor | 5c11a2e | 2019-04-25 17:26:15 -0700 | [diff] [blame] | 5137 | |
| 5138 | // Plain notification that has bubble metadata |
| 5139 | NotificationRecord nr = generateNotificationRecord(mTestNotificationChannel, |
| 5140 | null /* tvExtender */, true /* isBubble */); |
| 5141 | mBinderService.enqueueNotificationWithTag(PKG, PKG, "tag", |
| 5142 | nr.sbn.getId(), nr.sbn.getNotification(), nr.sbn.getUserId()); |
| 5143 | waitForIdle(); |
| 5144 | |
| 5145 | // Would be a normal notification because wouldn't have met requirements to bubble |
| 5146 | StatusBarNotification[] notifsBefore = mBinderService.getActiveNotifications(PKG); |
| 5147 | assertEquals(1, notifsBefore.length); |
| 5148 | assertEquals((notifsBefore[0].getNotification().flags & FLAG_BUBBLE), 0); |
| 5149 | |
| 5150 | // Make the package foreground so that we're allowed to be a bubble |
| 5151 | when(mActivityManager.getPackageImportance(nr.sbn.getPackageName())).thenReturn( |
| 5152 | IMPORTANCE_FOREGROUND); |
| 5153 | |
| 5154 | // And we are low ram |
| 5155 | when(mActivityManager.isLowRamDevice()).thenReturn(true); |
| 5156 | |
| 5157 | // We wouldn't be a bubble because the notification didn't meet requirements (low ram) |
| 5158 | StatusBarNotification[] notifsAfter = mBinderService.getActiveNotifications(PKG); |
| 5159 | assertEquals(1, notifsAfter.length); |
| 5160 | assertEquals((notifsAfter[0].getNotification().flags & FLAG_BUBBLE), 0); |
Mady Mellor | 5c11a2e | 2019-04-25 17:26:15 -0700 | [diff] [blame] | 5161 | } |
Mady Mellor | a773196 | 2019-06-17 17:57:02 -0700 | [diff] [blame] | 5162 | |
| 5163 | @Test |
| 5164 | public void testNotificationBubbles_flagAutoExpandForeground_fails_notForeground() |
| 5165 | throws Exception { |
| 5166 | // Bubbles are allowed! |
| 5167 | setUpPrefsForBubbles(true /* global */, true /* app */, true /* channel */); |
| 5168 | |
| 5169 | // Give it bubble metadata |
| 5170 | Notification.BubbleMetadata data = getBasicBubbleMetadataBuilder() |
| 5171 | .setSuppressNotification(true) |
| 5172 | .setAutoExpandBubble(true).build(); |
| 5173 | // Give it a person |
| 5174 | Person person = new Person.Builder() |
| 5175 | .setName("bubblebot") |
| 5176 | .build(); |
| 5177 | // It needs remote input to be bubble-able |
| 5178 | RemoteInput remoteInput = new RemoteInput.Builder("reply_key").setLabel("reply").build(); |
| 5179 | PendingIntent inputIntent = PendingIntent.getActivity(mContext, 0, new Intent(), 0); |
| 5180 | Icon icon = Icon.createWithResource(mContext, android.R.drawable.sym_def_app_icon); |
| 5181 | Notification.Action replyAction = new Notification.Action.Builder(icon, "Reply", |
| 5182 | inputIntent).addRemoteInput(remoteInput) |
| 5183 | .build(); |
| 5184 | // Make it messaging style |
| 5185 | Notification.Builder nb = new Notification.Builder(mContext, |
| 5186 | mTestNotificationChannel.getId()) |
| 5187 | .setContentTitle("foo") |
| 5188 | .setBubbleMetadata(data) |
| 5189 | .setStyle(new Notification.MessagingStyle(person) |
| 5190 | .setConversationTitle("Bubble Chat") |
| 5191 | .addMessage("Hello?", |
| 5192 | SystemClock.currentThreadTimeMillis() - 300000, person) |
| 5193 | .addMessage("Is it me you're looking for?", |
| 5194 | SystemClock.currentThreadTimeMillis(), person) |
| 5195 | ) |
| 5196 | .setActions(replyAction) |
| 5197 | .setSmallIcon(android.R.drawable.sym_def_app_icon); |
| 5198 | |
| 5199 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, null, mUid, 0, |
| 5200 | nb.build(), new UserHandle(mUid), null, 0); |
| 5201 | NotificationRecord nr = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 5202 | |
| 5203 | // Ensure we're not foreground |
| 5204 | when(mActivityManager.getPackageImportance(nr.sbn.getPackageName())).thenReturn( |
| 5205 | IMPORTANCE_VISIBLE); |
| 5206 | |
| 5207 | mBinderService.enqueueNotificationWithTag(PKG, PKG, null, |
| 5208 | nr.sbn.getId(), nr.sbn.getNotification(), nr.sbn.getUserId()); |
| 5209 | waitForIdle(); |
| 5210 | |
| 5211 | // yes allowed, yes messaging, yes bubble |
| 5212 | Notification notif = mService.getNotificationRecord(sbn.getKey()).getNotification(); |
| 5213 | assertTrue(notif.isBubbleNotification()); |
| 5214 | |
| 5215 | // Our flags should have failed since we're not foreground |
| 5216 | assertFalse(notif.getBubbleMetadata().getAutoExpandBubble()); |
| 5217 | assertFalse(notif.getBubbleMetadata().isNotificationSuppressed()); |
| 5218 | } |
| 5219 | |
| 5220 | @Test |
| 5221 | public void testNotificationBubbles_flagAutoExpandForeground_succeeds_foreground() |
| 5222 | throws RemoteException { |
| 5223 | // Bubbles are allowed! |
| 5224 | setUpPrefsForBubbles(true /* global */, true /* app */, true /* channel */); |
| 5225 | |
| 5226 | // Give it bubble metadata |
| 5227 | Notification.BubbleMetadata data = getBasicBubbleMetadataBuilder() |
| 5228 | .setSuppressNotification(true) |
| 5229 | .setAutoExpandBubble(true).build(); |
| 5230 | // Give it a person |
| 5231 | Person person = new Person.Builder() |
| 5232 | .setName("bubblebot") |
| 5233 | .build(); |
| 5234 | // It needs remote input to be bubble-able |
| 5235 | RemoteInput remoteInput = new RemoteInput.Builder("reply_key").setLabel("reply").build(); |
| 5236 | PendingIntent inputIntent = PendingIntent.getActivity(mContext, 0, new Intent(), 0); |
| 5237 | Icon icon = Icon.createWithResource(mContext, android.R.drawable.sym_def_app_icon); |
| 5238 | Notification.Action replyAction = new Notification.Action.Builder(icon, "Reply", |
| 5239 | inputIntent).addRemoteInput(remoteInput) |
| 5240 | .build(); |
| 5241 | // Make it messaging style |
| 5242 | Notification.Builder nb = new Notification.Builder(mContext, |
| 5243 | mTestNotificationChannel.getId()) |
| 5244 | .setContentTitle("foo") |
| 5245 | .setBubbleMetadata(data) |
| 5246 | .setStyle(new Notification.MessagingStyle(person) |
| 5247 | .setConversationTitle("Bubble Chat") |
| 5248 | .addMessage("Hello?", |
| 5249 | SystemClock.currentThreadTimeMillis() - 300000, person) |
| 5250 | .addMessage("Is it me you're looking for?", |
| 5251 | SystemClock.currentThreadTimeMillis(), person) |
| 5252 | ) |
| 5253 | .setActions(replyAction) |
| 5254 | .setSmallIcon(android.R.drawable.sym_def_app_icon); |
| 5255 | |
| 5256 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, null, mUid, 0, |
| 5257 | nb.build(), new UserHandle(mUid), null, 0); |
| 5258 | NotificationRecord nr = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 5259 | |
| 5260 | // Ensure we are in the foreground |
| 5261 | when(mActivityManager.getPackageImportance(nr.sbn.getPackageName())).thenReturn( |
| 5262 | IMPORTANCE_FOREGROUND); |
| 5263 | |
| 5264 | mBinderService.enqueueNotificationWithTag(PKG, PKG, null, |
| 5265 | nr.sbn.getId(), nr.sbn.getNotification(), nr.sbn.getUserId()); |
| 5266 | waitForIdle(); |
| 5267 | |
| 5268 | // yes allowed, yes messaging, yes bubble |
| 5269 | Notification notif = mService.getNotificationRecord(sbn.getKey()).getNotification(); |
| 5270 | assertTrue(notif.isBubbleNotification()); |
| 5271 | |
| 5272 | // Our flags should have failed since we are foreground |
| 5273 | assertTrue(notif.getBubbleMetadata().getAutoExpandBubble()); |
| 5274 | assertTrue(notif.getBubbleMetadata().isNotificationSuppressed()); |
| 5275 | } |
Mady Mellor | 22f2f07 | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 5276 | |
| 5277 | @Test |
| 5278 | public void testNotificationBubbles_bubbleChildrenStay_whenGroupSummaryDismissed() |
| 5279 | throws Exception { |
| 5280 | // Bubbles are allowed! |
| 5281 | setUpPrefsForBubbles(true /* global */, true /* app */, true /* channel */); |
| 5282 | |
| 5283 | NotificationRecord nrSummary = addGroupWithBubblesAndValidateAdded( |
| 5284 | true /* summaryAutoCancel */); |
| 5285 | |
| 5286 | // Dismiss summary |
| 5287 | final NotificationVisibility nv = NotificationVisibility.obtain(nrSummary.getKey(), 1, 2, |
| 5288 | true); |
| 5289 | mService.mNotificationDelegate.onNotificationClear(mUid, 0, PKG, nrSummary.sbn.getTag(), |
| 5290 | nrSummary.sbn.getId(), nrSummary.getUserId(), nrSummary.getKey(), |
| 5291 | NotificationStats.DISMISSAL_SHADE, |
| 5292 | NotificationStats.DISMISS_SENTIMENT_NEUTRAL, nv); |
| 5293 | waitForIdle(); |
| 5294 | |
| 5295 | // The bubble should still exist |
| 5296 | StatusBarNotification[] notifsAfter = mBinderService.getActiveNotifications(PKG); |
| 5297 | assertEquals(1, notifsAfter.length); |
| 5298 | } |
| 5299 | |
| 5300 | @Test |
| 5301 | public void testNotificationBubbles_bubbleChildrenStay_whenGroupSummaryClicked() |
| 5302 | throws Exception { |
| 5303 | // Bubbles are allowed! |
| 5304 | setUpPrefsForBubbles(true /* global */, true /* app */, true /* channel */); |
| 5305 | |
| 5306 | NotificationRecord nrSummary = addGroupWithBubblesAndValidateAdded( |
| 5307 | true /* summaryAutoCancel */); |
| 5308 | |
| 5309 | // Click summary |
| 5310 | final NotificationVisibility nv = NotificationVisibility.obtain(nrSummary.getKey(), 1, 2, |
| 5311 | true); |
| 5312 | mService.mNotificationDelegate.onNotificationClick(mUid, Binder.getCallingPid(), |
| 5313 | nrSummary.getKey(), nv); |
| 5314 | waitForIdle(); |
| 5315 | |
| 5316 | // The bubble should still exist |
| 5317 | StatusBarNotification[] notifsAfter = mBinderService.getActiveNotifications(PKG); |
| 5318 | assertEquals(1, notifsAfter.length); |
| 5319 | } |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 5320 | } |