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