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 | |
| 18 | import static android.app.NotificationManager.IMPORTANCE_DEFAULT; |
| 19 | import static android.app.NotificationManager.IMPORTANCE_HIGH; |
| 20 | import static android.app.NotificationManager.IMPORTANCE_LOW; |
| 21 | import static android.app.NotificationManager.IMPORTANCE_MAX; |
| 22 | import static android.app.NotificationManager.IMPORTANCE_NONE; |
| 23 | import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED; |
| 24 | |
| 25 | import static junit.framework.Assert.assertNull; |
| 26 | import static junit.framework.Assert.fail; |
| 27 | |
| 28 | import static org.junit.Assert.assertEquals; |
| 29 | import static org.junit.Assert.assertFalse; |
| 30 | import static org.junit.Assert.assertNotNull; |
| 31 | import static org.junit.Assert.assertTrue; |
| 32 | import static org.mockito.ArgumentMatchers.any; |
| 33 | import static org.mockito.Matchers.anyInt; |
| 34 | import static org.mockito.Matchers.anyString; |
| 35 | import static org.mockito.Matchers.eq; |
| 36 | import static org.mockito.Mockito.mock; |
| 37 | import static org.mockito.Mockito.never; |
| 38 | import static org.mockito.Mockito.reset; |
| 39 | import static org.mockito.Mockito.times; |
| 40 | import static org.mockito.Mockito.verify; |
| 41 | import static org.mockito.Mockito.when; |
| 42 | |
| 43 | import android.app.Notification; |
| 44 | import android.app.NotificationChannel; |
| 45 | import android.app.NotificationChannelGroup; |
| 46 | import android.app.NotificationManager; |
| 47 | import android.content.ContentProvider; |
| 48 | import android.content.Context; |
| 49 | import android.content.IContentProvider; |
| 50 | import android.content.pm.ApplicationInfo; |
| 51 | import android.content.pm.PackageInfo; |
| 52 | import android.content.pm.PackageManager; |
| 53 | import android.content.pm.Signature; |
| 54 | import android.content.res.Resources; |
| 55 | import android.graphics.Color; |
| 56 | import android.media.AudioAttributes; |
| 57 | import android.net.Uri; |
| 58 | import android.os.Build; |
| 59 | import android.os.UserHandle; |
| 60 | import android.provider.Settings; |
| 61 | import android.provider.Settings.Secure; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 62 | import android.test.suitebuilder.annotation.SmallTest; |
| 63 | import android.testing.TestableContentResolver; |
| 64 | import android.util.ArrayMap; |
| 65 | import android.util.Xml; |
| 66 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 67 | import androidx.test.InstrumentationRegistry; |
| 68 | import androidx.test.runner.AndroidJUnit4; |
| 69 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 70 | import com.android.internal.util.FastXmlSerializer; |
| 71 | import com.android.server.UiServiceTestCase; |
| 72 | |
| 73 | import org.json.JSONArray; |
| 74 | import org.json.JSONObject; |
| 75 | import org.junit.Before; |
| 76 | import org.junit.Test; |
| 77 | import org.junit.runner.RunWith; |
| 78 | import org.mockito.Mock; |
| 79 | import org.mockito.MockitoAnnotations; |
| 80 | import org.xmlpull.v1.XmlPullParser; |
| 81 | import org.xmlpull.v1.XmlSerializer; |
| 82 | |
| 83 | import java.io.BufferedInputStream; |
| 84 | import java.io.BufferedOutputStream; |
| 85 | import java.io.ByteArrayInputStream; |
| 86 | import java.io.ByteArrayOutputStream; |
| 87 | import java.util.Arrays; |
| 88 | import java.util.HashMap; |
| 89 | import java.util.List; |
| 90 | import java.util.Map; |
| 91 | import java.util.Objects; |
| 92 | import java.util.concurrent.ThreadLocalRandom; |
| 93 | |
| 94 | @SmallTest |
| 95 | @RunWith(AndroidJUnit4.class) |
| 96 | public class PreferencesHelperTest extends UiServiceTestCase { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 97 | private static final int UID_N_MR1 = 0; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 98 | private static final UserHandle USER = UserHandle.of(0); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 99 | private static final int UID_O = 1111; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 100 | private static final String SYSTEM_PKG = "android"; |
Beverly | 0479cde2 | 2018-11-09 11:05:34 -0500 | [diff] [blame] | 101 | private static final int SYSTEM_UID = 1000; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 102 | private static final UserHandle USER2 = UserHandle.of(10); |
| 103 | private static final String TEST_CHANNEL_ID = "test_channel_id"; |
| 104 | private static final String TEST_AUTHORITY = "test"; |
| 105 | private static final Uri SOUND_URI = |
| 106 | Uri.parse("content://" + TEST_AUTHORITY + "/internal/audio/media/10"); |
| 107 | private static final Uri CANONICAL_SOUND_URI = |
| 108 | Uri.parse("content://" + TEST_AUTHORITY |
| 109 | + "/internal/audio/media/10?title=Test&canonical=1"); |
| 110 | |
| 111 | @Mock NotificationUsageStats mUsageStats; |
| 112 | @Mock RankingHandler mHandler; |
| 113 | @Mock PackageManager mPm; |
| 114 | @Mock IContentProvider mTestIContentProvider; |
| 115 | @Mock Context mContext; |
| 116 | @Mock ZenModeHelper mMockZenModeHelper; |
| 117 | |
| 118 | private NotificationManager.Policy mTestNotificationPolicy; |
| 119 | |
| 120 | private PreferencesHelper mHelper; |
| 121 | private AudioAttributes mAudioAttributes; |
| 122 | |
| 123 | @Before |
| 124 | public void setUp() throws Exception { |
| 125 | MockitoAnnotations.initMocks(this); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 126 | |
| 127 | final ApplicationInfo legacy = new ApplicationInfo(); |
| 128 | legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1; |
| 129 | final ApplicationInfo upgrade = new ApplicationInfo(); |
| 130 | upgrade.targetSdkVersion = Build.VERSION_CODES.O; |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 131 | when(mPm.getApplicationInfoAsUser(eq(PKG_N_MR1), anyInt(), anyInt())).thenReturn(legacy); |
| 132 | when(mPm.getApplicationInfoAsUser(eq(PKG_O), anyInt(), anyInt())).thenReturn(upgrade); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 133 | when(mPm.getApplicationInfoAsUser(eq(SYSTEM_PKG), anyInt(), anyInt())).thenReturn(upgrade); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 134 | when(mPm.getPackageUidAsUser(eq(PKG_N_MR1), anyInt())).thenReturn(UID_N_MR1); |
| 135 | when(mPm.getPackageUidAsUser(eq(PKG_O), anyInt())).thenReturn(UID_O); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 136 | when(mPm.getPackageUidAsUser(eq(SYSTEM_PKG), anyInt())).thenReturn(SYSTEM_UID); |
| 137 | PackageInfo info = mock(PackageInfo.class); |
| 138 | info.signatures = new Signature[] {mock(Signature.class)}; |
| 139 | when(mPm.getPackageInfoAsUser(eq(SYSTEM_PKG), anyInt(), anyInt())).thenReturn(info); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 140 | when(mPm.getPackageInfoAsUser(eq(PKG_N_MR1), anyInt(), anyInt())) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 141 | .thenReturn(mock(PackageInfo.class)); |
| 142 | when(mContext.getResources()).thenReturn( |
| 143 | InstrumentationRegistry.getContext().getResources()); |
| 144 | when(mContext.getContentResolver()).thenReturn( |
| 145 | InstrumentationRegistry.getContext().getContentResolver()); |
| 146 | when(mContext.getPackageManager()).thenReturn(mPm); |
| 147 | when(mContext.getApplicationInfo()).thenReturn(legacy); |
| 148 | // most tests assume badging is enabled |
| 149 | TestableContentResolver contentResolver = getContext().getContentResolver(); |
| 150 | contentResolver.setFallbackToExisting(false); |
| 151 | Secure.putIntForUser(contentResolver, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 152 | Secure.NOTIFICATION_BADGING, 1, UserHandle.getUserId(UID_N_MR1)); |
Julia Reynolds | 4509ce7 | 2019-01-31 13:12:43 -0500 | [diff] [blame] | 153 | Secure.putIntForUser(contentResolver, |
| 154 | Secure.NOTIFICATION_BUBBLES, 1, UserHandle.getUserId(UID_N_MR1)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 155 | |
| 156 | ContentProvider testContentProvider = mock(ContentProvider.class); |
| 157 | when(testContentProvider.getIContentProvider()).thenReturn(mTestIContentProvider); |
| 158 | contentResolver.addProvider(TEST_AUTHORITY, testContentProvider); |
| 159 | |
| 160 | when(mTestIContentProvider.canonicalize(any(), eq(SOUND_URI))) |
| 161 | .thenReturn(CANONICAL_SOUND_URI); |
| 162 | when(mTestIContentProvider.canonicalize(any(), eq(CANONICAL_SOUND_URI))) |
| 163 | .thenReturn(CANONICAL_SOUND_URI); |
| 164 | when(mTestIContentProvider.uncanonicalize(any(), eq(CANONICAL_SOUND_URI))) |
| 165 | .thenReturn(SOUND_URI); |
| 166 | |
| 167 | mTestNotificationPolicy = new NotificationManager.Policy(0, 0, 0, 0, |
| 168 | NotificationManager.Policy.STATE_CHANNELS_BYPASSING_DND); |
| 169 | when(mMockZenModeHelper.getNotificationPolicy()).thenReturn(mTestNotificationPolicy); |
| 170 | mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper); |
| 171 | resetZenModeHelper(); |
| 172 | |
| 173 | mAudioAttributes = new AudioAttributes.Builder() |
| 174 | .setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN) |
| 175 | .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE) |
| 176 | .setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED) |
| 177 | .build(); |
| 178 | } |
| 179 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 180 | private ByteArrayOutputStream writeXmlAndPurge( |
| 181 | String pkg, int uid, boolean forBackup, int userId, String... channelIds) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 182 | throws Exception { |
| 183 | XmlSerializer serializer = new FastXmlSerializer(); |
| 184 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 185 | serializer.setOutput(new BufferedOutputStream(baos), "utf-8"); |
| 186 | serializer.startDocument(null, true); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 187 | mHelper.writeXml(serializer, forBackup, userId); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 188 | serializer.endDocument(); |
| 189 | serializer.flush(); |
| 190 | for (String channelId : channelIds) { |
| 191 | mHelper.permanentlyDeleteNotificationChannel(pkg, uid, channelId); |
| 192 | } |
| 193 | return baos; |
| 194 | } |
| 195 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 196 | private void loadStreamXml(ByteArrayOutputStream stream, boolean forRestore, int userId) |
| 197 | throws Exception { |
| 198 | loadByteArrayXml(stream.toByteArray(), forRestore, userId); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 199 | } |
| 200 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 201 | private void loadByteArrayXml(byte[] byteArray, boolean forRestore, int userId) |
| 202 | throws Exception { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 203 | XmlPullParser parser = Xml.newPullParser(); |
| 204 | parser.setInput(new BufferedInputStream(new ByteArrayInputStream(byteArray)), null); |
| 205 | parser.nextTag(); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 206 | mHelper.readXml(parser, forRestore, userId); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | private void compareChannels(NotificationChannel expected, NotificationChannel actual) { |
| 210 | assertEquals(expected.getId(), actual.getId()); |
| 211 | assertEquals(expected.getName(), actual.getName()); |
| 212 | assertEquals(expected.getDescription(), actual.getDescription()); |
| 213 | assertEquals(expected.shouldVibrate(), actual.shouldVibrate()); |
| 214 | assertEquals(expected.shouldShowLights(), actual.shouldShowLights()); |
| 215 | assertEquals(expected.getImportance(), actual.getImportance()); |
| 216 | assertEquals(expected.getLockscreenVisibility(), actual.getLockscreenVisibility()); |
| 217 | assertEquals(expected.getSound(), actual.getSound()); |
| 218 | assertEquals(expected.canBypassDnd(), actual.canBypassDnd()); |
| 219 | assertTrue(Arrays.equals(expected.getVibrationPattern(), actual.getVibrationPattern())); |
| 220 | assertEquals(expected.getGroup(), actual.getGroup()); |
| 221 | assertEquals(expected.getAudioAttributes(), actual.getAudioAttributes()); |
| 222 | assertEquals(expected.getLightColor(), actual.getLightColor()); |
| 223 | } |
| 224 | |
| 225 | private void compareGroups(NotificationChannelGroup expected, NotificationChannelGroup actual) { |
| 226 | assertEquals(expected.getId(), actual.getId()); |
| 227 | assertEquals(expected.getName(), actual.getName()); |
| 228 | assertEquals(expected.getDescription(), actual.getDescription()); |
| 229 | assertEquals(expected.isBlocked(), actual.isBlocked()); |
| 230 | } |
| 231 | |
| 232 | private NotificationChannel getChannel() { |
| 233 | return new NotificationChannel("id", "name", IMPORTANCE_LOW); |
| 234 | } |
| 235 | |
| 236 | private NotificationChannel findChannel(List<NotificationChannel> channels, String id) { |
| 237 | for (NotificationChannel channel : channels) { |
| 238 | if (channel.getId().equals(id)) { |
| 239 | return channel; |
| 240 | } |
| 241 | } |
| 242 | return null; |
| 243 | } |
| 244 | |
| 245 | private void resetZenModeHelper() { |
| 246 | reset(mMockZenModeHelper); |
| 247 | when(mMockZenModeHelper.getNotificationPolicy()).thenReturn(mTestNotificationPolicy); |
| 248 | } |
| 249 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 250 | private void setUpPackageWithUid(String packageName, int uid) throws Exception { |
| 251 | when(mPm.getApplicationInfoAsUser(eq(packageName), anyInt(), anyInt())) |
| 252 | .thenReturn(new ApplicationInfo()); |
| 253 | when(mPm.getPackageUidAsUser(eq(packageName), anyInt())).thenReturn(uid); |
| 254 | } |
| 255 | |
| 256 | @Test |
| 257 | public void testWriteXml_onlyBackupsTargetUser() throws Exception { |
| 258 | // Setup package notifications. |
| 259 | String package0 = "test.package.user0"; |
| 260 | int uid0 = 1001; |
| 261 | setUpPackageWithUid(package0, uid0); |
| 262 | NotificationChannel channel0 = new NotificationChannel("id0", "name0", IMPORTANCE_HIGH); |
| 263 | mHelper.createNotificationChannel(package0, uid0, channel0, true, false); |
| 264 | |
| 265 | String package10 = "test.package.user10"; |
| 266 | int uid10 = 1001001; |
| 267 | setUpPackageWithUid(package10, uid10); |
| 268 | NotificationChannel channel10 = new NotificationChannel("id10", "name10", IMPORTANCE_HIGH); |
| 269 | mHelper.createNotificationChannel(package10, uid10, channel10, true, false); |
| 270 | |
| 271 | ByteArrayOutputStream baos = writeXmlAndPurge(package10, uid10, true, 10); |
| 272 | |
| 273 | // Reset state. |
| 274 | mHelper.onPackagesChanged(true, 0, new String[] {package0}, new int[] {uid0}); |
| 275 | mHelper.onPackagesChanged(true, 10, new String[] {package10}, new int[] {uid10}); |
| 276 | |
| 277 | // Parse backup data. |
| 278 | loadStreamXml(baos, true, 0); |
| 279 | loadStreamXml(baos, true, 10); |
| 280 | |
| 281 | assertEquals( |
| 282 | channel10, |
| 283 | mHelper.getNotificationChannel(package10, uid10, channel10.getId(), false)); |
| 284 | assertNull(mHelper.getNotificationChannel(package0, uid0, channel0.getId(), false)); |
| 285 | } |
| 286 | |
| 287 | @Test |
| 288 | public void testReadXml_onlyRestoresTargetUser() throws Exception { |
| 289 | // Setup package in user 0. |
| 290 | String package0 = "test.package.user0"; |
| 291 | int uid0 = 1001; |
| 292 | setUpPackageWithUid(package0, uid0); |
| 293 | NotificationChannel channel0 = new NotificationChannel("id0", "name0", IMPORTANCE_HIGH); |
| 294 | mHelper.createNotificationChannel(package0, uid0, channel0, true, false); |
| 295 | |
| 296 | ByteArrayOutputStream baos = writeXmlAndPurge(package0, uid0, true, 0); |
| 297 | |
| 298 | // Reset state. |
| 299 | mHelper.onPackagesChanged(true, 0, new String[] {package0}, new int[] {uid0}); |
| 300 | |
| 301 | // Restore should convert the uid according to the target user. |
| 302 | int expectedUid = 1001001; |
| 303 | setUpPackageWithUid(package0, expectedUid); |
| 304 | // Parse backup data. |
| 305 | loadStreamXml(baos, true, 10); |
| 306 | |
| 307 | assertEquals( |
| 308 | channel0, |
| 309 | mHelper.getNotificationChannel(package0, expectedUid, channel0.getId(), false)); |
| 310 | assertNull(mHelper.getNotificationChannel(package0, uid0, channel0.getId(), false)); |
| 311 | } |
| 312 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 313 | @Test |
| 314 | public void testChannelXml() throws Exception { |
| 315 | NotificationChannelGroup ncg = new NotificationChannelGroup("1", "bye"); |
| 316 | ncg.setBlocked(true); |
| 317 | ncg.setDescription("group desc"); |
| 318 | NotificationChannelGroup ncg2 = new NotificationChannelGroup("2", "hello"); |
| 319 | NotificationChannel channel1 = |
| 320 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 321 | NotificationChannel channel2 = |
| 322 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 323 | channel2.setDescription("descriptions for all"); |
| 324 | channel2.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes); |
| 325 | channel2.enableLights(true); |
| 326 | channel2.setBypassDnd(true); |
| 327 | channel2.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 328 | channel2.enableVibration(true); |
| 329 | channel2.setGroup(ncg.getId()); |
| 330 | channel2.setVibrationPattern(new long[]{100, 67, 145, 156}); |
| 331 | channel2.setLightColor(Color.BLUE); |
| 332 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 333 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true); |
| 334 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg2, true); |
| 335 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false); |
| 336 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, false, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 337 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 338 | mHelper.setShowBadge(PKG_N_MR1, UID_N_MR1, true); |
| 339 | mHelper.setAppImportanceLocked(PKG_N_MR1, UID_N_MR1); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 340 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 341 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, false, |
| 342 | UserHandle.USER_ALL, channel1.getId(), channel2.getId(), |
| 343 | NotificationChannel.DEFAULT_CHANNEL_ID); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 344 | mHelper.onPackagesChanged(true, UserHandle.myUserId(), new String[]{PKG_N_MR1}, new int[]{ |
| 345 | UID_N_MR1}); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 346 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 347 | loadStreamXml(baos, false, UserHandle.USER_ALL); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 348 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 349 | assertTrue(mHelper.canShowBadge(PKG_N_MR1, UID_N_MR1)); |
| 350 | assertTrue(mHelper.getIsAppImportanceLocked(PKG_N_MR1, UID_N_MR1)); |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 351 | assertEquals(channel1, |
| 352 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1.getId(), false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 353 | compareChannels(channel2, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 354 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2.getId(), false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 355 | |
Julia Reynolds | 13ed28b | 2018-09-21 15:20:13 -0400 | [diff] [blame] | 356 | List<NotificationChannelGroup> actualGroups = mHelper.getNotificationChannelGroups( |
| 357 | PKG_N_MR1, UID_N_MR1, false, true, false).getList(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 358 | boolean foundNcg = false; |
| 359 | for (NotificationChannelGroup actual : actualGroups) { |
| 360 | if (ncg.getId().equals(actual.getId())) { |
| 361 | foundNcg = true; |
| 362 | compareGroups(ncg, actual); |
| 363 | } else if (ncg2.getId().equals(actual.getId())) { |
| 364 | compareGroups(ncg2, actual); |
| 365 | } |
| 366 | } |
| 367 | assertTrue(foundNcg); |
| 368 | |
| 369 | boolean foundChannel2Group = false; |
| 370 | for (NotificationChannelGroup actual : actualGroups) { |
| 371 | if (channel2.getGroup().equals(actual.getChannels().get(0).getGroup())) { |
| 372 | foundChannel2Group = true; |
| 373 | break; |
| 374 | } |
| 375 | } |
| 376 | assertTrue(foundChannel2Group); |
| 377 | } |
| 378 | |
| 379 | @Test |
| 380 | public void testChannelXmlForBackup() throws Exception { |
| 381 | NotificationChannelGroup ncg = new NotificationChannelGroup("1", "bye"); |
| 382 | NotificationChannelGroup ncg2 = new NotificationChannelGroup("2", "hello"); |
| 383 | NotificationChannel channel1 = |
| 384 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 385 | NotificationChannel channel2 = |
| 386 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 387 | channel2.setDescription("descriptions for all"); |
| 388 | channel2.setSound(SOUND_URI, mAudioAttributes); |
| 389 | channel2.enableLights(true); |
| 390 | channel2.setBypassDnd(true); |
| 391 | channel2.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 392 | channel2.enableVibration(false); |
| 393 | channel2.setGroup(ncg.getId()); |
| 394 | channel2.setLightColor(Color.BLUE); |
| 395 | NotificationChannel channel3 = new NotificationChannel("id3", "NAM3", IMPORTANCE_HIGH); |
| 396 | channel3.enableVibration(true); |
| 397 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 398 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true); |
| 399 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg2, true); |
| 400 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false); |
| 401 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, false, false); |
| 402 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3, false, false); |
| 403 | mHelper.createNotificationChannel(PKG_O, UID_O, getChannel(), true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 404 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 405 | mHelper.setShowBadge(PKG_N_MR1, UID_N_MR1, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 406 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 407 | mHelper.setImportance(PKG_O, UID_O, IMPORTANCE_NONE); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 408 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 409 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, true, |
| 410 | UserHandle.USER_SYSTEM, channel1.getId(), channel2.getId(), channel3.getId(), |
| 411 | NotificationChannel.DEFAULT_CHANNEL_ID); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 412 | mHelper.onPackagesChanged(true, UserHandle.myUserId(), new String[]{PKG_N_MR1, PKG_O}, |
| 413 | new int[]{UID_N_MR1, UID_O}); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 414 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 415 | mHelper.setShowBadge(PKG_O, UID_O, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 416 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 417 | loadStreamXml(baos, true, UserHandle.USER_SYSTEM); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 418 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 419 | assertEquals(IMPORTANCE_NONE, mHelper.getImportance(PKG_O, UID_O)); |
| 420 | assertTrue(mHelper.canShowBadge(PKG_N_MR1, UID_N_MR1)); |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 421 | assertEquals(channel1, |
| 422 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1.getId(), false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 423 | compareChannels(channel2, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 424 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2.getId(), false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 425 | compareChannels(channel3, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 426 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3.getId(), false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 427 | |
Julia Reynolds | 13ed28b | 2018-09-21 15:20:13 -0400 | [diff] [blame] | 428 | List<NotificationChannelGroup> actualGroups = mHelper.getNotificationChannelGroups( |
| 429 | PKG_N_MR1, UID_N_MR1, false, true, false).getList(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 430 | boolean foundNcg = false; |
| 431 | for (NotificationChannelGroup actual : actualGroups) { |
| 432 | if (ncg.getId().equals(actual.getId())) { |
| 433 | foundNcg = true; |
| 434 | compareGroups(ncg, actual); |
| 435 | } else if (ncg2.getId().equals(actual.getId())) { |
| 436 | compareGroups(ncg2, actual); |
| 437 | } |
| 438 | } |
| 439 | assertTrue(foundNcg); |
| 440 | |
| 441 | boolean foundChannel2Group = false; |
| 442 | for (NotificationChannelGroup actual : actualGroups) { |
| 443 | if (channel2.getGroup().equals(actual.getChannels().get(0).getGroup())) { |
| 444 | foundChannel2Group = true; |
| 445 | break; |
| 446 | } |
| 447 | } |
| 448 | assertTrue(foundChannel2Group); |
| 449 | } |
| 450 | |
| 451 | @Test |
| 452 | public void testBackupXml_backupCanonicalizedSoundUri() throws Exception { |
| 453 | NotificationChannel channel = |
| 454 | new NotificationChannel("id", "name", IMPORTANCE_LOW); |
| 455 | channel.setSound(SOUND_URI, mAudioAttributes); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 456 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 457 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 458 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, true, |
| 459 | UserHandle.USER_SYSTEM, channel.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 460 | |
| 461 | // Testing that in restore we are given the canonical version |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 462 | loadStreamXml(baos, true, UserHandle.USER_SYSTEM); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 463 | verify(mTestIContentProvider).uncanonicalize(any(), eq(CANONICAL_SOUND_URI)); |
| 464 | } |
| 465 | |
| 466 | @Test |
| 467 | public void testRestoreXml_withExistentCanonicalizedSoundUri() throws Exception { |
| 468 | Uri localUri = Uri.parse("content://" + TEST_AUTHORITY + "/local/url"); |
| 469 | Uri canonicalBasedOnLocal = localUri.buildUpon() |
| 470 | .appendQueryParameter("title", "Test") |
| 471 | .appendQueryParameter("canonical", "1") |
| 472 | .build(); |
| 473 | when(mTestIContentProvider.canonicalize(any(), eq(CANONICAL_SOUND_URI))) |
| 474 | .thenReturn(canonicalBasedOnLocal); |
| 475 | when(mTestIContentProvider.uncanonicalize(any(), eq(CANONICAL_SOUND_URI))) |
| 476 | .thenReturn(localUri); |
| 477 | when(mTestIContentProvider.uncanonicalize(any(), eq(canonicalBasedOnLocal))) |
| 478 | .thenReturn(localUri); |
| 479 | |
| 480 | NotificationChannel channel = |
| 481 | new NotificationChannel("id", "name", IMPORTANCE_LOW); |
| 482 | channel.setSound(SOUND_URI, mAudioAttributes); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 483 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 484 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, true, |
| 485 | UserHandle.USER_SYSTEM, channel.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 486 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 487 | loadStreamXml(baos, true, UserHandle.USER_SYSTEM); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 488 | |
| 489 | NotificationChannel actualChannel = mHelper.getNotificationChannel( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 490 | PKG_N_MR1, UID_N_MR1, channel.getId(), false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 491 | assertEquals(localUri, actualChannel.getSound()); |
| 492 | } |
| 493 | |
| 494 | @Test |
| 495 | public void testRestoreXml_withNonExistentCanonicalizedSoundUri() throws Exception { |
| 496 | Thread.sleep(3000); |
| 497 | when(mTestIContentProvider.canonicalize(any(), eq(CANONICAL_SOUND_URI))) |
| 498 | .thenReturn(null); |
| 499 | when(mTestIContentProvider.uncanonicalize(any(), eq(CANONICAL_SOUND_URI))) |
| 500 | .thenReturn(null); |
| 501 | |
| 502 | NotificationChannel channel = |
| 503 | new NotificationChannel("id", "name", IMPORTANCE_LOW); |
| 504 | channel.setSound(SOUND_URI, mAudioAttributes); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 505 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 506 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, true, |
| 507 | UserHandle.USER_SYSTEM, channel.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 508 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 509 | loadStreamXml(baos, true, UserHandle.USER_SYSTEM); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 510 | |
| 511 | NotificationChannel actualChannel = mHelper.getNotificationChannel( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 512 | PKG_N_MR1, UID_N_MR1, channel.getId(), false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 513 | assertEquals(Settings.System.DEFAULT_NOTIFICATION_URI, actualChannel.getSound()); |
| 514 | } |
| 515 | |
| 516 | |
| 517 | /** |
| 518 | * Although we don't make backups with uncanonicalized uris anymore, we used to, so we have to |
| 519 | * handle its restore properly. |
| 520 | */ |
| 521 | @Test |
| 522 | public void testRestoreXml_withUncanonicalizedNonLocalSoundUri() throws Exception { |
| 523 | // Not a local uncanonicalized uri, simulating that it fails to exist locally |
| 524 | when(mTestIContentProvider.canonicalize(any(), eq(SOUND_URI))).thenReturn(null); |
| 525 | String id = "id"; |
| 526 | String backupWithUncanonicalizedSoundUri = "<ranking version=\"1\">\n" |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 527 | + "<package name=\"" + PKG_N_MR1 + "\" show_badge=\"true\">\n" |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 528 | + "<channel id=\"" + id + "\" name=\"name\" importance=\"2\" " |
| 529 | + "sound=\"" + SOUND_URI + "\" " |
| 530 | + "usage=\"6\" content_type=\"0\" flags=\"1\" show_badge=\"true\" />\n" |
| 531 | + "<channel id=\"miscellaneous\" name=\"Uncategorized\" usage=\"5\" " |
| 532 | + "content_type=\"4\" flags=\"0\" show_badge=\"true\" />\n" |
| 533 | + "</package>\n" |
| 534 | + "</ranking>\n"; |
| 535 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 536 | loadByteArrayXml( |
| 537 | backupWithUncanonicalizedSoundUri.getBytes(), true, UserHandle.USER_SYSTEM); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 538 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 539 | NotificationChannel actualChannel = mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, id, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 540 | assertEquals(Settings.System.DEFAULT_NOTIFICATION_URI, actualChannel.getSound()); |
| 541 | } |
| 542 | |
| 543 | @Test |
| 544 | public void testBackupRestoreXml_withNullSoundUri() throws Exception { |
| 545 | NotificationChannel channel = |
| 546 | new NotificationChannel("id", "name", IMPORTANCE_LOW); |
| 547 | channel.setSound(null, mAudioAttributes); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 548 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 549 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, true, |
| 550 | UserHandle.USER_SYSTEM, channel.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 551 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 552 | loadStreamXml(baos, true, UserHandle.USER_SYSTEM); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 553 | |
| 554 | NotificationChannel actualChannel = mHelper.getNotificationChannel( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 555 | PKG_N_MR1, UID_N_MR1, channel.getId(), false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 556 | assertEquals(null, actualChannel.getSound()); |
| 557 | } |
| 558 | |
| 559 | @Test |
| 560 | public void testChannelXml_backup() throws Exception { |
| 561 | NotificationChannelGroup ncg = new NotificationChannelGroup("1", "bye"); |
| 562 | NotificationChannelGroup ncg2 = new NotificationChannelGroup("2", "hello"); |
| 563 | NotificationChannel channel1 = |
| 564 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 565 | NotificationChannel channel2 = |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 566 | new NotificationChannel("id2", "name2", IMPORTANCE_HIGH); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 567 | NotificationChannel channel3 = |
| 568 | new NotificationChannel("id3", "name3", IMPORTANCE_LOW); |
| 569 | channel3.setGroup(ncg.getId()); |
| 570 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 571 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true); |
| 572 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg2, true); |
| 573 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false); |
| 574 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, false, false); |
| 575 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 576 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 577 | mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1.getId()); |
| 578 | mHelper.deleteNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg.getId()); |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 579 | assertEquals(channel2, |
| 580 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2.getId(), false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 581 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 582 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, true, |
| 583 | UserHandle.USER_SYSTEM, channel1.getId(), channel2.getId(), channel3.getId(), |
| 584 | NotificationChannel.DEFAULT_CHANNEL_ID); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 585 | mHelper.onPackagesChanged(true, UserHandle.myUserId(), new String[]{PKG_N_MR1}, new int[]{ |
| 586 | UID_N_MR1}); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 587 | |
| 588 | XmlPullParser parser = Xml.newPullParser(); |
| 589 | parser.setInput(new BufferedInputStream(new ByteArrayInputStream(baos.toByteArray())), |
| 590 | null); |
| 591 | parser.nextTag(); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 592 | mHelper.readXml(parser, true, UserHandle.USER_SYSTEM); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 593 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 594 | assertNull(mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1.getId(), false)); |
| 595 | assertNull(mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3.getId(), false)); |
| 596 | assertNull(mHelper.getNotificationChannelGroup(ncg.getId(), PKG_N_MR1, UID_N_MR1)); |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 597 | assertEquals(channel2, |
| 598 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2.getId(), false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | @Test |
| 602 | public void testChannelXml_defaultChannelLegacyApp_noUserSettings() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 603 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, false, |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 604 | UserHandle.USER_ALL, NotificationChannel.DEFAULT_CHANNEL_ID); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 605 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 606 | loadStreamXml(baos, false, UserHandle.USER_ALL); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 607 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 608 | final NotificationChannel updated = mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 609 | NotificationChannel.DEFAULT_CHANNEL_ID, false); |
| 610 | assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, updated.getImportance()); |
| 611 | assertFalse(updated.canBypassDnd()); |
| 612 | assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE, updated.getLockscreenVisibility()); |
| 613 | assertEquals(0, updated.getUserLockedFields()); |
| 614 | } |
| 615 | |
| 616 | @Test |
| 617 | public void testChannelXml_defaultChannelUpdatedApp_userSettings() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 618 | final NotificationChannel defaultChannel = mHelper.getNotificationChannel(PKG_N_MR1, |
| 619 | UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 620 | NotificationChannel.DEFAULT_CHANNEL_ID, false); |
| 621 | defaultChannel.setImportance(NotificationManager.IMPORTANCE_LOW); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 622 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, defaultChannel, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 623 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 624 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, false, |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 625 | UserHandle.USER_ALL, NotificationChannel.DEFAULT_CHANNEL_ID); |
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 | loadStreamXml(baos, false, UserHandle.USER_ALL); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 628 | |
| 629 | assertEquals(NotificationManager.IMPORTANCE_LOW, mHelper.getNotificationChannel( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 630 | PKG_N_MR1, UID_N_MR1, NotificationChannel.DEFAULT_CHANNEL_ID, false).getImportance()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | @Test |
| 634 | public void testChannelXml_upgradeCreateDefaultChannel() throws Exception { |
| 635 | final String preupgradeXml = "<ranking version=\"1\">\n" |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 636 | + "<package name=\"" + PKG_N_MR1 |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 637 | + "\" importance=\"" + NotificationManager.IMPORTANCE_HIGH |
| 638 | + "\" priority=\"" + Notification.PRIORITY_MAX + "\" visibility=\"" |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 639 | + Notification.VISIBILITY_SECRET + "\"" +" uid=\"" + UID_N_MR1 + "\" />\n" |
| 640 | + "<package name=\"" + PKG_O + "\" uid=\"" + UID_O + "\" visibility=\"" |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 641 | + Notification.VISIBILITY_PRIVATE + "\" />\n" |
| 642 | + "</ranking>"; |
| 643 | XmlPullParser parser = Xml.newPullParser(); |
| 644 | parser.setInput(new BufferedInputStream(new ByteArrayInputStream(preupgradeXml.getBytes())), |
| 645 | null); |
| 646 | parser.nextTag(); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 647 | mHelper.readXml(parser, false, UserHandle.USER_ALL); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 648 | |
| 649 | final NotificationChannel updated1 = |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 650 | 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] | 651 | assertEquals(NotificationManager.IMPORTANCE_HIGH, updated1.getImportance()); |
| 652 | assertTrue(updated1.canBypassDnd()); |
| 653 | assertEquals(Notification.VISIBILITY_SECRET, updated1.getLockscreenVisibility()); |
| 654 | assertEquals(NotificationChannel.USER_LOCKED_IMPORTANCE |
| 655 | | NotificationChannel.USER_LOCKED_PRIORITY |
| 656 | | NotificationChannel.USER_LOCKED_VISIBILITY, |
| 657 | updated1.getUserLockedFields()); |
| 658 | |
| 659 | // No Default Channel created for updated packages |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 660 | assertEquals(null, mHelper.getNotificationChannel(PKG_O, UID_O, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 661 | NotificationChannel.DEFAULT_CHANNEL_ID, false)); |
| 662 | } |
| 663 | |
| 664 | @Test |
| 665 | public void testChannelXml_upgradeDeletesDefaultChannel() throws Exception { |
| 666 | final NotificationChannel defaultChannel = mHelper.getNotificationChannel( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 667 | PKG_N_MR1, UID_N_MR1, NotificationChannel.DEFAULT_CHANNEL_ID, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 668 | assertTrue(defaultChannel != null); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 669 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, false, |
| 670 | UserHandle.USER_ALL, NotificationChannel.DEFAULT_CHANNEL_ID); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 671 | // Load package at higher sdk. |
| 672 | final ApplicationInfo upgraded = new ApplicationInfo(); |
| 673 | upgraded.targetSdkVersion = Build.VERSION_CODES.N_MR1 + 1; |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 674 | when(mPm.getApplicationInfoAsUser(eq(PKG_N_MR1), anyInt(), anyInt())).thenReturn(upgraded); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 675 | loadStreamXml(baos, false, UserHandle.USER_ALL); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 676 | |
| 677 | // Default Channel should be gone. |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 678 | assertEquals(null, mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 679 | NotificationChannel.DEFAULT_CHANNEL_ID, false)); |
| 680 | } |
| 681 | |
| 682 | @Test |
| 683 | public void testDeletesDefaultChannelAfterChannelIsCreated() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 684 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 685 | new NotificationChannel("bananas", "bananas", IMPORTANCE_LOW), true, false); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 686 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, false, |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 687 | UserHandle.USER_ALL, NotificationChannel.DEFAULT_CHANNEL_ID, "bananas"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 688 | |
| 689 | // Load package at higher sdk. |
| 690 | final ApplicationInfo upgraded = new ApplicationInfo(); |
| 691 | upgraded.targetSdkVersion = Build.VERSION_CODES.N_MR1 + 1; |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 692 | when(mPm.getApplicationInfoAsUser(eq(PKG_N_MR1), anyInt(), anyInt())).thenReturn(upgraded); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 693 | loadStreamXml(baos, false, UserHandle.USER_ALL); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 694 | |
| 695 | // Default Channel should be gone. |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 696 | assertEquals(null, mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 697 | NotificationChannel.DEFAULT_CHANNEL_ID, false)); |
| 698 | } |
| 699 | |
| 700 | @Test |
| 701 | public void testLoadingOldChannelsDoesNotDeleteNewlyCreatedChannels() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 702 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, false, |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 703 | UserHandle.USER_ALL, NotificationChannel.DEFAULT_CHANNEL_ID, "bananas"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 704 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 705 | new NotificationChannel("bananas", "bananas", IMPORTANCE_LOW), true, false); |
| 706 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 707 | loadStreamXml(baos, false, UserHandle.USER_ALL); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 708 | |
| 709 | // 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] | 710 | assertTrue(mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "bananas", false) != null); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | @Test |
| 714 | public void testCreateChannel_blocked() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 715 | mHelper.setImportance(PKG_N_MR1, UID_N_MR1, IMPORTANCE_NONE); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 716 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 717 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 718 | new NotificationChannel("bananas", "bananas", IMPORTANCE_LOW), true, false); |
| 719 | } |
| 720 | |
| 721 | @Test |
| 722 | public void testCreateChannel_badImportance() throws Exception { |
| 723 | try { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 724 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 725 | new NotificationChannel("bananas", "bananas", IMPORTANCE_NONE - 1), |
| 726 | true, false); |
| 727 | fail("Was allowed to create a channel with invalid importance"); |
| 728 | } catch (IllegalArgumentException e) { |
| 729 | // yay |
| 730 | } |
| 731 | try { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 732 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 733 | new NotificationChannel("bananas", "bananas", IMPORTANCE_UNSPECIFIED), |
| 734 | true, false); |
| 735 | fail("Was allowed to create a channel with invalid importance"); |
| 736 | } catch (IllegalArgumentException e) { |
| 737 | // yay |
| 738 | } |
| 739 | try { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 740 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 741 | new NotificationChannel("bananas", "bananas", IMPORTANCE_MAX + 1), |
| 742 | true, false); |
| 743 | fail("Was allowed to create a channel with invalid importance"); |
| 744 | } catch (IllegalArgumentException e) { |
| 745 | // yay |
| 746 | } |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 747 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 748 | new NotificationChannel("bananas", "bananas", IMPORTANCE_NONE), true, false); |
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_MAX), true, false); |
| 751 | } |
| 752 | |
| 753 | |
| 754 | @Test |
| 755 | public void testUpdate() throws Exception { |
| 756 | // no fields locked by user |
| 757 | final NotificationChannel channel = |
| 758 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 759 | channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes); |
| 760 | channel.enableLights(true); |
| 761 | channel.setBypassDnd(true); |
| 762 | channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 763 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 764 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, false, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 765 | |
| 766 | // same id, try to update all fields |
| 767 | final NotificationChannel channel2 = |
| 768 | new NotificationChannel("id2", "name2", NotificationManager.IMPORTANCE_HIGH); |
| 769 | channel2.setSound(new Uri.Builder().scheme("test2").build(), mAudioAttributes); |
| 770 | channel2.enableLights(false); |
| 771 | channel2.setBypassDnd(false); |
| 772 | channel2.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC); |
| 773 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 774 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 775 | |
| 776 | // all fields should be changed |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 777 | assertEquals(channel2, mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId(), false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 778 | |
| 779 | verify(mHandler, times(1)).requestSort(); |
| 780 | } |
| 781 | |
| 782 | @Test |
| 783 | public void testUpdate_preUpgrade_updatesAppFields() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 784 | mHelper.setImportance(PKG_N_MR1, UID_N_MR1, IMPORTANCE_UNSPECIFIED); |
| 785 | assertTrue(mHelper.canShowBadge(PKG_N_MR1, UID_N_MR1)); |
| 786 | assertEquals(Notification.PRIORITY_DEFAULT, mHelper.getPackagePriority(PKG_N_MR1, UID_N_MR1)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 787 | assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 788 | mHelper.getPackageVisibility(PKG_N_MR1, UID_N_MR1)); |
| 789 | assertFalse(mHelper.getIsAppImportanceLocked(PKG_N_MR1, UID_N_MR1)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 790 | |
| 791 | NotificationChannel defaultChannel = mHelper.getNotificationChannel( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 792 | PKG_N_MR1, UID_N_MR1, NotificationChannel.DEFAULT_CHANNEL_ID, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 793 | |
| 794 | defaultChannel.setShowBadge(false); |
| 795 | defaultChannel.setImportance(IMPORTANCE_NONE); |
| 796 | defaultChannel.setBypassDnd(true); |
| 797 | defaultChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 798 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 799 | mHelper.setAppImportanceLocked(PKG_N_MR1, UID_N_MR1); |
| 800 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, defaultChannel, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 801 | |
| 802 | // ensure app level fields are changed |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 803 | assertFalse(mHelper.canShowBadge(PKG_N_MR1, UID_N_MR1)); |
| 804 | assertEquals(Notification.PRIORITY_MAX, mHelper.getPackagePriority(PKG_N_MR1, UID_N_MR1)); |
| 805 | assertEquals(Notification.VISIBILITY_SECRET, mHelper.getPackageVisibility(PKG_N_MR1, |
| 806 | UID_N_MR1)); |
| 807 | assertEquals(IMPORTANCE_NONE, mHelper.getImportance(PKG_N_MR1, UID_N_MR1)); |
| 808 | assertTrue(mHelper.getIsAppImportanceLocked(PKG_N_MR1, UID_N_MR1)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | @Test |
| 812 | public void testUpdate_postUpgrade_noUpdateAppFields() throws Exception { |
| 813 | final NotificationChannel channel = new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 814 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 815 | mHelper.createNotificationChannel(PKG_O, UID_O, channel, false, false); |
| 816 | assertTrue(mHelper.canShowBadge(PKG_O, UID_O)); |
| 817 | assertEquals(Notification.PRIORITY_DEFAULT, mHelper.getPackagePriority(PKG_O, UID_O)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 818 | assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 819 | mHelper.getPackageVisibility(PKG_O, UID_O)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 820 | |
| 821 | channel.setShowBadge(false); |
| 822 | channel.setImportance(IMPORTANCE_NONE); |
| 823 | channel.setBypassDnd(true); |
| 824 | channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 825 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 826 | mHelper.updateNotificationChannel(PKG_O, UID_O, channel, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 827 | |
| 828 | // ensure app level fields are not changed |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 829 | assertTrue(mHelper.canShowBadge(PKG_O, UID_O)); |
| 830 | assertEquals(Notification.PRIORITY_DEFAULT, mHelper.getPackagePriority(PKG_O, UID_O)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 831 | assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 832 | mHelper.getPackageVisibility(PKG_O, UID_O)); |
| 833 | assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, mHelper.getImportance(PKG_O, |
| 834 | UID_O)); |
| 835 | assertFalse(mHelper.getIsAppImportanceLocked(PKG_O, UID_O)); |
| 836 | } |
| 837 | |
| 838 | @Test |
| 839 | public void testUpdate_preUpgrade_noUpdateAppFieldsWithMultipleChannels() throws Exception { |
| 840 | final NotificationChannel channel = new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 841 | |
| 842 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, false, false); |
| 843 | assertTrue(mHelper.canShowBadge(PKG_N_MR1, UID_N_MR1)); |
| 844 | assertEquals(Notification.PRIORITY_DEFAULT, mHelper.getPackagePriority(PKG_N_MR1, UID_N_MR1)); |
| 845 | assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE, |
| 846 | mHelper.getPackageVisibility(PKG_N_MR1, UID_N_MR1)); |
| 847 | |
| 848 | channel.setShowBadge(false); |
| 849 | channel.setImportance(IMPORTANCE_NONE); |
| 850 | channel.setBypassDnd(true); |
| 851 | channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 852 | |
| 853 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true); |
| 854 | |
| 855 | NotificationChannel defaultChannel = mHelper.getNotificationChannel( |
| 856 | PKG_N_MR1, UID_N_MR1, NotificationChannel.DEFAULT_CHANNEL_ID, false); |
| 857 | |
| 858 | defaultChannel.setShowBadge(false); |
| 859 | defaultChannel.setImportance(IMPORTANCE_NONE); |
| 860 | defaultChannel.setBypassDnd(true); |
| 861 | defaultChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 862 | |
| 863 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, defaultChannel, true); |
| 864 | |
| 865 | // ensure app level fields are not changed |
| 866 | assertTrue(mHelper.canShowBadge(PKG_N_MR1, UID_N_MR1)); |
| 867 | assertEquals(Notification.PRIORITY_DEFAULT, mHelper.getPackagePriority(PKG_N_MR1, UID_N_MR1)); |
| 868 | assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE, |
| 869 | mHelper.getPackageVisibility(PKG_N_MR1, UID_N_MR1)); |
| 870 | assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, mHelper.getImportance(PKG_N_MR1, |
| 871 | UID_N_MR1)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | @Test |
| 875 | public void testGetNotificationChannel_ReturnsNullForUnknownChannel() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 876 | assertEquals(null, mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "garbage", false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | @Test |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 880 | public void testCreateChannel_CannotChangeHiddenFields() { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 881 | final NotificationChannel channel = |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 882 | new NotificationChannel("id2", "name2", IMPORTANCE_HIGH); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 883 | channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes); |
| 884 | channel.enableLights(true); |
| 885 | channel.setBypassDnd(true); |
| 886 | channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 887 | channel.setShowBadge(true); |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 888 | channel.setAllowBubbles(false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 889 | int lockMask = 0; |
| 890 | for (int i = 0; i < NotificationChannel.LOCKABLE_FIELDS.length; i++) { |
| 891 | lockMask |= NotificationChannel.LOCKABLE_FIELDS[i]; |
| 892 | } |
| 893 | channel.lockFields(lockMask); |
| 894 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 895 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 896 | |
| 897 | NotificationChannel savedChannel = |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 898 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId(), false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 899 | |
| 900 | assertEquals(channel.getName(), savedChannel.getName()); |
| 901 | assertEquals(channel.shouldShowLights(), savedChannel.shouldShowLights()); |
| 902 | assertFalse(savedChannel.canBypassDnd()); |
| 903 | assertFalse(Notification.VISIBILITY_SECRET == savedChannel.getLockscreenVisibility()); |
| 904 | assertEquals(channel.canShowBadge(), savedChannel.canShowBadge()); |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 905 | assertEquals(channel.canBubble(), savedChannel.canBubble()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 906 | |
| 907 | verify(mHandler, never()).requestSort(); |
| 908 | } |
| 909 | |
| 910 | @Test |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 911 | public void testCreateChannel_CannotChangeHiddenFieldsAssistant() { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 912 | final NotificationChannel channel = |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 913 | new NotificationChannel("id2", "name2", IMPORTANCE_HIGH); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 914 | channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes); |
| 915 | channel.enableLights(true); |
| 916 | channel.setBypassDnd(true); |
| 917 | channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 918 | channel.setShowBadge(true); |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 919 | channel.setAllowBubbles(false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 920 | int lockMask = 0; |
| 921 | for (int i = 0; i < NotificationChannel.LOCKABLE_FIELDS.length; i++) { |
| 922 | lockMask |= NotificationChannel.LOCKABLE_FIELDS[i]; |
| 923 | } |
| 924 | channel.lockFields(lockMask); |
| 925 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 926 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 927 | |
| 928 | NotificationChannel savedChannel = |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 929 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId(), false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 930 | |
| 931 | assertEquals(channel.getName(), savedChannel.getName()); |
| 932 | assertEquals(channel.shouldShowLights(), savedChannel.shouldShowLights()); |
| 933 | assertFalse(savedChannel.canBypassDnd()); |
| 934 | assertFalse(Notification.VISIBILITY_SECRET == savedChannel.getLockscreenVisibility()); |
| 935 | assertEquals(channel.canShowBadge(), savedChannel.canShowBadge()); |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 936 | assertEquals(channel.canBubble(), savedChannel.canBubble()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | @Test |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 940 | public void testClearLockedFields() { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 941 | final NotificationChannel channel = getChannel(); |
| 942 | mHelper.clearLockedFields(channel); |
| 943 | assertEquals(0, channel.getUserLockedFields()); |
| 944 | |
| 945 | channel.lockFields(NotificationChannel.USER_LOCKED_PRIORITY |
| 946 | | NotificationChannel.USER_LOCKED_IMPORTANCE); |
| 947 | mHelper.clearLockedFields(channel); |
| 948 | assertEquals(0, channel.getUserLockedFields()); |
| 949 | } |
| 950 | |
| 951 | @Test |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 952 | public void testLockFields_soundAndVibration() { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 953 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel(), true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 954 | |
| 955 | final NotificationChannel update1 = getChannel(); |
| 956 | update1.setSound(new Uri.Builder().scheme("test").build(), |
| 957 | new AudioAttributes.Builder().build()); |
| 958 | update1.lockFields(NotificationChannel.USER_LOCKED_PRIORITY); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 959 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update1, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 960 | assertEquals(NotificationChannel.USER_LOCKED_PRIORITY |
| 961 | | NotificationChannel.USER_LOCKED_SOUND, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 962 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update1.getId(), false) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 963 | .getUserLockedFields()); |
| 964 | |
| 965 | NotificationChannel update2 = getChannel(); |
| 966 | update2.enableVibration(true); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 967 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update2, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 968 | assertEquals(NotificationChannel.USER_LOCKED_PRIORITY |
| 969 | | NotificationChannel.USER_LOCKED_SOUND |
| 970 | | NotificationChannel.USER_LOCKED_VIBRATION, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 971 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update2.getId(), false) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 972 | .getUserLockedFields()); |
| 973 | } |
| 974 | |
| 975 | @Test |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 976 | public void testLockFields_vibrationAndLights() { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 977 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel(), true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 978 | |
| 979 | final NotificationChannel update1 = getChannel(); |
| 980 | update1.setVibrationPattern(new long[]{7945, 46 ,246}); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 981 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update1, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 982 | assertEquals(NotificationChannel.USER_LOCKED_VIBRATION, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 983 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update1.getId(), false) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 984 | .getUserLockedFields()); |
| 985 | |
| 986 | final NotificationChannel update2 = getChannel(); |
| 987 | update2.enableLights(true); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 988 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update2, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 989 | assertEquals(NotificationChannel.USER_LOCKED_VIBRATION |
| 990 | | NotificationChannel.USER_LOCKED_LIGHTS, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 991 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update2.getId(), false) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 992 | .getUserLockedFields()); |
| 993 | } |
| 994 | |
| 995 | @Test |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 996 | public void testLockFields_lightsAndImportance() { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 997 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel(), true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 998 | |
| 999 | final NotificationChannel update1 = getChannel(); |
| 1000 | update1.setLightColor(Color.GREEN); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1001 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update1, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1002 | assertEquals(NotificationChannel.USER_LOCKED_LIGHTS, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1003 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update1.getId(), false) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1004 | .getUserLockedFields()); |
| 1005 | |
| 1006 | final NotificationChannel update2 = getChannel(); |
| 1007 | update2.setImportance(IMPORTANCE_DEFAULT); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1008 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update2, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1009 | assertEquals(NotificationChannel.USER_LOCKED_LIGHTS |
| 1010 | | NotificationChannel.USER_LOCKED_IMPORTANCE, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1011 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update2.getId(), false) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1012 | .getUserLockedFields()); |
| 1013 | } |
| 1014 | |
| 1015 | @Test |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1016 | public void testLockFields_visibilityAndDndAndBadge() { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1017 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel(), true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1018 | assertEquals(0, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1019 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel().getId(), false) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1020 | .getUserLockedFields()); |
| 1021 | |
| 1022 | final NotificationChannel update1 = getChannel(); |
| 1023 | update1.setBypassDnd(true); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1024 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update1, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1025 | assertEquals(NotificationChannel.USER_LOCKED_PRIORITY, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1026 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update1.getId(), false) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1027 | .getUserLockedFields()); |
| 1028 | |
| 1029 | final NotificationChannel update2 = getChannel(); |
| 1030 | update2.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1031 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update2, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1032 | assertEquals(NotificationChannel.USER_LOCKED_PRIORITY |
| 1033 | | NotificationChannel.USER_LOCKED_VISIBILITY, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1034 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update2.getId(), false) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1035 | .getUserLockedFields()); |
| 1036 | |
| 1037 | final NotificationChannel update3 = getChannel(); |
| 1038 | update3.setShowBadge(false); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1039 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update3, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1040 | assertEquals(NotificationChannel.USER_LOCKED_PRIORITY |
| 1041 | | NotificationChannel.USER_LOCKED_VISIBILITY |
| 1042 | | NotificationChannel.USER_LOCKED_SHOW_BADGE, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1043 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update3.getId(), false) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1044 | .getUserLockedFields()); |
| 1045 | } |
| 1046 | |
| 1047 | @Test |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 1048 | public void testLockFields_allowBubble() { |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1049 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel(), true, false); |
| 1050 | assertEquals(0, |
| 1051 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel().getId(), false) |
| 1052 | .getUserLockedFields()); |
| 1053 | |
| 1054 | final NotificationChannel update = getChannel(); |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 1055 | update.setAllowBubbles(false); |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1056 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update, true); |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 1057 | assertEquals(NotificationChannel.USER_LOCKED_ALLOW_BUBBLE, |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1058 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update.getId(), false) |
| 1059 | .getUserLockedFields()); |
| 1060 | } |
| 1061 | |
| 1062 | @Test |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1063 | public void testDeleteNonExistentChannel() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1064 | mHelper.deleteNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, "does not exist"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | @Test |
| 1068 | public void testGetDeletedChannel() throws Exception { |
| 1069 | NotificationChannel channel = getChannel(); |
| 1070 | channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes); |
| 1071 | channel.enableLights(true); |
| 1072 | channel.setBypassDnd(true); |
| 1073 | channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 1074 | channel.enableVibration(true); |
| 1075 | channel.setVibrationPattern(new long[]{100, 67, 145, 156}); |
| 1076 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1077 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
| 1078 | mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1079 | |
| 1080 | // Does not return deleted channel |
| 1081 | NotificationChannel response = |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1082 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId(), false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1083 | assertNull(response); |
| 1084 | |
| 1085 | // Returns deleted channel |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1086 | response = mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId(), true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1087 | compareChannels(channel, response); |
| 1088 | assertTrue(response.isDeleted()); |
| 1089 | } |
| 1090 | |
| 1091 | @Test |
| 1092 | public void testGetDeletedChannels() throws Exception { |
| 1093 | Map<String, NotificationChannel> channelMap = new HashMap<>(); |
| 1094 | NotificationChannel channel = |
| 1095 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 1096 | channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes); |
| 1097 | channel.enableLights(true); |
| 1098 | channel.setBypassDnd(true); |
| 1099 | channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); |
| 1100 | channel.enableVibration(true); |
| 1101 | channel.setVibrationPattern(new long[]{100, 67, 145, 156}); |
| 1102 | channelMap.put(channel.getId(), channel); |
| 1103 | NotificationChannel channel2 = |
| 1104 | new NotificationChannel("id4", "a", NotificationManager.IMPORTANCE_HIGH); |
| 1105 | channelMap.put(channel2.getId(), channel2); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1106 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
| 1107 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1108 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1109 | mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1110 | |
| 1111 | // Returns only non-deleted channels |
| 1112 | List<NotificationChannel> channels = |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1113 | mHelper.getNotificationChannels(PKG_N_MR1, UID_N_MR1, false).getList(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1114 | assertEquals(2, channels.size()); // Default channel + non-deleted channel |
| 1115 | for (NotificationChannel nc : channels) { |
| 1116 | if (!NotificationChannel.DEFAULT_CHANNEL_ID.equals(nc.getId())) { |
| 1117 | compareChannels(channel2, nc); |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | // Returns deleted channels too |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1122 | channels = mHelper.getNotificationChannels(PKG_N_MR1, UID_N_MR1, true).getList(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1123 | assertEquals(3, channels.size()); // Includes default channel |
| 1124 | for (NotificationChannel nc : channels) { |
| 1125 | if (!NotificationChannel.DEFAULT_CHANNEL_ID.equals(nc.getId())) { |
| 1126 | compareChannels(channelMap.get(nc.getId()), nc); |
| 1127 | } |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | @Test |
| 1132 | public void testGetDeletedChannelCount() throws Exception { |
| 1133 | NotificationChannel channel = |
| 1134 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 1135 | NotificationChannel channel2 = |
| 1136 | new NotificationChannel("id4", "a", NotificationManager.IMPORTANCE_HIGH); |
| 1137 | NotificationChannel channel3 = |
| 1138 | new NotificationChannel("id5", "a", NotificationManager.IMPORTANCE_HIGH); |
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.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, true, false); |
| 1141 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1142 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1143 | mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId()); |
| 1144 | mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1145 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1146 | assertEquals(2, mHelper.getDeletedChannelCount(PKG_N_MR1, UID_N_MR1)); |
| 1147 | assertEquals(0, mHelper.getDeletedChannelCount("pkg2", UID_O)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | @Test |
| 1151 | public void testGetBlockedChannelCount() throws Exception { |
| 1152 | NotificationChannel channel = |
| 1153 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 1154 | NotificationChannel channel2 = |
| 1155 | new NotificationChannel("id4", "a", NotificationManager.IMPORTANCE_NONE); |
| 1156 | NotificationChannel channel3 = |
| 1157 | new NotificationChannel("id5", "a", NotificationManager.IMPORTANCE_NONE); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1158 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
| 1159 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, true, false); |
| 1160 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1161 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1162 | mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1163 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1164 | assertEquals(1, mHelper.getBlockedChannelCount(PKG_N_MR1, UID_N_MR1)); |
| 1165 | assertEquals(0, mHelper.getBlockedChannelCount("pkg2", UID_O)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | @Test |
Beverly | 4f7b53d | 2018-11-20 09:56:31 -0500 | [diff] [blame] | 1169 | public void testUpdateChannelsBypassingDnd_onUserSwitch_onUserUnlocked() throws Exception { |
| 1170 | int user = USER.getIdentifier(); |
| 1171 | NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1"); |
| 1172 | NotificationChannel channel1 = new NotificationChannel("id1", "name1", |
| 1173 | NotificationManager.IMPORTANCE_MAX); |
| 1174 | channel1.setBypassDnd(true); |
| 1175 | channel1.setGroup(ncg.getId()); |
| 1176 | |
| 1177 | // channel is associated with a group, then group is deleted |
| 1178 | mHelper.createNotificationChannelGroup(PKG_N_MR1, user, ncg, /* fromTargetApp */ true); |
| 1179 | mHelper.createNotificationChannel(PKG_N_MR1, user, channel1, true, /*has DND access*/ true); |
| 1180 | mHelper.deleteNotificationChannelGroup(PKG_N_MR1, user, ncg.getId()); |
| 1181 | |
| 1182 | mHelper.onUserSwitched(user); |
| 1183 | mHelper.onUserUnlocked(user); |
| 1184 | } |
| 1185 | |
| 1186 | @Test |
Beverly | 0479cde2 | 2018-11-09 11:05:34 -0500 | [diff] [blame] | 1187 | public void testGetChannelsBypassingDndCount_noChannelsBypassing() throws Exception { |
| 1188 | assertEquals(0, mHelper.getNotificationChannelsBypassingDnd(PKG_N_MR1, |
| 1189 | USER.getIdentifier()).getList().size()); |
| 1190 | } |
| 1191 | |
| 1192 | @Test |
| 1193 | public void testGetChannelsBypassingDnd_noChannelsForUserIdBypassing() |
| 1194 | throws Exception { |
| 1195 | int user = 9; |
| 1196 | NotificationChannel channel = new NotificationChannel("id", "name", |
| 1197 | NotificationManager.IMPORTANCE_MAX); |
| 1198 | channel.setBypassDnd(true); |
| 1199 | mHelper.createNotificationChannel(PKG_N_MR1, 111, channel, true, true); |
| 1200 | |
| 1201 | assertEquals(0, mHelper.getNotificationChannelsBypassingDnd(PKG_N_MR1, |
| 1202 | user).getList().size()); |
| 1203 | } |
| 1204 | |
| 1205 | @Test |
| 1206 | public void testGetChannelsBypassingDndCount_oneChannelBypassing_groupBlocked() { |
| 1207 | int user = USER.getIdentifier(); |
| 1208 | NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1"); |
| 1209 | NotificationChannel channel1 = new NotificationChannel("id1", "name1", |
| 1210 | NotificationManager.IMPORTANCE_MAX); |
| 1211 | channel1.setBypassDnd(true); |
| 1212 | channel1.setGroup(ncg.getId()); |
| 1213 | mHelper.createNotificationChannelGroup(PKG_N_MR1, user, ncg, /* fromTargetApp */ true); |
| 1214 | mHelper.createNotificationChannel(PKG_N_MR1, user, channel1, true, /*has DND access*/ true); |
| 1215 | |
| 1216 | assertEquals(1, mHelper.getNotificationChannelsBypassingDnd(PKG_N_MR1, |
| 1217 | user).getList().size()); |
| 1218 | |
| 1219 | // disable group |
| 1220 | ncg.setBlocked(true); |
| 1221 | mHelper.createNotificationChannelGroup(PKG_N_MR1, user, ncg, /* fromTargetApp */ false); |
| 1222 | assertEquals(0, mHelper.getNotificationChannelsBypassingDnd(PKG_N_MR1, |
| 1223 | user).getList().size()); |
| 1224 | } |
| 1225 | |
| 1226 | @Test |
| 1227 | public void testGetChannelsBypassingDndCount_multipleChannelsBypassing() { |
| 1228 | int user = USER.getIdentifier(); |
| 1229 | NotificationChannel channel1 = new NotificationChannel("id1", "name1", |
| 1230 | NotificationManager.IMPORTANCE_MAX); |
| 1231 | NotificationChannel channel2 = new NotificationChannel("id2", "name2", |
| 1232 | NotificationManager.IMPORTANCE_MAX); |
| 1233 | NotificationChannel channel3 = new NotificationChannel("id3", "name3", |
| 1234 | NotificationManager.IMPORTANCE_MAX); |
| 1235 | channel1.setBypassDnd(true); |
| 1236 | channel2.setBypassDnd(true); |
| 1237 | channel3.setBypassDnd(true); |
| 1238 | // has DND access, so can set bypassDnd attribute |
| 1239 | mHelper.createNotificationChannel(PKG_N_MR1, user, channel1, true, /*has DND access*/ true); |
| 1240 | mHelper.createNotificationChannel(PKG_N_MR1, user, channel2, true, true); |
| 1241 | mHelper.createNotificationChannel(PKG_N_MR1, user, channel3, true, true); |
| 1242 | assertEquals(3, mHelper.getNotificationChannelsBypassingDnd(PKG_N_MR1, |
| 1243 | user).getList().size()); |
| 1244 | |
| 1245 | // block notifications from this app |
| 1246 | mHelper.setEnabled(PKG_N_MR1, user, false); |
| 1247 | assertEquals(0, mHelper.getNotificationChannelsBypassingDnd(PKG_N_MR1, |
| 1248 | user).getList().size()); |
| 1249 | |
| 1250 | // re-enable notifications from this app |
| 1251 | mHelper.setEnabled(PKG_N_MR1, user, true); |
| 1252 | assertEquals(3, mHelper.getNotificationChannelsBypassingDnd(PKG_N_MR1, |
| 1253 | user).getList().size()); |
| 1254 | |
| 1255 | // setBypassDnd false for some channels |
| 1256 | channel1.setBypassDnd(false); |
| 1257 | channel2.setBypassDnd(false); |
| 1258 | assertEquals(1, mHelper.getNotificationChannelsBypassingDnd(PKG_N_MR1, |
| 1259 | user).getList().size()); |
| 1260 | |
| 1261 | // setBypassDnd false for rest of the channels |
| 1262 | channel3.setBypassDnd(false); |
| 1263 | assertEquals(0, mHelper.getNotificationChannelsBypassingDnd(PKG_N_MR1, |
| 1264 | user).getList().size()); |
| 1265 | } |
| 1266 | |
| 1267 | @Test |
| 1268 | public void testGetAppsBypassingDndCount_noAppsBypassing() throws Exception { |
| 1269 | assertEquals(0, mHelper.getAppsBypassingDndCount(USER.getIdentifier())); |
| 1270 | } |
| 1271 | |
| 1272 | @Test |
| 1273 | public void testGetAppsBypassingDndCount_noAppsForUserIdBypassing() throws Exception { |
| 1274 | int user = 9; |
| 1275 | NotificationChannel channel = new NotificationChannel("id", "name", |
| 1276 | NotificationManager.IMPORTANCE_MAX); |
| 1277 | channel.setBypassDnd(true); |
| 1278 | mHelper.createNotificationChannel(PKG_N_MR1, 111, channel, true, true); |
| 1279 | |
| 1280 | assertEquals(0, mHelper.getAppsBypassingDndCount(user)); |
| 1281 | } |
| 1282 | |
| 1283 | @Test |
| 1284 | public void testGetAppsBypassingDndCount_oneChannelBypassing_groupBlocked() { |
| 1285 | int user = USER.getIdentifier(); |
| 1286 | NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1"); |
| 1287 | NotificationChannel channel1 = new NotificationChannel("id1", "name1", |
| 1288 | NotificationManager.IMPORTANCE_MAX); |
| 1289 | channel1.setBypassDnd(true); |
| 1290 | channel1.setGroup(ncg.getId()); |
| 1291 | mHelper.createNotificationChannelGroup(PKG_N_MR1, user, ncg, /* fromTargetApp */ true); |
| 1292 | mHelper.createNotificationChannel(PKG_N_MR1, user, channel1, true, /*has DND access*/ true); |
| 1293 | |
| 1294 | assertEquals(1, mHelper.getAppsBypassingDndCount(user)); |
| 1295 | |
| 1296 | // disable group |
| 1297 | ncg.setBlocked(true); |
| 1298 | mHelper.createNotificationChannelGroup(PKG_N_MR1, user, ncg, /* fromTargetApp */ false); |
| 1299 | assertEquals(0, mHelper.getAppsBypassingDndCount(user)); |
| 1300 | } |
| 1301 | |
| 1302 | @Test |
| 1303 | public void testGetAppsBypassingDndCount_oneAppBypassing() { |
| 1304 | int user = USER.getIdentifier(); |
| 1305 | NotificationChannel channel1 = new NotificationChannel("id1", "name1", |
| 1306 | NotificationManager.IMPORTANCE_MAX); |
| 1307 | NotificationChannel channel2 = new NotificationChannel("id2", "name2", |
| 1308 | NotificationManager.IMPORTANCE_MAX); |
| 1309 | NotificationChannel channel3 = new NotificationChannel("id3", "name3", |
| 1310 | NotificationManager.IMPORTANCE_MAX); |
| 1311 | channel1.setBypassDnd(true); |
| 1312 | channel2.setBypassDnd(true); |
| 1313 | channel3.setBypassDnd(true); |
| 1314 | // has DND access, so can set bypassDnd attribute |
| 1315 | mHelper.createNotificationChannel(PKG_N_MR1, user, channel1, true, /*has DND access*/ true); |
| 1316 | mHelper.createNotificationChannel(PKG_N_MR1, user, channel2, true, true); |
| 1317 | mHelper.createNotificationChannel(PKG_N_MR1, user, channel3, true, true); |
| 1318 | assertEquals(1, mHelper.getAppsBypassingDndCount(user)); |
| 1319 | |
| 1320 | // block notifications from this app |
| 1321 | mHelper.setEnabled(PKG_N_MR1, user, false); |
| 1322 | assertEquals(0, mHelper.getAppsBypassingDndCount(user)); // no apps can bypass dnd |
| 1323 | |
| 1324 | // re-enable notifications from this app |
| 1325 | mHelper.setEnabled(PKG_N_MR1, user, true); |
| 1326 | assertEquals(1, mHelper.getAppsBypassingDndCount(user)); |
| 1327 | |
| 1328 | // setBypassDnd false for some channels |
| 1329 | channel1.setBypassDnd(false); |
| 1330 | channel2.setBypassDnd(false); |
| 1331 | assertEquals(1, mHelper.getAppsBypassingDndCount(user)); |
| 1332 | |
| 1333 | // setBypassDnd false for rest of the channels |
| 1334 | channel3.setBypassDnd(false); |
| 1335 | assertEquals(0, mHelper.getAppsBypassingDndCount(user)); |
| 1336 | } |
| 1337 | |
| 1338 | @Test |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1339 | public void testCreateAndDeleteCanChannelsBypassDnd() throws Exception { |
| 1340 | // create notification channel that can't bypass dnd |
| 1341 | // expected result: areChannelsBypassingDnd = false |
| 1342 | // setNotificationPolicy isn't called since areChannelsBypassingDnd was already false |
| 1343 | NotificationChannel channel = new NotificationChannel("id1", "name1", IMPORTANCE_LOW); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1344 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1345 | assertFalse(mHelper.areChannelsBypassingDnd()); |
| 1346 | verify(mMockZenModeHelper, never()).setNotificationPolicy(any()); |
| 1347 | resetZenModeHelper(); |
| 1348 | |
| 1349 | // create notification channel that can bypass dnd |
| 1350 | // expected result: areChannelsBypassingDnd = true |
| 1351 | NotificationChannel channel2 = new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 1352 | channel2.setBypassDnd(true); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1353 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, true, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1354 | assertTrue(mHelper.areChannelsBypassingDnd()); |
| 1355 | verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any()); |
| 1356 | resetZenModeHelper(); |
| 1357 | |
| 1358 | // delete channels |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1359 | mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1360 | assertTrue(mHelper.areChannelsBypassingDnd()); // channel2 can still bypass DND |
| 1361 | verify(mMockZenModeHelper, never()).setNotificationPolicy(any()); |
| 1362 | resetZenModeHelper(); |
| 1363 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1364 | mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1365 | assertFalse(mHelper.areChannelsBypassingDnd()); |
| 1366 | verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any()); |
| 1367 | resetZenModeHelper(); |
| 1368 | } |
| 1369 | |
| 1370 | @Test |
| 1371 | public void testUpdateCanChannelsBypassDnd() throws Exception { |
| 1372 | // create notification channel that can't bypass dnd |
| 1373 | // expected result: areChannelsBypassingDnd = false |
| 1374 | // setNotificationPolicy isn't called since areChannelsBypassingDnd was already false |
| 1375 | NotificationChannel channel = new NotificationChannel("id1", "name1", IMPORTANCE_LOW); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1376 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1377 | assertFalse(mHelper.areChannelsBypassingDnd()); |
| 1378 | verify(mMockZenModeHelper, never()).setNotificationPolicy(any()); |
| 1379 | resetZenModeHelper(); |
| 1380 | |
| 1381 | // update channel so it CAN bypass dnd: |
| 1382 | // expected result: areChannelsBypassingDnd = true |
| 1383 | channel.setBypassDnd(true); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1384 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1385 | assertTrue(mHelper.areChannelsBypassingDnd()); |
| 1386 | verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any()); |
| 1387 | resetZenModeHelper(); |
| 1388 | |
| 1389 | // update channel so it can't bypass dnd: |
| 1390 | // expected result: areChannelsBypassingDnd = false |
| 1391 | channel.setBypassDnd(false); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1392 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1393 | assertFalse(mHelper.areChannelsBypassingDnd()); |
| 1394 | verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any()); |
| 1395 | resetZenModeHelper(); |
| 1396 | } |
| 1397 | |
| 1398 | @Test |
| 1399 | public void testSetupNewZenModeHelper_canBypass() { |
| 1400 | // start notification policy off with mAreChannelsBypassingDnd = true, but |
| 1401 | // RankingHelper should change to false |
| 1402 | mTestNotificationPolicy = new NotificationManager.Policy(0, 0, 0, 0, |
| 1403 | NotificationManager.Policy.STATE_CHANNELS_BYPASSING_DND); |
| 1404 | when(mMockZenModeHelper.getNotificationPolicy()).thenReturn(mTestNotificationPolicy); |
| 1405 | mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper); |
| 1406 | assertFalse(mHelper.areChannelsBypassingDnd()); |
| 1407 | verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any()); |
| 1408 | resetZenModeHelper(); |
| 1409 | } |
| 1410 | |
| 1411 | @Test |
| 1412 | public void testSetupNewZenModeHelper_cannotBypass() { |
| 1413 | // start notification policy off with mAreChannelsBypassingDnd = false |
| 1414 | mTestNotificationPolicy = new NotificationManager.Policy(0, 0, 0, 0, 0); |
| 1415 | when(mMockZenModeHelper.getNotificationPolicy()).thenReturn(mTestNotificationPolicy); |
| 1416 | mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper); |
| 1417 | assertFalse(mHelper.areChannelsBypassingDnd()); |
| 1418 | verify(mMockZenModeHelper, never()).setNotificationPolicy(any()); |
| 1419 | resetZenModeHelper(); |
| 1420 | } |
| 1421 | |
| 1422 | @Test |
| 1423 | public void testCreateDeletedChannel() throws Exception { |
| 1424 | long[] vibration = new long[]{100, 67, 145, 156}; |
| 1425 | NotificationChannel channel = |
| 1426 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 1427 | channel.setVibrationPattern(vibration); |
| 1428 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1429 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
| 1430 | mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1431 | |
| 1432 | NotificationChannel newChannel = new NotificationChannel( |
| 1433 | channel.getId(), channel.getName(), NotificationManager.IMPORTANCE_HIGH); |
| 1434 | newChannel.setVibrationPattern(new long[]{100}); |
| 1435 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1436 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, newChannel, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1437 | |
| 1438 | // No long deleted, using old settings |
| 1439 | compareChannels(channel, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1440 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, newChannel.getId(), false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1441 | } |
| 1442 | |
| 1443 | @Test |
| 1444 | public void testOnlyHasDefaultChannel() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1445 | assertTrue(mHelper.onlyHasDefaultChannel(PKG_N_MR1, UID_N_MR1)); |
| 1446 | assertFalse(mHelper.onlyHasDefaultChannel(PKG_O, UID_O)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1447 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1448 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel(), true, false); |
| 1449 | assertFalse(mHelper.onlyHasDefaultChannel(PKG_N_MR1, UID_N_MR1)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1450 | } |
| 1451 | |
| 1452 | @Test |
| 1453 | public void testCreateChannel_defaultChannelId() throws Exception { |
| 1454 | try { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1455 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, new NotificationChannel( |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1456 | NotificationChannel.DEFAULT_CHANNEL_ID, "ha", IMPORTANCE_HIGH), true, false); |
| 1457 | fail("Allowed to create default channel"); |
| 1458 | } catch (IllegalArgumentException e) { |
| 1459 | // pass |
| 1460 | } |
| 1461 | } |
| 1462 | |
| 1463 | @Test |
| 1464 | public void testCreateChannel_alreadyExists() throws Exception { |
| 1465 | long[] vibration = new long[]{100, 67, 145, 156}; |
| 1466 | NotificationChannel channel = |
| 1467 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 1468 | channel.setVibrationPattern(vibration); |
| 1469 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1470 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1471 | |
| 1472 | NotificationChannel newChannel = new NotificationChannel( |
| 1473 | channel.getId(), channel.getName(), NotificationManager.IMPORTANCE_HIGH); |
| 1474 | newChannel.setVibrationPattern(new long[]{100}); |
| 1475 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1476 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, newChannel, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1477 | |
| 1478 | // Old settings not overridden |
| 1479 | compareChannels(channel, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1480 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, newChannel.getId(), false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | @Test |
| 1484 | public void testCreateChannel_noOverrideSound() throws Exception { |
| 1485 | Uri sound = new Uri.Builder().scheme("test").build(); |
| 1486 | final NotificationChannel channel = new NotificationChannel("id2", "name2", |
| 1487 | NotificationManager.IMPORTANCE_DEFAULT); |
| 1488 | channel.setSound(sound, mAudioAttributes); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1489 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1490 | assertEquals(sound, mHelper.getNotificationChannel( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1491 | PKG_N_MR1, UID_N_MR1, channel.getId(), false).getSound()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | @Test |
| 1495 | public void testPermanentlyDeleteChannels() throws Exception { |
| 1496 | NotificationChannel channel1 = |
| 1497 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 1498 | NotificationChannel channel2 = |
| 1499 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 1500 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1501 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false); |
| 1502 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, false, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1503 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1504 | mHelper.permanentlyDeleteNotificationChannels(PKG_N_MR1, UID_N_MR1); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1505 | |
| 1506 | // Only default channel remains |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1507 | 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] | 1508 | } |
| 1509 | |
| 1510 | @Test |
| 1511 | public void testDeleteGroup() throws Exception { |
| 1512 | NotificationChannelGroup notDeleted = new NotificationChannelGroup("not", "deleted"); |
| 1513 | NotificationChannelGroup deleted = new NotificationChannelGroup("totally", "deleted"); |
| 1514 | NotificationChannel nonGroupedNonDeletedChannel = |
| 1515 | new NotificationChannel("no group", "so not deleted", IMPORTANCE_HIGH); |
| 1516 | NotificationChannel groupedButNotDeleted = |
| 1517 | new NotificationChannel("not deleted", "belongs to notDeleted", IMPORTANCE_DEFAULT); |
| 1518 | groupedButNotDeleted.setGroup("not"); |
| 1519 | NotificationChannel groupedAndDeleted = |
| 1520 | new NotificationChannel("deleted", "belongs to deleted", IMPORTANCE_DEFAULT); |
| 1521 | groupedAndDeleted.setGroup("totally"); |
| 1522 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1523 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, notDeleted, true); |
| 1524 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, deleted, true); |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1525 | mHelper.createNotificationChannel( |
| 1526 | PKG_N_MR1, UID_N_MR1, nonGroupedNonDeletedChannel, true, false); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1527 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, groupedAndDeleted, true, false); |
| 1528 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, groupedButNotDeleted, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1529 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1530 | mHelper.deleteNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, deleted.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1531 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1532 | assertNull(mHelper.getNotificationChannelGroup(deleted.getId(), PKG_N_MR1, UID_N_MR1)); |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1533 | assertNotNull( |
| 1534 | mHelper.getNotificationChannelGroup(notDeleted.getId(), PKG_N_MR1, UID_N_MR1)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1535 | |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1536 | assertNull(mHelper.getNotificationChannel( |
| 1537 | PKG_N_MR1, UID_N_MR1, groupedAndDeleted.getId(), false)); |
| 1538 | compareChannels(groupedAndDeleted, mHelper.getNotificationChannel( |
| 1539 | PKG_N_MR1, UID_N_MR1, groupedAndDeleted.getId(), true)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1540 | |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1541 | compareChannels(groupedButNotDeleted, mHelper.getNotificationChannel( |
| 1542 | PKG_N_MR1, UID_N_MR1, groupedButNotDeleted.getId(), false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1543 | compareChannels(nonGroupedNonDeletedChannel, mHelper.getNotificationChannel( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1544 | PKG_N_MR1, UID_N_MR1, nonGroupedNonDeletedChannel.getId(), false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1545 | |
| 1546 | // notDeleted |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1547 | assertEquals(1, mHelper.getNotificationChannelGroups(PKG_N_MR1, UID_N_MR1).size()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1548 | |
| 1549 | verify(mHandler, never()).requestSort(); |
| 1550 | } |
| 1551 | |
| 1552 | @Test |
| 1553 | public void testOnUserRemoved() throws Exception { |
| 1554 | int[] user0Uids = {98, 235, 16, 3782}; |
| 1555 | int[] user1Uids = new int[user0Uids.length]; |
| 1556 | for (int i = 0; i < user0Uids.length; i++) { |
| 1557 | user1Uids[i] = UserHandle.PER_USER_RANGE + user0Uids[i]; |
| 1558 | |
| 1559 | final ApplicationInfo legacy = new ApplicationInfo(); |
| 1560 | legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1; |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1561 | when(mPm.getApplicationInfoAsUser(eq(PKG_N_MR1), anyInt(), anyInt())).thenReturn(legacy); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1562 | |
| 1563 | // 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] | 1564 | mHelper.getImportance(PKG_N_MR1, user0Uids[i]); |
| 1565 | mHelper.getImportance(PKG_N_MR1, user1Uids[i]); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1566 | } |
| 1567 | |
| 1568 | mHelper.onUserRemoved(1); |
| 1569 | |
| 1570 | // user 0 records remain |
| 1571 | for (int i = 0; i < user0Uids.length; i++) { |
| 1572 | assertEquals(1, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1573 | mHelper.getNotificationChannels(PKG_N_MR1, user0Uids[i], false).getList().size()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1574 | } |
| 1575 | // user 1 records are gone |
| 1576 | for (int i = 0; i < user1Uids.length; i++) { |
| 1577 | assertEquals(0, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1578 | mHelper.getNotificationChannels(PKG_N_MR1, user1Uids[i], false).getList().size()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1579 | } |
| 1580 | } |
| 1581 | |
| 1582 | @Test |
| 1583 | public void testOnPackageChanged_packageRemoval() throws Exception { |
| 1584 | // Deleted |
| 1585 | NotificationChannel channel1 = |
| 1586 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1587 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1588 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1589 | mHelper.onPackagesChanged(true, UserHandle.USER_SYSTEM, new String[]{PKG_N_MR1}, new int[]{ |
| 1590 | UID_N_MR1}); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1591 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1592 | assertEquals(0, mHelper.getNotificationChannels(PKG_N_MR1, UID_N_MR1, true).getList().size()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1593 | |
| 1594 | // Not deleted |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1595 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1596 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1597 | mHelper.onPackagesChanged(false, UserHandle.USER_SYSTEM, new String[]{PKG_N_MR1}, new int[]{ |
| 1598 | UID_N_MR1}); |
| 1599 | 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] | 1600 | } |
| 1601 | |
| 1602 | @Test |
| 1603 | public void testOnPackageChanged_packageRemoval_importance() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1604 | mHelper.setImportance(PKG_N_MR1, UID_N_MR1, NotificationManager.IMPORTANCE_HIGH); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1605 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1606 | mHelper.onPackagesChanged(true, UserHandle.USER_SYSTEM, new String[]{PKG_N_MR1}, new int[]{ |
| 1607 | UID_N_MR1}); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1608 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1609 | assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, mHelper.getImportance(PKG_N_MR1, |
| 1610 | UID_N_MR1)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | @Test |
| 1614 | public void testOnPackageChanged_packageRemoval_groups() throws Exception { |
| 1615 | NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1616 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1617 | NotificationChannelGroup ncg2 = new NotificationChannelGroup("group2", "name2"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1618 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg2, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1619 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1620 | mHelper.onPackagesChanged(true, UserHandle.USER_SYSTEM, new String[]{PKG_N_MR1}, new int[]{ |
| 1621 | UID_N_MR1}); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1622 | |
Julia Reynolds | 13ed28b | 2018-09-21 15:20:13 -0400 | [diff] [blame] | 1623 | assertEquals(0, mHelper.getNotificationChannelGroups( |
| 1624 | PKG_N_MR1, UID_N_MR1, true, true, false).getList().size()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1625 | } |
| 1626 | |
| 1627 | @Test |
| 1628 | public void testOnPackageChange_downgradeTargetSdk() throws Exception { |
| 1629 | // create channel as api 26 |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1630 | mHelper.createNotificationChannel(PKG_O, UID_O, getChannel(), true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1631 | |
| 1632 | // install new app version targeting 25 |
| 1633 | final ApplicationInfo legacy = new ApplicationInfo(); |
| 1634 | legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1; |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1635 | when(mPm.getApplicationInfoAsUser(eq(PKG_O), anyInt(), anyInt())).thenReturn(legacy); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1636 | mHelper.onPackagesChanged( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1637 | false, UserHandle.USER_SYSTEM, new String[]{PKG_O}, new int[]{UID_O}); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1638 | |
| 1639 | // make sure the default channel was readded |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1640 | //assertEquals(2, mHelper.getNotificationChannels(PKG_O, UID_O, false).getList().size()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1641 | assertNotNull(mHelper.getNotificationChannel( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1642 | PKG_O, UID_O, NotificationChannel.DEFAULT_CHANNEL_ID, false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | @Test |
| 1646 | public void testRecordDefaults() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1647 | assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, mHelper.getImportance(PKG_N_MR1, |
| 1648 | UID_N_MR1)); |
| 1649 | assertEquals(true, mHelper.canShowBadge(PKG_N_MR1, UID_N_MR1)); |
| 1650 | 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] | 1651 | } |
| 1652 | |
| 1653 | @Test |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1654 | public void testCreateGroup() { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1655 | NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1656 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true); |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1657 | assertEquals(ncg, |
| 1658 | mHelper.getNotificationChannelGroups(PKG_N_MR1, UID_N_MR1).iterator().next()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1659 | verify(mHandler, never()).requestSort(); |
| 1660 | } |
| 1661 | |
| 1662 | @Test |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1663 | public void testCannotCreateChannel_badGroup() { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1664 | NotificationChannel channel1 = |
| 1665 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 1666 | channel1.setGroup("garbage"); |
| 1667 | try { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1668 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1669 | fail("Created a channel with a bad group"); |
| 1670 | } catch (IllegalArgumentException e) { |
| 1671 | } |
| 1672 | } |
| 1673 | |
| 1674 | @Test |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1675 | public void testCannotCreateChannel_goodGroup() { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1676 | NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1677 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1678 | NotificationChannel channel1 = |
| 1679 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 1680 | channel1.setGroup(ncg.getId()); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1681 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1682 | |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1683 | assertEquals(ncg.getId(), mHelper.getNotificationChannel( |
| 1684 | PKG_N_MR1, UID_N_MR1, channel1.getId(), false).getGroup()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1685 | } |
| 1686 | |
| 1687 | @Test |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1688 | public void testGetChannelGroups() { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1689 | NotificationChannelGroup unused = new NotificationChannelGroup("unused", "s"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1690 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, unused, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1691 | NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1692 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1693 | NotificationChannelGroup ncg2 = new NotificationChannelGroup("group2", "name2"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1694 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg2, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1695 | |
| 1696 | NotificationChannel channel1 = |
| 1697 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 1698 | channel1.setGroup(ncg.getId()); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1699 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1700 | NotificationChannel channel1a = |
| 1701 | new NotificationChannel("id1a", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 1702 | channel1a.setGroup(ncg.getId()); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1703 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1a, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1704 | |
| 1705 | NotificationChannel channel2 = |
| 1706 | new NotificationChannel("id2", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 1707 | channel2.setGroup(ncg2.getId()); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1708 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1709 | |
| 1710 | NotificationChannel channel3 = |
| 1711 | new NotificationChannel("id3", "name1", NotificationManager.IMPORTANCE_HIGH); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1712 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1713 | |
Julia Reynolds | 13ed28b | 2018-09-21 15:20:13 -0400 | [diff] [blame] | 1714 | List<NotificationChannelGroup> actual = mHelper.getNotificationChannelGroups( |
| 1715 | PKG_N_MR1, UID_N_MR1, true, true, false).getList(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1716 | assertEquals(3, actual.size()); |
| 1717 | for (NotificationChannelGroup group : actual) { |
| 1718 | if (group.getId() == null) { |
| 1719 | assertEquals(2, group.getChannels().size()); // misc channel too |
| 1720 | assertTrue(channel3.getId().equals(group.getChannels().get(0).getId()) |
| 1721 | || channel3.getId().equals(group.getChannels().get(1).getId())); |
| 1722 | } else if (group.getId().equals(ncg.getId())) { |
| 1723 | assertEquals(2, group.getChannels().size()); |
| 1724 | if (group.getChannels().get(0).getId().equals(channel1.getId())) { |
| 1725 | assertTrue(group.getChannels().get(1).getId().equals(channel1a.getId())); |
| 1726 | } else if (group.getChannels().get(0).getId().equals(channel1a.getId())) { |
| 1727 | assertTrue(group.getChannels().get(1).getId().equals(channel1.getId())); |
| 1728 | } else { |
| 1729 | fail("expected channel not found"); |
| 1730 | } |
| 1731 | } else if (group.getId().equals(ncg2.getId())) { |
| 1732 | assertEquals(1, group.getChannels().size()); |
| 1733 | assertEquals(channel2.getId(), group.getChannels().get(0).getId()); |
| 1734 | } |
| 1735 | } |
| 1736 | } |
| 1737 | |
| 1738 | @Test |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1739 | public void testGetChannelGroups_noSideEffects() { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1740 | NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1741 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1742 | |
| 1743 | NotificationChannel channel1 = |
| 1744 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 1745 | channel1.setGroup(ncg.getId()); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1746 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false); |
Julia Reynolds | 13ed28b | 2018-09-21 15:20:13 -0400 | [diff] [blame] | 1747 | mHelper.getNotificationChannelGroups(PKG_N_MR1, UID_N_MR1, true, true, false).getList(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1748 | |
| 1749 | channel1.setImportance(IMPORTANCE_LOW); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1750 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1751 | |
Julia Reynolds | 13ed28b | 2018-09-21 15:20:13 -0400 | [diff] [blame] | 1752 | List<NotificationChannelGroup> actual = mHelper.getNotificationChannelGroups( |
| 1753 | PKG_N_MR1, UID_N_MR1, true, true, false).getList(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1754 | |
| 1755 | assertEquals(2, actual.size()); |
| 1756 | for (NotificationChannelGroup group : actual) { |
| 1757 | if (Objects.equals(group.getId(), ncg.getId())) { |
| 1758 | assertEquals(1, group.getChannels().size()); |
| 1759 | } |
| 1760 | } |
| 1761 | } |
| 1762 | |
| 1763 | @Test |
Julia Reynolds | 13ed28b | 2018-09-21 15:20:13 -0400 | [diff] [blame] | 1764 | public void testGetChannelGroups_includeEmptyGroups() { |
| 1765 | NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1"); |
| 1766 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true); |
| 1767 | NotificationChannelGroup ncgEmpty = new NotificationChannelGroup("group2", "name2"); |
| 1768 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncgEmpty, true); |
| 1769 | |
| 1770 | NotificationChannel channel1 = |
| 1771 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 1772 | channel1.setGroup(ncg.getId()); |
| 1773 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false); |
| 1774 | |
| 1775 | List<NotificationChannelGroup> actual = mHelper.getNotificationChannelGroups( |
| 1776 | PKG_N_MR1, UID_N_MR1, false, false, true).getList(); |
| 1777 | |
| 1778 | assertEquals(2, actual.size()); |
| 1779 | for (NotificationChannelGroup group : actual) { |
| 1780 | if (Objects.equals(group.getId(), ncg.getId())) { |
| 1781 | assertEquals(1, group.getChannels().size()); |
| 1782 | } |
| 1783 | if (Objects.equals(group.getId(), ncgEmpty.getId())) { |
| 1784 | assertEquals(0, group.getChannels().size()); |
| 1785 | } |
| 1786 | } |
| 1787 | } |
| 1788 | |
| 1789 | @Test |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1790 | public void testCreateChannel_updateName() { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1791 | NotificationChannel nc = new NotificationChannel("id", "hello", IMPORTANCE_DEFAULT); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1792 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, nc, true, false); |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1793 | NotificationChannel actual = |
| 1794 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "id", false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1795 | assertEquals("hello", actual.getName()); |
| 1796 | |
| 1797 | nc = new NotificationChannel("id", "goodbye", IMPORTANCE_HIGH); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1798 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, nc, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1799 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1800 | actual = mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "id", false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1801 | assertEquals("goodbye", actual.getName()); |
| 1802 | assertEquals(IMPORTANCE_DEFAULT, actual.getImportance()); |
| 1803 | |
| 1804 | verify(mHandler, times(1)).requestSort(); |
| 1805 | } |
| 1806 | |
| 1807 | @Test |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1808 | public void testCreateChannel_addToGroup() { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1809 | NotificationChannelGroup group = new NotificationChannelGroup("group", ""); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1810 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1811 | NotificationChannel nc = new NotificationChannel("id", "hello", IMPORTANCE_DEFAULT); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1812 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, nc, true, false); |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1813 | NotificationChannel actual = |
| 1814 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "id", false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1815 | assertNull(actual.getGroup()); |
| 1816 | |
| 1817 | nc = new NotificationChannel("id", "hello", IMPORTANCE_HIGH); |
| 1818 | nc.setGroup(group.getId()); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1819 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, nc, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1820 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1821 | actual = mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "id", false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1822 | assertNotNull(actual.getGroup()); |
| 1823 | assertEquals(IMPORTANCE_DEFAULT, actual.getImportance()); |
| 1824 | |
| 1825 | verify(mHandler, times(1)).requestSort(); |
| 1826 | } |
| 1827 | |
| 1828 | @Test |
| 1829 | public void testDumpChannelsJson() throws Exception { |
| 1830 | final ApplicationInfo upgrade = new ApplicationInfo(); |
| 1831 | upgrade.targetSdkVersion = Build.VERSION_CODES.O; |
| 1832 | try { |
| 1833 | when(mPm.getApplicationInfoAsUser( |
| 1834 | anyString(), anyInt(), anyInt())).thenReturn(upgrade); |
| 1835 | } catch (PackageManager.NameNotFoundException e) { |
| 1836 | } |
| 1837 | ArrayMap<String, Integer> expectedChannels = new ArrayMap<>(); |
| 1838 | int numPackages = ThreadLocalRandom.current().nextInt(1, 5); |
| 1839 | for (int i = 0; i < numPackages; i++) { |
| 1840 | String pkgName = "pkg" + i; |
| 1841 | int numChannels = ThreadLocalRandom.current().nextInt(1, 10); |
| 1842 | for (int j = 0; j < numChannels; j++) { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1843 | mHelper.createNotificationChannel(pkgName, UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1844 | new NotificationChannel("" + j, "a", IMPORTANCE_HIGH), true, false); |
| 1845 | } |
| 1846 | expectedChannels.put(pkgName, numChannels); |
| 1847 | } |
| 1848 | |
| 1849 | // delete the first channel of the first package |
| 1850 | String pkg = expectedChannels.keyAt(0); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1851 | mHelper.deleteNotificationChannel("pkg" + 0, UID_N_MR1, "0"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1852 | // dump should not include deleted channels |
| 1853 | int count = expectedChannels.get(pkg); |
| 1854 | expectedChannels.put(pkg, count - 1); |
| 1855 | |
| 1856 | JSONArray actual = mHelper.dumpChannelsJson(new NotificationManagerService.DumpFilter()); |
| 1857 | assertEquals(numPackages, actual.length()); |
| 1858 | for (int i = 0; i < numPackages; i++) { |
| 1859 | JSONObject object = actual.getJSONObject(i); |
| 1860 | assertTrue(expectedChannels.containsKey(object.get("packageName"))); |
| 1861 | assertEquals(expectedChannels.get(object.get("packageName")).intValue(), |
| 1862 | object.getInt("channelCount")); |
| 1863 | } |
| 1864 | } |
| 1865 | |
| 1866 | @Test |
| 1867 | public void testBadgingOverrideTrue() throws Exception { |
| 1868 | Secure.putIntForUser(getContext().getContentResolver(), |
| 1869 | Secure.NOTIFICATION_BADGING, 1, |
| 1870 | USER.getIdentifier()); |
| 1871 | mHelper.updateBadgingEnabled(); // would be called by settings observer |
| 1872 | assertTrue(mHelper.badgingEnabled(USER)); |
| 1873 | } |
| 1874 | |
| 1875 | @Test |
| 1876 | public void testBadgingOverrideFalse() throws Exception { |
| 1877 | Secure.putIntForUser(getContext().getContentResolver(), |
| 1878 | Secure.NOTIFICATION_BADGING, 0, |
| 1879 | USER.getIdentifier()); |
| 1880 | mHelper.updateBadgingEnabled(); // would be called by settings observer |
| 1881 | assertFalse(mHelper.badgingEnabled(USER)); |
| 1882 | } |
| 1883 | |
| 1884 | @Test |
| 1885 | public void testBadgingForUserAll() throws Exception { |
| 1886 | try { |
| 1887 | mHelper.badgingEnabled(UserHandle.ALL); |
| 1888 | } catch (Exception e) { |
| 1889 | fail("just don't throw"); |
| 1890 | } |
| 1891 | } |
| 1892 | |
| 1893 | @Test |
| 1894 | public void testBadgingOverrideUserIsolation() throws Exception { |
| 1895 | Secure.putIntForUser(getContext().getContentResolver(), |
| 1896 | Secure.NOTIFICATION_BADGING, 0, |
| 1897 | USER.getIdentifier()); |
| 1898 | Secure.putIntForUser(getContext().getContentResolver(), |
| 1899 | Secure.NOTIFICATION_BADGING, 1, |
| 1900 | USER2.getIdentifier()); |
| 1901 | mHelper.updateBadgingEnabled(); // would be called by settings observer |
| 1902 | assertFalse(mHelper.badgingEnabled(USER)); |
| 1903 | assertTrue(mHelper.badgingEnabled(USER2)); |
| 1904 | } |
| 1905 | |
| 1906 | @Test |
Julia Reynolds | 4509ce7 | 2019-01-31 13:12:43 -0500 | [diff] [blame] | 1907 | public void testBubblesOverrideTrue() { |
| 1908 | Secure.putIntForUser(getContext().getContentResolver(), |
| 1909 | Secure.NOTIFICATION_BUBBLES, 1, |
| 1910 | USER.getIdentifier()); |
| 1911 | mHelper.updateBubblesEnabled(); // would be called by settings observer |
| 1912 | assertTrue(mHelper.bubblesEnabled(USER)); |
| 1913 | } |
| 1914 | |
| 1915 | @Test |
| 1916 | public void testBubblesOverrideFalse() { |
| 1917 | Secure.putIntForUser(getContext().getContentResolver(), |
| 1918 | Secure.NOTIFICATION_BUBBLES, 0, |
| 1919 | USER.getIdentifier()); |
| 1920 | mHelper.updateBubblesEnabled(); // would be called by settings observer |
| 1921 | assertFalse(mHelper.bubblesEnabled(USER)); |
| 1922 | } |
| 1923 | |
| 1924 | @Test |
| 1925 | public void testBubblesForUserAll() { |
| 1926 | try { |
| 1927 | mHelper.bubblesEnabled(UserHandle.ALL); |
| 1928 | } catch (Exception e) { |
| 1929 | fail("just don't throw"); |
| 1930 | } |
| 1931 | } |
| 1932 | |
| 1933 | @Test |
| 1934 | public void testBubblesOverrideUserIsolation() { |
| 1935 | Secure.putIntForUser(getContext().getContentResolver(), |
| 1936 | Secure.NOTIFICATION_BUBBLES, 0, |
| 1937 | USER.getIdentifier()); |
| 1938 | Secure.putIntForUser(getContext().getContentResolver(), |
| 1939 | Secure.NOTIFICATION_BUBBLES, 1, |
| 1940 | USER2.getIdentifier()); |
| 1941 | mHelper.updateBubblesEnabled(); // would be called by settings observer |
| 1942 | assertFalse(mHelper.bubblesEnabled(USER)); |
| 1943 | assertTrue(mHelper.bubblesEnabled(USER2)); |
| 1944 | } |
| 1945 | |
| 1946 | @Test |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1947 | public void testOnLocaleChanged_updatesDefaultChannels() throws Exception { |
| 1948 | String newLabel = "bananas!"; |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1949 | final NotificationChannel defaultChannel = mHelper.getNotificationChannel(PKG_N_MR1, |
| 1950 | UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1951 | NotificationChannel.DEFAULT_CHANNEL_ID, false); |
| 1952 | assertFalse(newLabel.equals(defaultChannel.getName())); |
| 1953 | |
| 1954 | Resources res = mock(Resources.class); |
| 1955 | when(mContext.getResources()).thenReturn(res); |
| 1956 | when(res.getString(com.android.internal.R.string.default_notification_channel_label)) |
| 1957 | .thenReturn(newLabel); |
| 1958 | |
| 1959 | mHelper.onLocaleChanged(mContext, USER.getIdentifier()); |
| 1960 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1961 | assertEquals(newLabel, mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1962 | NotificationChannel.DEFAULT_CHANNEL_ID, false).getName()); |
| 1963 | } |
| 1964 | |
| 1965 | @Test |
| 1966 | public void testIsGroupBlocked_noGroup() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1967 | assertFalse(mHelper.isGroupBlocked(PKG_N_MR1, UID_N_MR1, null)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1968 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1969 | assertFalse(mHelper.isGroupBlocked(PKG_N_MR1, UID_N_MR1, "non existent group")); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1970 | } |
| 1971 | |
| 1972 | @Test |
| 1973 | public void testIsGroupBlocked_notBlocked() throws Exception { |
| 1974 | NotificationChannelGroup group = new NotificationChannelGroup("id", "name"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1975 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1976 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1977 | assertFalse(mHelper.isGroupBlocked(PKG_N_MR1, UID_N_MR1, group.getId())); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1978 | } |
| 1979 | |
| 1980 | @Test |
| 1981 | public void testIsGroupBlocked_blocked() throws Exception { |
| 1982 | NotificationChannelGroup group = new NotificationChannelGroup("id", "name"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1983 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1984 | group.setBlocked(true); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1985 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1986 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1987 | assertTrue(mHelper.isGroupBlocked(PKG_N_MR1, UID_N_MR1, group.getId())); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1988 | } |
| 1989 | |
| 1990 | @Test |
| 1991 | public void testIsGroup_appCannotResetBlock() throws Exception { |
| 1992 | NotificationChannelGroup group = new NotificationChannelGroup("id", "name"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1993 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1994 | NotificationChannelGroup group2 = group.clone(); |
| 1995 | group2.setBlocked(true); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1996 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group2, false); |
| 1997 | assertTrue(mHelper.isGroupBlocked(PKG_N_MR1, UID_N_MR1, group.getId())); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1998 | |
| 1999 | NotificationChannelGroup group3 = group.clone(); |
| 2000 | group3.setBlocked(false); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2001 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group3, true); |
| 2002 | assertTrue(mHelper.isGroupBlocked(PKG_N_MR1, UID_N_MR1, group.getId())); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2003 | } |
| 2004 | |
| 2005 | @Test |
| 2006 | public void testGetNotificationChannelGroupWithChannels() throws Exception { |
| 2007 | NotificationChannelGroup group = new NotificationChannelGroup("group", ""); |
| 2008 | NotificationChannelGroup other = new NotificationChannelGroup("something else", ""); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2009 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group, true); |
| 2010 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, other, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2011 | |
| 2012 | NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_DEFAULT); |
| 2013 | a.setGroup(group.getId()); |
| 2014 | NotificationChannel b = new NotificationChannel("b", "b", IMPORTANCE_DEFAULT); |
| 2015 | b.setGroup(other.getId()); |
| 2016 | NotificationChannel c = new NotificationChannel("c", "c", IMPORTANCE_DEFAULT); |
| 2017 | c.setGroup(group.getId()); |
| 2018 | NotificationChannel d = new NotificationChannel("d", "d", IMPORTANCE_DEFAULT); |
| 2019 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2020 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, a, true, false); |
| 2021 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, b, true, false); |
| 2022 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, c, true, false); |
| 2023 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, d, true, false); |
| 2024 | mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, c.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2025 | |
| 2026 | NotificationChannelGroup retrieved = mHelper.getNotificationChannelGroupWithChannels( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2027 | PKG_N_MR1, UID_N_MR1, group.getId(), true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2028 | assertEquals(2, retrieved.getChannels().size()); |
| 2029 | compareChannels(a, findChannel(retrieved.getChannels(), a.getId())); |
| 2030 | compareChannels(c, findChannel(retrieved.getChannels(), c.getId())); |
| 2031 | |
| 2032 | retrieved = mHelper.getNotificationChannelGroupWithChannels( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2033 | PKG_N_MR1, UID_N_MR1, group.getId(), false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2034 | assertEquals(1, retrieved.getChannels().size()); |
| 2035 | compareChannels(a, findChannel(retrieved.getChannels(), a.getId())); |
| 2036 | } |
| 2037 | |
| 2038 | @Test |
| 2039 | public void testAndroidPkgCannotBypassDnd_creation() { |
| 2040 | NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW); |
| 2041 | test.setBypassDnd(true); |
| 2042 | |
| 2043 | mHelper.createNotificationChannel(SYSTEM_PKG, SYSTEM_UID, test, true, false); |
| 2044 | |
| 2045 | assertFalse(mHelper.getNotificationChannel(SYSTEM_PKG, SYSTEM_UID, "A", false) |
| 2046 | .canBypassDnd()); |
| 2047 | } |
| 2048 | |
| 2049 | @Test |
| 2050 | public void testDndPkgCanBypassDnd_creation() { |
| 2051 | NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW); |
| 2052 | test.setBypassDnd(true); |
| 2053 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2054 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, test, true, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2055 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2056 | assertTrue(mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "A", false).canBypassDnd()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2057 | } |
| 2058 | |
| 2059 | @Test |
| 2060 | public void testNormalPkgCannotBypassDnd_creation() { |
| 2061 | NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW); |
| 2062 | test.setBypassDnd(true); |
| 2063 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2064 | mHelper.createNotificationChannel(PKG_N_MR1, 1000, test, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2065 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2066 | assertFalse(mHelper.getNotificationChannel(PKG_N_MR1, 1000, "A", false).canBypassDnd()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2067 | } |
| 2068 | |
| 2069 | @Test |
| 2070 | public void testAndroidPkgCannotBypassDnd_update() throws Exception { |
| 2071 | NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW); |
| 2072 | mHelper.createNotificationChannel(SYSTEM_PKG, SYSTEM_UID, test, true, false); |
| 2073 | |
| 2074 | NotificationChannel update = new NotificationChannel("A", "a", IMPORTANCE_LOW); |
| 2075 | update.setBypassDnd(true); |
| 2076 | mHelper.createNotificationChannel(SYSTEM_PKG, SYSTEM_UID, update, true, false); |
| 2077 | |
| 2078 | assertFalse(mHelper.getNotificationChannel(SYSTEM_PKG, SYSTEM_UID, "A", false) |
| 2079 | .canBypassDnd()); |
| 2080 | } |
| 2081 | |
| 2082 | @Test |
| 2083 | public void testDndPkgCanBypassDnd_update() throws Exception { |
| 2084 | NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2085 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, test, true, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2086 | |
| 2087 | NotificationChannel update = new NotificationChannel("A", "a", IMPORTANCE_LOW); |
| 2088 | update.setBypassDnd(true); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2089 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, update, true, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2090 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2091 | assertTrue(mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "A", false).canBypassDnd()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | @Test |
| 2095 | public void testNormalPkgCannotBypassDnd_update() { |
| 2096 | NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2097 | mHelper.createNotificationChannel(PKG_N_MR1, 1000, test, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2098 | NotificationChannel update = new NotificationChannel("A", "a", IMPORTANCE_LOW); |
| 2099 | update.setBypassDnd(true); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2100 | mHelper.createNotificationChannel(PKG_N_MR1, 1000, update, true, false); |
| 2101 | assertFalse(mHelper.getNotificationChannel(PKG_N_MR1, 1000, "A", false).canBypassDnd()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2102 | } |
| 2103 | |
| 2104 | @Test |
| 2105 | public void testGetBlockedAppCount_noApps() { |
| 2106 | assertEquals(0, mHelper.getBlockedAppCount(0)); |
| 2107 | } |
| 2108 | |
| 2109 | @Test |
| 2110 | public void testGetBlockedAppCount_noAppsForUserId() { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2111 | mHelper.setEnabled(PKG_N_MR1, 100, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2112 | assertEquals(0, mHelper.getBlockedAppCount(9)); |
| 2113 | } |
| 2114 | |
| 2115 | @Test |
| 2116 | public void testGetBlockedAppCount_appsForUserId() { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 2117 | mHelper.setEnabled(PKG_N_MR1, 1020, false); |
| 2118 | mHelper.setEnabled(PKG_N_MR1, 1030, false); |
| 2119 | mHelper.setEnabled(PKG_N_MR1, 1060, false); |
| 2120 | mHelper.setEnabled(PKG_N_MR1, 1000, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2121 | assertEquals(3, mHelper.getBlockedAppCount(0)); |
| 2122 | } |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 2123 | |
| 2124 | @Test |
Julia Reynolds | 12ad7ca | 2019-01-28 09:29:16 -0500 | [diff] [blame] | 2125 | public void testXml_statusBarIcons_default() throws Exception { |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2126 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_O, UID_O, false, UserHandle.USER_ALL); |
Julia Reynolds | 12ad7ca | 2019-01-28 09:29:16 -0500 | [diff] [blame] | 2127 | mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2128 | loadStreamXml(baos, false, UserHandle.USER_ALL); |
Julia Reynolds | 12ad7ca | 2019-01-28 09:29:16 -0500 | [diff] [blame] | 2129 | |
| 2130 | assertEquals(PreferencesHelper.DEFAULT_HIDE_SILENT_STATUS_BAR_ICONS, |
| 2131 | mHelper.shouldHideSilentStatusIcons()); |
| 2132 | } |
| 2133 | |
| 2134 | @Test |
| 2135 | public void testXml_statusBarIcons() throws Exception { |
| 2136 | mHelper.setHideSilentStatusIcons(!PreferencesHelper.DEFAULT_HIDE_SILENT_STATUS_BAR_ICONS); |
| 2137 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2138 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_O, UID_O, false, UserHandle.USER_ALL); |
Julia Reynolds | 12ad7ca | 2019-01-28 09:29:16 -0500 | [diff] [blame] | 2139 | mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2140 | loadStreamXml(baos, false, UserHandle.USER_ALL); |
Julia Reynolds | 12ad7ca | 2019-01-28 09:29:16 -0500 | [diff] [blame] | 2141 | |
| 2142 | assertEquals(!PreferencesHelper.DEFAULT_HIDE_SILENT_STATUS_BAR_ICONS, |
| 2143 | mHelper.shouldHideSilentStatusIcons()); |
| 2144 | } |
| 2145 | |
| 2146 | @Test |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 2147 | public void testSetNotificationDelegate() { |
| 2148 | mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53); |
| 2149 | assertEquals("other", mHelper.getNotificationDelegate(PKG_O, UID_O)); |
| 2150 | } |
| 2151 | |
| 2152 | @Test |
| 2153 | public void testRevokeNotificationDelegate() { |
| 2154 | mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53); |
| 2155 | mHelper.revokeNotificationDelegate(PKG_O, UID_O); |
| 2156 | |
| 2157 | assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O)); |
| 2158 | } |
| 2159 | |
| 2160 | @Test |
| 2161 | public void testRevokeNotificationDelegate_noDelegateExistsNoCrash() { |
| 2162 | mHelper.revokeNotificationDelegate(PKG_O, UID_O); |
| 2163 | |
| 2164 | assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O)); |
| 2165 | } |
| 2166 | |
| 2167 | @Test |
| 2168 | public void testToggleNotificationDelegate() { |
| 2169 | mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53); |
| 2170 | mHelper.toggleNotificationDelegate(PKG_O, UID_O, false); |
| 2171 | |
| 2172 | assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O)); |
| 2173 | |
| 2174 | mHelper.toggleNotificationDelegate(PKG_O, UID_O, true); |
| 2175 | assertEquals("other", mHelper.getNotificationDelegate(PKG_O, UID_O)); |
| 2176 | } |
| 2177 | |
| 2178 | @Test |
| 2179 | public void testToggleNotificationDelegate_noDelegateExistsNoCrash() { |
| 2180 | mHelper.toggleNotificationDelegate(PKG_O, UID_O, false); |
| 2181 | assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O)); |
| 2182 | |
| 2183 | mHelper.toggleNotificationDelegate(PKG_O, UID_O, true); |
| 2184 | assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O)); |
| 2185 | } |
| 2186 | |
| 2187 | @Test |
| 2188 | public void testIsDelegateAllowed_noSource() { |
| 2189 | assertFalse(mHelper.isDelegateAllowed("does not exist", -1, "whatever", 0)); |
| 2190 | } |
| 2191 | |
| 2192 | @Test |
| 2193 | public void testIsDelegateAllowed_noDelegate() { |
| 2194 | mHelper.setImportance(PKG_O, UID_O, IMPORTANCE_UNSPECIFIED); |
| 2195 | |
| 2196 | assertFalse(mHelper.isDelegateAllowed(PKG_O, UID_O, "whatever", 0)); |
| 2197 | } |
| 2198 | |
| 2199 | @Test |
| 2200 | public void testIsDelegateAllowed_delegateDisabledByApp() { |
| 2201 | mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53); |
| 2202 | mHelper.revokeNotificationDelegate(PKG_O, UID_O); |
| 2203 | |
| 2204 | assertFalse(mHelper.isDelegateAllowed(PKG_O, UID_O, "other", 53)); |
| 2205 | } |
| 2206 | |
| 2207 | @Test |
| 2208 | public void testIsDelegateAllowed_wrongDelegate() { |
| 2209 | mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53); |
| 2210 | mHelper.revokeNotificationDelegate(PKG_O, UID_O); |
| 2211 | |
| 2212 | assertFalse(mHelper.isDelegateAllowed(PKG_O, UID_O, "banana", 27)); |
| 2213 | } |
| 2214 | |
| 2215 | @Test |
| 2216 | public void testIsDelegateAllowed_delegateDisabledByUser() { |
| 2217 | mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53); |
| 2218 | mHelper.toggleNotificationDelegate(PKG_O, UID_O, false); |
| 2219 | |
| 2220 | assertFalse(mHelper.isDelegateAllowed(PKG_O, UID_O, "other", 53)); |
| 2221 | } |
| 2222 | |
| 2223 | @Test |
| 2224 | public void testIsDelegateAllowed() { |
| 2225 | mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53); |
| 2226 | |
| 2227 | assertTrue(mHelper.isDelegateAllowed(PKG_O, UID_O, "other", 53)); |
| 2228 | } |
| 2229 | |
| 2230 | @Test |
| 2231 | public void testDelegateXml_noDelegate() throws Exception { |
| 2232 | mHelper.setImportance(PKG_O, UID_O, IMPORTANCE_UNSPECIFIED); |
| 2233 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2234 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_O, UID_O, false, UserHandle.USER_ALL); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 2235 | mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2236 | loadStreamXml(baos, false, UserHandle.USER_ALL); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 2237 | |
| 2238 | assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O)); |
| 2239 | } |
| 2240 | |
| 2241 | @Test |
| 2242 | public void testDelegateXml_delegate() throws Exception { |
| 2243 | mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53); |
| 2244 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2245 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_O, UID_O, false, UserHandle.USER_ALL); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 2246 | mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2247 | loadStreamXml(baos, false, UserHandle.USER_ALL); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 2248 | |
| 2249 | assertEquals("other", mHelper.getNotificationDelegate(PKG_O, UID_O)); |
| 2250 | } |
| 2251 | |
| 2252 | @Test |
| 2253 | public void testDelegateXml_disabledDelegate() throws Exception { |
| 2254 | mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53); |
| 2255 | mHelper.revokeNotificationDelegate(PKG_O, UID_O); |
| 2256 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2257 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_O, UID_O, false, UserHandle.USER_ALL); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 2258 | mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2259 | loadStreamXml(baos, false, UserHandle.USER_ALL); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 2260 | |
| 2261 | assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O)); |
| 2262 | } |
| 2263 | |
| 2264 | @Test |
| 2265 | public void testDelegateXml_userDisabledDelegate() throws Exception { |
| 2266 | mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53); |
| 2267 | mHelper.toggleNotificationDelegate(PKG_O, UID_O, false); |
| 2268 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2269 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_O, UID_O, false, UserHandle.USER_ALL); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 2270 | mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2271 | loadStreamXml(baos, false, UserHandle.USER_ALL); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 2272 | |
| 2273 | // appears disabled |
| 2274 | assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O)); |
| 2275 | |
| 2276 | // but was loaded and can be toggled back on |
| 2277 | mHelper.toggleNotificationDelegate(PKG_O, UID_O, true); |
| 2278 | assertEquals("other", mHelper.getNotificationDelegate(PKG_O, UID_O)); |
| 2279 | } |
| 2280 | |
| 2281 | @Test |
| 2282 | public void testDelegateXml_entirelyDisabledDelegate() throws Exception { |
| 2283 | mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53); |
| 2284 | mHelper.toggleNotificationDelegate(PKG_O, UID_O, false); |
| 2285 | mHelper.revokeNotificationDelegate(PKG_O, UID_O); |
| 2286 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2287 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_O, UID_O, false, UserHandle.USER_ALL); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 2288 | mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2289 | loadStreamXml(baos, false, UserHandle.USER_ALL); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 2290 | |
| 2291 | // appears disabled |
| 2292 | assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O)); |
| 2293 | |
| 2294 | mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53); |
| 2295 | assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O)); |
| 2296 | |
| 2297 | mHelper.toggleNotificationDelegate(PKG_O, UID_O, true); |
| 2298 | assertEquals("other", mHelper.getNotificationDelegate(PKG_O, UID_O)); |
| 2299 | } |
Julia Reynolds | 33ab8a0 | 2018-12-17 16:19:52 -0500 | [diff] [blame] | 2300 | |
| 2301 | @Test |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 2302 | public void testAllowBubbles_defaults() throws Exception { |
Mady Mellor | 9db685a | 2019-01-23 13:23:37 -0800 | [diff] [blame] | 2303 | assertTrue(mHelper.areBubblesAllowed(PKG_O, UID_O)); |
Julia Reynolds | 33ab8a0 | 2018-12-17 16:19:52 -0500 | [diff] [blame] | 2304 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2305 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_O, UID_O, false, UserHandle.USER_ALL); |
Julia Reynolds | 33ab8a0 | 2018-12-17 16:19:52 -0500 | [diff] [blame] | 2306 | mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2307 | loadStreamXml(baos, false, UserHandle.USER_ALL); |
Julia Reynolds | 33ab8a0 | 2018-12-17 16:19:52 -0500 | [diff] [blame] | 2308 | |
Mady Mellor | 9db685a | 2019-01-23 13:23:37 -0800 | [diff] [blame] | 2309 | assertTrue(mHelper.areBubblesAllowed(PKG_O, UID_O)); |
Julia Reynolds | 33ab8a0 | 2018-12-17 16:19:52 -0500 | [diff] [blame] | 2310 | assertEquals(0, mHelper.getAppLockedFields(PKG_O, UID_O)); |
| 2311 | } |
| 2312 | |
| 2313 | @Test |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 2314 | public void testAllowBubbles_xml() throws Exception { |
| 2315 | mHelper.setBubblesAllowed(PKG_O, UID_O, false); |
Mady Mellor | 9db685a | 2019-01-23 13:23:37 -0800 | [diff] [blame] | 2316 | assertFalse(mHelper.areBubblesAllowed(PKG_O, UID_O)); |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 2317 | assertEquals(PreferencesHelper.LockableAppFields.USER_LOCKED_BUBBLE, |
Julia Reynolds | 33ab8a0 | 2018-12-17 16:19:52 -0500 | [diff] [blame] | 2318 | mHelper.getAppLockedFields(PKG_O, UID_O)); |
| 2319 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2320 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_O, UID_O, false, UserHandle.USER_ALL); |
Julia Reynolds | 33ab8a0 | 2018-12-17 16:19:52 -0500 | [diff] [blame] | 2321 | mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 2322 | loadStreamXml(baos, false, UserHandle.USER_ALL); |
Julia Reynolds | 33ab8a0 | 2018-12-17 16:19:52 -0500 | [diff] [blame] | 2323 | |
Mady Mellor | 9db685a | 2019-01-23 13:23:37 -0800 | [diff] [blame] | 2324 | assertFalse(mHelper.areBubblesAllowed(PKG_O, UID_O)); |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 2325 | assertEquals(PreferencesHelper.LockableAppFields.USER_LOCKED_BUBBLE, |
Julia Reynolds | 33ab8a0 | 2018-12-17 16:19:52 -0500 | [diff] [blame] | 2326 | mHelper.getAppLockedFields(PKG_O, UID_O)); |
| 2327 | } |
Julia Reynolds | 413ba84 | 2019-01-11 10:38:08 -0500 | [diff] [blame] | 2328 | |
| 2329 | @Test |
| 2330 | public void testLockChannelsForOEM_emptyList() { |
| 2331 | mHelper.lockChannelsForOEM(null); |
| 2332 | mHelper.lockChannelsForOEM(new String[0]); |
| 2333 | // no exception |
| 2334 | } |
| 2335 | |
| 2336 | @Test |
| 2337 | public void testLockChannelsForOEM_appWide() { |
| 2338 | NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_HIGH); |
| 2339 | NotificationChannel b = new NotificationChannel("b", "b", IMPORTANCE_LOW); |
| 2340 | NotificationChannel c = new NotificationChannel("c", "c", IMPORTANCE_DEFAULT); |
| 2341 | // different uids, same package |
| 2342 | mHelper.createNotificationChannel(PKG_O, 3, a, true, false); |
| 2343 | mHelper.createNotificationChannel(PKG_O, 3, b, false, false); |
| 2344 | mHelper.createNotificationChannel(PKG_O, 30, c, true, true); |
| 2345 | |
| 2346 | mHelper.lockChannelsForOEM(new String[] {PKG_O}); |
| 2347 | |
| 2348 | assertTrue(mHelper.getNotificationChannel(PKG_O, 3, a.getId(), false) |
| 2349 | .isImportanceLockedByOEM()); |
| 2350 | assertTrue(mHelper.getNotificationChannel(PKG_O, 3, b.getId(), false) |
| 2351 | .isImportanceLockedByOEM()); |
| 2352 | assertTrue(mHelper.getNotificationChannel(PKG_O, 30, c.getId(), false) |
| 2353 | .isImportanceLockedByOEM()); |
| 2354 | } |
| 2355 | |
| 2356 | @Test |
| 2357 | public void testLockChannelsForOEM_onlyGivenPkg() { |
| 2358 | NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_HIGH); |
| 2359 | NotificationChannel b = new NotificationChannel("b", "b", IMPORTANCE_LOW); |
| 2360 | mHelper.createNotificationChannel(PKG_O, 3, a, true, false); |
| 2361 | mHelper.createNotificationChannel(PKG_N_MR1, 30, b, false, false); |
| 2362 | |
| 2363 | mHelper.lockChannelsForOEM(new String[] {PKG_O}); |
| 2364 | |
| 2365 | assertTrue(mHelper.getNotificationChannel(PKG_O, 3, a.getId(), false) |
| 2366 | .isImportanceLockedByOEM()); |
| 2367 | assertFalse(mHelper.getNotificationChannel(PKG_N_MR1, 30, b.getId(), false) |
| 2368 | .isImportanceLockedByOEM()); |
| 2369 | } |
| 2370 | |
| 2371 | @Test |
| 2372 | public void testLockChannelsForOEM_channelSpecific() { |
| 2373 | NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_HIGH); |
| 2374 | NotificationChannel b = new NotificationChannel("b", "b", IMPORTANCE_LOW); |
| 2375 | NotificationChannel c = new NotificationChannel("c", "c", IMPORTANCE_DEFAULT); |
| 2376 | // different uids, same package |
| 2377 | mHelper.createNotificationChannel(PKG_O, 3, a, true, false); |
| 2378 | mHelper.createNotificationChannel(PKG_O, 3, b, false, false); |
| 2379 | mHelper.createNotificationChannel(PKG_O, 30, c, true, true); |
| 2380 | |
| 2381 | mHelper.lockChannelsForOEM(new String[] {PKG_O + ":b", PKG_O + ":c"}); |
| 2382 | |
| 2383 | assertFalse(mHelper.getNotificationChannel(PKG_O, 3, a.getId(), false) |
| 2384 | .isImportanceLockedByOEM()); |
| 2385 | assertTrue(mHelper.getNotificationChannel(PKG_O, 3, b.getId(), false) |
| 2386 | .isImportanceLockedByOEM()); |
| 2387 | assertTrue(mHelper.getNotificationChannel(PKG_O, 30, c.getId(), false) |
| 2388 | .isImportanceLockedByOEM()); |
| 2389 | } |
| 2390 | |
| 2391 | @Test |
| 2392 | public void testLockChannelsForOEM_channelDoesNotExistYet_appWide() { |
| 2393 | NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_HIGH); |
| 2394 | NotificationChannel b = new NotificationChannel("b", "b", IMPORTANCE_LOW); |
| 2395 | mHelper.createNotificationChannel(PKG_O, 3, a, true, false); |
| 2396 | |
| 2397 | mHelper.lockChannelsForOEM(new String[] {PKG_O}); |
| 2398 | |
| 2399 | assertTrue(mHelper.getNotificationChannel(PKG_O, 3, a.getId(), false) |
| 2400 | .isImportanceLockedByOEM()); |
| 2401 | |
| 2402 | mHelper.createNotificationChannel(PKG_O, 3, b, true, false); |
| 2403 | assertTrue(mHelper.getNotificationChannel(PKG_O, 3, b.getId(), false) |
| 2404 | .isImportanceLockedByOEM()); |
| 2405 | } |
| 2406 | |
| 2407 | @Test |
| 2408 | public void testLockChannelsForOEM_channelDoesNotExistYet_channelSpecific() { |
| 2409 | NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_HIGH); |
| 2410 | NotificationChannel b = new NotificationChannel("b", "b", IMPORTANCE_LOW); |
| 2411 | mHelper.createNotificationChannel(PKG_O, UID_O, a, true, false); |
| 2412 | |
| 2413 | mHelper.lockChannelsForOEM(new String[] {PKG_O + ":a", PKG_O + ":b"}); |
| 2414 | |
| 2415 | assertTrue(mHelper.getNotificationChannel(PKG_O, UID_O, a.getId(), false) |
| 2416 | .isImportanceLockedByOEM()); |
| 2417 | |
| 2418 | mHelper.createNotificationChannel(PKG_O, UID_O, b, true, false); |
| 2419 | assertTrue(mHelper.getNotificationChannel(PKG_O, UID_O, b.getId(), false) |
| 2420 | .isImportanceLockedByOEM()); |
| 2421 | } |
| 2422 | |
| 2423 | @Test |
| 2424 | public void testUpdateNotificationChannel_oemLockedImportance() { |
| 2425 | NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_HIGH); |
| 2426 | mHelper.createNotificationChannel(PKG_O, UID_O, a, true, false); |
| 2427 | |
| 2428 | mHelper.lockChannelsForOEM(new String[] {PKG_O}); |
| 2429 | |
| 2430 | NotificationChannel update = new NotificationChannel("a", "a", IMPORTANCE_NONE); |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 2431 | update.setAllowBubbles(false); |
Julia Reynolds | 413ba84 | 2019-01-11 10:38:08 -0500 | [diff] [blame] | 2432 | |
| 2433 | mHelper.updateNotificationChannel(PKG_O, UID_O, update, true); |
| 2434 | |
| 2435 | assertEquals(IMPORTANCE_HIGH, |
| 2436 | mHelper.getNotificationChannel(PKG_O, UID_O, a.getId(), false).getImportance()); |
| 2437 | assertEquals(false, |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 2438 | mHelper.getNotificationChannel(PKG_O, UID_O, a.getId(), false).canBubble()); |
Julia Reynolds | 413ba84 | 2019-01-11 10:38:08 -0500 | [diff] [blame] | 2439 | |
| 2440 | mHelper.updateNotificationChannel(PKG_O, UID_O, update, true); |
| 2441 | |
| 2442 | assertEquals(IMPORTANCE_HIGH, |
| 2443 | mHelper.getNotificationChannel(PKG_O, UID_O, a.getId(), false).getImportance()); |
| 2444 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2445 | } |