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