Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.android.server.notification; |
| 17 | |
Julia Reynolds | 0f76734 | 2019-12-18 09:11:55 -0500 | [diff] [blame] | 18 | import static android.app.NotificationChannel.CONVERSATION_CHANNEL_ID_FORMAT; |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 19 | import static android.app.NotificationManager.BUBBLE_PREFERENCE_ALL; |
| 20 | import static android.app.NotificationManager.BUBBLE_PREFERENCE_NONE; |
| 21 | import static android.app.NotificationManager.BUBBLE_PREFERENCE_SELECTED; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 22 | import static android.app.NotificationManager.IMPORTANCE_DEFAULT; |
| 23 | import static android.app.NotificationManager.IMPORTANCE_HIGH; |
| 24 | import static android.app.NotificationManager.IMPORTANCE_LOW; |
| 25 | import static android.app.NotificationManager.IMPORTANCE_MAX; |
| 26 | import static android.app.NotificationManager.IMPORTANCE_NONE; |
| 27 | import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED; |
| 28 | |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 29 | import static com.android.server.notification.PreferencesHelper.DEFAULT_BUBBLE_PREFERENCE; |
Julia Reynolds | c29370a | 2019-08-20 16:08:42 -0400 | [diff] [blame] | 30 | import static com.android.server.notification.PreferencesHelper.NOTIFICATION_CHANNEL_COUNT_LIMIT; |
| 31 | |
Julia Reynolds | 882f206 | 2020-02-05 12:11:38 -0500 | [diff] [blame] | 32 | import static com.google.common.truth.Truth.assertThat; |
| 33 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 34 | import static junit.framework.Assert.assertNull; |
| 35 | import static junit.framework.Assert.fail; |
| 36 | |
| 37 | import static org.junit.Assert.assertEquals; |
| 38 | import static org.junit.Assert.assertFalse; |
| 39 | import static org.junit.Assert.assertNotNull; |
| 40 | import static org.junit.Assert.assertTrue; |
| 41 | import static org.mockito.ArgumentMatchers.any; |
Dmitri Plotnikov | 7a223fb | 2020-02-14 17:04:13 -0800 | [diff] [blame] | 42 | import static org.mockito.ArgumentMatchers.anyInt; |
| 43 | import static org.mockito.ArgumentMatchers.anyString; |
| 44 | import static org.mockito.ArgumentMatchers.eq; |
| 45 | import static org.mockito.Mockito.doReturn; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 46 | import static org.mockito.Mockito.mock; |
| 47 | import static org.mockito.Mockito.never; |
| 48 | import static org.mockito.Mockito.reset; |
| 49 | import static org.mockito.Mockito.times; |
| 50 | import static org.mockito.Mockito.verify; |
| 51 | import static org.mockito.Mockito.when; |
| 52 | |
| 53 | import android.app.Notification; |
| 54 | import android.app.NotificationChannel; |
| 55 | import android.app.NotificationChannelGroup; |
| 56 | import android.app.NotificationManager; |
| 57 | import android.content.ContentProvider; |
| 58 | import android.content.Context; |
| 59 | import android.content.IContentProvider; |
| 60 | import android.content.pm.ApplicationInfo; |
| 61 | import android.content.pm.PackageInfo; |
| 62 | import android.content.pm.PackageManager; |
| 63 | import android.content.pm.Signature; |
| 64 | import android.content.res.Resources; |
| 65 | import android.graphics.Color; |
| 66 | import android.media.AudioAttributes; |
| 67 | import android.net.Uri; |
| 68 | import android.os.Build; |
| 69 | import android.os.UserHandle; |
| 70 | import android.provider.Settings; |
Lyn Han | 4463f84 | 2019-07-09 15:27:28 -0700 | [diff] [blame] | 71 | import android.provider.Settings.Global; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 72 | import android.provider.Settings.Secure; |
Julia Reynolds | 882f206 | 2020-02-05 12:11:38 -0500 | [diff] [blame] | 73 | import android.service.notification.ConversationChannelWrapper; |
Dmitri Plotnikov | 7a223fb | 2020-02-14 17:04:13 -0800 | [diff] [blame] | 74 | import android.test.mock.MockIContentProvider; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 75 | import android.test.suitebuilder.annotation.SmallTest; |
| 76 | import android.testing.TestableContentResolver; |
| 77 | import android.util.ArrayMap; |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 78 | import android.util.ArraySet; |
Julia Reynolds | e7ca31b | 2019-04-25 15:41:47 -0400 | [diff] [blame] | 79 | import android.util.Pair; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 80 | import android.util.Xml; |
| 81 | |
Beverly | 4767922 | 2019-05-16 15:46:11 -0400 | [diff] [blame] | 82 | import androidx.test.InstrumentationRegistry; |
| 83 | import androidx.test.runner.AndroidJUnit4; |
| 84 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 85 | import com.android.internal.util.FastXmlSerializer; |
| 86 | import com.android.server.UiServiceTestCase; |
| 87 | |
| 88 | import org.json.JSONArray; |
| 89 | import org.json.JSONObject; |
| 90 | import org.junit.Before; |
| 91 | import org.junit.Test; |
| 92 | import org.junit.runner.RunWith; |
| 93 | import org.mockito.Mock; |
| 94 | import org.mockito.MockitoAnnotations; |
Dmitri Plotnikov | 7a223fb | 2020-02-14 17:04:13 -0800 | [diff] [blame] | 95 | import org.mockito.Spy; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 96 | import org.xmlpull.v1.XmlPullParser; |
| 97 | import org.xmlpull.v1.XmlSerializer; |
| 98 | |
| 99 | import java.io.BufferedInputStream; |
| 100 | import java.io.BufferedOutputStream; |
| 101 | import java.io.ByteArrayInputStream; |
| 102 | import java.io.ByteArrayOutputStream; |
Julia Reynolds | 882f206 | 2020-02-05 12:11:38 -0500 | [diff] [blame] | 103 | import java.util.ArrayList; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 104 | import java.util.Arrays; |
| 105 | import java.util.HashMap; |
| 106 | import java.util.List; |
| 107 | import java.util.Map; |
| 108 | import java.util.Objects; |
| 109 | import java.util.concurrent.ThreadLocalRandom; |
| 110 | |
| 111 | @SmallTest |
| 112 | @RunWith(AndroidJUnit4.class) |
| 113 | public class PreferencesHelperTest extends UiServiceTestCase { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 114 | private static final int UID_N_MR1 = 0; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 115 | private static final UserHandle USER = UserHandle.of(0); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 116 | private static final int UID_O = 1111; |
Julia Reynolds | 0297145 | 2020-02-14 16:44:19 -0500 | [diff] [blame] | 117 | private static final int UID_P = 2222; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 118 | private static final String SYSTEM_PKG = "android"; |
Beverly | 0479cde2 | 2018-11-09 11:05:34 -0500 | [diff] [blame] | 119 | private static final int SYSTEM_UID = 1000; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 120 | private static final UserHandle USER2 = UserHandle.of(10); |
| 121 | private static final String TEST_CHANNEL_ID = "test_channel_id"; |
| 122 | private static final String TEST_AUTHORITY = "test"; |
| 123 | private static final Uri SOUND_URI = |
| 124 | Uri.parse("content://" + TEST_AUTHORITY + "/internal/audio/media/10"); |
| 125 | private static final Uri CANONICAL_SOUND_URI = |
| 126 | Uri.parse("content://" + TEST_AUTHORITY |
| 127 | + "/internal/audio/media/10?title=Test&canonical=1"); |
| 128 | |
| 129 | @Mock NotificationUsageStats mUsageStats; |
| 130 | @Mock RankingHandler mHandler; |
| 131 | @Mock PackageManager mPm; |
Dmitri Plotnikov | 7a223fb | 2020-02-14 17:04:13 -0800 | [diff] [blame] | 132 | @Spy IContentProvider mTestIContentProvider = new MockIContentProvider(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 133 | @Mock Context mContext; |
| 134 | @Mock ZenModeHelper mMockZenModeHelper; |
| 135 | |
| 136 | private NotificationManager.Policy mTestNotificationPolicy; |
| 137 | |
| 138 | private PreferencesHelper mHelper; |
| 139 | private AudioAttributes mAudioAttributes; |
Will Brockman | 23db6d4 | 2020-02-28 09:51:12 -0500 | [diff] [blame] | 140 | private NotificationChannelLoggerFake mLogger = new NotificationChannelLoggerFake(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 141 | |
| 142 | @Before |
| 143 | public void setUp() throws Exception { |
| 144 | MockitoAnnotations.initMocks(this); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 145 | |
| 146 | final ApplicationInfo legacy = new ApplicationInfo(); |
| 147 | legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1; |
| 148 | final ApplicationInfo upgrade = new ApplicationInfo(); |
| 149 | upgrade.targetSdkVersion = Build.VERSION_CODES.O; |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 150 | when(mPm.getApplicationInfoAsUser(eq(PKG_N_MR1), anyInt(), anyInt())).thenReturn(legacy); |
| 151 | when(mPm.getApplicationInfoAsUser(eq(PKG_O), anyInt(), anyInt())).thenReturn(upgrade); |
Julia Reynolds | 0297145 | 2020-02-14 16:44:19 -0500 | [diff] [blame] | 152 | when(mPm.getApplicationInfoAsUser(eq(PKG_P), anyInt(), anyInt())).thenReturn(upgrade); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 153 | when(mPm.getApplicationInfoAsUser(eq(SYSTEM_PKG), anyInt(), anyInt())).thenReturn(upgrade); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 154 | when(mPm.getPackageUidAsUser(eq(PKG_N_MR1), anyInt())).thenReturn(UID_N_MR1); |
| 155 | when(mPm.getPackageUidAsUser(eq(PKG_O), anyInt())).thenReturn(UID_O); |
Julia Reynolds | 0297145 | 2020-02-14 16:44:19 -0500 | [diff] [blame] | 156 | when(mPm.getPackageUidAsUser(eq(PKG_P), anyInt())).thenReturn(UID_P); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 157 | when(mPm.getPackageUidAsUser(eq(SYSTEM_PKG), anyInt())).thenReturn(SYSTEM_UID); |
| 158 | PackageInfo info = mock(PackageInfo.class); |
| 159 | info.signatures = new Signature[] {mock(Signature.class)}; |
| 160 | when(mPm.getPackageInfoAsUser(eq(SYSTEM_PKG), anyInt(), anyInt())).thenReturn(info); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 161 | when(mPm.getPackageInfoAsUser(eq(PKG_N_MR1), anyInt(), anyInt())) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 162 | .thenReturn(mock(PackageInfo.class)); |
| 163 | when(mContext.getResources()).thenReturn( |
| 164 | InstrumentationRegistry.getContext().getResources()); |
| 165 | when(mContext.getContentResolver()).thenReturn( |
| 166 | InstrumentationRegistry.getContext().getContentResolver()); |
| 167 | when(mContext.getPackageManager()).thenReturn(mPm); |
| 168 | when(mContext.getApplicationInfo()).thenReturn(legacy); |
| 169 | // most tests assume badging is enabled |
| 170 | TestableContentResolver contentResolver = getContext().getContentResolver(); |
| 171 | contentResolver.setFallbackToExisting(false); |
| 172 | Secure.putIntForUser(contentResolver, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 173 | Secure.NOTIFICATION_BADGING, 1, UserHandle.getUserId(UID_N_MR1)); |
Lyn Han | 4463f84 | 2019-07-09 15:27:28 -0700 | [diff] [blame] | 174 | Global.putInt(contentResolver, Global.NOTIFICATION_BUBBLES, 1); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 175 | |
| 176 | ContentProvider testContentProvider = mock(ContentProvider.class); |
| 177 | when(testContentProvider.getIContentProvider()).thenReturn(mTestIContentProvider); |
| 178 | contentResolver.addProvider(TEST_AUTHORITY, testContentProvider); |
| 179 | |
Dmitri Plotnikov | 7a223fb | 2020-02-14 17:04:13 -0800 | [diff] [blame] | 180 | doReturn(CANONICAL_SOUND_URI) |
| 181 | .when(mTestIContentProvider).canonicalize(any(), any(), eq(SOUND_URI)); |
| 182 | doReturn(CANONICAL_SOUND_URI) |
| 183 | .when(mTestIContentProvider).canonicalize(any(), any(), eq(CANONICAL_SOUND_URI)); |
| 184 | doReturn(SOUND_URI) |
| 185 | .when(mTestIContentProvider).uncanonicalize(any(), any(), eq(CANONICAL_SOUND_URI)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 186 | |
| 187 | mTestNotificationPolicy = new NotificationManager.Policy(0, 0, 0, 0, |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 188 | NotificationManager.Policy.STATE_CHANNELS_BYPASSING_DND, 0); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 189 | when(mMockZenModeHelper.getNotificationPolicy()).thenReturn(mTestNotificationPolicy); |
Will Brockman | 23db6d4 | 2020-02-28 09:51:12 -0500 | [diff] [blame] | 190 | mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper, mLogger); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 191 | resetZenModeHelper(); |
| 192 | |
| 193 | mAudioAttributes = new AudioAttributes.Builder() |
| 194 | .setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN) |
| 195 | .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE) |
| 196 | .setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED) |
| 197 | .build(); |
| 198 | } |
| 199 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 200 | private ByteArrayOutputStream writeXmlAndPurge( |
| 201 | String pkg, int uid, boolean forBackup, int userId, String... channelIds) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 202 | throws Exception { |
| 203 | XmlSerializer serializer = new FastXmlSerializer(); |
| 204 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 205 | serializer.setOutput(new BufferedOutputStream(baos), "utf-8"); |
| 206 | serializer.startDocument(null, true); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 207 | mHelper.writeXml(serializer, forBackup, userId); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 208 | serializer.endDocument(); |
| 209 | serializer.flush(); |
| 210 | for (String channelId : channelIds) { |
| 211 | mHelper.permanentlyDeleteNotificationChannel(pkg, uid, channelId); |
| 212 | } |
| 213 | return baos; |
| 214 | } |
| 215 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 216 | private void loadStreamXml(ByteArrayOutputStream stream, boolean forRestore, int userId) |
| 217 | throws Exception { |
| 218 | loadByteArrayXml(stream.toByteArray(), forRestore, userId); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 219 | } |
| 220 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 221 | private void loadByteArrayXml(byte[] byteArray, boolean forRestore, int userId) |
| 222 | throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 223 | XmlPullParser parser = Xml.newPullParser(); |
| 224 | parser.setInput(new BufferedInputStream(new ByteArrayInputStream(byteArray)), null); |
| 225 | parser.nextTag(); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 226 | mHelper.readXml(parser, forRestore, userId); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | private void compareChannels(NotificationChannel expected, NotificationChannel actual) { |
| 230 | assertEquals(expected.getId(), actual.getId()); |
| 231 | assertEquals(expected.getName(), actual.getName()); |
| 232 | assertEquals(expected.getDescription(), actual.getDescription()); |
| 233 | assertEquals(expected.shouldVibrate(), actual.shouldVibrate()); |
| 234 | assertEquals(expected.shouldShowLights(), actual.shouldShowLights()); |
| 235 | assertEquals(expected.getImportance(), actual.getImportance()); |
| 236 | assertEquals(expected.getLockscreenVisibility(), actual.getLockscreenVisibility()); |
| 237 | assertEquals(expected.getSound(), actual.getSound()); |
| 238 | assertEquals(expected.canBypassDnd(), actual.canBypassDnd()); |
| 239 | assertTrue(Arrays.equals(expected.getVibrationPattern(), actual.getVibrationPattern())); |
| 240 | assertEquals(expected.getGroup(), actual.getGroup()); |
| 241 | assertEquals(expected.getAudioAttributes(), actual.getAudioAttributes()); |
| 242 | assertEquals(expected.getLightColor(), actual.getLightColor()); |
Julia Reynolds | 0f76734 | 2019-12-18 09:11:55 -0500 | [diff] [blame] | 243 | assertEquals(expected.getParentChannelId(), actual.getParentChannelId()); |
| 244 | assertEquals(expected.getConversationId(), actual.getConversationId()); |
Julia Reynolds | dcd70d6 | 2020-01-15 10:33:43 -0500 | [diff] [blame] | 245 | assertEquals(expected.isDemoted(), actual.isDemoted()); |
Julia Reynolds | 0f76734 | 2019-12-18 09:11:55 -0500 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | private void compareChannelsParentChild(NotificationChannel parent, |
| 249 | NotificationChannel actual, String conversationId) { |
| 250 | assertEquals(parent.getName(), actual.getName()); |
| 251 | assertEquals(parent.getDescription(), actual.getDescription()); |
| 252 | assertEquals(parent.shouldVibrate(), actual.shouldVibrate()); |
| 253 | assertEquals(parent.shouldShowLights(), actual.shouldShowLights()); |
| 254 | assertEquals(parent.getImportance(), actual.getImportance()); |
| 255 | assertEquals(parent.getLockscreenVisibility(), actual.getLockscreenVisibility()); |
| 256 | assertEquals(parent.getSound(), actual.getSound()); |
| 257 | assertEquals(parent.canBypassDnd(), actual.canBypassDnd()); |
| 258 | assertTrue(Arrays.equals(parent.getVibrationPattern(), actual.getVibrationPattern())); |
| 259 | assertEquals(parent.getGroup(), actual.getGroup()); |
| 260 | assertEquals(parent.getAudioAttributes(), actual.getAudioAttributes()); |
| 261 | assertEquals(parent.getLightColor(), actual.getLightColor()); |
| 262 | assertEquals(parent.getId(), actual.getParentChannelId()); |
| 263 | assertEquals(conversationId, actual.getConversationId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | private void compareGroups(NotificationChannelGroup expected, NotificationChannelGroup actual) { |
| 267 | assertEquals(expected.getId(), actual.getId()); |
| 268 | assertEquals(expected.getName(), actual.getName()); |
| 269 | assertEquals(expected.getDescription(), actual.getDescription()); |
| 270 | assertEquals(expected.isBlocked(), actual.isBlocked()); |
| 271 | } |
| 272 | |
| 273 | private NotificationChannel getChannel() { |
| 274 | return new NotificationChannel("id", "name", IMPORTANCE_LOW); |
| 275 | } |
| 276 | |
| 277 | private NotificationChannel findChannel(List<NotificationChannel> channels, String id) { |
| 278 | for (NotificationChannel channel : channels) { |
| 279 | if (channel.getId().equals(id)) { |
| 280 | return channel; |
| 281 | } |
| 282 | } |
| 283 | return null; |
| 284 | } |
| 285 | |
| 286 | private void resetZenModeHelper() { |
| 287 | reset(mMockZenModeHelper); |
| 288 | when(mMockZenModeHelper.getNotificationPolicy()).thenReturn(mTestNotificationPolicy); |
| 289 | } |
| 290 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 291 | private void setUpPackageWithUid(String packageName, int uid) throws Exception { |
| 292 | when(mPm.getApplicationInfoAsUser(eq(packageName), anyInt(), anyInt())) |
| 293 | .thenReturn(new ApplicationInfo()); |
| 294 | when(mPm.getPackageUidAsUser(eq(packageName), anyInt())).thenReturn(uid); |
| 295 | } |
| 296 | |
| 297 | @Test |
| 298 | public void testWriteXml_onlyBackupsTargetUser() throws Exception { |
| 299 | // Setup package notifications. |
| 300 | String package0 = "test.package.user0"; |
| 301 | int uid0 = 1001; |
| 302 | setUpPackageWithUid(package0, uid0); |
| 303 | NotificationChannel channel0 = new NotificationChannel("id0", "name0", IMPORTANCE_HIGH); |
Julia Reynolds | dafd3a4 | 2019-05-24 13:33:28 -0400 | [diff] [blame] | 304 | assertTrue(mHelper.createNotificationChannel(package0, uid0, channel0, true, false)); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 305 | |
| 306 | String package10 = "test.package.user10"; |
| 307 | int uid10 = 1001001; |
| 308 | setUpPackageWithUid(package10, uid10); |
| 309 | NotificationChannel channel10 = new NotificationChannel("id10", "name10", IMPORTANCE_HIGH); |
Julia Reynolds | dafd3a4 | 2019-05-24 13:33:28 -0400 | [diff] [blame] | 310 | assertTrue(mHelper.createNotificationChannel(package10, uid10, channel10, true, false)); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 311 | |
| 312 | ByteArrayOutputStream baos = writeXmlAndPurge(package10, uid10, true, 10); |
| 313 | |
| 314 | // Reset state. |
| 315 | mHelper.onPackagesChanged(true, 0, new String[] {package0}, new int[] {uid0}); |
| 316 | mHelper.onPackagesChanged(true, 10, new String[] {package10}, new int[] {uid10}); |
| 317 | |
| 318 | // Parse backup data. |
| 319 | loadStreamXml(baos, true, 0); |
| 320 | loadStreamXml(baos, true, 10); |
| 321 | |
| 322 | assertEquals( |
| 323 | channel10, |
| 324 | mHelper.getNotificationChannel(package10, uid10, channel10.getId(), false)); |
| 325 | assertNull(mHelper.getNotificationChannel(package0, uid0, channel0.getId(), false)); |
| 326 | } |
| 327 | |
| 328 | @Test |
| 329 | public void testReadXml_onlyRestoresTargetUser() throws Exception { |
| 330 | // Setup package in user 0. |
| 331 | String package0 = "test.package.user0"; |
| 332 | int uid0 = 1001; |
| 333 | setUpPackageWithUid(package0, uid0); |
| 334 | NotificationChannel channel0 = new NotificationChannel("id0", "name0", IMPORTANCE_HIGH); |
Julia Reynolds | dafd3a4 | 2019-05-24 13:33:28 -0400 | [diff] [blame] | 335 | assertTrue(mHelper.createNotificationChannel(package0, uid0, channel0, true, false)); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 336 | |
| 337 | ByteArrayOutputStream baos = writeXmlAndPurge(package0, uid0, true, 0); |
| 338 | |
| 339 | // Reset state. |
| 340 | mHelper.onPackagesChanged(true, 0, new String[] {package0}, new int[] {uid0}); |
| 341 | |
| 342 | // Restore should convert the uid according to the target user. |
| 343 | int expectedUid = 1001001; |
| 344 | setUpPackageWithUid(package0, expectedUid); |
| 345 | // Parse backup data. |
| 346 | loadStreamXml(baos, true, 10); |
| 347 | |
| 348 | assertEquals( |
| 349 | channel0, |
| 350 | mHelper.getNotificationChannel(package0, expectedUid, channel0.getId(), false)); |
| 351 | assertNull(mHelper.getNotificationChannel(package0, uid0, channel0.getId(), false)); |
| 352 | } |
| 353 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 354 | @Test |
| 355 | public void testChannelXml() throws Exception { |
| 356 | NotificationChannelGroup ncg = new NotificationChannelGroup("1", "bye"); |
| 357 | ncg.setBlocked(true); |
| 358 | ncg.setDescription("group desc"); |
| 359 | NotificationChannelGroup ncg2 = new NotificationChannelGroup("2", "hello"); |
| 360 | NotificationChannel channel1 = |
| 361 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 362 | NotificationChannel channel2 = |
| 363 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 364 | channel2.setDescription("descriptions for all"); |
| 365 | channel2.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes); |
| 366 | channel2.enableLights(true); |
| 367 | channel2.setBypassDnd(true); |
| 368 | channel2.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 369 | channel2.enableVibration(true); |
| 370 | channel2.setGroup(ncg.getId()); |
| 371 | channel2.setVibrationPattern(new long[]{100, 67, 145, 156}); |
| 372 | channel2.setLightColor(Color.BLUE); |
Julia Reynolds | 12ba4cf | 2020-01-10 16:01:38 -0500 | [diff] [blame] | 373 | channel2.setConversationId("id1", "conversation"); |
Julia Reynolds | dcd70d6 | 2020-01-15 10:33:43 -0500 | [diff] [blame] | 374 | channel2.setDemoted(true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 375 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 376 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true); |
| 377 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg2, true); |
Julia Reynolds | dafd3a4 | 2019-05-24 13:33:28 -0400 | [diff] [blame] | 378 | assertTrue(mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false)); |
| 379 | assertTrue(mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, false, false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 380 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 381 | mHelper.setShowBadge(PKG_N_MR1, UID_N_MR1, true); |
| 382 | mHelper.setAppImportanceLocked(PKG_N_MR1, UID_N_MR1); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 383 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 384 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, false, |
| 385 | UserHandle.USER_ALL, channel1.getId(), channel2.getId(), |
| 386 | NotificationChannel.DEFAULT_CHANNEL_ID); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 387 | mHelper.onPackagesChanged(true, UserHandle.myUserId(), new String[]{PKG_N_MR1}, new int[]{ |
| 388 | UID_N_MR1}); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 389 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 390 | loadStreamXml(baos, false, UserHandle.USER_ALL); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 391 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 392 | assertTrue(mHelper.canShowBadge(PKG_N_MR1, UID_N_MR1)); |
| 393 | assertTrue(mHelper.getIsAppImportanceLocked(PKG_N_MR1, UID_N_MR1)); |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 394 | assertEquals(channel1, |
| 395 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1.getId(), false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 396 | compareChannels(channel2, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 397 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2.getId(), false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 398 | |
Julia Reynolds | 13ed28b | 2018-09-21 15:20:13 -0400 | [diff] [blame] | 399 | List<NotificationChannelGroup> actualGroups = mHelper.getNotificationChannelGroups( |
| 400 | PKG_N_MR1, UID_N_MR1, false, true, false).getList(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 401 | boolean foundNcg = false; |
| 402 | for (NotificationChannelGroup actual : actualGroups) { |
| 403 | if (ncg.getId().equals(actual.getId())) { |
| 404 | foundNcg = true; |
| 405 | compareGroups(ncg, actual); |
| 406 | } else if (ncg2.getId().equals(actual.getId())) { |
| 407 | compareGroups(ncg2, actual); |
| 408 | } |
| 409 | } |
| 410 | assertTrue(foundNcg); |
| 411 | |
| 412 | boolean foundChannel2Group = false; |
| 413 | for (NotificationChannelGroup actual : actualGroups) { |
| 414 | if (channel2.getGroup().equals(actual.getChannels().get(0).getGroup())) { |
| 415 | foundChannel2Group = true; |
| 416 | break; |
| 417 | } |
| 418 | } |
| 419 | assertTrue(foundChannel2Group); |
| 420 | } |
| 421 | |
| 422 | @Test |
| 423 | public void testChannelXmlForBackup() throws Exception { |
| 424 | NotificationChannelGroup ncg = new NotificationChannelGroup("1", "bye"); |
| 425 | NotificationChannelGroup ncg2 = new NotificationChannelGroup("2", "hello"); |
| 426 | NotificationChannel channel1 = |
| 427 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 428 | NotificationChannel channel2 = |
| 429 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 430 | channel2.setDescription("descriptions for all"); |
| 431 | channel2.setSound(SOUND_URI, mAudioAttributes); |
| 432 | channel2.enableLights(true); |
| 433 | channel2.setBypassDnd(true); |
| 434 | channel2.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 435 | channel2.enableVibration(false); |
| 436 | channel2.setGroup(ncg.getId()); |
| 437 | channel2.setLightColor(Color.BLUE); |
| 438 | NotificationChannel channel3 = new NotificationChannel("id3", "NAM3", IMPORTANCE_HIGH); |
| 439 | channel3.enableVibration(true); |
| 440 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 441 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true); |
| 442 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg2, true); |
| 443 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false); |
| 444 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, false, false); |
| 445 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3, false, false); |
| 446 | mHelper.createNotificationChannel(PKG_O, UID_O, getChannel(), true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 447 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 448 | mHelper.setShowBadge(PKG_N_MR1, UID_N_MR1, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 449 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 450 | mHelper.setImportance(PKG_O, UID_O, IMPORTANCE_NONE); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 451 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 452 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, true, |
| 453 | UserHandle.USER_SYSTEM, channel1.getId(), channel2.getId(), channel3.getId(), |
| 454 | NotificationChannel.DEFAULT_CHANNEL_ID); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 455 | mHelper.onPackagesChanged(true, UserHandle.myUserId(), new String[]{PKG_N_MR1, PKG_O}, |
| 456 | new int[]{UID_N_MR1, UID_O}); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 457 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 458 | mHelper.setShowBadge(PKG_O, UID_O, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 459 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 460 | loadStreamXml(baos, true, UserHandle.USER_SYSTEM); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 461 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 462 | assertEquals(IMPORTANCE_NONE, mHelper.getImportance(PKG_O, UID_O)); |
| 463 | assertTrue(mHelper.canShowBadge(PKG_N_MR1, UID_N_MR1)); |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 464 | assertEquals(channel1, |
| 465 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1.getId(), false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 466 | compareChannels(channel2, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 467 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2.getId(), false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 468 | compareChannels(channel3, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 469 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3.getId(), false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 470 | |
Julia Reynolds | 13ed28b | 2018-09-21 15:20:13 -0400 | [diff] [blame] | 471 | List<NotificationChannelGroup> actualGroups = mHelper.getNotificationChannelGroups( |
| 472 | PKG_N_MR1, UID_N_MR1, false, true, false).getList(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 473 | boolean foundNcg = false; |
| 474 | for (NotificationChannelGroup actual : actualGroups) { |
| 475 | if (ncg.getId().equals(actual.getId())) { |
| 476 | foundNcg = true; |
| 477 | compareGroups(ncg, actual); |
| 478 | } else if (ncg2.getId().equals(actual.getId())) { |
| 479 | compareGroups(ncg2, actual); |
| 480 | } |
| 481 | } |
| 482 | assertTrue(foundNcg); |
| 483 | |
| 484 | boolean foundChannel2Group = false; |
| 485 | for (NotificationChannelGroup actual : actualGroups) { |
| 486 | if (channel2.getGroup().equals(actual.getChannels().get(0).getGroup())) { |
| 487 | foundChannel2Group = true; |
| 488 | break; |
| 489 | } |
| 490 | } |
| 491 | assertTrue(foundChannel2Group); |
| 492 | } |
| 493 | |
| 494 | @Test |
| 495 | public void testBackupXml_backupCanonicalizedSoundUri() throws Exception { |
| 496 | NotificationChannel channel = |
| 497 | new NotificationChannel("id", "name", IMPORTANCE_LOW); |
| 498 | channel.setSound(SOUND_URI, mAudioAttributes); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 499 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 500 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 501 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, true, |
| 502 | UserHandle.USER_SYSTEM, channel.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 503 | |
| 504 | // Testing that in restore we are given the canonical version |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 505 | loadStreamXml(baos, true, UserHandle.USER_SYSTEM); |
Philip P. Moltmann | 128b703 | 2019-09-27 08:44:12 -0700 | [diff] [blame] | 506 | verify(mTestIContentProvider).uncanonicalize(any(), any(), eq(CANONICAL_SOUND_URI)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | @Test |
| 510 | public void testRestoreXml_withExistentCanonicalizedSoundUri() throws Exception { |
| 511 | Uri localUri = Uri.parse("content://" + TEST_AUTHORITY + "/local/url"); |
| 512 | Uri canonicalBasedOnLocal = localUri.buildUpon() |
| 513 | .appendQueryParameter("title", "Test") |
| 514 | .appendQueryParameter("canonical", "1") |
| 515 | .build(); |
Dmitri Plotnikov | 7a223fb | 2020-02-14 17:04:13 -0800 | [diff] [blame] | 516 | doReturn(canonicalBasedOnLocal) |
| 517 | .when(mTestIContentProvider).canonicalize(any(), any(), eq(CANONICAL_SOUND_URI)); |
| 518 | doReturn(localUri) |
| 519 | .when(mTestIContentProvider).uncanonicalize(any(), any(), eq(CANONICAL_SOUND_URI)); |
| 520 | doReturn(localUri) |
| 521 | .when(mTestIContentProvider).uncanonicalize(any(), any(), |
| 522 | eq(canonicalBasedOnLocal)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 523 | |
| 524 | NotificationChannel channel = |
| 525 | new NotificationChannel("id", "name", IMPORTANCE_LOW); |
| 526 | channel.setSound(SOUND_URI, mAudioAttributes); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 527 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 528 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, true, |
| 529 | UserHandle.USER_SYSTEM, channel.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 530 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 531 | loadStreamXml(baos, true, UserHandle.USER_SYSTEM); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 532 | |
| 533 | NotificationChannel actualChannel = mHelper.getNotificationChannel( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 534 | PKG_N_MR1, UID_N_MR1, channel.getId(), false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 535 | assertEquals(localUri, actualChannel.getSound()); |
| 536 | } |
| 537 | |
| 538 | @Test |
| 539 | public void testRestoreXml_withNonExistentCanonicalizedSoundUri() throws Exception { |
| 540 | Thread.sleep(3000); |
Dmitri Plotnikov | 7a223fb | 2020-02-14 17:04:13 -0800 | [diff] [blame] | 541 | doReturn(null) |
| 542 | .when(mTestIContentProvider).canonicalize(any(), any(), eq(CANONICAL_SOUND_URI)); |
| 543 | doReturn(null) |
| 544 | .when(mTestIContentProvider).uncanonicalize(any(), any(), eq(CANONICAL_SOUND_URI)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 545 | |
| 546 | NotificationChannel channel = |
| 547 | new NotificationChannel("id", "name", IMPORTANCE_LOW); |
| 548 | channel.setSound(SOUND_URI, mAudioAttributes); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 549 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 550 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, true, |
| 551 | UserHandle.USER_SYSTEM, channel.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 552 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 553 | loadStreamXml(baos, true, UserHandle.USER_SYSTEM); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 554 | |
| 555 | NotificationChannel actualChannel = mHelper.getNotificationChannel( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 556 | PKG_N_MR1, UID_N_MR1, channel.getId(), false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 557 | assertEquals(Settings.System.DEFAULT_NOTIFICATION_URI, actualChannel.getSound()); |
| 558 | } |
| 559 | |
| 560 | |
| 561 | /** |
| 562 | * Although we don't make backups with uncanonicalized uris anymore, we used to, so we have to |
| 563 | * handle its restore properly. |
| 564 | */ |
| 565 | @Test |
| 566 | public void testRestoreXml_withUncanonicalizedNonLocalSoundUri() throws Exception { |
| 567 | // Not a local uncanonicalized uri, simulating that it fails to exist locally |
Dmitri Plotnikov | 7a223fb | 2020-02-14 17:04:13 -0800 | [diff] [blame] | 568 | doReturn(null) |
| 569 | .when(mTestIContentProvider).canonicalize(any(), any(), eq(SOUND_URI)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 570 | String id = "id"; |
| 571 | String backupWithUncanonicalizedSoundUri = "<ranking version=\"1\">\n" |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 572 | + "<package name=\"" + PKG_N_MR1 + "\" show_badge=\"true\">\n" |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 573 | + "<channel id=\"" + id + "\" name=\"name\" importance=\"2\" " |
| 574 | + "sound=\"" + SOUND_URI + "\" " |
| 575 | + "usage=\"6\" content_type=\"0\" flags=\"1\" show_badge=\"true\" />\n" |
| 576 | + "<channel id=\"miscellaneous\" name=\"Uncategorized\" usage=\"5\" " |
| 577 | + "content_type=\"4\" flags=\"0\" show_badge=\"true\" />\n" |
| 578 | + "</package>\n" |
| 579 | + "</ranking>\n"; |
| 580 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 581 | loadByteArrayXml( |
| 582 | backupWithUncanonicalizedSoundUri.getBytes(), true, UserHandle.USER_SYSTEM); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 583 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 584 | NotificationChannel actualChannel = mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, id, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 585 | assertEquals(Settings.System.DEFAULT_NOTIFICATION_URI, actualChannel.getSound()); |
| 586 | } |
| 587 | |
| 588 | @Test |
| 589 | public void testBackupRestoreXml_withNullSoundUri() throws Exception { |
| 590 | NotificationChannel channel = |
| 591 | new NotificationChannel("id", "name", IMPORTANCE_LOW); |
| 592 | channel.setSound(null, mAudioAttributes); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 593 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 594 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, true, |
| 595 | UserHandle.USER_SYSTEM, channel.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 596 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 597 | loadStreamXml(baos, true, UserHandle.USER_SYSTEM); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 598 | |
| 599 | NotificationChannel actualChannel = mHelper.getNotificationChannel( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 600 | PKG_N_MR1, UID_N_MR1, channel.getId(), false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 601 | assertEquals(null, actualChannel.getSound()); |
| 602 | } |
| 603 | |
| 604 | @Test |
| 605 | public void testChannelXml_backup() throws Exception { |
| 606 | NotificationChannelGroup ncg = new NotificationChannelGroup("1", "bye"); |
| 607 | NotificationChannelGroup ncg2 = new NotificationChannelGroup("2", "hello"); |
| 608 | NotificationChannel channel1 = |
| 609 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 610 | NotificationChannel channel2 = |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 611 | new NotificationChannel("id2", "name2", IMPORTANCE_HIGH); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 612 | NotificationChannel channel3 = |
| 613 | new NotificationChannel("id3", "name3", IMPORTANCE_LOW); |
| 614 | channel3.setGroup(ncg.getId()); |
| 615 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 616 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true); |
| 617 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg2, true); |
| 618 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false); |
| 619 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, false, false); |
| 620 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 621 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 622 | mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1.getId()); |
| 623 | mHelper.deleteNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg.getId()); |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 624 | assertEquals(channel2, |
| 625 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2.getId(), false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 626 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 627 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, true, |
| 628 | UserHandle.USER_SYSTEM, channel1.getId(), channel2.getId(), channel3.getId(), |
| 629 | NotificationChannel.DEFAULT_CHANNEL_ID); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 630 | mHelper.onPackagesChanged(true, UserHandle.myUserId(), new String[]{PKG_N_MR1}, new int[]{ |
| 631 | UID_N_MR1}); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 632 | |
| 633 | XmlPullParser parser = Xml.newPullParser(); |
| 634 | parser.setInput(new BufferedInputStream(new ByteArrayInputStream(baos.toByteArray())), |
| 635 | null); |
| 636 | parser.nextTag(); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 637 | mHelper.readXml(parser, true, UserHandle.USER_SYSTEM); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 638 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 639 | assertNull(mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1.getId(), false)); |
| 640 | assertNull(mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3.getId(), false)); |
| 641 | assertNull(mHelper.getNotificationChannelGroup(ncg.getId(), PKG_N_MR1, UID_N_MR1)); |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 642 | assertEquals(channel2, |
| 643 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2.getId(), false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | @Test |
| 647 | public void testChannelXml_defaultChannelLegacyApp_noUserSettings() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 648 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, false, |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 649 | UserHandle.USER_ALL, NotificationChannel.DEFAULT_CHANNEL_ID); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 650 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 651 | loadStreamXml(baos, false, UserHandle.USER_ALL); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 652 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 653 | final NotificationChannel updated = mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 654 | NotificationChannel.DEFAULT_CHANNEL_ID, false); |
| 655 | assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, updated.getImportance()); |
| 656 | assertFalse(updated.canBypassDnd()); |
| 657 | assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE, updated.getLockscreenVisibility()); |
| 658 | assertEquals(0, updated.getUserLockedFields()); |
| 659 | } |
| 660 | |
| 661 | @Test |
| 662 | public void testChannelXml_defaultChannelUpdatedApp_userSettings() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 663 | final NotificationChannel defaultChannel = mHelper.getNotificationChannel(PKG_N_MR1, |
| 664 | UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 665 | NotificationChannel.DEFAULT_CHANNEL_ID, false); |
| 666 | defaultChannel.setImportance(NotificationManager.IMPORTANCE_LOW); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 667 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, defaultChannel, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 668 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 669 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, false, |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 670 | UserHandle.USER_ALL, NotificationChannel.DEFAULT_CHANNEL_ID); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 671 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 672 | loadStreamXml(baos, false, UserHandle.USER_ALL); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 673 | |
| 674 | assertEquals(NotificationManager.IMPORTANCE_LOW, mHelper.getNotificationChannel( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 675 | PKG_N_MR1, UID_N_MR1, NotificationChannel.DEFAULT_CHANNEL_ID, false).getImportance()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | @Test |
| 679 | public void testChannelXml_upgradeCreateDefaultChannel() throws Exception { |
| 680 | final String preupgradeXml = "<ranking version=\"1\">\n" |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 681 | + "<package name=\"" + PKG_N_MR1 |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 682 | + "\" importance=\"" + NotificationManager.IMPORTANCE_HIGH |
| 683 | + "\" priority=\"" + Notification.PRIORITY_MAX + "\" visibility=\"" |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 684 | + Notification.VISIBILITY_SECRET + "\"" +" uid=\"" + UID_N_MR1 + "\" />\n" |
| 685 | + "<package name=\"" + PKG_O + "\" uid=\"" + UID_O + "\" visibility=\"" |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 686 | + Notification.VISIBILITY_PRIVATE + "\" />\n" |
| 687 | + "</ranking>"; |
| 688 | XmlPullParser parser = Xml.newPullParser(); |
| 689 | parser.setInput(new BufferedInputStream(new ByteArrayInputStream(preupgradeXml.getBytes())), |
| 690 | null); |
| 691 | parser.nextTag(); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 692 | mHelper.readXml(parser, false, UserHandle.USER_ALL); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 693 | |
| 694 | final NotificationChannel updated1 = |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 695 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, NotificationChannel.DEFAULT_CHANNEL_ID, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 696 | assertEquals(NotificationManager.IMPORTANCE_HIGH, updated1.getImportance()); |
| 697 | assertTrue(updated1.canBypassDnd()); |
| 698 | assertEquals(Notification.VISIBILITY_SECRET, updated1.getLockscreenVisibility()); |
| 699 | assertEquals(NotificationChannel.USER_LOCKED_IMPORTANCE |
| 700 | | NotificationChannel.USER_LOCKED_PRIORITY |
| 701 | | NotificationChannel.USER_LOCKED_VISIBILITY, |
| 702 | updated1.getUserLockedFields()); |
| 703 | |
| 704 | // No Default Channel created for updated packages |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 705 | assertEquals(null, mHelper.getNotificationChannel(PKG_O, UID_O, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 706 | NotificationChannel.DEFAULT_CHANNEL_ID, false)); |
| 707 | } |
| 708 | |
| 709 | @Test |
| 710 | public void testChannelXml_upgradeDeletesDefaultChannel() throws Exception { |
| 711 | final NotificationChannel defaultChannel = mHelper.getNotificationChannel( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 712 | PKG_N_MR1, UID_N_MR1, NotificationChannel.DEFAULT_CHANNEL_ID, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 713 | assertTrue(defaultChannel != null); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 714 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, false, |
| 715 | UserHandle.USER_ALL, NotificationChannel.DEFAULT_CHANNEL_ID); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 716 | // Load package at higher sdk. |
| 717 | final ApplicationInfo upgraded = new ApplicationInfo(); |
| 718 | upgraded.targetSdkVersion = Build.VERSION_CODES.N_MR1 + 1; |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 719 | when(mPm.getApplicationInfoAsUser(eq(PKG_N_MR1), anyInt(), anyInt())).thenReturn(upgraded); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 720 | loadStreamXml(baos, false, UserHandle.USER_ALL); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 721 | |
| 722 | // Default Channel should be gone. |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 723 | assertEquals(null, mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 724 | NotificationChannel.DEFAULT_CHANNEL_ID, false)); |
| 725 | } |
| 726 | |
| 727 | @Test |
| 728 | public void testDeletesDefaultChannelAfterChannelIsCreated() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 729 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 730 | new NotificationChannel("bananas", "bananas", IMPORTANCE_LOW), true, false); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 731 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, false, |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 732 | UserHandle.USER_ALL, NotificationChannel.DEFAULT_CHANNEL_ID, "bananas"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 733 | |
| 734 | // Load package at higher sdk. |
| 735 | final ApplicationInfo upgraded = new ApplicationInfo(); |
| 736 | upgraded.targetSdkVersion = Build.VERSION_CODES.N_MR1 + 1; |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 737 | when(mPm.getApplicationInfoAsUser(eq(PKG_N_MR1), anyInt(), anyInt())).thenReturn(upgraded); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 738 | loadStreamXml(baos, false, UserHandle.USER_ALL); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 739 | |
| 740 | // Default Channel should be gone. |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 741 | assertEquals(null, mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 742 | NotificationChannel.DEFAULT_CHANNEL_ID, false)); |
| 743 | } |
| 744 | |
| 745 | @Test |
| 746 | public void testLoadingOldChannelsDoesNotDeleteNewlyCreatedChannels() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 747 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, false, |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 748 | UserHandle.USER_ALL, NotificationChannel.DEFAULT_CHANNEL_ID, "bananas"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 749 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 750 | new NotificationChannel("bananas", "bananas", IMPORTANCE_LOW), true, false); |
| 751 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 752 | loadStreamXml(baos, false, UserHandle.USER_ALL); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 753 | |
| 754 | // Should still have the newly created channel that wasn't in the xml. |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 755 | assertTrue(mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "bananas", false) != null); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | @Test |
| 759 | public void testCreateChannel_blocked() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 760 | mHelper.setImportance(PKG_N_MR1, UID_N_MR1, IMPORTANCE_NONE); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 761 | |
Julia Reynolds | dafd3a4 | 2019-05-24 13:33:28 -0400 | [diff] [blame] | 762 | assertTrue(mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, |
| 763 | new NotificationChannel("bananas", "bananas", IMPORTANCE_LOW), true, false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | @Test |
| 767 | public void testCreateChannel_badImportance() throws Exception { |
| 768 | try { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 769 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 770 | new NotificationChannel("bananas", "bananas", IMPORTANCE_NONE - 1), |
| 771 | true, false); |
| 772 | fail("Was allowed to create a channel with invalid importance"); |
| 773 | } catch (IllegalArgumentException e) { |
| 774 | // yay |
| 775 | } |
| 776 | try { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 777 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 778 | new NotificationChannel("bananas", "bananas", IMPORTANCE_UNSPECIFIED), |
| 779 | true, false); |
| 780 | fail("Was allowed to create a channel with invalid importance"); |
| 781 | } catch (IllegalArgumentException e) { |
| 782 | // yay |
| 783 | } |
| 784 | try { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 785 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 786 | new NotificationChannel("bananas", "bananas", IMPORTANCE_MAX + 1), |
| 787 | true, false); |
| 788 | fail("Was allowed to create a channel with invalid importance"); |
| 789 | } catch (IllegalArgumentException e) { |
| 790 | // yay |
| 791 | } |
Julia Reynolds | dafd3a4 | 2019-05-24 13:33:28 -0400 | [diff] [blame] | 792 | assertTrue(mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, |
| 793 | new NotificationChannel("bananas", "bananas", IMPORTANCE_NONE), true, false)); |
| 794 | assertFalse(mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, |
| 795 | new NotificationChannel("bananas", "bananas", IMPORTANCE_MAX), true, false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | |
| 799 | @Test |
| 800 | public void testUpdate() throws Exception { |
| 801 | // no fields locked by user |
| 802 | final NotificationChannel channel = |
| 803 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 804 | channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes); |
| 805 | channel.enableLights(true); |
| 806 | channel.setBypassDnd(true); |
| 807 | channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 808 | |
Julia Reynolds | dafd3a4 | 2019-05-24 13:33:28 -0400 | [diff] [blame] | 809 | assertTrue(mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, false, false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 810 | |
| 811 | // same id, try to update all fields |
| 812 | final NotificationChannel channel2 = |
| 813 | new NotificationChannel("id2", "name2", NotificationManager.IMPORTANCE_HIGH); |
| 814 | channel2.setSound(new Uri.Builder().scheme("test2").build(), mAudioAttributes); |
| 815 | channel2.enableLights(false); |
| 816 | channel2.setBypassDnd(false); |
| 817 | channel2.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC); |
| 818 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 819 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 820 | |
| 821 | // all fields should be changed |
Julia Reynolds | dafd3a4 | 2019-05-24 13:33:28 -0400 | [diff] [blame] | 822 | assertEquals(channel2, |
| 823 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId(), false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 824 | |
| 825 | verify(mHandler, times(1)).requestSort(); |
| 826 | } |
| 827 | |
| 828 | @Test |
| 829 | public void testUpdate_preUpgrade_updatesAppFields() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 830 | mHelper.setImportance(PKG_N_MR1, UID_N_MR1, IMPORTANCE_UNSPECIFIED); |
| 831 | assertTrue(mHelper.canShowBadge(PKG_N_MR1, UID_N_MR1)); |
| 832 | assertEquals(Notification.PRIORITY_DEFAULT, mHelper.getPackagePriority(PKG_N_MR1, UID_N_MR1)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 833 | assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 834 | mHelper.getPackageVisibility(PKG_N_MR1, UID_N_MR1)); |
| 835 | assertFalse(mHelper.getIsAppImportanceLocked(PKG_N_MR1, UID_N_MR1)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 836 | |
| 837 | NotificationChannel defaultChannel = mHelper.getNotificationChannel( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 838 | PKG_N_MR1, UID_N_MR1, NotificationChannel.DEFAULT_CHANNEL_ID, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 839 | |
| 840 | defaultChannel.setShowBadge(false); |
| 841 | defaultChannel.setImportance(IMPORTANCE_NONE); |
| 842 | defaultChannel.setBypassDnd(true); |
| 843 | defaultChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 844 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 845 | mHelper.setAppImportanceLocked(PKG_N_MR1, UID_N_MR1); |
| 846 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, defaultChannel, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 847 | |
| 848 | // ensure app level fields are changed |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 849 | assertFalse(mHelper.canShowBadge(PKG_N_MR1, UID_N_MR1)); |
| 850 | assertEquals(Notification.PRIORITY_MAX, mHelper.getPackagePriority(PKG_N_MR1, UID_N_MR1)); |
| 851 | assertEquals(Notification.VISIBILITY_SECRET, mHelper.getPackageVisibility(PKG_N_MR1, |
| 852 | UID_N_MR1)); |
| 853 | assertEquals(IMPORTANCE_NONE, mHelper.getImportance(PKG_N_MR1, UID_N_MR1)); |
| 854 | assertTrue(mHelper.getIsAppImportanceLocked(PKG_N_MR1, UID_N_MR1)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | @Test |
| 858 | public void testUpdate_postUpgrade_noUpdateAppFields() throws Exception { |
| 859 | final NotificationChannel channel = new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 860 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 861 | mHelper.createNotificationChannel(PKG_O, UID_O, channel, false, false); |
| 862 | assertTrue(mHelper.canShowBadge(PKG_O, UID_O)); |
| 863 | assertEquals(Notification.PRIORITY_DEFAULT, mHelper.getPackagePriority(PKG_O, UID_O)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 864 | assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 865 | mHelper.getPackageVisibility(PKG_O, UID_O)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 866 | |
| 867 | channel.setShowBadge(false); |
| 868 | channel.setImportance(IMPORTANCE_NONE); |
| 869 | channel.setBypassDnd(true); |
| 870 | channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 871 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 872 | mHelper.updateNotificationChannel(PKG_O, UID_O, channel, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 873 | |
| 874 | // ensure app level fields are not changed |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 875 | assertTrue(mHelper.canShowBadge(PKG_O, UID_O)); |
| 876 | assertEquals(Notification.PRIORITY_DEFAULT, mHelper.getPackagePriority(PKG_O, UID_O)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 877 | assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 878 | mHelper.getPackageVisibility(PKG_O, UID_O)); |
| 879 | assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, mHelper.getImportance(PKG_O, |
| 880 | UID_O)); |
| 881 | assertFalse(mHelper.getIsAppImportanceLocked(PKG_O, UID_O)); |
| 882 | } |
| 883 | |
| 884 | @Test |
| 885 | public void testUpdate_preUpgrade_noUpdateAppFieldsWithMultipleChannels() throws Exception { |
| 886 | final NotificationChannel channel = new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 887 | |
| 888 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, false, false); |
| 889 | assertTrue(mHelper.canShowBadge(PKG_N_MR1, UID_N_MR1)); |
| 890 | assertEquals(Notification.PRIORITY_DEFAULT, mHelper.getPackagePriority(PKG_N_MR1, UID_N_MR1)); |
| 891 | assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE, |
| 892 | mHelper.getPackageVisibility(PKG_N_MR1, UID_N_MR1)); |
| 893 | |
| 894 | channel.setShowBadge(false); |
| 895 | channel.setImportance(IMPORTANCE_NONE); |
| 896 | channel.setBypassDnd(true); |
| 897 | channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 898 | |
| 899 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true); |
| 900 | |
| 901 | NotificationChannel defaultChannel = mHelper.getNotificationChannel( |
| 902 | PKG_N_MR1, UID_N_MR1, NotificationChannel.DEFAULT_CHANNEL_ID, false); |
| 903 | |
| 904 | defaultChannel.setShowBadge(false); |
| 905 | defaultChannel.setImportance(IMPORTANCE_NONE); |
| 906 | defaultChannel.setBypassDnd(true); |
| 907 | defaultChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 908 | |
| 909 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, defaultChannel, true); |
| 910 | |
| 911 | // ensure app level fields are not changed |
| 912 | assertTrue(mHelper.canShowBadge(PKG_N_MR1, UID_N_MR1)); |
| 913 | assertEquals(Notification.PRIORITY_DEFAULT, mHelper.getPackagePriority(PKG_N_MR1, UID_N_MR1)); |
| 914 | assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE, |
| 915 | mHelper.getPackageVisibility(PKG_N_MR1, UID_N_MR1)); |
| 916 | assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, mHelper.getImportance(PKG_N_MR1, |
| 917 | UID_N_MR1)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | @Test |
| 921 | public void testGetNotificationChannel_ReturnsNullForUnknownChannel() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 922 | assertEquals(null, mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "garbage", false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | @Test |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 926 | public void testCreateChannel_CannotChangeHiddenFields() { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 927 | final NotificationChannel channel = |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 928 | new NotificationChannel("id2", "name2", IMPORTANCE_HIGH); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 929 | channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes); |
| 930 | channel.enableLights(true); |
| 931 | channel.setBypassDnd(true); |
| 932 | channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 933 | channel.setShowBadge(true); |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 934 | channel.setAllowBubbles(false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 935 | int lockMask = 0; |
| 936 | for (int i = 0; i < NotificationChannel.LOCKABLE_FIELDS.length; i++) { |
| 937 | lockMask |= NotificationChannel.LOCKABLE_FIELDS[i]; |
| 938 | } |
| 939 | channel.lockFields(lockMask); |
| 940 | |
Julia Reynolds | dafd3a4 | 2019-05-24 13:33:28 -0400 | [diff] [blame] | 941 | assertTrue(mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 942 | |
| 943 | NotificationChannel savedChannel = |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 944 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId(), false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 945 | |
| 946 | assertEquals(channel.getName(), savedChannel.getName()); |
| 947 | assertEquals(channel.shouldShowLights(), savedChannel.shouldShowLights()); |
| 948 | assertFalse(savedChannel.canBypassDnd()); |
| 949 | assertFalse(Notification.VISIBILITY_SECRET == savedChannel.getLockscreenVisibility()); |
| 950 | assertEquals(channel.canShowBadge(), savedChannel.canShowBadge()); |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 951 | assertEquals(channel.canBubble(), savedChannel.canBubble()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 952 | |
| 953 | verify(mHandler, never()).requestSort(); |
| 954 | } |
| 955 | |
| 956 | @Test |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 957 | public void testCreateChannel_CannotChangeHiddenFieldsAssistant() { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 958 | final NotificationChannel channel = |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 959 | new NotificationChannel("id2", "name2", IMPORTANCE_HIGH); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 960 | channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes); |
| 961 | channel.enableLights(true); |
| 962 | channel.setBypassDnd(true); |
| 963 | channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 964 | channel.setShowBadge(true); |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 965 | channel.setAllowBubbles(false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 966 | int lockMask = 0; |
| 967 | for (int i = 0; i < NotificationChannel.LOCKABLE_FIELDS.length; i++) { |
| 968 | lockMask |= NotificationChannel.LOCKABLE_FIELDS[i]; |
| 969 | } |
| 970 | channel.lockFields(lockMask); |
| 971 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 972 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 973 | |
| 974 | NotificationChannel savedChannel = |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 975 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId(), false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 976 | |
| 977 | assertEquals(channel.getName(), savedChannel.getName()); |
| 978 | assertEquals(channel.shouldShowLights(), savedChannel.shouldShowLights()); |
| 979 | assertFalse(savedChannel.canBypassDnd()); |
| 980 | assertFalse(Notification.VISIBILITY_SECRET == savedChannel.getLockscreenVisibility()); |
| 981 | assertEquals(channel.canShowBadge(), savedChannel.canShowBadge()); |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 982 | assertEquals(channel.canBubble(), savedChannel.canBubble()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | @Test |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 986 | public void testClearLockedFields() { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 987 | final NotificationChannel channel = getChannel(); |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 988 | mHelper.clearLockedFieldsLocked(channel); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 989 | assertEquals(0, channel.getUserLockedFields()); |
| 990 | |
| 991 | channel.lockFields(NotificationChannel.USER_LOCKED_PRIORITY |
| 992 | | NotificationChannel.USER_LOCKED_IMPORTANCE); |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 993 | mHelper.clearLockedFieldsLocked(channel); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 994 | assertEquals(0, channel.getUserLockedFields()); |
| 995 | } |
| 996 | |
| 997 | @Test |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 998 | public void testLockFields_soundAndVibration() { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 999 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel(), true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1000 | |
| 1001 | final NotificationChannel update1 = getChannel(); |
| 1002 | update1.setSound(new Uri.Builder().scheme("test").build(), |
| 1003 | new AudioAttributes.Builder().build()); |
| 1004 | update1.lockFields(NotificationChannel.USER_LOCKED_PRIORITY); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1005 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update1, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1006 | assertEquals(NotificationChannel.USER_LOCKED_PRIORITY |
| 1007 | | NotificationChannel.USER_LOCKED_SOUND, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1008 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update1.getId(), false) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1009 | .getUserLockedFields()); |
| 1010 | |
| 1011 | NotificationChannel update2 = getChannel(); |
| 1012 | update2.enableVibration(true); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1013 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update2, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1014 | assertEquals(NotificationChannel.USER_LOCKED_PRIORITY |
| 1015 | | NotificationChannel.USER_LOCKED_SOUND |
| 1016 | | NotificationChannel.USER_LOCKED_VIBRATION, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1017 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update2.getId(), false) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1018 | .getUserLockedFields()); |
| 1019 | } |
| 1020 | |
| 1021 | @Test |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1022 | public void testLockFields_vibrationAndLights() { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1023 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel(), true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1024 | |
| 1025 | final NotificationChannel update1 = getChannel(); |
| 1026 | update1.setVibrationPattern(new long[]{7945, 46 ,246}); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1027 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update1, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1028 | assertEquals(NotificationChannel.USER_LOCKED_VIBRATION, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1029 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update1.getId(), false) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1030 | .getUserLockedFields()); |
| 1031 | |
| 1032 | final NotificationChannel update2 = getChannel(); |
| 1033 | update2.enableLights(true); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1034 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update2, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1035 | assertEquals(NotificationChannel.USER_LOCKED_VIBRATION |
| 1036 | | NotificationChannel.USER_LOCKED_LIGHTS, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1037 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update2.getId(), false) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1038 | .getUserLockedFields()); |
| 1039 | } |
| 1040 | |
| 1041 | @Test |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1042 | public void testLockFields_lightsAndImportance() { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1043 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel(), true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1044 | |
| 1045 | final NotificationChannel update1 = getChannel(); |
| 1046 | update1.setLightColor(Color.GREEN); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1047 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update1, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1048 | assertEquals(NotificationChannel.USER_LOCKED_LIGHTS, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1049 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update1.getId(), false) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1050 | .getUserLockedFields()); |
| 1051 | |
| 1052 | final NotificationChannel update2 = getChannel(); |
| 1053 | update2.setImportance(IMPORTANCE_DEFAULT); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1054 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update2, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1055 | assertEquals(NotificationChannel.USER_LOCKED_LIGHTS |
| 1056 | | NotificationChannel.USER_LOCKED_IMPORTANCE, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1057 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update2.getId(), false) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1058 | .getUserLockedFields()); |
| 1059 | } |
| 1060 | |
| 1061 | @Test |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1062 | public void testLockFields_visibilityAndDndAndBadge() { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1063 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel(), true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1064 | assertEquals(0, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1065 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel().getId(), false) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1066 | .getUserLockedFields()); |
| 1067 | |
| 1068 | final NotificationChannel update1 = getChannel(); |
| 1069 | update1.setBypassDnd(true); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1070 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update1, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1071 | assertEquals(NotificationChannel.USER_LOCKED_PRIORITY, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1072 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update1.getId(), false) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1073 | .getUserLockedFields()); |
| 1074 | |
| 1075 | final NotificationChannel update2 = getChannel(); |
| 1076 | update2.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1077 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update2, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1078 | assertEquals(NotificationChannel.USER_LOCKED_PRIORITY |
| 1079 | | NotificationChannel.USER_LOCKED_VISIBILITY, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1080 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update2.getId(), false) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1081 | .getUserLockedFields()); |
| 1082 | |
| 1083 | final NotificationChannel update3 = getChannel(); |
| 1084 | update3.setShowBadge(false); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1085 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update3, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1086 | assertEquals(NotificationChannel.USER_LOCKED_PRIORITY |
| 1087 | | NotificationChannel.USER_LOCKED_VISIBILITY |
| 1088 | | NotificationChannel.USER_LOCKED_SHOW_BADGE, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1089 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update3.getId(), false) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1090 | .getUserLockedFields()); |
| 1091 | } |
| 1092 | |
| 1093 | @Test |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 1094 | public void testLockFields_allowBubble() { |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1095 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel(), true, false); |
| 1096 | assertEquals(0, |
| 1097 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel().getId(), false) |
| 1098 | .getUserLockedFields()); |
| 1099 | |
| 1100 | final NotificationChannel update = getChannel(); |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 1101 | update.setAllowBubbles(true); |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1102 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update, true); |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 1103 | assertEquals(NotificationChannel.USER_LOCKED_ALLOW_BUBBLE, |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1104 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update.getId(), false) |
| 1105 | .getUserLockedFields()); |
| 1106 | } |
| 1107 | |
| 1108 | @Test |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1109 | public void testDeleteNonExistentChannel() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1110 | mHelper.deleteNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, "does not exist"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | @Test |
Will Brockman | 23db6d4 | 2020-02-28 09:51:12 -0500 | [diff] [blame] | 1114 | public void testDoubleDeleteChannel() throws Exception { |
| 1115 | NotificationChannel channel = getChannel(); |
| 1116 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
| 1117 | mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId()); |
| 1118 | mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId()); |
| 1119 | assertEquals(2, mLogger.getCalls().size()); |
| 1120 | assertEquals( |
| 1121 | NotificationChannelLogger.NotificationChannelEvent.NOTIFICATION_CHANNEL_CREATED, |
| 1122 | mLogger.get(0).event); |
| 1123 | assertEquals( |
| 1124 | NotificationChannelLogger.NotificationChannelEvent.NOTIFICATION_CHANNEL_DELETED, |
| 1125 | mLogger.get(1).event); |
| 1126 | // No log for the second delete of the same channel. |
| 1127 | } |
| 1128 | |
| 1129 | @Test |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1130 | public void testGetDeletedChannel() throws Exception { |
| 1131 | NotificationChannel channel = getChannel(); |
| 1132 | channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes); |
| 1133 | channel.enableLights(true); |
| 1134 | channel.setBypassDnd(true); |
| 1135 | channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 1136 | channel.enableVibration(true); |
| 1137 | channel.setVibrationPattern(new long[]{100, 67, 145, 156}); |
| 1138 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1139 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
| 1140 | mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1141 | |
| 1142 | // Does not return deleted channel |
| 1143 | NotificationChannel response = |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1144 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId(), false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1145 | assertNull(response); |
| 1146 | |
| 1147 | // Returns deleted channel |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1148 | response = mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId(), true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1149 | compareChannels(channel, response); |
| 1150 | assertTrue(response.isDeleted()); |
| 1151 | } |
| 1152 | |
| 1153 | @Test |
| 1154 | public void testGetDeletedChannels() throws Exception { |
| 1155 | Map<String, NotificationChannel> channelMap = new HashMap<>(); |
| 1156 | NotificationChannel channel = |
| 1157 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 1158 | channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes); |
| 1159 | channel.enableLights(true); |
| 1160 | channel.setBypassDnd(true); |
| 1161 | channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); |
| 1162 | channel.enableVibration(true); |
| 1163 | channel.setVibrationPattern(new long[]{100, 67, 145, 156}); |
| 1164 | channelMap.put(channel.getId(), channel); |
| 1165 | NotificationChannel channel2 = |
| 1166 | new NotificationChannel("id4", "a", NotificationManager.IMPORTANCE_HIGH); |
| 1167 | channelMap.put(channel2.getId(), channel2); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1168 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
| 1169 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1170 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1171 | mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1172 | |
| 1173 | // Returns only non-deleted channels |
| 1174 | List<NotificationChannel> channels = |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1175 | mHelper.getNotificationChannels(PKG_N_MR1, UID_N_MR1, false).getList(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1176 | assertEquals(2, channels.size()); // Default channel + non-deleted channel |
| 1177 | for (NotificationChannel nc : channels) { |
| 1178 | if (!NotificationChannel.DEFAULT_CHANNEL_ID.equals(nc.getId())) { |
| 1179 | compareChannels(channel2, nc); |
| 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | // Returns deleted channels too |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1184 | channels = mHelper.getNotificationChannels(PKG_N_MR1, UID_N_MR1, true).getList(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1185 | assertEquals(3, channels.size()); // Includes default channel |
| 1186 | for (NotificationChannel nc : channels) { |
| 1187 | if (!NotificationChannel.DEFAULT_CHANNEL_ID.equals(nc.getId())) { |
| 1188 | compareChannels(channelMap.get(nc.getId()), nc); |
| 1189 | } |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | @Test |
| 1194 | public void testGetDeletedChannelCount() throws Exception { |
| 1195 | NotificationChannel channel = |
| 1196 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 1197 | NotificationChannel channel2 = |
| 1198 | new NotificationChannel("id4", "a", NotificationManager.IMPORTANCE_HIGH); |
| 1199 | NotificationChannel channel3 = |
| 1200 | new NotificationChannel("id5", "a", NotificationManager.IMPORTANCE_HIGH); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1201 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
| 1202 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, true, false); |
| 1203 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1204 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1205 | mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId()); |
| 1206 | mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1207 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1208 | assertEquals(2, mHelper.getDeletedChannelCount(PKG_N_MR1, UID_N_MR1)); |
| 1209 | assertEquals(0, mHelper.getDeletedChannelCount("pkg2", UID_O)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1210 | } |
| 1211 | |
| 1212 | @Test |
| 1213 | public void testGetBlockedChannelCount() throws Exception { |
| 1214 | NotificationChannel channel = |
| 1215 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 1216 | NotificationChannel channel2 = |
| 1217 | new NotificationChannel("id4", "a", NotificationManager.IMPORTANCE_NONE); |
| 1218 | NotificationChannel channel3 = |
| 1219 | new NotificationChannel("id5", "a", NotificationManager.IMPORTANCE_NONE); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1220 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
| 1221 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, true, false); |
| 1222 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1223 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1224 | mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1225 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1226 | assertEquals(1, mHelper.getBlockedChannelCount(PKG_N_MR1, UID_N_MR1)); |
| 1227 | assertEquals(0, mHelper.getBlockedChannelCount("pkg2", UID_O)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | @Test |
Beverly | 4f7b53d | 2018-11-20 09:56:31 -0500 | [diff] [blame] | 1231 | public void testUpdateChannelsBypassingDnd_onUserSwitch_onUserUnlocked() throws Exception { |
| 1232 | int user = USER.getIdentifier(); |
| 1233 | NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1"); |
| 1234 | NotificationChannel channel1 = new NotificationChannel("id1", "name1", |
| 1235 | NotificationManager.IMPORTANCE_MAX); |
| 1236 | channel1.setBypassDnd(true); |
| 1237 | channel1.setGroup(ncg.getId()); |
| 1238 | |
| 1239 | // channel is associated with a group, then group is deleted |
| 1240 | mHelper.createNotificationChannelGroup(PKG_N_MR1, user, ncg, /* fromTargetApp */ true); |
| 1241 | mHelper.createNotificationChannel(PKG_N_MR1, user, channel1, true, /*has DND access*/ true); |
| 1242 | mHelper.deleteNotificationChannelGroup(PKG_N_MR1, user, ncg.getId()); |
| 1243 | |
| 1244 | mHelper.onUserSwitched(user); |
| 1245 | mHelper.onUserUnlocked(user); |
| 1246 | } |
| 1247 | |
| 1248 | @Test |
Beverly | 0479cde2 | 2018-11-09 11:05:34 -0500 | [diff] [blame] | 1249 | public void testGetChannelsBypassingDndCount_noChannelsBypassing() throws Exception { |
| 1250 | assertEquals(0, mHelper.getNotificationChannelsBypassingDnd(PKG_N_MR1, |
| 1251 | USER.getIdentifier()).getList().size()); |
| 1252 | } |
| 1253 | |
| 1254 | @Test |
| 1255 | public void testGetChannelsBypassingDnd_noChannelsForUserIdBypassing() |
| 1256 | throws Exception { |
| 1257 | int user = 9; |
| 1258 | NotificationChannel channel = new NotificationChannel("id", "name", |
| 1259 | NotificationManager.IMPORTANCE_MAX); |
| 1260 | channel.setBypassDnd(true); |
| 1261 | mHelper.createNotificationChannel(PKG_N_MR1, 111, channel, true, true); |
| 1262 | |
| 1263 | assertEquals(0, mHelper.getNotificationChannelsBypassingDnd(PKG_N_MR1, |
| 1264 | user).getList().size()); |
| 1265 | } |
| 1266 | |
| 1267 | @Test |
| 1268 | public void testGetChannelsBypassingDndCount_oneChannelBypassing_groupBlocked() { |
| 1269 | int user = USER.getIdentifier(); |
| 1270 | NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1"); |
| 1271 | NotificationChannel channel1 = new NotificationChannel("id1", "name1", |
| 1272 | NotificationManager.IMPORTANCE_MAX); |
| 1273 | channel1.setBypassDnd(true); |
| 1274 | channel1.setGroup(ncg.getId()); |
| 1275 | mHelper.createNotificationChannelGroup(PKG_N_MR1, user, ncg, /* fromTargetApp */ true); |
| 1276 | mHelper.createNotificationChannel(PKG_N_MR1, user, channel1, true, /*has DND access*/ true); |
| 1277 | |
| 1278 | assertEquals(1, mHelper.getNotificationChannelsBypassingDnd(PKG_N_MR1, |
| 1279 | user).getList().size()); |
| 1280 | |
| 1281 | // disable group |
| 1282 | ncg.setBlocked(true); |
| 1283 | mHelper.createNotificationChannelGroup(PKG_N_MR1, user, ncg, /* fromTargetApp */ false); |
| 1284 | assertEquals(0, mHelper.getNotificationChannelsBypassingDnd(PKG_N_MR1, |
| 1285 | user).getList().size()); |
| 1286 | } |
| 1287 | |
| 1288 | @Test |
| 1289 | public void testGetChannelsBypassingDndCount_multipleChannelsBypassing() { |
| 1290 | int user = USER.getIdentifier(); |
| 1291 | NotificationChannel channel1 = new NotificationChannel("id1", "name1", |
| 1292 | NotificationManager.IMPORTANCE_MAX); |
| 1293 | NotificationChannel channel2 = new NotificationChannel("id2", "name2", |
| 1294 | NotificationManager.IMPORTANCE_MAX); |
| 1295 | NotificationChannel channel3 = new NotificationChannel("id3", "name3", |
| 1296 | NotificationManager.IMPORTANCE_MAX); |
| 1297 | channel1.setBypassDnd(true); |
| 1298 | channel2.setBypassDnd(true); |
| 1299 | channel3.setBypassDnd(true); |
| 1300 | // has DND access, so can set bypassDnd attribute |
| 1301 | mHelper.createNotificationChannel(PKG_N_MR1, user, channel1, true, /*has DND access*/ true); |
| 1302 | mHelper.createNotificationChannel(PKG_N_MR1, user, channel2, true, true); |
| 1303 | mHelper.createNotificationChannel(PKG_N_MR1, user, channel3, true, true); |
| 1304 | assertEquals(3, mHelper.getNotificationChannelsBypassingDnd(PKG_N_MR1, |
| 1305 | user).getList().size()); |
| 1306 | |
| 1307 | // block notifications from this app |
| 1308 | mHelper.setEnabled(PKG_N_MR1, user, false); |
| 1309 | assertEquals(0, mHelper.getNotificationChannelsBypassingDnd(PKG_N_MR1, |
| 1310 | user).getList().size()); |
| 1311 | |
| 1312 | // re-enable notifications from this app |
| 1313 | mHelper.setEnabled(PKG_N_MR1, user, true); |
| 1314 | assertEquals(3, mHelper.getNotificationChannelsBypassingDnd(PKG_N_MR1, |
| 1315 | user).getList().size()); |
| 1316 | |
| 1317 | // setBypassDnd false for some channels |
| 1318 | channel1.setBypassDnd(false); |
| 1319 | channel2.setBypassDnd(false); |
| 1320 | assertEquals(1, mHelper.getNotificationChannelsBypassingDnd(PKG_N_MR1, |
| 1321 | user).getList().size()); |
| 1322 | |
| 1323 | // setBypassDnd false for rest of the channels |
| 1324 | channel3.setBypassDnd(false); |
| 1325 | assertEquals(0, mHelper.getNotificationChannelsBypassingDnd(PKG_N_MR1, |
| 1326 | user).getList().size()); |
| 1327 | } |
| 1328 | |
| 1329 | @Test |
| 1330 | public void testGetAppsBypassingDndCount_noAppsBypassing() throws Exception { |
| 1331 | assertEquals(0, mHelper.getAppsBypassingDndCount(USER.getIdentifier())); |
| 1332 | } |
| 1333 | |
| 1334 | @Test |
| 1335 | public void testGetAppsBypassingDndCount_noAppsForUserIdBypassing() throws Exception { |
| 1336 | int user = 9; |
| 1337 | NotificationChannel channel = new NotificationChannel("id", "name", |
| 1338 | NotificationManager.IMPORTANCE_MAX); |
| 1339 | channel.setBypassDnd(true); |
| 1340 | mHelper.createNotificationChannel(PKG_N_MR1, 111, channel, true, true); |
| 1341 | |
| 1342 | assertEquals(0, mHelper.getAppsBypassingDndCount(user)); |
| 1343 | } |
| 1344 | |
| 1345 | @Test |
| 1346 | public void testGetAppsBypassingDndCount_oneChannelBypassing_groupBlocked() { |
| 1347 | int user = USER.getIdentifier(); |
| 1348 | NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1"); |
| 1349 | NotificationChannel channel1 = new NotificationChannel("id1", "name1", |
| 1350 | NotificationManager.IMPORTANCE_MAX); |
| 1351 | channel1.setBypassDnd(true); |
| 1352 | channel1.setGroup(ncg.getId()); |
| 1353 | mHelper.createNotificationChannelGroup(PKG_N_MR1, user, ncg, /* fromTargetApp */ true); |
| 1354 | mHelper.createNotificationChannel(PKG_N_MR1, user, channel1, true, /*has DND access*/ true); |
| 1355 | |
| 1356 | assertEquals(1, mHelper.getAppsBypassingDndCount(user)); |
| 1357 | |
| 1358 | // disable group |
| 1359 | ncg.setBlocked(true); |
| 1360 | mHelper.createNotificationChannelGroup(PKG_N_MR1, user, ncg, /* fromTargetApp */ false); |
| 1361 | assertEquals(0, mHelper.getAppsBypassingDndCount(user)); |
| 1362 | } |
| 1363 | |
| 1364 | @Test |
| 1365 | public void testGetAppsBypassingDndCount_oneAppBypassing() { |
| 1366 | int user = USER.getIdentifier(); |
| 1367 | NotificationChannel channel1 = new NotificationChannel("id1", "name1", |
| 1368 | NotificationManager.IMPORTANCE_MAX); |
| 1369 | NotificationChannel channel2 = new NotificationChannel("id2", "name2", |
| 1370 | NotificationManager.IMPORTANCE_MAX); |
| 1371 | NotificationChannel channel3 = new NotificationChannel("id3", "name3", |
| 1372 | NotificationManager.IMPORTANCE_MAX); |
| 1373 | channel1.setBypassDnd(true); |
| 1374 | channel2.setBypassDnd(true); |
| 1375 | channel3.setBypassDnd(true); |
| 1376 | // has DND access, so can set bypassDnd attribute |
| 1377 | mHelper.createNotificationChannel(PKG_N_MR1, user, channel1, true, /*has DND access*/ true); |
| 1378 | mHelper.createNotificationChannel(PKG_N_MR1, user, channel2, true, true); |
| 1379 | mHelper.createNotificationChannel(PKG_N_MR1, user, channel3, true, true); |
| 1380 | assertEquals(1, mHelper.getAppsBypassingDndCount(user)); |
| 1381 | |
| 1382 | // block notifications from this app |
| 1383 | mHelper.setEnabled(PKG_N_MR1, user, false); |
| 1384 | assertEquals(0, mHelper.getAppsBypassingDndCount(user)); // no apps can bypass dnd |
| 1385 | |
| 1386 | // re-enable notifications from this app |
| 1387 | mHelper.setEnabled(PKG_N_MR1, user, true); |
| 1388 | assertEquals(1, mHelper.getAppsBypassingDndCount(user)); |
| 1389 | |
| 1390 | // setBypassDnd false for some channels |
| 1391 | channel1.setBypassDnd(false); |
| 1392 | channel2.setBypassDnd(false); |
| 1393 | assertEquals(1, mHelper.getAppsBypassingDndCount(user)); |
| 1394 | |
| 1395 | // setBypassDnd false for rest of the channels |
| 1396 | channel3.setBypassDnd(false); |
| 1397 | assertEquals(0, mHelper.getAppsBypassingDndCount(user)); |
| 1398 | } |
| 1399 | |
| 1400 | @Test |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1401 | public void testCreateAndDeleteCanChannelsBypassDnd() throws Exception { |
| 1402 | // create notification channel that can't bypass dnd |
| 1403 | // expected result: areChannelsBypassingDnd = false |
| 1404 | // setNotificationPolicy isn't called since areChannelsBypassingDnd was already false |
| 1405 | NotificationChannel channel = new NotificationChannel("id1", "name1", IMPORTANCE_LOW); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1406 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1407 | assertFalse(mHelper.areChannelsBypassingDnd()); |
| 1408 | verify(mMockZenModeHelper, never()).setNotificationPolicy(any()); |
| 1409 | resetZenModeHelper(); |
| 1410 | |
| 1411 | // create notification channel that can bypass dnd |
| 1412 | // expected result: areChannelsBypassingDnd = true |
| 1413 | NotificationChannel channel2 = new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 1414 | channel2.setBypassDnd(true); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1415 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, true, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1416 | assertTrue(mHelper.areChannelsBypassingDnd()); |
| 1417 | verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any()); |
| 1418 | resetZenModeHelper(); |
| 1419 | |
| 1420 | // delete channels |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1421 | mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1422 | assertTrue(mHelper.areChannelsBypassingDnd()); // channel2 can still bypass DND |
| 1423 | verify(mMockZenModeHelper, never()).setNotificationPolicy(any()); |
| 1424 | resetZenModeHelper(); |
| 1425 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1426 | mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1427 | assertFalse(mHelper.areChannelsBypassingDnd()); |
| 1428 | verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any()); |
| 1429 | resetZenModeHelper(); |
| 1430 | } |
| 1431 | |
| 1432 | @Test |
| 1433 | public void testUpdateCanChannelsBypassDnd() throws Exception { |
| 1434 | // create notification channel that can't bypass dnd |
| 1435 | // expected result: areChannelsBypassingDnd = false |
| 1436 | // setNotificationPolicy isn't called since areChannelsBypassingDnd was already false |
| 1437 | NotificationChannel channel = new NotificationChannel("id1", "name1", IMPORTANCE_LOW); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1438 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1439 | assertFalse(mHelper.areChannelsBypassingDnd()); |
| 1440 | verify(mMockZenModeHelper, never()).setNotificationPolicy(any()); |
| 1441 | resetZenModeHelper(); |
| 1442 | |
| 1443 | // update channel so it CAN bypass dnd: |
| 1444 | // expected result: areChannelsBypassingDnd = true |
| 1445 | channel.setBypassDnd(true); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1446 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1447 | assertTrue(mHelper.areChannelsBypassingDnd()); |
| 1448 | verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any()); |
| 1449 | resetZenModeHelper(); |
| 1450 | |
| 1451 | // update channel so it can't bypass dnd: |
| 1452 | // expected result: areChannelsBypassingDnd = false |
| 1453 | channel.setBypassDnd(false); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1454 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1455 | assertFalse(mHelper.areChannelsBypassingDnd()); |
| 1456 | verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any()); |
| 1457 | resetZenModeHelper(); |
| 1458 | } |
| 1459 | |
| 1460 | @Test |
| 1461 | public void testSetupNewZenModeHelper_canBypass() { |
| 1462 | // start notification policy off with mAreChannelsBypassingDnd = true, but |
| 1463 | // RankingHelper should change to false |
| 1464 | mTestNotificationPolicy = new NotificationManager.Policy(0, 0, 0, 0, |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1465 | NotificationManager.Policy.STATE_CHANNELS_BYPASSING_DND, 0); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1466 | when(mMockZenModeHelper.getNotificationPolicy()).thenReturn(mTestNotificationPolicy); |
Will Brockman | 23db6d4 | 2020-02-28 09:51:12 -0500 | [diff] [blame] | 1467 | mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper, mLogger); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1468 | assertFalse(mHelper.areChannelsBypassingDnd()); |
| 1469 | verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any()); |
| 1470 | resetZenModeHelper(); |
| 1471 | } |
| 1472 | |
| 1473 | @Test |
| 1474 | public void testSetupNewZenModeHelper_cannotBypass() { |
| 1475 | // start notification policy off with mAreChannelsBypassingDnd = false |
Julia Reynolds | 24edc00 | 2020-01-29 16:35:32 -0500 | [diff] [blame] | 1476 | mTestNotificationPolicy = new NotificationManager.Policy(0, 0, 0, 0, 0, 0); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1477 | when(mMockZenModeHelper.getNotificationPolicy()).thenReturn(mTestNotificationPolicy); |
Will Brockman | 23db6d4 | 2020-02-28 09:51:12 -0500 | [diff] [blame] | 1478 | mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper, mLogger); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1479 | assertFalse(mHelper.areChannelsBypassingDnd()); |
| 1480 | verify(mMockZenModeHelper, never()).setNotificationPolicy(any()); |
| 1481 | resetZenModeHelper(); |
| 1482 | } |
| 1483 | |
| 1484 | @Test |
| 1485 | public void testCreateDeletedChannel() throws Exception { |
| 1486 | long[] vibration = new long[]{100, 67, 145, 156}; |
| 1487 | NotificationChannel channel = |
| 1488 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 1489 | channel.setVibrationPattern(vibration); |
| 1490 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1491 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
| 1492 | mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1493 | |
| 1494 | NotificationChannel newChannel = new NotificationChannel( |
| 1495 | channel.getId(), channel.getName(), NotificationManager.IMPORTANCE_HIGH); |
| 1496 | newChannel.setVibrationPattern(new long[]{100}); |
| 1497 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1498 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, newChannel, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1499 | |
| 1500 | // No long deleted, using old settings |
| 1501 | compareChannels(channel, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1502 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, newChannel.getId(), false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | @Test |
| 1506 | public void testOnlyHasDefaultChannel() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1507 | assertTrue(mHelper.onlyHasDefaultChannel(PKG_N_MR1, UID_N_MR1)); |
| 1508 | assertFalse(mHelper.onlyHasDefaultChannel(PKG_O, UID_O)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1509 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1510 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel(), true, false); |
| 1511 | assertFalse(mHelper.onlyHasDefaultChannel(PKG_N_MR1, UID_N_MR1)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | @Test |
| 1515 | public void testCreateChannel_defaultChannelId() throws Exception { |
| 1516 | try { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1517 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, new NotificationChannel( |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1518 | NotificationChannel.DEFAULT_CHANNEL_ID, "ha", IMPORTANCE_HIGH), true, false); |
| 1519 | fail("Allowed to create default channel"); |
| 1520 | } catch (IllegalArgumentException e) { |
| 1521 | // pass |
| 1522 | } |
| 1523 | } |
| 1524 | |
| 1525 | @Test |
| 1526 | public void testCreateChannel_alreadyExists() throws Exception { |
| 1527 | long[] vibration = new long[]{100, 67, 145, 156}; |
| 1528 | NotificationChannel channel = |
| 1529 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 1530 | channel.setVibrationPattern(vibration); |
| 1531 | |
Julia Reynolds | dafd3a4 | 2019-05-24 13:33:28 -0400 | [diff] [blame] | 1532 | assertTrue(mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1533 | |
| 1534 | NotificationChannel newChannel = new NotificationChannel( |
| 1535 | channel.getId(), channel.getName(), NotificationManager.IMPORTANCE_HIGH); |
| 1536 | newChannel.setVibrationPattern(new long[]{100}); |
Julia Reynolds | dafd3a4 | 2019-05-24 13:33:28 -0400 | [diff] [blame] | 1537 | newChannel.setAllowBubbles(!channel.canBubble()); |
| 1538 | newChannel.setLightColor(Color.BLUE); |
| 1539 | newChannel.setSound(Uri.EMPTY, null); |
| 1540 | newChannel.setShowBadge(!channel.canShowBadge()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1541 | |
Julia Reynolds | dafd3a4 | 2019-05-24 13:33:28 -0400 | [diff] [blame] | 1542 | assertFalse(mHelper.createNotificationChannel( |
| 1543 | PKG_N_MR1, UID_N_MR1, newChannel, true, false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1544 | |
| 1545 | // Old settings not overridden |
| 1546 | compareChannels(channel, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1547 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, newChannel.getId(), false)); |
Will Brockman | 23db6d4 | 2020-02-28 09:51:12 -0500 | [diff] [blame] | 1548 | |
| 1549 | assertEquals(1, mLogger.getCalls().size()); |
| 1550 | assertEquals( |
| 1551 | NotificationChannelLogger.NotificationChannelEvent.NOTIFICATION_CHANNEL_CREATED, |
| 1552 | mLogger.get(0).event); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1553 | } |
| 1554 | |
| 1555 | @Test |
| 1556 | public void testCreateChannel_noOverrideSound() throws Exception { |
| 1557 | Uri sound = new Uri.Builder().scheme("test").build(); |
| 1558 | final NotificationChannel channel = new NotificationChannel("id2", "name2", |
| 1559 | NotificationManager.IMPORTANCE_DEFAULT); |
| 1560 | channel.setSound(sound, mAudioAttributes); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1561 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1562 | assertEquals(sound, mHelper.getNotificationChannel( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1563 | PKG_N_MR1, UID_N_MR1, channel.getId(), false).getSound()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1564 | } |
| 1565 | |
| 1566 | @Test |
| 1567 | public void testPermanentlyDeleteChannels() throws Exception { |
| 1568 | NotificationChannel channel1 = |
| 1569 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 1570 | NotificationChannel channel2 = |
| 1571 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 1572 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1573 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false); |
| 1574 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, false, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1575 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1576 | mHelper.permanentlyDeleteNotificationChannels(PKG_N_MR1, UID_N_MR1); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1577 | |
| 1578 | // Only default channel remains |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1579 | assertEquals(1, mHelper.getNotificationChannels(PKG_N_MR1, UID_N_MR1, true).getList().size()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1580 | } |
| 1581 | |
| 1582 | @Test |
| 1583 | public void testDeleteGroup() throws Exception { |
| 1584 | NotificationChannelGroup notDeleted = new NotificationChannelGroup("not", "deleted"); |
| 1585 | NotificationChannelGroup deleted = new NotificationChannelGroup("totally", "deleted"); |
| 1586 | NotificationChannel nonGroupedNonDeletedChannel = |
| 1587 | new NotificationChannel("no group", "so not deleted", IMPORTANCE_HIGH); |
| 1588 | NotificationChannel groupedButNotDeleted = |
| 1589 | new NotificationChannel("not deleted", "belongs to notDeleted", IMPORTANCE_DEFAULT); |
| 1590 | groupedButNotDeleted.setGroup("not"); |
| 1591 | NotificationChannel groupedAndDeleted = |
| 1592 | new NotificationChannel("deleted", "belongs to deleted", IMPORTANCE_DEFAULT); |
| 1593 | groupedAndDeleted.setGroup("totally"); |
| 1594 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1595 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, notDeleted, true); |
| 1596 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, deleted, true); |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1597 | mHelper.createNotificationChannel( |
| 1598 | PKG_N_MR1, UID_N_MR1, nonGroupedNonDeletedChannel, true, false); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1599 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, groupedAndDeleted, true, false); |
| 1600 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, groupedButNotDeleted, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1601 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1602 | mHelper.deleteNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, deleted.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1603 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1604 | assertNull(mHelper.getNotificationChannelGroup(deleted.getId(), PKG_N_MR1, UID_N_MR1)); |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1605 | assertNotNull( |
| 1606 | mHelper.getNotificationChannelGroup(notDeleted.getId(), PKG_N_MR1, UID_N_MR1)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1607 | |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1608 | assertNull(mHelper.getNotificationChannel( |
| 1609 | PKG_N_MR1, UID_N_MR1, groupedAndDeleted.getId(), false)); |
| 1610 | compareChannels(groupedAndDeleted, mHelper.getNotificationChannel( |
| 1611 | PKG_N_MR1, UID_N_MR1, groupedAndDeleted.getId(), true)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1612 | |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1613 | compareChannels(groupedButNotDeleted, mHelper.getNotificationChannel( |
| 1614 | PKG_N_MR1, UID_N_MR1, groupedButNotDeleted.getId(), false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1615 | compareChannels(nonGroupedNonDeletedChannel, mHelper.getNotificationChannel( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1616 | PKG_N_MR1, UID_N_MR1, nonGroupedNonDeletedChannel.getId(), false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1617 | |
| 1618 | // notDeleted |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1619 | assertEquals(1, mHelper.getNotificationChannelGroups(PKG_N_MR1, UID_N_MR1).size()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1620 | |
| 1621 | verify(mHandler, never()).requestSort(); |
Will Brockman | 23db6d4 | 2020-02-28 09:51:12 -0500 | [diff] [blame] | 1622 | |
| 1623 | assertEquals(7, mLogger.getCalls().size()); |
| 1624 | assertEquals( |
| 1625 | NotificationChannelLogger.NotificationChannelEvent |
| 1626 | .NOTIFICATION_CHANNEL_GROUP_DELETED, |
| 1627 | mLogger.get(5).event); // Next-to-last log is the deletion of the channel group. |
| 1628 | assertEquals( |
| 1629 | NotificationChannelLogger.NotificationChannelEvent |
| 1630 | .NOTIFICATION_CHANNEL_DELETED, |
| 1631 | mLogger.get(6).event); // Final log is the deletion of the channel. |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1632 | } |
| 1633 | |
| 1634 | @Test |
| 1635 | public void testOnUserRemoved() throws Exception { |
| 1636 | int[] user0Uids = {98, 235, 16, 3782}; |
| 1637 | int[] user1Uids = new int[user0Uids.length]; |
| 1638 | for (int i = 0; i < user0Uids.length; i++) { |
| 1639 | user1Uids[i] = UserHandle.PER_USER_RANGE + user0Uids[i]; |
| 1640 | |
| 1641 | final ApplicationInfo legacy = new ApplicationInfo(); |
| 1642 | legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1; |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1643 | when(mPm.getApplicationInfoAsUser(eq(PKG_N_MR1), anyInt(), anyInt())).thenReturn(legacy); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1644 | |
| 1645 | // create records with the default channel for all user 0 and user 1 uids |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1646 | mHelper.getImportance(PKG_N_MR1, user0Uids[i]); |
| 1647 | mHelper.getImportance(PKG_N_MR1, user1Uids[i]); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1648 | } |
| 1649 | |
| 1650 | mHelper.onUserRemoved(1); |
| 1651 | |
| 1652 | // user 0 records remain |
| 1653 | for (int i = 0; i < user0Uids.length; i++) { |
| 1654 | assertEquals(1, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1655 | mHelper.getNotificationChannels(PKG_N_MR1, user0Uids[i], false).getList().size()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1656 | } |
| 1657 | // user 1 records are gone |
| 1658 | for (int i = 0; i < user1Uids.length; i++) { |
| 1659 | assertEquals(0, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1660 | mHelper.getNotificationChannels(PKG_N_MR1, user1Uids[i], false).getList().size()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1661 | } |
| 1662 | } |
| 1663 | |
| 1664 | @Test |
| 1665 | public void testOnPackageChanged_packageRemoval() throws Exception { |
| 1666 | // Deleted |
| 1667 | NotificationChannel channel1 = |
| 1668 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1669 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1670 | |
Julia Reynolds | dafd3a4 | 2019-05-24 13:33:28 -0400 | [diff] [blame] | 1671 | assertTrue(mHelper.onPackagesChanged(true, UserHandle.USER_SYSTEM, new String[]{PKG_N_MR1}, |
| 1672 | new int[]{UID_N_MR1})); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1673 | |
Julia Reynolds | dafd3a4 | 2019-05-24 13:33:28 -0400 | [diff] [blame] | 1674 | assertEquals(0, mHelper.getNotificationChannels( |
| 1675 | PKG_N_MR1, UID_N_MR1, true).getList().size()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1676 | |
| 1677 | // Not deleted |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1678 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1679 | |
Julia Reynolds | dafd3a4 | 2019-05-24 13:33:28 -0400 | [diff] [blame] | 1680 | assertFalse(mHelper.onPackagesChanged(false, UserHandle.USER_SYSTEM, |
| 1681 | new String[]{PKG_N_MR1}, new int[]{UID_N_MR1})); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1682 | assertEquals(2, mHelper.getNotificationChannels(PKG_N_MR1, UID_N_MR1, false).getList().size()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1683 | } |
| 1684 | |
| 1685 | @Test |
| 1686 | public void testOnPackageChanged_packageRemoval_importance() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1687 | mHelper.setImportance(PKG_N_MR1, UID_N_MR1, NotificationManager.IMPORTANCE_HIGH); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1688 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1689 | mHelper.onPackagesChanged(true, UserHandle.USER_SYSTEM, new String[]{PKG_N_MR1}, new int[]{ |
| 1690 | UID_N_MR1}); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1691 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1692 | assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, mHelper.getImportance(PKG_N_MR1, |
| 1693 | UID_N_MR1)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1694 | } |
| 1695 | |
| 1696 | @Test |
| 1697 | public void testOnPackageChanged_packageRemoval_groups() throws Exception { |
| 1698 | NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1699 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1700 | NotificationChannelGroup ncg2 = new NotificationChannelGroup("group2", "name2"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1701 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg2, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1702 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1703 | mHelper.onPackagesChanged(true, UserHandle.USER_SYSTEM, new String[]{PKG_N_MR1}, new int[]{ |
| 1704 | UID_N_MR1}); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1705 | |
Julia Reynolds | 13ed28b | 2018-09-21 15:20:13 -0400 | [diff] [blame] | 1706 | assertEquals(0, mHelper.getNotificationChannelGroups( |
| 1707 | PKG_N_MR1, UID_N_MR1, true, true, false).getList().size()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1708 | } |
| 1709 | |
| 1710 | @Test |
| 1711 | public void testOnPackageChange_downgradeTargetSdk() throws Exception { |
| 1712 | // create channel as api 26 |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1713 | mHelper.createNotificationChannel(PKG_O, UID_O, getChannel(), true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1714 | |
| 1715 | // install new app version targeting 25 |
| 1716 | final ApplicationInfo legacy = new ApplicationInfo(); |
| 1717 | legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1; |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1718 | when(mPm.getApplicationInfoAsUser(eq(PKG_O), anyInt(), anyInt())).thenReturn(legacy); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1719 | mHelper.onPackagesChanged( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1720 | false, UserHandle.USER_SYSTEM, new String[]{PKG_O}, new int[]{UID_O}); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1721 | |
| 1722 | // make sure the default channel was readded |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1723 | //assertEquals(2, mHelper.getNotificationChannels(PKG_O, UID_O, false).getList().size()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1724 | assertNotNull(mHelper.getNotificationChannel( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1725 | PKG_O, UID_O, NotificationChannel.DEFAULT_CHANNEL_ID, false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1726 | } |
| 1727 | |
| 1728 | @Test |
Julia Reynolds | 7af51c5 | 2019-04-19 11:08:27 -0400 | [diff] [blame] | 1729 | public void testClearData() { |
| 1730 | ArraySet<String> pkg = new ArraySet<>(); |
| 1731 | pkg.add(PKG_O); |
Julia Reynolds | e7ca31b | 2019-04-25 15:41:47 -0400 | [diff] [blame] | 1732 | ArraySet<Pair<String, Integer>> pkgPair = new ArraySet<>(); |
| 1733 | pkgPair.add(new Pair(PKG_O, UID_O)); |
Julia Reynolds | 7af51c5 | 2019-04-19 11:08:27 -0400 | [diff] [blame] | 1734 | mHelper.createNotificationChannel(PKG_O, UID_O, getChannel(), true, false); |
| 1735 | mHelper.createNotificationChannelGroup( |
| 1736 | PKG_O, UID_O, new NotificationChannelGroup("1", "bye"), true); |
| 1737 | mHelper.lockChannelsForOEM(pkg.toArray(new String[]{})); |
Julia Reynolds | e7ca31b | 2019-04-25 15:41:47 -0400 | [diff] [blame] | 1738 | mHelper.updateDefaultApps(UserHandle.getUserId(UID_O), null, pkgPair); |
Julia Reynolds | 7af51c5 | 2019-04-19 11:08:27 -0400 | [diff] [blame] | 1739 | mHelper.setNotificationDelegate(PKG_O, UID_O, "", 1); |
| 1740 | mHelper.setImportance(PKG_O, UID_O, IMPORTANCE_NONE); |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 1741 | mHelper.setBubblesAllowed(PKG_O, UID_O, DEFAULT_BUBBLE_PREFERENCE); |
Julia Reynolds | 7af51c5 | 2019-04-19 11:08:27 -0400 | [diff] [blame] | 1742 | mHelper.setShowBadge(PKG_O, UID_O, false); |
| 1743 | mHelper.setAppImportanceLocked(PKG_O, UID_O); |
| 1744 | |
| 1745 | mHelper.clearData(PKG_O, UID_O); |
| 1746 | |
| 1747 | assertEquals(IMPORTANCE_UNSPECIFIED, mHelper.getImportance(PKG_O, UID_O)); |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 1748 | assertEquals(mHelper.getBubblePreference(PKG_O, UID_O), DEFAULT_BUBBLE_PREFERENCE); |
Julia Reynolds | 7af51c5 | 2019-04-19 11:08:27 -0400 | [diff] [blame] | 1749 | assertTrue(mHelper.canShowBadge(PKG_O, UID_O)); |
| 1750 | assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O)); |
| 1751 | assertEquals(0, mHelper.getAppLockedFields(PKG_O, UID_O)); |
| 1752 | assertEquals(0, mHelper.getNotificationChannels(PKG_O, UID_O, true).getList().size()); |
| 1753 | assertEquals(0, mHelper.getNotificationChannelGroups(PKG_O, UID_O).size()); |
| 1754 | |
| 1755 | NotificationChannel channel = getChannel(); |
| 1756 | mHelper.createNotificationChannel(PKG_O, UID_O, channel, true, false); |
| 1757 | |
| 1758 | assertTrue(channel.isImportanceLockedByCriticalDeviceFunction()); |
| 1759 | assertTrue(channel.isImportanceLockedByOEM()); |
| 1760 | } |
| 1761 | |
| 1762 | @Test |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1763 | public void testRecordDefaults() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1764 | assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, mHelper.getImportance(PKG_N_MR1, |
| 1765 | UID_N_MR1)); |
| 1766 | assertEquals(true, mHelper.canShowBadge(PKG_N_MR1, UID_N_MR1)); |
| 1767 | assertEquals(1, mHelper.getNotificationChannels(PKG_N_MR1, UID_N_MR1, false).getList().size()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1768 | } |
| 1769 | |
| 1770 | @Test |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1771 | public void testCreateGroup() { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1772 | NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1773 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true); |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1774 | assertEquals(ncg, |
| 1775 | mHelper.getNotificationChannelGroups(PKG_N_MR1, UID_N_MR1).iterator().next()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1776 | verify(mHandler, never()).requestSort(); |
Will Brockman | 23db6d4 | 2020-02-28 09:51:12 -0500 | [diff] [blame] | 1777 | assertEquals(1, mLogger.getCalls().size()); |
| 1778 | assertEquals( |
| 1779 | NotificationChannelLogger.NotificationChannelEvent |
| 1780 | .NOTIFICATION_CHANNEL_GROUP_CREATED, |
| 1781 | mLogger.get(0).event); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1782 | } |
| 1783 | |
| 1784 | @Test |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1785 | public void testCannotCreateChannel_badGroup() { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1786 | NotificationChannel channel1 = |
| 1787 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 1788 | channel1.setGroup("garbage"); |
| 1789 | try { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1790 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1791 | fail("Created a channel with a bad group"); |
| 1792 | } catch (IllegalArgumentException e) { |
| 1793 | } |
Will Brockman | 23db6d4 | 2020-02-28 09:51:12 -0500 | [diff] [blame] | 1794 | assertEquals(0, mLogger.getCalls().size()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1795 | } |
| 1796 | |
| 1797 | @Test |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1798 | public void testCannotCreateChannel_goodGroup() { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1799 | NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1800 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1801 | NotificationChannel channel1 = |
| 1802 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 1803 | channel1.setGroup(ncg.getId()); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1804 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1805 | |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1806 | assertEquals(ncg.getId(), mHelper.getNotificationChannel( |
| 1807 | PKG_N_MR1, UID_N_MR1, channel1.getId(), false).getGroup()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1808 | } |
| 1809 | |
| 1810 | @Test |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1811 | public void testGetChannelGroups() { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1812 | NotificationChannelGroup unused = new NotificationChannelGroup("unused", "s"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1813 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, unused, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1814 | NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1815 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1816 | NotificationChannelGroup ncg2 = new NotificationChannelGroup("group2", "name2"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1817 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg2, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1818 | |
| 1819 | NotificationChannel channel1 = |
| 1820 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 1821 | channel1.setGroup(ncg.getId()); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1822 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1823 | NotificationChannel channel1a = |
| 1824 | new NotificationChannel("id1a", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 1825 | channel1a.setGroup(ncg.getId()); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1826 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1a, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1827 | |
| 1828 | NotificationChannel channel2 = |
| 1829 | new NotificationChannel("id2", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 1830 | channel2.setGroup(ncg2.getId()); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1831 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1832 | |
| 1833 | NotificationChannel channel3 = |
| 1834 | new NotificationChannel("id3", "name1", NotificationManager.IMPORTANCE_HIGH); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1835 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1836 | |
Julia Reynolds | 13ed28b | 2018-09-21 15:20:13 -0400 | [diff] [blame] | 1837 | List<NotificationChannelGroup> actual = mHelper.getNotificationChannelGroups( |
| 1838 | PKG_N_MR1, UID_N_MR1, true, true, false).getList(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1839 | assertEquals(3, actual.size()); |
| 1840 | for (NotificationChannelGroup group : actual) { |
| 1841 | if (group.getId() == null) { |
| 1842 | assertEquals(2, group.getChannels().size()); // misc channel too |
| 1843 | assertTrue(channel3.getId().equals(group.getChannels().get(0).getId()) |
| 1844 | || channel3.getId().equals(group.getChannels().get(1).getId())); |
| 1845 | } else if (group.getId().equals(ncg.getId())) { |
| 1846 | assertEquals(2, group.getChannels().size()); |
| 1847 | if (group.getChannels().get(0).getId().equals(channel1.getId())) { |
| 1848 | assertTrue(group.getChannels().get(1).getId().equals(channel1a.getId())); |
| 1849 | } else if (group.getChannels().get(0).getId().equals(channel1a.getId())) { |
| 1850 | assertTrue(group.getChannels().get(1).getId().equals(channel1.getId())); |
| 1851 | } else { |
| 1852 | fail("expected channel not found"); |
| 1853 | } |
| 1854 | } else if (group.getId().equals(ncg2.getId())) { |
| 1855 | assertEquals(1, group.getChannels().size()); |
| 1856 | assertEquals(channel2.getId(), group.getChannels().get(0).getId()); |
| 1857 | } |
| 1858 | } |
| 1859 | } |
| 1860 | |
| 1861 | @Test |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1862 | public void testGetChannelGroups_noSideEffects() { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1863 | NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1864 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1865 | |
| 1866 | NotificationChannel channel1 = |
| 1867 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 1868 | channel1.setGroup(ncg.getId()); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1869 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false); |
Julia Reynolds | 13ed28b | 2018-09-21 15:20:13 -0400 | [diff] [blame] | 1870 | mHelper.getNotificationChannelGroups(PKG_N_MR1, UID_N_MR1, true, true, false).getList(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1871 | |
| 1872 | channel1.setImportance(IMPORTANCE_LOW); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1873 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1874 | |
Julia Reynolds | 13ed28b | 2018-09-21 15:20:13 -0400 | [diff] [blame] | 1875 | List<NotificationChannelGroup> actual = mHelper.getNotificationChannelGroups( |
| 1876 | PKG_N_MR1, UID_N_MR1, true, true, false).getList(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1877 | |
| 1878 | assertEquals(2, actual.size()); |
| 1879 | for (NotificationChannelGroup group : actual) { |
| 1880 | if (Objects.equals(group.getId(), ncg.getId())) { |
| 1881 | assertEquals(1, group.getChannels().size()); |
| 1882 | } |
| 1883 | } |
| 1884 | } |
| 1885 | |
| 1886 | @Test |
Julia Reynolds | 13ed28b | 2018-09-21 15:20:13 -0400 | [diff] [blame] | 1887 | public void testGetChannelGroups_includeEmptyGroups() { |
| 1888 | NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1"); |
| 1889 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true); |
| 1890 | NotificationChannelGroup ncgEmpty = new NotificationChannelGroup("group2", "name2"); |
| 1891 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncgEmpty, true); |
| 1892 | |
| 1893 | NotificationChannel channel1 = |
| 1894 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 1895 | channel1.setGroup(ncg.getId()); |
| 1896 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false); |
| 1897 | |
| 1898 | List<NotificationChannelGroup> actual = mHelper.getNotificationChannelGroups( |
| 1899 | PKG_N_MR1, UID_N_MR1, false, false, true).getList(); |
| 1900 | |
| 1901 | assertEquals(2, actual.size()); |
| 1902 | for (NotificationChannelGroup group : actual) { |
| 1903 | if (Objects.equals(group.getId(), ncg.getId())) { |
| 1904 | assertEquals(1, group.getChannels().size()); |
| 1905 | } |
| 1906 | if (Objects.equals(group.getId(), ncgEmpty.getId())) { |
| 1907 | assertEquals(0, group.getChannels().size()); |
| 1908 | } |
| 1909 | } |
| 1910 | } |
| 1911 | |
| 1912 | @Test |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1913 | public void testCreateChannel_updateName() { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1914 | NotificationChannel nc = new NotificationChannel("id", "hello", IMPORTANCE_DEFAULT); |
Julia Reynolds | dafd3a4 | 2019-05-24 13:33:28 -0400 | [diff] [blame] | 1915 | assertTrue(mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, nc, true, false)); |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1916 | NotificationChannel actual = |
| 1917 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "id", false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1918 | assertEquals("hello", actual.getName()); |
| 1919 | |
| 1920 | nc = new NotificationChannel("id", "goodbye", IMPORTANCE_HIGH); |
Julia Reynolds | dafd3a4 | 2019-05-24 13:33:28 -0400 | [diff] [blame] | 1921 | assertTrue(mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, nc, true, false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1922 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1923 | actual = mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "id", false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1924 | assertEquals("goodbye", actual.getName()); |
| 1925 | assertEquals(IMPORTANCE_DEFAULT, actual.getImportance()); |
| 1926 | |
| 1927 | verify(mHandler, times(1)).requestSort(); |
| 1928 | } |
| 1929 | |
| 1930 | @Test |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1931 | public void testCreateChannel_addToGroup() { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1932 | NotificationChannelGroup group = new NotificationChannelGroup("group", ""); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1933 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1934 | NotificationChannel nc = new NotificationChannel("id", "hello", IMPORTANCE_DEFAULT); |
Julia Reynolds | dafd3a4 | 2019-05-24 13:33:28 -0400 | [diff] [blame] | 1935 | assertTrue(mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, nc, true, false)); |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1936 | NotificationChannel actual = |
| 1937 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "id", false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1938 | assertNull(actual.getGroup()); |
| 1939 | |
| 1940 | nc = new NotificationChannel("id", "hello", IMPORTANCE_HIGH); |
| 1941 | nc.setGroup(group.getId()); |
Julia Reynolds | dafd3a4 | 2019-05-24 13:33:28 -0400 | [diff] [blame] | 1942 | assertTrue(mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, nc, true, false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1943 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1944 | actual = mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "id", false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1945 | assertNotNull(actual.getGroup()); |
| 1946 | assertEquals(IMPORTANCE_DEFAULT, actual.getImportance()); |
| 1947 | |
| 1948 | verify(mHandler, times(1)).requestSort(); |
Will Brockman | 23db6d4 | 2020-02-28 09:51:12 -0500 | [diff] [blame] | 1949 | assertEquals(3, mLogger.getCalls().size()); |
| 1950 | assertEquals( |
| 1951 | NotificationChannelLogger.NotificationChannelEvent |
| 1952 | .NOTIFICATION_CHANNEL_GROUP_CREATED, |
| 1953 | mLogger.get(0).event); |
| 1954 | assertEquals( |
| 1955 | NotificationChannelLogger.NotificationChannelEvent.NOTIFICATION_CHANNEL_CREATED, |
| 1956 | mLogger.get(1).event); |
| 1957 | assertEquals( |
| 1958 | NotificationChannelLogger.NotificationChannelEvent.NOTIFICATION_CHANNEL_UPDATED, |
| 1959 | mLogger.get(2).event); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | @Test |
| 1963 | public void testDumpChannelsJson() throws Exception { |
| 1964 | final ApplicationInfo upgrade = new ApplicationInfo(); |
| 1965 | upgrade.targetSdkVersion = Build.VERSION_CODES.O; |
| 1966 | try { |
| 1967 | when(mPm.getApplicationInfoAsUser( |
| 1968 | anyString(), anyInt(), anyInt())).thenReturn(upgrade); |
| 1969 | } catch (PackageManager.NameNotFoundException e) { |
| 1970 | } |
| 1971 | ArrayMap<String, Integer> expectedChannels = new ArrayMap<>(); |
| 1972 | int numPackages = ThreadLocalRandom.current().nextInt(1, 5); |
| 1973 | for (int i = 0; i < numPackages; i++) { |
| 1974 | String pkgName = "pkg" + i; |
| 1975 | int numChannels = ThreadLocalRandom.current().nextInt(1, 10); |
| 1976 | for (int j = 0; j < numChannels; j++) { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1977 | mHelper.createNotificationChannel(pkgName, UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1978 | new NotificationChannel("" + j, "a", IMPORTANCE_HIGH), true, false); |
| 1979 | } |
| 1980 | expectedChannels.put(pkgName, numChannels); |
| 1981 | } |
| 1982 | |
| 1983 | // delete the first channel of the first package |
| 1984 | String pkg = expectedChannels.keyAt(0); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1985 | mHelper.deleteNotificationChannel("pkg" + 0, UID_N_MR1, "0"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1986 | // dump should not include deleted channels |
| 1987 | int count = expectedChannels.get(pkg); |
| 1988 | expectedChannels.put(pkg, count - 1); |
| 1989 | |
| 1990 | JSONArray actual = mHelper.dumpChannelsJson(new NotificationManagerService.DumpFilter()); |
| 1991 | assertEquals(numPackages, actual.length()); |
| 1992 | for (int i = 0; i < numPackages; i++) { |
| 1993 | JSONObject object = actual.getJSONObject(i); |
| 1994 | assertTrue(expectedChannels.containsKey(object.get("packageName"))); |
| 1995 | assertEquals(expectedChannels.get(object.get("packageName")).intValue(), |
| 1996 | object.getInt("channelCount")); |
| 1997 | } |
| 1998 | } |
| 1999 | |
| 2000 | @Test |
| 2001 | public void testBadgingOverrideTrue() throws Exception { |
| 2002 | Secure.putIntForUser(getContext().getContentResolver(), |
| 2003 | Secure.NOTIFICATION_BADGING, 1, |
| 2004 | USER.getIdentifier()); |
| 2005 | mHelper.updateBadgingEnabled(); // would be called by settings observer |
| 2006 | assertTrue(mHelper.badgingEnabled(USER)); |
| 2007 | } |
| 2008 | |
| 2009 | @Test |
| 2010 | public void testBadgingOverrideFalse() throws Exception { |
| 2011 | Secure.putIntForUser(getContext().getContentResolver(), |
| 2012 | Secure.NOTIFICATION_BADGING, 0, |
| 2013 | USER.getIdentifier()); |
| 2014 | mHelper.updateBadgingEnabled(); // would be called by settings observer |
| 2015 | assertFalse(mHelper.badgingEnabled(USER)); |
| 2016 | } |
| 2017 | |
| 2018 | @Test |
| 2019 | public void testBadgingForUserAll() throws Exception { |
| 2020 | try { |
| 2021 | mHelper.badgingEnabled(UserHandle.ALL); |
| 2022 | } catch (Exception e) { |
| 2023 | fail("just don't throw"); |
| 2024 | } |
| 2025 | } |
| 2026 | |
| 2027 | @Test |
| 2028 | public void testBadgingOverrideUserIsolation() throws Exception { |
| 2029 | Secure.putIntForUser(getContext().getContentResolver(), |
| 2030 | Secure.NOTIFICATION_BADGING, 0, |
| 2031 | USER.getIdentifier()); |
| 2032 | Secure.putIntForUser(getContext().getContentResolver(), |
| 2033 | Secure.NOTIFICATION_BADGING, 1, |
| 2034 | USER2.getIdentifier()); |
| 2035 | mHelper.updateBadgingEnabled(); // would be called by settings observer |
| 2036 | assertFalse(mHelper.badgingEnabled(USER)); |
| 2037 | assertTrue(mHelper.badgingEnabled(USER2)); |
| 2038 | } |
| 2039 | |
| 2040 | @Test |
Julia Reynolds | 4509ce7 | 2019-01-31 13:12:43 -0500 | [diff] [blame] | 2041 | public void testBubblesOverrideTrue() { |
Lyn Han | 4463f84 | 2019-07-09 15:27:28 -0700 | [diff] [blame] | 2042 | Global.putInt(getContext().getContentResolver(), |
| 2043 | Global.NOTIFICATION_BUBBLES, 1); |
Julia Reynolds | 4509ce7 | 2019-01-31 13:12:43 -0500 | [diff] [blame] | 2044 | mHelper.updateBubblesEnabled(); // would be called by settings observer |
Lyn Han | 4463f84 | 2019-07-09 15:27:28 -0700 | [diff] [blame] | 2045 | assertTrue(mHelper.bubblesEnabled()); |
Julia Reynolds | 4509ce7 | 2019-01-31 13:12:43 -0500 | [diff] [blame] | 2046 | } |
| 2047 | |
| 2048 | @Test |
| 2049 | public void testBubblesOverrideFalse() { |
Lyn Han | 4463f84 | 2019-07-09 15:27:28 -0700 | [diff] [blame] | 2050 | Global.putInt(getContext().getContentResolver(), |
| 2051 | Global.NOTIFICATION_BUBBLES, 0); |
Julia Reynolds | 4509ce7 | 2019-01-31 13:12:43 -0500 | [diff] [blame] | 2052 | mHelper.updateBubblesEnabled(); // would be called by settings observer |
Lyn Han | 4463f84 | 2019-07-09 15:27:28 -0700 | [diff] [blame] | 2053 | assertFalse(mHelper.bubblesEnabled()); |
Julia Reynolds | 4509ce7 | 2019-01-31 13:12:43 -0500 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | @Test |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2057 | public void testOnLocaleChanged_updatesDefaultChannels() throws Exception { |
| 2058 | String newLabel = "bananas!"; |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2059 | final NotificationChannel defaultChannel = mHelper.getNotificationChannel(PKG_N_MR1, |
| 2060 | UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2061 | NotificationChannel.DEFAULT_CHANNEL_ID, false); |
| 2062 | assertFalse(newLabel.equals(defaultChannel.getName())); |
| 2063 | |
| 2064 | Resources res = mock(Resources.class); |
| 2065 | when(mContext.getResources()).thenReturn(res); |
| 2066 | when(res.getString(com.android.internal.R.string.default_notification_channel_label)) |
| 2067 | .thenReturn(newLabel); |
| 2068 | |
| 2069 | mHelper.onLocaleChanged(mContext, USER.getIdentifier()); |
| 2070 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2071 | assertEquals(newLabel, mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2072 | NotificationChannel.DEFAULT_CHANNEL_ID, false).getName()); |
| 2073 | } |
| 2074 | |
| 2075 | @Test |
| 2076 | public void testIsGroupBlocked_noGroup() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2077 | assertFalse(mHelper.isGroupBlocked(PKG_N_MR1, UID_N_MR1, null)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2078 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2079 | assertFalse(mHelper.isGroupBlocked(PKG_N_MR1, UID_N_MR1, "non existent group")); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2080 | } |
| 2081 | |
| 2082 | @Test |
| 2083 | public void testIsGroupBlocked_notBlocked() throws Exception { |
| 2084 | NotificationChannelGroup group = new NotificationChannelGroup("id", "name"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2085 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2086 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2087 | assertFalse(mHelper.isGroupBlocked(PKG_N_MR1, UID_N_MR1, group.getId())); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2088 | } |
| 2089 | |
| 2090 | @Test |
| 2091 | public void testIsGroupBlocked_blocked() throws Exception { |
| 2092 | NotificationChannelGroup group = new NotificationChannelGroup("id", "name"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2093 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2094 | group.setBlocked(true); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2095 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2096 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2097 | assertTrue(mHelper.isGroupBlocked(PKG_N_MR1, UID_N_MR1, group.getId())); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2098 | } |
| 2099 | |
| 2100 | @Test |
| 2101 | public void testIsGroup_appCannotResetBlock() throws Exception { |
| 2102 | NotificationChannelGroup group = new NotificationChannelGroup("id", "name"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2103 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2104 | NotificationChannelGroup group2 = group.clone(); |
| 2105 | group2.setBlocked(true); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2106 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group2, false); |
| 2107 | assertTrue(mHelper.isGroupBlocked(PKG_N_MR1, UID_N_MR1, group.getId())); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2108 | |
| 2109 | NotificationChannelGroup group3 = group.clone(); |
| 2110 | group3.setBlocked(false); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2111 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group3, true); |
| 2112 | assertTrue(mHelper.isGroupBlocked(PKG_N_MR1, UID_N_MR1, group.getId())); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2113 | } |
| 2114 | |
| 2115 | @Test |
| 2116 | public void testGetNotificationChannelGroupWithChannels() throws Exception { |
| 2117 | NotificationChannelGroup group = new NotificationChannelGroup("group", ""); |
| 2118 | NotificationChannelGroup other = new NotificationChannelGroup("something else", ""); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2119 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group, true); |
| 2120 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, other, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2121 | |
| 2122 | NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_DEFAULT); |
| 2123 | a.setGroup(group.getId()); |
| 2124 | NotificationChannel b = new NotificationChannel("b", "b", IMPORTANCE_DEFAULT); |
| 2125 | b.setGroup(other.getId()); |
| 2126 | NotificationChannel c = new NotificationChannel("c", "c", IMPORTANCE_DEFAULT); |
| 2127 | c.setGroup(group.getId()); |
| 2128 | NotificationChannel d = new NotificationChannel("d", "d", IMPORTANCE_DEFAULT); |
| 2129 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2130 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, a, true, false); |
| 2131 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, b, true, false); |
| 2132 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, c, true, false); |
| 2133 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, d, true, false); |
| 2134 | mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, c.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2135 | |
| 2136 | NotificationChannelGroup retrieved = mHelper.getNotificationChannelGroupWithChannels( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2137 | PKG_N_MR1, UID_N_MR1, group.getId(), true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2138 | assertEquals(2, retrieved.getChannels().size()); |
| 2139 | compareChannels(a, findChannel(retrieved.getChannels(), a.getId())); |
| 2140 | compareChannels(c, findChannel(retrieved.getChannels(), c.getId())); |
| 2141 | |
| 2142 | retrieved = mHelper.getNotificationChannelGroupWithChannels( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2143 | PKG_N_MR1, UID_N_MR1, group.getId(), false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2144 | assertEquals(1, retrieved.getChannels().size()); |
| 2145 | compareChannels(a, findChannel(retrieved.getChannels(), a.getId())); |
| 2146 | } |
| 2147 | |
| 2148 | @Test |
| 2149 | public void testAndroidPkgCannotBypassDnd_creation() { |
| 2150 | NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW); |
| 2151 | test.setBypassDnd(true); |
| 2152 | |
| 2153 | mHelper.createNotificationChannel(SYSTEM_PKG, SYSTEM_UID, test, true, false); |
| 2154 | |
| 2155 | assertFalse(mHelper.getNotificationChannel(SYSTEM_PKG, SYSTEM_UID, "A", false) |
| 2156 | .canBypassDnd()); |
| 2157 | } |
| 2158 | |
| 2159 | @Test |
| 2160 | public void testDndPkgCanBypassDnd_creation() { |
| 2161 | NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW); |
| 2162 | test.setBypassDnd(true); |
| 2163 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2164 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, test, true, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2165 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2166 | assertTrue(mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "A", false).canBypassDnd()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2167 | } |
| 2168 | |
| 2169 | @Test |
| 2170 | public void testNormalPkgCannotBypassDnd_creation() { |
| 2171 | NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW); |
| 2172 | test.setBypassDnd(true); |
| 2173 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2174 | mHelper.createNotificationChannel(PKG_N_MR1, 1000, test, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2175 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2176 | assertFalse(mHelper.getNotificationChannel(PKG_N_MR1, 1000, "A", false).canBypassDnd()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2177 | } |
| 2178 | |
| 2179 | @Test |
| 2180 | public void testAndroidPkgCannotBypassDnd_update() throws Exception { |
| 2181 | NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW); |
| 2182 | mHelper.createNotificationChannel(SYSTEM_PKG, SYSTEM_UID, test, true, false); |
| 2183 | |
| 2184 | NotificationChannel update = new NotificationChannel("A", "a", IMPORTANCE_LOW); |
| 2185 | update.setBypassDnd(true); |
Julia Reynolds | dafd3a4 | 2019-05-24 13:33:28 -0400 | [diff] [blame] | 2186 | assertFalse(mHelper.createNotificationChannel(SYSTEM_PKG, SYSTEM_UID, update, true, false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2187 | |
| 2188 | assertFalse(mHelper.getNotificationChannel(SYSTEM_PKG, SYSTEM_UID, "A", false) |
| 2189 | .canBypassDnd()); |
| 2190 | } |
| 2191 | |
| 2192 | @Test |
| 2193 | public void testDndPkgCanBypassDnd_update() throws Exception { |
| 2194 | NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2195 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, test, true, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2196 | |
| 2197 | NotificationChannel update = new NotificationChannel("A", "a", IMPORTANCE_LOW); |
| 2198 | update.setBypassDnd(true); |
Julia Reynolds | dafd3a4 | 2019-05-24 13:33:28 -0400 | [diff] [blame] | 2199 | assertTrue(mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, update, true, true)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2200 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2201 | assertTrue(mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "A", false).canBypassDnd()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2202 | } |
| 2203 | |
| 2204 | @Test |
| 2205 | public void testNormalPkgCannotBypassDnd_update() { |
| 2206 | NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2207 | mHelper.createNotificationChannel(PKG_N_MR1, 1000, test, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2208 | NotificationChannel update = new NotificationChannel("A", "a", IMPORTANCE_LOW); |
| 2209 | update.setBypassDnd(true); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2210 | mHelper.createNotificationChannel(PKG_N_MR1, 1000, update, true, false); |
| 2211 | assertFalse(mHelper.getNotificationChannel(PKG_N_MR1, 1000, "A", false).canBypassDnd()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2212 | } |
| 2213 | |
| 2214 | @Test |
| 2215 | public void testGetBlockedAppCount_noApps() { |
| 2216 | assertEquals(0, mHelper.getBlockedAppCount(0)); |
| 2217 | } |
| 2218 | |
| 2219 | @Test |
| 2220 | public void testGetBlockedAppCount_noAppsForUserId() { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2221 | mHelper.setEnabled(PKG_N_MR1, 100, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2222 | assertEquals(0, mHelper.getBlockedAppCount(9)); |
| 2223 | } |
| 2224 | |
| 2225 | @Test |
| 2226 | public void testGetBlockedAppCount_appsForUserId() { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2227 | mHelper.setEnabled(PKG_N_MR1, 1020, false); |
| 2228 | mHelper.setEnabled(PKG_N_MR1, 1030, false); |
| 2229 | mHelper.setEnabled(PKG_N_MR1, 1060, false); |
| 2230 | mHelper.setEnabled(PKG_N_MR1, 1000, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2231 | assertEquals(3, mHelper.getBlockedAppCount(0)); |
| 2232 | } |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 2233 | |
| 2234 | @Test |
Julia Reynolds | 12ad7ca | 2019-01-28 09:29:16 -0500 | [diff] [blame] | 2235 | public void testXml_statusBarIcons_default() throws Exception { |
Julia Reynolds | 2594b47 | 2019-04-03 13:30:16 -0400 | [diff] [blame] | 2236 | String preQXml = "<ranking version=\"1\">\n" |
| 2237 | + "<package name=\"" + PKG_N_MR1 + "\" show_badge=\"true\">\n" |
| 2238 | + "<channel id=\"something\" name=\"name\" importance=\"2\" " |
| 2239 | + "show_badge=\"true\" />\n" |
| 2240 | + "<channel id=\"miscellaneous\" name=\"Uncategorized\" usage=\"5\" " |
| 2241 | + "content_type=\"4\" flags=\"0\" show_badge=\"true\" />\n" |
| 2242 | + "</package>\n" |
| 2243 | + "</ranking>\n"; |
Will Brockman | 23db6d4 | 2020-02-28 09:51:12 -0500 | [diff] [blame] | 2244 | mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper, mLogger); |
Julia Reynolds | 2594b47 | 2019-04-03 13:30:16 -0400 | [diff] [blame] | 2245 | loadByteArrayXml(preQXml.getBytes(), true, UserHandle.USER_SYSTEM); |
Julia Reynolds | 12ad7ca | 2019-01-28 09:29:16 -0500 | [diff] [blame] | 2246 | |
| 2247 | assertEquals(PreferencesHelper.DEFAULT_HIDE_SILENT_STATUS_BAR_ICONS, |
| 2248 | mHelper.shouldHideSilentStatusIcons()); |
| 2249 | } |
| 2250 | |
| 2251 | @Test |
| 2252 | public void testXml_statusBarIcons() throws Exception { |
| 2253 | mHelper.setHideSilentStatusIcons(!PreferencesHelper.DEFAULT_HIDE_SILENT_STATUS_BAR_ICONS); |
| 2254 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2255 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_O, UID_O, false, UserHandle.USER_ALL); |
Will Brockman | 23db6d4 | 2020-02-28 09:51:12 -0500 | [diff] [blame] | 2256 | mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper, mLogger); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2257 | loadStreamXml(baos, false, UserHandle.USER_ALL); |
Julia Reynolds | 12ad7ca | 2019-01-28 09:29:16 -0500 | [diff] [blame] | 2258 | |
| 2259 | assertEquals(!PreferencesHelper.DEFAULT_HIDE_SILENT_STATUS_BAR_ICONS, |
| 2260 | mHelper.shouldHideSilentStatusIcons()); |
| 2261 | } |
| 2262 | |
| 2263 | @Test |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 2264 | public void testSetNotificationDelegate() { |
| 2265 | mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53); |
| 2266 | assertEquals("other", mHelper.getNotificationDelegate(PKG_O, UID_O)); |
| 2267 | } |
| 2268 | |
| 2269 | @Test |
| 2270 | public void testRevokeNotificationDelegate() { |
| 2271 | mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53); |
| 2272 | mHelper.revokeNotificationDelegate(PKG_O, UID_O); |
| 2273 | |
| 2274 | assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O)); |
| 2275 | } |
| 2276 | |
| 2277 | @Test |
| 2278 | public void testRevokeNotificationDelegate_noDelegateExistsNoCrash() { |
| 2279 | mHelper.revokeNotificationDelegate(PKG_O, UID_O); |
| 2280 | |
| 2281 | assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O)); |
| 2282 | } |
| 2283 | |
| 2284 | @Test |
| 2285 | public void testToggleNotificationDelegate() { |
| 2286 | mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53); |
| 2287 | mHelper.toggleNotificationDelegate(PKG_O, UID_O, false); |
| 2288 | |
| 2289 | assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O)); |
| 2290 | |
| 2291 | mHelper.toggleNotificationDelegate(PKG_O, UID_O, true); |
| 2292 | assertEquals("other", mHelper.getNotificationDelegate(PKG_O, UID_O)); |
| 2293 | } |
| 2294 | |
| 2295 | @Test |
| 2296 | public void testToggleNotificationDelegate_noDelegateExistsNoCrash() { |
| 2297 | mHelper.toggleNotificationDelegate(PKG_O, UID_O, false); |
| 2298 | assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O)); |
| 2299 | |
| 2300 | mHelper.toggleNotificationDelegate(PKG_O, UID_O, true); |
| 2301 | assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O)); |
| 2302 | } |
| 2303 | |
| 2304 | @Test |
| 2305 | public void testIsDelegateAllowed_noSource() { |
| 2306 | assertFalse(mHelper.isDelegateAllowed("does not exist", -1, "whatever", 0)); |
| 2307 | } |
| 2308 | |
| 2309 | @Test |
| 2310 | public void testIsDelegateAllowed_noDelegate() { |
| 2311 | mHelper.setImportance(PKG_O, UID_O, IMPORTANCE_UNSPECIFIED); |
| 2312 | |
| 2313 | assertFalse(mHelper.isDelegateAllowed(PKG_O, UID_O, "whatever", 0)); |
| 2314 | } |
| 2315 | |
| 2316 | @Test |
| 2317 | public void testIsDelegateAllowed_delegateDisabledByApp() { |
| 2318 | mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53); |
| 2319 | mHelper.revokeNotificationDelegate(PKG_O, UID_O); |
| 2320 | |
| 2321 | assertFalse(mHelper.isDelegateAllowed(PKG_O, UID_O, "other", 53)); |
| 2322 | } |
| 2323 | |
| 2324 | @Test |
| 2325 | public void testIsDelegateAllowed_wrongDelegate() { |
| 2326 | mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53); |
| 2327 | mHelper.revokeNotificationDelegate(PKG_O, UID_O); |
| 2328 | |
| 2329 | assertFalse(mHelper.isDelegateAllowed(PKG_O, UID_O, "banana", 27)); |
| 2330 | } |
| 2331 | |
| 2332 | @Test |
| 2333 | public void testIsDelegateAllowed_delegateDisabledByUser() { |
| 2334 | mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53); |
| 2335 | mHelper.toggleNotificationDelegate(PKG_O, UID_O, false); |
| 2336 | |
| 2337 | assertFalse(mHelper.isDelegateAllowed(PKG_O, UID_O, "other", 53)); |
| 2338 | } |
| 2339 | |
| 2340 | @Test |
| 2341 | public void testIsDelegateAllowed() { |
| 2342 | mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53); |
| 2343 | |
| 2344 | assertTrue(mHelper.isDelegateAllowed(PKG_O, UID_O, "other", 53)); |
| 2345 | } |
| 2346 | |
| 2347 | @Test |
| 2348 | public void testDelegateXml_noDelegate() throws Exception { |
| 2349 | mHelper.setImportance(PKG_O, UID_O, IMPORTANCE_UNSPECIFIED); |
| 2350 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2351 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_O, UID_O, false, UserHandle.USER_ALL); |
Will Brockman | 23db6d4 | 2020-02-28 09:51:12 -0500 | [diff] [blame] | 2352 | mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper, mLogger); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2353 | loadStreamXml(baos, false, UserHandle.USER_ALL); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 2354 | |
| 2355 | assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O)); |
| 2356 | } |
| 2357 | |
| 2358 | @Test |
| 2359 | public void testDelegateXml_delegate() throws Exception { |
| 2360 | mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53); |
| 2361 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2362 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_O, UID_O, false, UserHandle.USER_ALL); |
Will Brockman | 23db6d4 | 2020-02-28 09:51:12 -0500 | [diff] [blame] | 2363 | mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper, mLogger); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2364 | loadStreamXml(baos, false, UserHandle.USER_ALL); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 2365 | |
| 2366 | assertEquals("other", mHelper.getNotificationDelegate(PKG_O, UID_O)); |
| 2367 | } |
| 2368 | |
| 2369 | @Test |
| 2370 | public void testDelegateXml_disabledDelegate() throws Exception { |
| 2371 | mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53); |
| 2372 | mHelper.revokeNotificationDelegate(PKG_O, UID_O); |
| 2373 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2374 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_O, UID_O, false, UserHandle.USER_ALL); |
Will Brockman | 23db6d4 | 2020-02-28 09:51:12 -0500 | [diff] [blame] | 2375 | mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper, mLogger); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2376 | loadStreamXml(baos, false, UserHandle.USER_ALL); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 2377 | |
| 2378 | assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O)); |
| 2379 | } |
| 2380 | |
| 2381 | @Test |
| 2382 | public void testDelegateXml_userDisabledDelegate() throws Exception { |
| 2383 | mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53); |
| 2384 | mHelper.toggleNotificationDelegate(PKG_O, UID_O, false); |
| 2385 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2386 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_O, UID_O, false, UserHandle.USER_ALL); |
Will Brockman | 23db6d4 | 2020-02-28 09:51:12 -0500 | [diff] [blame] | 2387 | mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper, mLogger); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2388 | loadStreamXml(baos, false, UserHandle.USER_ALL); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 2389 | |
| 2390 | // appears disabled |
| 2391 | assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O)); |
| 2392 | |
| 2393 | // but was loaded and can be toggled back on |
| 2394 | mHelper.toggleNotificationDelegate(PKG_O, UID_O, true); |
| 2395 | assertEquals("other", mHelper.getNotificationDelegate(PKG_O, UID_O)); |
| 2396 | } |
| 2397 | |
| 2398 | @Test |
| 2399 | public void testDelegateXml_entirelyDisabledDelegate() throws Exception { |
| 2400 | mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53); |
| 2401 | mHelper.toggleNotificationDelegate(PKG_O, UID_O, false); |
| 2402 | mHelper.revokeNotificationDelegate(PKG_O, UID_O); |
| 2403 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2404 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_O, UID_O, false, UserHandle.USER_ALL); |
Will Brockman | 23db6d4 | 2020-02-28 09:51:12 -0500 | [diff] [blame] | 2405 | mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper, mLogger); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2406 | loadStreamXml(baos, false, UserHandle.USER_ALL); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 2407 | |
| 2408 | // appears disabled |
| 2409 | assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O)); |
| 2410 | |
| 2411 | mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53); |
| 2412 | assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O)); |
| 2413 | |
| 2414 | mHelper.toggleNotificationDelegate(PKG_O, UID_O, true); |
| 2415 | assertEquals("other", mHelper.getNotificationDelegate(PKG_O, UID_O)); |
| 2416 | } |
Julia Reynolds | 33ab8a0 | 2018-12-17 16:19:52 -0500 | [diff] [blame] | 2417 | |
| 2418 | @Test |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 2419 | public void testBubblePreference_defaults() throws Exception { |
| 2420 | assertEquals(mHelper.getBubblePreference(PKG_O, UID_O), BUBBLE_PREFERENCE_NONE); |
Julia Reynolds | 33ab8a0 | 2018-12-17 16:19:52 -0500 | [diff] [blame] | 2421 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2422 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_O, UID_O, false, UserHandle.USER_ALL); |
Will Brockman | 23db6d4 | 2020-02-28 09:51:12 -0500 | [diff] [blame] | 2423 | mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper, mLogger); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2424 | loadStreamXml(baos, false, UserHandle.USER_ALL); |
Julia Reynolds | 33ab8a0 | 2018-12-17 16:19:52 -0500 | [diff] [blame] | 2425 | |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 2426 | assertEquals(mHelper.getBubblePreference(PKG_O, UID_O), BUBBLE_PREFERENCE_NONE); |
Julia Reynolds | 33ab8a0 | 2018-12-17 16:19:52 -0500 | [diff] [blame] | 2427 | assertEquals(0, mHelper.getAppLockedFields(PKG_O, UID_O)); |
| 2428 | } |
| 2429 | |
| 2430 | @Test |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 2431 | public void testBubblePreference_xml() throws Exception { |
| 2432 | mHelper.setBubblesAllowed(PKG_O, UID_O, BUBBLE_PREFERENCE_NONE); |
| 2433 | assertEquals(mHelper.getBubblePreference(PKG_O, UID_O), BUBBLE_PREFERENCE_NONE); |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 2434 | assertEquals(PreferencesHelper.LockableAppFields.USER_LOCKED_BUBBLE, |
Julia Reynolds | 33ab8a0 | 2018-12-17 16:19:52 -0500 | [diff] [blame] | 2435 | mHelper.getAppLockedFields(PKG_O, UID_O)); |
| 2436 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2437 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_O, UID_O, false, UserHandle.USER_ALL); |
Will Brockman | 23db6d4 | 2020-02-28 09:51:12 -0500 | [diff] [blame] | 2438 | mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper, mLogger); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2439 | loadStreamXml(baos, false, UserHandle.USER_ALL); |
Julia Reynolds | 33ab8a0 | 2018-12-17 16:19:52 -0500 | [diff] [blame] | 2440 | |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 2441 | assertEquals(mHelper.getBubblePreference(PKG_O, UID_O), BUBBLE_PREFERENCE_NONE); |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 2442 | assertEquals(PreferencesHelper.LockableAppFields.USER_LOCKED_BUBBLE, |
Julia Reynolds | 33ab8a0 | 2018-12-17 16:19:52 -0500 | [diff] [blame] | 2443 | mHelper.getAppLockedFields(PKG_O, UID_O)); |
| 2444 | } |
Julia Reynolds | 413ba84 | 2019-01-11 10:38:08 -0500 | [diff] [blame] | 2445 | |
| 2446 | @Test |
| 2447 | public void testLockChannelsForOEM_emptyList() { |
| 2448 | mHelper.lockChannelsForOEM(null); |
| 2449 | mHelper.lockChannelsForOEM(new String[0]); |
| 2450 | // no exception |
| 2451 | } |
| 2452 | |
| 2453 | @Test |
| 2454 | public void testLockChannelsForOEM_appWide() { |
| 2455 | NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_HIGH); |
| 2456 | NotificationChannel b = new NotificationChannel("b", "b", IMPORTANCE_LOW); |
| 2457 | NotificationChannel c = new NotificationChannel("c", "c", IMPORTANCE_DEFAULT); |
| 2458 | // different uids, same package |
| 2459 | mHelper.createNotificationChannel(PKG_O, 3, a, true, false); |
| 2460 | mHelper.createNotificationChannel(PKG_O, 3, b, false, false); |
| 2461 | mHelper.createNotificationChannel(PKG_O, 30, c, true, true); |
| 2462 | |
| 2463 | mHelper.lockChannelsForOEM(new String[] {PKG_O}); |
| 2464 | |
| 2465 | assertTrue(mHelper.getNotificationChannel(PKG_O, 3, a.getId(), false) |
| 2466 | .isImportanceLockedByOEM()); |
| 2467 | assertTrue(mHelper.getNotificationChannel(PKG_O, 3, b.getId(), false) |
| 2468 | .isImportanceLockedByOEM()); |
| 2469 | assertTrue(mHelper.getNotificationChannel(PKG_O, 30, c.getId(), false) |
| 2470 | .isImportanceLockedByOEM()); |
| 2471 | } |
| 2472 | |
| 2473 | @Test |
| 2474 | public void testLockChannelsForOEM_onlyGivenPkg() { |
| 2475 | NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_HIGH); |
| 2476 | NotificationChannel b = new NotificationChannel("b", "b", IMPORTANCE_LOW); |
| 2477 | mHelper.createNotificationChannel(PKG_O, 3, a, true, false); |
| 2478 | mHelper.createNotificationChannel(PKG_N_MR1, 30, b, false, false); |
| 2479 | |
| 2480 | mHelper.lockChannelsForOEM(new String[] {PKG_O}); |
| 2481 | |
| 2482 | assertTrue(mHelper.getNotificationChannel(PKG_O, 3, a.getId(), false) |
| 2483 | .isImportanceLockedByOEM()); |
| 2484 | assertFalse(mHelper.getNotificationChannel(PKG_N_MR1, 30, b.getId(), false) |
| 2485 | .isImportanceLockedByOEM()); |
| 2486 | } |
| 2487 | |
| 2488 | @Test |
| 2489 | public void testLockChannelsForOEM_channelSpecific() { |
| 2490 | NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_HIGH); |
| 2491 | NotificationChannel b = new NotificationChannel("b", "b", IMPORTANCE_LOW); |
| 2492 | NotificationChannel c = new NotificationChannel("c", "c", IMPORTANCE_DEFAULT); |
| 2493 | // different uids, same package |
| 2494 | mHelper.createNotificationChannel(PKG_O, 3, a, true, false); |
| 2495 | mHelper.createNotificationChannel(PKG_O, 3, b, false, false); |
| 2496 | mHelper.createNotificationChannel(PKG_O, 30, c, true, true); |
| 2497 | |
| 2498 | mHelper.lockChannelsForOEM(new String[] {PKG_O + ":b", PKG_O + ":c"}); |
| 2499 | |
| 2500 | assertFalse(mHelper.getNotificationChannel(PKG_O, 3, a.getId(), false) |
| 2501 | .isImportanceLockedByOEM()); |
| 2502 | assertTrue(mHelper.getNotificationChannel(PKG_O, 3, b.getId(), false) |
| 2503 | .isImportanceLockedByOEM()); |
| 2504 | assertTrue(mHelper.getNotificationChannel(PKG_O, 30, c.getId(), false) |
| 2505 | .isImportanceLockedByOEM()); |
| 2506 | } |
| 2507 | |
| 2508 | @Test |
Julia Reynolds | 72b2844 | 2019-11-12 11:43:39 -0500 | [diff] [blame] | 2509 | public void testLockChannelsForOEM_channelSpecific_clearData() { |
| 2510 | NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_HIGH); |
| 2511 | mHelper.getImportance(PKG_O, UID_O); |
| 2512 | mHelper.lockChannelsForOEM(new String[] {PKG_O + ":" + a.getId()}); |
| 2513 | mHelper.createNotificationChannel(PKG_O, UID_O, a, true, false); |
| 2514 | assertTrue(mHelper.getNotificationChannel(PKG_O, UID_O, a.getId(), false) |
| 2515 | .isImportanceLockedByOEM()); |
| 2516 | |
| 2517 | mHelper.clearData(PKG_O, UID_O); |
| 2518 | |
| 2519 | // it's back! |
| 2520 | mHelper.createNotificationChannel(PKG_O, UID_O, a, true, false); |
| 2521 | // and still locked |
| 2522 | assertTrue(mHelper.getNotificationChannel(PKG_O, UID_O, a.getId(), false) |
| 2523 | .isImportanceLockedByOEM()); |
| 2524 | } |
| 2525 | |
| 2526 | @Test |
Julia Reynolds | 413ba84 | 2019-01-11 10:38:08 -0500 | [diff] [blame] | 2527 | public void testLockChannelsForOEM_channelDoesNotExistYet_appWide() { |
| 2528 | NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_HIGH); |
| 2529 | NotificationChannel b = new NotificationChannel("b", "b", IMPORTANCE_LOW); |
| 2530 | mHelper.createNotificationChannel(PKG_O, 3, a, true, false); |
| 2531 | |
| 2532 | mHelper.lockChannelsForOEM(new String[] {PKG_O}); |
| 2533 | |
| 2534 | assertTrue(mHelper.getNotificationChannel(PKG_O, 3, a.getId(), false) |
| 2535 | .isImportanceLockedByOEM()); |
| 2536 | |
| 2537 | mHelper.createNotificationChannel(PKG_O, 3, b, true, false); |
| 2538 | assertTrue(mHelper.getNotificationChannel(PKG_O, 3, b.getId(), false) |
| 2539 | .isImportanceLockedByOEM()); |
| 2540 | } |
| 2541 | |
| 2542 | @Test |
| 2543 | public void testLockChannelsForOEM_channelDoesNotExistYet_channelSpecific() { |
| 2544 | NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_HIGH); |
| 2545 | NotificationChannel b = new NotificationChannel("b", "b", IMPORTANCE_LOW); |
| 2546 | mHelper.createNotificationChannel(PKG_O, UID_O, a, true, false); |
| 2547 | |
| 2548 | mHelper.lockChannelsForOEM(new String[] {PKG_O + ":a", PKG_O + ":b"}); |
| 2549 | |
| 2550 | assertTrue(mHelper.getNotificationChannel(PKG_O, UID_O, a.getId(), false) |
| 2551 | .isImportanceLockedByOEM()); |
| 2552 | |
| 2553 | mHelper.createNotificationChannel(PKG_O, UID_O, b, true, false); |
| 2554 | assertTrue(mHelper.getNotificationChannel(PKG_O, UID_O, b.getId(), false) |
| 2555 | .isImportanceLockedByOEM()); |
| 2556 | } |
| 2557 | |
| 2558 | @Test |
| 2559 | public void testUpdateNotificationChannel_oemLockedImportance() { |
| 2560 | NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_HIGH); |
| 2561 | mHelper.createNotificationChannel(PKG_O, UID_O, a, true, false); |
| 2562 | |
| 2563 | mHelper.lockChannelsForOEM(new String[] {PKG_O}); |
| 2564 | |
| 2565 | NotificationChannel update = new NotificationChannel("a", "a", IMPORTANCE_NONE); |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 2566 | update.setAllowBubbles(false); |
Julia Reynolds | 413ba84 | 2019-01-11 10:38:08 -0500 | [diff] [blame] | 2567 | |
| 2568 | mHelper.updateNotificationChannel(PKG_O, UID_O, update, true); |
| 2569 | |
| 2570 | assertEquals(IMPORTANCE_HIGH, |
| 2571 | mHelper.getNotificationChannel(PKG_O, UID_O, a.getId(), false).getImportance()); |
| 2572 | assertEquals(false, |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 2573 | mHelper.getNotificationChannel(PKG_O, UID_O, a.getId(), false).canBubble()); |
Julia Reynolds | 413ba84 | 2019-01-11 10:38:08 -0500 | [diff] [blame] | 2574 | |
| 2575 | mHelper.updateNotificationChannel(PKG_O, UID_O, update, true); |
| 2576 | |
| 2577 | assertEquals(IMPORTANCE_HIGH, |
| 2578 | mHelper.getNotificationChannel(PKG_O, UID_O, a.getId(), false).getImportance()); |
| 2579 | } |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 2580 | |
| 2581 | @Test |
| 2582 | public void testUpdateDefaultApps_add_multiUser() { |
| 2583 | NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_HIGH); |
| 2584 | NotificationChannel b = new NotificationChannel("b", "b", IMPORTANCE_LOW); |
| 2585 | NotificationChannel c = new NotificationChannel("c", "c", IMPORTANCE_DEFAULT); |
| 2586 | // different uids, same package |
| 2587 | mHelper.createNotificationChannel(PKG_O, UID_O, a, true, false); |
| 2588 | mHelper.createNotificationChannel(PKG_O, UID_O, b, false, false); |
| 2589 | mHelper.createNotificationChannel(PKG_O, UserHandle.PER_USER_RANGE + 1, c, true, true); |
| 2590 | |
Julia Reynolds | e7ca31b | 2019-04-25 15:41:47 -0400 | [diff] [blame] | 2591 | ArraySet<Pair<String, Integer>> toAdd = new ArraySet<>(); |
| 2592 | toAdd.add(new Pair(PKG_O, UID_O)); |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 2593 | mHelper.updateDefaultApps(UserHandle.getUserId(UID_O), null, toAdd); |
| 2594 | |
| 2595 | assertTrue(mHelper.getNotificationChannel(PKG_O, UID_O, a.getId(), false) |
| 2596 | .isImportanceLockedByCriticalDeviceFunction()); |
| 2597 | assertTrue(mHelper.getNotificationChannel(PKG_O, UID_O, b.getId(), false) |
| 2598 | .isImportanceLockedByCriticalDeviceFunction()); |
| 2599 | assertFalse(mHelper.getNotificationChannel( |
| 2600 | PKG_O, UserHandle.PER_USER_RANGE + 1, c.getId(), false) |
| 2601 | .isImportanceLockedByCriticalDeviceFunction()); |
| 2602 | } |
| 2603 | |
| 2604 | @Test |
| 2605 | public void testUpdateDefaultApps_add_onlyGivenPkg() { |
| 2606 | NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_HIGH); |
| 2607 | NotificationChannel b = new NotificationChannel("b", "b", IMPORTANCE_LOW); |
| 2608 | mHelper.createNotificationChannel(PKG_O, UID_O, a, true, false); |
| 2609 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, b, false, false); |
| 2610 | |
Julia Reynolds | e7ca31b | 2019-04-25 15:41:47 -0400 | [diff] [blame] | 2611 | ArraySet<Pair<String, Integer>> toAdd = new ArraySet<>(); |
| 2612 | toAdd.add(new Pair(PKG_O, UID_O)); |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 2613 | mHelper.updateDefaultApps(UserHandle.getUserId(UID_O), null, toAdd); |
| 2614 | |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 2615 | assertTrue(mHelper.getNotificationChannel(PKG_O, UID_O, a.getId(), false) |
| 2616 | .isImportanceLockedByCriticalDeviceFunction()); |
| 2617 | assertFalse(mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, b.getId(), false) |
| 2618 | .isImportanceLockedByCriticalDeviceFunction()); |
| 2619 | } |
| 2620 | |
| 2621 | @Test |
| 2622 | public void testUpdateDefaultApps_remove() { |
| 2623 | NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_HIGH); |
| 2624 | NotificationChannel b = new NotificationChannel("b", "b", IMPORTANCE_LOW); |
| 2625 | // different uids, same package |
| 2626 | mHelper.createNotificationChannel(PKG_O, UID_O, a, true, false); |
| 2627 | mHelper.createNotificationChannel(PKG_O, UID_O, b, false, false); |
| 2628 | |
Julia Reynolds | e7ca31b | 2019-04-25 15:41:47 -0400 | [diff] [blame] | 2629 | ArraySet<Pair<String, Integer>> toAdd = new ArraySet<>(); |
| 2630 | toAdd.add(new Pair(PKG_O, UID_O)); |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 2631 | mHelper.updateDefaultApps(UserHandle.getUserId(UID_O), null, toAdd); |
| 2632 | |
| 2633 | assertTrue(mHelper.getNotificationChannel(PKG_O, UID_O, a.getId(), false) |
| 2634 | .isImportanceLockedByCriticalDeviceFunction()); |
| 2635 | assertTrue(mHelper.getNotificationChannel(PKG_O, UID_O, b.getId(), false) |
| 2636 | .isImportanceLockedByCriticalDeviceFunction()); |
| 2637 | |
| 2638 | ArraySet<String> toRemove = new ArraySet<>(); |
| 2639 | toRemove.add(PKG_O); |
| 2640 | mHelper.updateDefaultApps(UserHandle.getUserId(UID_O), toRemove, null); |
| 2641 | |
| 2642 | assertFalse(mHelper.getNotificationChannel(PKG_O, UID_O, a.getId(), false) |
| 2643 | .isImportanceLockedByCriticalDeviceFunction()); |
| 2644 | assertFalse(mHelper.getNotificationChannel(PKG_O, UID_O, b.getId(), false) |
| 2645 | .isImportanceLockedByCriticalDeviceFunction()); |
| 2646 | } |
| 2647 | |
| 2648 | @Test |
| 2649 | public void testUpdateDefaultApps_addAndRemove() { |
| 2650 | NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_HIGH); |
| 2651 | NotificationChannel b = new NotificationChannel("b", "b", IMPORTANCE_LOW); |
| 2652 | mHelper.createNotificationChannel(PKG_O, UID_O, a, true, false); |
| 2653 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, b, false, false); |
| 2654 | |
Julia Reynolds | e7ca31b | 2019-04-25 15:41:47 -0400 | [diff] [blame] | 2655 | ArraySet<Pair<String, Integer>> toAdd = new ArraySet<>(); |
| 2656 | toAdd.add(new Pair(PKG_O, UID_O)); |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 2657 | mHelper.updateDefaultApps(UserHandle.getUserId(UID_O), null, toAdd); |
| 2658 | |
| 2659 | |
| 2660 | assertTrue(mHelper.getNotificationChannel(PKG_O, UID_O, a.getId(), false) |
| 2661 | .isImportanceLockedByCriticalDeviceFunction()); |
| 2662 | assertFalse(mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, b.getId(), false) |
| 2663 | .isImportanceLockedByCriticalDeviceFunction()); |
| 2664 | |
| 2665 | // now the default is PKG_N_MR1 |
| 2666 | ArraySet<String> toRemove = new ArraySet<>(); |
| 2667 | toRemove.add(PKG_O); |
| 2668 | toAdd = new ArraySet<>(); |
Julia Reynolds | e7ca31b | 2019-04-25 15:41:47 -0400 | [diff] [blame] | 2669 | toAdd.add(new Pair(PKG_N_MR1, UID_N_MR1)); |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 2670 | mHelper.updateDefaultApps(USER.getIdentifier(), toRemove, toAdd); |
| 2671 | |
| 2672 | assertFalse(mHelper.getNotificationChannel(PKG_O, UID_O, a.getId(), false) |
| 2673 | .isImportanceLockedByCriticalDeviceFunction()); |
| 2674 | assertTrue(mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, b.getId(), false) |
| 2675 | .isImportanceLockedByCriticalDeviceFunction()); |
| 2676 | } |
| 2677 | |
| 2678 | @Test |
| 2679 | public void testUpdateDefaultApps_appDoesNotExist_noCrash() { |
Julia Reynolds | e7ca31b | 2019-04-25 15:41:47 -0400 | [diff] [blame] | 2680 | ArraySet<Pair<String, Integer>> toAdd = new ArraySet<>(); |
| 2681 | toAdd.add(new Pair(PKG_O, UID_O)); |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 2682 | ArraySet<String> toRemove = new ArraySet<>(); |
| 2683 | toRemove.add(PKG_N_MR1); |
| 2684 | mHelper.updateDefaultApps(UserHandle.getUserId(UID_O), toRemove, toAdd); |
| 2685 | } |
| 2686 | |
| 2687 | @Test |
| 2688 | public void testUpdateDefaultApps_channelDoesNotExistYet() { |
| 2689 | NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_HIGH); |
| 2690 | NotificationChannel b = new NotificationChannel("b", "b", IMPORTANCE_LOW); |
| 2691 | mHelper.createNotificationChannel(PKG_O, UID_O, a, true, false); |
| 2692 | |
Julia Reynolds | e7ca31b | 2019-04-25 15:41:47 -0400 | [diff] [blame] | 2693 | ArraySet<Pair<String, Integer>> toAdd = new ArraySet<>(); |
| 2694 | toAdd.add(new Pair(PKG_O, UID_O)); |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 2695 | mHelper.updateDefaultApps(UserHandle.getUserId(UID_O), null, toAdd); |
| 2696 | |
| 2697 | assertTrue(mHelper.getNotificationChannel(PKG_O, UID_O, a.getId(), false) |
| 2698 | .isImportanceLockedByCriticalDeviceFunction()); |
| 2699 | |
| 2700 | mHelper.createNotificationChannel(PKG_O, UID_O, b, true, false); |
| 2701 | assertTrue(mHelper.getNotificationChannel(PKG_O, UID_O, b.getId(), false) |
| 2702 | .isImportanceLockedByCriticalDeviceFunction()); |
| 2703 | } |
| 2704 | |
| 2705 | @Test |
| 2706 | public void testUpdateNotificationChannel_defaultAppLockedImportance() { |
| 2707 | NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_HIGH); |
| 2708 | mHelper.createNotificationChannel(PKG_O, UID_O, a, true, false); |
Julia Reynolds | e7ca31b | 2019-04-25 15:41:47 -0400 | [diff] [blame] | 2709 | ArraySet<Pair<String, Integer>> toAdd = new ArraySet<>(); |
| 2710 | toAdd.add(new Pair(PKG_O, UID_O)); |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 2711 | mHelper.updateDefaultApps(UserHandle.getUserId(UID_O), null, toAdd); |
| 2712 | |
| 2713 | NotificationChannel update = new NotificationChannel("a", "a", IMPORTANCE_NONE); |
| 2714 | update.setAllowBubbles(false); |
| 2715 | |
| 2716 | mHelper.updateNotificationChannel(PKG_O, UID_O, update, true); |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 2717 | assertEquals(IMPORTANCE_HIGH, |
| 2718 | mHelper.getNotificationChannel(PKG_O, UID_O, a.getId(), false).getImportance()); |
| 2719 | assertEquals(false, |
| 2720 | mHelper.getNotificationChannel(PKG_O, UID_O, a.getId(), false).canBubble()); |
| 2721 | |
| 2722 | mHelper.updateNotificationChannel(PKG_O, UID_O, update, false); |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 2723 | assertEquals(IMPORTANCE_HIGH, |
| 2724 | mHelper.getNotificationChannel(PKG_O, UID_O, a.getId(), false).getImportance()); |
Beverly | 4767922 | 2019-05-16 15:46:11 -0400 | [diff] [blame] | 2725 | |
| 2726 | NotificationChannel updateImportanceLow = new NotificationChannel("a", "a", |
| 2727 | IMPORTANCE_LOW); |
| 2728 | mHelper.updateNotificationChannel(PKG_O, UID_O, updateImportanceLow, true); |
| 2729 | assertEquals(IMPORTANCE_LOW, |
| 2730 | mHelper.getNotificationChannel(PKG_O, UID_O, a.getId(), false).getImportance()); |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 2731 | } |
Julia Reynolds | e7ca31b | 2019-04-25 15:41:47 -0400 | [diff] [blame] | 2732 | |
| 2733 | @Test |
| 2734 | public void testDefaultApp_appHasNoSettingsYet() { |
| 2735 | ArraySet<Pair<String, Integer>> toAdd = new ArraySet<>(); |
| 2736 | toAdd.add(new Pair(PKG_O, UID_O)); |
| 2737 | mHelper.updateDefaultApps(UserHandle.getUserId(UID_O), null, toAdd); |
| 2738 | |
| 2739 | NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_HIGH); |
| 2740 | mHelper.createNotificationChannel(PKG_O, UID_O, a, true, false); |
| 2741 | |
| 2742 | assertTrue(a.isImportanceLockedByCriticalDeviceFunction()); |
| 2743 | } |
| 2744 | |
| 2745 | @Test |
| 2746 | public void testChannelXml_backupDefaultApp() throws Exception { |
| 2747 | NotificationChannel channel1 = |
| 2748 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 2749 | |
| 2750 | mHelper.createNotificationChannel(PKG_O, UID_O, channel1, true, false); |
| 2751 | |
| 2752 | // clear data |
| 2753 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_O, UID_O, true, |
| 2754 | UserHandle.USER_SYSTEM, channel1.getId(), NotificationChannel.DEFAULT_CHANNEL_ID); |
| 2755 | mHelper.onPackagesChanged(true, UserHandle.myUserId(), new String[]{PKG_O}, new int[]{ |
| 2756 | UID_O}); |
| 2757 | |
| 2758 | ArraySet<Pair<String, Integer>> toAdd = new ArraySet<>(); |
| 2759 | toAdd.add(new Pair(PKG_O, UID_O)); |
| 2760 | mHelper.updateDefaultApps(UserHandle.getUserId(UID_O), null, toAdd); |
| 2761 | |
| 2762 | XmlPullParser parser = Xml.newPullParser(); |
| 2763 | parser.setInput(new BufferedInputStream(new ByteArrayInputStream(baos.toByteArray())), |
| 2764 | null); |
| 2765 | parser.nextTag(); |
| 2766 | mHelper.readXml(parser, true, UserHandle.USER_SYSTEM); |
| 2767 | |
| 2768 | assertTrue(mHelper.getNotificationChannel(PKG_O, UID_O, channel1.getId(), false) |
| 2769 | .isImportanceLockedByCriticalDeviceFunction()); |
| 2770 | } |
Mady Mellor | 9f29614 | 2019-05-24 09:42:52 -0700 | [diff] [blame] | 2771 | |
| 2772 | @Test |
Mady Mellor | a92268c | 2020-03-09 17:25:08 -0700 | [diff] [blame] | 2773 | public void testSetBubblesAllowed_none() { |
| 2774 | // Change it to non-default first |
| 2775 | mHelper.setBubblesAllowed(PKG_O, UID_O, BUBBLE_PREFERENCE_ALL); |
| 2776 | assertEquals(mHelper.getBubblePreference(PKG_O, UID_O), BUBBLE_PREFERENCE_ALL); |
| 2777 | verify(mHandler, times(1)).requestSort(); |
| 2778 | reset(mHandler); |
| 2779 | // Now test |
| 2780 | mHelper.setBubblesAllowed(PKG_O, UID_O, BUBBLE_PREFERENCE_NONE); |
| 2781 | assertEquals(mHelper.getBubblePreference(PKG_O, UID_O), BUBBLE_PREFERENCE_NONE); |
| 2782 | verify(mHandler, times(1)).requestSort(); |
| 2783 | } |
| 2784 | |
| 2785 | @Test |
| 2786 | public void testSetBubblesAllowed_all() { |
| 2787 | mHelper.setBubblesAllowed(PKG_O, UID_O, BUBBLE_PREFERENCE_ALL); |
| 2788 | assertEquals(mHelper.getBubblePreference(PKG_O, UID_O), BUBBLE_PREFERENCE_ALL); |
| 2789 | verify(mHandler, times(1)).requestSort(); |
| 2790 | } |
| 2791 | |
| 2792 | @Test |
| 2793 | public void testSetBubblesAllowed_selected() { |
| 2794 | mHelper.setBubblesAllowed(PKG_O, UID_O, BUBBLE_PREFERENCE_SELECTED); |
| 2795 | assertEquals(mHelper.getBubblePreference(PKG_O, UID_O), BUBBLE_PREFERENCE_SELECTED); |
Mady Mellor | 9f29614 | 2019-05-24 09:42:52 -0700 | [diff] [blame] | 2796 | verify(mHandler, times(1)).requestSort(); |
| 2797 | } |
Julia Reynolds | c29370a | 2019-08-20 16:08:42 -0400 | [diff] [blame] | 2798 | |
| 2799 | @Test |
| 2800 | public void testTooManyChannels() { |
| 2801 | for (int i = 0; i < NOTIFICATION_CHANNEL_COUNT_LIMIT; i++) { |
| 2802 | NotificationChannel channel = new NotificationChannel(String.valueOf(i), |
| 2803 | String.valueOf(i), NotificationManager.IMPORTANCE_HIGH); |
| 2804 | mHelper.createNotificationChannel(PKG_O, UID_O, channel, true, true); |
| 2805 | } |
| 2806 | try { |
| 2807 | NotificationChannel channel = new NotificationChannel( |
| 2808 | String.valueOf(NOTIFICATION_CHANNEL_COUNT_LIMIT), |
| 2809 | String.valueOf(NOTIFICATION_CHANNEL_COUNT_LIMIT), |
| 2810 | NotificationManager.IMPORTANCE_HIGH); |
| 2811 | mHelper.createNotificationChannel(PKG_O, UID_O, channel, true, true); |
| 2812 | fail("Allowed to create too many notification channels"); |
| 2813 | } catch (IllegalStateException e) { |
| 2814 | // great |
| 2815 | } |
| 2816 | } |
| 2817 | |
| 2818 | @Test |
| 2819 | public void testTooManyChannels_xml() throws Exception { |
| 2820 | String extraChannel = "EXTRA"; |
| 2821 | String extraChannel1 = "EXTRA1"; |
| 2822 | |
| 2823 | // create first... many... directly so we don't need a big xml blob in this test |
| 2824 | for (int i = 0; i < NOTIFICATION_CHANNEL_COUNT_LIMIT; i++) { |
| 2825 | NotificationChannel channel = new NotificationChannel(String.valueOf(i), |
| 2826 | String.valueOf(i), NotificationManager.IMPORTANCE_HIGH); |
| 2827 | mHelper.createNotificationChannel(PKG_O, UID_O, channel, true, true); |
| 2828 | } |
| 2829 | |
| 2830 | final String xml = "<ranking version=\"1\">\n" |
| 2831 | + "<package name=\"" + PKG_O + "\" uid=\"" + UID_O + "\" >\n" |
| 2832 | + "<channel id=\"" + extraChannel + "\" name=\"hi\" importance=\"3\"/>" |
| 2833 | + "<channel id=\"" + extraChannel1 + "\" name=\"hi\" importance=\"3\"/>" |
| 2834 | + "</package>" |
| 2835 | + "</ranking>"; |
| 2836 | XmlPullParser parser = Xml.newPullParser(); |
| 2837 | parser.setInput(new BufferedInputStream(new ByteArrayInputStream(xml.getBytes())), |
| 2838 | null); |
| 2839 | parser.nextTag(); |
| 2840 | mHelper.readXml(parser, false, UserHandle.USER_ALL); |
| 2841 | |
| 2842 | assertNull(mHelper.getNotificationChannel(PKG_O, UID_O, extraChannel, true)); |
| 2843 | assertNull(mHelper.getNotificationChannel(PKG_O, UID_O, extraChannel1, true)); |
| 2844 | } |
Julia Reynolds | db34c1b | 2019-11-08 10:58:15 -0500 | [diff] [blame] | 2845 | |
| 2846 | @Test |
| 2847 | public void testRestoreMultiUser() throws Exception { |
| 2848 | String pkg = "restore_pkg"; |
| 2849 | String channelId = "channelId"; |
| 2850 | int user0Importance = 3; |
| 2851 | int user10Importance = 4; |
| 2852 | when(mPm.getPackageUidAsUser(eq(pkg), anyInt())).thenReturn(UserHandle.USER_NULL); |
| 2853 | |
| 2854 | // both users have the same package, but different notification settings |
| 2855 | final String xmlUser0 = "<ranking version=\"1\">\n" |
| 2856 | + "<package name=\"" + pkg + "\" >\n" |
| 2857 | + "<channel id=\"" + channelId + "\" name=\"hi\"" |
| 2858 | + " importance=\"" + user0Importance + "\"/>" |
| 2859 | + "</package>" |
| 2860 | + "</ranking>"; |
| 2861 | final String xmlUser10 = "<ranking version=\"1\">\n" |
| 2862 | + "<package name=\"" + pkg + "\" >\n" |
| 2863 | + "<channel id=\"" + channelId + "\" name=\"hi\"" |
| 2864 | + " importance=\"" + user10Importance + "\"/>" |
| 2865 | + "</package>" |
| 2866 | + "</ranking>"; |
| 2867 | |
| 2868 | // trigger a restore for both users |
| 2869 | XmlPullParser parser = Xml.newPullParser(); |
| 2870 | parser.setInput(new BufferedInputStream(new ByteArrayInputStream(xmlUser0.getBytes())), |
| 2871 | null); |
| 2872 | parser.nextTag(); |
| 2873 | mHelper.readXml(parser, true, 0); |
| 2874 | parser = Xml.newPullParser(); |
| 2875 | parser.setInput(new BufferedInputStream(new ByteArrayInputStream(xmlUser10.getBytes())), |
| 2876 | null); |
| 2877 | parser.nextTag(); |
| 2878 | mHelper.readXml(parser, true, 10); |
| 2879 | |
| 2880 | // "install" package on both users |
| 2881 | String[] pkgList = new String[] {pkg}; |
| 2882 | int[] uidList0 = new int[] {UserHandle.PER_USER_RANGE}; |
| 2883 | int[] uidList10 = new int[] {UserHandle.PER_USER_RANGE + 1}; |
| 2884 | when(mPm.getPackageUidAsUser(pkg, 0)).thenReturn(uidList0[0]); |
| 2885 | when(mPm.getPackageUidAsUser(pkg, 10)).thenReturn(uidList10[0]); |
| 2886 | ApplicationInfo info = new ApplicationInfo(); |
| 2887 | info.targetSdkVersion = Build.VERSION_CODES.Q; |
| 2888 | when(mPm.getApplicationInfoAsUser(eq(pkg), anyInt(), anyInt())).thenReturn(info); |
| 2889 | |
| 2890 | mHelper.onPackagesChanged(false, 0, pkgList, uidList0); |
| 2891 | mHelper.onPackagesChanged(false, 10, pkgList, uidList10); |
| 2892 | |
| 2893 | assertEquals(user0Importance, |
| 2894 | mHelper.getNotificationChannel(pkg, uidList0[0], channelId, false).getImportance()); |
| 2895 | assertEquals(user10Importance, mHelper.getNotificationChannel( |
| 2896 | pkg, uidList10[0], channelId, false).getImportance()); |
| 2897 | } |
Julia Reynolds | 0f76734 | 2019-12-18 09:11:55 -0500 | [diff] [blame] | 2898 | |
| 2899 | @Test |
| 2900 | public void testGetConversationNotificationChannel() { |
| 2901 | String conversationId = "friend"; |
| 2902 | |
| 2903 | NotificationChannel parent = |
| 2904 | new NotificationChannel("parent", "messages", IMPORTANCE_DEFAULT); |
| 2905 | mHelper.createNotificationChannel(PKG_O, UID_O, parent, true, false); |
| 2906 | |
| 2907 | NotificationChannel friend = new NotificationChannel(String.format( |
| 2908 | CONVERSATION_CHANNEL_ID_FORMAT, parent.getId(), conversationId), |
| 2909 | "messages", IMPORTANCE_DEFAULT); |
| 2910 | friend.setConversationId(parent.getId(), conversationId); |
| 2911 | mHelper.createNotificationChannel(PKG_O, UID_O, friend, true, false); |
| 2912 | |
Julia Reynolds | 12ba4cf | 2020-01-10 16:01:38 -0500 | [diff] [blame] | 2913 | compareChannelsParentChild(parent, mHelper.getConversationNotificationChannel( |
| 2914 | PKG_O, UID_O, parent.getId(), conversationId, false, false), conversationId); |
| 2915 | } |
| 2916 | |
| 2917 | @Test |
| 2918 | public void testGetNotificationChannel_conversationProvidedByNotCustomizedYet() { |
| 2919 | String conversationId = "friend"; |
| 2920 | |
| 2921 | NotificationChannel parent = |
| 2922 | new NotificationChannel("parent", "messages", IMPORTANCE_DEFAULT); |
| 2923 | mHelper.createNotificationChannel(PKG_O, UID_O, parent, true, false); |
| 2924 | |
| 2925 | compareChannels(parent, mHelper.getConversationNotificationChannel( |
| 2926 | PKG_O, UID_O, parent.getId(), conversationId, true, false)); |
Julia Reynolds | 0f76734 | 2019-12-18 09:11:55 -0500 | [diff] [blame] | 2927 | } |
| 2928 | |
| 2929 | @Test |
| 2930 | public void testConversationNotificationChannelsRequireParents() { |
| 2931 | String parentId = "does not exist"; |
| 2932 | String conversationId = "friend"; |
| 2933 | |
| 2934 | NotificationChannel friend = new NotificationChannel(String.format( |
| 2935 | CONVERSATION_CHANNEL_ID_FORMAT, parentId, conversationId), |
| 2936 | "messages", IMPORTANCE_DEFAULT); |
| 2937 | friend.setConversationId(parentId, conversationId); |
| 2938 | |
| 2939 | try { |
| 2940 | mHelper.createNotificationChannel(PKG_O, UID_O, friend, true, false); |
| 2941 | fail("allowed creation of conversation channel without a parent"); |
| 2942 | } catch (IllegalArgumentException e) { |
| 2943 | // good |
| 2944 | } |
| 2945 | } |
Julia Reynolds | 7c26752 | 2020-01-16 11:26:41 -0500 | [diff] [blame] | 2946 | |
| 2947 | @Test |
Julia Reynolds | e24faa2 | 2020-04-02 12:44:47 -0400 | [diff] [blame] | 2948 | public void testPlaceholderConversationId_shortcutNotRequired() throws Exception { |
| 2949 | Settings.Global.putInt(mContext.getContentResolver(), |
| 2950 | Settings.Global.REQUIRE_SHORTCUTS_FOR_CONVERSATIONS, 0); |
| 2951 | |
Will Brockman | 23db6d4 | 2020-02-28 09:51:12 -0500 | [diff] [blame] | 2952 | mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper, mLogger); |
Julia Reynolds | 7c26752 | 2020-01-16 11:26:41 -0500 | [diff] [blame] | 2953 | |
| 2954 | final String xml = "<ranking version=\"1\">\n" |
| 2955 | + "<package name=\"" + PKG_O + "\" uid=\"" + UID_O + "\" >\n" |
| 2956 | + "<channel id=\"id\" name=\"hi\" importance=\"3\" conv_id=\"foo:placeholder_id\"/>" |
| 2957 | + "</package>" |
| 2958 | + "</ranking>"; |
| 2959 | XmlPullParser parser = Xml.newPullParser(); |
| 2960 | parser.setInput(new BufferedInputStream(new ByteArrayInputStream(xml.getBytes())), |
| 2961 | null); |
| 2962 | parser.nextTag(); |
| 2963 | mHelper.readXml(parser, false, UserHandle.USER_ALL); |
| 2964 | |
| 2965 | assertNotNull(mHelper.getNotificationChannel(PKG_O, UID_O, "id", true)); |
| 2966 | } |
| 2967 | |
| 2968 | @Test |
Julia Reynolds | e24faa2 | 2020-04-02 12:44:47 -0400 | [diff] [blame] | 2969 | public void testPlaceholderConversationId_shortcutRequired() throws Exception { |
| 2970 | Settings.Global.putInt(mContext.getContentResolver(), |
| 2971 | Settings.Global.REQUIRE_SHORTCUTS_FOR_CONVERSATIONS, 1); |
Will Brockman | 23db6d4 | 2020-02-28 09:51:12 -0500 | [diff] [blame] | 2972 | mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper, mLogger); |
Julia Reynolds | 7c26752 | 2020-01-16 11:26:41 -0500 | [diff] [blame] | 2973 | |
| 2974 | final String xml = "<ranking version=\"1\">\n" |
| 2975 | + "<package name=\"" + PKG_O + "\" uid=\"" + UID_O + "\" >\n" |
| 2976 | + "<channel id=\"id\" name=\"hi\" importance=\"3\" conv_id=\"foo:placeholder_id\"/>" |
| 2977 | + "</package>" |
| 2978 | + "</ranking>"; |
| 2979 | XmlPullParser parser = Xml.newPullParser(); |
| 2980 | parser.setInput(new BufferedInputStream(new ByteArrayInputStream(xml.getBytes())), |
| 2981 | null); |
| 2982 | parser.nextTag(); |
| 2983 | mHelper.readXml(parser, false, UserHandle.USER_ALL); |
| 2984 | |
| 2985 | assertNull(mHelper.getNotificationChannel(PKG_O, UID_O, "id", true)); |
| 2986 | } |
| 2987 | |
| 2988 | @Test |
Julia Reynolds | e24faa2 | 2020-04-02 12:44:47 -0400 | [diff] [blame] | 2989 | public void testNormalConversationId_shortcutRequired() throws Exception { |
| 2990 | Settings.Global.putInt(mContext.getContentResolver(), |
| 2991 | Settings.Global.REQUIRE_SHORTCUTS_FOR_CONVERSATIONS, 1); |
Will Brockman | 23db6d4 | 2020-02-28 09:51:12 -0500 | [diff] [blame] | 2992 | mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper, mLogger); |
Julia Reynolds | 7c26752 | 2020-01-16 11:26:41 -0500 | [diff] [blame] | 2993 | |
| 2994 | final String xml = "<ranking version=\"1\">\n" |
| 2995 | + "<package name=\"" + PKG_O + "\" uid=\"" + UID_O + "\" >\n" |
| 2996 | + "<channel id=\"id\" name=\"hi\" importance=\"3\" conv_id=\"other\"/>" |
| 2997 | + "</package>" |
| 2998 | + "</ranking>"; |
| 2999 | XmlPullParser parser = Xml.newPullParser(); |
| 3000 | parser.setInput(new BufferedInputStream(new ByteArrayInputStream(xml.getBytes())), |
| 3001 | null); |
| 3002 | parser.nextTag(); |
| 3003 | mHelper.readXml(parser, false, UserHandle.USER_ALL); |
| 3004 | |
| 3005 | assertNotNull(mHelper.getNotificationChannel(PKG_O, UID_O, "id", true)); |
| 3006 | } |
| 3007 | |
| 3008 | @Test |
Julia Reynolds | e24faa2 | 2020-04-02 12:44:47 -0400 | [diff] [blame] | 3009 | public void testNoConversationId_shortcutRequired() throws Exception { |
| 3010 | Settings.Global.putInt(mContext.getContentResolver(), |
| 3011 | Settings.Global.REQUIRE_SHORTCUTS_FOR_CONVERSATIONS, 1); |
Will Brockman | 23db6d4 | 2020-02-28 09:51:12 -0500 | [diff] [blame] | 3012 | mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper, mLogger); |
Julia Reynolds | 7c26752 | 2020-01-16 11:26:41 -0500 | [diff] [blame] | 3013 | |
| 3014 | final String xml = "<ranking version=\"1\">\n" |
| 3015 | + "<package name=\"" + PKG_O + "\" uid=\"" + UID_O + "\" >\n" |
| 3016 | + "<channel id=\"id\" name=\"hi\" importance=\"3\"/>" |
| 3017 | + "</package>" |
| 3018 | + "</ranking>"; |
| 3019 | XmlPullParser parser = Xml.newPullParser(); |
| 3020 | parser.setInput(new BufferedInputStream(new ByteArrayInputStream(xml.getBytes())), |
| 3021 | null); |
| 3022 | parser.nextTag(); |
| 3023 | mHelper.readXml(parser, false, UserHandle.USER_ALL); |
| 3024 | |
| 3025 | assertNotNull(mHelper.getNotificationChannel(PKG_O, UID_O, "id", true)); |
| 3026 | } |
Julia Reynolds | 882f206 | 2020-02-05 12:11:38 -0500 | [diff] [blame] | 3027 | |
| 3028 | @Test |
Julia Reynolds | 0297145 | 2020-02-14 16:44:19 -0500 | [diff] [blame] | 3029 | public void testGetConversations_all() { |
| 3030 | String convoId = "convo"; |
| 3031 | NotificationChannel messages = |
| 3032 | new NotificationChannel("messages", "Messages", IMPORTANCE_DEFAULT); |
| 3033 | mHelper.createNotificationChannel(PKG_O, UID_O, messages, true, false); |
| 3034 | NotificationChannel calls = |
| 3035 | new NotificationChannel("calls", "Calls", IMPORTANCE_DEFAULT); |
| 3036 | mHelper.createNotificationChannel(PKG_O, UID_O, calls, true, false); |
| 3037 | NotificationChannel p = |
| 3038 | new NotificationChannel("p calls", "Calls", IMPORTANCE_DEFAULT); |
| 3039 | mHelper.createNotificationChannel(PKG_P, UID_P, p, true, false); |
| 3040 | |
| 3041 | NotificationChannel channel = |
| 3042 | new NotificationChannel("A person msgs", "messages from A", IMPORTANCE_DEFAULT); |
| 3043 | channel.setConversationId(messages.getId(), convoId); |
| 3044 | mHelper.createNotificationChannel(PKG_O, UID_O, channel, true, false); |
| 3045 | |
| 3046 | NotificationChannel diffConvo = |
| 3047 | new NotificationChannel("B person msgs", "messages from B", IMPORTANCE_DEFAULT); |
| 3048 | diffConvo.setConversationId(p.getId(), "different convo"); |
| 3049 | mHelper.createNotificationChannel(PKG_P, UID_P, diffConvo, true, false); |
| 3050 | |
| 3051 | NotificationChannel channel2 = |
| 3052 | new NotificationChannel("A person calls", "calls from A", IMPORTANCE_DEFAULT); |
| 3053 | channel2.setConversationId(calls.getId(), convoId); |
| 3054 | channel2.setImportantConversation(true); |
| 3055 | mHelper.createNotificationChannel(PKG_O, UID_O, channel2, true, false); |
| 3056 | |
| 3057 | List<ConversationChannelWrapper> convos = mHelper.getConversations(false); |
| 3058 | |
| 3059 | assertEquals(3, convos.size()); |
| 3060 | assertTrue(conversationWrapperContainsChannel(convos, channel)); |
| 3061 | assertTrue(conversationWrapperContainsChannel(convos, diffConvo)); |
| 3062 | assertTrue(conversationWrapperContainsChannel(convos, channel2)); |
| 3063 | } |
| 3064 | |
| 3065 | @Test |
| 3066 | public void testGetConversations_onlyImportant() { |
| 3067 | String convoId = "convo"; |
| 3068 | NotificationChannel messages = |
| 3069 | new NotificationChannel("messages", "Messages", IMPORTANCE_DEFAULT); |
| 3070 | mHelper.createNotificationChannel(PKG_O, UID_O, messages, true, false); |
| 3071 | NotificationChannel calls = |
| 3072 | new NotificationChannel("calls", "Calls", IMPORTANCE_DEFAULT); |
| 3073 | mHelper.createNotificationChannel(PKG_O, UID_O, calls, true, false); |
| 3074 | NotificationChannel p = |
| 3075 | new NotificationChannel("p calls", "Calls", IMPORTANCE_DEFAULT); |
| 3076 | mHelper.createNotificationChannel(PKG_P, UID_P, p, true, false); |
| 3077 | |
| 3078 | NotificationChannel channel = |
| 3079 | new NotificationChannel("A person msgs", "messages from A", IMPORTANCE_DEFAULT); |
| 3080 | channel.setConversationId(messages.getId(), convoId); |
| 3081 | channel.setImportantConversation(true); |
| 3082 | mHelper.createNotificationChannel(PKG_O, UID_O, channel, true, false); |
| 3083 | |
| 3084 | NotificationChannel diffConvo = |
| 3085 | new NotificationChannel("B person msgs", "messages from B", IMPORTANCE_DEFAULT); |
| 3086 | diffConvo.setConversationId(p.getId(), "different convo"); |
| 3087 | diffConvo.setImportantConversation(true); |
| 3088 | mHelper.createNotificationChannel(PKG_P, UID_P, diffConvo, true, false); |
| 3089 | |
| 3090 | NotificationChannel channel2 = |
| 3091 | new NotificationChannel("A person calls", "calls from A", IMPORTANCE_DEFAULT); |
| 3092 | channel2.setConversationId(calls.getId(), convoId); |
| 3093 | mHelper.createNotificationChannel(PKG_O, UID_O, channel2, true, false); |
| 3094 | |
| 3095 | List<ConversationChannelWrapper> convos = mHelper.getConversations(true); |
| 3096 | |
| 3097 | assertEquals(2, convos.size()); |
| 3098 | assertTrue(conversationWrapperContainsChannel(convos, channel)); |
| 3099 | assertTrue(conversationWrapperContainsChannel(convos, diffConvo)); |
| 3100 | assertFalse(conversationWrapperContainsChannel(convos, channel2)); |
| 3101 | } |
| 3102 | |
| 3103 | private boolean conversationWrapperContainsChannel(List<ConversationChannelWrapper> list, |
| 3104 | NotificationChannel expected) { |
| 3105 | for (ConversationChannelWrapper ccw : list) { |
| 3106 | if (ccw.getNotificationChannel().equals(expected)) { |
| 3107 | return true; |
| 3108 | } |
| 3109 | } |
| 3110 | |
| 3111 | return false; |
| 3112 | } |
| 3113 | |
| 3114 | @Test |
Julia Reynolds | 882f206 | 2020-02-05 12:11:38 -0500 | [diff] [blame] | 3115 | public void testGetConversations_invalidPkg() { |
| 3116 | assertThat(mHelper.getConversations("bad", 1)).isEmpty(); |
| 3117 | } |
| 3118 | |
| 3119 | @Test |
| 3120 | public void testGetConversations_noConversations() { |
| 3121 | NotificationChannel channel = |
| 3122 | new NotificationChannel("not_convo", "not_convo", IMPORTANCE_DEFAULT); |
| 3123 | mHelper.createNotificationChannel(PKG_O, UID_O, channel, true, false); |
| 3124 | |
| 3125 | assertThat(mHelper.getConversations(PKG_O, UID_O)).isEmpty(); |
| 3126 | } |
| 3127 | |
| 3128 | @Test |
| 3129 | public void testGetConversations_noDisabledGroups() { |
| 3130 | NotificationChannelGroup group = new NotificationChannelGroup("a", "a"); |
| 3131 | group.setBlocked(true); |
| 3132 | mHelper.createNotificationChannelGroup(PKG_O, UID_O, group, true); |
| 3133 | NotificationChannel parent = new NotificationChannel("parent", "p", 1); |
| 3134 | mHelper.createNotificationChannel(PKG_O, UID_O, parent, true, false); |
| 3135 | |
| 3136 | NotificationChannel channel = |
| 3137 | new NotificationChannel("convo", "convo", IMPORTANCE_DEFAULT); |
| 3138 | channel.setConversationId("parent", "convo"); |
| 3139 | channel.setGroup(group.getId()); |
| 3140 | mHelper.createNotificationChannel(PKG_O, UID_O, channel, true, false); |
| 3141 | |
| 3142 | assertThat(mHelper.getConversations(PKG_O, UID_O)).isEmpty(); |
| 3143 | } |
| 3144 | |
| 3145 | @Test |
| 3146 | public void testGetConversations_noDeleted() { |
| 3147 | NotificationChannel parent = new NotificationChannel("parent", "p", 1); |
| 3148 | mHelper.createNotificationChannel(PKG_O, UID_O, parent, true, false); |
| 3149 | NotificationChannel channel = |
| 3150 | new NotificationChannel("convo", "convo", IMPORTANCE_DEFAULT); |
| 3151 | channel.setConversationId("parent", "convo"); |
| 3152 | mHelper.createNotificationChannel(PKG_O, UID_O, channel, true, false); |
| 3153 | mHelper.deleteNotificationChannel(PKG_O, UID_O, channel.getId()); |
| 3154 | |
| 3155 | assertThat(mHelper.getConversations(PKG_O, UID_O)).isEmpty(); |
| 3156 | } |
| 3157 | |
| 3158 | @Test |
| 3159 | public void testGetConversations() { |
| 3160 | NotificationChannelGroup group = new NotificationChannelGroup("acct", "account_name"); |
| 3161 | mHelper.createNotificationChannelGroup(PKG_O, UID_O, group, true); |
| 3162 | |
| 3163 | NotificationChannel messages = |
| 3164 | new NotificationChannel("messages", "Messages", IMPORTANCE_DEFAULT); |
| 3165 | messages.setGroup(group.getId()); |
| 3166 | mHelper.createNotificationChannel(PKG_O, UID_O, messages, true, false); |
| 3167 | NotificationChannel calls = |
| 3168 | new NotificationChannel("calls", "Calls", IMPORTANCE_HIGH); |
| 3169 | mHelper.createNotificationChannel(PKG_O, UID_O, calls, true, false); |
| 3170 | |
| 3171 | NotificationChannel channel = |
| 3172 | new NotificationChannel("A person", "A lovely person", IMPORTANCE_DEFAULT); |
| 3173 | channel.setGroup(group.getId()); |
| 3174 | channel.setConversationId(messages.getId(), channel.getName().toString()); |
| 3175 | mHelper.createNotificationChannel(PKG_O, UID_O, channel, true, false); |
| 3176 | |
| 3177 | NotificationChannel channel2 = |
| 3178 | new NotificationChannel("B person", "B fabulous person", IMPORTANCE_DEFAULT); |
Julia Reynolds | a625b94 | 2020-02-15 09:42:23 -0500 | [diff] [blame] | 3179 | channel2.setConversationId(calls.getId(), channel2.getName().toString()); |
Julia Reynolds | 882f206 | 2020-02-05 12:11:38 -0500 | [diff] [blame] | 3180 | mHelper.createNotificationChannel(PKG_O, UID_O, channel2, true, false); |
| 3181 | |
| 3182 | Map<String, NotificationChannel> expected = new HashMap<>(); |
| 3183 | expected.put(channel.getId(), channel); |
| 3184 | expected.put(channel2.getId(), channel2); |
| 3185 | |
| 3186 | Map<String, CharSequence> expectedGroup = new HashMap<>(); |
| 3187 | expectedGroup.put(channel.getId(), group.getName()); |
| 3188 | expectedGroup.put(channel2.getId(), null); |
| 3189 | |
| 3190 | Map<String, CharSequence> expectedParentLabel= new HashMap<>(); |
| 3191 | expectedParentLabel.put(channel.getId(), messages.getName()); |
| 3192 | expectedParentLabel.put(channel2.getId(), calls.getName()); |
| 3193 | |
| 3194 | ArrayList<ConversationChannelWrapper> convos = mHelper.getConversations(PKG_O, UID_O); |
| 3195 | assertThat(convos).hasSize(2); |
| 3196 | |
| 3197 | for (ConversationChannelWrapper convo : convos) { |
| 3198 | assertThat(convo.getNotificationChannel()) |
| 3199 | .isEqualTo(expected.get(convo.getNotificationChannel().getId())); |
| 3200 | assertThat(convo.getParentChannelLabel()) |
| 3201 | .isEqualTo(expectedParentLabel.get(convo.getNotificationChannel().getId())); |
| 3202 | assertThat(convo.getGroupLabel()) |
| 3203 | .isEqualTo(expectedGroup.get(convo.getNotificationChannel().getId())); |
| 3204 | } |
| 3205 | } |
Julia Reynolds | a625b94 | 2020-02-15 09:42:23 -0500 | [diff] [blame] | 3206 | |
| 3207 | @Test |
| 3208 | public void testDeleteConversation() { |
| 3209 | String convoId = "convo"; |
| 3210 | NotificationChannel messages = |
| 3211 | new NotificationChannel("messages", "Messages", IMPORTANCE_DEFAULT); |
| 3212 | mHelper.createNotificationChannel(PKG_O, UID_O, messages, true, false); |
| 3213 | NotificationChannel calls = |
| 3214 | new NotificationChannel("calls", "Calls", IMPORTANCE_DEFAULT); |
| 3215 | mHelper.createNotificationChannel(PKG_O, UID_O, calls, true, false); |
| 3216 | |
| 3217 | NotificationChannel channel = |
| 3218 | new NotificationChannel("A person msgs", "messages from A", IMPORTANCE_DEFAULT); |
| 3219 | channel.setConversationId(messages.getId(), convoId); |
| 3220 | mHelper.createNotificationChannel(PKG_O, UID_O, channel, true, false); |
| 3221 | |
| 3222 | NotificationChannel noMatch = |
| 3223 | new NotificationChannel("B person msgs", "messages from B", IMPORTANCE_DEFAULT); |
| 3224 | noMatch.setConversationId(messages.getId(), "different convo"); |
| 3225 | mHelper.createNotificationChannel(PKG_O, UID_O, noMatch, true, false); |
| 3226 | |
| 3227 | NotificationChannel channel2 = |
| 3228 | new NotificationChannel("A person calls", "calls from A", IMPORTANCE_DEFAULT); |
| 3229 | channel2.setConversationId(calls.getId(), convoId); |
| 3230 | mHelper.createNotificationChannel(PKG_O, UID_O, channel2, true, false); |
| 3231 | |
| 3232 | assertEquals(channel, mHelper.getNotificationChannel(PKG_O, UID_O, channel.getId(), false)); |
| 3233 | assertEquals(channel2, |
| 3234 | mHelper.getNotificationChannel(PKG_O, UID_O, channel2.getId(), false)); |
| 3235 | assertEquals(2, mHelper.deleteConversation(PKG_O, UID_O, convoId).size()); |
| 3236 | |
| 3237 | assertEquals(messages, |
| 3238 | mHelper.getNotificationChannel(PKG_O, UID_O, messages.getId(), false)); |
| 3239 | assertEquals(noMatch, |
| 3240 | mHelper.getNotificationChannel(PKG_O, UID_O, noMatch.getId(), false)); |
| 3241 | |
| 3242 | assertNull(mHelper.getNotificationChannel(PKG_O, UID_O, channel.getId(), false)); |
| 3243 | assertNull(mHelper.getNotificationChannel(PKG_O, UID_O, channel2.getId(), false)); |
| 3244 | assertEquals(channel, mHelper.getNotificationChannel(PKG_O, UID_O, channel.getId(), true)); |
| 3245 | assertEquals(channel2, |
| 3246 | mHelper.getNotificationChannel(PKG_O, UID_O, channel2.getId(), true)); |
Will Brockman | 23db6d4 | 2020-02-28 09:51:12 -0500 | [diff] [blame] | 3247 | |
| 3248 | assertEquals(7, mLogger.getCalls().size()); |
| 3249 | assertEquals( |
| 3250 | NotificationChannelLogger.NotificationChannelEvent.NOTIFICATION_CHANNEL_CREATED, |
| 3251 | mLogger.get(0).event); // Channel messages |
| 3252 | assertEquals( |
| 3253 | NotificationChannelLogger.NotificationChannelEvent.NOTIFICATION_CHANNEL_CREATED, |
| 3254 | mLogger.get(1).event); // Channel calls |
| 3255 | assertEquals( |
| 3256 | NotificationChannelLogger.NotificationChannelEvent |
| 3257 | .NOTIFICATION_CHANNEL_CONVERSATION_CREATED, |
| 3258 | mLogger.get(2).event); // Channel channel - Conversation A person msgs |
| 3259 | assertEquals( |
| 3260 | NotificationChannelLogger.NotificationChannelEvent |
| 3261 | .NOTIFICATION_CHANNEL_CONVERSATION_CREATED, |
| 3262 | mLogger.get(3).event); // Channel noMatch - Conversation B person msgs |
| 3263 | assertEquals( |
| 3264 | NotificationChannelLogger.NotificationChannelEvent |
| 3265 | .NOTIFICATION_CHANNEL_CONVERSATION_CREATED, |
| 3266 | mLogger.get(4).event); // Channel channel2 - Conversation A person calls |
| 3267 | assertEquals( |
| 3268 | NotificationChannelLogger.NotificationChannelEvent |
| 3269 | .NOTIFICATION_CHANNEL_CONVERSATION_DELETED, |
| 3270 | mLogger.get(5).event); // Delete Channel channel - Conversation A person msgs |
| 3271 | assertEquals( |
| 3272 | NotificationChannelLogger.NotificationChannelEvent |
| 3273 | .NOTIFICATION_CHANNEL_CONVERSATION_DELETED, |
| 3274 | mLogger.get(6).event); // Delete Channel channel2 - Conversation A person calls |
Julia Reynolds | a625b94 | 2020-02-15 09:42:23 -0500 | [diff] [blame] | 3275 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 3276 | } |