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; |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 30 | import static org.junit.Assert.assertNotEquals; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 31 | import static org.junit.Assert.assertNotNull; |
| 32 | import static org.junit.Assert.assertTrue; |
| 33 | import static org.mockito.ArgumentMatchers.any; |
| 34 | import static org.mockito.Matchers.anyInt; |
| 35 | import static org.mockito.Matchers.anyString; |
| 36 | import static org.mockito.Matchers.eq; |
| 37 | import static org.mockito.Mockito.mock; |
| 38 | import static org.mockito.Mockito.never; |
| 39 | import static org.mockito.Mockito.reset; |
| 40 | import static org.mockito.Mockito.times; |
| 41 | import static org.mockito.Mockito.verify; |
| 42 | import static org.mockito.Mockito.when; |
| 43 | |
| 44 | import android.app.Notification; |
| 45 | import android.app.NotificationChannel; |
| 46 | import android.app.NotificationChannelGroup; |
| 47 | import android.app.NotificationManager; |
| 48 | import android.content.ContentProvider; |
| 49 | import android.content.Context; |
| 50 | import android.content.IContentProvider; |
| 51 | import android.content.pm.ApplicationInfo; |
| 52 | import android.content.pm.PackageInfo; |
| 53 | import android.content.pm.PackageManager; |
| 54 | import android.content.pm.Signature; |
| 55 | import android.content.res.Resources; |
| 56 | import android.graphics.Color; |
| 57 | import android.media.AudioAttributes; |
| 58 | import android.net.Uri; |
| 59 | import android.os.Build; |
| 60 | import android.os.UserHandle; |
| 61 | import android.provider.Settings; |
| 62 | import android.provider.Settings.Secure; |
| 63 | import android.support.test.InstrumentationRegistry; |
| 64 | import android.support.test.runner.AndroidJUnit4; |
| 65 | import android.test.suitebuilder.annotation.SmallTest; |
| 66 | import android.testing.TestableContentResolver; |
| 67 | import android.util.ArrayMap; |
| 68 | import android.util.Xml; |
| 69 | |
| 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"; |
| 101 | private static final int SYSTEM_UID= 1000; |
| 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); |
| 126 | UserHandle user = UserHandle.ALL; |
| 127 | |
| 128 | final ApplicationInfo legacy = new ApplicationInfo(); |
| 129 | legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1; |
| 130 | final ApplicationInfo upgrade = new ApplicationInfo(); |
| 131 | upgrade.targetSdkVersion = Build.VERSION_CODES.O; |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 132 | when(mPm.getApplicationInfoAsUser(eq(PKG_N_MR1), anyInt(), anyInt())).thenReturn(legacy); |
| 133 | when(mPm.getApplicationInfoAsUser(eq(PKG_O), anyInt(), anyInt())).thenReturn(upgrade); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 134 | when(mPm.getApplicationInfoAsUser(eq(SYSTEM_PKG), anyInt(), anyInt())).thenReturn(upgrade); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 135 | when(mPm.getPackageUidAsUser(eq(PKG_N_MR1), anyInt())).thenReturn(UID_N_MR1); |
| 136 | when(mPm.getPackageUidAsUser(eq(PKG_O), anyInt())).thenReturn(UID_O); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 137 | when(mPm.getPackageUidAsUser(eq(SYSTEM_PKG), anyInt())).thenReturn(SYSTEM_UID); |
| 138 | PackageInfo info = mock(PackageInfo.class); |
| 139 | info.signatures = new Signature[] {mock(Signature.class)}; |
| 140 | when(mPm.getPackageInfoAsUser(eq(SYSTEM_PKG), anyInt(), anyInt())).thenReturn(info); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 141 | when(mPm.getPackageInfoAsUser(eq(PKG_N_MR1), anyInt(), anyInt())) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 142 | .thenReturn(mock(PackageInfo.class)); |
| 143 | when(mContext.getResources()).thenReturn( |
| 144 | InstrumentationRegistry.getContext().getResources()); |
| 145 | when(mContext.getContentResolver()).thenReturn( |
| 146 | InstrumentationRegistry.getContext().getContentResolver()); |
| 147 | when(mContext.getPackageManager()).thenReturn(mPm); |
| 148 | when(mContext.getApplicationInfo()).thenReturn(legacy); |
| 149 | // most tests assume badging is enabled |
| 150 | TestableContentResolver contentResolver = getContext().getContentResolver(); |
| 151 | contentResolver.setFallbackToExisting(false); |
| 152 | Secure.putIntForUser(contentResolver, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 153 | Secure.NOTIFICATION_BADGING, 1, UserHandle.getUserId(UID_N_MR1)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 154 | |
| 155 | ContentProvider testContentProvider = mock(ContentProvider.class); |
| 156 | when(testContentProvider.getIContentProvider()).thenReturn(mTestIContentProvider); |
| 157 | contentResolver.addProvider(TEST_AUTHORITY, testContentProvider); |
| 158 | |
| 159 | when(mTestIContentProvider.canonicalize(any(), eq(SOUND_URI))) |
| 160 | .thenReturn(CANONICAL_SOUND_URI); |
| 161 | when(mTestIContentProvider.canonicalize(any(), eq(CANONICAL_SOUND_URI))) |
| 162 | .thenReturn(CANONICAL_SOUND_URI); |
| 163 | when(mTestIContentProvider.uncanonicalize(any(), eq(CANONICAL_SOUND_URI))) |
| 164 | .thenReturn(SOUND_URI); |
| 165 | |
| 166 | mTestNotificationPolicy = new NotificationManager.Policy(0, 0, 0, 0, |
| 167 | NotificationManager.Policy.STATE_CHANNELS_BYPASSING_DND); |
| 168 | when(mMockZenModeHelper.getNotificationPolicy()).thenReturn(mTestNotificationPolicy); |
| 169 | mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper); |
| 170 | resetZenModeHelper(); |
| 171 | |
| 172 | mAudioAttributes = new AudioAttributes.Builder() |
| 173 | .setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN) |
| 174 | .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE) |
| 175 | .setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED) |
| 176 | .build(); |
| 177 | } |
| 178 | |
| 179 | private NotificationChannel getDefaultChannel() { |
| 180 | return new NotificationChannel(NotificationChannel.DEFAULT_CHANNEL_ID, "name", |
| 181 | IMPORTANCE_LOW); |
| 182 | } |
| 183 | |
| 184 | private ByteArrayOutputStream writeXmlAndPurge(String pkg, int uid, boolean forBackup, |
| 185 | String... channelIds) |
| 186 | throws Exception { |
| 187 | XmlSerializer serializer = new FastXmlSerializer(); |
| 188 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 189 | serializer.setOutput(new BufferedOutputStream(baos), "utf-8"); |
| 190 | serializer.startDocument(null, true); |
| 191 | mHelper.writeXml(serializer, forBackup); |
| 192 | serializer.endDocument(); |
| 193 | serializer.flush(); |
| 194 | for (String channelId : channelIds) { |
| 195 | mHelper.permanentlyDeleteNotificationChannel(pkg, uid, channelId); |
| 196 | } |
| 197 | return baos; |
| 198 | } |
| 199 | |
| 200 | private void loadStreamXml(ByteArrayOutputStream stream, boolean forRestore) throws Exception { |
| 201 | loadByteArrayXml(stream.toByteArray(), forRestore); |
| 202 | } |
| 203 | |
| 204 | private void loadByteArrayXml(byte[] byteArray, boolean forRestore) throws Exception { |
| 205 | XmlPullParser parser = Xml.newPullParser(); |
| 206 | parser.setInput(new BufferedInputStream(new ByteArrayInputStream(byteArray)), null); |
| 207 | parser.nextTag(); |
| 208 | mHelper.readXml(parser, forRestore); |
| 209 | } |
| 210 | |
| 211 | private void compareChannels(NotificationChannel expected, NotificationChannel actual) { |
| 212 | assertEquals(expected.getId(), actual.getId()); |
| 213 | assertEquals(expected.getName(), actual.getName()); |
| 214 | assertEquals(expected.getDescription(), actual.getDescription()); |
| 215 | assertEquals(expected.shouldVibrate(), actual.shouldVibrate()); |
| 216 | assertEquals(expected.shouldShowLights(), actual.shouldShowLights()); |
| 217 | assertEquals(expected.getImportance(), actual.getImportance()); |
| 218 | assertEquals(expected.getLockscreenVisibility(), actual.getLockscreenVisibility()); |
| 219 | assertEquals(expected.getSound(), actual.getSound()); |
| 220 | assertEquals(expected.canBypassDnd(), actual.canBypassDnd()); |
| 221 | assertTrue(Arrays.equals(expected.getVibrationPattern(), actual.getVibrationPattern())); |
| 222 | assertEquals(expected.getGroup(), actual.getGroup()); |
| 223 | assertEquals(expected.getAudioAttributes(), actual.getAudioAttributes()); |
| 224 | assertEquals(expected.getLightColor(), actual.getLightColor()); |
| 225 | } |
| 226 | |
| 227 | private void compareGroups(NotificationChannelGroup expected, NotificationChannelGroup actual) { |
| 228 | assertEquals(expected.getId(), actual.getId()); |
| 229 | assertEquals(expected.getName(), actual.getName()); |
| 230 | assertEquals(expected.getDescription(), actual.getDescription()); |
| 231 | assertEquals(expected.isBlocked(), actual.isBlocked()); |
| 232 | } |
| 233 | |
| 234 | private NotificationChannel getChannel() { |
| 235 | return new NotificationChannel("id", "name", IMPORTANCE_LOW); |
| 236 | } |
| 237 | |
| 238 | private NotificationChannel findChannel(List<NotificationChannel> channels, String id) { |
| 239 | for (NotificationChannel channel : channels) { |
| 240 | if (channel.getId().equals(id)) { |
| 241 | return channel; |
| 242 | } |
| 243 | } |
| 244 | return null; |
| 245 | } |
| 246 | |
| 247 | private void resetZenModeHelper() { |
| 248 | reset(mMockZenModeHelper); |
| 249 | when(mMockZenModeHelper.getNotificationPolicy()).thenReturn(mTestNotificationPolicy); |
| 250 | } |
| 251 | |
| 252 | @Test |
| 253 | public void testChannelXml() throws Exception { |
| 254 | NotificationChannelGroup ncg = new NotificationChannelGroup("1", "bye"); |
| 255 | ncg.setBlocked(true); |
| 256 | ncg.setDescription("group desc"); |
| 257 | NotificationChannelGroup ncg2 = new NotificationChannelGroup("2", "hello"); |
| 258 | NotificationChannel channel1 = |
| 259 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 260 | NotificationChannel channel2 = |
| 261 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 262 | channel2.setDescription("descriptions for all"); |
| 263 | channel2.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes); |
| 264 | channel2.enableLights(true); |
| 265 | channel2.setBypassDnd(true); |
| 266 | channel2.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 267 | channel2.enableVibration(true); |
| 268 | channel2.setGroup(ncg.getId()); |
| 269 | channel2.setVibrationPattern(new long[]{100, 67, 145, 156}); |
| 270 | channel2.setLightColor(Color.BLUE); |
| 271 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 272 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true); |
| 273 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg2, true); |
| 274 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false); |
| 275 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, false, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 276 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 277 | mHelper.setShowBadge(PKG_N_MR1, UID_N_MR1, true); |
| 278 | mHelper.setAppImportanceLocked(PKG_N_MR1, UID_N_MR1); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 279 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 280 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, false, channel1.getId(), |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 281 | channel2.getId(), NotificationChannel.DEFAULT_CHANNEL_ID); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 282 | mHelper.onPackagesChanged(true, UserHandle.myUserId(), new String[]{PKG_N_MR1}, new int[]{ |
| 283 | UID_N_MR1}); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 284 | |
| 285 | loadStreamXml(baos, false); |
| 286 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 287 | assertTrue(mHelper.canShowBadge(PKG_N_MR1, UID_N_MR1)); |
| 288 | assertTrue(mHelper.getIsAppImportanceLocked(PKG_N_MR1, UID_N_MR1)); |
| 289 | assertEquals(channel1, mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1.getId(), false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 290 | compareChannels(channel2, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 291 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2.getId(), false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 292 | |
| 293 | List<NotificationChannelGroup> actualGroups = |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 294 | mHelper.getNotificationChannelGroups(PKG_N_MR1, UID_N_MR1, false, true).getList(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 295 | boolean foundNcg = false; |
| 296 | for (NotificationChannelGroup actual : actualGroups) { |
| 297 | if (ncg.getId().equals(actual.getId())) { |
| 298 | foundNcg = true; |
| 299 | compareGroups(ncg, actual); |
| 300 | } else if (ncg2.getId().equals(actual.getId())) { |
| 301 | compareGroups(ncg2, actual); |
| 302 | } |
| 303 | } |
| 304 | assertTrue(foundNcg); |
| 305 | |
| 306 | boolean foundChannel2Group = false; |
| 307 | for (NotificationChannelGroup actual : actualGroups) { |
| 308 | if (channel2.getGroup().equals(actual.getChannels().get(0).getGroup())) { |
| 309 | foundChannel2Group = true; |
| 310 | break; |
| 311 | } |
| 312 | } |
| 313 | assertTrue(foundChannel2Group); |
| 314 | } |
| 315 | |
| 316 | @Test |
| 317 | public void testChannelXmlForBackup() throws Exception { |
| 318 | NotificationChannelGroup ncg = new NotificationChannelGroup("1", "bye"); |
| 319 | NotificationChannelGroup ncg2 = new NotificationChannelGroup("2", "hello"); |
| 320 | NotificationChannel channel1 = |
| 321 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 322 | NotificationChannel channel2 = |
| 323 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 324 | channel2.setDescription("descriptions for all"); |
| 325 | channel2.setSound(SOUND_URI, mAudioAttributes); |
| 326 | channel2.enableLights(true); |
| 327 | channel2.setBypassDnd(true); |
| 328 | channel2.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 329 | channel2.enableVibration(false); |
| 330 | channel2.setGroup(ncg.getId()); |
| 331 | channel2.setLightColor(Color.BLUE); |
| 332 | NotificationChannel channel3 = new NotificationChannel("id3", "NAM3", IMPORTANCE_HIGH); |
| 333 | channel3.enableVibration(true); |
| 334 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 335 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true); |
| 336 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg2, true); |
| 337 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false); |
| 338 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, false, false); |
| 339 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3, false, false); |
| 340 | mHelper.createNotificationChannel(PKG_O, UID_O, getChannel(), true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 341 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 342 | mHelper.setShowBadge(PKG_N_MR1, UID_N_MR1, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 343 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 344 | mHelper.setImportance(PKG_O, UID_O, IMPORTANCE_NONE); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 345 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 346 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, true, channel1.getId(), |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 347 | channel2.getId(), channel3.getId(), NotificationChannel.DEFAULT_CHANNEL_ID); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 348 | mHelper.onPackagesChanged(true, UserHandle.myUserId(), new String[]{PKG_N_MR1, PKG_O}, |
| 349 | new int[]{UID_N_MR1, UID_O}); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 350 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 351 | mHelper.setShowBadge(PKG_O, UID_O, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 352 | |
| 353 | loadStreamXml(baos, true); |
| 354 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 355 | assertEquals(IMPORTANCE_NONE, mHelper.getImportance(PKG_O, UID_O)); |
| 356 | assertTrue(mHelper.canShowBadge(PKG_N_MR1, UID_N_MR1)); |
| 357 | assertEquals(channel1, mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1.getId(), false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 358 | compareChannels(channel2, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 359 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2.getId(), false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 360 | compareChannels(channel3, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 361 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3.getId(), false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 362 | |
| 363 | List<NotificationChannelGroup> actualGroups = |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 364 | mHelper.getNotificationChannelGroups(PKG_N_MR1, UID_N_MR1, false, true).getList(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 365 | boolean foundNcg = false; |
| 366 | for (NotificationChannelGroup actual : actualGroups) { |
| 367 | if (ncg.getId().equals(actual.getId())) { |
| 368 | foundNcg = true; |
| 369 | compareGroups(ncg, actual); |
| 370 | } else if (ncg2.getId().equals(actual.getId())) { |
| 371 | compareGroups(ncg2, actual); |
| 372 | } |
| 373 | } |
| 374 | assertTrue(foundNcg); |
| 375 | |
| 376 | boolean foundChannel2Group = false; |
| 377 | for (NotificationChannelGroup actual : actualGroups) { |
| 378 | if (channel2.getGroup().equals(actual.getChannels().get(0).getGroup())) { |
| 379 | foundChannel2Group = true; |
| 380 | break; |
| 381 | } |
| 382 | } |
| 383 | assertTrue(foundChannel2Group); |
| 384 | } |
| 385 | |
| 386 | @Test |
| 387 | public void testBackupXml_backupCanonicalizedSoundUri() throws Exception { |
| 388 | NotificationChannel channel = |
| 389 | new NotificationChannel("id", "name", IMPORTANCE_LOW); |
| 390 | channel.setSound(SOUND_URI, mAudioAttributes); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 391 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 392 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 393 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, true, channel.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 394 | |
| 395 | // Testing that in restore we are given the canonical version |
| 396 | loadStreamXml(baos, true); |
| 397 | verify(mTestIContentProvider).uncanonicalize(any(), eq(CANONICAL_SOUND_URI)); |
| 398 | } |
| 399 | |
| 400 | @Test |
| 401 | public void testRestoreXml_withExistentCanonicalizedSoundUri() throws Exception { |
| 402 | Uri localUri = Uri.parse("content://" + TEST_AUTHORITY + "/local/url"); |
| 403 | Uri canonicalBasedOnLocal = localUri.buildUpon() |
| 404 | .appendQueryParameter("title", "Test") |
| 405 | .appendQueryParameter("canonical", "1") |
| 406 | .build(); |
| 407 | when(mTestIContentProvider.canonicalize(any(), eq(CANONICAL_SOUND_URI))) |
| 408 | .thenReturn(canonicalBasedOnLocal); |
| 409 | when(mTestIContentProvider.uncanonicalize(any(), eq(CANONICAL_SOUND_URI))) |
| 410 | .thenReturn(localUri); |
| 411 | when(mTestIContentProvider.uncanonicalize(any(), eq(canonicalBasedOnLocal))) |
| 412 | .thenReturn(localUri); |
| 413 | |
| 414 | NotificationChannel channel = |
| 415 | new NotificationChannel("id", "name", IMPORTANCE_LOW); |
| 416 | channel.setSound(SOUND_URI, mAudioAttributes); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 417 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
| 418 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, true, channel.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 419 | |
| 420 | loadStreamXml(baos, true); |
| 421 | |
| 422 | NotificationChannel actualChannel = mHelper.getNotificationChannel( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 423 | PKG_N_MR1, UID_N_MR1, channel.getId(), false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 424 | assertEquals(localUri, actualChannel.getSound()); |
| 425 | } |
| 426 | |
| 427 | @Test |
| 428 | public void testRestoreXml_withNonExistentCanonicalizedSoundUri() throws Exception { |
| 429 | Thread.sleep(3000); |
| 430 | when(mTestIContentProvider.canonicalize(any(), eq(CANONICAL_SOUND_URI))) |
| 431 | .thenReturn(null); |
| 432 | when(mTestIContentProvider.uncanonicalize(any(), eq(CANONICAL_SOUND_URI))) |
| 433 | .thenReturn(null); |
| 434 | |
| 435 | NotificationChannel channel = |
| 436 | new NotificationChannel("id", "name", IMPORTANCE_LOW); |
| 437 | channel.setSound(SOUND_URI, mAudioAttributes); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 438 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
| 439 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, true, channel.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 440 | |
| 441 | loadStreamXml(baos, true); |
| 442 | |
| 443 | NotificationChannel actualChannel = mHelper.getNotificationChannel( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 444 | PKG_N_MR1, UID_N_MR1, channel.getId(), false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 445 | assertEquals(Settings.System.DEFAULT_NOTIFICATION_URI, actualChannel.getSound()); |
| 446 | } |
| 447 | |
| 448 | |
| 449 | /** |
| 450 | * Although we don't make backups with uncanonicalized uris anymore, we used to, so we have to |
| 451 | * handle its restore properly. |
| 452 | */ |
| 453 | @Test |
| 454 | public void testRestoreXml_withUncanonicalizedNonLocalSoundUri() throws Exception { |
| 455 | // Not a local uncanonicalized uri, simulating that it fails to exist locally |
| 456 | when(mTestIContentProvider.canonicalize(any(), eq(SOUND_URI))).thenReturn(null); |
| 457 | String id = "id"; |
| 458 | String backupWithUncanonicalizedSoundUri = "<ranking version=\"1\">\n" |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 459 | + "<package name=\"" + PKG_N_MR1 + "\" show_badge=\"true\">\n" |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 460 | + "<channel id=\"" + id + "\" name=\"name\" importance=\"2\" " |
| 461 | + "sound=\"" + SOUND_URI + "\" " |
| 462 | + "usage=\"6\" content_type=\"0\" flags=\"1\" show_badge=\"true\" />\n" |
| 463 | + "<channel id=\"miscellaneous\" name=\"Uncategorized\" usage=\"5\" " |
| 464 | + "content_type=\"4\" flags=\"0\" show_badge=\"true\" />\n" |
| 465 | + "</package>\n" |
| 466 | + "</ranking>\n"; |
| 467 | |
| 468 | loadByteArrayXml(backupWithUncanonicalizedSoundUri.getBytes(), true); |
| 469 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 470 | NotificationChannel actualChannel = mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, id, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 471 | assertEquals(Settings.System.DEFAULT_NOTIFICATION_URI, actualChannel.getSound()); |
| 472 | } |
| 473 | |
| 474 | @Test |
| 475 | public void testBackupRestoreXml_withNullSoundUri() throws Exception { |
| 476 | NotificationChannel channel = |
| 477 | new NotificationChannel("id", "name", IMPORTANCE_LOW); |
| 478 | channel.setSound(null, mAudioAttributes); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 479 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
| 480 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, true, channel.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 481 | |
| 482 | loadStreamXml(baos, true); |
| 483 | |
| 484 | NotificationChannel actualChannel = mHelper.getNotificationChannel( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 485 | PKG_N_MR1, UID_N_MR1, channel.getId(), false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 486 | assertEquals(null, actualChannel.getSound()); |
| 487 | } |
| 488 | |
| 489 | @Test |
| 490 | public void testChannelXml_backup() throws Exception { |
| 491 | NotificationChannelGroup ncg = new NotificationChannelGroup("1", "bye"); |
| 492 | NotificationChannelGroup ncg2 = new NotificationChannelGroup("2", "hello"); |
| 493 | NotificationChannel channel1 = |
| 494 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 495 | NotificationChannel channel2 = |
| 496 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 497 | NotificationChannel channel3 = |
| 498 | new NotificationChannel("id3", "name3", IMPORTANCE_LOW); |
| 499 | channel3.setGroup(ncg.getId()); |
| 500 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 501 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true); |
| 502 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg2, true); |
| 503 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false); |
| 504 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, false, false); |
| 505 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 506 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 507 | mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1.getId()); |
| 508 | mHelper.deleteNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg.getId()); |
| 509 | assertEquals(channel2, mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2.getId(), false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 510 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 511 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, true, channel1.getId(), |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 512 | channel2.getId(), channel3.getId(), NotificationChannel.DEFAULT_CHANNEL_ID); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 513 | mHelper.onPackagesChanged(true, UserHandle.myUserId(), new String[]{PKG_N_MR1}, new int[]{ |
| 514 | UID_N_MR1}); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 515 | |
| 516 | XmlPullParser parser = Xml.newPullParser(); |
| 517 | parser.setInput(new BufferedInputStream(new ByteArrayInputStream(baos.toByteArray())), |
| 518 | null); |
| 519 | parser.nextTag(); |
| 520 | mHelper.readXml(parser, true); |
| 521 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 522 | assertNull(mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1.getId(), false)); |
| 523 | assertNull(mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3.getId(), false)); |
| 524 | assertNull(mHelper.getNotificationChannelGroup(ncg.getId(), PKG_N_MR1, UID_N_MR1)); |
| 525 | //assertEquals(ncg2, mHelper.getNotificationChannelGroup(ncg2.getId(), PKG_N_MR1, UID_N_MR1)); |
| 526 | assertEquals(channel2, mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2.getId(), false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | @Test |
| 530 | public void testChannelXml_defaultChannelLegacyApp_noUserSettings() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 531 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, false, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 532 | NotificationChannel.DEFAULT_CHANNEL_ID); |
| 533 | |
| 534 | loadStreamXml(baos, false); |
| 535 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 536 | final NotificationChannel updated = mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 537 | NotificationChannel.DEFAULT_CHANNEL_ID, false); |
| 538 | assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, updated.getImportance()); |
| 539 | assertFalse(updated.canBypassDnd()); |
| 540 | assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE, updated.getLockscreenVisibility()); |
| 541 | assertEquals(0, updated.getUserLockedFields()); |
| 542 | } |
| 543 | |
| 544 | @Test |
| 545 | public void testChannelXml_defaultChannelUpdatedApp_userSettings() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 546 | final NotificationChannel defaultChannel = mHelper.getNotificationChannel(PKG_N_MR1, |
| 547 | UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 548 | NotificationChannel.DEFAULT_CHANNEL_ID, false); |
| 549 | defaultChannel.setImportance(NotificationManager.IMPORTANCE_LOW); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 550 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, defaultChannel, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 551 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 552 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, false, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 553 | NotificationChannel.DEFAULT_CHANNEL_ID); |
| 554 | |
| 555 | loadStreamXml(baos, false); |
| 556 | |
| 557 | assertEquals(NotificationManager.IMPORTANCE_LOW, mHelper.getNotificationChannel( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 558 | PKG_N_MR1, UID_N_MR1, NotificationChannel.DEFAULT_CHANNEL_ID, false).getImportance()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | @Test |
| 562 | public void testChannelXml_upgradeCreateDefaultChannel() throws Exception { |
| 563 | final String preupgradeXml = "<ranking version=\"1\">\n" |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 564 | + "<package name=\"" + PKG_N_MR1 |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 565 | + "\" importance=\"" + NotificationManager.IMPORTANCE_HIGH |
| 566 | + "\" priority=\"" + Notification.PRIORITY_MAX + "\" visibility=\"" |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 567 | + Notification.VISIBILITY_SECRET + "\"" +" uid=\"" + UID_N_MR1 + "\" />\n" |
| 568 | + "<package name=\"" + PKG_O + "\" uid=\"" + UID_O + "\" visibility=\"" |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 569 | + Notification.VISIBILITY_PRIVATE + "\" />\n" |
| 570 | + "</ranking>"; |
| 571 | XmlPullParser parser = Xml.newPullParser(); |
| 572 | parser.setInput(new BufferedInputStream(new ByteArrayInputStream(preupgradeXml.getBytes())), |
| 573 | null); |
| 574 | parser.nextTag(); |
| 575 | mHelper.readXml(parser, false); |
| 576 | |
| 577 | final NotificationChannel updated1 = |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 578 | 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] | 579 | assertEquals(NotificationManager.IMPORTANCE_HIGH, updated1.getImportance()); |
| 580 | assertTrue(updated1.canBypassDnd()); |
| 581 | assertEquals(Notification.VISIBILITY_SECRET, updated1.getLockscreenVisibility()); |
| 582 | assertEquals(NotificationChannel.USER_LOCKED_IMPORTANCE |
| 583 | | NotificationChannel.USER_LOCKED_PRIORITY |
| 584 | | NotificationChannel.USER_LOCKED_VISIBILITY, |
| 585 | updated1.getUserLockedFields()); |
| 586 | |
| 587 | // No Default Channel created for updated packages |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 588 | assertEquals(null, mHelper.getNotificationChannel(PKG_O, UID_O, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 589 | NotificationChannel.DEFAULT_CHANNEL_ID, false)); |
| 590 | } |
| 591 | |
| 592 | @Test |
| 593 | public void testChannelXml_upgradeDeletesDefaultChannel() throws Exception { |
| 594 | final NotificationChannel defaultChannel = mHelper.getNotificationChannel( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 595 | PKG_N_MR1, UID_N_MR1, NotificationChannel.DEFAULT_CHANNEL_ID, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 596 | assertTrue(defaultChannel != null); |
| 597 | ByteArrayOutputStream baos = |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 598 | writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, false, NotificationChannel.DEFAULT_CHANNEL_ID); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 599 | // Load package at higher sdk. |
| 600 | final ApplicationInfo upgraded = new ApplicationInfo(); |
| 601 | upgraded.targetSdkVersion = Build.VERSION_CODES.N_MR1 + 1; |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 602 | when(mPm.getApplicationInfoAsUser(eq(PKG_N_MR1), anyInt(), anyInt())).thenReturn(upgraded); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 603 | loadStreamXml(baos, false); |
| 604 | |
| 605 | // Default Channel should be gone. |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 606 | assertEquals(null, mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 607 | NotificationChannel.DEFAULT_CHANNEL_ID, false)); |
| 608 | } |
| 609 | |
| 610 | @Test |
| 611 | public void testDeletesDefaultChannelAfterChannelIsCreated() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 612 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 613 | new NotificationChannel("bananas", "bananas", IMPORTANCE_LOW), true, false); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 614 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, false, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 615 | NotificationChannel.DEFAULT_CHANNEL_ID, "bananas"); |
| 616 | |
| 617 | // Load package at higher sdk. |
| 618 | final ApplicationInfo upgraded = new ApplicationInfo(); |
| 619 | upgraded.targetSdkVersion = Build.VERSION_CODES.N_MR1 + 1; |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 620 | when(mPm.getApplicationInfoAsUser(eq(PKG_N_MR1), anyInt(), anyInt())).thenReturn(upgraded); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 621 | loadStreamXml(baos, false); |
| 622 | |
| 623 | // Default Channel should be gone. |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 624 | assertEquals(null, mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 625 | NotificationChannel.DEFAULT_CHANNEL_ID, false)); |
| 626 | } |
| 627 | |
| 628 | @Test |
| 629 | public void testLoadingOldChannelsDoesNotDeleteNewlyCreatedChannels() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 630 | ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, false, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 631 | NotificationChannel.DEFAULT_CHANNEL_ID, "bananas"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 632 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 633 | new NotificationChannel("bananas", "bananas", IMPORTANCE_LOW), true, false); |
| 634 | |
| 635 | loadStreamXml(baos, false); |
| 636 | |
| 637 | // 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] | 638 | assertTrue(mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "bananas", false) != null); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | @Test |
| 642 | public void testCreateChannel_blocked() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 643 | mHelper.setImportance(PKG_N_MR1, UID_N_MR1, IMPORTANCE_NONE); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 644 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 645 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 646 | new NotificationChannel("bananas", "bananas", IMPORTANCE_LOW), true, false); |
| 647 | } |
| 648 | |
| 649 | @Test |
| 650 | public void testCreateChannel_badImportance() throws Exception { |
| 651 | try { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 652 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 653 | new NotificationChannel("bananas", "bananas", IMPORTANCE_NONE - 1), |
| 654 | true, false); |
| 655 | fail("Was allowed to create a channel with invalid importance"); |
| 656 | } catch (IllegalArgumentException e) { |
| 657 | // yay |
| 658 | } |
| 659 | try { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 660 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 661 | new NotificationChannel("bananas", "bananas", IMPORTANCE_UNSPECIFIED), |
| 662 | true, false); |
| 663 | fail("Was allowed to create a channel with invalid importance"); |
| 664 | } catch (IllegalArgumentException e) { |
| 665 | // yay |
| 666 | } |
| 667 | try { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 668 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 669 | new NotificationChannel("bananas", "bananas", IMPORTANCE_MAX + 1), |
| 670 | true, false); |
| 671 | fail("Was allowed to create a channel with invalid importance"); |
| 672 | } catch (IllegalArgumentException e) { |
| 673 | // yay |
| 674 | } |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 675 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 676 | new NotificationChannel("bananas", "bananas", IMPORTANCE_NONE), true, false); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 677 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 678 | new NotificationChannel("bananas", "bananas", IMPORTANCE_MAX), true, false); |
| 679 | } |
| 680 | |
| 681 | |
| 682 | @Test |
| 683 | public void testUpdate() throws Exception { |
| 684 | // no fields locked by user |
| 685 | final NotificationChannel channel = |
| 686 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 687 | channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes); |
| 688 | channel.enableLights(true); |
| 689 | channel.setBypassDnd(true); |
| 690 | channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 691 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 692 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, false, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 693 | |
| 694 | // same id, try to update all fields |
| 695 | final NotificationChannel channel2 = |
| 696 | new NotificationChannel("id2", "name2", NotificationManager.IMPORTANCE_HIGH); |
| 697 | channel2.setSound(new Uri.Builder().scheme("test2").build(), mAudioAttributes); |
| 698 | channel2.enableLights(false); |
| 699 | channel2.setBypassDnd(false); |
| 700 | channel2.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC); |
| 701 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 702 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 703 | |
| 704 | // all fields should be changed |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 705 | 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] | 706 | |
| 707 | verify(mHandler, times(1)).requestSort(); |
| 708 | } |
| 709 | |
| 710 | @Test |
| 711 | public void testUpdate_preUpgrade_updatesAppFields() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 712 | mHelper.setImportance(PKG_N_MR1, UID_N_MR1, IMPORTANCE_UNSPECIFIED); |
| 713 | assertTrue(mHelper.canShowBadge(PKG_N_MR1, UID_N_MR1)); |
| 714 | assertEquals(Notification.PRIORITY_DEFAULT, mHelper.getPackagePriority(PKG_N_MR1, UID_N_MR1)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 715 | assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 716 | mHelper.getPackageVisibility(PKG_N_MR1, UID_N_MR1)); |
| 717 | assertFalse(mHelper.getIsAppImportanceLocked(PKG_N_MR1, UID_N_MR1)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 718 | |
| 719 | NotificationChannel defaultChannel = mHelper.getNotificationChannel( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 720 | PKG_N_MR1, UID_N_MR1, NotificationChannel.DEFAULT_CHANNEL_ID, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 721 | |
| 722 | defaultChannel.setShowBadge(false); |
| 723 | defaultChannel.setImportance(IMPORTANCE_NONE); |
| 724 | defaultChannel.setBypassDnd(true); |
| 725 | defaultChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 726 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 727 | mHelper.setAppImportanceLocked(PKG_N_MR1, UID_N_MR1); |
| 728 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, defaultChannel, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 729 | |
| 730 | // ensure app level fields are changed |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 731 | assertFalse(mHelper.canShowBadge(PKG_N_MR1, UID_N_MR1)); |
| 732 | assertEquals(Notification.PRIORITY_MAX, mHelper.getPackagePriority(PKG_N_MR1, UID_N_MR1)); |
| 733 | assertEquals(Notification.VISIBILITY_SECRET, mHelper.getPackageVisibility(PKG_N_MR1, |
| 734 | UID_N_MR1)); |
| 735 | assertEquals(IMPORTANCE_NONE, mHelper.getImportance(PKG_N_MR1, UID_N_MR1)); |
| 736 | assertTrue(mHelper.getIsAppImportanceLocked(PKG_N_MR1, UID_N_MR1)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | @Test |
| 740 | public void testUpdate_postUpgrade_noUpdateAppFields() throws Exception { |
| 741 | final NotificationChannel channel = new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 742 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 743 | mHelper.createNotificationChannel(PKG_O, UID_O, channel, false, false); |
| 744 | assertTrue(mHelper.canShowBadge(PKG_O, UID_O)); |
| 745 | assertEquals(Notification.PRIORITY_DEFAULT, mHelper.getPackagePriority(PKG_O, UID_O)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 746 | assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 747 | mHelper.getPackageVisibility(PKG_O, UID_O)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 748 | |
| 749 | channel.setShowBadge(false); |
| 750 | channel.setImportance(IMPORTANCE_NONE); |
| 751 | channel.setBypassDnd(true); |
| 752 | channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 753 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 754 | mHelper.updateNotificationChannel(PKG_O, UID_O, channel, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 755 | |
| 756 | // ensure app level fields are not changed |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 757 | assertTrue(mHelper.canShowBadge(PKG_O, UID_O)); |
| 758 | assertEquals(Notification.PRIORITY_DEFAULT, mHelper.getPackagePriority(PKG_O, UID_O)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 759 | assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 760 | mHelper.getPackageVisibility(PKG_O, UID_O)); |
| 761 | assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, mHelper.getImportance(PKG_O, |
| 762 | UID_O)); |
| 763 | assertFalse(mHelper.getIsAppImportanceLocked(PKG_O, UID_O)); |
| 764 | } |
| 765 | |
| 766 | @Test |
| 767 | public void testUpdate_preUpgrade_noUpdateAppFieldsWithMultipleChannels() throws Exception { |
| 768 | final NotificationChannel channel = new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 769 | |
| 770 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, false, false); |
| 771 | assertTrue(mHelper.canShowBadge(PKG_N_MR1, UID_N_MR1)); |
| 772 | assertEquals(Notification.PRIORITY_DEFAULT, mHelper.getPackagePriority(PKG_N_MR1, UID_N_MR1)); |
| 773 | assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE, |
| 774 | mHelper.getPackageVisibility(PKG_N_MR1, UID_N_MR1)); |
| 775 | |
| 776 | channel.setShowBadge(false); |
| 777 | channel.setImportance(IMPORTANCE_NONE); |
| 778 | channel.setBypassDnd(true); |
| 779 | channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 780 | |
| 781 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true); |
| 782 | |
| 783 | NotificationChannel defaultChannel = mHelper.getNotificationChannel( |
| 784 | PKG_N_MR1, UID_N_MR1, NotificationChannel.DEFAULT_CHANNEL_ID, false); |
| 785 | |
| 786 | defaultChannel.setShowBadge(false); |
| 787 | defaultChannel.setImportance(IMPORTANCE_NONE); |
| 788 | defaultChannel.setBypassDnd(true); |
| 789 | defaultChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 790 | |
| 791 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, defaultChannel, true); |
| 792 | |
| 793 | // ensure app level fields are not changed |
| 794 | assertTrue(mHelper.canShowBadge(PKG_N_MR1, UID_N_MR1)); |
| 795 | assertEquals(Notification.PRIORITY_DEFAULT, mHelper.getPackagePriority(PKG_N_MR1, UID_N_MR1)); |
| 796 | assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE, |
| 797 | mHelper.getPackageVisibility(PKG_N_MR1, UID_N_MR1)); |
| 798 | assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, mHelper.getImportance(PKG_N_MR1, |
| 799 | UID_N_MR1)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | @Test |
| 803 | public void testGetNotificationChannel_ReturnsNullForUnknownChannel() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 804 | assertEquals(null, mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "garbage", false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | @Test |
| 808 | public void testCreateChannel_CannotChangeHiddenFields() throws Exception { |
| 809 | final NotificationChannel channel = |
| 810 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 811 | channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes); |
| 812 | channel.enableLights(true); |
| 813 | channel.setBypassDnd(true); |
| 814 | channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 815 | channel.setShowBadge(true); |
| 816 | int lockMask = 0; |
| 817 | for (int i = 0; i < NotificationChannel.LOCKABLE_FIELDS.length; i++) { |
| 818 | lockMask |= NotificationChannel.LOCKABLE_FIELDS[i]; |
| 819 | } |
| 820 | channel.lockFields(lockMask); |
| 821 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 822 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 823 | |
| 824 | NotificationChannel savedChannel = |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 825 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId(), false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 826 | |
| 827 | assertEquals(channel.getName(), savedChannel.getName()); |
| 828 | assertEquals(channel.shouldShowLights(), savedChannel.shouldShowLights()); |
| 829 | assertFalse(savedChannel.canBypassDnd()); |
| 830 | assertFalse(Notification.VISIBILITY_SECRET == savedChannel.getLockscreenVisibility()); |
| 831 | assertEquals(channel.canShowBadge(), savedChannel.canShowBadge()); |
| 832 | |
| 833 | verify(mHandler, never()).requestSort(); |
| 834 | } |
| 835 | |
| 836 | @Test |
| 837 | public void testCreateChannel_CannotChangeHiddenFieldsAssistant() throws Exception { |
| 838 | final NotificationChannel channel = |
| 839 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 840 | channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes); |
| 841 | channel.enableLights(true); |
| 842 | channel.setBypassDnd(true); |
| 843 | channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 844 | channel.setShowBadge(true); |
| 845 | int lockMask = 0; |
| 846 | for (int i = 0; i < NotificationChannel.LOCKABLE_FIELDS.length; i++) { |
| 847 | lockMask |= NotificationChannel.LOCKABLE_FIELDS[i]; |
| 848 | } |
| 849 | channel.lockFields(lockMask); |
| 850 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 851 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 852 | |
| 853 | NotificationChannel savedChannel = |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 854 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId(), false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 855 | |
| 856 | assertEquals(channel.getName(), savedChannel.getName()); |
| 857 | assertEquals(channel.shouldShowLights(), savedChannel.shouldShowLights()); |
| 858 | assertFalse(savedChannel.canBypassDnd()); |
| 859 | assertFalse(Notification.VISIBILITY_SECRET == savedChannel.getLockscreenVisibility()); |
| 860 | assertEquals(channel.canShowBadge(), savedChannel.canShowBadge()); |
| 861 | } |
| 862 | |
| 863 | @Test |
| 864 | public void testClearLockedFields() throws Exception { |
| 865 | final NotificationChannel channel = getChannel(); |
| 866 | mHelper.clearLockedFields(channel); |
| 867 | assertEquals(0, channel.getUserLockedFields()); |
| 868 | |
| 869 | channel.lockFields(NotificationChannel.USER_LOCKED_PRIORITY |
| 870 | | NotificationChannel.USER_LOCKED_IMPORTANCE); |
| 871 | mHelper.clearLockedFields(channel); |
| 872 | assertEquals(0, channel.getUserLockedFields()); |
| 873 | } |
| 874 | |
| 875 | @Test |
| 876 | public void testLockFields_soundAndVibration() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 877 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel(), true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 878 | |
| 879 | final NotificationChannel update1 = getChannel(); |
| 880 | update1.setSound(new Uri.Builder().scheme("test").build(), |
| 881 | new AudioAttributes.Builder().build()); |
| 882 | update1.lockFields(NotificationChannel.USER_LOCKED_PRIORITY); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 883 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update1, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 884 | assertEquals(NotificationChannel.USER_LOCKED_PRIORITY |
| 885 | | NotificationChannel.USER_LOCKED_SOUND, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 886 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update1.getId(), false) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 887 | .getUserLockedFields()); |
| 888 | |
| 889 | NotificationChannel update2 = getChannel(); |
| 890 | update2.enableVibration(true); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 891 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update2, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 892 | assertEquals(NotificationChannel.USER_LOCKED_PRIORITY |
| 893 | | NotificationChannel.USER_LOCKED_SOUND |
| 894 | | NotificationChannel.USER_LOCKED_VIBRATION, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 895 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update2.getId(), false) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 896 | .getUserLockedFields()); |
| 897 | } |
| 898 | |
| 899 | @Test |
| 900 | public void testLockFields_vibrationAndLights() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 901 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel(), true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 902 | |
| 903 | final NotificationChannel update1 = getChannel(); |
| 904 | update1.setVibrationPattern(new long[]{7945, 46 ,246}); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 905 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update1, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 906 | assertEquals(NotificationChannel.USER_LOCKED_VIBRATION, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 907 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update1.getId(), false) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 908 | .getUserLockedFields()); |
| 909 | |
| 910 | final NotificationChannel update2 = getChannel(); |
| 911 | update2.enableLights(true); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 912 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update2, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 913 | assertEquals(NotificationChannel.USER_LOCKED_VIBRATION |
| 914 | | NotificationChannel.USER_LOCKED_LIGHTS, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 915 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update2.getId(), false) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 916 | .getUserLockedFields()); |
| 917 | } |
| 918 | |
| 919 | @Test |
| 920 | public void testLockFields_lightsAndImportance() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 921 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel(), true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 922 | |
| 923 | final NotificationChannel update1 = getChannel(); |
| 924 | update1.setLightColor(Color.GREEN); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 925 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update1, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 926 | assertEquals(NotificationChannel.USER_LOCKED_LIGHTS, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 927 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update1.getId(), false) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 928 | .getUserLockedFields()); |
| 929 | |
| 930 | final NotificationChannel update2 = getChannel(); |
| 931 | update2.setImportance(IMPORTANCE_DEFAULT); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 932 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update2, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 933 | assertEquals(NotificationChannel.USER_LOCKED_LIGHTS |
| 934 | | NotificationChannel.USER_LOCKED_IMPORTANCE, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 935 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update2.getId(), false) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 936 | .getUserLockedFields()); |
| 937 | } |
| 938 | |
| 939 | @Test |
| 940 | public void testLockFields_visibilityAndDndAndBadge() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 941 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel(), true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 942 | assertEquals(0, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 943 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel().getId(), false) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 944 | .getUserLockedFields()); |
| 945 | |
| 946 | final NotificationChannel update1 = getChannel(); |
| 947 | update1.setBypassDnd(true); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 948 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update1, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 949 | assertEquals(NotificationChannel.USER_LOCKED_PRIORITY, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 950 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update1.getId(), false) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 951 | .getUserLockedFields()); |
| 952 | |
| 953 | final NotificationChannel update2 = getChannel(); |
| 954 | update2.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 955 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update2, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 956 | assertEquals(NotificationChannel.USER_LOCKED_PRIORITY |
| 957 | | NotificationChannel.USER_LOCKED_VISIBILITY, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 958 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update2.getId(), false) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 959 | .getUserLockedFields()); |
| 960 | |
| 961 | final NotificationChannel update3 = getChannel(); |
| 962 | update3.setShowBadge(false); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 963 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update3, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 964 | assertEquals(NotificationChannel.USER_LOCKED_PRIORITY |
| 965 | | NotificationChannel.USER_LOCKED_VISIBILITY |
| 966 | | NotificationChannel.USER_LOCKED_SHOW_BADGE, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 967 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update3.getId(), false) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 968 | .getUserLockedFields()); |
| 969 | } |
| 970 | |
| 971 | @Test |
| 972 | public void testDeleteNonExistentChannel() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 973 | mHelper.deleteNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, "does not exist"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | @Test |
| 977 | public void testGetDeletedChannel() throws Exception { |
| 978 | NotificationChannel channel = getChannel(); |
| 979 | channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes); |
| 980 | channel.enableLights(true); |
| 981 | channel.setBypassDnd(true); |
| 982 | channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); |
| 983 | channel.enableVibration(true); |
| 984 | channel.setVibrationPattern(new long[]{100, 67, 145, 156}); |
| 985 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 986 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
| 987 | mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 988 | |
| 989 | // Does not return deleted channel |
| 990 | NotificationChannel response = |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 991 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId(), false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 992 | assertNull(response); |
| 993 | |
| 994 | // Returns deleted channel |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 995 | response = mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId(), true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 996 | compareChannels(channel, response); |
| 997 | assertTrue(response.isDeleted()); |
| 998 | } |
| 999 | |
| 1000 | @Test |
| 1001 | public void testGetDeletedChannels() throws Exception { |
| 1002 | Map<String, NotificationChannel> channelMap = new HashMap<>(); |
| 1003 | NotificationChannel channel = |
| 1004 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 1005 | channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes); |
| 1006 | channel.enableLights(true); |
| 1007 | channel.setBypassDnd(true); |
| 1008 | channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); |
| 1009 | channel.enableVibration(true); |
| 1010 | channel.setVibrationPattern(new long[]{100, 67, 145, 156}); |
| 1011 | channelMap.put(channel.getId(), channel); |
| 1012 | NotificationChannel channel2 = |
| 1013 | new NotificationChannel("id4", "a", NotificationManager.IMPORTANCE_HIGH); |
| 1014 | channelMap.put(channel2.getId(), channel2); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1015 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
| 1016 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1017 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1018 | mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1019 | |
| 1020 | // Returns only non-deleted channels |
| 1021 | List<NotificationChannel> channels = |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1022 | mHelper.getNotificationChannels(PKG_N_MR1, UID_N_MR1, false).getList(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1023 | assertEquals(2, channels.size()); // Default channel + non-deleted channel |
| 1024 | for (NotificationChannel nc : channels) { |
| 1025 | if (!NotificationChannel.DEFAULT_CHANNEL_ID.equals(nc.getId())) { |
| 1026 | compareChannels(channel2, nc); |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | // Returns deleted channels too |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1031 | channels = mHelper.getNotificationChannels(PKG_N_MR1, UID_N_MR1, true).getList(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1032 | assertEquals(3, channels.size()); // Includes default channel |
| 1033 | for (NotificationChannel nc : channels) { |
| 1034 | if (!NotificationChannel.DEFAULT_CHANNEL_ID.equals(nc.getId())) { |
| 1035 | compareChannels(channelMap.get(nc.getId()), nc); |
| 1036 | } |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | @Test |
| 1041 | public void testGetDeletedChannelCount() throws Exception { |
| 1042 | NotificationChannel channel = |
| 1043 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 1044 | NotificationChannel channel2 = |
| 1045 | new NotificationChannel("id4", "a", NotificationManager.IMPORTANCE_HIGH); |
| 1046 | NotificationChannel channel3 = |
| 1047 | new NotificationChannel("id5", "a", NotificationManager.IMPORTANCE_HIGH); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1048 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
| 1049 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, true, false); |
| 1050 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1051 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1052 | mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId()); |
| 1053 | mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1054 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1055 | assertEquals(2, mHelper.getDeletedChannelCount(PKG_N_MR1, UID_N_MR1)); |
| 1056 | assertEquals(0, mHelper.getDeletedChannelCount("pkg2", UID_O)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | @Test |
| 1060 | public void testGetBlockedChannelCount() throws Exception { |
| 1061 | NotificationChannel channel = |
| 1062 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 1063 | NotificationChannel channel2 = |
| 1064 | new NotificationChannel("id4", "a", NotificationManager.IMPORTANCE_NONE); |
| 1065 | NotificationChannel channel3 = |
| 1066 | new NotificationChannel("id5", "a", NotificationManager.IMPORTANCE_NONE); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1067 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
| 1068 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, true, false); |
| 1069 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1070 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1071 | mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1072 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1073 | assertEquals(1, mHelper.getBlockedChannelCount(PKG_N_MR1, UID_N_MR1)); |
| 1074 | assertEquals(0, mHelper.getBlockedChannelCount("pkg2", UID_O)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | @Test |
| 1078 | public void testCreateAndDeleteCanChannelsBypassDnd() throws Exception { |
| 1079 | // create notification channel that can't bypass dnd |
| 1080 | // expected result: areChannelsBypassingDnd = false |
| 1081 | // setNotificationPolicy isn't called since areChannelsBypassingDnd was already false |
| 1082 | NotificationChannel channel = new NotificationChannel("id1", "name1", IMPORTANCE_LOW); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1083 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1084 | assertFalse(mHelper.areChannelsBypassingDnd()); |
| 1085 | verify(mMockZenModeHelper, never()).setNotificationPolicy(any()); |
| 1086 | resetZenModeHelper(); |
| 1087 | |
| 1088 | // create notification channel that can bypass dnd |
| 1089 | // expected result: areChannelsBypassingDnd = true |
| 1090 | NotificationChannel channel2 = new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 1091 | channel2.setBypassDnd(true); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1092 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, true, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1093 | assertTrue(mHelper.areChannelsBypassingDnd()); |
| 1094 | verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any()); |
| 1095 | resetZenModeHelper(); |
| 1096 | |
| 1097 | // delete channels |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1098 | mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1099 | assertTrue(mHelper.areChannelsBypassingDnd()); // channel2 can still bypass DND |
| 1100 | verify(mMockZenModeHelper, never()).setNotificationPolicy(any()); |
| 1101 | resetZenModeHelper(); |
| 1102 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1103 | mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1104 | assertFalse(mHelper.areChannelsBypassingDnd()); |
| 1105 | verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any()); |
| 1106 | resetZenModeHelper(); |
| 1107 | } |
| 1108 | |
| 1109 | @Test |
| 1110 | public void testUpdateCanChannelsBypassDnd() throws Exception { |
| 1111 | // create notification channel that can't bypass dnd |
| 1112 | // expected result: areChannelsBypassingDnd = false |
| 1113 | // setNotificationPolicy isn't called since areChannelsBypassingDnd was already false |
| 1114 | NotificationChannel channel = new NotificationChannel("id1", "name1", IMPORTANCE_LOW); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1115 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1116 | assertFalse(mHelper.areChannelsBypassingDnd()); |
| 1117 | verify(mMockZenModeHelper, never()).setNotificationPolicy(any()); |
| 1118 | resetZenModeHelper(); |
| 1119 | |
| 1120 | // update channel so it CAN bypass dnd: |
| 1121 | // expected result: areChannelsBypassingDnd = true |
| 1122 | channel.setBypassDnd(true); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1123 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1124 | assertTrue(mHelper.areChannelsBypassingDnd()); |
| 1125 | verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any()); |
| 1126 | resetZenModeHelper(); |
| 1127 | |
| 1128 | // update channel so it can't bypass dnd: |
| 1129 | // expected result: areChannelsBypassingDnd = false |
| 1130 | channel.setBypassDnd(false); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1131 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1132 | assertFalse(mHelper.areChannelsBypassingDnd()); |
| 1133 | verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any()); |
| 1134 | resetZenModeHelper(); |
| 1135 | } |
| 1136 | |
| 1137 | @Test |
| 1138 | public void testSetupNewZenModeHelper_canBypass() { |
| 1139 | // start notification policy off with mAreChannelsBypassingDnd = true, but |
| 1140 | // RankingHelper should change to false |
| 1141 | mTestNotificationPolicy = new NotificationManager.Policy(0, 0, 0, 0, |
| 1142 | NotificationManager.Policy.STATE_CHANNELS_BYPASSING_DND); |
| 1143 | when(mMockZenModeHelper.getNotificationPolicy()).thenReturn(mTestNotificationPolicy); |
| 1144 | mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper); |
| 1145 | assertFalse(mHelper.areChannelsBypassingDnd()); |
| 1146 | verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any()); |
| 1147 | resetZenModeHelper(); |
| 1148 | } |
| 1149 | |
| 1150 | @Test |
| 1151 | public void testSetupNewZenModeHelper_cannotBypass() { |
| 1152 | // start notification policy off with mAreChannelsBypassingDnd = false |
| 1153 | mTestNotificationPolicy = new NotificationManager.Policy(0, 0, 0, 0, 0); |
| 1154 | when(mMockZenModeHelper.getNotificationPolicy()).thenReturn(mTestNotificationPolicy); |
| 1155 | mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper); |
| 1156 | assertFalse(mHelper.areChannelsBypassingDnd()); |
| 1157 | verify(mMockZenModeHelper, never()).setNotificationPolicy(any()); |
| 1158 | resetZenModeHelper(); |
| 1159 | } |
| 1160 | |
| 1161 | @Test |
| 1162 | public void testCreateDeletedChannel() throws Exception { |
| 1163 | long[] vibration = new long[]{100, 67, 145, 156}; |
| 1164 | NotificationChannel channel = |
| 1165 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 1166 | channel.setVibrationPattern(vibration); |
| 1167 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1168 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
| 1169 | mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1170 | |
| 1171 | NotificationChannel newChannel = new NotificationChannel( |
| 1172 | channel.getId(), channel.getName(), NotificationManager.IMPORTANCE_HIGH); |
| 1173 | newChannel.setVibrationPattern(new long[]{100}); |
| 1174 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1175 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, newChannel, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1176 | |
| 1177 | // No long deleted, using old settings |
| 1178 | compareChannels(channel, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1179 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, newChannel.getId(), false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | @Test |
| 1183 | public void testOnlyHasDefaultChannel() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1184 | assertTrue(mHelper.onlyHasDefaultChannel(PKG_N_MR1, UID_N_MR1)); |
| 1185 | assertFalse(mHelper.onlyHasDefaultChannel(PKG_O, UID_O)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1186 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1187 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel(), true, false); |
| 1188 | assertFalse(mHelper.onlyHasDefaultChannel(PKG_N_MR1, UID_N_MR1)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | @Test |
| 1192 | public void testCreateChannel_defaultChannelId() throws Exception { |
| 1193 | try { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1194 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, new NotificationChannel( |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1195 | NotificationChannel.DEFAULT_CHANNEL_ID, "ha", IMPORTANCE_HIGH), true, false); |
| 1196 | fail("Allowed to create default channel"); |
| 1197 | } catch (IllegalArgumentException e) { |
| 1198 | // pass |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | @Test |
| 1203 | public void testCreateChannel_alreadyExists() throws Exception { |
| 1204 | long[] vibration = new long[]{100, 67, 145, 156}; |
| 1205 | NotificationChannel channel = |
| 1206 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 1207 | channel.setVibrationPattern(vibration); |
| 1208 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1209 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1210 | |
| 1211 | NotificationChannel newChannel = new NotificationChannel( |
| 1212 | channel.getId(), channel.getName(), NotificationManager.IMPORTANCE_HIGH); |
| 1213 | newChannel.setVibrationPattern(new long[]{100}); |
| 1214 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1215 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, newChannel, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1216 | |
| 1217 | // Old settings not overridden |
| 1218 | compareChannels(channel, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1219 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, newChannel.getId(), false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | @Test |
| 1223 | public void testCreateChannel_noOverrideSound() throws Exception { |
| 1224 | Uri sound = new Uri.Builder().scheme("test").build(); |
| 1225 | final NotificationChannel channel = new NotificationChannel("id2", "name2", |
| 1226 | NotificationManager.IMPORTANCE_DEFAULT); |
| 1227 | channel.setSound(sound, mAudioAttributes); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1228 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1229 | assertEquals(sound, mHelper.getNotificationChannel( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1230 | PKG_N_MR1, UID_N_MR1, channel.getId(), false).getSound()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1231 | } |
| 1232 | |
| 1233 | @Test |
| 1234 | public void testPermanentlyDeleteChannels() throws Exception { |
| 1235 | NotificationChannel channel1 = |
| 1236 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 1237 | NotificationChannel channel2 = |
| 1238 | new NotificationChannel("id2", "name2", IMPORTANCE_LOW); |
| 1239 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1240 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false); |
| 1241 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, false, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1242 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1243 | mHelper.permanentlyDeleteNotificationChannels(PKG_N_MR1, UID_N_MR1); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1244 | |
| 1245 | // Only default channel remains |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1246 | 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] | 1247 | } |
| 1248 | |
| 1249 | @Test |
| 1250 | public void testDeleteGroup() throws Exception { |
| 1251 | NotificationChannelGroup notDeleted = new NotificationChannelGroup("not", "deleted"); |
| 1252 | NotificationChannelGroup deleted = new NotificationChannelGroup("totally", "deleted"); |
| 1253 | NotificationChannel nonGroupedNonDeletedChannel = |
| 1254 | new NotificationChannel("no group", "so not deleted", IMPORTANCE_HIGH); |
| 1255 | NotificationChannel groupedButNotDeleted = |
| 1256 | new NotificationChannel("not deleted", "belongs to notDeleted", IMPORTANCE_DEFAULT); |
| 1257 | groupedButNotDeleted.setGroup("not"); |
| 1258 | NotificationChannel groupedAndDeleted = |
| 1259 | new NotificationChannel("deleted", "belongs to deleted", IMPORTANCE_DEFAULT); |
| 1260 | groupedAndDeleted.setGroup("totally"); |
| 1261 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1262 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, notDeleted, true); |
| 1263 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, deleted, true); |
| 1264 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, nonGroupedNonDeletedChannel, true, false); |
| 1265 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, groupedAndDeleted, true, false); |
| 1266 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, groupedButNotDeleted, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1267 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1268 | mHelper.deleteNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, deleted.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1269 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1270 | assertNull(mHelper.getNotificationChannelGroup(deleted.getId(), PKG_N_MR1, UID_N_MR1)); |
| 1271 | assertNotNull(mHelper.getNotificationChannelGroup(notDeleted.getId(), PKG_N_MR1, UID_N_MR1)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1272 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1273 | assertNull(mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, groupedAndDeleted.getId(), false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1274 | compareChannels(groupedAndDeleted, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1275 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, groupedAndDeleted.getId(), true)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1276 | |
| 1277 | compareChannels(groupedButNotDeleted, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1278 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, groupedButNotDeleted.getId(), false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1279 | compareChannels(nonGroupedNonDeletedChannel, mHelper.getNotificationChannel( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1280 | PKG_N_MR1, UID_N_MR1, nonGroupedNonDeletedChannel.getId(), false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1281 | |
| 1282 | // notDeleted |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1283 | assertEquals(1, mHelper.getNotificationChannelGroups(PKG_N_MR1, UID_N_MR1).size()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1284 | |
| 1285 | verify(mHandler, never()).requestSort(); |
| 1286 | } |
| 1287 | |
| 1288 | @Test |
| 1289 | public void testOnUserRemoved() throws Exception { |
| 1290 | int[] user0Uids = {98, 235, 16, 3782}; |
| 1291 | int[] user1Uids = new int[user0Uids.length]; |
| 1292 | for (int i = 0; i < user0Uids.length; i++) { |
| 1293 | user1Uids[i] = UserHandle.PER_USER_RANGE + user0Uids[i]; |
| 1294 | |
| 1295 | final ApplicationInfo legacy = new ApplicationInfo(); |
| 1296 | legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1; |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1297 | when(mPm.getApplicationInfoAsUser(eq(PKG_N_MR1), anyInt(), anyInt())).thenReturn(legacy); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1298 | |
| 1299 | // 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] | 1300 | mHelper.getImportance(PKG_N_MR1, user0Uids[i]); |
| 1301 | mHelper.getImportance(PKG_N_MR1, user1Uids[i]); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | mHelper.onUserRemoved(1); |
| 1305 | |
| 1306 | // user 0 records remain |
| 1307 | for (int i = 0; i < user0Uids.length; i++) { |
| 1308 | assertEquals(1, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1309 | mHelper.getNotificationChannels(PKG_N_MR1, user0Uids[i], false).getList().size()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1310 | } |
| 1311 | // user 1 records are gone |
| 1312 | for (int i = 0; i < user1Uids.length; i++) { |
| 1313 | assertEquals(0, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1314 | mHelper.getNotificationChannels(PKG_N_MR1, user1Uids[i], false).getList().size()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | @Test |
| 1319 | public void testOnPackageChanged_packageRemoval() throws Exception { |
| 1320 | // Deleted |
| 1321 | NotificationChannel channel1 = |
| 1322 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1323 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1324 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1325 | mHelper.onPackagesChanged(true, UserHandle.USER_SYSTEM, new String[]{PKG_N_MR1}, new int[]{ |
| 1326 | UID_N_MR1}); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1327 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1328 | 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] | 1329 | |
| 1330 | // Not deleted |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1331 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1332 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1333 | mHelper.onPackagesChanged(false, UserHandle.USER_SYSTEM, new String[]{PKG_N_MR1}, new int[]{ |
| 1334 | UID_N_MR1}); |
| 1335 | 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] | 1336 | } |
| 1337 | |
| 1338 | @Test |
| 1339 | public void testOnPackageChanged_packageRemoval_importance() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1340 | mHelper.setImportance(PKG_N_MR1, UID_N_MR1, NotificationManager.IMPORTANCE_HIGH); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1341 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1342 | mHelper.onPackagesChanged(true, UserHandle.USER_SYSTEM, new String[]{PKG_N_MR1}, new int[]{ |
| 1343 | UID_N_MR1}); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1344 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1345 | assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, mHelper.getImportance(PKG_N_MR1, |
| 1346 | UID_N_MR1)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1347 | } |
| 1348 | |
| 1349 | @Test |
| 1350 | public void testOnPackageChanged_packageRemoval_groups() throws Exception { |
| 1351 | NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1352 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1353 | NotificationChannelGroup ncg2 = new NotificationChannelGroup("group2", "name2"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1354 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg2, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1355 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1356 | mHelper.onPackagesChanged(true, UserHandle.USER_SYSTEM, new String[]{PKG_N_MR1}, new int[]{ |
| 1357 | UID_N_MR1}); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1358 | |
| 1359 | assertEquals(0, |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1360 | mHelper.getNotificationChannelGroups(PKG_N_MR1, UID_N_MR1, true, true).getList().size()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | @Test |
| 1364 | public void testOnPackageChange_downgradeTargetSdk() throws Exception { |
| 1365 | // create channel as api 26 |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1366 | mHelper.createNotificationChannel(PKG_O, UID_O, getChannel(), true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1367 | |
| 1368 | // install new app version targeting 25 |
| 1369 | final ApplicationInfo legacy = new ApplicationInfo(); |
| 1370 | legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1; |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1371 | when(mPm.getApplicationInfoAsUser(eq(PKG_O), anyInt(), anyInt())).thenReturn(legacy); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1372 | mHelper.onPackagesChanged( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1373 | false, UserHandle.USER_SYSTEM, new String[]{PKG_O}, new int[]{UID_O}); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1374 | |
| 1375 | // make sure the default channel was readded |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1376 | //assertEquals(2, mHelper.getNotificationChannels(PKG_O, UID_O, false).getList().size()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1377 | assertNotNull(mHelper.getNotificationChannel( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1378 | PKG_O, UID_O, NotificationChannel.DEFAULT_CHANNEL_ID, false)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | @Test |
| 1382 | public void testRecordDefaults() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1383 | assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, mHelper.getImportance(PKG_N_MR1, |
| 1384 | UID_N_MR1)); |
| 1385 | assertEquals(true, mHelper.canShowBadge(PKG_N_MR1, UID_N_MR1)); |
| 1386 | 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] | 1387 | } |
| 1388 | |
| 1389 | @Test |
| 1390 | public void testCreateGroup() throws Exception { |
| 1391 | NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1392 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true); |
| 1393 | assertEquals(ncg, mHelper.getNotificationChannelGroups(PKG_N_MR1, UID_N_MR1).iterator().next()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1394 | verify(mHandler, never()).requestSort(); |
| 1395 | } |
| 1396 | |
| 1397 | @Test |
| 1398 | public void testCannotCreateChannel_badGroup() throws Exception { |
| 1399 | NotificationChannel channel1 = |
| 1400 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 1401 | channel1.setGroup("garbage"); |
| 1402 | try { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1403 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1404 | fail("Created a channel with a bad group"); |
| 1405 | } catch (IllegalArgumentException e) { |
| 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | @Test |
| 1410 | public void testCannotCreateChannel_goodGroup() throws Exception { |
| 1411 | NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1412 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1413 | NotificationChannel channel1 = |
| 1414 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 1415 | channel1.setGroup(ncg.getId()); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1416 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1417 | |
| 1418 | assertEquals(ncg.getId(), |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1419 | mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1.getId(), false).getGroup()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | @Test |
| 1423 | public void testGetChannelGroups() throws Exception { |
| 1424 | NotificationChannelGroup unused = new NotificationChannelGroup("unused", "s"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1425 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, unused, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1426 | NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1427 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1428 | NotificationChannelGroup ncg2 = new NotificationChannelGroup("group2", "name2"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1429 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg2, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1430 | |
| 1431 | NotificationChannel channel1 = |
| 1432 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 1433 | channel1.setGroup(ncg.getId()); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1434 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1435 | NotificationChannel channel1a = |
| 1436 | new NotificationChannel("id1a", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 1437 | channel1a.setGroup(ncg.getId()); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1438 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1a, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1439 | |
| 1440 | NotificationChannel channel2 = |
| 1441 | new NotificationChannel("id2", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 1442 | channel2.setGroup(ncg2.getId()); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1443 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1444 | |
| 1445 | NotificationChannel channel3 = |
| 1446 | new NotificationChannel("id3", "name1", NotificationManager.IMPORTANCE_HIGH); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1447 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1448 | |
| 1449 | List<NotificationChannelGroup> actual = |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1450 | mHelper.getNotificationChannelGroups(PKG_N_MR1, UID_N_MR1, true, true).getList(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1451 | assertEquals(3, actual.size()); |
| 1452 | for (NotificationChannelGroup group : actual) { |
| 1453 | if (group.getId() == null) { |
| 1454 | assertEquals(2, group.getChannels().size()); // misc channel too |
| 1455 | assertTrue(channel3.getId().equals(group.getChannels().get(0).getId()) |
| 1456 | || channel3.getId().equals(group.getChannels().get(1).getId())); |
| 1457 | } else if (group.getId().equals(ncg.getId())) { |
| 1458 | assertEquals(2, group.getChannels().size()); |
| 1459 | if (group.getChannels().get(0).getId().equals(channel1.getId())) { |
| 1460 | assertTrue(group.getChannels().get(1).getId().equals(channel1a.getId())); |
| 1461 | } else if (group.getChannels().get(0).getId().equals(channel1a.getId())) { |
| 1462 | assertTrue(group.getChannels().get(1).getId().equals(channel1.getId())); |
| 1463 | } else { |
| 1464 | fail("expected channel not found"); |
| 1465 | } |
| 1466 | } else if (group.getId().equals(ncg2.getId())) { |
| 1467 | assertEquals(1, group.getChannels().size()); |
| 1468 | assertEquals(channel2.getId(), group.getChannels().get(0).getId()); |
| 1469 | } |
| 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | @Test |
| 1474 | public void testGetChannelGroups_noSideEffects() throws Exception { |
| 1475 | NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1476 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1477 | |
| 1478 | NotificationChannel channel1 = |
| 1479 | new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); |
| 1480 | channel1.setGroup(ncg.getId()); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1481 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false); |
| 1482 | mHelper.getNotificationChannelGroups(PKG_N_MR1, UID_N_MR1, true, true).getList(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1483 | |
| 1484 | channel1.setImportance(IMPORTANCE_LOW); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1485 | mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1486 | |
| 1487 | List<NotificationChannelGroup> actual = |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1488 | mHelper.getNotificationChannelGroups(PKG_N_MR1, UID_N_MR1, true, true).getList(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1489 | |
| 1490 | assertEquals(2, actual.size()); |
| 1491 | for (NotificationChannelGroup group : actual) { |
| 1492 | if (Objects.equals(group.getId(), ncg.getId())) { |
| 1493 | assertEquals(1, group.getChannels().size()); |
| 1494 | } |
| 1495 | } |
| 1496 | } |
| 1497 | |
| 1498 | @Test |
| 1499 | public void testCreateChannel_updateName() throws Exception { |
| 1500 | NotificationChannel nc = new NotificationChannel("id", "hello", IMPORTANCE_DEFAULT); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1501 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, nc, true, false); |
| 1502 | NotificationChannel actual = mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "id", false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1503 | assertEquals("hello", actual.getName()); |
| 1504 | |
| 1505 | nc = new NotificationChannel("id", "goodbye", IMPORTANCE_HIGH); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1506 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, nc, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1507 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1508 | actual = mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "id", false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1509 | assertEquals("goodbye", actual.getName()); |
| 1510 | assertEquals(IMPORTANCE_DEFAULT, actual.getImportance()); |
| 1511 | |
| 1512 | verify(mHandler, times(1)).requestSort(); |
| 1513 | } |
| 1514 | |
| 1515 | @Test |
| 1516 | public void testCreateChannel_addToGroup() throws Exception { |
| 1517 | NotificationChannelGroup group = new NotificationChannelGroup("group", ""); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1518 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1519 | NotificationChannel nc = new NotificationChannel("id", "hello", IMPORTANCE_DEFAULT); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1520 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, nc, true, false); |
| 1521 | NotificationChannel actual = mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "id", false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1522 | assertNull(actual.getGroup()); |
| 1523 | |
| 1524 | nc = new NotificationChannel("id", "hello", IMPORTANCE_HIGH); |
| 1525 | nc.setGroup(group.getId()); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1526 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, nc, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1527 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1528 | actual = mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "id", false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1529 | assertNotNull(actual.getGroup()); |
| 1530 | assertEquals(IMPORTANCE_DEFAULT, actual.getImportance()); |
| 1531 | |
| 1532 | verify(mHandler, times(1)).requestSort(); |
| 1533 | } |
| 1534 | |
| 1535 | @Test |
| 1536 | public void testDumpChannelsJson() throws Exception { |
| 1537 | final ApplicationInfo upgrade = new ApplicationInfo(); |
| 1538 | upgrade.targetSdkVersion = Build.VERSION_CODES.O; |
| 1539 | try { |
| 1540 | when(mPm.getApplicationInfoAsUser( |
| 1541 | anyString(), anyInt(), anyInt())).thenReturn(upgrade); |
| 1542 | } catch (PackageManager.NameNotFoundException e) { |
| 1543 | } |
| 1544 | ArrayMap<String, Integer> expectedChannels = new ArrayMap<>(); |
| 1545 | int numPackages = ThreadLocalRandom.current().nextInt(1, 5); |
| 1546 | for (int i = 0; i < numPackages; i++) { |
| 1547 | String pkgName = "pkg" + i; |
| 1548 | int numChannels = ThreadLocalRandom.current().nextInt(1, 10); |
| 1549 | for (int j = 0; j < numChannels; j++) { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1550 | mHelper.createNotificationChannel(pkgName, UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1551 | new NotificationChannel("" + j, "a", IMPORTANCE_HIGH), true, false); |
| 1552 | } |
| 1553 | expectedChannels.put(pkgName, numChannels); |
| 1554 | } |
| 1555 | |
| 1556 | // delete the first channel of the first package |
| 1557 | String pkg = expectedChannels.keyAt(0); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1558 | mHelper.deleteNotificationChannel("pkg" + 0, UID_N_MR1, "0"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1559 | // dump should not include deleted channels |
| 1560 | int count = expectedChannels.get(pkg); |
| 1561 | expectedChannels.put(pkg, count - 1); |
| 1562 | |
| 1563 | JSONArray actual = mHelper.dumpChannelsJson(new NotificationManagerService.DumpFilter()); |
| 1564 | assertEquals(numPackages, actual.length()); |
| 1565 | for (int i = 0; i < numPackages; i++) { |
| 1566 | JSONObject object = actual.getJSONObject(i); |
| 1567 | assertTrue(expectedChannels.containsKey(object.get("packageName"))); |
| 1568 | assertEquals(expectedChannels.get(object.get("packageName")).intValue(), |
| 1569 | object.getInt("channelCount")); |
| 1570 | } |
| 1571 | } |
| 1572 | |
| 1573 | @Test |
| 1574 | public void testBadgingOverrideTrue() throws Exception { |
| 1575 | Secure.putIntForUser(getContext().getContentResolver(), |
| 1576 | Secure.NOTIFICATION_BADGING, 1, |
| 1577 | USER.getIdentifier()); |
| 1578 | mHelper.updateBadgingEnabled(); // would be called by settings observer |
| 1579 | assertTrue(mHelper.badgingEnabled(USER)); |
| 1580 | } |
| 1581 | |
| 1582 | @Test |
| 1583 | public void testBadgingOverrideFalse() throws Exception { |
| 1584 | Secure.putIntForUser(getContext().getContentResolver(), |
| 1585 | Secure.NOTIFICATION_BADGING, 0, |
| 1586 | USER.getIdentifier()); |
| 1587 | mHelper.updateBadgingEnabled(); // would be called by settings observer |
| 1588 | assertFalse(mHelper.badgingEnabled(USER)); |
| 1589 | } |
| 1590 | |
| 1591 | @Test |
| 1592 | public void testBadgingForUserAll() throws Exception { |
| 1593 | try { |
| 1594 | mHelper.badgingEnabled(UserHandle.ALL); |
| 1595 | } catch (Exception e) { |
| 1596 | fail("just don't throw"); |
| 1597 | } |
| 1598 | } |
| 1599 | |
| 1600 | @Test |
| 1601 | public void testBadgingOverrideUserIsolation() throws Exception { |
| 1602 | Secure.putIntForUser(getContext().getContentResolver(), |
| 1603 | Secure.NOTIFICATION_BADGING, 0, |
| 1604 | USER.getIdentifier()); |
| 1605 | Secure.putIntForUser(getContext().getContentResolver(), |
| 1606 | Secure.NOTIFICATION_BADGING, 1, |
| 1607 | USER2.getIdentifier()); |
| 1608 | mHelper.updateBadgingEnabled(); // would be called by settings observer |
| 1609 | assertFalse(mHelper.badgingEnabled(USER)); |
| 1610 | assertTrue(mHelper.badgingEnabled(USER2)); |
| 1611 | } |
| 1612 | |
| 1613 | @Test |
| 1614 | public void testOnLocaleChanged_updatesDefaultChannels() throws Exception { |
| 1615 | String newLabel = "bananas!"; |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1616 | final NotificationChannel defaultChannel = mHelper.getNotificationChannel(PKG_N_MR1, |
| 1617 | UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1618 | NotificationChannel.DEFAULT_CHANNEL_ID, false); |
| 1619 | assertFalse(newLabel.equals(defaultChannel.getName())); |
| 1620 | |
| 1621 | Resources res = mock(Resources.class); |
| 1622 | when(mContext.getResources()).thenReturn(res); |
| 1623 | when(res.getString(com.android.internal.R.string.default_notification_channel_label)) |
| 1624 | .thenReturn(newLabel); |
| 1625 | |
| 1626 | mHelper.onLocaleChanged(mContext, USER.getIdentifier()); |
| 1627 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1628 | assertEquals(newLabel, mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1629 | NotificationChannel.DEFAULT_CHANNEL_ID, false).getName()); |
| 1630 | } |
| 1631 | |
| 1632 | @Test |
| 1633 | public void testIsGroupBlocked_noGroup() throws Exception { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1634 | assertFalse(mHelper.isGroupBlocked(PKG_N_MR1, UID_N_MR1, null)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1635 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1636 | assertFalse(mHelper.isGroupBlocked(PKG_N_MR1, UID_N_MR1, "non existent group")); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1637 | } |
| 1638 | |
| 1639 | @Test |
| 1640 | public void testIsGroupBlocked_notBlocked() throws Exception { |
| 1641 | NotificationChannelGroup group = new NotificationChannelGroup("id", "name"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1642 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1643 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1644 | assertFalse(mHelper.isGroupBlocked(PKG_N_MR1, UID_N_MR1, group.getId())); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1645 | } |
| 1646 | |
| 1647 | @Test |
| 1648 | public void testIsGroupBlocked_blocked() throws Exception { |
| 1649 | NotificationChannelGroup group = new NotificationChannelGroup("id", "name"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1650 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1651 | group.setBlocked(true); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1652 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1653 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1654 | assertTrue(mHelper.isGroupBlocked(PKG_N_MR1, UID_N_MR1, group.getId())); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1655 | } |
| 1656 | |
| 1657 | @Test |
| 1658 | public void testIsGroup_appCannotResetBlock() throws Exception { |
| 1659 | NotificationChannelGroup group = new NotificationChannelGroup("id", "name"); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1660 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1661 | NotificationChannelGroup group2 = group.clone(); |
| 1662 | group2.setBlocked(true); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1663 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group2, false); |
| 1664 | assertTrue(mHelper.isGroupBlocked(PKG_N_MR1, UID_N_MR1, group.getId())); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1665 | |
| 1666 | NotificationChannelGroup group3 = group.clone(); |
| 1667 | group3.setBlocked(false); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1668 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group3, true); |
| 1669 | assertTrue(mHelper.isGroupBlocked(PKG_N_MR1, UID_N_MR1, group.getId())); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1670 | } |
| 1671 | |
| 1672 | @Test |
| 1673 | public void testGetNotificationChannelGroupWithChannels() throws Exception { |
| 1674 | NotificationChannelGroup group = new NotificationChannelGroup("group", ""); |
| 1675 | NotificationChannelGroup other = new NotificationChannelGroup("something else", ""); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1676 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group, true); |
| 1677 | mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, other, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1678 | |
| 1679 | NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_DEFAULT); |
| 1680 | a.setGroup(group.getId()); |
| 1681 | NotificationChannel b = new NotificationChannel("b", "b", IMPORTANCE_DEFAULT); |
| 1682 | b.setGroup(other.getId()); |
| 1683 | NotificationChannel c = new NotificationChannel("c", "c", IMPORTANCE_DEFAULT); |
| 1684 | c.setGroup(group.getId()); |
| 1685 | NotificationChannel d = new NotificationChannel("d", "d", IMPORTANCE_DEFAULT); |
| 1686 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1687 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, a, true, false); |
| 1688 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, b, true, false); |
| 1689 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, c, true, false); |
| 1690 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, d, true, false); |
| 1691 | mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, c.getId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1692 | |
| 1693 | NotificationChannelGroup retrieved = mHelper.getNotificationChannelGroupWithChannels( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1694 | PKG_N_MR1, UID_N_MR1, group.getId(), true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1695 | assertEquals(2, retrieved.getChannels().size()); |
| 1696 | compareChannels(a, findChannel(retrieved.getChannels(), a.getId())); |
| 1697 | compareChannels(c, findChannel(retrieved.getChannels(), c.getId())); |
| 1698 | |
| 1699 | retrieved = mHelper.getNotificationChannelGroupWithChannels( |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1700 | PKG_N_MR1, UID_N_MR1, group.getId(), false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1701 | assertEquals(1, retrieved.getChannels().size()); |
| 1702 | compareChannels(a, findChannel(retrieved.getChannels(), a.getId())); |
| 1703 | } |
| 1704 | |
| 1705 | @Test |
| 1706 | public void testAndroidPkgCannotBypassDnd_creation() { |
| 1707 | NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW); |
| 1708 | test.setBypassDnd(true); |
| 1709 | |
| 1710 | mHelper.createNotificationChannel(SYSTEM_PKG, SYSTEM_UID, test, true, false); |
| 1711 | |
| 1712 | assertFalse(mHelper.getNotificationChannel(SYSTEM_PKG, SYSTEM_UID, "A", false) |
| 1713 | .canBypassDnd()); |
| 1714 | } |
| 1715 | |
| 1716 | @Test |
| 1717 | public void testDndPkgCanBypassDnd_creation() { |
| 1718 | NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW); |
| 1719 | test.setBypassDnd(true); |
| 1720 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1721 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, test, true, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1722 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1723 | assertTrue(mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "A", false).canBypassDnd()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1724 | } |
| 1725 | |
| 1726 | @Test |
| 1727 | public void testNormalPkgCannotBypassDnd_creation() { |
| 1728 | NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW); |
| 1729 | test.setBypassDnd(true); |
| 1730 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1731 | mHelper.createNotificationChannel(PKG_N_MR1, 1000, test, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1732 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1733 | assertFalse(mHelper.getNotificationChannel(PKG_N_MR1, 1000, "A", false).canBypassDnd()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1734 | } |
| 1735 | |
| 1736 | @Test |
| 1737 | public void testAndroidPkgCannotBypassDnd_update() throws Exception { |
| 1738 | NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW); |
| 1739 | mHelper.createNotificationChannel(SYSTEM_PKG, SYSTEM_UID, test, true, false); |
| 1740 | |
| 1741 | NotificationChannel update = new NotificationChannel("A", "a", IMPORTANCE_LOW); |
| 1742 | update.setBypassDnd(true); |
| 1743 | mHelper.createNotificationChannel(SYSTEM_PKG, SYSTEM_UID, update, true, false); |
| 1744 | |
| 1745 | assertFalse(mHelper.getNotificationChannel(SYSTEM_PKG, SYSTEM_UID, "A", false) |
| 1746 | .canBypassDnd()); |
| 1747 | } |
| 1748 | |
| 1749 | @Test |
| 1750 | public void testDndPkgCanBypassDnd_update() throws Exception { |
| 1751 | NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1752 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, test, true, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1753 | |
| 1754 | NotificationChannel update = new NotificationChannel("A", "a", IMPORTANCE_LOW); |
| 1755 | update.setBypassDnd(true); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1756 | mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, update, true, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1757 | |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1758 | assertTrue(mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "A", false).canBypassDnd()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | @Test |
| 1762 | public void testNormalPkgCannotBypassDnd_update() { |
| 1763 | NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1764 | mHelper.createNotificationChannel(PKG_N_MR1, 1000, test, true, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1765 | NotificationChannel update = new NotificationChannel("A", "a", IMPORTANCE_LOW); |
| 1766 | update.setBypassDnd(true); |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1767 | mHelper.createNotificationChannel(PKG_N_MR1, 1000, update, true, false); |
| 1768 | assertFalse(mHelper.getNotificationChannel(PKG_N_MR1, 1000, "A", false).canBypassDnd()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1769 | } |
| 1770 | |
| 1771 | @Test |
| 1772 | public void testGetBlockedAppCount_noApps() { |
| 1773 | assertEquals(0, mHelper.getBlockedAppCount(0)); |
| 1774 | } |
| 1775 | |
| 1776 | @Test |
| 1777 | public void testGetBlockedAppCount_noAppsForUserId() { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1778 | mHelper.setEnabled(PKG_N_MR1, 100, false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1779 | assertEquals(0, mHelper.getBlockedAppCount(9)); |
| 1780 | } |
| 1781 | |
| 1782 | @Test |
| 1783 | public void testGetBlockedAppCount_appsForUserId() { |
Aaron Heuckroth | 9ae59f8 | 2018-07-13 12:23:36 -0400 | [diff] [blame] | 1784 | mHelper.setEnabled(PKG_N_MR1, 1020, false); |
| 1785 | mHelper.setEnabled(PKG_N_MR1, 1030, false); |
| 1786 | mHelper.setEnabled(PKG_N_MR1, 1060, false); |
| 1787 | mHelper.setEnabled(PKG_N_MR1, 1000, true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1788 | assertEquals(3, mHelper.getBlockedAppCount(0)); |
| 1789 | } |
| 1790 | } |